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"
25 #include <QFileDialog>
28 #include "clientsettings.h"
33 QtMultimediaNotificationBackend::QtMultimediaNotificationBackend(QObject* parent)
34 : AbstractNotificationBackend(parent)
36 NotificationSettings notificationSettings;
37 notificationSettings.notify("QtMultimedia/Enabled", this, &QtMultimediaNotificationBackend::enabledChanged);
38 notificationSettings.notify("QtMultimedia/AudioFile", this, &QtMultimediaNotificationBackend::audioFileChanged);
40 createMediaObject(notificationSettings.value("QtMultimedia/AudioFile", QString()).toString());
42 _enabled = notificationSettings.value("QtMultimedia/Enabled", true).toBool();
45 void QtMultimediaNotificationBackend::notify(const Notification& notification)
47 if (_enabled && (notification.type == Highlight || notification.type == PrivMsg)) {
48 if (_media && _media->availability() == QMultimedia::Available) {
57 void QtMultimediaNotificationBackend::close(uint notificationId)
59 Q_UNUSED(notificationId);
62 void QtMultimediaNotificationBackend::enabledChanged(const QVariant& v)
64 _enabled = v.toBool();
67 void QtMultimediaNotificationBackend::audioFileChanged(const QVariant& v)
69 createMediaObject(v.toString());
72 SettingsPage* QtMultimediaNotificationBackend::createConfigWidget() const
74 return new ConfigWidget();
77 void QtMultimediaNotificationBackend::createMediaObject(const QString& file)
84 _media = std::make_unique<QMediaPlayer>();
85 _media->setMedia(QUrl::fromLocalFile(file));
88 /***************************************************************************/
90 QtMultimediaNotificationBackend::ConfigWidget::ConfigWidget(QWidget* parent)
91 : SettingsPage("Internal", "QtMultimediaNotification", parent)
94 ui.enabled->setIcon(icon::get("media-playback-start"));
95 ui.play->setIcon(icon::get("media-playback-start"));
96 ui.open->setIcon(icon::get("document-open"));
98 _audioAvailable = (QMediaPlayer().availability() == QMultimedia::Available);
100 connect(ui.enabled, &QAbstractButton::toggled, this, &ConfigWidget::widgetChanged);
101 connect(ui.filename, &QLineEdit::textChanged, this, &ConfigWidget::widgetChanged);
104 void QtMultimediaNotificationBackend::ConfigWidget::widgetChanged()
106 if (!_audioAvailable) {
107 ui.play->setEnabled(ui.enabled->isChecked());
108 ui.open->setEnabled(false);
109 ui.filename->setEnabled(false);
110 ui.filename->setText({});
113 ui.play->setEnabled(ui.enabled->isChecked() && !ui.filename->text().isEmpty());
115 bool changed = (_enabled != ui.enabled->isChecked() || _filename != ui.filename->text());
117 if (changed != hasChanged())
118 setChangedState(changed);
122 bool QtMultimediaNotificationBackend::ConfigWidget::hasDefaults() const
127 void QtMultimediaNotificationBackend::ConfigWidget::defaults()
129 ui.enabled->setChecked(false);
130 ui.filename->setText({});
134 void QtMultimediaNotificationBackend::ConfigWidget::load()
136 NotificationSettings s;
137 _enabled = s.value("QtMultimedia/Enabled", false).toBool();
138 _filename = s.value("QtMultimedia/AudioFile", QString()).toString();
140 ui.enabled->setChecked(_enabled);
141 ui.filename->setText(_filename);
143 setChangedState(false);
146 void QtMultimediaNotificationBackend::ConfigWidget::save()
148 NotificationSettings s;
149 s.setValue("QtMultimedia/Enabled", ui.enabled->isChecked());
150 s.setValue("QtMultimedia/AudioFile", ui.filename->text());
154 void QtMultimediaNotificationBackend::ConfigWidget::on_open_clicked()
156 QString file = QFileDialog::getOpenFileName(this, tr("Select Audio File"));
157 if (!file.isEmpty()) {
158 ui.filename->setText(file);
159 ui.play->setEnabled(true);
164 void QtMultimediaNotificationBackend::ConfigWidget::on_play_clicked()
166 if (_audioAvailable) {
167 if (!ui.filename->text().isEmpty()) {
168 _audioPreview = std::make_unique<QMediaPlayer>();
169 _audioPreview->setMedia(QUrl::fromLocalFile(ui.filename->text()));
170 _audioPreview->play();
174 QApplication::beep();