RazorAutostartEntry => LxQt::AutostartEntry
[lxde/liblxqt.git] / razornotification.cpp
1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * Razor - 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 "razornotification.h"
26 #include "razornotification_p.h"
27 #include <QtGui/QMessageBox>
28 #include <QDebug>
29
30 RazorNotification::RazorNotification(const QString& summary, QObject* parent) :
31 QObject(parent),
32 d_ptr(new RazorNotificationPrivate(summary, this))
33 {
34 }
35
36 RazorNotification::~RazorNotification()
37 {
38 Q_D(RazorNotification);
39 delete d;
40 }
41
42 void RazorNotification::update()
43 {
44 Q_D(RazorNotification);
45 d->update();
46 }
47
48 void RazorNotification::close()
49 {
50 Q_D(RazorNotification);
51 d->close();
52 }
53
54 void RazorNotification::setSummary(const QString& summary)
55 {
56 Q_D(RazorNotification);
57 d->mSummary = summary;
58 }
59
60 void RazorNotification::setBody(const QString& body)
61 {
62 Q_D(RazorNotification);
63 d->mBody = body;
64 }
65
66 void RazorNotification::setIcon(const QString& iconName)
67 {
68 Q_D(RazorNotification);
69 d->mIconName = iconName;
70 }
71
72 void RazorNotification::setActions(const QStringList& actions, int defaultAction)
73 {
74 Q_D(RazorNotification);
75 d->setActions(actions, defaultAction);
76 }
77
78 void RazorNotification::setTimeout(int timeout)
79 {
80 Q_D(RazorNotification);
81 d->mTimeout = timeout;
82 }
83
84 void RazorNotification::setHint(const QString& hintName, const QVariant& value)
85 {
86 Q_D(RazorNotification);
87 d->mHints.insert(hintName, value);
88 }
89
90 void RazorNotification::setUrgencyHint(Urgency urgency)
91 {
92 Q_D(RazorNotification);
93 d->mHints.insert("urgency", qvariant_cast<uchar>(urgency));
94 }
95
96 void RazorNotification::clearHints()
97 {
98 Q_D(RazorNotification);
99 d->mHints.clear();
100 }
101
102 QStringList RazorNotification::getCapabilities()
103 {
104 Q_D(RazorNotification);
105 return d->mInterface->GetCapabilities().value();
106 }
107
108 const RazorNotification::ServerInfo RazorNotification::serverInfo()
109 {
110 Q_D(RazorNotification);
111 return d->serverInfo();
112 }
113
114 void RazorNotification::notify(const QString& summary, const QString& body, const QString& iconName)
115 {
116 RazorNotification notification(summary);
117 notification.setBody(body);
118 notification.setIcon(iconName);
119 notification.update();
120 }
121
122 RazorNotificationPrivate::RazorNotificationPrivate(const QString& summary, RazorNotification* parent) :
123 mId(0),
124 mSummary(summary),
125 mTimeout(-1),
126 q_ptr(parent)
127 {
128 mInterface = new OrgFreedesktopNotificationsInterface("org.freedesktop.Notifications",
129 "/org/freedesktop/Notifications",
130 QDBusConnection::sessionBus(), this);
131 connect(mInterface, SIGNAL(NotificationClosed(uint, uint)), this, SLOT(notificationClosed(uint,uint)));
132 connect(mInterface, SIGNAL(ActionInvoked(uint,QString)), this, SLOT(handleAction(uint,QString)));
133 }
134
135 RazorNotificationPrivate::~RazorNotificationPrivate()
136 {
137 }
138
139 void RazorNotificationPrivate::update()
140 {
141 QDBusPendingReply<uint> reply = mInterface->Notify(qAppName(), mId, mIconName, mSummary, mBody, mActions, mHints, mTimeout);
142 reply.waitForFinished();
143 if (!reply.isError())
144 {
145 mId = reply.value();
146 }
147 else
148 {
149 if (mHints.contains("urgency") && mHints.value("urgency").toInt() != RazorNotification::UrgencyLow)
150 QMessageBox::information(0, tr("Notifications Fallback"), mSummary + " \n\n " + mBody);
151 }
152 }
153
154
155 void RazorNotificationPrivate::setActions(QStringList actions, int defaultAction)
156 {
157 mActions.clear();
158 mDefaultAction = defaultAction;
159 for (int ix = 0; ix < actions.size(); ix++)
160 {
161 if (ix == defaultAction)
162 mActions.append("default");
163 else
164 mActions.append(QString::number(ix));
165 mActions.append(actions[ix]);
166 }
167 }
168
169 const RazorNotification::ServerInfo RazorNotificationPrivate::serverInfo()
170 {
171 RazorNotification::ServerInfo info;
172 info.name = mInterface->GetServerInformation(info.vendor, info.version, info.specVersion);
173 return info;
174 }
175
176 void RazorNotificationPrivate::handleAction(uint id, QString key)
177 {
178 if (id != mId)
179 return;
180
181 Q_Q(RazorNotification);
182 qDebug() << "action invoked:" << key;
183 bool ok = true;
184 int keyId;
185 if (key == "default")
186 keyId = mDefaultAction;
187 else
188 keyId = key.toInt(&ok);
189
190 if (ok && key >= 0)
191 emit q->actionActivated(keyId);
192 }
193
194 void RazorNotificationPrivate::close()
195 {
196 mInterface->CloseNotification(mId);
197 mId = 0;
198 }
199
200 void RazorNotificationPrivate::notificationClosed(uint id, uint reason)
201 {
202 Q_Q(RazorNotification);
203 if (id != 0 && id == mId)
204 {
205 mId = 0;
206 }
207 emit q->notificationClosed(RazorNotification::CloseReason(reason));
208 }