modernize: Use std::make_unique
[quassel.git] / src / qtui / knotificationbackend.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 "knotificationbackend.h"
22
23 #include <QTextDocument>
24 #include <QVBoxLayout>
25
26 #include <KNotifications/KNotification>
27 #include <KNotifyConfig/KNotifyConfigWidget>
28
29 #include "client.h"
30 #include "icon.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, n.message.toHtmlEscaped());
60     KNotification *notification = KNotification::event(type, message, icon::get("dialog-information").pixmap(48), 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 }
72
73
74 void KNotificationBackend::removeNotificationById(uint notificationId)
75 {
76     QList<QPair<uint, QPointer<KNotification> > >::iterator i = _notifications.begin();
77     while (i != _notifications.end()) {
78         if (i->first == notificationId) {
79             if (i->second)
80                 i->second->close();
81             i = _notifications.erase(i);
82         }
83         else
84             ++i;
85     }
86     updateToolTip();
87 }
88
89
90 void KNotificationBackend::close(uint notificationId)
91 {
92     removeNotificationById(notificationId);
93     //if(!_notifications.count()) // FIXME make configurable
94 }
95
96
97 void KNotificationBackend::notificationActivated()
98 {
99     uint id = 0;
100     KNotification *n = qobject_cast<KNotification *>(sender());
101     if (n)
102         id = n->property("notificationId").toUInt();
103
104     notificationActivated(id);
105 }
106
107
108 void KNotificationBackend::notificationActivated(SystemTray::ActivationReason reason)
109 {
110     if (reason == SystemTray::Trigger) {
111         if (_notifications.count())
112             notificationActivated(_notifications.first().first);  // oldest one
113         else
114             GraphicalUi::toggleMainWidget();
115     }
116 }
117
118
119 void KNotificationBackend::notificationActivated(uint notificationId)
120 {
121     emit activated(notificationId);
122 }
123
124
125 void KNotificationBackend::updateToolTip()
126 {
127     QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
128         _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count()) : QString());
129 }
130
131
132 SettingsPage *KNotificationBackend::createConfigWidget() const
133 {
134     return new ConfigWidget();
135 }
136
137
138 /***************************************************************************/
139
140 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "KNotification", parent)
141 {
142     _widget = new KNotifyConfigWidget(this);
143     _widget->setApplication("quassel");
144
145     QVBoxLayout *layout = new QVBoxLayout(this);
146     layout->addWidget(_widget);
147
148     connect(_widget, SIGNAL(changed(bool)), SLOT(widgetChanged(bool)));
149 }
150
151
152 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed)
153 {
154     setChangedState(changed);
155 }
156
157
158 void KNotificationBackend::ConfigWidget::load()
159 {
160     setChangedState(false);
161 }
162
163
164 void KNotificationBackend::ConfigWidget::save()
165 {
166     _widget->save();
167     load();
168 }