File: dbusstatusnotifier.cpp

package info (click to toggle)
syncthingtray 1.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,804 kB
  • sloc: cpp: 31,085; xml: 1,694; java: 570; sh: 81; javascript: 53; makefile: 25
file content (88 lines) | stat: -rw-r--r-- 4,333 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifdef QT_UTILITIES_SUPPORT_DBUS_NOTIFICATIONS
#include "./dbusstatusnotifier.h"

#include <syncthingmodel/syncthingicons.h>

// use meta-data of syncthingtray application here
#include "resources/../../tray/resources/config.h"

#include <QCoreApplication>
#include <QPixmap>

using namespace Data;
using namespace QtUtilities;

namespace QtGui {

inline QImage makeImage(const QIcon &icon)
{
    return icon.pixmap(QSize(128, 128)).toImage();
}

DBusStatusNotifier::DBusStatusNotifier(QObject *parent)
    : QObject(parent)
    , m_disconnectedNotification(QStringLiteral(APP_NAME), QStringLiteral("network-disconnect"), 5000)
    , m_internalErrorNotification(QStringLiteral(APP_NAME) + tr(" - internal error"), NotificationIcon::Critical, 5000)
    , m_launcherErrorNotification(QStringLiteral(APP_NAME) + tr(" - launcher error"), NotificationIcon::Critical, 5000)
    , m_syncthingNotification(tr("Syncthing notification"), NotificationIcon::Warning, 10000)
    , m_syncCompleteNotification(QStringLiteral(APP_NAME), NotificationIcon::Information, 5000)
    , m_newDevNotification(QStringLiteral(APP_NAME) + tr(" - new device"), NotificationIcon::Information, 5000)
    , m_newDirNotification(QStringLiteral(APP_NAME) + tr(" - new folder"), NotificationIcon::Information, 5000)
{
    m_disconnectedNotification.setApplicationName(QStringLiteral(APP_NAME));
    m_disconnectedNotification.setMessage(tr("Disconnected from Syncthing"));
    m_disconnectedNotification.setActions(QStringList({ QStringLiteral("reconnect"), tr("Try to reconnect") }));
    connect(&m_disconnectedNotification, &DBusNotification::actionInvoked, this, &DBusStatusNotifier::connectRequested);

    m_internalErrorNotification.setApplicationName(QStringLiteral(APP_NAME));
    m_internalErrorNotification.setActions(QStringList({ QStringLiteral("details"), tr("View details") }));
    connect(&m_internalErrorNotification, &DBusNotification::actionInvoked, this, &DBusStatusNotifier::errorDetailsRequested);

    m_internalErrorNotification.setApplicationName(QStringLiteral(APP_NAME));
    m_internalErrorNotification.setActions(QStringList({ QStringLiteral("details"), tr("View details") }));
    connect(&m_internalErrorNotification, &DBusNotification::actionInvoked, this, &DBusStatusNotifier::errorDetailsRequested);

    m_syncthingNotification.setApplicationName(QStringLiteral(APP_NAME));
    m_syncthingNotification.setActions(QStringList({ QStringLiteral("show"), tr("Show"), QStringLiteral("dismiss"), tr("Dismiss") }));
    connect(&m_syncthingNotification, &DBusNotification::actionInvoked, this, &DBusStatusNotifier::handleSyncthingNotificationAction);

    m_syncCompleteNotification.setApplicationName(QStringLiteral(APP_NAME));

    m_newDevNotification.setApplicationName(QStringLiteral(APP_NAME));
    m_newDevNotification.setActions(QStringList({ QStringLiteral("webui"), tr("Open web UI") }));
    connect(&m_newDevNotification, &DBusNotification::actionInvoked, this, &DBusStatusNotifier::webUiRequested);

    m_newDirNotification.setApplicationName(QStringLiteral(APP_NAME));
    m_newDirNotification.setActions(m_newDevNotification.actions());
    connect(&m_newDirNotification, &DBusNotification::actionInvoked, this, &DBusStatusNotifier::webUiRequested);

    const auto &iconManager = IconManager::instance();
    connect(&iconManager, &Data::IconManager::statusIconsChanged, this, &DBusStatusNotifier::setIcons);
    setIcons(iconManager.statusIcons(), iconManager.trayIcons());
}

void DBusStatusNotifier::setIcons(const StatusIcons &, const StatusIcons &icons)
{
    if (!icons.isValid) {
        return;
    }
    m_launcherErrorNotification.setImage(makeImage(icons.error));
    m_syncthingNotification.setImage(m_launcherErrorNotification.image());
    m_syncthingNotification.setImage(makeImage(icons.notify));
    m_syncCompleteNotification.setImage(makeImage(icons.syncComplete));
    m_newDevNotification.setImage(makeImage(icons.newItem));
    m_newDirNotification.setImage(m_newDevNotification.image());
}

void DBusStatusNotifier::handleSyncthingNotificationAction(const QString &action)
{
    if (action == QLatin1String("dismiss")) {
        emit dismissNotificationsRequested();
    } else if (action == QLatin1String("show")) {
        emit showNotificationsRequested();
    }
}

} // namespace QtGui

#endif