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),
35 NotificationSettings notificationSettings;
36 notificationSettings.notify("QtMultimedia/Enabled", this, SLOT(enabledChanged(const QVariant &)));
37 notificationSettings.notify("QtMultimedia/AudioFile", this, SLOT(audioFileChanged(const QVariant &)));
39 createMediaObject(notificationSettings.value("QtMultimedia/AudioFile", QString()).toString());
41 _enabled = notificationSettings.value("QtMultimedia/Enabled", true).toBool();
45 QtMultimediaNotificationBackend::~QtMultimediaNotificationBackend()
52 void QtMultimediaNotificationBackend::notify(const Notification ¬ification)
54 if (_enabled && (notification.type == Highlight || notification.type == PrivMsg)) {
55 if (_media && _media->availability() == QMultimedia::Available) {
65 void QtMultimediaNotificationBackend::close(uint notificationId)
67 Q_UNUSED(notificationId);
71 void QtMultimediaNotificationBackend::enabledChanged(const QVariant &v)
73 _enabled = v.toBool();
77 void QtMultimediaNotificationBackend::audioFileChanged(const QVariant &v)
79 createMediaObject(v.toString());
83 SettingsPage *QtMultimediaNotificationBackend::createConfigWidget() const
85 return new ConfigWidget();
89 void QtMultimediaNotificationBackend::createMediaObject(const QString &file)
99 _media = new QMediaPlayer;
100 _media->setMedia(QUrl::fromLocalFile(file));
104 /***************************************************************************/
106 QtMultimediaNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
107 : SettingsPage("Internal", "QtMultimediaNotification", parent),
111 ui.enabled->setIcon(QIcon::fromTheme("media-playback-start"));
112 ui.play->setIcon(QIcon::fromTheme("media-playback-start"));
113 ui.open->setIcon(QIcon::fromTheme("document-open"));
115 QMediaPlayer *player = new QMediaPlayer;
116 _audioAvailable = player->availability() == QMultimedia::Available;
119 connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
120 connect(ui.filename, SIGNAL(textChanged(const QString &)), SLOT(widgetChanged()));
124 QtMultimediaNotificationBackend::ConfigWidget::~ConfigWidget()
131 void QtMultimediaNotificationBackend::ConfigWidget::widgetChanged()
133 if (! _audioAvailable) {
134 ui.play->setEnabled(ui.enabled->isChecked());
135 ui.open->setEnabled(false);
136 ui.filename->setEnabled(false);
137 ui.filename->setText(QString());
140 ui.play->setEnabled(ui.enabled->isChecked() && !ui.filename->text().isEmpty());
142 bool changed = (enabled != ui.enabled->isChecked() || filename != ui.filename->text());
144 if (changed != hasChanged())
145 setChangedState(changed);
150 bool QtMultimediaNotificationBackend::ConfigWidget::hasDefaults() const
156 void QtMultimediaNotificationBackend::ConfigWidget::defaults()
158 ui.enabled->setChecked(false);
159 ui.filename->setText(QString());
164 void QtMultimediaNotificationBackend::ConfigWidget::load()
166 NotificationSettings s;
167 enabled = s.value("QtMultimedia/Enabled", false).toBool();
168 filename = s.value("QtMultimedia/AudioFile", QString()).toString();
170 ui.enabled->setChecked(enabled);
171 ui.filename->setText(filename);
173 setChangedState(false);
177 void QtMultimediaNotificationBackend::ConfigWidget::save()
179 NotificationSettings s;
180 s.setValue("QtMultimedia/Enabled", ui.enabled->isChecked());
181 s.setValue("QtMultimedia/AudioFile", ui.filename->text());
186 void QtMultimediaNotificationBackend::ConfigWidget::on_open_clicked()
188 QString file = QFileDialog::getOpenFileName(this, tr("Select Audio File"));
189 if (!file.isEmpty()) {
190 ui.filename->setText(file);
191 ui.play->setEnabled(true);
197 void QtMultimediaNotificationBackend::ConfigWidget::on_play_clicked()
199 if (_audioAvailable) {
200 if (!ui.filename->text().isEmpty()) {
204 audioPreview = new QMediaPlayer;
205 audioPreview->setMedia(QUrl::fromLocalFile(ui.filename->text()));
206 audioPreview->play();
210 QApplication::beep();