Settings: unify duplicated code
[lxde/liblxqt.git] / lxqtnotification.cpp
1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * LXQt - a lightweight, Qt based, desktop toolset
5 * http://razor-qt.org
6 *
7 * Copyright (C) 2012 Alec Moskvin <alecm@gmx.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 * END_COMMON_COPYRIGHT_HEADER */
24
25 #include "lxqtnotification.h"
26 #include "lxqtnotification_p.h"
27 #include <QMessageBox>
28 #include <QDebug>
29
30 using namespace LXQt;
31
32 Notification::Notification(const QString& summary, QObject* parent) :
33 QObject(parent),
34 d_ptr(new NotificationPrivate(summary, this))
35 {
36 }
37
38 Notification::~Notification()
39 {
40 Q_D(Notification);
41 delete d;
42 }
43
44 void Notification::update()
45 {
46 Q_D(Notification);
47 d->update();
48 }
49
50 void Notification::close()
51 {
52 Q_D(Notification);
53 d->close();
54 }
55
56 void Notification::setSummary(const QString& summary)
57 {
58 Q_D(Notification);
59 d->mSummary = summary;
60 }
61
62 void Notification::setBody(const QString& body)
63 {
64 Q_D(Notification);
65 d->mBody = body;
66 }
67
68 void Notification::setIcon(const QString& iconName)
69 {
70 Q_D(Notification);
71 d->mIconName = iconName;
72 }
73
74 void Notification::setActions(const QStringList& actions, int defaultAction)
75 {
76 Q_D(Notification);
77 d->setActions(actions, defaultAction);
78 }
79
80 void Notification::setTimeout(int timeout)
81 {
82 Q_D(Notification);
83 d->mTimeout = timeout;
84 }
85
86 void Notification::setHint(const QString& hintName, const QVariant& value)
87 {
88 Q_D(Notification);
89 d->mHints.insert(hintName, value);
90 }
91
92 void Notification::setUrgencyHint(Urgency urgency)
93 {
94 Q_D(Notification);
95 d->mHints.insert("urgency", qvariant_cast<uchar>(urgency));
96 }
97
98 void Notification::clearHints()
99 {
100 Q_D(Notification);
101 d->mHints.clear();
102 }
103
104 QStringList Notification::getCapabilities()
105 {
106 Q_D(Notification);
107 return d->mInterface->GetCapabilities().value();
108 }
109
110 const Notification::ServerInfo Notification::serverInfo()
111 {
112 Q_D(Notification);
113 return d->serverInfo();
114 }
115
116 void Notification::notify(const QString& summary, const QString& body, const QString& iconName)
117 {
118 Notification notification(summary);
119 notification.setBody(body);
120 notification.setIcon(iconName);
121 notification.update();
122 }
123
124 NotificationPrivate::NotificationPrivate(const QString& summary, Notification* parent) :
125 mId(0),
126 mSummary(summary),
127 mTimeout(-1),
128 q_ptr(parent)
129 {
130 mInterface = new OrgFreedesktopNotificationsInterface("org.freedesktop.Notifications",
131 "/org/freedesktop/Notifications",
132 QDBusConnection::sessionBus(), this);
133 connect(mInterface, SIGNAL(NotificationClosed(uint, uint)), this, SLOT(notificationClosed(uint,uint)));
134 connect(mInterface, SIGNAL(ActionInvoked(uint,QString)), this, SLOT(handleAction(uint,QString)));
135 }
136
137 NotificationPrivate::~NotificationPrivate()
138 {
139 }
140
141 void NotificationPrivate::update()
142 {
143 QDBusPendingReply<uint> reply = mInterface->Notify(qAppName(), mId, mIconName, mSummary, mBody, mActions, mHints, mTimeout);
144 reply.waitForFinished();
145 if (!reply.isError())
146 {
147 mId = reply.value();
148 }
149 else
150 {
151 if (mHints.contains("urgency") && mHints.value("urgency").toInt() != Notification::UrgencyLow)
152 QMessageBox::information(0, tr("Notifications Fallback"), mSummary + " \n\n " + mBody);
153 }
154 }
155
156
157 void NotificationPrivate::setActions(QStringList actions, int defaultAction)
158 {
159 mActions.clear();
160 mDefaultAction = defaultAction;
161 for (int ix = 0; ix < actions.size(); ix++)
162 {
163 if (ix == defaultAction)
164 mActions.append("default");
165 else
166 mActions.append(QString::number(ix));
167 mActions.append(actions[ix]);
168 }
169 }
170
171 const Notification::ServerInfo NotificationPrivate::serverInfo()
172 {
173 Notification::ServerInfo info;
174 info.name = mInterface->GetServerInformation(info.vendor, info.version, info.specVersion);
175 return info;
176 }
177
178 void NotificationPrivate::handleAction(uint id, QString key)
179 {
180 if (id != mId)
181 return;
182
183 Q_Q(Notification);
184 qDebug() << "action invoked:" << key;
185 bool ok = true;
186 int keyId;
187 if (key == "default")
188 keyId = mDefaultAction;
189 else
190 keyId = key.toInt(&ok);
191
192 if (ok && keyId >= 0)
193 emit q->actionActivated(keyId);
194 }
195
196 void NotificationPrivate::close()
197 {
198 mInterface->CloseNotification(mId);
199 mId = 0;
200 }
201
202 void NotificationPrivate::notificationClosed(uint id, uint reason)
203 {
204 Q_Q(Notification);
205 if (id != 0 && id == mId)
206 {
207 mId = 0;
208 }
209 emit q->notificationClosed(Notification::CloseReason(reason));
210 }