Fixes #410 - away log (you'll find it in the views menu)
[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   Q_UNUSED(notification)
51   if(_enabled && _media) {
52     _media->play();
53   }
54 }
55
56 void PhononNotificationBackend::close(uint notificationId) {
57   Q_UNUSED(notificationId);
58 }
59
60 void PhononNotificationBackend::enabledChanged(const QVariant &v) {
61   _enabled = v.toBool();
62 }
63
64 void PhononNotificationBackend::audioFileChanged(const QVariant &v) {
65   createMediaObject(v.toString());
66 }
67
68 SettingsPage *PhononNotificationBackend::createConfigWidget() const {
69   return new ConfigWidget();
70 }
71
72 void PhononNotificationBackend::createMediaObject(const QString &file) {
73   if(_media)
74     delete _media;
75
76   if(file.isEmpty()) {
77     _media = 0;
78     return;
79   }
80
81   _media = Phonon::createPlayer(Phonon::NotificationCategory,
82                                 Phonon::MediaSource(file));
83 }
84
85 /***************************************************************************/
86
87 PhononNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
88 : SettingsPage("Internal", "PhononNotification", parent),
89   audioPreview(0)
90 {
91   ui.setupUi(this);
92   ui.play->setIcon(SmallIcon("media-playback-start"));
93   ui.open->setIcon(SmallIcon("document-open"));
94
95   connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
96   connect(ui.filename, SIGNAL(textChanged(const QString &)), SLOT(widgetChanged()));
97 }
98
99 PhononNotificationBackend::ConfigWidget::~ConfigWidget() {
100   if(audioPreview)
101     delete audioPreview;
102 }
103
104 void PhononNotificationBackend::ConfigWidget::widgetChanged() {
105   ui.play->setEnabled(ui.enabled->isChecked() && !ui.filename->text().isEmpty());
106
107   bool changed = (enabled != ui.enabled->isChecked()
108                || filename != ui.filename->text());
109
110   if(changed != hasChanged()) setChangedState(changed);
111 }
112
113 bool PhononNotificationBackend::ConfigWidget::hasDefaults() const {
114   return true;
115 }
116
117 void PhononNotificationBackend::ConfigWidget::defaults() {
118   ui.enabled->setChecked(false);
119   ui.filename->setText(QString());
120   widgetChanged();
121 }
122
123 void PhononNotificationBackend::ConfigWidget::load() {
124   NotificationSettings s;
125   enabled = s.value("Phonon/Enabled", false).toBool();
126   filename = s.value("Phonon/AudioFile", QString()).toString();
127
128   ui.enabled->setChecked(enabled);
129   ui.filename->setText(filename);
130
131   setChangedState(false);
132 }
133
134 void PhononNotificationBackend::ConfigWidget::save() {
135   NotificationSettings s;
136   s.setValue("Phonon/Enabled", ui.enabled->isChecked());
137   s.setValue("Phonon/AudioFile", ui.filename->text());
138   load();
139 }
140
141 void PhononNotificationBackend::ConfigWidget::on_open_clicked() {
142   QString file = QFileDialog::getOpenFileName(this, tr("Select Audio File"));
143   if(!file.isEmpty()) {
144     ui.filename->setText(file);
145     ui.play->setEnabled(true);
146     widgetChanged();
147   }
148 }
149
150 void PhononNotificationBackend::ConfigWidget::on_play_clicked() {
151   if(!ui.filename->text().isEmpty()) {
152     if(audioPreview)
153       delete audioPreview;
154
155     audioPreview = Phonon::createPlayer(Phonon::NotificationCategory,
156                                         Phonon::MediaSource(ui.filename->text()));
157     audioPreview->play();
158   }
159 }