src: Yearly copyright bump
[quassel.git] / src / qtui / qtmultimedianotificationbackend.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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 "qtmultimedianotificationbackend.h"
22
23 #include <QFileDialog>
24 #include <QUrl>
25
26 #include "clientsettings.h"
27 #include "icon.h"
28 #include "mainwin.h"
29 #include "qtui.h"
30
31 QtMultimediaNotificationBackend::QtMultimediaNotificationBackend(QObject *parent)
32     : AbstractNotificationBackend(parent)
33 {
34     NotificationSettings notificationSettings;
35     notificationSettings.notify("QtMultimedia/Enabled", this, SLOT(enabledChanged(const QVariant &)));
36     notificationSettings.notify("QtMultimedia/AudioFile", this, SLOT(audioFileChanged(const QVariant &)));
37
38     createMediaObject(notificationSettings.value("QtMultimedia/AudioFile", QString()).toString());
39
40     _enabled = notificationSettings.value("QtMultimedia/Enabled", true).toBool();
41 }
42
43
44 void QtMultimediaNotificationBackend::notify(const Notification &notification)
45 {
46     if (_enabled && (notification.type == Highlight || notification.type == PrivMsg)) {
47         if (_media && _media->availability() == QMultimedia::Available) {
48             _media->stop();
49             _media->play();
50         }
51         else
52             QApplication::beep();
53     }
54 }
55
56
57 void QtMultimediaNotificationBackend::close(uint notificationId)
58 {
59     Q_UNUSED(notificationId);
60 }
61
62
63 void QtMultimediaNotificationBackend::enabledChanged(const QVariant &v)
64 {
65     _enabled = v.toBool();
66 }
67
68
69 void QtMultimediaNotificationBackend::audioFileChanged(const QVariant &v)
70 {
71     createMediaObject(v.toString());
72 }
73
74
75 SettingsPage *QtMultimediaNotificationBackend::createConfigWidget() const
76 {
77     return new ConfigWidget();
78 }
79
80
81 void QtMultimediaNotificationBackend::createMediaObject(const QString &file)
82 {
83     if (file.isEmpty()) {
84         _media.reset();
85         return;
86     }
87
88     _media.reset(new QMediaPlayer);
89     _media->setMedia(QUrl::fromLocalFile(file));
90 }
91
92
93 /***************************************************************************/
94
95 QtMultimediaNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
96     : SettingsPage("Internal", "QtMultimediaNotification", parent)
97 {
98     ui.setupUi(this);
99     ui.enabled->setIcon(icon::get("media-playback-start"));
100     ui.play->setIcon(icon::get("media-playback-start"));
101     ui.open->setIcon(icon::get("document-open"));
102
103     _audioAvailable = (QMediaPlayer().availability() == QMultimedia::Available);
104
105     connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
106     connect(ui.filename, SIGNAL(textChanged(const QString &)), SLOT(widgetChanged()));
107 }
108
109
110 void QtMultimediaNotificationBackend::ConfigWidget::widgetChanged()
111 {
112     if (! _audioAvailable) {
113         ui.play->setEnabled(ui.enabled->isChecked());
114         ui.open->setEnabled(false);
115         ui.filename->setEnabled(false);
116         ui.filename->setText({});
117     }
118     else {
119         ui.play->setEnabled(ui.enabled->isChecked() && !ui.filename->text().isEmpty());
120
121         bool changed = (_enabled != ui.enabled->isChecked() || _filename != ui.filename->text());
122
123         if (changed != hasChanged())
124             setChangedState(changed);
125     }
126 }
127
128
129 bool QtMultimediaNotificationBackend::ConfigWidget::hasDefaults() const
130 {
131     return true;
132 }
133
134
135 void QtMultimediaNotificationBackend::ConfigWidget::defaults()
136 {
137     ui.enabled->setChecked(false);
138     ui.filename->setText({});
139     widgetChanged();
140 }
141
142
143 void QtMultimediaNotificationBackend::ConfigWidget::load()
144 {
145     NotificationSettings s;
146     _enabled = s.value("QtMultimedia/Enabled", false).toBool();
147     _filename = s.value("QtMultimedia/AudioFile", QString()).toString();
148
149     ui.enabled->setChecked(_enabled);
150     ui.filename->setText(_filename);
151
152     setChangedState(false);
153 }
154
155
156 void QtMultimediaNotificationBackend::ConfigWidget::save()
157 {
158     NotificationSettings s;
159     s.setValue("QtMultimedia/Enabled", ui.enabled->isChecked());
160     s.setValue("QtMultimedia/AudioFile", ui.filename->text());
161     load();
162 }
163
164
165 void QtMultimediaNotificationBackend::ConfigWidget::on_open_clicked()
166 {
167     QString file = QFileDialog::getOpenFileName(this, tr("Select Audio File"));
168     if (!file.isEmpty()) {
169         ui.filename->setText(file);
170         ui.play->setEnabled(true);
171         widgetChanged();
172     }
173 }
174
175
176 void QtMultimediaNotificationBackend::ConfigWidget::on_play_clicked()
177 {
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();
183         }
184     }
185     else
186         QApplication::beep();
187 }