In the CLI interface, make it possible to refer a device by name

Only works with trusted devices
This commit is contained in:
Aleix Pol 2016-06-05 23:31:13 +02:00
parent 4d1cf58612
commit c7429b56b7
3 changed files with 22 additions and 2 deletions

View file

@ -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();

View file

@ -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()
{

View file

@ -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