kdeconnectd.cpp 6.21 KB
Newer Older
1
/**
2
 * SPDX-FileCopyrightText: 2014 Yuri Samoilenko <kinnalru@gmail.com>
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 <QNetworkAccessManager>
9
#include <QTimer>
10
11
12
#include <QCommandLineOption>
#include <QCommandLineParser>
#include <QDBusMessage>
Nicolas Fella's avatar
Nicolas Fella committed
13
#include <QSessionManager>
14
15
#include <QStandardPaths>
#include <QIcon>
16
#include <QProcess>
17

18
#include <KAboutData>
19
#include <KDBusService>
20
21
#include <KNotification>
#include <KLocalizedString>
22
#include <KIO/AccessManager>
23

24
25
#include <dbushelper.h>

26
#include "core/daemon.h"
27
#include "core/device.h"
Albert Vaca Cintora's avatar
WIPx3!    
Albert Vaca Cintora committed
28
#include "core/backends/pairinghandler.h"
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
29
#include "kdeconnect-version.h"
30
#include "kdeconnectd_debug.h"
31

32
33
34
class DesktopDaemon : public Daemon
{
    Q_OBJECT
35
    Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.daemon")
36
public:
Nicolas Fella's avatar
Nicolas Fella committed
37
    DesktopDaemon(QObject* parent = nullptr)
38
        : Daemon(parent)
Nicolas Fella's avatar
Nicolas Fella committed
39
        , m_nam(nullptr)
40
    {
41
42
        qApp->setWindowIcon(QIcon::fromTheme(QStringLiteral("kdeconnect"),
            QIcon(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, QStringLiteral("icons/hicolor/scalable/apps/kdeconnect.svg")))));
43
    }
44

45
    void askPairingConfirmation(Device* device) override
46
    {
47
        KNotification* notification = new KNotification(QStringLiteral("pairingRequest"), KNotification::NotificationFlag::Persistent);
48
        QTimer::singleShot(PairingHandler::pairingTimeoutMsec(), notification, &KNotification::close);
49
        notification->setIconName(QStringLiteral("dialog-information"));
50
        notification->setComponentName(QStringLiteral("kdeconnect"));
51
        notification->setTitle(QStringLiteral("KDE Connect"));
52
        notification->setText(i18n("Pairing request from %1\n<br/>Key: %2...", device->name().toHtmlEscaped(), QString::fromUtf8(device->verificationKey().left(8))));
53
        notification->setDefaultAction(i18n("Open"));
54
        notification->setActions(QStringList() << i18n("Accept") << i18n("Reject") << i18n("View key"));
55
56
        connect(notification, &KNotification::action1Activated, device, &Device::acceptPairing);
        connect(notification, &KNotification::action2Activated, device, &Device::rejectPairing);
57
58
59
60
61
62
        QString deviceId = device->id();
        auto openSettings = [this, deviceId] {
            openConfiguration(deviceId);
        };
        connect(notification, &KNotification::action3Activated, openSettings);
        connect(notification, QOverload<>::of(&KNotification::activated), openSettings);
63
64
65
        notification->sendEvent();
    }

66
    void reportError(const QString & title, const QString & description) override
67
    {
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
68
        qCWarning(KDECONNECT_DAEMON) << title << ":" << description;
69
70
        KNotification::event(KNotification::Error, title, description);
    }
71

72
    QNetworkAccessManager* networkAccessManager() override
73
74
75
76
77
78
79
    {
        if (!m_nam) {
            m_nam = new KIO::AccessManager(this);
        }
        return m_nam;
    }

80
81
82
83
84
    KJobTrackerInterface* jobTracker() override
    {
        return KIO::getJobTracker();
    }

85
86
87
88
89
90
91
92
93
94
    Q_SCRIPTABLE void sendSimpleNotification(const QString &eventId, const QString &title, const QString &text, const QString &iconName) override
    {
        KNotification* notification = new KNotification(eventId); //KNotification::Persistent
        notification->setIconName(iconName);
        notification->setComponentName(QStringLiteral("kdeconnect"));
        notification->setTitle(title);
        notification->setText(text);
        notification->sendEvent();
    }

95
96
97
    void quit() override {
        QApplication::quit();
    }
98

99
100
private:
    QNetworkAccessManager* m_nam;
101
102
};

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copied from plasma-workspace/libkworkspace/kworkspace.cpp
static void detectPlatform(int argc, char **argv)
{
    if (qEnvironmentVariableIsSet("QT_QPA_PLATFORM")) {
        return;
    }
    for (int i = 0; i < argc; i++) {
        if (qstrcmp(argv[i], "-platform") == 0 ||
                qstrcmp(argv[i], "--platform") == 0 ||
                QByteArray(argv[i]).startsWith("-platform=") ||
                QByteArray(argv[i]).startsWith("--platform=")) {
            return;
        }
    }
    const QByteArray sessionType = qgetenv("XDG_SESSION_TYPE");
    if (sessionType.isEmpty()) {
        return;
    }
    if (qstrcmp(sessionType, "wayland") == 0) {
        qputenv("QT_QPA_PLATFORM", "wayland");
    } else if (qstrcmp(sessionType, "x11") == 0) {
        qputenv("QT_QPA_PLATFORM", "xcb");
    }
}

128
129
int main(int argc, char* argv[])
{
Nicolas Fella's avatar
Nicolas Fella committed
130
    detectPlatform(argc, argv);
131

132
    QApplication app(argc, argv);
133
    app.setAttribute(Qt::AA_UseHighDpiPixmaps, true);
134
    KAboutData aboutData(
Nicolas Fella's avatar
Nicolas Fella committed
135
        QStringLiteral("kdeconnect.daemon"),
136
137
138
139
140
141
        i18n("KDE Connect Daemon"),
        QStringLiteral(KDECONNECT_VERSION_STRING),
        i18n("KDE Connect Daemon"),
        KAboutLicense::GPL
    );
    KAboutData::setApplicationData(aboutData);
142
143
    app.setQuitOnLastWindowClosed(false);

Weixuan Xiao's avatar
Weixuan Xiao committed
144
#ifdef USE_PRIVATE_DBUS
145
    DBusHelper::launchDBusDaemon();
Weixuan Xiao's avatar
Weixuan Xiao committed
146
147
#endif

148
149
150
151
152
153
154
155
    QCommandLineParser parser;
    QCommandLineOption replaceOption({QStringLiteral("replace")}, i18n("Replace an existing instance"));
    parser.addOption(replaceOption);
    aboutData.setupCommandLine(&parser);

    parser.process(app);
    aboutData.processCommandLine(&parser);
    if (parser.isSet(replaceOption)) {
156
        auto message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"),
157
158
159
                                                    QStringLiteral("/MainApplication"),
                                                    QStringLiteral("org.qtproject.Qt.QCoreApplication"),
                                                    QStringLiteral("quit"));
160
        DBusHelper::sessionBus().call(message); //deliberately block until it's done, so we register the name after the app quits
161
162
    }

163
164
    KDBusService dbusService(KDBusService::Unique);

165
    DesktopDaemon daemon;
166

Nicolas Fella's avatar
Nicolas Fella committed
167
168
169
170
171
172
173
    // kdeconnectd is autostarted, so disable session management to speed up startup
    auto disableSessionManagement = [](QSessionManager &sm) {
        sm.setRestartHint(QSessionManager::RestartNever);
    };
    QObject::connect(&app, &QGuiApplication::commitDataRequest, disableSessionManagement);
    QObject::connect(&app, &QGuiApplication::saveStateRequest, disableSessionManagement);

174
175
    return app.exec();
}
176
177

#include "kdeconnectd.moc"