| 1 | /* |
| 2 | * Copyright (C) 2021 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <QCoreApplication> |
| 21 | #include <QList> |
| 22 | #include <QVariant> |
| 23 | |
| 24 | #include "FreeDesktopNotificationService.h" |
| 25 | #include "src/common/helper/FileUrlHelper.h" |
| 26 | |
| 27 | FreeDesktopNotificationService::FreeDesktopNotificationService() : mNotificationTimeout(7000) |
| 28 | { |
| 29 | mDBusInterface = new QDBusInterface(QStringLiteral("org.freedesktop.Notifications"), |
| 30 | QStringLiteral("/org/freedesktop/Notifications"), |
| 31 | QStringLiteral("org.freedesktop.Notifications"), |
| 32 | QDBusConnection::sessionBus() |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | void FreeDesktopNotificationService::showInfo(const QString &title, const QString &message, const QString &contentUrl) |
| 37 | { |
| 38 | showToast(title, message, contentUrl, QStringLiteral("dialog-information")); |
| 39 | } |
| 40 | |
| 41 | void FreeDesktopNotificationService::showWarning(const QString &title, const QString &message, const QString &contentUrl) |
| 42 | { |
| 43 | showToast(title, message, contentUrl, QStringLiteral("dialog-warning")); |
| 44 | } |
| 45 | |
| 46 | void FreeDesktopNotificationService::showCritical(const QString &title, const QString &message, const QString &contentUrl) |
| 47 | { |
| 48 | showToast(title, message, contentUrl, QStringLiteral("dialog-error")); |
| 49 | } |
| 50 | |
| 51 | void FreeDesktopNotificationService::showToast(const QString &title, const QString &message, const QString &contentUrl, const QString &appIcon) |
| 52 | { |
| 53 | QList<QVariant> args; |
| 54 | |
| 55 | args << qAppName() // app_name |
| 56 | << static_cast<unsigned int>(0) // replaces_id (0 = does not replace existing notification) |
| 57 | << appIcon // app_icon |
| 58 | << title // summary |
| 59 | << message // body |
| 60 | << QStringList() // actions |
| 61 | << getHintsMap(contentUrl) // hints |
| 62 | << mNotificationTimeout; // expire_timeout |
| 63 | |
| 64 | mDBusInterface->callWithArgumentList(QDBus::NoBlock, QStringLiteral("Notify"), args); |
| 65 | } |
| 66 | |
| 67 | QVariantMap FreeDesktopNotificationService::getHintsMap(const QString &contentUrl) |
| 68 | { |
| 69 | QVariantMap hintsMap; |
| 70 | if (!contentUrl.isEmpty()) { |
| 71 | hintsMap[QLatin1String("image-path")] = FileUrlHelper::toFileUrl(contentUrl); |
| 72 | } |
| 73 | return hintsMap; |
| 74 | } |