1 /***************************************************************************
2 * Copyright (C) 2005-2015 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include <QFileDialog>
25 #include "qtmultimedianotificationbackend.h"
27 #include "clientsettings.h"
31 QtMultimediaNotificationBackend::QtMultimediaNotificationBackend(QObject *parent)
32 : AbstractNotificationBackend(parent)
34 NotificationSettings notificationSettings;
35 notificationSettings.notify("QtMultimedia/Enabled", this, SLOT(enabledChanged(const QVariant &)));
36 notificationSettings.notify("QtMultimedia/AudioFile", this, SLOT(audioFileChanged(const QVariant &)));
38 createMediaObject(notificationSettings.value("QtMultimedia/AudioFile", QString()).toString());
40 _enabled = notificationSettings.value("QtMultimedia/Enabled", true).toBool();
44 void QtMultimediaNotificationBackend::notify(const Notification ¬ification)
46 if (_enabled && (notification.type == Highlight || notification.type == PrivMsg)) {
47 if (_media && _media->availability() == QMultimedia::Available) {
57 void QtMultimediaNotificationBackend::close(uint notificationId)
59 Q_UNUSED(notificationId);
63 void QtMultimediaNotificationBackend::enabledChanged(const QVariant &v)
65 _enabled = v.toBool();
69 void QtMultimediaNotificationBackend::audioFileChanged(const QVariant &v)
71 createMediaObject(v.toString());
75 SettingsPage *QtMultimediaNotificationBackend::createConfigWidget() const
77 return new ConfigWidget();
81 void QtMultimediaNotificationBackend::createMediaObject(const QString &file)
88 _media.reset(new QMediaPlayer);
89 _media->setMedia(QUrl::fromLocalFile(file));
93 /***************************************************************************/
95 QtMultimediaNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
96 : SettingsPage("Internal", "QtMultimediaNotification", parent)
99 ui.enabled->setIcon(QIcon::fromTheme("media-playback-start"));
100 ui.play->setIcon(QIcon::fromTheme("media-playback-start"));
101 ui.open->setIcon(QIcon::fromTheme("document-open"));
103 _audioAvailable = (QMediaPlayer().availability() == QMultimedia::Available);
105 connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
106 connect(ui.filename, SIGNAL(textChanged(const QString &)), SLOT(widgetChanged()));
110 void QtMultimediaNotificationBackend::ConfigWidget::widgetChanged()
112 if (! _audioAvailable) {
113 ui.play->setEnabled(ui.enabled->isChecked());
114 ui.open->setEnabled(false);
115 ui.filename->setEnabled(false);
116 ui.filename->setText({});
119 ui.play->setEnabled(ui.enabled->isChecked() && !ui.filename->text().isEmpty());
121 bool changed = (_enabled != ui.enabled->isChecked() || _filename != ui.filename->text());
123 if (changed != hasChanged())
124 setChangedState(changed);
129 bool QtMultimediaNotificationBackend::ConfigWidget::hasDefaults() const
135 void QtMultimediaNotificationBackend::ConfigWidget::defaults()
137 ui.enabled->setChecked(false);
138 ui.filename->setText({});
143 void QtMultimediaNotificationBackend::ConfigWidget::load()
145 NotificationSettings s;
146 _enabled = s.value("QtMultimedia/Enabled", false).toBool();
147 _filename = s.value("QtMultimedia/AudioFile", QString()).toString();
149 ui.enabled->setChecked(_enabled);
150 ui.filename->setText(_filename);
152 setChangedState(false);
156 void QtMultimediaNotificationBackend::ConfigWidget::save()
158 NotificationSettings s;
159 s.setValue("QtMultimedia/Enabled", ui.enabled->isChecked());
160 s.setValue("QtMultimedia/AudioFile", ui.filename->text());
165 void QtMultimediaNotificationBackend::ConfigWidget::on_open_clicked()
167 QString file = QFileDialog::getOpenFileName(this, tr("Select Audio File"));
168 if (!file.isEmpty()) {
169 ui.filename->setText(file);
170 ui.play->setEnabled(true);
176 void QtMultimediaNotificationBackend::ConfigWidget::on_play_clicked()
178 if (_audioAvailable) {
179 if (!ui.filename->text().isEmpty()) {
180 _audioPreview.reset(new QMediaPlayer);
181 _audioPreview->setMedia(QUrl::fromLocalFile(ui.filename->text()));
182 _audioPreview->play();
186 QApplication::beep();