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