File indexing completed on 2024-05-12 04:58:10

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "desktopnotificationsfactory.h"
0019 #include "desktopnotification.h"
0020 #include "datapaths.h"
0021 #include "settings.h"
0022 #include "mainapplication.h"
0023 #include "browserwindow.h"
0024 #include "../config.h"
0025 
0026 #include <QFile>
0027 #include <QDir>
0028 
0029 #if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
0030 #include <QDBusInterface>
0031 #endif
0032 
0033 DesktopNotificationsFactory::DesktopNotificationsFactory(QObject* parent)
0034     : QObject(parent)
0035     , m_uint(0)
0036 {
0037     loadSettings();
0038 }
0039 
0040 void DesktopNotificationsFactory::loadSettings()
0041 {
0042     Settings settings;
0043     settings.beginGroup(QSL("Notifications"));
0044     m_enabled = settings.value(QSL("Enabled"), true).toBool();
0045     m_timeout = settings.value(QSL("Timeout"), 6000).toInt();
0046 #if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
0047     m_notifType = settings.value(QSL("UseNativeDesktop"), true).toBool() ? DesktopNative : PopupWidget;
0048 #else
0049     m_notifType = PopupWidget;
0050 #endif
0051     m_position = settings.value(QSL("Position"), QPoint(10, 10)).toPoint();
0052     settings.endGroup();
0053 }
0054 
0055 bool DesktopNotificationsFactory::supportsNativeNotifications() const
0056 {
0057 #if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
0058     return true;
0059 #else
0060     return false;
0061 #endif
0062 }
0063 
0064 void DesktopNotificationsFactory::showNotification(const QString &heading, const QString &text)
0065 {
0066     showNotification(QPixmap(), heading, text);
0067 }
0068 
0069 void DesktopNotificationsFactory::showNotification(const QPixmap &icon, const QString &heading, const QString &text)
0070 {
0071     if (!m_enabled) {
0072         return;
0073     }
0074 
0075     switch (m_notifType) {
0076     case PopupWidget:
0077         if (!m_desktopNotif) {
0078             m_desktopNotif = new DesktopNotification();
0079         }
0080         m_desktopNotif.data()->setPixmap(icon);
0081         m_desktopNotif.data()->setHeading(heading);
0082         m_desktopNotif.data()->setText(text);
0083         m_desktopNotif.data()->setTimeout(m_timeout);
0084         m_desktopNotif.data()->move(m_position);
0085         m_desktopNotif.data()->show();
0086         break;
0087     case DesktopNative:
0088 #if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
0089         QFile tmp(DataPaths::path(DataPaths::Temp) + QLatin1String("/falkon_notif.png"));
0090         tmp.open(QFile::WriteOnly);
0091         icon.save(tmp.fileName());
0092 
0093         const QVariantMap hints {
0094             {QStringLiteral("desktop-entry"), QGuiApplication::desktopFileName()}
0095         };
0096 
0097         QDBusInterface dbus(QSL("org.freedesktop.Notifications"), QSL("/org/freedesktop/Notifications"), QSL("org.freedesktop.Notifications"), QDBusConnection::sessionBus());
0098         QVariantList args;
0099         args.append(QLatin1String("Falkon"));
0100         args.append(m_uint);
0101         args.append(tmp.fileName());
0102         args.append(heading);
0103         args.append(text);
0104         args.append(QStringList());
0105         args.append(hints);
0106         args.append(m_timeout);
0107         dbus.callWithCallback(QSL("Notify"), args, this, SLOT(updateLastId(QDBusMessage)), SLOT(error(QDBusError)));
0108 #endif
0109         break;
0110     }
0111 }
0112 
0113 void DesktopNotificationsFactory::nativeNotificationPreview()
0114 {
0115     Type type = m_notifType;
0116 
0117     m_notifType = DesktopNative;
0118     const QPixmap icon = mApp->getWindow()->windowIcon().pixmap(64);
0119     showNotification(icon, QObject::tr("Native System Notification"), tr("Preview"));
0120     m_notifType = type;
0121 }
0122 
0123 void DesktopNotificationsFactory::updateLastId(const QDBusMessage &msg)
0124 {
0125     Q_UNUSED(msg)
0126 #if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
0127     QVariantList list = msg.arguments();
0128     if (list.count() > 0) {
0129         m_uint = list.at(0).toInt();
0130     }
0131 #endif
0132 }
0133 
0134 void DesktopNotificationsFactory::error(const QDBusError &error)
0135 {
0136     Q_UNUSED(error)
0137 #if defined(Q_OS_UNIX) && !defined(DISABLE_DBUS)
0138     qWarning() << "QDBusError:" << error.message();
0139 #endif
0140 }