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