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