Settings: unify duplicated code
[lxde/liblxqt.git] / lxqtnotification.cpp
CommitLineData
f2cde75a
AM
1/* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
b9223fe7 4 * LXQt - a lightweight, Qt based, desktop toolset
f2cde75a
AM
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
0e4fbf35
AS
25#include "lxqtnotification.h"
26#include "lxqtnotification_p.h"
2d13fe11 27#include <QMessageBox>
f2cde75a
AM
28#include <QDebug>
29
f05ba5af 30using namespace LXQt;
0e4fbf35
AS
31
32Notification::Notification(const QString& summary, QObject* parent) :
c5fe3700 33 QObject(parent),
0e4fbf35 34 d_ptr(new NotificationPrivate(summary, this))
c5fe3700
AM
35{
36}
37
0e4fbf35 38Notification::~Notification()
f2cde75a 39{
0e4fbf35 40 Q_D(Notification);
f2cde75a
AM
41 delete d;
42}
43
0e4fbf35 44void Notification::update()
f2cde75a 45{
0e4fbf35 46 Q_D(Notification);
c5fe3700 47 d->update();
f2cde75a
AM
48}
49
0e4fbf35 50void Notification::close()
f2cde75a 51{
0e4fbf35 52 Q_D(Notification);
f2cde75a
AM
53 d->close();
54}
55
0e4fbf35 56void Notification::setSummary(const QString& summary)
c5fe3700 57{
0e4fbf35 58 Q_D(Notification);
c5fe3700
AM
59 d->mSummary = summary;
60}
61
0e4fbf35 62void Notification::setBody(const QString& body)
c5fe3700 63{
0e4fbf35 64 Q_D(Notification);
c5fe3700
AM
65 d->mBody = body;
66}
67
0e4fbf35 68void Notification::setIcon(const QString& iconName)
c5fe3700 69{
0e4fbf35 70 Q_D(Notification);
c5fe3700
AM
71 d->mIconName = iconName;
72}
73
0e4fbf35 74void Notification::setActions(const QStringList& actions, int defaultAction)
c5fe3700 75{
0e4fbf35 76 Q_D(Notification);
c5fe3700
AM
77 d->setActions(actions, defaultAction);
78}
79
0e4fbf35 80void Notification::setTimeout(int timeout)
c5fe3700 81{
0e4fbf35 82 Q_D(Notification);
c5fe3700
AM
83 d->mTimeout = timeout;
84}
85
0e4fbf35 86void Notification::setHint(const QString& hintName, const QVariant& value)
da24150b 87{
0e4fbf35 88 Q_D(Notification);
da24150b
AM
89 d->mHints.insert(hintName, value);
90}
91
0e4fbf35 92void Notification::setUrgencyHint(Urgency urgency)
da24150b 93{
0e4fbf35 94 Q_D(Notification);
da24150b
AM
95 d->mHints.insert("urgency", qvariant_cast<uchar>(urgency));
96}
97
0e4fbf35 98void Notification::clearHints()
da24150b 99{
0e4fbf35 100 Q_D(Notification);
da24150b
AM
101 d->mHints.clear();
102}
103
0e4fbf35 104QStringList Notification::getCapabilities()
6deb51c9 105{
0e4fbf35 106 Q_D(Notification);
6deb51c9
AM
107 return d->mInterface->GetCapabilities().value();
108}
109
0e4fbf35 110const Notification::ServerInfo Notification::serverInfo()
6deb51c9 111{
0e4fbf35 112 Q_D(Notification);
6deb51c9
AM
113 return d->serverInfo();
114}
115
0e4fbf35 116void Notification::notify(const QString& summary, const QString& body, const QString& iconName)
c5fe3700 117{
0e4fbf35 118 Notification notification(summary);
c5fe3700
AM
119 notification.setBody(body);
120 notification.setIcon(iconName);
121 notification.update();
122}
123
0e4fbf35 124NotificationPrivate::NotificationPrivate(const QString& summary, Notification* parent) :
c5fe3700
AM
125 mId(0),
126 mSummary(summary),
127 mTimeout(-1),
f2cde75a
AM
128 q_ptr(parent)
129{
130 mInterface = new OrgFreedesktopNotificationsInterface("org.freedesktop.Notifications",
131 "/org/freedesktop/Notifications",
6deb51c9 132 QDBusConnection::sessionBus(), this);
f2cde75a 133 connect(mInterface, SIGNAL(NotificationClosed(uint, uint)), this, SLOT(notificationClosed(uint,uint)));
c5fe3700 134 connect(mInterface, SIGNAL(ActionInvoked(uint,QString)), this, SLOT(handleAction(uint,QString)));
f2cde75a
AM
135}
136
0e4fbf35 137NotificationPrivate::~NotificationPrivate()
f2cde75a
AM
138{
139}
140
0e4fbf35 141void NotificationPrivate::update()
f2cde75a 142{
da24150b 143 QDBusPendingReply<uint> reply = mInterface->Notify(qAppName(), mId, mIconName, mSummary, mBody, mActions, mHints, mTimeout);
f2cde75a 144 reply.waitForFinished();
ec81af0b 145 if (!reply.isError())
f2cde75a 146 {
ec81af0b 147 mId = reply.value();
f2cde75a 148 }
731cfd19
PV
149 else
150 {
0e4fbf35 151 if (mHints.contains("urgency") && mHints.value("urgency").toInt() != Notification::UrgencyLow)
ec81af0b 152 QMessageBox::information(0, tr("Notifications Fallback"), mSummary + " \n\n " + mBody);
731cfd19 153 }
f2cde75a
AM
154}
155
c5fe3700 156
0e4fbf35 157void NotificationPrivate::setActions(QStringList actions, int defaultAction)
c5fe3700
AM
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
0e4fbf35 171const Notification::ServerInfo NotificationPrivate::serverInfo()
6deb51c9 172{
0e4fbf35 173 Notification::ServerInfo info;
6deb51c9
AM
174 info.name = mInterface->GetServerInformation(info.vendor, info.version, info.specVersion);
175 return info;
176}
177
0e4fbf35 178void NotificationPrivate::handleAction(uint id, QString key)
c5fe3700
AM
179{
180 if (id != mId)
181 return;
182
0e4fbf35 183 Q_Q(Notification);
c5fe3700
AM
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
7f5f1c99 192 if (ok && keyId >= 0)
c5fe3700
AM
193 emit q->actionActivated(keyId);
194}
195
0e4fbf35 196void NotificationPrivate::close()
f2cde75a
AM
197{
198 mInterface->CloseNotification(mId);
199 mId = 0;
200}
201
0e4fbf35 202void NotificationPrivate::notificationClosed(uint id, uint reason)
f2cde75a 203{
0e4fbf35 204 Q_Q(Notification);
f2cde75a
AM
205 if (id != 0 && id == mId)
206 {
207 mId = 0;
208 }
0e4fbf35 209 emit q->notificationClosed(Notification::CloseReason(reason));
f2cde75a 210}