RazorAutostartEntry => LxQt::AutostartEntry
[lxde/liblxqt.git] / razornotification.cpp
CommitLineData
f2cde75a
AM
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
c5fe3700
AM
30RazorNotification::RazorNotification(const QString& summary, QObject* parent) :
31 QObject(parent),
32 d_ptr(new RazorNotificationPrivate(summary, this))
33{
34}
35
f2cde75a
AM
36RazorNotification::~RazorNotification()
37{
38 Q_D(RazorNotification);
39 delete d;
40}
41
c5fe3700 42void RazorNotification::update()
f2cde75a
AM
43{
44 Q_D(RazorNotification);
c5fe3700 45 d->update();
f2cde75a
AM
46}
47
48void RazorNotification::close()
49{
50 Q_D(RazorNotification);
51 d->close();
52}
53
c5fe3700
AM
54void RazorNotification::setSummary(const QString& summary)
55{
56 Q_D(RazorNotification);
57 d->mSummary = summary;
58}
59
60void RazorNotification::setBody(const QString& body)
61{
62 Q_D(RazorNotification);
63 d->mBody = body;
64}
65
66void RazorNotification::setIcon(const QString& iconName)
67{
68 Q_D(RazorNotification);
69 d->mIconName = iconName;
70}
71
72void RazorNotification::setActions(const QStringList& actions, int defaultAction)
73{
74 Q_D(RazorNotification);
75 d->setActions(actions, defaultAction);
76}
77
78void RazorNotification::setTimeout(int timeout)
79{
80 Q_D(RazorNotification);
81 d->mTimeout = timeout;
82}
83
6deb51c9 84void RazorNotification::setHint(const QString& hintName, const QVariant& value)
da24150b
AM
85{
86 Q_D(RazorNotification);
87 d->mHints.insert(hintName, value);
88}
89
90void RazorNotification::setUrgencyHint(Urgency urgency)
91{
92 Q_D(RazorNotification);
93 d->mHints.insert("urgency", qvariant_cast<uchar>(urgency));
94}
95
96void RazorNotification::clearHints()
97{
98 Q_D(RazorNotification);
99 d->mHints.clear();
100}
101
6deb51c9
AM
102QStringList RazorNotification::getCapabilities()
103{
104 Q_D(RazorNotification);
105 return d->mInterface->GetCapabilities().value();
106}
107
108const RazorNotification::ServerInfo RazorNotification::serverInfo()
109{
110 Q_D(RazorNotification);
111 return d->serverInfo();
112}
113
c5fe3700
AM
114void 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
122RazorNotificationPrivate::RazorNotificationPrivate(const QString& summary, RazorNotification* parent) :
123 mId(0),
124 mSummary(summary),
125 mTimeout(-1),
f2cde75a
AM
126 q_ptr(parent)
127{
128 mInterface = new OrgFreedesktopNotificationsInterface("org.freedesktop.Notifications",
129 "/org/freedesktop/Notifications",
6deb51c9 130 QDBusConnection::sessionBus(), this);
f2cde75a 131 connect(mInterface, SIGNAL(NotificationClosed(uint, uint)), this, SLOT(notificationClosed(uint,uint)));
c5fe3700 132 connect(mInterface, SIGNAL(ActionInvoked(uint,QString)), this, SLOT(handleAction(uint,QString)));
f2cde75a
AM
133}
134
135RazorNotificationPrivate::~RazorNotificationPrivate()
136{
137}
138
c5fe3700 139void RazorNotificationPrivate::update()
f2cde75a 140{
da24150b 141 QDBusPendingReply<uint> reply = mInterface->Notify(qAppName(), mId, mIconName, mSummary, mBody, mActions, mHints, mTimeout);
f2cde75a 142 reply.waitForFinished();
ec81af0b 143 if (!reply.isError())
f2cde75a 144 {
ec81af0b 145 mId = reply.value();
f2cde75a 146 }
731cfd19
PV
147 else
148 {
ec81af0b
AM
149 if (mHints.contains("urgency") && mHints.value("urgency").toInt() != RazorNotification::UrgencyLow)
150 QMessageBox::information(0, tr("Notifications Fallback"), mSummary + " \n\n " + mBody);
731cfd19 151 }
f2cde75a
AM
152}
153
c5fe3700
AM
154
155void 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
6deb51c9
AM
169const 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
c5fe3700
AM
176void 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
f2cde75a
AM
194void RazorNotificationPrivate::close()
195{
196 mInterface->CloseNotification(mId);
197 mId = 0;
198}
199
200void 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}