Improved kdeconnect-cli, adding parameters to ease scripting.

Also:
Added some missing i18n calls.
Using QTextStream(stderr) instead of qCritical() to avoid "quoted output".
This commit is contained in:
Albert Vaca 2015-02-24 22:13:30 -08:00
parent 7e3338bae5
commit 0cb24acbc0

View file

@ -45,6 +45,8 @@ int main(int argc, char** argv)
about.addAuthor( i18n("Albert Vaca Cintora"), QString(), "albertvaka@gmail.com" ); about.addAuthor( i18n("Albert Vaca Cintora"), QString(), "albertvaka@gmail.com" );
QCommandLineParser parser; QCommandLineParser parser;
parser.addOption(QCommandLineOption(QStringList("l") << "list-devices", i18n("List all devices"))); parser.addOption(QCommandLineOption(QStringList("l") << "list-devices", i18n("List all devices")));
parser.addOption(QCommandLineOption(QStringList("a") << "list-available", i18n("List available (paired and reachable) devices")));
parser.addOption(QCommandLineOption("id-only", i18n("Make --list-devices or --list-available print only the devices id, to ease scripting")));
parser.addOption(QCommandLineOption("refresh", i18n("Search for devices in the network and re-establishe connections"))); parser.addOption(QCommandLineOption("refresh", i18n("Search for devices in the network and re-establishe connections")));
parser.addOption(QCommandLineOption("pair", i18n("Request pairing to a said device"))); parser.addOption(QCommandLineOption("pair", i18n("Request pairing to a said device")));
parser.addOption(QCommandLineOption("unpair", i18n("Stop pairing to a said device"))); parser.addOption(QCommandLineOption("unpair", i18n("Stop pairing to a said device")));
@ -59,33 +61,45 @@ int main(int argc, char** argv)
parser.process(app); parser.process(app);
about.processCommandLine(&parser); about.processCommandLine(&parser);
if(parser.isSet("l")) { if(parser.isSet("l") || parser.isSet("a")) {
DevicesModel devices; DevicesModel devices;
for(int i=0, rows=devices.rowCount(); i<rows; ++i) { if (parser.isSet("a")) {
QModelIndex idx = devices.index(i); devices.setDisplayFilter(DevicesModel::StatusFlag::StatusPaired | DevicesModel::StatusFlag::StatusReachable);
QString statusInfo; }
switch(idx.data(DevicesModel::StatusModelRole).toInt()) { int deviceCount = devices.rowCount();
case DevicesModel::StatusPaired: for(int i=0; i < deviceCount; ++i) {
statusInfo = "(paired)"; QModelIndex idx = devices.index(i);
break; if (parser.isSet("id-only")) {
case DevicesModel::StatusReachable: QTextStream(stdout) << idx.data(DevicesModel::ModelRoles::IdModelRole).toString() << endl;
statusInfo = "(reachable)"; } else {
break; QString statusInfo;
case DevicesModel::StatusReachable | DevicesModel::StatusPaired: switch(idx.data(DevicesModel::StatusModelRole).toInt()) {
statusInfo = "(paired and reachable)"; case DevicesModel::StatusPaired:
break; statusInfo = i18n("(paired)");
} break;
QTextStream(stdout) << "- " << idx.data(Qt::DisplayRole).toString() case DevicesModel::StatusReachable:
<< ": " << idx.data(DevicesModel::IdModelRole).toString() << ' ' << statusInfo << endl; statusInfo = i18n("(reachable)");
break;
case DevicesModel::StatusReachable | DevicesModel::StatusPaired:
statusInfo = i18n("(paired and reachable)");
break;
}
QTextStream(stdout) << "- " << idx.data(Qt::DisplayRole).toString()
<< ": " << idx.data(DevicesModel::IdModelRole).toString() << ' ' << statusInfo << endl;
}
}
if (!parser.isSet("id-only")) {
QTextStream(stdout) << i18n("%1 device(s) found", deviceCount) << endl;
} else if (!deviceCount) {
QTextStream(stderr) << i18n("No devices found") << endl;
} }
QTextStream(stdout) << devices.rowCount() << " devices found" << endl;
} else if(parser.isSet("refresh")) { } else if(parser.isSet("refresh")) {
QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kdeconnect", "/modules/kdeconnect", "org.kde.kdeconnect.daemon", "forceOnNetworkChange"); QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kdeconnect", "/modules/kdeconnect", "org.kde.kdeconnect.daemon", "forceOnNetworkChange");
QDBusConnection::sessionBus().call(msg); QDBusConnection::sessionBus().call(msg);
} else { } else {
QString device; QString device;
if(!parser.isSet("device")) { if(!parser.isSet("device")) {
qCritical() << (i18n("No device specified")); QTextStream(stderr) << i18n("No device specified") << endl;
} }
device = parser.value("device"); device = parser.value("device");
QUrl url; QUrl url;
@ -96,12 +110,13 @@ int main(int argc, char** argv)
QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kdeconnect", "/modules/kdeconnect/devices/"+device+"/share", "org.kde.kdeconnect.device.share", "shareUrl"); QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kdeconnect", "/modules/kdeconnect/devices/"+device+"/share", "org.kde.kdeconnect.device.share", "shareUrl");
msg.setArguments(QVariantList() << url.toString()); msg.setArguments(QVariantList() << url.toString());
QDBusConnection::sessionBus().call(msg); QDBusConnection::sessionBus().call(msg);
} else } else {
qCritical() << (i18n("Couldn't share %1", url.toString())); QTextStream(stderr) << (i18n("Couldn't share %1", url.toString())) << endl;
}
} else if(parser.isSet("pair")) { } else if(parser.isSet("pair")) {
DeviceDbusInterface dev(device); DeviceDbusInterface dev(device);
if(dev.isPaired()) if(dev.isPaired())
QTextStream(stdout) << "Already paired" << endl; QTextStream(stderr) << i18n("Already paired") << endl;
else { else {
QDBusPendingReply<void> req = dev.requestPair(); QDBusPendingReply<void> req = dev.requestPair();
req.waitForFinished(); req.waitForFinished();
@ -109,7 +124,7 @@ int main(int argc, char** argv)
} else if(parser.isSet("unpair")) { } else if(parser.isSet("unpair")) {
DeviceDbusInterface dev(device); DeviceDbusInterface dev(device);
if(!dev.isPaired()) if(!dev.isPaired())
QTextStream(stdout) << "Already not paired" << endl; QTextStream(stderr) << i18n("Already not paired") << endl;
else { else {
QDBusPendingReply<void> req = dev.unpair(); QDBusPendingReply<void> req = dev.unpair();
req.waitForFinished(); req.waitForFinished();
@ -127,10 +142,10 @@ int main(int argc, char** argv)
for(int i=0, rows=notifications.rowCount(); i<rows; ++i) { for(int i=0, rows=notifications.rowCount(); i<rows; ++i) {
QModelIndex idx = notifications.index(i); QModelIndex idx = notifications.index(i);
QTextStream(stdout) << "- " << idx.data(NotificationsModel::AppNameModelRole).toString() QTextStream(stdout) << "- " << idx.data(NotificationsModel::AppNameModelRole).toString()
<< ": " << idx.data(NotificationsModel::NameModelRole).toString() << endl; << ": " << idx.data(NotificationsModel::NameModelRole).toString() << endl;
} }
} else { } else {
qCritical() << i18n("Nothing to be done with the device"); QTextStream(stderr) << i18n("Nothing to be done") << endl;
} }
} }
QMetaObject::invokeMethod(&app, "quit", Qt::QueuedConnection); QMetaObject::invokeMethod(&app, "quit", Qt::QueuedConnection);