2011-04-27 09:05:54 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
|
2011-04-27 09:05:54 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-04-22 16:03:38 +02:00
|
|
|
#include "desktopnotificationsfactory.h"
|
2011-04-23 22:33:25 +02:00
|
|
|
#include "desktopnotification.h"
|
2014-03-09 21:51:42 +01:00
|
|
|
#include "datapaths.h"
|
2012-01-11 21:58:25 +01:00
|
|
|
#include "settings.h"
|
2011-04-22 16:03:38 +02:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QFile>
|
|
|
|
#include <QDir>
|
|
|
|
|
2014-03-03 14:47:47 +01:00
|
|
|
#if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QDBusInterface>
|
|
|
|
#endif
|
|
|
|
|
2011-04-23 22:33:25 +02:00
|
|
|
DesktopNotificationsFactory::DesktopNotificationsFactory(QObject* parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_uint(0)
|
2011-04-22 16:03:38 +02:00
|
|
|
{
|
2011-04-23 22:33:25 +02:00
|
|
|
loadSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopNotificationsFactory::loadSettings()
|
|
|
|
{
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-04-23 22:33:25 +02:00
|
|
|
settings.beginGroup("Notifications");
|
|
|
|
m_enabled = settings.value("Enabled", true).toBool();
|
|
|
|
m_timeout = settings.value("Timeout", 6000).toInt();
|
2014-03-03 14:47:47 +01:00
|
|
|
#if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
|
2011-04-23 22:33:25 +02:00
|
|
|
m_notifType = settings.value("UseNativeDesktop", true).toBool() ? DesktopNative : PopupWidget;
|
|
|
|
#else
|
|
|
|
m_notifType = PopupWidget;
|
|
|
|
#endif
|
|
|
|
m_position = settings.value("Position", QPoint(10, 10)).toPoint();
|
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
2012-07-10 11:56:24 +02:00
|
|
|
bool DesktopNotificationsFactory::supportsNativeNotifications() const
|
|
|
|
{
|
2014-03-03 14:47:47 +01:00
|
|
|
#if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
|
2012-07-10 11:56:24 +02:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-07-08 14:03:50 +02:00
|
|
|
void DesktopNotificationsFactory::showNotification(const QPixmap &icon, const QString &heading, const QString &text)
|
2011-04-23 22:33:25 +02:00
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!m_enabled) {
|
2011-04-24 22:40:35 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-24 22:40:35 +02:00
|
|
|
|
2011-04-23 22:33:25 +02:00
|
|
|
switch (m_notifType) {
|
|
|
|
case PopupWidget:
|
2012-01-18 18:36:10 +01:00
|
|
|
if (!m_desktopNotif) {
|
2011-04-23 22:33:25 +02:00
|
|
|
m_desktopNotif = new DesktopNotification();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-12-09 21:56:01 +01:00
|
|
|
m_desktopNotif.data()->setPixmap(icon);
|
|
|
|
m_desktopNotif.data()->setHeading(heading);
|
|
|
|
m_desktopNotif.data()->setText(text);
|
|
|
|
m_desktopNotif.data()->setTimeout(m_timeout);
|
|
|
|
m_desktopNotif.data()->move(m_position);
|
|
|
|
m_desktopNotif.data()->show();
|
2011-04-23 22:33:25 +02:00
|
|
|
break;
|
|
|
|
case DesktopNative:
|
2014-03-03 14:47:47 +01:00
|
|
|
#if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
|
2014-03-09 21:51:42 +01:00
|
|
|
QFile tmp(DataPaths::path(DataPaths::Temp) + QLatin1String("/qupzilla_notif.png"));
|
2011-04-24 22:40:35 +02:00
|
|
|
tmp.open(QFile::WriteOnly);
|
|
|
|
icon.save(tmp.fileName());
|
|
|
|
|
2011-04-23 22:33:25 +02:00
|
|
|
QDBusInterface dbus("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications", QDBusConnection::sessionBus());
|
|
|
|
QVariantList args;
|
2012-09-04 12:42:45 +02:00
|
|
|
args.append(QLatin1String("qupzilla"));
|
2011-04-23 22:33:25 +02:00
|
|
|
args.append(m_uint);
|
2011-04-24 22:40:35 +02:00
|
|
|
args.append(tmp.fileName());
|
2011-04-23 22:33:25 +02:00
|
|
|
args.append(heading);
|
|
|
|
args.append(text);
|
|
|
|
args.append(QStringList());
|
|
|
|
args.append(QVariantMap());
|
|
|
|
args.append(m_timeout);
|
2013-07-25 11:37:54 +02:00
|
|
|
dbus.callWithCallback("Notify", args, this, SLOT(updateLastId(QDBusMessage)), SLOT(error(QDBusError)));
|
2011-04-29 17:47:55 +02:00
|
|
|
#endif
|
2011-04-23 22:33:25 +02:00
|
|
|
break;
|
|
|
|
}
|
2011-04-22 16:03:38 +02:00
|
|
|
}
|
2011-11-19 18:21:22 +01:00
|
|
|
|
|
|
|
void DesktopNotificationsFactory::nativeNotificationPreview()
|
|
|
|
{
|
2014-03-03 14:47:47 +01:00
|
|
|
#if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
|
2014-03-09 21:51:42 +01:00
|
|
|
QFile tmp(DataPaths::path(DataPaths::Temp) + "/qupzilla_notif.png");
|
2011-11-20 17:09:10 +01:00
|
|
|
tmp.open(QFile::WriteOnly);
|
2012-07-11 18:30:00 +02:00
|
|
|
QPixmap(":icons/preferences/dialog-question.png").save(tmp.fileName());
|
2011-11-19 18:21:22 +01:00
|
|
|
|
2011-11-20 17:09:10 +01:00
|
|
|
QDBusInterface dbus("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications", QDBusConnection::sessionBus());
|
|
|
|
QVariantList args;
|
2012-09-04 12:42:45 +02:00
|
|
|
args.append(QLatin1String("qupzilla"));
|
2011-11-20 17:09:10 +01:00
|
|
|
args.append(m_uint);
|
|
|
|
args.append(tmp.fileName());
|
2012-01-21 20:27:45 +01:00
|
|
|
args.append(QObject::tr("Native System Notification"));
|
2012-09-02 11:42:41 +02:00
|
|
|
args.append(QString());
|
2011-11-20 17:09:10 +01:00
|
|
|
args.append(QStringList());
|
|
|
|
args.append(QVariantMap());
|
|
|
|
args.append(m_timeout);
|
2013-07-25 11:37:54 +02:00
|
|
|
dbus.callWithCallback("Notify", args, this, SLOT(updateLastId(QDBusMessage)), SLOT(error(QDBusError)));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-03-03 14:47:47 +01:00
|
|
|
#if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
|
2013-07-25 11:37:54 +02:00
|
|
|
void DesktopNotificationsFactory::updateLastId(const QDBusMessage &msg)
|
|
|
|
{
|
|
|
|
QVariantList list = msg.arguments();
|
2011-11-20 17:09:10 +01:00
|
|
|
if (list.count() > 0) {
|
|
|
|
m_uint = list.at(0).toInt();
|
|
|
|
}
|
2013-07-25 11:37:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopNotificationsFactory::error(const QDBusError &error)
|
|
|
|
{
|
|
|
|
qWarning() << "QDBusError:" << error.message();
|
2011-11-19 18:21:22 +01:00
|
|
|
}
|
2013-08-01 21:51:56 +02:00
|
|
|
#endif
|