fix timestamp in Russian day change message
[quassel.git] / src / qtui / knotificationbackend.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2013 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 <KNotification>
22 #include <KNotifyConfigWidget>
23 #include <QTextDocument>
24 #include <QVBoxLayout>
25
26 #include "knotificationbackend.h"
27
28 #include "client.h"
29 #include "icon.h"
30 #include "iconloader.h"
31 #include "mainwin.h"
32 #include "networkmodel.h"
33 #include "qtui.h"
34
35 KNotificationBackend::KNotificationBackend(QObject *parent)
36     : AbstractNotificationBackend(parent)
37 {
38     connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(SystemTray::ActivationReason)),
39         SLOT(notificationActivated(SystemTray::ActivationReason)));
40
41     updateToolTip();
42 }
43
44
45 void KNotificationBackend::notify(const Notification &n)
46 {
47     QString type;
48     switch (n.type) {
49     case Highlight:
50         type = "Highlight"; break;
51     case HighlightFocused:
52         type = "HighlightFocused"; break;
53     case PrivMsg:
54         type = "PrivMsg"; break;
55     case PrivMsgFocused:
56         type = "PrivMsgFocused"; break;
57     }
58
59     QString message = QString("<b>&lt;%1&gt;</b> %2").arg(n.sender, Qt::escape(n.message));
60     KNotification *notification = KNotification::event(type, message, DesktopIcon("dialog-information"), QtUi::mainWindow(),
61         KNotification::RaiseWidgetOnActivation
62         |KNotification::CloseWhenWidgetActivated
63         |KNotification::CloseOnTimeout);
64     connect(notification, SIGNAL(activated(uint)), SLOT(notificationActivated()));
65     notification->setActions(QStringList("View"));
66     notification->setProperty("notificationId", n.notificationId);
67
68     _notifications.append(qMakePair(n.notificationId, QPointer<KNotification>(notification)));
69
70     updateToolTip();
71     QtUi::mainWindow()->systemTray()->setAlert(true);
72 }
73
74
75 void KNotificationBackend::removeNotificationById(uint notificationId)
76 {
77     QList<QPair<uint, QPointer<KNotification> > >::iterator i = _notifications.begin();
78     while (i != _notifications.end()) {
79         if (i->first == notificationId) {
80             if (i->second)
81                 i->second->close();
82             i = _notifications.erase(i);
83         }
84         else
85             ++i;
86     }
87     updateToolTip();
88 }
89
90
91 void KNotificationBackend::close(uint notificationId)
92 {
93     removeNotificationById(notificationId);
94     //if(!_notifications.count()) // FIXME make configurable
95     QtUi::mainWindow()->systemTray()->setAlert(false);
96 }
97
98
99 void KNotificationBackend::notificationActivated()
100 {
101     uint id = 0;
102     KNotification *n = qobject_cast<KNotification *>(sender());
103     if (n)
104         id = n->property("notificationId").toUInt();
105
106     notificationActivated(id);
107 }
108
109
110 void KNotificationBackend::notificationActivated(SystemTray::ActivationReason reason)
111 {
112     if (reason == SystemTray::Trigger) {
113         if (_notifications.count())
114             notificationActivated(_notifications.first().first);  // oldest one
115         else
116             GraphicalUi::toggleMainWidget();
117     }
118 }
119
120
121 void KNotificationBackend::notificationActivated(uint notificationId)
122 {
123     emit activated(notificationId);
124 }
125
126
127 void KNotificationBackend::updateToolTip()
128 {
129     QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
130         _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count()) : QString());
131 }
132
133
134 SettingsPage *KNotificationBackend::createConfigWidget() const
135 {
136     return new ConfigWidget();
137 }
138
139
140 /***************************************************************************/
141
142 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "KNotification", parent)
143 {
144     _widget = new KNotifyConfigWidget(this);
145     _widget->setApplication("quassel");
146
147     QVBoxLayout *layout = new QVBoxLayout(this);
148     layout->addWidget(_widget);
149
150     connect(_widget, SIGNAL(changed(bool)), SLOT(widgetChanged(bool)));
151 }
152
153
154 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed)
155 {
156     setChangedState(changed);
157 }
158
159
160 void KNotificationBackend::ConfigWidget::load()
161 {
162     setChangedState(false);
163 }
164
165
166 void KNotificationBackend::ConfigWidget::save()
167 {
168     _widget->save();
169     load();
170 }