a782b3160c63181fab83e7702cb9cb23bce9e17a
[quassel.git] / src / qtui / taskbarnotificationbackend.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 "taskbarnotificationbackend.h"
22
23 #include <QApplication>
24 #include <QCheckBox>
25 #include <QHBoxLayout>
26 #include <QSpinBox>
27
28 #include "clientsettings.h"
29 #include "icon.h"
30 #include "mainwin.h"
31 #include "qtui.h"
32
33 TaskbarNotificationBackend::TaskbarNotificationBackend(QObject* parent)
34     : AbstractNotificationBackend(parent)
35 {
36     NotificationSettings notificationSettings;
37     _enabled = notificationSettings.value("Taskbar/Enabled", true).toBool();
38     _timeout = notificationSettings.value("Taskbar/Timeout", 0).toInt();
39
40     notificationSettings.notify("Taskbar/Enabled", this, &TaskbarNotificationBackend::enabledChanged);
41     notificationSettings.notify("Taskbar/Timeout", this, &TaskbarNotificationBackend::timeoutChanged);
42 }
43
44 void TaskbarNotificationBackend::notify(const Notification& notification)
45 {
46     if (_enabled && (notification.type == Highlight || notification.type == PrivMsg)) {
47         QApplication::alert(QtUi::mainWindow(), _timeout);
48     }
49 }
50
51 void TaskbarNotificationBackend::close(uint notificationId)
52 {
53     Q_UNUSED(notificationId);
54 }
55
56 void TaskbarNotificationBackend::enabledChanged(const QVariant& v)
57 {
58     _enabled = v.toBool();
59 }
60
61 void TaskbarNotificationBackend::timeoutChanged(const QVariant& v)
62 {
63     _timeout = v.toInt();
64 }
65
66 SettingsPage* TaskbarNotificationBackend::createConfigWidget() const
67 {
68     return new ConfigWidget();
69 }
70
71 /***************************************************************************/
72
73 TaskbarNotificationBackend::ConfigWidget::ConfigWidget(QWidget* parent)
74     : SettingsPage("Internal", "TaskbarNotification", parent)
75 {
76     auto* layout = new QHBoxLayout(this);
77 #ifdef Q_OS_MAC
78     layout->addWidget(enabledBox = new QCheckBox(tr("Activate dock entry, timeout:"), this));
79 #else
80     layout->addWidget(enabledBox = new QCheckBox(tr("Mark taskbar entry, timeout:"), this));
81 #endif
82     enabledBox->setIcon(icon::get("flag-blue"));
83     enabledBox->setEnabled(true);
84
85     timeoutBox = new QSpinBox(this);
86     timeoutBox->setMinimum(0);
87     timeoutBox->setMaximum(99);
88     timeoutBox->setSpecialValueText(tr("Unlimited"));
89     timeoutBox->setSuffix(tr(" seconds"));
90     layout->addWidget(timeoutBox);
91     layout->addStretch(20);
92
93     connect(enabledBox, &QAbstractButton::toggled, this, &ConfigWidget::widgetChanged);
94     connect(enabledBox, &QAbstractButton::toggled, timeoutBox, &QWidget::setEnabled);
95     connect(timeoutBox, selectOverload<int>(&QSpinBox::valueChanged), this, &ConfigWidget::widgetChanged);
96 }
97
98 void TaskbarNotificationBackend::ConfigWidget::widgetChanged()
99 {
100     bool changed = (enabled != enabledBox->isChecked() || timeout / 1000 != timeoutBox->value());
101     if (changed != hasChanged())
102         setChangedState(changed);
103 }
104
105 bool TaskbarNotificationBackend::ConfigWidget::hasDefaults() const
106 {
107     return true;
108 }
109
110 void TaskbarNotificationBackend::ConfigWidget::defaults()
111 {
112     enabledBox->setChecked(true);
113     timeoutBox->setValue(0);
114     widgetChanged();
115 }
116
117 void TaskbarNotificationBackend::ConfigWidget::load()
118 {
119     NotificationSettings s;
120     enabled = s.value("Taskbar/Enabled", true).toBool();
121     timeout = s.value("Taskbar/Timeout", 0).toInt();
122
123     enabledBox->setChecked(enabled);
124     timeoutBox->setValue(timeout / 1000);
125
126     setChangedState(false);
127 }
128
129 void TaskbarNotificationBackend::ConfigWidget::save()
130 {
131     NotificationSettings s;
132     s.setValue("Taskbar/Enabled", enabledBox->isChecked());
133     s.setValue("Taskbar/Timeout", timeoutBox->value() * 1000);
134     load();
135 }