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