diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,7 @@ add_subdirectory(cli) add_subdirectory(indicator) add_subdirectory(fileitemactionplugin) +add_subdirectory(urlhandler) if(KF5DocTools_FOUND) add_subdirectory(doc) endif() diff --git a/cli/kdeconnect-cli.cpp b/cli/kdeconnect-cli.cpp --- a/cli/kdeconnect-cli.cpp +++ b/cli/kdeconnect-cli.cpp @@ -27,33 +27,13 @@ #include #include -#include #include "interfaces/devicesmodel.h" #include "interfaces/notificationsmodel.h" #include "interfaces/dbusinterfaces.h" +#include "interfaces/dbushelpers.h" #include "kdeconnect-version.h" -template -Q_REQUIRED_RESULT T blockOnReply(QDBusPendingReply reply) -{ - reply.waitForFinished(); - if (reply.isError()) { - QTextStream(stderr) << i18n("error: ") << reply.error().message() << endl; - exit(1); - } - return reply.value(); -} - -void blockOnReply(QDBusPendingReply reply) -{ - reply.waitForFinished(); - if (reply.isError()) { - QTextStream(stderr) << i18n("error: ") << reply.error().message() << endl; - exit(1); - } -} - int main(int argc, char** argv) { QCoreApplication app(argc, argv); diff --git a/interfaces/dbushelpers.h b/interfaces/dbushelpers.h new file mode 100644 --- /dev/null +++ b/interfaces/dbushelpers.h @@ -0,0 +1,48 @@ +/* + * Copyright 2015 Aleix Pol Gonzalez + * + * 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 + * along with this program. If not, see . + */ + +#ifndef DBUSHELPERS_H +#define DBUSHELPERS_H + +#include +#include +#include + +template +Q_REQUIRED_RESULT T blockOnReply(QDBusPendingReply reply) +{ + reply.waitForFinished(); + if (reply.isError()) { + QTextStream(stderr) << i18n("error: ") << reply.error().message() << endl; + exit(1); + } + return reply.value(); +} + +void blockOnReply(QDBusPendingReply reply) +{ + reply.waitForFinished(); + if (reply.isError()) { + QTextStream(stderr) << i18n("error: ") << reply.error().message() << endl; + exit(1); + } +} + +#endif diff --git a/urlhandler/CMakeLists.txt b/urlhandler/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/urlhandler/CMakeLists.txt @@ -0,0 +1,13 @@ +ki18n_wrap_ui(telhandler_SRCS dialog.ui) + +add_executable(kdeconnect-handler kdeconnect-handler.cpp ${telhandler_SRCS}) + +target_link_libraries(kdeconnect-handler + kdeconnectinterfaces + Qt5::Widgets + KF5::CoreAddons + KF5::I18n +) + +install(TARGETS kdeconnect-handler ${INSTALL_TARGETS_DEFAULT_ARGS}) +install(PROGRAMS org.kde.kdeconnect.telhandler.desktop DESTINATION ${XDG_APPS_INSTALL_DIR} ) diff --git a/urlhandler/Messages.sh b/urlhandler/Messages.sh new file mode 100644 --- /dev/null +++ b/urlhandler/Messages.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +$XGETTEXT `find . -name '*.cpp'` -o $podir/kdeconnect-urlhandler.pot diff --git a/urlhandler/dialog.ui b/urlhandler/dialog.ui new file mode 100644 --- /dev/null +++ b/urlhandler/dialog.ui @@ -0,0 +1,82 @@ + + + Dialog + + + + 0 + 0 + 420 + 104 + + + + + + + + + Device to open the url with: + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + Dialog + accept() + + + 224 + 80 + + + 157 + 103 + + + + + buttonBox + rejected() + Dialog + reject() + + + 292 + 86 + + + 286 + 103 + + + + + diff --git a/urlhandler/kdeconnect-handler.cpp b/urlhandler/kdeconnect-handler.cpp new file mode 100644 --- /dev/null +++ b/urlhandler/kdeconnect-handler.cpp @@ -0,0 +1,93 @@ +/* + * Copyright 2017 Aleix Pol Gonzalez + * + * 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 + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "interfaces/devicesmodel.h" +#include "interfaces/dbushelpers.h" +#include "kdeconnect-version.h" +#include "ui_dialog.h" + +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, + KAboutLicense::GPL, + i18n("(C) 2017 Aleix Pol Gonzalez")); + about.addAuthor( QStringLiteral("Aleix Pol Gonzalez"), QString(), QStringLiteral("aleixpol@kde.org") ); + KAboutData::setApplicationData(about); + + QUrl urlToShare; + { + QCommandLineParser parser; + parser.addPositionalArgument(QStringLiteral("url"), i18n("URL to share")); + parser.addHelpOption(); + about.setupCommandLine(&parser); + parser.process(app); + about.processCommandLine(&parser); + if (parser.positionalArguments().count() != 1) { + parser.showHelp(1); + return 1; + } + + urlToShare = QUrl::fromUserInput(parser.positionalArguments().constFirst()); + } + + DevicesModel model; + model.setDisplayFilter(DevicesModel::Paired | DevicesModel::Reachable); + + QDialog dialog; + dialog.setWindowTitle(urlToShare.toDisplayString()); + Ui::Dialog uidialog; + uidialog.setupUi(&dialog); + uidialog.devicePicker->setModel(&model); + uidialog.urlLabel->setText(urlToShare.toDisplayString()); + if (dialog.exec() == QDialog::Accepted) { + QUrl url = urlToShare; + const int currentDeviceIndex = uidialog.devicePicker->currentIndex(); + if(!url.isEmpty() && currentDeviceIndex >= 0) { + const QString device = model.index(currentDeviceIndex, 0).data(DevicesModel::IdModelRole).toString(); + + QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), "/modules/kdeconnect/devices/"+device+"/share", QStringLiteral("org.kde.kdeconnect.device.share"), QStringLiteral("shareUrl")); + msg.setArguments({ url.toString() }); + blockOnReply(QDBusConnection::sessionBus().asyncCall(msg)); + return 0; + } else { + QTextStream(stderr) << (i18n("Couldn't share %1", url.toString())) << endl; + return 1; + } + } else { + return 1; + } +} diff --git a/urlhandler/org.kde.kdeconnect.telhandler.desktop b/urlhandler/org.kde.kdeconnect.telhandler.desktop new file mode 100644 --- /dev/null +++ b/urlhandler/org.kde.kdeconnect.telhandler.desktop @@ -0,0 +1,8 @@ +[Desktop Entry] +Name=KDE Connect Phone URL Handler +Exec=kdeconnect-handler %U +Icon=kdeconnect +Type=Application +NoDisplay=true + +MimeType=x-scheme-handler/tel