Changeset View
Changeset View
Standalone View
Standalone View
urlhandler/kdeconnect-handler.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2017 Aleix Pol Gonzalez <aleixpol@kde.org> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include <QApplication> | ||||
22 | #include <QDialog> | ||||
23 | #include <QCommandLineParser> | ||||
24 | #include <QComboBox> | ||||
25 | #include <QTextStream> | ||||
26 | #include <QUrl> | ||||
27 | #include <QDBusMessage> | ||||
28 | #include <QDBusConnection> | ||||
29 | | ||||
30 | #include <KAboutData> | ||||
31 | #include <KLocalizedString> | ||||
32 | | ||||
33 | #include <interfaces/devicesmodel.h> | ||||
34 | #include <interfaces/devicessortproxymodel.h> | ||||
35 | #include <interfaces/dbusinterfaces.h> | ||||
36 | #include <interfaces/dbushelpers.h> | ||||
37 | #include "kdeconnect-version.h" | ||||
38 | #include "ui_dialog.h" | ||||
39 | | ||||
40 | /** | ||||
41 | * Show only devices that can be shared to | ||||
42 | */ | ||||
43 | class ShareDevicesProxyModel : public DevicesSortProxyModel | ||||
44 | { | ||||
45 | public: | ||||
46 | bool filterAcceptsRow(int source_row, const QModelIndex & source_parent) const override { | ||||
47 | const QModelIndex idx = sourceModel()->index(source_row, 0, source_parent); | ||||
48 | auto device = qobject_cast<DeviceDbusInterface*>(idx.data(DevicesModel::DeviceRole).value<QObject*>()); | ||||
49 | return device->supportedPlugins().contains(QStringLiteral("kdeconnect_share")); | ||||
50 | } | ||||
51 | }; | ||||
52 | | ||||
53 | int main(int argc, char** argv) | ||||
54 | { | ||||
55 | QApplication app(argc, argv); | ||||
56 | const QString description = i18n("KDE Connect URL handler"); | ||||
57 | KAboutData about(QStringLiteral("kdeconnect-urlhandler"), | ||||
58 | description, | ||||
59 | QStringLiteral(KDECONNECT_VERSION_STRING), | ||||
60 | description, | ||||
61 | KAboutLicense::GPL, | ||||
62 | i18n("(C) 2017 Aleix Pol Gonzalez")); | ||||
63 | about.addAuthor( QStringLiteral("Aleix Pol Gonzalez"), QString(), QStringLiteral("aleixpol@kde.org") ); | ||||
64 | KAboutData::setApplicationData(about); | ||||
65 | | ||||
66 | QUrl urlToShare; | ||||
67 | { | ||||
68 | QCommandLineParser parser; | ||||
69 | parser.addPositionalArgument(QStringLiteral("url"), i18n("URL to share")); | ||||
70 | parser.addHelpOption(); | ||||
71 | about.setupCommandLine(&parser); | ||||
72 | parser.process(app); | ||||
73 | about.processCommandLine(&parser); | ||||
74 | if (parser.positionalArguments().count() != 1) { | ||||
75 | parser.showHelp(1); | ||||
76 | return 1; | ||||
77 | } | ||||
78 | | ||||
79 | urlToShare = QUrl::fromUserInput(parser.positionalArguments().constFirst()); | ||||
80 | } | ||||
81 | | ||||
82 | DevicesModel model; | ||||
83 | model.setDisplayFilter(DevicesModel::Paired | DevicesModel::Reachable); | ||||
84 | ShareDevicesProxyModel proxyModel; | ||||
85 | proxyModel.setSourceModel(&model); | ||||
86 | | ||||
87 | QDialog dialog; | ||||
88 | dialog.setWindowTitle(urlToShare.toDisplayString()); | ||||
89 | Ui::Dialog uidialog; | ||||
90 | uidialog.setupUi(&dialog); | ||||
91 | uidialog.devicePicker->setModel(&proxyModel); | ||||
92 | uidialog.urlLabel->setText(urlToShare.toDisplayString()); | ||||
93 | if (dialog.exec() == QDialog::Accepted) { | ||||
94 | QUrl url = urlToShare; | ||||
95 | const int currentDeviceIndex = uidialog.devicePicker->currentIndex(); | ||||
96 | if(!url.isEmpty() && currentDeviceIndex >= 0) { | ||||
97 | const QString device = proxyModel.index(currentDeviceIndex, 0).data(DevicesModel::IdModelRole).toString(); | ||||
98 | | ||||
99 | QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), "/modules/kdeconnect/devices/"+device+"/share", QStringLiteral("org.kde.kdeconnect.device.share"), QStringLiteral("shareUrl")); | ||||
100 | msg.setArguments({ url.toString() }); | ||||
101 | blockOnReply(QDBusConnection::sessionBus().asyncCall(msg)); | ||||
102 | return 0; | ||||
103 | } else { | ||||
104 | QTextStream(stderr) << (i18n("Couldn't share %1", url.toString())) << endl; | ||||
105 | return 1; | ||||
106 | } | ||||
107 | } else { | ||||
108 | return 1; | ||||
109 | } | ||||
110 | } |