kdeconnect-handler.cpp 6.82 KB
Newer Older
1
/*
2
 * SPDX-FileCopyrightText: 2017 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
8
9
10
11
12
13
 */

#include <QApplication>
#include <QDialog>
#include <QCommandLineParser>
#include <QComboBox>
#include <QTextStream>
#include <QUrl>
#include <QDBusMessage>
14
15
16
#include <QFileDialog>
#include <QMessageBox>
#include <QAbstractButton>
17
18
19

#include <KAboutData>
#include <KLocalizedString>
20
#include <KUrlRequester>
21
#include <KDBusService>
22
#include <KColorSchemeManager>
23

24
25
#include <dbushelper.h>

26
#include <interfaces/devicesmodel.h>
27
#include <interfaces/devicespluginfilterproxymodel.h>
28
29
30
31
32
33
34
35
36
37
38
#include <interfaces/dbusinterfaces.h>
#include <interfaces/dbushelpers.h>
#include "kdeconnect-version.h"
#include "ui_dialog.h"

/**
 * Show only devices that can be shared to
 */

int main(int argc, char** argv)
{
39
    QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
40
    QApplication app(argc, argv);
41
    app.setWindowIcon(QIcon::fromTheme(QStringLiteral("kdeconnect")));
42
43
44
45
46
    const QString description = i18n("KDE Connect URL handler");
    KAboutData about(QStringLiteral("kdeconnect-urlhandler"),
                     description,
                     QStringLiteral(KDECONNECT_VERSION_STRING),
                     description,
Nicolas Fella's avatar
Nicolas Fella committed
47
                     KAboutLicense::GPL,
48
49
                     i18n("(C) 2017 Aleix Pol Gonzalez"));
    about.addAuthor( QStringLiteral("Aleix Pol Gonzalez"), QString(), QStringLiteral("aleixpol@kde.org") );
50
    about.setProgramLogo(QIcon(QStringLiteral(":/icons/kdeconnect/kdeconnect.svg")));
51
    KAboutData::setApplicationData(about);
52
    KDBusService dbusService(KDBusService::Unique);
53

54
#ifdef Q_OS_WIN
55
    KColorSchemeManager manager;
56
57
58
    QApplication::setStyle(QStringLiteral("breeze"));
#endif

59
    QUrl urlToShare;
60
    bool open;
61
    QString deviceId;
62
63
64
    {
        QCommandLineParser parser;
        parser.addPositionalArgument(QStringLiteral("url"), i18n("URL to share"));
65
        parser.addOption(QCommandLineOption(QStringLiteral("device"), i18n("Select a device"), i18n("id")));
66
        parser.addOption(QCommandLineOption(QStringLiteral("open"), QStringLiteral("Open the file on the remote device")));
67
68
69
        about.setupCommandLine(&parser);
        parser.process(app);
        about.processCommandLine(&parser);
70
71
        if (parser.positionalArguments().count() == 1) {
            urlToShare = QUrl::fromUserInput(parser.positionalArguments().constFirst(), QDir::currentPath(), QUrl::AssumeLocalFile);
72
        }
73
        open = parser.isSet(QStringLiteral("open"));
74
        deviceId = parser.value(QStringLiteral("device"));
75
76
77
78
    }

    DevicesModel model;
    model.setDisplayFilter(DevicesModel::Paired | DevicesModel::Reachable);
79
80
    DevicesPluginFilterProxyModel proxyModel;
    proxyModel.setPluginFilter(QStringLiteral("kdeconnect_share"));
81
82
83
    proxyModel.setSourceModel(&model);

    QDialog dialog;
84

85
86
87
    Ui::Dialog uidialog;
    uidialog.setupUi(&dialog);
    uidialog.devicePicker->setModel(&proxyModel);
88
89
90
91
92

    if (!deviceId.isEmpty()) {
        uidialog.devicePicker->setCurrentIndex(model.rowForDevice(deviceId));
    }

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    uidialog.openOnPeerCheckBox->setChecked(open);

    KUrlRequester* urlRequester = new KUrlRequester(&dialog);
    urlRequester->setStartDir(QUrl::fromLocalFile(QDir::homePath()));
    uidialog.urlHorizontalLayout->addWidget(urlRequester);

    QObject::connect(uidialog.sendUrlRadioButton, &QRadioButton::toggled, [&uidialog, urlRequester](const bool checked) {
        if (checked) {
            urlRequester->setPlaceholderText(i18n("Enter URL here"));
            urlRequester->button()->setVisible(false);
            uidialog.openOnPeerCheckBox->setVisible(false);
        }
    });

    QObject::connect(uidialog.sendFileRadioButton, &QAbstractButton::toggled, [&uidialog, urlRequester](const bool checked) {
        if (checked) {
            urlRequester->setPlaceholderText(i18n("Enter file location here"));
            urlRequester->button()->setVisible(true);
            uidialog.openOnPeerCheckBox->setVisible(true);
        }
    });

115
116
117
118
119
120
121
    QObject::connect(urlRequester, &KUrlRequester::textChanged, [urlRequester, &uidialog](const QString& newUrl)
    {
            bool isLocalFileUrl = QFileInfo(newUrl).exists() && QFileInfo(newUrl).isFile();   // we don't support sending directories yet!
            uidialog.sendFileRadioButton->setChecked(isLocalFileUrl);
            uidialog.sendUrlRadioButton->setChecked(!isLocalFileUrl);
    });

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    if (!urlToShare.isEmpty()) {
        uidialog.sendUrlRadioButton->setVisible(false);
        uidialog.sendFileRadioButton->setVisible(false);
        urlRequester->setVisible(false);

        QString displayUrl;
        if (urlToShare.scheme() == QLatin1String("tel")) {
            displayUrl = urlToShare.toDisplayString(QUrl::RemoveScheme);
            uidialog.label->setText(i18n("Device to call %1 with:", displayUrl));
        } else if (urlToShare.isLocalFile() && open) {
            displayUrl = urlToShare.toDisplayString(QUrl::PreferLocalFile);
            uidialog.label->setText(i18n("Device to open %1 on:", displayUrl));
        } else if (urlToShare.scheme() == QLatin1String("sms")) {
            displayUrl = urlToShare.toDisplayString(QUrl::PreferLocalFile);
            uidialog.label->setText(i18n("Device to send a SMS with:"));
        } else {
            displayUrl = urlToShare.toDisplayString(QUrl::PreferLocalFile);
            uidialog.label->setText(i18n("Device to send %1 to:", displayUrl));
        }

    }

    if (open || urlToShare.isLocalFile()) {
        uidialog.sendFileRadioButton->setChecked(true);
        urlRequester->setUrl(QUrl(urlToShare.toLocalFile()));
147
148

    } else {
149
150
        uidialog.sendUrlRadioButton->setChecked(true);
        urlRequester->setUrl(urlToShare);
151
152
    }

153

154
    if (dialog.exec() == QDialog::Accepted) {
155
156
        const QUrl url = urlRequester->url();
        open = uidialog.openOnPeerCheckBox->isChecked();
157
158
159
        const int currentDeviceIndex = uidialog.devicePicker->currentIndex();
        if(!url.isEmpty() && currentDeviceIndex >= 0) {
            const QString device = proxyModel.index(currentDeviceIndex, 0).data(DevicesModel::IdModelRole).toString();
160
            const QString action = open && url.isLocalFile() ? QStringLiteral("openFile") : QStringLiteral("shareUrl");
161

162
            QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), QStringLiteral("/modules/kdeconnect/devices/") + device + QStringLiteral("/share"), QStringLiteral("org.kde.kdeconnect.device.share"), action);
163
            msg.setArguments({ url.toString() });
164
            blockOnReply(DBusHelper::sessionBus().asyncCall(msg));
165
166
            return 0;
        } else {
167
168
169
            QMessageBox::critical(nullptr, description,
                                i18n("Couldn't share %1", url.toString())
                                );
170
171
172
173
174
175
            return 1;
        }
    } else {
        return 1;
    }
}