Add TaskbarNotificationBackend
[quassel.git] / src / qtui / taskbarnotificationbackend.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-08 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 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19 ***************************************************************************/
20
21 #include "taskbarnotificationbackend.h"
22
23 #include <QtGui>
24
25 #include "clientsettings.h"
26 #include "mainwin.h"
27 #include "qtui.h"
28
29 TaskbarNotificationBackend::TaskbarNotificationBackend(QObject *parent) : AbstractNotificationBackend(parent) {
30   NotificationSettings notificationSettings;
31   _enabled = notificationSettings.value("Taskbar/Enabled", true).toBool();
32   _timeout = notificationSettings.value("Taskbar/Timeout", 0).toBool();
33
34   notificationSettings.notify("Taskbar/Enabled", this, SLOT(enabledChanged(const QVariant &)));
35   notificationSettings.notify("Taskbar/Timeout", this, SLOT(timeoutChanged(const QVariant &)));
36
37   _configWidget = new ConfigWidget();
38 }
39
40 TaskbarNotificationBackend::~TaskbarNotificationBackend() {
41   delete _configWidget;
42 }
43
44 void TaskbarNotificationBackend::notify(const Notification &notification) {
45   Q_UNUSED(notification)
46   if(_enabled) {
47     QApplication::alert(QtUi::mainWindow(), _timeout);
48   }
49 }
50
51 void TaskbarNotificationBackend::close(uint notificationId) {
52   Q_UNUSED(notificationId);
53 }
54
55 void TaskbarNotificationBackend::enabledChanged(const QVariant &v) {
56   _enabled = v.toBool();
57 }
58
59 void TaskbarNotificationBackend::timeoutChanged(const QVariant &v) {
60   _timeout = v.toInt();
61 }
62
63 SettingsPage *TaskbarNotificationBackend::configWidget() const {
64   return _configWidget;
65 }
66
67 /***************************************************************************/
68
69 TaskbarNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "TaskbarNotification", parent) {
70   QHBoxLayout *layout = new QHBoxLayout(this);
71 #ifdef Q_WS_MAC
72   layout->addWidget(enabledBox = new QCheckBox(tr("Activate dock entry, timeout:"), this));
73 #else
74   layout->addWidget(enabledBox = new QCheckBox(tr("Mark taskbar entry, timeout:"), this));
75 #endif
76   timeoutBox = new QSpinBox(this);
77   timeoutBox->setMinimum(0);
78   timeoutBox->setMaximum(99);
79   timeoutBox->setSpecialValueText(tr("Unlimited"));
80   timeoutBox->setSuffix(tr(" s"));
81   layout->addWidget(timeoutBox);
82   layout->addStretch(1);
83
84   connect(enabledBox, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
85   connect(timeoutBox, SIGNAL(valueChanged(int)), SLOT(widgetChanged()));
86 }
87
88 void TaskbarNotificationBackend::ConfigWidget::widgetChanged() {
89   bool changed = (enabled != enabledBox->isChecked() || timeout/1000 != timeoutBox->value());
90   if(changed != hasChanged()) setChangedState(changed);
91 }
92
93 bool TaskbarNotificationBackend::ConfigWidget::hasDefaults() const {
94   return true;
95 }
96
97 void TaskbarNotificationBackend::ConfigWidget::defaults() {
98   enabledBox->setChecked(true);
99   timeoutBox->setValue(0);
100   widgetChanged();
101 }
102
103 void TaskbarNotificationBackend::ConfigWidget::load() {
104   NotificationSettings s;
105   enabled = s.value("Taskbar/Enabled", true).toBool();
106   timeout = s.value("Taskbar/Timeout", 0).toInt();
107
108   enabledBox->setChecked(enabled);
109   timeoutBox->setValue(timeout/1000);
110
111   setChangedState(false);
112 }
113
114 void TaskbarNotificationBackend::ConfigWidget::save() {
115   NotificationSettings s;
116   s.setValue("Taskbar/Enabled", enabledBox->isChecked());
117   s.setValue("Taskbar/Timeout", timeoutBox->value() * 1000);
118   load();
119 }