1 /***************************************************************************
2 * Copyright (C) 2005-2020 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
TaskbarNotificationBackend(QObject * parent)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
notify(const Notification & notification)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
close(uint notificationId)51 void TaskbarNotificationBackend::close(uint notificationId)
52 {
53 Q_UNUSED(notificationId);
54 }
55
enabledChanged(const QVariant & v)56 void TaskbarNotificationBackend::enabledChanged(const QVariant& v)
57 {
58 _enabled = v.toBool();
59 }
60
timeoutChanged(const QVariant & v)61 void TaskbarNotificationBackend::timeoutChanged(const QVariant& v)
62 {
63 _timeout = v.toInt();
64 }
65
createConfigWidget() const66 SettingsPage* TaskbarNotificationBackend::createConfigWidget() const
67 {
68 return new ConfigWidget();
69 }
70
71 /***************************************************************************/
72
ConfigWidget(QWidget * parent)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
widgetChanged()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
hasDefaults() const105 bool TaskbarNotificationBackend::ConfigWidget::hasDefaults() const
106 {
107 return true;
108 }
109
defaults()110 void TaskbarNotificationBackend::ConfigWidget::defaults()
111 {
112 enabledBox->setChecked(true);
113 timeoutBox->setValue(0);
114 widgetChanged();
115 }
116
load()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
save()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 }
136