Falkon Develop
Cross-platform Qt-based web browser
desktopnotificationsfactory.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
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 3 of the License, or
8* (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
19#include "desktopnotification.h"
20#include "datapaths.h"
21#include "settings.h"
22#include "mainapplication.h"
23#include "browserwindow.h"
24#include "../config.h"
25
26#include <QFile>
27#include <QDir>
28
29#if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
30#include <QDBusInterface>
31#endif
32
34 : QObject(parent)
35 , m_uint(0)
36{
38}
39
41{
42 Settings settings;
43 settings.beginGroup(QSL("Notifications"));
44 m_enabled = settings.value(QSL("Enabled"), true).toBool();
45 m_timeout = settings.value(QSL("Timeout"), 6000).toInt();
46#if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
47 m_notifType = settings.value(QSL("UseNativeDesktop"), true).toBool() ? DesktopNative : PopupWidget;
48#else
49 m_notifType = PopupWidget;
50#endif
51 m_position = settings.value(QSL("Position"), QPoint(10, 10)).toPoint();
52 settings.endGroup();
53}
54
56{
57#if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
58 return true;
59#else
60 return false;
61#endif
62}
63
64void DesktopNotificationsFactory::showNotification(const QString &heading, const QString &text)
65{
66 showNotification(QPixmap(), heading, text);
67}
68
69void DesktopNotificationsFactory::showNotification(const QPixmap &icon, const QString &heading, const QString &text)
70{
71 if (!m_enabled) {
72 return;
73 }
74
75 switch (m_notifType) {
76 case PopupWidget:
77 if (!m_desktopNotif) {
78 m_desktopNotif = new DesktopNotification();
79 }
80 m_desktopNotif.data()->setPixmap(icon);
81 m_desktopNotif.data()->setHeading(heading);
82 m_desktopNotif.data()->setText(text);
83 m_desktopNotif.data()->setTimeout(m_timeout);
84 m_desktopNotif.data()->move(m_position);
85 m_desktopNotif.data()->show();
86 break;
87 case DesktopNative:
88#if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
89 QFile tmp(DataPaths::path(DataPaths::Temp) + QLatin1String("/falkon_notif.png"));
90 tmp.open(QFile::WriteOnly);
91 icon.save(tmp.fileName());
92
93 const QVariantMap hints {
94 {QStringLiteral("desktop-entry"), QGuiApplication::desktopFileName()}
95 };
96
97 QDBusInterface dbus(QSL("org.freedesktop.Notifications"), QSL("/org/freedesktop/Notifications"), QSL("org.freedesktop.Notifications"), QDBusConnection::sessionBus());
98 QVariantList args;
99 args.append(QLatin1String("Falkon"));
100 args.append(m_uint);
101 args.append(tmp.fileName());
102 args.append(heading);
103 args.append(text);
104 args.append(QStringList());
105 args.append(hints);
106 args.append(m_timeout);
107 dbus.callWithCallback(QSL("Notify"), args, this, SLOT(updateLastId(QDBusMessage)), SLOT(error(QDBusError)));
108#endif
109 break;
110 }
111}
112
114{
115 Type type = m_notifType;
116
117 m_notifType = DesktopNative;
118 const QPixmap icon = mApp->getWindow()->windowIcon().pixmap(64);
119 showNotification(icon, QObject::tr("Native System Notification"), tr("Preview"));
120 m_notifType = type;
121}
122
123void DesktopNotificationsFactory::updateLastId(const QDBusMessage &msg)
124{
125 Q_UNUSED(msg)
126#if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
127 QVariantList list = msg.arguments();
128 if (list.count() > 0) {
129 m_uint = list.at(0).toInt();
130 }
131#endif
132}
133
134void DesktopNotificationsFactory::error(const QDBusError &error)
135{
136 Q_UNUSED(error)
137#if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
138 qWarning() << "QDBusError:" << error.message();
139#endif
140}
static QString path(Path type)
Definition: datapaths.cpp:66
DesktopNotificationsFactory(QObject *parent=nullptr)
void showNotification(const QString &heading, const QString &text)
void beginGroup(const QString &prefix)
Definition: settings.cpp:79
void endGroup()
Definition: settings.cpp:84
QVariant value(const QString &key, const QVariant &defaultValue=QVariant())
Definition: settings.cpp:74
#define mApp
#define QSL(x)
Definition: qzcommon.h:40