2017-06-06 16:48:30 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Aleix Pol Gonzalez <aleixpol@kde.org>
|
|
|
|
*
|
|
|
|
* 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
|
2019-03-23 16:29:26 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-06-06 16:48:30 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QDialog>
|
|
|
|
#include <QCommandLineParser>
|
|
|
|
#include <QComboBox>
|
|
|
|
#include <QTextStream>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QDBusMessage>
|
|
|
|
|
|
|
|
#include <KAboutData>
|
|
|
|
#include <KLocalizedString>
|
|
|
|
|
2019-06-09 16:28:49 +01:00
|
|
|
#include <dbushelper.h>
|
|
|
|
|
2017-06-06 16:48:30 +01:00
|
|
|
#include <interfaces/devicesmodel.h>
|
2019-07-21 16:51:13 +01:00
|
|
|
#include <interfaces/devicespluginfilterproxymodel.h>
|
2017-06-06 16:48:30 +01:00
|
|
|
#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)
|
|
|
|
{
|
|
|
|
QApplication app(argc, argv);
|
|
|
|
const QString description = i18n("KDE Connect URL handler");
|
|
|
|
KAboutData about(QStringLiteral("kdeconnect-urlhandler"),
|
|
|
|
description,
|
|
|
|
QStringLiteral(KDECONNECT_VERSION_STRING),
|
|
|
|
description,
|
2019-06-14 20:54:39 +01:00
|
|
|
KAboutLicense::GPL,
|
2017-06-06 16:48:30 +01:00
|
|
|
i18n("(C) 2017 Aleix Pol Gonzalez"));
|
|
|
|
about.addAuthor( QStringLiteral("Aleix Pol Gonzalez"), QString(), QStringLiteral("aleixpol@kde.org") );
|
|
|
|
KAboutData::setApplicationData(about);
|
|
|
|
|
|
|
|
QUrl urlToShare;
|
2018-11-04 18:52:51 +00:00
|
|
|
bool open;
|
2017-06-06 16:48:30 +01:00
|
|
|
{
|
|
|
|
QCommandLineParser parser;
|
|
|
|
parser.addPositionalArgument(QStringLiteral("url"), i18n("URL to share"));
|
2018-11-04 18:52:51 +00:00
|
|
|
parser.addOption(QCommandLineOption(QStringLiteral("open"), QStringLiteral("Open the file on the remote device")));
|
2017-06-06 16:48:30 +01:00
|
|
|
parser.addHelpOption();
|
|
|
|
about.setupCommandLine(&parser);
|
|
|
|
parser.process(app);
|
|
|
|
about.processCommandLine(&parser);
|
|
|
|
if (parser.positionalArguments().count() != 1) {
|
|
|
|
parser.showHelp(1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-06-14 20:53:52 +01:00
|
|
|
urlToShare = QUrl::fromUserInput(parser.positionalArguments().constFirst(), QDir::currentPath(), QUrl::AssumeLocalFile);
|
2018-11-04 18:52:51 +00:00
|
|
|
open = parser.isSet(QStringLiteral("open"));
|
2017-06-06 16:48:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DevicesModel model;
|
|
|
|
model.setDisplayFilter(DevicesModel::Paired | DevicesModel::Reachable);
|
2019-07-21 16:51:13 +01:00
|
|
|
DevicesPluginFilterProxyModel proxyModel;
|
|
|
|
proxyModel.setPluginFilter(QStringLiteral("kdeconnect_share"));
|
2017-06-06 16:48:30 +01:00
|
|
|
proxyModel.setSourceModel(&model);
|
|
|
|
|
|
|
|
QDialog dialog;
|
2018-11-03 00:03:54 +00:00
|
|
|
|
2017-06-06 16:48:30 +01:00
|
|
|
Ui::Dialog uidialog;
|
|
|
|
uidialog.setupUi(&dialog);
|
|
|
|
uidialog.devicePicker->setModel(&proxyModel);
|
2018-01-25 09:25:57 +00:00
|
|
|
|
2018-11-03 00:03:54 +00:00
|
|
|
QString displayUrl;
|
|
|
|
|
2018-01-25 09:25:57 +00:00
|
|
|
if (urlToShare.scheme() == QLatin1String("tel")) {
|
2018-11-03 00:03:54 +00:00
|
|
|
displayUrl = urlToShare.toDisplayString(QUrl::RemoveScheme);
|
|
|
|
uidialog.label->setText(i18n("Device to call %1 with:", displayUrl));
|
2018-11-04 18:52:51 +00:00
|
|
|
} else if (urlToShare.isLocalFile() && open) {
|
2018-11-03 00:03:54 +00:00
|
|
|
displayUrl = urlToShare.toDisplayString(QUrl::PreferLocalFile);
|
2019-06-14 20:54:20 +01:00
|
|
|
uidialog.label->setText(i18n("Device to open %1 on:", displayUrl));
|
2018-01-25 09:25:57 +00:00
|
|
|
} else {
|
2018-11-03 00:03:54 +00:00
|
|
|
displayUrl = urlToShare.toDisplayString(QUrl::PreferLocalFile);
|
2019-06-14 20:54:20 +01:00
|
|
|
uidialog.label->setText(i18n("Device to send %1 to:", displayUrl));
|
2018-01-25 09:25:57 +00:00
|
|
|
}
|
|
|
|
|
2018-11-03 00:03:54 +00:00
|
|
|
dialog.setWindowTitle(displayUrl);
|
|
|
|
|
2017-06-06 16:48:30 +01:00
|
|
|
if (dialog.exec() == QDialog::Accepted) {
|
|
|
|
QUrl url = urlToShare;
|
|
|
|
const int currentDeviceIndex = uidialog.devicePicker->currentIndex();
|
|
|
|
if(!url.isEmpty() && currentDeviceIndex >= 0) {
|
|
|
|
const QString device = proxyModel.index(currentDeviceIndex, 0).data(DevicesModel::IdModelRole).toString();
|
2018-11-04 18:52:51 +00:00
|
|
|
const QString action = open && url.isLocalFile() ? QStringLiteral("openFile") : QStringLiteral("shareUrl");
|
2017-06-06 16:48:30 +01:00
|
|
|
|
2019-06-10 15:40:28 +01:00
|
|
|
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), QStringLiteral("/modules/kdeconnect/devices/") + device + QStringLiteral("/share"), QStringLiteral("org.kde.kdeconnect.device.share"), action);
|
2017-06-06 16:48:30 +01:00
|
|
|
msg.setArguments({ url.toString() });
|
2019-06-09 16:28:49 +01:00
|
|
|
blockOnReply(DbusHelper::sessionBus().asyncCall(msg));
|
2017-06-06 16:48:30 +01:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
QTextStream(stderr) << (i18n("Couldn't share %1", url.toString())) << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|