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/dbushelpers.h" | ||||
35 | #include "kdeconnect-version.h" | ||||
36 | #include "ui_dialog.h" | ||||
37 | | ||||
38 | int main(int argc, char** argv) | ||||
39 | { | ||||
40 | QApplication app(argc, argv); | ||||
41 | const QString description = i18n("KDE Connect URL handler"); | ||||
42 | KAboutData about(QStringLiteral("kdeconnect-urlhandler"), | ||||
43 | description, | ||||
44 | QStringLiteral(KDECONNECT_VERSION_STRING), | ||||
45 | description, | ||||
46 | KAboutLicense::GPL, | ||||
47 | i18n("(C) 2017 Aleix Pol Gonzalez")); | ||||
48 | about.addAuthor( QStringLiteral("Aleix Pol Gonzalez"), QString(), QStringLiteral("aleixpol@kde.org") ); | ||||
49 | KAboutData::setApplicationData(about); | ||||
50 | | ||||
51 | QUrl urlToShare; | ||||
52 | { | ||||
53 | QCommandLineParser parser; | ||||
54 | parser.addPositionalArgument(QStringLiteral("url"), i18n("URL to share")); | ||||
55 | parser.addHelpOption(); | ||||
56 | about.setupCommandLine(&parser); | ||||
57 | parser.process(app); | ||||
58 | about.processCommandLine(&parser); | ||||
59 | if (parser.positionalArguments().count() != 1) { | ||||
60 | parser.showHelp(1); | ||||
61 | return 1; | ||||
62 | } | ||||
63 | | ||||
64 | urlToShare = QUrl::fromUserInput(parser.positionalArguments().constFirst()); | ||||
65 | } | ||||
66 | | ||||
67 | DevicesModel model; | ||||
68 | model.setDisplayFilter(DevicesModel::Paired | DevicesModel::Reachable); | ||||
69 | | ||||
70 | QDialog dialog; | ||||
71 | dialog.setWindowTitle(urlToShare.toDisplayString()); | ||||
72 | Ui::Dialog uidialog; | ||||
73 | uidialog.setupUi(&dialog); | ||||
74 | uidialog.devicePicker->setModel(&model); | ||||
75 | uidialog.urlLabel->setText(urlToShare.toDisplayString()); | ||||
76 | if (dialog.exec() == QDialog::Accepted) { | ||||
77 | QUrl url = urlToShare; | ||||
78 | const int currentDeviceIndex = uidialog.devicePicker->currentIndex(); | ||||
79 | if(!url.isEmpty() && currentDeviceIndex >= 0) { | ||||
80 | const QString device = model.index(currentDeviceIndex, 0).data(DevicesModel::IdModelRole).toString(); | ||||
81 | | ||||
82 | QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), "/modules/kdeconnect/devices/"+device+"/share", QStringLiteral("org.kde.kdeconnect.device.share"), QStringLiteral("shareUrl")); | ||||
83 | msg.setArguments({ url.toString() }); | ||||
84 | blockOnReply(QDBusConnection::sessionBus().asyncCall(msg)); | ||||
85 | return 0; | ||||
86 | } else { | ||||
87 | QTextStream(stderr) << (i18n("Couldn't share %1", url.toString())) << endl; | ||||
88 | return 1; | ||||
89 | } | ||||
90 | } else { | ||||
91 | return 1; | ||||
92 | } | ||||
93 | } |