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