Improve how we deal with dbus errors from the cli

Summary: Always use pending calls and centralize dbus error management

Reviewers: #kde_connect, albertvaka

Differential Revision: https://phabricator.kde.org/D4206
This commit is contained in:
Aleix Pol 2017-01-19 20:01:11 +01:00
parent 4c9e14f2a1
commit 339ce7beea

View file

@ -34,6 +34,26 @@
#include "interfaces/dbusinterfaces.h" #include "interfaces/dbusinterfaces.h"
#include "kdeconnect-version.h" #include "kdeconnect-version.h"
template <typename T>
Q_REQUIRED_RESULT T blockOnReply(QDBusPendingReply<T> reply)
{
reply.waitForFinished();
if (reply.isError()) {
QTextStream(stderr) << i18n("error: ") << reply.error().message() << endl;
exit(1);
}
return reply.value();
}
void blockOnReply(QDBusPendingReply<void> reply)
{
reply.waitForFinished();
if (reply.isError()) {
QTextStream(stderr) << i18n("error: ") << reply.error().message() << endl;
exit(1);
}
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
@ -82,13 +102,11 @@ int main(int argc, char** argv)
if (parser.isSet(QStringLiteral("a"))) { if (parser.isSet(QStringLiteral("a"))) {
reachable = true; reachable = true;
} else { } else {
iface.acquireDiscoveryMode(id); blockOnReply(iface.acquireDiscoveryMode(id));
QThread::sleep(2); QThread::sleep(2);
} }
QDBusPendingReply<QStringList> reply = iface.devices(paired, reachable); const QStringList devices = blockOnReply<QStringList>(iface.devices(paired, reachable));
reply.waitForFinished();
const QStringList devices = reply.value();
Q_FOREACH (const QString& id, devices) { Q_FOREACH (const QString& id, devices) {
if (parser.isSet(QStringLiteral("id-only"))) { if (parser.isSet(QStringLiteral("id-only"))) {
QTextStream(stdout) << id << endl; QTextStream(stdout) << id << endl;
@ -114,15 +132,15 @@ int main(int argc, char** argv)
QTextStream(stderr) << i18n("No devices found") << endl; QTextStream(stderr) << i18n("No devices found") << endl;
} }
iface.releaseDiscoveryMode(id); blockOnReply(iface.releaseDiscoveryMode(id));
} else if(parser.isSet(QStringLiteral("refresh"))) { } else if(parser.isSet(QStringLiteral("refresh"))) {
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), QStringLiteral("/modules/kdeconnect"), QStringLiteral("org.kde.kdeconnect.daemon"), QStringLiteral("forceOnNetworkChange")); QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), QStringLiteral("/modules/kdeconnect"), QStringLiteral("org.kde.kdeconnect.daemon"), QStringLiteral("forceOnNetworkChange"));
QDBusConnection::sessionBus().call(msg); blockOnReply(QDBusConnection::sessionBus().asyncCall(msg));
} else { } else {
QString device = parser.value(QStringLiteral("device")); QString device = parser.value(QStringLiteral("device"));
if (device.isEmpty() && parser.isSet(QStringLiteral("name"))) { if (device.isEmpty() && parser.isSet(QStringLiteral("name"))) {
device = iface.deviceIdByName(parser.value(QStringLiteral("name"))); device = blockOnReply(iface.deviceIdByName(parser.value(QStringLiteral("name"))));
if (device.isEmpty()) { if (device.isEmpty()) {
QTextStream(stderr) << "Couldn't find device: " << parser.value(QStringLiteral("name")) << endl; QTextStream(stderr) << "Couldn't find device: " << parser.value(QStringLiteral("name")) << endl;
return 1; return 1;
@ -139,7 +157,7 @@ int main(int argc, char** argv)
if(!url.isEmpty() && !device.isEmpty()) { if(!url.isEmpty() && !device.isEmpty()) {
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), "/modules/kdeconnect/devices/"+device+"/share", QStringLiteral("org.kde.kdeconnect.device.share"), QStringLiteral("shareUrl")); QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), "/modules/kdeconnect/devices/"+device+"/share", QStringLiteral("org.kde.kdeconnect.device.share"), QStringLiteral("shareUrl"));
msg.setArguments(QVariantList() << url.toString()); msg.setArguments(QVariantList() << url.toString());
QDBusConnection::sessionBus().call(msg); blockOnReply(QDBusConnection::sessionBus().asyncCall(msg));
} else { } else {
QTextStream(stderr) << (i18n("Couldn't share %1", url.toString())) << endl; QTextStream(stderr) << (i18n("Couldn't share %1", url.toString())) << endl;
} }
@ -149,7 +167,7 @@ int main(int argc, char** argv)
//Device doesn't exist, go into discovery mode and wait up to 30 seconds for the device to appear //Device doesn't exist, go into discovery mode and wait up to 30 seconds for the device to appear
QEventLoop wait; QEventLoop wait;
QTextStream(stderr) << i18n("waiting for device...") << endl; QTextStream(stderr) << i18n("waiting for device...") << endl;
iface.acquireDiscoveryMode(id); blockOnReply(iface.acquireDiscoveryMode(id));
QObject::connect(&iface, &DaemonDbusInterface::deviceAdded, [&](const QString &deviceAddedId) { QObject::connect(&iface, &DaemonDbusInterface::deviceAdded, [&](const QString &deviceAddedId) {
if (device == deviceAddedId) { if (device == deviceAddedId) {
@ -163,24 +181,22 @@ int main(int argc, char** argv)
if (!dev.isReachable()) { if (!dev.isReachable()) {
QTextStream(stderr) << i18n("Device not found") << endl; QTextStream(stderr) << i18n("Device not found") << endl;
} else if(dev.property("isPaired").toBool()) { } else if(blockOnReply<bool>(dev.isTrusted())) {
QTextStream(stderr) << i18n("Already paired") << endl; QTextStream(stderr) << i18n("Already paired") << endl;
} else { } else {
QTextStream(stderr) << i18n("Pair requested") << endl; QTextStream(stderr) << i18n("Pair requested") << endl;
QDBusPendingReply<void> req = dev.requestPair(); blockOnReply(dev.requestPair());
req.waitForFinished();
} }
iface.releaseDiscoveryMode(id); blockOnReply(iface.releaseDiscoveryMode(id));
} else if(parser.isSet(QStringLiteral("unpair"))) { } else if(parser.isSet(QStringLiteral("unpair"))) {
DeviceDbusInterface dev(device); DeviceDbusInterface dev(device);
if (!dev.isReachable()) { if (!dev.isReachable()) {
QTextStream(stderr) << i18n("Device does not exist") << endl; QTextStream(stderr) << i18n("Device does not exist") << endl;
} else if(!dev.property("isPaired").toBool()) { } else if(!dev.isTrusted()) {
QTextStream(stderr) << i18n("Already not paired") << endl; QTextStream(stderr) << i18n("Already not paired") << endl;
} else { } else {
QTextStream(stderr) << i18n("Unpaired") << endl; QTextStream(stderr) << i18n("Unpaired") << endl;
QDBusPendingReply<void> req = dev.unpair(); blockOnReply(dev.unpair());
req.waitForFinished();
} }
} else if(parser.isSet(QStringLiteral("ping")) || parser.isSet(QStringLiteral("ping-msg"))) { } else if(parser.isSet(QStringLiteral("ping")) || parser.isSet(QStringLiteral("ping-msg"))) {
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), "/modules/kdeconnect/devices/"+device+"/ping", QStringLiteral("org.kde.kdeconnect.device.ping"), QStringLiteral("sendPing")); QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), "/modules/kdeconnect/devices/"+device+"/ping", QStringLiteral("org.kde.kdeconnect.device.ping"), QStringLiteral("sendPing"));
@ -188,19 +204,19 @@ int main(int argc, char** argv)
QString message = parser.value(QStringLiteral("ping-msg")); QString message = parser.value(QStringLiteral("ping-msg"));
msg.setArguments(QVariantList() << message); msg.setArguments(QVariantList() << message);
} }
QDBusConnection::sessionBus().call(msg); blockOnReply(QDBusConnection::sessionBus().asyncCall(msg));
} else if(parser.isSet(QStringLiteral("send-sms"))) { } else if(parser.isSet(QStringLiteral("send-sms"))) {
if (parser.isSet(QStringLiteral("destination"))) { if (parser.isSet(QStringLiteral("destination"))) {
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), "/modules/kdeconnect/devices/"+device+"/telephony", QStringLiteral("org.kde.kdeconnect.device.telephony"), QStringLiteral("sendSms")); QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), "/modules/kdeconnect/devices/"+device+"/telephony", QStringLiteral("org.kde.kdeconnect.device.telephony"), QStringLiteral("sendSms"));
msg.setArguments({ parser.value("destination"), parser.value("send-sms") }); msg.setArguments({ parser.value("destination"), parser.value("send-sms") });
QDBusConnection::sessionBus().call(msg); blockOnReply(QDBusConnection::sessionBus().asyncCall(msg));
} else { } else {
QTextStream(stderr) << i18n("error: should specify the SMS's recipient by passing --destination <phone number>"); QTextStream(stderr) << i18n("error: should specify the SMS's recipient by passing --destination <phone number>");
return 1; return 1;
} }
} else if(parser.isSet(QStringLiteral("ring"))) { } else if(parser.isSet(QStringLiteral("ring"))) {
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), "/modules/kdeconnect/devices/"+device+"/findmyphone", QStringLiteral("org.kde.kdeconnect.device.findmyphone"), QStringLiteral("ring")); QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), "/modules/kdeconnect/devices/"+device+"/findmyphone", QStringLiteral("org.kde.kdeconnect.device.findmyphone"), QStringLiteral("ring"));
QDBusConnection::sessionBus().call(msg); blockOnReply(QDBusConnection::sessionBus().asyncCall(msg));
} else if(parser.isSet("send-keys")) { } else if(parser.isSet("send-keys")) {
QString seq = parser.value("send-keys"); QString seq = parser.value("send-keys");
QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kdeconnect", "/modules/kdeconnect/devices/"+device+"/remotekeyboard", "org.kde.kdeconnect.device.remotekeyboard", "sendKeyPress"); QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kdeconnect", "/modules/kdeconnect/devices/"+device+"/remotekeyboard", "org.kde.kdeconnect.device.remotekeyboard", "sendKeyPress");
@ -211,13 +227,13 @@ int main(int argc, char** argv)
while (!in.atEnd()) { while (!in.atEnd()) {
QByteArray line = in.readLine(); // sanitize to ASCII-codes > 31? QByteArray line = in.readLine(); // sanitize to ASCII-codes > 31?
msg.setArguments({QString(line), -1, false, false, false}); msg.setArguments({QString(line), -1, false, false, false});
QDBusConnection::sessionBus().call(msg); blockOnReply(QDBusConnection::sessionBus().asyncCall(msg));
} }
in.close(); in.close();
} }
} else { } else {
msg.setArguments({seq, -1, false, false, false}); msg.setArguments({seq, -1, false, false, false});
QDBusConnection::sessionBus().call(msg); blockOnReply(QDBusConnection::sessionBus().asyncCall(msg));
} }
} else if(parser.isSet(QStringLiteral("list-notifications"))) { } else if(parser.isSet(QStringLiteral("list-notifications"))) {
NotificationsModel notifications; NotificationsModel notifications;
@ -234,18 +250,13 @@ int main(int argc, char** argv)
const QJsonObject cont = it->toObject(); const QJsonObject cont = it->toObject();
QTextStream(stdout) << it.key() << ": " << cont.value(QStringLiteral("name")).toString() << ": " << cont.value(QStringLiteral("command")).toString() << endl; QTextStream(stdout) << it.key() << ": " << cont.value(QStringLiteral("name")).toString() << ": " << cont.value(QStringLiteral("command")).toString() << endl;
} }
const auto err = iface.lastError();
if (err.isValid()) {
QTextStream(stderr) << err.message() << endl;
}
} else if(parser.isSet(QStringLiteral("execute-command"))) { } else if(parser.isSet(QStringLiteral("execute-command"))) {
RemoteCommandsDbusInterface iface(device); RemoteCommandsDbusInterface iface(device);
iface.triggerCommand(parser.value(QStringLiteral("execute-command"))); blockOnReply(iface.triggerCommand(parser.value(QStringLiteral("execute-command"))));
} else if(parser.isSet(QStringLiteral("encryption-info"))) { } else if(parser.isSet(QStringLiteral("encryption-info"))) {
DeviceDbusInterface dev(device); DeviceDbusInterface dev(device);
QDBusPendingReply<QString> devReply = dev.encryptionInfo(); // QSsl::Der = 1 QString info = blockOnReply<QString>(dev.encryptionInfo()); // QSsl::Der = 1
devReply.waitForFinished(); QTextStream(stdout) << info << endl;
QTextStream(stderr) << devReply.value() << endl;
} else { } else {
QTextStream(stderr) << i18n("Nothing to be done") << endl; QTextStream(stderr) << i18n("Nothing to be done") << endl;
} }