From c7429b56b79c07ca5d1cb7db33c1ca7bb424f54f Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Sun, 5 Jun 2016 23:31:13 +0200 Subject: [PATCH] In the CLI interface, make it possible to refer a device by name Only works with trusted devices --- cli/kdeconnect-cli.cpp | 13 +++++++++++-- core/daemon.cpp | 9 +++++++++ core/daemon.h | 2 ++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/cli/kdeconnect-cli.cpp b/cli/kdeconnect-cli.cpp index bb3f11f7e..776ff138d 100644 --- a/cli/kdeconnect-cli.cpp +++ b/cli/kdeconnect-cli.cpp @@ -60,6 +60,7 @@ int main(int argc, char** argv) parser.addOption(QCommandLineOption("list-notifications", i18n("Display the notifications on a said device"))); parser.addOption(QCommandLineOption("lock", i18n("Lock the specified device"))); parser.addOption(QCommandLineOption(QStringList("device") << "d", i18n("Device ID"), "dev")); + parser.addOption(QCommandLineOption(QStringList("name") << "n", i18n("Device Name"), "name")); parser.addOption(QCommandLineOption("encryption-info", i18n("Get encryption info about said device"))); about.setupCommandLine(&parser); @@ -110,10 +111,18 @@ int main(int argc, char** argv) QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kdeconnect", "/modules/kdeconnect", "org.kde.kdeconnect.daemon", "forceOnNetworkChange"); QDBusConnection::sessionBus().call(msg); } else { - if(!parser.isSet("device")) { + QString device = parser.value("device"); + if (device.isEmpty() && parser.isSet("name")) { + device = iface.deviceIdByName(parser.value("name")); + if (device.isEmpty()) { + QTextStream(stderr) << "Couldn't find device: " << parser.value("name") << endl; + return 1; + } + } + + if(device.isEmpty()) { QTextStream(stderr) << i18n("No device specified") << endl; } - const QString device = parser.value("device"); if(parser.isSet("share")) { QUrl url = QUrl::fromUserInput(parser.value("share"), QDir::currentPath()); parser.clearPositionalArguments(); diff --git a/core/daemon.cpp b/core/daemon.cpp index 9d4bb4330..b41616c38 100644 --- a/core/daemon.cpp +++ b/core/daemon.cpp @@ -241,6 +241,15 @@ bool Daemon::isDiscoveringDevices() const return !d->mDiscoveryModeAcquisitions.isEmpty(); } +QString Daemon::deviceIdByName(const QString &name) const +{ + foreach(Device* d, d->mDevices) { + if (d->name() == name && d->isTrusted()) + return d->id(); + } + return {}; +} + Daemon::~Daemon() { diff --git a/core/daemon.h b/core/daemon.h index 6cc43af23..1addbe478 100644 --- a/core/daemon.h +++ b/core/daemon.h @@ -70,6 +70,8 @@ public Q_SLOTS: //Returns a list of ids. The respective devices can be manipulated using the dbus path: "/modules/kdeconnect/Devices/"+id Q_SCRIPTABLE QStringList devices(bool onlyReachable = false, bool onlyPaired = false) const; + Q_SCRIPTABLE QString deviceIdByName(const QString &name) const; + Q_SIGNALS: Q_SCRIPTABLE void deviceAdded(const QString& id); Q_SCRIPTABLE void deviceRemoved(const QString& id); //Note that paired devices will never be removed