cfgircsounds.cpp
1 //------------------------------------------------------------------------------
2 // cfgircsounds.cpp
3 //
4 // Copyright (C) 2011 "Zalewa" <zalewapl@gmail.com>
5 //------------------------------------------------------------------------------
6 #include "cfgircsounds.h"
7 #include "ui_cfgircsounds.h"
8 #include "irc/configuration/ircconfig.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  {
73  QSound::play(path);
74  }
75 }
76 
78 {
79  d->cbNicknameUsed->setChecked(gIRCConfig.sounds.bUseNicknameUsedSound);
80  d->cbPrivateMessage->setChecked(gIRCConfig.sounds.bUsePrivateMessageReceivedSound);
81 
82  d->leNicknameUsed->setText(gIRCConfig.sounds.nicknameUsedSound);
83  d->lePrivateMessage->setText(gIRCConfig.sounds.privateMessageReceivedSound);
84 }
85 
87 {
88  gIRCConfig.sounds.bUseNicknameUsedSound = d->cbNicknameUsed->isChecked();
89  gIRCConfig.sounds.nicknameUsedSound = d->leNicknameUsed->text().trimmed();
90 
91  gIRCConfig.sounds.bUsePrivateMessageReceivedSound = d->cbPrivateMessage->isChecked();
92  gIRCConfig.sounds.privateMessageReceivedSound = d->lePrivateMessage->text().trimmed();
93 }
94 
95 void CFGIRCSounds::setPath(QLineEdit* pLineEdit, const QString& path)
96 {
97  QString trimmedPath = path.trimmed();
98  if (!trimmedPath.isEmpty())
99  {
100  pLineEdit->setText(trimmedPath);
101  }
102  emit validationRequested();
103 }
104 
106 {
107  bool error = false;
108  QString nicknameUsedError;
109  if (d->cbNicknameUsed->isChecked())
110  nicknameUsedError = validateFilePath(d->leNicknameUsed->text().trimmed());
111  d->lblNicknameUsedWarning->setVisible(!nicknameUsedError.isEmpty());
112  d->lblNicknameUsedWarning->setToolTip(nicknameUsedError);
113  error = error || !nicknameUsedError.isEmpty();
114 
115  QString privateMessageError;
116  if (d->cbPrivateMessage->isChecked())
117  privateMessageError = validateFilePath(d->lePrivateMessage->text().trimmed());
118  d->lblPrivateMessageWarning->setVisible(!privateMessageError.isEmpty());
119  d->lblPrivateMessageWarning->setToolTip(privateMessageError);
120  error = error || !privateMessageError.isEmpty();
121 
122  return !error ? VALIDATION_OK : VALIDATION_ERROR;
123 }
124 
125 QString CFGIRCSounds::validateFilePath(const QString &path) const
126 {
127  if (path.trimmed().isEmpty())
128  {
129  return tr("No path specified.");
130  }
131 
132  QFileInfo fileInfo(path.trimmed());
133  if (!fileInfo.exists())
134  {
135  return tr("File doesn't exist.");
136  }
137 
138  if (!fileInfo.isFile())
139  {
140  return tr("This is not a file.");
141  }
142  return "";
143 }
Validation
Result of validate()
Definition: configpage.h:49
void validationRequested()
Request that the page should be (re-)validated.
void saveSettings()
Reimplement this to write settings to config from widgets.
Validation detected no problems.
Definition: configpage.h:52
void readSettings()
Reimplement this to read settings from config into widgets.
Validation validate()
Validate settings on this page.
Validation detected at least one problem.
Definition: configpage.h:54
Base class for configuration pages.
Definition: configpage.h:43