b4adc1c25597b077b0957ea4a1949e36989596b0
[quassel.git] / src / qtui / phononnotificationbackend.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2013 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
23 #include <phonon/mediaobject.h>
24 #include <phonon/backendcapabilities.h>
25
26 #include "phononnotificationbackend.h"
27
28 #include "clientsettings.h"
29 #include "iconloader.h"
30 #include "mainwin.h"
31 #include "qtui.h"
32
33 PhononNotificationBackend::PhononNotificationBackend(QObject *parent)
34     : AbstractNotificationBackend(parent),
35     _media(0)
36 {
37     NotificationSettings notificationSettings;
38     notificationSettings.notify("Phonon/Enabled", this, SLOT(enabledChanged(const QVariant &)));
39     notificationSettings.notify("Phonon/AudioFile", this, SLOT(audioFileChanged(const QVariant &)));
40
41     createMediaObject(notificationSettings.value("Phonon/AudioFile", QString()).toString());
42
43     _enabled = notificationSettings.value("Phonon/Enabled", true).toBool();
44     _audioAvailable = !Phonon::BackendCapabilities::availableAudioOutputDevices().isEmpty();
45 }
46
47
48 PhononNotificationBackend::~PhononNotificationBackend()
49 {
50     if (_media)
51         delete _media;
52 }
53
54
55 void PhononNotificationBackend::notify(const Notification &notification)
56 {
57     if (_enabled && (notification.type == Highlight || notification.type == PrivMsg)) {
58         if (_audioAvailable && _media) {
59             _media->stop();
60             _media->play();
61         }
62         else
63             QApplication::beep();
64     }
65 }
66
67
68 void PhononNotificationBackend::close(uint notificationId)
69 {
70     Q_UNUSED(notificationId);
71 }
72
73
74 void PhononNotificationBackend::enabledChanged(const QVariant &v)
75 {
76     _enabled = v.toBool();
77 }
78
79
80 void PhononNotificationBackend::audioFileChanged(const QVariant &v)
81 {
82     createMediaObject(v.toString());
83 }
84
85
86 SettingsPage *PhononNotificationBackend::createConfigWidget() const
87 {
88     return new ConfigWidget();
89 }
90
91
92 void PhononNotificationBackend::createMediaObject(const QString &file)
93 {
94     if (_media)
95         delete _media;
96
97     if (file.isEmpty()) {
98         _media = 0;
99         return;
100     }
101
102     _media = Phonon::createPlayer(Phonon::NotificationCategory, Phonon::MediaSource(file));
103 }
104
105
106 /***************************************************************************/
107
108 PhononNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
109     : SettingsPage("Internal", "PhononNotification", parent),
110     audioPreview(0)
111 {
112     ui.setupUi(this);
113     _audioAvailable = !Phonon::BackendCapabilities::availableAudioOutputDevices().isEmpty();
114     ui.enabled->setIcon(SmallIcon("media-playback-start"));
115     ui.play->setIcon(SmallIcon("media-playback-start"));
116     ui.open->setIcon(SmallIcon("document-open"));
117
118     connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
119     connect(ui.filename, SIGNAL(textChanged(const QString &)), SLOT(widgetChanged()));
120 }
121
122
123 PhononNotificationBackend::ConfigWidget::~ConfigWidget()
124 {
125     if (audioPreview)
126         delete audioPreview;
127 }
128
129
130 void PhononNotificationBackend::ConfigWidget::widgetChanged()
131 {
132     if (! _audioAvailable) {
133         ui.play->setEnabled(ui.enabled->isChecked());
134         ui.open->setEnabled(false);
135         ui.filename->setEnabled(false);
136         ui.filename->setText(QString());
137     }
138     else {
139         ui.play->setEnabled(ui.enabled->isChecked() && !ui.filename->text().isEmpty());
140
141         bool changed = (enabled != ui.enabled->isChecked() || filename != ui.filename->text());
142
143         if (changed != hasChanged())
144             setChangedState(changed);
145     }
146 }
147
148
149 bool PhononNotificationBackend::ConfigWidget::hasDefaults() const
150 {
151     return true;
152 }
153
154
155 void PhononNotificationBackend::ConfigWidget::defaults()
156 {
157     ui.enabled->setChecked(false);
158     ui.filename->setText(QString());
159     widgetChanged();
160 }
161
162
163 void PhononNotificationBackend::ConfigWidget::load()
164 {
165     NotificationSettings s;
166     enabled = s.value("Phonon/Enabled", false).toBool();
167     filename = s.value("Phonon/AudioFile", QString()).toString();
168
169     ui.enabled->setChecked(enabled);
170     ui.filename->setText(filename);
171
172     setChangedState(false);
173 }
174
175
176 void PhononNotificationBackend::ConfigWidget::save()
177 {
178     NotificationSettings s;
179     s.setValue("Phonon/Enabled", ui.enabled->isChecked());
180     s.setValue("Phonon/AudioFile", ui.filename->text());
181     load();
182 }
183
184
185 void PhononNotificationBackend::ConfigWidget::on_open_clicked()
186 {
187     QString file = QFileDialog::getOpenFileName(this, tr("Select Audio File"));
188     if (!file.isEmpty()) {
189         ui.filename->setText(file);
190         ui.play->setEnabled(true);
191         widgetChanged();
192     }
193 }
194
195
196 void PhononNotificationBackend::ConfigWidget::on_play_clicked()
197 {
198     if (_audioAvailable) {
199         if (!ui.filename->text().isEmpty()) {
200             if (audioPreview)
201                 delete audioPreview;
202
203             audioPreview = Phonon::createPlayer(Phonon::NotificationCategory, Phonon::MediaSource(ui.filename->text()));
204             audioPreview->play();
205         }
206     }
207     else
208         QApplication::beep();
209 }