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