1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 "qtmultimedianotificationbackend.h"
23 #include <QFileDialog>
27 #include "clientsettings.h"
32 QtMultimediaNotificationBackend::QtMultimediaNotificationBackend(QObject *parent)
33 : 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 void QtMultimediaNotificationBackend::notify(const Notification ¬ification)
47 if (_enabled && (notification.type == Highlight || notification.type == PrivMsg)) {
48 if (_media && _media->availability() == QMultimedia::Available) {
58 void QtMultimediaNotificationBackend::close(uint notificationId)
60 Q_UNUSED(notificationId);
64 void QtMultimediaNotificationBackend::enabledChanged(const QVariant &v)
66 _enabled = v.toBool();
70 void QtMultimediaNotificationBackend::audioFileChanged(const QVariant &v)
72 createMediaObject(v.toString());
76 SettingsPage *QtMultimediaNotificationBackend::createConfigWidget() const
78 return new ConfigWidget();
82 void QtMultimediaNotificationBackend::createMediaObject(const QString &file)
89 _media = std::make_unique<QMediaPlayer>();
90 _media->setMedia(QUrl::fromLocalFile(file));
94 /***************************************************************************/
96 QtMultimediaNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
97 : SettingsPage("Internal", "QtMultimediaNotification", parent)
100 ui.enabled->setIcon(icon::get("media-playback-start"));
101 ui.play->setIcon(icon::get("media-playback-start"));
102 ui.open->setIcon(icon::get("document-open"));
104 _audioAvailable = (QMediaPlayer().availability() == QMultimedia::Available);
106 connect(ui.enabled, &QAbstractButton::toggled, this, &ConfigWidget::widgetChanged);
107 connect(ui.filename, &QLineEdit::textChanged, this, &ConfigWidget::widgetChanged);
111 void QtMultimediaNotificationBackend::ConfigWidget::widgetChanged()
113 if (! _audioAvailable) {
114 ui.play->setEnabled(ui.enabled->isChecked());
115 ui.open->setEnabled(false);
116 ui.filename->setEnabled(false);
117 ui.filename->setText({});
120 ui.play->setEnabled(ui.enabled->isChecked() && !ui.filename->text().isEmpty());
122 bool changed = (_enabled != ui.enabled->isChecked() || _filename != ui.filename->text());
124 if (changed != hasChanged())
125 setChangedState(changed);
130 bool QtMultimediaNotificationBackend::ConfigWidget::hasDefaults() const
136 void QtMultimediaNotificationBackend::ConfigWidget::defaults()
138 ui.enabled->setChecked(false);
139 ui.filename->setText({});
144 void QtMultimediaNotificationBackend::ConfigWidget::load()
146 NotificationSettings s;
147 _enabled = s.value("QtMultimedia/Enabled", false).toBool();
148 _filename = s.value("QtMultimedia/AudioFile", QString()).toString();
150 ui.enabled->setChecked(_enabled);
151 ui.filename->setText(_filename);
153 setChangedState(false);
157 void QtMultimediaNotificationBackend::ConfigWidget::save()
159 NotificationSettings s;
160 s.setValue("QtMultimedia/Enabled", ui.enabled->isChecked());
161 s.setValue("QtMultimedia/AudioFile", ui.filename->text());
166 void QtMultimediaNotificationBackend::ConfigWidget::on_open_clicked()
168 QString file = QFileDialog::getOpenFileName(this, tr("Select Audio File"));
169 if (!file.isEmpty()) {
170 ui.filename->setText(file);
171 ui.play->setEnabled(true);
177 void QtMultimediaNotificationBackend::ConfigWidget::on_play_clicked()
179 if (_audioAvailable) {
180 if (!ui.filename->text().isEmpty()) {
181 _audioPreview = std::make_unique<QMediaPlayer>();
182 _audioPreview->setMedia(QUrl::fromLocalFile(ui.filename->text()));
183 _audioPreview->play();
187 QApplication::beep();