main.cpp 6.09 KB
Newer Older
1
/*
2
 * SPDX-FileCopyrightText: 2016 Aleix Pol Gonzalez <aleixpol@kde.org>
3
 *
4
 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5
6
7
 */

#include <QApplication>
8
#include <QProcess>
Weixuan Xiao's avatar
Weixuan Xiao committed
9
#include <QThread>
10
#include <QPointer>
11
12

#ifdef QSYSTRAY
13
#include <QSystemTrayIcon>
14
15
16
#else
#include <KStatusNotifierItem>
#endif
17

18
#include <KDBusService>
19
#include <KAboutData>
20
#include <KCMultiDialog>
21
22
23
24
25
26
27
#include <KLocalizedString>

#include "interfaces/devicesmodel.h"
#include "interfaces/dbusinterfaces.h"
#include "kdeconnect-version.h"
#include "deviceindicator.h"

28
29
#include <dbushelper.h>

30
31
#include "indicatorhelper.h"

32
33
34
int main(int argc, char** argv)
{
    QApplication app(argc, argv);
35
    app.setAttribute(Qt::AA_UseHighDpiPixmaps, true);
36
    KAboutData about(QStringLiteral("kdeconnect-indicator"),
37
38
39
40
41
42
43
                     i18n("KDE Connect Indicator"),
                     QStringLiteral(KDECONNECT_VERSION_STRING),
                     i18n("KDE Connect Indicator tool"),
                     KAboutLicense::GPL,
                     i18n("(C) 2016 Aleix Pol Gonzalez"));
    KAboutData::setApplicationData(about);

44
#ifdef Q_OS_WIN
45
    QApplication::setStyle(QStringLiteral("breeze"));
46
47
    IndicatorHelper helper(QUrl::fromLocalFile(qApp->applicationDirPath()));
#else
48
    IndicatorHelper helper;
49
#endif
50

51
52
    helper.preInit();

Yuri Chornoivan's avatar
Yuri Chornoivan committed
53
    // Run Daemon initialization step
54
55
    QProcess kdeconnectd;
    if (helper.daemonHook(kdeconnectd)) {
56
57
        return -1;
    }
58

59
60
61
62
    KDBusService dbusService(KDBusService::Unique);

    DevicesModel model;
    model.setDisplayFilter(DevicesModel::Reachable | DevicesModel::Paired);
63
    QMenu* menu = new QMenu;
64

65
66
    QPointer<KCMultiDialog> dialog;

67
    DaemonDbusInterface iface;
68
69

    auto refreshMenu = [&iface, &model, &menu, &helper, &dialog]() {
70
        menu->clear();
Nicolas Fella's avatar
Nicolas Fella committed
71
        auto configure = menu->addAction(QIcon::fromTheme(QStringLiteral("configure")), i18n("Configure..."));
72
73
74
75
76
77
78
79
80
        QObject::connect(configure, &QAction::triggered, configure, [&dialog]() {
            if (dialog == nullptr) {
                dialog = new KCMultiDialog;
                dialog->addModule(QStringLiteral("kcm_kdeconnect"));
                dialog->setAttribute(Qt::WA_DeleteOnClose);
                dialog->show();
            } else {
                dialog->activateWindow();
            }
81
82
        });
        for (int i=0, count = model.rowCount(); i<count; ++i) {
83
            DeviceDbusInterface* device = model.getDevice(i);
84
85
86
            auto indicator = new DeviceIndicator(device);
            QObject::connect(device, &DeviceDbusInterface::destroyed, indicator, &QObject::deleteLater);

87
            menu->addMenu(indicator);
88
        }
89
90
91
92
        const QStringList requests = iface.pairingRequests();
        if (!requests.isEmpty()) {
            menu->addSection(i18n("Pairing requests"));

93
94
            for(const auto& req: requests) {
                DeviceDbusInterface* dev = new DeviceDbusInterface(req, menu);
95
96
97
98
99
                auto pairMenu = menu->addMenu(dev->name());
                pairMenu->addAction(i18n("Pair"), dev, &DeviceDbusInterface::acceptPairing);
                pairMenu->addAction(i18n("Reject"), dev, &DeviceDbusInterface::rejectPairing);
            }
        }
100
        // Add quit menu
101
102
#if defined Q_OS_MAC

103
        menu->addAction(i18n("Quit"), [](){
104
105
106
107
            auto message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect.daemon"),
                                                        QStringLiteral("/MainApplication"),
                                                        QStringLiteral("org.qtproject.Qt.QCoreApplication"),
                                                        QStringLiteral("quit"));
108
109
110
111
112
113
            DBusHelper::sessionBus().call(message, QDBus::NoBlock); // Close our daemon
            message = QDBusMessage::createMethodCall(qApp->applicationName(),
                                                        QStringLiteral("/MainApplication"),
                                                        QStringLiteral("org.qtproject.Qt.QCoreApplication"),
                                                        QStringLiteral("quit"));
            DBusHelper::sessionBus().call(message, QDBus::NoBlock); // Close our indicator
114
        });
115
116
#elif defined Q_OS_WIN

117
        menu->addAction(i18n("Quit"), [](){qApp->quit();});
118
#endif
119
120
    };

121
    QObject::connect(&iface, &DaemonDbusInterface::pairingRequestsChangedProxy, &model, refreshMenu);
122
123
    QObject::connect(&model, &DevicesModel::rowsInserted, &model, refreshMenu);
    QObject::connect(&model, &DevicesModel::rowsRemoved, &model, refreshMenu);
124

125
126
    // Run icon to add icon path (if necessary)
    helper.iconPathHook();
Weixuan Xiao's avatar
Weixuan Xiao committed
127

128
129
#ifdef QSYSTRAY
    QSystemTrayIcon systray;
130
    helper.systrayIconHook(systray);
131
    systray.setVisible(true);
Weixuan Xiao's avatar
Weixuan Xiao committed
132
    systray.setToolTip(QStringLiteral("KDE Connect"));
133
134
135
    QObject::connect(&model, &DevicesModel::rowsChanged, &model, [&systray, &model]() {
        systray.setToolTip(i18np("%1 device connected", "%1 devices connected", model.rowCount()));
    });
136
137
138
139
140
    QObject::connect(&systray, &QSystemTrayIcon::activated, [&iface](QSystemTrayIcon::ActivationReason reason) {
        if (reason == QSystemTrayIcon::DoubleClick) {
            iface.openConfiguration();
        }
    });
141

142
    systray.setContextMenu(menu);
143
144
#else
    KStatusNotifierItem systray;
145
    helper.systrayIconHook(systray);
146
    systray.setToolTip(QStringLiteral("kdeconnect"), QStringLiteral("KDE Connect"), QStringLiteral("KDE Connect"));
147
148
149
150
151
152
153
154
155
156
157
    systray.setCategory(KStatusNotifierItem::Communications);
    systray.setStatus(KStatusNotifierItem::Passive);
    systray.setStandardActionsEnabled(false);
    QObject::connect(&model, &DevicesModel::rowsChanged, &model, [&systray, &model]() {
        const auto count = model.rowCount();
        systray.setStatus(count == 0 ? KStatusNotifierItem::Passive : KStatusNotifierItem::Active);
        systray.setToolTip(QStringLiteral("kdeconnect"), QStringLiteral("KDE Connect"), i18np("%1 device connected", "%1 devices connected", count));
    });

    systray.setContextMenu(menu);
#endif
158
159

    refreshMenu();
160

161
162
    app.setQuitOnLastWindowClosed(false);

163
164
165
    // Finish init
    helper.postInit();

166
167
    return app.exec();
}