From b36ecb7e88272d0605d9fbe8f667ec32fad1a0e7 Mon Sep 17 00:00:00 2001 From: Gurkengewuerz Date: Tue, 14 May 2019 15:43:19 +0200 Subject: [PATCH] switched from libnotify to dbus --- .circleci/config.yml | 5 +-- KShare.pro | 1 + src/main.cpp | 2 - src/notifications.cpp | 42 +-------------------- src/notifications.hpp | 1 - src/src.pro | 12 +++--- src/systemnotification.cpp | 75 ++++++++++++++++++++++++++++++++++++++ src/systemnotification.h | 41 +++++++++++++++++++++ 8 files changed, 126 insertions(+), 53 deletions(-) create mode 100644 src/systemnotification.cpp create mode 100644 src/systemnotification.h diff --git a/.circleci/config.yml b/.circleci/config.yml index 31fbff0..8acacbd 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -32,9 +32,6 @@ jobs: libxcb-util-dev \ libxcb-cursor-dev \ libxcb1-dev \ - libglib2.0-dev \ - libgtk2.0-dev \ - libnotify-dev \ - run: name: QMake Version command: qmake --version @@ -67,4 +64,4 @@ jobs: ./makedeb.sh ci; cp *.deb /kshare/ - store_artifacts: - path: /kshare/ \ No newline at end of file + path: /kshare/ diff --git a/KShare.pro b/KShare.pro index 073c15a..5f95a79 100644 --- a/KShare.pro +++ b/KShare.pro @@ -7,5 +7,6 @@ DISTFILES += \ OlderSystemFix.patch \ AppVeyor/appveyor.yml \ AppVeyor/make_installer.sh \ + .circleci/config.yml \ .travis.yml \ install.sh diff --git a/src/main.cpp b/src/main.cpp index 5244152..e3b0290 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -109,8 +109,6 @@ int main(int argc, char *argv[]) { } #endif - notifications::init(); - QCommandLineParser parser; parser.addHelpOption(); diff --git a/src/notifications.cpp b/src/notifications.cpp index 02f43ef..f37a7ed 100644 --- a/src/notifications.cpp +++ b/src/notifications.cpp @@ -1,25 +1,10 @@ #include "notifications.hpp" +#include "systemnotification.h" #include "mainwindow.hpp" #include "ui_mainwindow.h" -#include #include -#ifdef Q_OS_LINUX -#undef signals -extern "C" -{ -#include -} -#define signals public -#endif - -void notifications::init() { -#ifdef Q_OS_LINUX - notify_init("KShare"); -#endif -} - void notifications::notify(QString title, QString body, QSystemTrayIcon::MessageIcon icon) { if (!MainWindow::inst() || !MainWindow::inst()->valid()) return; notifyNolog(title, body, icon); @@ -32,30 +17,7 @@ void notifications::notifyNolog(QString title, QString body, QSystemTrayIcon::Me QApplication::alert(MainWindow::inst()); } -#ifdef Q_OS_LINUX - NotifyNotification *n = notify_notification_new(title.toLocal8Bit(), body.toLocal8Bit(), 0); - notify_notification_set_timeout(n, 5000); + SystemNotification().sendMessage(body, title, icon); - NotifyUrgency urgency; - switch(icon) { - case QSystemTrayIcon::Warning: - urgency = NotifyUrgency::NOTIFY_URGENCY_NORMAL; - break; - - case QSystemTrayIcon::Critical: - urgency = NotifyUrgency::NOTIFY_URGENCY_CRITICAL; - break; - - default: - urgency = NotifyUrgency::NOTIFY_URGENCY_LOW; - break; - } - - notify_notification_set_urgency(n, urgency); - notify_notification_show(n, 0); -#else - if (!MainWindow::inst()) return; - MainWindow::inst()->tray->showMessage(title, body, icon, 5000); -#endif MainWindow::inst()->statusBar()->showMessage(title + ": " + body); } diff --git a/src/notifications.hpp b/src/notifications.hpp index 9acceac..75306be 100644 --- a/src/notifications.hpp +++ b/src/notifications.hpp @@ -5,7 +5,6 @@ #include namespace notifications { - void init(); void notify(QString title, QString body, QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information); void notifyNolog(QString title, QString body, QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information); } // namespace notifications diff --git a/src/src.pro b/src/src.pro index 184c239..7fde2c4 100644 --- a/src/src.pro +++ b/src/src.pro @@ -73,7 +73,8 @@ SOURCES += main.cpp\ screenoverlay/screenoverlay.cpp \ screenoverlay/screenoverlaysettings.cpp \ logger.cpp \ - clipboard/clipboardcopy.cpp + clipboard/clipboardcopy.cpp \ + systemnotification.cpp HEADERS += mainwindow.hpp \ cropeditor/cropeditor.hpp \ @@ -125,7 +126,8 @@ HEADERS += mainwindow.hpp \ screenoverlay/screenoverlay.hpp \ screenoverlay/screenoverlaysettings.hpp \ logger.hpp \ - clipboard/clipboardcopy.hpp + clipboard/clipboardcopy.hpp \ + systemnotification.h nopkg { # win32 { @@ -146,9 +148,6 @@ nopkg { } else { CONFIG += link_pkgconfig PKGCONFIG += libavformat libavcodec libswscale libavutil - unix { - PKGCONFIG += libnotify - } } mac { @@ -157,6 +156,7 @@ mac { HEADERS += $$PWD/platformspecifics/mac/macbackend.hpp LIBS += -framework Carbon QMAKE_INFO_PLIST = $$PWD/../packages/macos/Info.plist + QT += dbus warning(Mac is on TODO); } else:win32 { RC_FILE = $$PWD/icon.rc @@ -168,7 +168,7 @@ mac { RC_FILE = $$PWD/icon.rc SOURCES += $$PWD/platformspecifics/x11/x11backend.cpp HEADERS += $$PWD/platformspecifics/x11/x11backend.hpp - QT += x11extras + QT += x11extras dbus LIBS += -lxcb-cursor -lxcb-xfixes -lxcb target.path = bin/ diff --git a/src/systemnotification.cpp b/src/systemnotification.cpp new file mode 100644 index 0000000..6c36861 --- /dev/null +++ b/src/systemnotification.cpp @@ -0,0 +1,75 @@ +#include "systemnotification.h" +#include +#include + +#ifndef Q_OS_WIN +#include +#include +#include +#else +#include "mainwindow.hpp" +#include "ui_mainwindow.h" +#endif + +#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX) +SystemNotification::SystemNotification(QObject *parent) : QObject(parent) { + m_interface = new QDBusInterface(QStringLiteral("org.freedesktop.Notifications"), + QStringLiteral("/org/freedesktop/Notifications"), + QStringLiteral("org.freedesktop.Notifications"), + QDBusConnection::sessionBus(), + this); +} +#else +SystemNotification::SystemNotification(QObject *parent) : QObject(parent) { + m_interface = nullptr; +} +#endif + +void SystemNotification::sendMessage(const QString &text, const QString &savePath) { + sendMessage(text, tr("KShare Info"), QSystemTrayIcon::Information, savePath); +} + +void SystemNotification::sendMessage( + const QString &text, + const QString &title, + const QSystemTrayIcon::MessageIcon icon, + const QString &savePath, + const int timeout) +{ + +#ifndef Q_OS_WIN + QList args; + QVariantMap hintsMap; + if (!savePath.isEmpty()) { + QUrl fullPath = QUrl::fromLocalFile(savePath); + // allows the notification to be dragged and dropped + hintsMap[QStringLiteral("x-kde-urls")] = QStringList({fullPath.toString()}); + } + + QString iconString = ""; + switch (icon) { + case QSystemTrayIcon::Warning: + iconString = "dialog-warning"; + break; + case QSystemTrayIcon::Critical: + iconString = "dialog-error"; + break; + default: + iconString = "dialog-information"; + break; + } + + args << (qAppName()) //appname + << static_cast(0) //id + << iconString //icon + << title //summary + << text //body + << QStringList() //actions + << hintsMap //hints + << timeout; //timeout + m_interface->callWithArgumentList(QDBus::AutoDetect, QStringLiteral("Notify"), args); +#else + if (!MainWindow::inst()) return; + MainWindow::inst()->tray->showMessage(text, title, icon, timeout); +#endif +} diff --git a/src/systemnotification.h b/src/systemnotification.h new file mode 100644 index 0000000..aec0a02 --- /dev/null +++ b/src/systemnotification.h @@ -0,0 +1,41 @@ +// Copyright(c) 2017-2019 Alejandro Sirgo Rica & Contributors +// +// This file is part of Flameshot. +// +// Flameshot 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. +// +// Flameshot 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 Flameshot. If not, see . + +#pragma once + +#include +#include + +class QDBusInterface; + +class SystemNotification : public QObject { + Q_OBJECT +public: + explicit SystemNotification(QObject *parent = nullptr); + + void sendMessage(const QString &text, + const QString &savePath = {}); + + void sendMessage(const QString &text, + const QString &title, + const QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information, + const QString &savePath = {}, + const int timeout = 5000); + +private: + QDBusInterface *m_interface; +};