2017-06-06 16:48:30 +01:00
|
|
|
/*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2017 Aleix Pol Gonzalez <aleixpol@kde.org>
|
2017-06-06 16:48:30 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2017-06-06 16:48:30 +01:00
|
|
|
*/
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QAbstractButton>
|
2017-06-06 16:48:30 +01:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QComboBox>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QCommandLineParser>
|
2017-06-06 16:48:30 +01:00
|
|
|
#include <QDBusMessage>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QDialog>
|
2020-11-23 13:55:06 +00:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMessageBox>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QTextStream>
|
|
|
|
#include <QUrl>
|
2017-06-06 16:48:30 +01:00
|
|
|
|
|
|
|
#include <KAboutData>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <KColorSchemeManager>
|
|
|
|
#include <KDBusService>
|
2017-06-06 16:48:30 +01:00
|
|
|
#include <KLocalizedString>
|
2020-11-23 13:55:06 +00:00
|
|
|
#include <KUrlRequester>
|
2017-06-06 16:48:30 +01:00
|
|
|
|
2019-06-09 16:28:49 +01:00
|
|
|
#include <dbushelper.h>
|
|
|
|
|
2017-06-06 16:48:30 +01:00
|
|
|
#include "kdeconnect-version.h"
|
|
|
|
#include "ui_dialog.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <interfaces/dbushelpers.h>
|
|
|
|
#include <interfaces/dbusinterfaces.h>
|
|
|
|
#include <interfaces/devicesmodel.h>
|
|
|
|
#include <interfaces/devicespluginfilterproxymodel.h>
|
2017-06-06 16:48:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show only devices that can be shared to
|
|
|
|
*/
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
int main(int argc, char **argv)
|
2017-06-06 16:48:30 +01:00
|
|
|
{
|
|
|
|
QApplication app(argc, argv);
|
2021-05-23 06:12:29 +01:00
|
|
|
app.setWindowIcon(QIcon::fromTheme(QStringLiteral("kdeconnect")));
|
2017-06-06 16:48:30 +01:00
|
|
|
const QString description = i18n("KDE Connect URL handler");
|
2022-11-13 15:41:17 +00:00
|
|
|
KAboutData about(QStringLiteral("kdeconnect.handler"),
|
2017-06-06 16:48:30 +01:00
|
|
|
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"));
|
2022-09-10 22:23:52 +01:00
|
|
|
about.addAuthor(QStringLiteral("Aleix Pol Gonzalez"), QString(), QStringLiteral("aleixpol@kde.org"));
|
2021-06-12 00:31:47 +01:00
|
|
|
about.setProgramLogo(QIcon(QStringLiteral(":/icons/kdeconnect/kdeconnect.svg")));
|
2017-06-06 16:48:30 +01:00
|
|
|
KAboutData::setApplicationData(about);
|
2021-06-11 19:15:39 +01:00
|
|
|
KDBusService dbusService(KDBusService::Unique);
|
2017-06-06 16:48:30 +01:00
|
|
|
|
2021-06-02 22:45:21 +01:00
|
|
|
#ifdef Q_OS_WIN
|
2021-06-13 09:19:12 +01:00
|
|
|
KColorSchemeManager manager;
|
2021-06-02 22:45:21 +01:00
|
|
|
QApplication::setStyle(QStringLiteral("breeze"));
|
|
|
|
#endif
|
|
|
|
|
2017-06-06 16:48:30 +01:00
|
|
|
QUrl urlToShare;
|
2018-11-04 18:52:51 +00:00
|
|
|
bool open;
|
2021-06-12 01:39:44 +01:00
|
|
|
QString deviceId;
|
2017-06-06 16:48:30 +01:00
|
|
|
{
|
|
|
|
QCommandLineParser parser;
|
|
|
|
parser.addPositionalArgument(QStringLiteral("url"), i18n("URL to share"));
|
2021-06-12 01:39:44 +01:00
|
|
|
parser.addOption(QCommandLineOption(QStringLiteral("device"), i18n("Select a device"), i18n("id")));
|
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
|
|
|
about.setupCommandLine(&parser);
|
|
|
|
parser.process(app);
|
|
|
|
about.processCommandLine(&parser);
|
2020-11-23 13:55:06 +00:00
|
|
|
if (parser.positionalArguments().count() == 1) {
|
|
|
|
urlToShare = QUrl::fromUserInput(parser.positionalArguments().constFirst(), QDir::currentPath(), QUrl::AssumeLocalFile);
|
2017-06-06 16:48:30 +01:00
|
|
|
}
|
2018-11-04 18:52:51 +00:00
|
|
|
open = parser.isSet(QStringLiteral("open"));
|
2021-06-12 01:39:44 +01:00
|
|
|
deviceId = parser.value(QStringLiteral("device"));
|
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);
|
2021-06-12 01:39:44 +01:00
|
|
|
|
|
|
|
if (!deviceId.isEmpty()) {
|
|
|
|
uidialog.devicePicker->setCurrentIndex(model.rowForDevice(deviceId));
|
|
|
|
}
|
|
|
|
|
2020-11-23 13:55:06 +00:00
|
|
|
uidialog.openOnPeerCheckBox->setChecked(open);
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
KUrlRequester *urlRequester = new KUrlRequester(&dialog);
|
2020-11-23 13:55:06 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-10-22 09:17:16 +01:00
|
|
|
QObject::connect(urlRequester, &KUrlRequester::textChanged, [&urlRequester, &uidialog]() {
|
|
|
|
QUrl fileUrl(urlRequester->url());
|
|
|
|
bool isLocalFileUrl = false;
|
|
|
|
if (fileUrl.isLocalFile()) {
|
|
|
|
QFileInfo fileInfo(fileUrl.toLocalFile());
|
|
|
|
isLocalFileUrl = fileInfo.exists() && fileInfo.isFile(); // we don't support sending directories yet!
|
|
|
|
}
|
2022-09-10 22:23:52 +01:00
|
|
|
uidialog.sendFileRadioButton->setChecked(isLocalFileUrl);
|
|
|
|
uidialog.sendUrlRadioButton->setChecked(!isLocalFileUrl);
|
2021-07-02 23:53:49 +01:00
|
|
|
});
|
|
|
|
|
2020-11-23 13:55:06 +00:00
|
|
|
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()));
|
2018-01-25 09:25:57 +00:00
|
|
|
|
|
|
|
} else {
|
2020-11-23 13:55:06 +00:00
|
|
|
uidialog.sendUrlRadioButton->setChecked(true);
|
|
|
|
urlRequester->setUrl(urlToShare);
|
2018-01-25 09:25:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 16:48:30 +01:00
|
|
|
if (dialog.exec() == QDialog::Accepted) {
|
2020-11-23 13:55:06 +00:00
|
|
|
const QUrl url = urlRequester->url();
|
|
|
|
open = uidialog.openOnPeerCheckBox->isChecked();
|
2017-06-06 16:48:30 +01:00
|
|
|
const int currentDeviceIndex = uidialog.devicePicker->currentIndex();
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!url.isEmpty() && currentDeviceIndex >= 0) {
|
2017-06-06 16:48:30 +01:00
|
|
|
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
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"),
|
2023-08-27 06:25:47 +01:00
|
|
|
QLatin1String("/modules/kdeconnect/devices/%1/share").arg(device),
|
2022-09-10 22:23:52 +01:00
|
|
|
QStringLiteral("org.kde.kdeconnect.device.share"),
|
|
|
|
action);
|
|
|
|
msg.setArguments({url.toString()});
|
2022-04-12 06:40:03 +01:00
|
|
|
blockOnReply(QDBusConnection::sessionBus().asyncCall(msg));
|
2017-06-06 16:48:30 +01:00
|
|
|
return 0;
|
|
|
|
} else {
|
2022-09-10 22:23:52 +01:00
|
|
|
QMessageBox::critical(nullptr, description, i18n("Couldn't share %1", url.toString()));
|
2017-06-06 16:48:30 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|