diff options
| author | Javier <dev.git@javispedro.com> | 2014-12-03 00:00:42 +0100 |
|---|---|---|
| committer | Javier <dev.git@javispedro.com> | 2014-12-03 00:06:13 +0100 |
| commit | 843e8c2550f69de3b9dfc3ec5f13d2c3a5710896 (patch) | |
| tree | 80f8edf321fed7d1790b609c2dab51c9d22fdd8b /daemon/manager.cpp | |
| parent | c35a3a9bea759cadf1e975a2a62e50789cad096c (diff) | |
implement more parts of the new D-Bus API
Diffstat (limited to 'daemon/manager.cpp')
| -rw-r--r-- | daemon/manager.cpp | 81 |
1 files changed, 73 insertions, 8 deletions
diff --git a/daemon/manager.cpp b/daemon/manager.cpp index 38b3948..0666de0 100644 --- a/daemon/manager.cpp +++ b/daemon/manager.cpp @@ -56,6 +56,8 @@ Manager::Manager(Settings *settings, QObject *parent) : connect(notifications, SIGNAL(facebookNotify(const QString &,const QString &)), SLOT(onFacebookNotify(const QString &,const QString &))); connect(appmsg, &AppMsgManager::messageReceived, this, &Manager::onAppMessage); + connect(appmsg, &AppMsgManager::appStarted, this, &Manager::onAppOpened); + connect(appmsg, &AppMsgManager::appStopped, this, &Manager::onAppClosed); QDBusConnection session = QDBusConnection::sessionBus(); new WatchAdaptor(proxy); @@ -389,19 +391,82 @@ void Manager::transliterateMessage(const QString &text) } } -void Manager::test() +void Manager::onAppMessage(const QUuid &uuid, const QVariantMap &data) { - logger()->debug() << "Starting test"; - - js->showConfiguration(); + emit proxy->AppMessage(uuid.toString(), data); } -void Manager::onAppMessage(const QUuid &uuid, const QVariantMap &data) +void Manager::onAppOpened(const QUuid &uuid) { - emit proxy->AppMessage(uuid.toString(), data); + currentAppUuid = uuid; + emit proxy->AppOpened(uuid.toString()); } -void Manager::onWebviewClosed(const QString &result) +void Manager::onAppClosed(const QUuid &uuid) { - js->handleWebviewClosed(result); + currentAppUuid = QUuid(); + emit proxy->AppClosed(uuid.toString()); +} + +bool PebbledProxy::SendAppMessage(const QString &uuid, const QVariantMap &data) { + Q_ASSERT(calledFromDBus()); + const QDBusMessage msg = message(); + setDelayedReply(true); + manager()->appmsg->send(uuid, data, [this, msg]() { + QDBusMessage reply = msg.createReply(QVariant::fromValue(true)); + this->connection().send(reply); + }, [this, msg]() { + QDBusMessage reply = msg.createReply(QVariant::fromValue(false)); + this->connection().send(reply); + }); + return false; // D-Bus clients should never see this reply. +} + +QString PebbledProxy::StartAppConfiguration(const QString &uuid) { + Q_ASSERT(calledFromDBus()); + const QDBusMessage msg = message(); + + if (manager()->currentAppUuid != uuid) { + sendErrorReply(msg.interface() + ".Error.AppNotRunning", + "The requested app is not currently opened in the watch"); + return QString(); + } + + if (!manager()->js->isJSKitAppRunning()) { + sendErrorReply(msg.interface() + ".Error.JSNotActive", + "The requested app is not a PebbleKit JS application"); + return QString(); + } + + // After calling showConfiguration() on the script, + // it will (eventually!) return a URL to us via the appOpenUrl signal. + + // So we can't send the D-Bus reply right now. + setDelayedReply(true); + + // Set up a signal handler to catch the appOpenUrl signal. + QMetaObject::Connection c = connect(manager()->js, &JSKitManager::appOpenUrl, + [this,msg,c](const QUrl &url) { + // Workaround: due to a GCC bug we can't capture the uuid parameter, but we can extract + // it again from the original message arguments. + QString uuid = msg.arguments().at(0).toString(); + if (manager()->currentAppUuid != uuid) { + // App was changed while we were waiting for the script.. + QDBusMessage reply = msg.createErrorReply(msg.interface() + ".Error.AppNotRunning", + "The requested app is not currently opened in the watch"); + connection().send(reply); + } else { + QDBusMessage reply = msg.createReply(QVariant::fromValue(url.toString())); + connection().send(reply); + } + + disconnect(c); + }); + // TODO: JS script may fail, never call OpenURL, or something like that + // In those cases we may leak the above connection + // So we need to also set a timeout or similar. + + manager()->js->showConfiguration(); + + return QString(); // This return value should never be used. } |
