kdeconnectd.cpp 6.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/**
 * Copyright 2014 Yuri Samoilenko <kinnalru@gmail.com>
 *
 * This program 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 2 of
 * the License or (at your option) version 3 or any later version
 * accepted by the membership of KDE e.V. (or its successor approved
 * by the membership of KDE e.V.), which shall act as a proxy
 * defined in Section 14 of version 3 of the license.
 *
 * This program 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
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 20 21
 */

#include <QApplication>
22
#include <QNetworkAccessManager>
23
#include <QTimer>
24
#include <QLoggingCategory>
25 26 27
#include <QCommandLineOption>
#include <QCommandLineParser>
#include <QDBusMessage>
Nicolas Fella's avatar
Nicolas Fella committed
28
#include <QSessionManager>
29 30
#include <QStandardPaths>
#include <QIcon>
31

32
#include <KAboutData>
33
#include <KDBusService>
34 35
#include <KNotification>
#include <KLocalizedString>
36
#include <KIO/AccessManager>
37

38 39
#include <dbushelper.h>

40
#include "core/daemon.h"
41
#include "core/device.h"
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
42
#include "core/backends/pairinghandler.h"
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
43
#include "kdeconnect-version.h"
44

45 46 47
Q_DECLARE_LOGGING_CATEGORY(KDECONNECT_DAEMON)
Q_LOGGING_CATEGORY(KDECONNECT_DAEMON, "kdeconnect.daemon")

48 49 50
class DesktopDaemon : public Daemon
{
    Q_OBJECT
51
    Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.daemon")
52
public:
Nicolas Fella's avatar
Nicolas Fella committed
53
    DesktopDaemon(QObject* parent = nullptr)
54
        : Daemon(parent)
Nicolas Fella's avatar
Nicolas Fella committed
55
        , m_nam(nullptr)
56 57 58
    {
        qApp->setWindowIcon(QIcon(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, QStringLiteral("icons/hicolor/scalable/apps/kdeconnect.svgz"))));
    }
59

60
    void askPairingConfirmation(Device* device) override
61
    {
62
        KNotification* notification = new KNotification(QStringLiteral("pairingRequest"));
63
        notification->setIconName(QStringLiteral("dialog-information"));
64
        notification->setComponentName(QStringLiteral("kdeconnect"));
65
        notification->setTitle(QStringLiteral("KDE Connect"));
Fabian Vogt's avatar
Fabian Vogt committed
66
        notification->setText(i18n("Pairing request from %1", device->name().toHtmlEscaped()));
67
        notification->setActions(QStringList() << i18n("Accept") << i18n("Reject"));
68 69 70
//         notification->setTimeout(PairingHandler::pairingTimeoutMsec());
        connect(notification, &KNotification::action1Activated, device, &Device::acceptPairing);
        connect(notification, &KNotification::action2Activated, device, &Device::rejectPairing);
71 72 73
        notification->sendEvent();
    }

74
    void reportError(const QString & title, const QString & description) override
75
    {
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
76
        qCWarning(KDECONNECT_DAEMON) << title << ":" << description;
77 78
        KNotification::event(KNotification::Error, title, description);
    }
79

80
    QNetworkAccessManager* networkAccessManager() override
81 82 83 84 85 86 87
    {
        if (!m_nam) {
            m_nam = new KIO::AccessManager(this);
        }
        return m_nam;
    }

88 89 90 91 92 93 94 95 96 97
    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();
    }

98 99 100
    void quit() override {
        QApplication::quit();
    }
101

102 103
private:
    QNetworkAccessManager* m_nam;
104 105
};

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
// 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");
    }
}

131 132
int main(int argc, char* argv[])
{
Nicolas Fella's avatar
Nicolas Fella committed
133
    detectPlatform(argc, argv);
134

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

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

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

165 166
    KDBusService dbusService(KDBusService::Unique);

167
    DesktopDaemon daemon;
168

Nicolas Fella's avatar
Nicolas Fella committed
169 170 171 172 173 174 175
    // 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);

176 177
    return app.exec();
}
178 179

#include "kdeconnectd.moc"