Introduce a notification type and add extra notifications for KNotify
[quassel.git] / src / qtui / phononnotificationbackend.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-09 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 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19 ***************************************************************************/
20
21 #include <QFileDialog>
22
23 #include <phonon/mediaobject.h>
24
25 #include "phononnotificationbackend.h"
26
27 #include "clientsettings.h"
28 #include "iconloader.h"
29 #include "mainwin.h"
30 #include "qtui.h"
31
32 PhononNotificationBackend::PhononNotificationBackend(QObject *parent)
33   : AbstractNotificationBackend(parent),
34   _media(0)
35 {
36   NotificationSettings notificationSettings;
37   _enabled = notificationSettings.value("Phonon/Enabled", true).toBool();
38   createMediaObject(notificationSettings.value("Phonon/AudioFile", QString()).toString());
39
40   notificationSettings.notify("Phonon/Enabled", this, SLOT(enabledChanged(const QVariant &)));
41   notificationSettings.notify("Phonon/AudioFile", this, SLOT(audioFileChanged(const QVariant &)));
42 }
43
44 PhononNotificationBackend::~PhononNotificationBackend() {
45   if(_media)
46     delete _media;
47 }
48
49 void PhononNotificationBackend::notify(const Notification &notification) {
50   if(_enabled && _media && (notification.type == Highlight || notification.type == PrivMsg)) {
51     _media->play();
52   }
53 }
54
55 void PhononNotificationBackend::close(uint notificationId) {
56   Q_UNUSED(notificationId);
57 }
58
59 void PhononNotificationBackend::enabledChanged(const QVariant &v) {
60   _enabled = v.toBool();
61 }
62
63 void PhononNotificationBackend::audioFileChanged(const QVariant &v) {
64   createMediaObject(v.toString());
65 }
66
67 SettingsPage *PhononNotificationBackend::createConfigWidget() const {
68   return new ConfigWidget();
69 }
70
71 void PhononNotificationBackend::createMediaObject(const QString &file) {
72   if(_media)
73     delete _media;
74
75   if(file.isEmpty()) {
76     _media = 0;
77     return;
78   }
79
80   _media = Phonon::createPlayer(Phonon::NotificationCategory,
81                                 Phonon::MediaSource(file));
82 }
83
84 /***************************************************************************/
85
86 PhononNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
87 : SettingsPage("Internal", "PhononNotification", parent),
88   audioPreview(0)
89 {
90   ui.setupUi(this);
91   ui.play->setIcon(SmallIcon("media-playback-start"));
92   ui.open->setIcon(SmallIcon("document-open"));
93
94   connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
95   connect(ui.filename, SIGNAL(textChanged(const QString &)), SLOT(widgetChanged()));
96 }
97
98 PhononNotificationBackend::ConfigWidget::~ConfigWidget() {
99   if(audioPreview)
100     delete audioPreview;
101 }
102
103 void PhononNotificationBackend::ConfigWidget::widgetChanged() {
104   ui.play->setEnabled(ui.enabled->isChecked() && !ui.filename->text().isEmpty());
105
106   bool changed = (enabled != ui.enabled->isChecked()
107                || filename != ui.filename->text());
108
109   if(changed != hasChanged()) setChangedState(changed);
110 }
111
112 bool PhononNotificationBackend::ConfigWidget::hasDefaults() const {
113   return true;
114 }
115
116 void PhononNotificationBackend::ConfigWidget::defaults() {
117   ui.enabled->setChecked(false);
118   ui.filename->setText(QString());
119   widgetChanged();
120 }
121
122 void PhononNotificationBackend::ConfigWidget::load() {
123   NotificationSettings s;
124   enabled = s.value("Phonon/Enabled", false).toBool();
125   filename = s.value("Phonon/AudioFile", QString()).toString();
126
127   ui.enabled->setChecked(enabled);
128   ui.filename->setText(filename);
129
130   setChangedState(false);
131 }
132
133 void PhononNotificationBackend::ConfigWidget::save() {
134   NotificationSettings s;
135   s.setValue("Phonon/Enabled", ui.enabled->isChecked());
136   s.setValue("Phonon/AudioFile", ui.filename->text());
137   load();
138 }
139
140 void PhononNotificationBackend::ConfigWidget::on_open_clicked() {
141   QString file = QFileDialog::getOpenFileName(this, tr("Select Audio File"));
142   if(!file.isEmpty()) {
143     ui.filename->setText(file);
144     ui.play->setEnabled(true);
145     widgetChanged();
146   }
147 }
148
149 void PhononNotificationBackend::ConfigWidget::on_play_clicked() {
150   if(!ui.filename->text().isEmpty()) {
151     if(audioPreview)
152       delete audioPreview;
153
154     audioPreview = Phonon::createPlayer(Phonon::NotificationCategory,
155                                         Phonon::MediaSource(ui.filename->text()));
156     audioPreview->play();
157   }
158 }