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