Fix trimming
[quassel.git] / src / qtui / knotificationbackend.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 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 <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 void KNotificationBackend::notify(const Notification &n) {
45   QString type;
46   switch(n.type) {
47     case Highlight:
48       type = "Highlight"; break;
49     case HighlightFocused:
50       type = "HighlightFocused"; break;
51     case PrivMsg:
52       type = "PrivMsg"; break;
53     case PrivMsgFocused:
54       type = "PrivMsgFocused"; break;
55   }
56
57   QString message = QString("<b>&lt;%1&gt;</b> %2").arg(n.sender, Qt::escape(n.message));
58   KNotification *notification = KNotification::event(type, message, DesktopIcon("dialog-information"), QtUi::mainWindow(),
59                                 KNotification::RaiseWidgetOnActivation
60                                |KNotification::CloseWhenWidgetActivated
61                                |KNotification::CloseOnTimeout);
62   connect(notification, SIGNAL(activated(uint)), SLOT(notificationActivated()));
63   notification->setActions(QStringList("View"));
64   notification->setProperty("notificationId", n.notificationId);
65
66   _notifications.append(qMakePair(n.notificationId, QPointer<KNotification>(notification)));
67
68   updateToolTip();
69   QtUi::mainWindow()->systemTray()->setAlert(true);
70 }
71
72 void KNotificationBackend::removeNotificationById(uint notificationId) {
73   QList<QPair<uint, QPointer<KNotification> > >::iterator i = _notifications.begin();
74   while(i != _notifications.end()) {
75     if(i->first == notificationId) {
76       if(i->second)
77         i->second->close();
78       i = _notifications.erase(i);
79     } else
80       ++i;
81   }
82   updateToolTip();
83 }
84
85 void KNotificationBackend::close(uint notificationId) {
86   removeNotificationById(notificationId);
87   //if(!_notifications.count()) // FIXME make configurable
88     QtUi::mainWindow()->systemTray()->setAlert(false);
89 }
90
91 void KNotificationBackend::notificationActivated() {
92   uint id = 0;
93   KNotification *n = qobject_cast<KNotification *>(sender());
94   if(n)
95     id = n->property("notificationId").toUInt();
96
97   notificationActivated(id);
98 }
99
100 void KNotificationBackend::notificationActivated(SystemTray::ActivationReason reason) {
101   if(reason == SystemTray::Trigger) {
102     if( _notifications.count())
103       notificationActivated(_notifications.first().first); // oldest one
104     else
105       GraphicalUi::toggleMainWidget();
106   }
107 }
108
109 void KNotificationBackend::notificationActivated(uint notificationId) {
110   emit activated(notificationId);
111 }
112
113 void KNotificationBackend::updateToolTip() {
114   QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
115                                                _notifications.count()? tr("%n pending highlights", "", _notifications.count()) : QString());
116 }
117
118 SettingsPage *KNotificationBackend::createConfigWidget() const {
119   return new ConfigWidget();
120 }
121
122 /***************************************************************************/
123
124 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "KNotification", parent) {
125   _widget = new KNotifyConfigWidget(this);
126   _widget->setApplication("quassel");
127
128   QVBoxLayout *layout = new QVBoxLayout(this);
129   layout->addWidget(_widget);
130
131   connect(_widget, SIGNAL(changed(bool)), SLOT(widgetChanged(bool)));
132 }
133
134 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed) {
135   setChangedState(changed);
136 }
137
138 void KNotificationBackend::ConfigWidget::load() {
139   setChangedState(false);
140 }
141
142 void KNotificationBackend::ConfigWidget::save() {
143   _widget->save();
144   load();
145 }