1 /***************************************************************************
2 * Copyright (C) 2005-2013 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include <QFileDialog>
23 #include <phonon/mediaobject.h>
24 #include <phonon/backendcapabilities.h>
26 #include "phononnotificationbackend.h"
28 #include "clientsettings.h"
29 #include "iconloader.h"
33 PhononNotificationBackend::PhononNotificationBackend(QObject *parent)
34 : AbstractNotificationBackend(parent),
37 NotificationSettings notificationSettings;
38 notificationSettings.notify("Phonon/Enabled", this, SLOT(enabledChanged(const QVariant &)));
39 notificationSettings.notify("Phonon/AudioFile", this, SLOT(audioFileChanged(const QVariant &)));
41 createMediaObject(notificationSettings.value("Phonon/AudioFile", QString()).toString());
43 _enabled = notificationSettings.value("Phonon/Enabled", true).toBool();
44 _audioAvailable = !Phonon::BackendCapabilities::availableAudioOutputDevices().isEmpty();
48 PhononNotificationBackend::~PhononNotificationBackend()
55 void PhononNotificationBackend::notify(const Notification ¬ification)
57 if (_enabled && (notification.type == Highlight || notification.type == PrivMsg)) {
58 if (_audioAvailable && _media) {
68 void PhononNotificationBackend::close(uint notificationId)
70 Q_UNUSED(notificationId);
74 void PhononNotificationBackend::enabledChanged(const QVariant &v)
76 _enabled = v.toBool();
80 void PhononNotificationBackend::audioFileChanged(const QVariant &v)
82 createMediaObject(v.toString());
86 SettingsPage *PhononNotificationBackend::createConfigWidget() const
88 return new ConfigWidget();
92 void PhononNotificationBackend::createMediaObject(const QString &file)
102 _media = Phonon::createPlayer(Phonon::NotificationCategory, Phonon::MediaSource(file));
106 /***************************************************************************/
108 PhononNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
109 : SettingsPage("Internal", "PhononNotification", parent),
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"));
118 connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
119 connect(ui.filename, SIGNAL(textChanged(const QString &)), SLOT(widgetChanged()));
123 PhononNotificationBackend::ConfigWidget::~ConfigWidget()
130 void PhononNotificationBackend::ConfigWidget::widgetChanged()
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());
139 ui.play->setEnabled(ui.enabled->isChecked() && !ui.filename->text().isEmpty());
141 bool changed = (enabled != ui.enabled->isChecked() || filename != ui.filename->text());
143 if (changed != hasChanged())
144 setChangedState(changed);
149 bool PhononNotificationBackend::ConfigWidget::hasDefaults() const
155 void PhononNotificationBackend::ConfigWidget::defaults()
157 ui.enabled->setChecked(false);
158 ui.filename->setText(QString());
163 void PhononNotificationBackend::ConfigWidget::load()
165 NotificationSettings s;
166 enabled = s.value("Phonon/Enabled", false).toBool();
167 filename = s.value("Phonon/AudioFile", QString()).toString();
169 ui.enabled->setChecked(enabled);
170 ui.filename->setText(filename);
172 setChangedState(false);
176 void PhononNotificationBackend::ConfigWidget::save()
178 NotificationSettings s;
179 s.setValue("Phonon/Enabled", ui.enabled->isChecked());
180 s.setValue("Phonon/AudioFile", ui.filename->text());
185 void PhononNotificationBackend::ConfigWidget::on_open_clicked()
187 QString file = QFileDialog::getOpenFileName(this, tr("Select Audio File"));
188 if (!file.isEmpty()) {
189 ui.filename->setText(file);
190 ui.play->setEnabled(true);
196 void PhononNotificationBackend::ConfigWidget::on_play_clicked()
198 if (_audioAvailable) {
199 if (!ui.filename->text().isEmpty()) {
203 audioPreview = Phonon::createPlayer(Phonon::NotificationCategory, Phonon::MediaSource(ui.filename->text()));
204 audioPreview->play();
208 QApplication::beep();