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