1 | /*************************************************************************** |
2 | * Copyright (C) 2005-2014 by the Quassel Project * |
3 | * devel@quassel-irc.org * |
4 | * * |
5 | * This program is free software; you can redistribute it and/or modify * |
6 | * it under the terms of the GNU General Public License as published by * |
7 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * (at your option) version 3. * |
9 | * * |
10 | * This program is distributed in the hope that it will be useful, * |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * GNU General Public License for more details. * |
14 | * * |
15 | * You should have received a copy of the GNU General Public License * |
16 | * along with this program; if not, write to the * |
17 | * Free Software Foundation, Inc., * |
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
19 | ***************************************************************************/ |
20 | |
21 | #include <QFileDialog> |
22 | #include <QUrl> |
23 | |
24 | #include <phonon/mediaobject.h> |
25 | #include <phonon/backendcapabilities.h> |
26 | |
27 | #include "phononnotificationbackend.h" |
28 | |
29 | #include "clientsettings.h" |
30 | #include "iconloader.h" |
31 | #include "mainwin.h" |
32 | #include "qtui.h" |
33 | |
34 | PhononNotificationBackend::PhononNotificationBackend(QObject *parent) |
35 | : AbstractNotificationBackend(parent), |
36 | _media(0) |
37 | { |
38 | NotificationSettings notificationSettings; |
39 | notificationSettings.notify("Phonon/Enabled" , this, SLOT(enabledChanged(const QVariant &))); |
40 | notificationSettings.notify("Phonon/AudioFile" , this, SLOT(audioFileChanged(const QVariant &))); |
41 | |
42 | createMediaObject(notificationSettings.value("Phonon/AudioFile" , QString()).toString()); |
43 | |
44 | _enabled = notificationSettings.value("Phonon/Enabled" , true).toBool(); |
45 | _audioAvailable = !Phonon::BackendCapabilities::availableAudioOutputDevices().isEmpty(); |
46 | } |
47 | |
48 | |
49 | PhononNotificationBackend::~PhononNotificationBackend() |
50 | { |
51 | if (_media) |
52 | delete _media; |
53 | } |
54 | |
55 | |
56 | void PhononNotificationBackend::notify(const Notification ¬ification) |
57 | { |
58 | if (_enabled && (notification.type == Highlight || notification.type == PrivMsg)) { |
59 | if (_audioAvailable && _media) { |
60 | _media->stop(); |
61 | _media->play(); |
62 | } |
63 | else |
64 | QApplication::beep(); |
65 | } |
66 | } |
67 | |
68 | |
69 | void PhononNotificationBackend::close(uint notificationId) |
70 | { |
71 | Q_UNUSED(notificationId); |
72 | } |
73 | |
74 | |
75 | void PhononNotificationBackend::enabledChanged(const QVariant &v) |
76 | { |
77 | _enabled = v.toBool(); |
78 | } |
79 | |
80 | |
81 | void PhononNotificationBackend::audioFileChanged(const QVariant &v) |
82 | { |
83 | createMediaObject(v.toString()); |
84 | } |
85 | |
86 | |
87 | SettingsPage *PhononNotificationBackend::createConfigWidget() const |
88 | { |
89 | return new ConfigWidget(); |
90 | } |
91 | |
92 | |
93 | void PhononNotificationBackend::createMediaObject(const QString &file) |
94 | { |
95 | if (_media) |
96 | delete _media; |
97 | |
98 | if (file.isEmpty()) { |
99 | _media = 0; |
100 | return; |
101 | } |
102 | |
103 | _media = Phonon::createPlayer(Phonon::NotificationCategory, Phonon::MediaSource(QUrl::fromLocalFile(file))); |
104 | } |
105 | |
106 | |
107 | /***************************************************************************/ |
108 | |
109 | PhononNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) |
110 | : SettingsPage("Internal" , "PhononNotification" , parent), |
111 | audioPreview(0) |
112 | { |
113 | ui.setupUi(this); |
114 | _audioAvailable = !Phonon::BackendCapabilities::availableAudioOutputDevices().isEmpty(); |
115 | ui.enabled->setIcon(SmallIcon("media-playback-start" )); |
116 | ui.play->setIcon(SmallIcon("media-playback-start" )); |
117 | ui.open->setIcon(SmallIcon("document-open" )); |
118 | |
119 | connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged())); |
120 | connect(ui.filename, SIGNAL(textChanged(const QString &)), SLOT(widgetChanged())); |
121 | } |
122 | |
123 | |
124 | PhononNotificationBackend::ConfigWidget::~ConfigWidget() |
125 | { |
126 | if (audioPreview) |
127 | delete audioPreview; |
128 | } |
129 | |
130 | |
131 | void PhononNotificationBackend::ConfigWidget::widgetChanged() |
132 | { |
133 | if (! _audioAvailable) { |
134 | ui.play->setEnabled(ui.enabled->isChecked()); |
135 | ui.open->setEnabled(false); |
136 | ui.filename->setEnabled(false); |
137 | ui.filename->setText(QString()); |
138 | } |
139 | else { |
140 | ui.play->setEnabled(ui.enabled->isChecked() && !ui.filename->text().isEmpty()); |
141 | |
142 | bool changed = (enabled != ui.enabled->isChecked() || filename != ui.filename->text()); |
143 | |
144 | if (changed != hasChanged()) |
145 | setChangedState(changed); |
146 | } |
147 | } |
148 | |
149 | |
150 | bool PhononNotificationBackend::ConfigWidget::hasDefaults() const |
151 | { |
152 | return true; |
153 | } |
154 | |
155 | |
156 | void PhononNotificationBackend::ConfigWidget::defaults() |
157 | { |
158 | ui.enabled->setChecked(false); |
159 | ui.filename->setText(QString()); |
160 | widgetChanged(); |
161 | } |
162 | |
163 | |
164 | void PhononNotificationBackend::ConfigWidget::load() |
165 | { |
166 | NotificationSettings s; |
167 | enabled = s.value("Phonon/Enabled" , false).toBool(); |
168 | filename = s.value("Phonon/AudioFile" , QString()).toString(); |
169 | |
170 | ui.enabled->setChecked(enabled); |
171 | ui.filename->setText(filename); |
172 | |
173 | setChangedState(false); |
174 | } |
175 | |
176 | |
177 | void PhononNotificationBackend::ConfigWidget::save() |
178 | { |
179 | NotificationSettings s; |
180 | s.setValue("Phonon/Enabled" , ui.enabled->isChecked()); |
181 | s.setValue("Phonon/AudioFile" , ui.filename->text()); |
182 | load(); |
183 | } |
184 | |
185 | |
186 | void PhononNotificationBackend::ConfigWidget::on_open_clicked() |
187 | { |
188 | QString file = QFileDialog::getOpenFileName(this, tr("Select Audio File" )); |
189 | if (!file.isEmpty()) { |
190 | ui.filename->setText(file); |
191 | ui.play->setEnabled(true); |
192 | widgetChanged(); |
193 | } |
194 | } |
195 | |
196 | |
197 | void PhononNotificationBackend::ConfigWidget::on_play_clicked() |
198 | { |
199 | if (_audioAvailable) { |
200 | if (!ui.filename->text().isEmpty()) { |
201 | if (audioPreview) |
202 | delete audioPreview; |
203 | |
204 | audioPreview = Phonon::createPlayer(Phonon::NotificationCategory, Phonon::MediaSource(QUrl::fromLocalFile(ui.filename->text()))); |
205 | audioPreview->play(); |
206 | } |
207 | } |
208 | else |
209 | QApplication::beep(); |
210 | } |
211 | |