cfgircsounds.cpp
1 //------------------------------------------------------------------------------
2 // cfgircsounds.cpp
3 //
4 // Copyright (C) 2011 "Zalewa" <zalewapl@gmail.com>
5 //------------------------------------------------------------------------------
6 #include "cfgircsounds.h"
7 #include "irc/configuration/ircconfig.h"
8 #include "ui_cfgircsounds.h"
9 
10 #include <QFile>
11 #include <QFileDialog>
12 #include <QSound>
13 
14 DClass<CFGIRCSounds> : public Ui::CFGIRCSounds
15 {
16 };
17 
18 DPointered(CFGIRCSounds)
19 
20 CFGIRCSounds::CFGIRCSounds(QWidget *parent)
21  : ConfigPage(parent)
22 {
23  d->setupUi(this);
24  d->lblNicknameUsedWarning->hide();
25  d->lblPrivateMessageWarning->hide();
26 
27  this->connect(d->cbNicknameUsed, SIGNAL(toggled(bool)),
28  SIGNAL(validationRequested()));
29  this->connect(d->leNicknameUsed, SIGNAL(editingFinished()),
30  SIGNAL(validationRequested()));
31  this->connect(d->cbPrivateMessage, SIGNAL(toggled(bool)),
32  SIGNAL(validationRequested()));
33  this->connect(d->lePrivateMessage, SIGNAL(editingFinished()),
34  SIGNAL(validationRequested()));
35 }
36 
37 CFGIRCSounds::~CFGIRCSounds()
38 {
39 }
40 
41 void CFGIRCSounds::browseNicknameUsed()
42 {
43  setPath(d->leNicknameUsed, getPathToWav());
44 }
45 
46 void CFGIRCSounds::browsePrivateMessage()
47 {
48  setPath(d->lePrivateMessage, getPathToWav());
49 }
50 
51 QString CFGIRCSounds::getPathToWav()
52 {
53  return QFileDialog::getOpenFileName(this, tr("Pick Sound File"),
54  QString(),
55  tr("WAVE (*.wav)"));
56 }
57 
58 void CFGIRCSounds::playNicknameUsed()
59 {
60  playSound(d->leNicknameUsed->text());
61 }
62 
63 void CFGIRCSounds::playPrivateMessage()
64 {
65  playSound(d->lePrivateMessage->text());
66 }
67 
68 void CFGIRCSounds::playSound(const QString &path) const
69 {
70  QFile file(path);
71  if (file.exists())
72  QSound::play(path);
73 }
74 
76 {
77  d->cbNicknameUsed->setChecked(gIRCConfig.sounds.bUseNicknameUsedSound);
78  d->cbPrivateMessage->setChecked(gIRCConfig.sounds.bUsePrivateMessageReceivedSound);
79 
80  d->leNicknameUsed->setText(gIRCConfig.sounds.nicknameUsedSound);
81  d->lePrivateMessage->setText(gIRCConfig.sounds.privateMessageReceivedSound);
82 }
83 
85 {
86  gIRCConfig.sounds.bUseNicknameUsedSound = d->cbNicknameUsed->isChecked();
87  gIRCConfig.sounds.nicknameUsedSound = d->leNicknameUsed->text().trimmed();
88 
89  gIRCConfig.sounds.bUsePrivateMessageReceivedSound = d->cbPrivateMessage->isChecked();
90  gIRCConfig.sounds.privateMessageReceivedSound = d->lePrivateMessage->text().trimmed();
91 }
92 
93 void CFGIRCSounds::setPath(QLineEdit *pLineEdit, const QString &path)
94 {
95  QString trimmedPath = path.trimmed();
96  if (!trimmedPath.isEmpty())
97  pLineEdit->setText(trimmedPath);
98  emit validationRequested();
99 }
100 
102 {
103  bool error = false;
104  QString nicknameUsedError;
105  if (d->cbNicknameUsed->isChecked())
106  nicknameUsedError = validateFilePath(d->leNicknameUsed->text().trimmed());
107  d->lblNicknameUsedWarning->setVisible(!nicknameUsedError.isEmpty());
108  d->lblNicknameUsedWarning->setToolTip(nicknameUsedError);
109  error = error || !nicknameUsedError.isEmpty();
110 
111  QString privateMessageError;
112  if (d->cbPrivateMessage->isChecked())
113  privateMessageError = validateFilePath(d->lePrivateMessage->text().trimmed());
114  d->lblPrivateMessageWarning->setVisible(!privateMessageError.isEmpty());
115  d->lblPrivateMessageWarning->setToolTip(privateMessageError);
116  error = error || !privateMessageError.isEmpty();
117 
118  return !error ? VALIDATION_OK : VALIDATION_ERROR;
119 }
120 
121 QString CFGIRCSounds::validateFilePath(const QString &path) const
122 {
123  if (path.trimmed().isEmpty())
124  return tr("No path specified.");
125 
126  QFileInfo fileInfo(path.trimmed());
127  if (!fileInfo.exists())
128  return tr("File doesn't exist.");
129 
130  if (!fileInfo.isFile())
131  return tr("This is not a file.");
132  return "";
133 }