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