1 /***************************************************************************
2 *                                                                         *
3 *   This program is free software; you can redistribute it and/or modify  *
4 *   it under the terms of the GNU General Public License as published by  *
5 *   the Free Software Foundation; either version 3 of the License, or     *
6 *   (at your option) any later version.                                   *
7 *                                                                         *
8 ***************************************************************************/
9 
10 #include "SettingsNotification.h"
11 
12 #include "WulforSettings.h"
13 #include "WulforUtil.h"
14 #include "Notification.h"
15 #include "ShellCommandRunner.h"
16 
17 #include <QFileDialog>
18 #include <QSound>
19 #include <QDir>
20 
SettingsNotification(QWidget * parent)21 SettingsNotification::SettingsNotification(QWidget *parent) :
22     QWidget(parent)
23 {
24     setupUi(this);
25 
26     init();
27 }
28 
init()29 void SettingsNotification::init(){
30     WulforUtil *WU = WulforUtil::getInstance();
31 
32     {//Text
33         checkBox_EXIT_CONFIRM->setChecked(WBGET(WB_EXIT_CONFIRM));
34 
35         groupBox->setChecked(WBGET(WB_NOTIFY_ENABLED));
36 
37         unsigned emap = static_cast<unsigned>(WIGET(WI_NOTIFY_EVENTMAP));
38 
39         checkBox_NICKSAY->setChecked(emap & Notification::NICKSAY);
40         checkBox_ANY->setChecked(emap & Notification::ANY);
41         checkBox_PM->setChecked(emap & Notification::PM);
42         checkBox_TRDONE->setChecked(emap & Notification::TRANSFER);
43         checkBox_FAVJOIN->setChecked(emap & Notification::FAVORITE);
44         checkBox_MWACTIVE->setChecked(WBGET(WB_NOTIFY_SHOW_ON_ACTIVE));
45         checkBox_MWVISIBLE->setChecked(WBGET(WB_NOTIFY_SHOW_ON_VISIBLE));
46         checkBox_CHICON->setChecked(WBGET(WB_NOTIFY_CH_ICON_ALWAYS));
47 
48         if (WBGET(WB_NOTIFY_SHOW_ON_ACTIVE)){
49             checkBox_MWVISIBLE->setChecked(true);
50             checkBox_MWVISIBLE->setDisabled(true);
51         }
52 
53         comboBox->setCurrentIndex(WIGET(WI_NOTIFY_MODULE));
54     }
55     {//Sound
56         QString encoded = WSGET(WS_NOTIFY_SOUNDS);
57         QString decoded = QByteArray::fromBase64(encoded.toUtf8());
58         QStringList sounds = decoded.split("\n");
59 
60         if (sounds.size() == 4){
61             lineEdit_SNDNICKSAY->setText(sounds.at(0));
62             lineEdit_SNDPM->setText(sounds.at(1));
63             lineEdit_SNDTRDONE->setText(sounds.at(2));
64             lineEdit_FAV->setText(sounds.at(3));
65         }
66 
67         groupBox_SND->setChecked(WBGET(WB_NOTIFY_SND_ENABLED));
68         groupBox_SNDCMD->setChecked(WBGET(WB_NOTIFY_SND_EXTERNAL));
69 
70         lineEdit_SNDCMD->setText(WSGET(WS_NOTIFY_SND_CMD));
71 
72         unsigned emap = static_cast<unsigned>(WIGET(WI_NOTIFY_SNDMAP));
73 
74         groupBox_NICK->setChecked(emap & Notification::NICKSAY);
75         groupBox_PM->setChecked(emap & Notification::PM);
76         groupBox_TR->setChecked(emap & Notification::TRANSFER);
77         groupBox_FAV->setChecked(emap & Notification::FAVORITE);
78 
79         checkBox_ACTIVEPM->setChecked(WBGET("notification/play-sound-with-active-pm", true));
80     }
81 
82     toolButton_BRWNICK->setIcon(WU->getPixmap(WulforUtil::eiFOLDER_BLUE));
83     toolButton_BRWPM->setIcon(WU->getPixmap(WulforUtil::eiFOLDER_BLUE));
84     toolButton_BRWTR->setIcon(WU->getPixmap(WulforUtil::eiFOLDER_BLUE));
85     toolButton_BRWFAV->setIcon(WU->getPixmap(WulforUtil::eiFOLDER_BLUE));
86 
87     connect(toolButton_BRWNICK, SIGNAL(clicked()), this, SLOT(slotBrowseFile()));
88     connect(toolButton_BRWPM,   SIGNAL(clicked()), this, SLOT(slotBrowseFile()));
89     connect(toolButton_BRWTR,   SIGNAL(clicked()), this, SLOT(slotBrowseFile()));
90     connect(toolButton_BRWFAV,  SIGNAL(clicked()), this, SLOT(slotBrowseFile()));
91 
92     connect(pushButton_TESTNICKSAY, SIGNAL(clicked()), this, SLOT(slotTest()));
93     connect(pushButton_TESTPM,      SIGNAL(clicked()), this, SLOT(slotTest()));
94     connect(pushButton_TESTTR,      SIGNAL(clicked()), this, SLOT(slotTest()));
95     connect(pushButton_TESTFAV,     SIGNAL(clicked()), this, SLOT(slotTest()));
96 
97     connect(groupBox_SNDCMD, SIGNAL(toggled(bool)), this, SLOT(slotToggleSndCmd(bool)));
98 
99 #ifndef DBUS_NOTIFY
100     frame->setVisible(false);
101 #endif
102 }
103 
playFile(const QString & file)104 void SettingsNotification::playFile(const QString &file){
105     if (WBGET(WB_NOTIFY_SND_ENABLED) || groupBox_SND->isChecked()){
106         if (file.isEmpty() || !QFile::exists(file))
107             return;
108 
109         if (!WBGET(WB_NOTIFY_SND_EXTERNAL))
110             QSound::play(file);
111         else {
112             QString cmd = lineEdit_SNDCMD->text();
113 
114             if (cmd.isEmpty())
115                 return;
116 
117             ShellCommandRunner *r = new ShellCommandRunner(cmd, QStringList() << file, this);
118             connect(r, SIGNAL(finished(bool,QString)), this, SLOT(slotCmdFinished(bool,QString)));
119 
120             r->start();
121         }
122     }
123 }
124 
ok()125 void SettingsNotification::ok(){
126     {//Text
127         WBSET(WB_NOTIFY_ENABLED, groupBox->isChecked());
128         WBSET(WB_NOTIFY_CH_ICON_ALWAYS, checkBox_CHICON->isChecked());
129         WBSET(WB_NOTIFY_SHOW_ON_ACTIVE, checkBox_MWACTIVE->isChecked());
130         WBSET(WB_NOTIFY_SHOW_ON_VISIBLE, checkBox_MWVISIBLE->isChecked());
131 
132         WBSET(WB_EXIT_CONFIRM, checkBox_EXIT_CONFIRM->isChecked());
133 
134         unsigned emap = 0;
135 
136         if (checkBox_ANY->isChecked())
137             emap |= Notification::ANY;
138 
139         if (checkBox_TRDONE->isChecked())
140             emap |= Notification::TRANSFER;
141 
142         if (checkBox_NICKSAY->isChecked())
143             emap |= Notification::NICKSAY;
144 
145         if (checkBox_PM->isChecked())
146             emap |= Notification::PM;
147 
148         if (checkBox_FAVJOIN->isChecked())
149             emap |= Notification::FAVORITE;
150 
151         WISET(WI_NOTIFY_EVENTMAP, emap);
152         WISET(WI_NOTIFY_MODULE, comboBox->currentIndex());
153 
154         Notification::getInstance()->switchModule(comboBox->currentIndex());
155     }
156     {//Sound
157         QString sounds = "";
158 
159         sounds += lineEdit_SNDNICKSAY->text() + "\n";
160         sounds += lineEdit_SNDPM->text() + "\n";
161         sounds += lineEdit_SNDTRDONE->text() + "\n";
162         sounds += lineEdit_FAV->text();
163 
164         WSSET(WS_NOTIFY_SOUNDS, sounds.toUtf8().toBase64());
165         WBSET(WB_NOTIFY_SND_ENABLED, groupBox_SND->isChecked());
166         WBSET("notification/play-sound-with-active-pm", checkBox_ACTIVEPM->isChecked());
167 
168         Notification::getInstance()->reloadSounds();
169 
170         if (WBGET(WB_NOTIFY_SND_EXTERNAL))
171             WSSET(WS_NOTIFY_SND_CMD, lineEdit_SNDCMD->text());
172 
173         unsigned emap = 0;
174 
175         if (groupBox_TR->isChecked())
176             emap |= Notification::TRANSFER;
177 
178         if (groupBox_NICK->isChecked())
179             emap |= Notification::NICKSAY;
180 
181         if (groupBox_PM->isChecked())
182             emap |= Notification::PM;
183 
184         if (groupBox_FAV->isChecked())
185             emap |= Notification::FAVORITE;
186 
187         WISET(WI_NOTIFY_SNDMAP, emap);
188     }
189 }
190 
slotBrowseFile()191 void SettingsNotification::slotBrowseFile(){
192     static QString defaultPath = QDir::homePath();
193 
194     QString f = QFileDialog::getOpenFileName(this, tr("Select file"), defaultPath, tr("All files (*.*)"));
195 
196     if (f.isEmpty())
197         return;
198 
199     f = QDir::toNativeSeparators(f);
200 
201     defaultPath = f.left(f.lastIndexOf(QDir::separator()));
202 
203     QToolButton *btn = reinterpret_cast<QToolButton*>(sender());
204 
205     if (btn == toolButton_BRWNICK)
206         lineEdit_SNDNICKSAY->setText(f);
207     else if (btn == toolButton_BRWPM)
208         lineEdit_SNDPM->setText(f);
209     else if (btn == toolButton_BRWTR)
210         lineEdit_SNDTRDONE->setText(f);
211     else if (btn == toolButton_BRWFAV)
212         lineEdit_FAV->setText(f);
213 }
214 
slotTest()215 void SettingsNotification::slotTest(){
216     QPushButton *btn = reinterpret_cast<QPushButton*>(sender());
217 
218     if (btn == pushButton_TESTNICKSAY)
219         playFile(lineEdit_SNDNICKSAY->text());
220     else if (btn == pushButton_TESTPM)
221         playFile(lineEdit_SNDPM->text());
222     else if (btn == pushButton_TESTTR)
223         playFile(lineEdit_SNDTRDONE->text());
224     else if (btn == pushButton_TESTFAV)
225         playFile(lineEdit_FAV->text());
226 }
227 
slotToggleSndCmd(bool checked)228 void SettingsNotification::slotToggleSndCmd(bool checked){
229     WBSET(WB_NOTIFY_SND_EXTERNAL, checked);
230 }
231 
slotCmdFinished(bool,QString)232 void SettingsNotification::slotCmdFinished(bool, QString){
233     ShellCommandRunner *r = reinterpret_cast<ShellCommandRunner*>(sender());
234 
235     r->exit(0);
236     r->wait(100);
237 
238     if (r->isRunning())
239         r->terminate();
240 
241     delete r;
242 }
243