kdeconnectd.cpp 5.88 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
        qApp->setWindowIcon(QIcon(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, QStringLiteral("icons/hicolor/scalable/apps/kdeconnect.svg"))));
42
    }
43

44
    void askPairingConfirmation(Device* device) override
45
    {
46
        KNotification* notification = new KNotification(QStringLiteral("pairingRequest"), KNotification::NotificationFlag::Persistent);
47
        QTimer::singleShot(PairingHandler::pairingTimeoutMsec(), notification, &KNotification::close);
48
        notification->setIconName(QStringLiteral("dialog-information"));
49
        notification->setComponentName(QStringLiteral("kdeconnect"));
50
        notification->setTitle(QStringLiteral("KDE Connect"));
Fabian Vogt's avatar
Fabian Vogt committed
51
        notification->setText(i18n("Pairing request from %1", device->name().toHtmlEscaped()));
52
        notification->setDefaultAction(i18n("Open"));
53
        notification->setActions(QStringList() << i18n("Accept") << i18n("Reject"));
54 55
        connect(notification, &KNotification::action1Activated, device, &Device::acceptPairing);
        connect(notification, &KNotification::action2Activated, device, &Device::rejectPairing);
56
        connect(notification, QOverload<>::of(&KNotification::activated), this, []{
57
            QProcess::startDetached(QStringLiteral("kdeconnect-settings"), {});
58
        });
59 60 61
        notification->sendEvent();
    }

62
    void reportError(const QString & title, const QString & description) override
63
    {
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
64
        qCWarning(KDECONNECT_DAEMON) << title << ":" << description;
65 66
        KNotification::event(KNotification::Error, title, description);
    }
67

68
    QNetworkAccessManager* networkAccessManager() override
69 70 71 72 73 74 75
    {
        if (!m_nam) {
            m_nam = new KIO::AccessManager(this);
        }
        return m_nam;
    }

76 77 78 79 80
    KJobTrackerInterface* jobTracker() override
    {
        return KIO::getJobTracker();
    }

81 82 83 84 85 86 87 88 89 90
    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();
    }

91 92 93
    void quit() override {
        QApplication::quit();
    }
94

95 96
private:
    QNetworkAccessManager* m_nam;
97 98
};

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
// 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");
    }
}

124 125
int main(int argc, char* argv[])
{
Nicolas Fella's avatar
Nicolas Fella committed
126
    detectPlatform(argc, argv);
127

128
    QApplication app(argc, argv);
129
    KAboutData aboutData(
Nicolas Fella's avatar
Nicolas Fella committed
130
        QStringLiteral("kdeconnect.daemon"),
131 132 133 134 135 136
        i18n("KDE Connect Daemon"),
        QStringLiteral(KDECONNECT_VERSION_STRING),
        i18n("KDE Connect Daemon"),
        KAboutLicense::GPL
    );
    KAboutData::setApplicationData(aboutData);
137 138
    app.setQuitOnLastWindowClosed(false);

Weixuan Xiao's avatar
Weixuan Xiao committed
139
#ifdef USE_PRIVATE_DBUS
140
    DBusHelper::launchDBusDaemon();
Weixuan Xiao's avatar
Weixuan Xiao committed
141 142
#endif

143 144 145 146 147 148 149 150
    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)) {
151
        auto message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"),
152 153 154
                                                    QStringLiteral("/MainApplication"),
                                                    QStringLiteral("org.qtproject.Qt.QCoreApplication"),
                                                    QStringLiteral("quit"));
155
        DBusHelper::sessionBus().call(message); //deliberately block until it's done, so we register the name after the app quits
156 157
    }

158 159
    KDBusService dbusService(KDBusService::Unique);

160
    DesktopDaemon daemon;
161

Nicolas Fella's avatar
Nicolas Fella committed
162 163 164 165 166 167 168
    // 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);

169 170
    return app.exec();
}
171 172

#include "kdeconnectd.moc"