diff --git a/libnymea-core/integrations/thingmanagerimplementation.cpp b/libnymea-core/integrations/thingmanagerimplementation.cpp index 8911fd14..69af465b 100644 --- a/libnymea-core/integrations/thingmanagerimplementation.cpp +++ b/libnymea-core/integrations/thingmanagerimplementation.cpp @@ -764,7 +764,7 @@ BrowseResult *ThingManagerImplementation::browseThing(const ThingId &thingId, co { Thing *thing = m_configuredThing.value(thingId); - BrowseResult *result = new BrowseResult(thing, itemId, locale, this, 30000); + BrowseResult *result = new BrowseResult(thing, this, itemId, locale, this, 30000); if (!thing) { qCWarning(dcThingManager()) << "Cannot browse thing. No such thing:" << thingId.toString(); @@ -802,7 +802,7 @@ BrowserItemResult *ThingManagerImplementation::browserItemDetails(const ThingId { Thing *thing = m_configuredThing.value(thingId); - BrowserItemResult *result = new BrowserItemResult(thing, itemId, locale, this, 30000); + BrowserItemResult *result = new BrowserItemResult(thing, this, itemId, locale, this, 30000); if (!thing) { qCWarning(dcThingManager()) << "Cannot browse thing. No such thing:" << thingId.toString(); @@ -840,7 +840,7 @@ BrowserActionInfo* ThingManagerImplementation::executeBrowserItem(const BrowserA { Thing *thing = m_configuredThing.value(browserAction.thingId()); - BrowserActionInfo *info = new BrowserActionInfo(thing, browserAction, this, 30000); + BrowserActionInfo *info = new BrowserActionInfo(thing, this, browserAction, this, 30000); if (!thing) { info->finish(Thing::ThingErrorThingNotFound); @@ -872,7 +872,7 @@ BrowserItemActionInfo* ThingManagerImplementation::executeBrowserItemAction(cons { Thing *thing = m_configuredThing.value(browserItemAction.thingId()); - BrowserItemActionInfo *info = new BrowserItemActionInfo(thing, browserItemAction, this, 30000); + BrowserItemActionInfo *info = new BrowserItemActionInfo(thing, this, browserItemAction, this, 30000); if (!thing) { info->finish(Thing::ThingErrorThingNotFound); diff --git a/libnymea-core/jsonrpc/devicehandler.cpp b/libnymea-core/jsonrpc/devicehandler.cpp index 62d4ad04..f13b9f70 100644 --- a/libnymea-core/jsonrpc/devicehandler.cpp +++ b/libnymea-core/jsonrpc/devicehandler.cpp @@ -287,18 +287,30 @@ DeviceHandler::DeviceHandler(QObject *parent) : registerMethod("GetStateValues", description, params, returns); params.clear(); returns.clear(); - description = "Browse a device. If a DeviceClass indicates a device is browsable, this method will return the BrowserItems. If no parameter besides the deviceId is used, the root node of this device will be returned. Any returned item which is browsable can be passed as node. Results will be children of the given node."; + description = "Browse a device. If a DeviceClass indicates a device is browsable, this method will return " + "the BrowserItems. If no parameter besides the deviceId is used, the root node of this device " + "will be returned. Any returned item which is browsable can be passed as node. Results will be " + "children of the given node.\n" + "In case of an error during browsing, the error will be indicated and the displayMessage may contain " + "additional information for the user. The displayMessage will be translated. A client UI showing this " + "message to the user should be prepared for empty, but also longer strings."; params.insert("deviceId", enumValueName(Uuid)); params.insert("o:itemId", enumValueName(String)); returns.insert("deviceError", enumRef()); + returns.insert("o:displayMessage", enumValueName(String)); returns.insert("items", QVariantList() << objectRef("BrowserItem")); registerMethod("BrowseDevice", description, params, returns); params.clear(); returns.clear(); - description = "Get a single item from the browser. This won't give any more info on an item than a regular browseDevice call, but it allows to fetch details of an item if only the ID is known."; + description = "Get a single item from the browser. This won't give any more info on an item than a regular browseDevice " + "call, but it allows to fetch details of an item if only the ID is known.\n" + "In case of an error during browsing, the error will be indicated and the displayMessage may contain " + "additional information for the user. The displayMessage will be translated. A client UI showing this " + "message to the user should be prepared for empty, but also longer strings."; params.insert("deviceId", enumValueName(Uuid)); params.insert("o:itemId", enumValueName(String)); returns.insert("deviceError", enumRef()); + returns.insert("o:displayMessage", enumValueName(String)); returns.insert("o:item", objectRef("BrowserItem")); registerMethod("GetBrowserItem", description, params, returns); @@ -312,19 +324,27 @@ DeviceHandler::DeviceHandler(QObject *parent) : registerMethod("ExecuteAction", description, params, returns); params.clear(); returns.clear(); - description = "Execute the item identified by itemId on the given device."; + description = "Execute the item identified by itemId on the given device.\n" + "In case of an error during execution, the error will be indicated and the displayMessage may contain " + "additional information for the user. The displayMessage will be translated. A client UI showing this " + "message to the user should be prepared for empty, but also longer strings."; params.insert("deviceId", enumValueName(Uuid)); params.insert("itemId", enumValueName(String)); returns.insert("deviceError", enumRef()); + returns.insert("o:displayMessage", enumValueName(String)); registerMethod("ExecuteBrowserItem", description, params, returns); params.clear(); returns.clear(); - description = "Execute the action for the browser item identified by actionTypeId and the itemId on the given device."; + description = "Execute the action for the browser item identified by actionTypeId and the itemId on the given device.\n" + "In case of an error during execution, the error will be indicated and the displayMessage may contain " + "additional information for the user. The displayMessage will be translated. A client UI showing this " + "message to the user should be prepared for empty, but also longer strings."; params.insert("deviceId", enumValueName(Uuid)); params.insert("itemId", enumValueName(String)); params.insert("actionTypeId", enumValueName(Uuid)); params.insert("o:params", objectRef()); returns.insert("deviceError", enumRef()); + returns.insert("o:displayMessage", enumValueName(String)); registerMethod("ExecuteBrowserItemAction", description, params, returns); // Notifications @@ -788,7 +808,7 @@ JsonReply *DeviceHandler::BrowseDevice(const QVariantMap ¶ms, const JsonCont JsonReply *jsonReply = createAsyncReply("BrowseDevice"); BrowseResult *result = NymeaCore::instance()->thingManager()->browseThing(thingId, itemId, context.locale()); - connect(result, &BrowseResult::finished, jsonReply, [this, jsonReply, result](){ + connect(result, &BrowseResult::finished, jsonReply, [this, jsonReply, result, context](){ QVariantMap returns = statusToReply(result->status()); QVariantList list; @@ -796,6 +816,9 @@ JsonReply *DeviceHandler::BrowseDevice(const QVariantMap ¶ms, const JsonCont list.append(packBrowserItem(item)); } returns.insert("items", list); + if (!result->displayMessage().isEmpty()) { + returns.insert("displayMessage", result->translatedDisplayMessage(context.locale())); + } jsonReply->setData(returns); jsonReply->finished(); }); @@ -812,11 +835,14 @@ JsonReply *DeviceHandler::GetBrowserItem(const QVariantMap ¶ms, const JsonCo JsonReply *jsonReply = createAsyncReply("GetBrowserItem"); BrowserItemResult *result = NymeaCore::instance()->thingManager()->browserItemDetails(thingId, itemId, context.locale()); - connect(result, &BrowserItemResult::finished, jsonReply, [this, jsonReply, result](){ + connect(result, &BrowserItemResult::finished, jsonReply, [this, jsonReply, result, context](){ QVariantMap params = statusToReply(result->status()); if (result->status() == Device::ThingErrorNoError) { params.insert("item", packBrowserItem(result->item())); } + if (!result->displayMessage().isEmpty()) { + params.insert("displayMessage", result->translatedDisplayMessage(context.locale())); + } jsonReply->setData(params); jsonReply->finished(); }); @@ -850,7 +876,7 @@ JsonReply *DeviceHandler::ExecuteAction(const QVariantMap ¶ms, const JsonCon return jsonReply; } -JsonReply *DeviceHandler::ExecuteBrowserItem(const QVariantMap ¶ms) +JsonReply *DeviceHandler::ExecuteBrowserItem(const QVariantMap ¶ms, const JsonContext &context) { ThingId thingId = ThingId(params.value("deviceId").toString()); QString itemId = params.value("itemId").toString(); @@ -859,9 +885,12 @@ JsonReply *DeviceHandler::ExecuteBrowserItem(const QVariantMap ¶ms) JsonReply *jsonReply = createAsyncReply("ExecuteBrowserItem"); BrowserActionInfo *info = NymeaCore::instance()->executeBrowserItem(action); - connect(info, &BrowserActionInfo::finished, jsonReply, [info, jsonReply](){ + connect(info, &BrowserActionInfo::finished, jsonReply, [info, jsonReply, context](){ QVariantMap data; data.insert("deviceError", enumValueName(info->status()).replace("Thing", "Device")); + if (!info->displayMessage().isEmpty()) { + data.insert("displayMessage", info->translatedDisplayMessage(context.locale())); + } jsonReply->setData(data); jsonReply->finished(); }); @@ -869,7 +898,7 @@ JsonReply *DeviceHandler::ExecuteBrowserItem(const QVariantMap ¶ms) return jsonReply; } -JsonReply *DeviceHandler::ExecuteBrowserItemAction(const QVariantMap ¶ms) +JsonReply *DeviceHandler::ExecuteBrowserItemAction(const QVariantMap ¶ms, const JsonContext &context) { ThingId thingId = ThingId(params.value("deviceId").toString()); QString itemId = params.value("itemId").toString(); @@ -880,9 +909,12 @@ JsonReply *DeviceHandler::ExecuteBrowserItemAction(const QVariantMap ¶ms) JsonReply *jsonReply = createAsyncReply("ExecuteBrowserItemAction"); BrowserItemActionInfo *info = NymeaCore::instance()->executeBrowserItemAction(browserItemAction); - connect(info, &BrowserItemActionInfo::finished, jsonReply, [info, jsonReply](){ + connect(info, &BrowserItemActionInfo::finished, jsonReply, [info, jsonReply, context](){ QVariantMap data; data.insert("deviceError", enumValueName(info->status()).replace("Thing", "Device")); + if (!info->displayMessage().isEmpty()) { + data.insert("displayMessage", info->translatedDisplayMessage(context.locale())); + } jsonReply->setData(data); jsonReply->finished(); }); diff --git a/libnymea-core/jsonrpc/devicehandler.h b/libnymea-core/jsonrpc/devicehandler.h index 908f66f6..95873c0e 100644 --- a/libnymea-core/jsonrpc/devicehandler.h +++ b/libnymea-core/jsonrpc/devicehandler.h @@ -167,8 +167,8 @@ public: Q_INVOKABLE JsonReply *GetBrowserItem(const QVariantMap ¶ms, const JsonContext &context) const; Q_INVOKABLE JsonReply *ExecuteAction(const QVariantMap ¶ms, const JsonContext &context); - Q_INVOKABLE JsonReply *ExecuteBrowserItem(const QVariantMap ¶ms); - Q_INVOKABLE JsonReply *ExecuteBrowserItemAction(const QVariantMap ¶ms); + Q_INVOKABLE JsonReply *ExecuteBrowserItem(const QVariantMap ¶ms, const JsonContext &context); + Q_INVOKABLE JsonReply *ExecuteBrowserItemAction(const QVariantMap ¶ms, const JsonContext &context); static QVariantMap packBrowserItem(const BrowserItem &item); diff --git a/libnymea-core/jsonrpc/integrationshandler.cpp b/libnymea-core/jsonrpc/integrationshandler.cpp index 820e695f..639af631 100644 --- a/libnymea-core/jsonrpc/integrationshandler.cpp +++ b/libnymea-core/jsonrpc/integrationshandler.cpp @@ -288,20 +288,28 @@ IntegrationsHandler::IntegrationsHandler(ThingManager *thingManager, QObject *pa description = "Browse a thing. " "If a ThingClass indicates a thing is browsable, this method will return the BrowserItems. If no " "parameter besides the thingId is used, the root node of this thingwill be returned. Any " - "returned item which is browsable can be passed as node. Results will be children of the given node."; + "returned item which is browsable can be passed as node. Results will be children of the given node.\n" + "In case of an error during browsing, the error will be indicated and the displayMessage may contain " + "additional information for the user. The displayMessage will be translated. A client UI showing this " + "message to the user should be prepared for empty, but also longer strings."; params.insert("thingId", enumValueName(Uuid)); params.insert("o:itemId", enumValueName(String)); returns.insert("thingError", enumRef()); + returns.insert("o:displayMessage", enumValueName(String)); returns.insert("items", QVariantList() << objectRef("BrowserItem")); registerMethod("BrowseThing", description, params, returns); params.clear(); returns.clear(); description = "Get a single item from the browser. " "This won't give any more info on an item than a regular BrowseThing call, but it allows to fetch " - "details of an item if only the ID is known."; + "details of an item if only the ID is known.\n" + "In case of an error during browsing, the error will be indicated and the displayMessage may contain " + "additional information for the user. The displayMessage will be translated. A client UI showing this " + "message to the user should be prepared for empty, but also longer strings."; params.insert("thingId", enumValueName(Uuid)); params.insert("o:itemId", enumValueName(String)); returns.insert("thingError", enumRef()); + returns.insert("o:displayMessage", enumValueName(String)); returns.insert("o:item", objectRef("BrowserItem")); registerMethod("GetBrowserItem", description, params, returns); @@ -315,19 +323,27 @@ IntegrationsHandler::IntegrationsHandler(ThingManager *thingManager, QObject *pa registerMethod("ExecuteAction", description, params, returns); params.clear(); returns.clear(); - description = "Execute the item identified by itemId on the given thing."; + description = "Execute the item identified by itemId on the given thing.\n" + "In case of an error during execution, the error will be indicated and the displayMessage may contain " + "additional information for the user. The displayMessage will be translated. A client UI showing this " + "message to the user should be prepared for empty, but also longer strings."; params.insert("thingId", enumValueName(Uuid)); params.insert("itemId", enumValueName(String)); returns.insert("thingError", enumRef()); + returns.insert("o:displayMessage", enumValueName(String)); registerMethod("ExecuteBrowserItem", description, params, returns); params.clear(); returns.clear(); - description = "Execute the action for the browser item identified by actionTypeId and the itemId on the given thing."; + description = "Execute the action for the browser item identified by actionTypeId and the itemId on the given thing.\n" + "In case of an error during execution, the error will be indicated and the displayMessage may contain " + "additional information for the user. The displayMessage will be translated. A client UI showing this " + "message to the user should be prepared for empty, but also longer strings."; params.insert("thingId", enumValueName(Uuid)); params.insert("itemId", enumValueName(String)); params.insert("actionTypeId", enumValueName(Uuid)); params.insert("o:params", objectRef()); returns.insert("thingError", enumRef()); + returns.insert("o:displayMessage", enumValueName(String)); registerMethod("ExecuteBrowserItemAction", description, params, returns); // Notifications @@ -800,7 +816,7 @@ JsonReply *IntegrationsHandler::BrowseThing(const QVariantMap ¶ms, const Jso JsonReply *jsonReply = createAsyncReply("BrowseThing"); BrowseResult *result = NymeaCore::instance()->thingManager()->browseThing(thingId, itemId, context.locale()); - connect(result, &BrowseResult::finished, jsonReply, [this, jsonReply, result](){ + connect(result, &BrowseResult::finished, jsonReply, [this, jsonReply, result, context](){ QVariantMap returns = statusToReply(result->status()); QVariantList list; @@ -808,6 +824,9 @@ JsonReply *IntegrationsHandler::BrowseThing(const QVariantMap ¶ms, const Jso list.append(packBrowserItem(item)); } returns.insert("items", list); + if (!result->displayMessage().isEmpty()) { + returns.insert("displayMessage", result->translatedDisplayMessage(context.locale())); + } jsonReply->setData(returns); jsonReply->finished(); }); @@ -824,11 +843,14 @@ JsonReply *IntegrationsHandler::GetBrowserItem(const QVariantMap ¶ms, const JsonReply *jsonReply = createAsyncReply("GetBrowserItem"); BrowserItemResult *result = NymeaCore::instance()->thingManager()->browserItemDetails(thingId, itemId, context.locale()); - connect(result, &BrowserItemResult::finished, jsonReply, [this, jsonReply, result](){ + connect(result, &BrowserItemResult::finished, jsonReply, [this, jsonReply, result, context](){ QVariantMap params = statusToReply(result->status()); if (result->status() == Thing::ThingErrorNoError) { params.insert("item", packBrowserItem(result->item())); } + if (!result->displayMessage().isEmpty()) { + params.insert("displayMessage", result->translatedDisplayMessage(context.locale())); + } jsonReply->setData(params); jsonReply->finished(); }); @@ -862,7 +884,7 @@ JsonReply *IntegrationsHandler::ExecuteAction(const QVariantMap ¶ms, const J return jsonReply; } -JsonReply *IntegrationsHandler::ExecuteBrowserItem(const QVariantMap ¶ms) +JsonReply *IntegrationsHandler::ExecuteBrowserItem(const QVariantMap ¶ms, const JsonContext &context) { ThingId thingId = ThingId(params.value("thingId").toString()); QString itemId = params.value("itemId").toString(); @@ -871,9 +893,12 @@ JsonReply *IntegrationsHandler::ExecuteBrowserItem(const QVariantMap ¶ms) JsonReply *jsonReply = createAsyncReply("ExecuteBrowserItem"); BrowserActionInfo *info = NymeaCore::instance()->executeBrowserItem(action); - connect(info, &BrowserActionInfo::finished, jsonReply, [info, jsonReply](){ + connect(info, &BrowserActionInfo::finished, jsonReply, [info, jsonReply, context](){ QVariantMap data; data.insert("thingError", enumValueName(info->status())); + if (!info->displayMessage().isEmpty()) { + data.insert("displayMessage", info->translatedDisplayMessage(context.locale())); + } jsonReply->setData(data); jsonReply->finished(); }); @@ -881,7 +906,7 @@ JsonReply *IntegrationsHandler::ExecuteBrowserItem(const QVariantMap ¶ms) return jsonReply; } -JsonReply *IntegrationsHandler::ExecuteBrowserItemAction(const QVariantMap ¶ms) +JsonReply *IntegrationsHandler::ExecuteBrowserItemAction(const QVariantMap ¶ms, const JsonContext &context) { ThingId thingId = ThingId(params.value("thingId").toString()); QString itemId = params.value("itemId").toString(); @@ -892,9 +917,12 @@ JsonReply *IntegrationsHandler::ExecuteBrowserItemAction(const QVariantMap ¶ JsonReply *jsonReply = createAsyncReply("ExecuteBrowserItemAction"); BrowserItemActionInfo *info = NymeaCore::instance()->executeBrowserItemAction(browserItemAction); - connect(info, &BrowserItemActionInfo::finished, jsonReply, [info, jsonReply](){ + connect(info, &BrowserItemActionInfo::finished, jsonReply, [info, jsonReply, context](){ QVariantMap data; data.insert("thingError", enumValueName(info->status())); + if (!info->displayMessage().isEmpty()) { + data.insert("displayMessage", info->translatedDisplayMessage(context.locale())); + } jsonReply->setData(data); jsonReply->finished(); }); diff --git a/libnymea-core/jsonrpc/integrationshandler.h b/libnymea-core/jsonrpc/integrationshandler.h index de97c0ab..47f88fab 100644 --- a/libnymea-core/jsonrpc/integrationshandler.h +++ b/libnymea-core/jsonrpc/integrationshandler.h @@ -69,8 +69,8 @@ public: Q_INVOKABLE JsonReply *GetBrowserItem(const QVariantMap ¶ms, const JsonContext &context) const; Q_INVOKABLE JsonReply *ExecuteAction(const QVariantMap ¶ms, const JsonContext &context); - Q_INVOKABLE JsonReply *ExecuteBrowserItem(const QVariantMap ¶ms); - Q_INVOKABLE JsonReply *ExecuteBrowserItemAction(const QVariantMap ¶ms); + Q_INVOKABLE JsonReply *ExecuteBrowserItem(const QVariantMap ¶ms, const JsonContext &context); + Q_INVOKABLE JsonReply *ExecuteBrowserItemAction(const QVariantMap ¶ms, const JsonContext &context); static QVariantMap packBrowserItem(const BrowserItem &item); diff --git a/libnymea/integrations/browseractioninfo.cpp b/libnymea/integrations/browseractioninfo.cpp index 57a1b55a..935f6eeb 100644 --- a/libnymea/integrations/browseractioninfo.cpp +++ b/libnymea/integrations/browseractioninfo.cpp @@ -29,13 +29,15 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "browseractioninfo.h" +#include "thingmanager.h" #include -BrowserActionInfo::BrowserActionInfo(Thing *thing, const BrowserAction &browserAction, QObject *parent, quint32 timeout): +BrowserActionInfo::BrowserActionInfo(Thing *thing, ThingManager *thingManager, const BrowserAction &browserAction, QObject *parent, quint32 timeout): QObject (parent), m_thing(thing), - m_browserAction(browserAction) + m_browserAction(browserAction), + m_thingManager(thingManager) { connect(this, &BrowserActionInfo::finished, this, &BrowserActionInfo::deleteLater, Qt::QueuedConnection); @@ -67,9 +69,24 @@ Thing::ThingError BrowserActionInfo::status() const return m_status; } -void BrowserActionInfo::finish(Thing::ThingError status) +QString BrowserActionInfo::displayMessage() const +{ + return m_displayMessage; +} + +QString BrowserActionInfo::translatedDisplayMessage(const QLocale &locale) +{ + if (!m_thingManager || !m_thing) { + return m_displayMessage; + } + + return m_thingManager->translate(m_thing->pluginId(), m_displayMessage.toUtf8(), locale); +} + +void BrowserActionInfo::finish(Thing::ThingError status, const QString &displayMessage) { m_finished = true; m_status = status; + m_displayMessage = displayMessage; staticMetaObject.invokeMethod(this, "finished", Qt::QueuedConnection); } diff --git a/libnymea/integrations/browseractioninfo.h b/libnymea/integrations/browseractioninfo.h index 2ea42134..16476358 100644 --- a/libnymea/integrations/browseractioninfo.h +++ b/libnymea/integrations/browseractioninfo.h @@ -36,24 +36,28 @@ #include "thing.h" #include "types/browseraction.h" +class ThingManager; + class BrowserActionInfo : public QObject { Q_OBJECT public: - explicit BrowserActionInfo(Thing* thing, const BrowserAction &browserAction, QObject *parent, quint32 timeout = 0); + explicit BrowserActionInfo(Thing* thing, ThingManager *thingManager, const BrowserAction &browserAction, QObject *parent, quint32 timeout = 0); Thing* thing() const; BrowserAction browserAction() const; bool isFinished() const; Thing::ThingError status() const; + QString displayMessage() const; + QString translatedDisplayMessage(const QLocale &locale); signals: void finished(); void aborted(); public slots: - void finish(Thing::ThingError status); + void finish(Thing::ThingError status, const QString &displayMessage = QString()); private: Thing *m_thing = nullptr; @@ -61,6 +65,9 @@ private: bool m_finished = false; Thing::ThingError m_status = Thing::ThingErrorNoError; + QString m_displayMessage; + + ThingManager *m_thingManager = nullptr; }; #endif // BROWSERACTIONINFO_H diff --git a/libnymea/integrations/browseresult.cpp b/libnymea/integrations/browseresult.cpp index 105aca3b..93735f31 100644 --- a/libnymea/integrations/browseresult.cpp +++ b/libnymea/integrations/browseresult.cpp @@ -29,14 +29,16 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "browseresult.h" +#include "thingmanager.h" #include -BrowseResult::BrowseResult(Thing *thing, const QString &itemId, const QLocale &locale, QObject *parent, quint32 timeout): +BrowseResult::BrowseResult(Thing *thing, ThingManager *thingManager, const QString &itemId, const QLocale &locale, QObject *parent, quint32 timeout): QObject(parent), m_thing(thing), m_itemId(itemId), - m_locale(locale) + m_locale(locale), + m_thingManager(thingManager) { connect(this, &BrowseResult::finished, this, &BrowseResult::deleteLater, Qt::QueuedConnection); @@ -78,6 +80,20 @@ Thing::ThingError BrowseResult::status() const return m_status; } +QString BrowseResult::displayMessage() const +{ + return m_displayMessage; +} + +QString BrowseResult::translatedDisplayMessage(const QLocale &locale) +{ + if (!m_thingManager || !m_thing) { + return m_displayMessage; + } + + return m_thingManager->translate(m_thing->pluginId(), m_displayMessage.toUtf8(), locale); +} + void BrowseResult::addItem(const BrowserItem &item) { m_items.append(item); @@ -88,9 +104,10 @@ void BrowseResult::addItems(const BrowserItems &items) m_items.append(items); } -void BrowseResult::finish(Thing::ThingError status) +void BrowseResult::finish(Thing::ThingError status, const QString &displayMessage) { m_finished = true; m_status = status; + m_displayMessage = displayMessage; staticMetaObject.invokeMethod(this, "finished", Qt::QueuedConnection); } diff --git a/libnymea/integrations/browseresult.h b/libnymea/integrations/browseresult.h index 26104949..888f4851 100644 --- a/libnymea/integrations/browseresult.h +++ b/libnymea/integrations/browseresult.h @@ -35,11 +35,13 @@ #include "thing.h" +class ThingManager; + class BrowseResult : public QObject { Q_OBJECT public: - explicit BrowseResult(Thing *thing, const QString &itemId, const QLocale &locale, QObject *parent, quint32 timeout = 0); + explicit BrowseResult(Thing *thing, ThingManager *thingManager, const QString &itemId, const QLocale &locale, QObject *parent, quint32 timeout = 0); Thing* thing() const; QString itemId() const; @@ -49,12 +51,14 @@ public: bool isFinished() const; Thing::ThingError status() const; + QString displayMessage() const; + QString translatedDisplayMessage(const QLocale &locale); public slots: void addItem(const BrowserItem &item); void addItems(const BrowserItems &items); - void finish(Thing::ThingError status); + void finish(Thing::ThingError status, const QString &displayMessage = QString()); signals: void finished(); @@ -69,6 +73,9 @@ private: bool m_finished = false; Thing::ThingError m_status = Thing::ThingErrorNoError; + QString m_displayMessage; + + ThingManager *m_thingManager = nullptr; }; #endif // BROWSERESULT_H diff --git a/libnymea/integrations/browseritemactioninfo.cpp b/libnymea/integrations/browseritemactioninfo.cpp index 1e05e582..6335801a 100644 --- a/libnymea/integrations/browseritemactioninfo.cpp +++ b/libnymea/integrations/browseritemactioninfo.cpp @@ -29,13 +29,15 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "browseritemactioninfo.h" +#include "thingmanager.h" #include -BrowserItemActionInfo::BrowserItemActionInfo(Thing *thing, const BrowserItemAction &browserItemAction, QObject *parent, quint32 timeout): +BrowserItemActionInfo::BrowserItemActionInfo(Thing *thing, ThingManager *thingManager, const BrowserItemAction &browserItemAction, QObject *parent, quint32 timeout): QObject(parent), m_thing(thing), - m_browserItemAction(browserItemAction) + m_browserItemAction(browserItemAction), + m_thingManager(thingManager) { connect(this, &BrowserItemActionInfo::finished, this, &BrowserItemActionInfo::deleteLater, Qt::QueuedConnection); @@ -67,9 +69,24 @@ Thing::ThingError BrowserItemActionInfo::status() const return m_status; } -void BrowserItemActionInfo::finish(Thing::ThingError status) +QString BrowserItemActionInfo::displayMessage() const +{ + return m_displayMessage; +} + +QString BrowserItemActionInfo::translatedDisplayMessage(const QLocale &locale) +{ + if (!m_thingManager || !m_thing) { + return m_displayMessage; + } + + return m_thingManager->translate(m_thing->pluginId(), m_displayMessage.toUtf8(), locale); +} + +void BrowserItemActionInfo::finish(Thing::ThingError status, const QString &displayMessage) { m_finished = true; m_status = status; + m_displayMessage = displayMessage; staticMetaObject.invokeMethod(this, "finished", Qt::QueuedConnection); } diff --git a/libnymea/integrations/browseritemactioninfo.h b/libnymea/integrations/browseritemactioninfo.h index a82ec01e..870d0e79 100644 --- a/libnymea/integrations/browseritemactioninfo.h +++ b/libnymea/integrations/browseritemactioninfo.h @@ -36,11 +36,13 @@ #include "thing.h" #include "types/browseritemaction.h" +class ThingManager; + class BrowserItemActionInfo : public QObject { Q_OBJECT public: - explicit BrowserItemActionInfo(Thing *thing, const BrowserItemAction &browserItemAction, QObject *parent, quint32 timeout = 0); + explicit BrowserItemActionInfo(Thing *thing, ThingManager *thingManager, const BrowserItemAction &browserItemAction, QObject *parent, quint32 timeout = 0); Thing *thing() const; BrowserItemAction browserItemAction() const; @@ -48,13 +50,15 @@ public: bool isFinished() const; Thing::ThingError status() const; + QString displayMessage() const; + QString translatedDisplayMessage(const QLocale &locale); signals: void finished(); void aborted(); public slots: - void finish(Thing::ThingError status); + void finish(Thing::ThingError status, const QString &displayMessage = QString()); private: Thing *m_thing = nullptr; @@ -62,6 +66,9 @@ private: bool m_finished = false; Thing::ThingError m_status = Thing::ThingErrorNoError; + QString m_displayMessage; + + ThingManager *m_thingManager = nullptr; }; #endif // BROWSERITEMACTIONINFO_H diff --git a/libnymea/integrations/browseritemresult.cpp b/libnymea/integrations/browseritemresult.cpp index 89401fe3..a028feac 100644 --- a/libnymea/integrations/browseritemresult.cpp +++ b/libnymea/integrations/browseritemresult.cpp @@ -29,14 +29,16 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "browseritemresult.h" +#include "thingmanager.h" #include -BrowserItemResult::BrowserItemResult(Thing *thing, const QString &itemId, const QLocale &locale, QObject *parent, quint32 timeout): +BrowserItemResult::BrowserItemResult(Thing *thing, ThingManager *thingManager, const QString &itemId, const QLocale &locale, QObject *parent, quint32 timeout): QObject(parent), m_thing(thing), m_itemId(itemId), - m_locale(locale) + m_locale(locale), + m_thingManager(thingManager) { connect(this, &BrowserItemResult::finished, this, &BrowserItemResult::deleteLater, Qt::QueuedConnection); @@ -78,15 +80,30 @@ Thing::ThingError BrowserItemResult::status() const return m_status; } +QString BrowserItemResult::displayMessage() const +{ + return m_displayMessage; +} + +QString BrowserItemResult::translatedDisplayMessage(const QLocale &locale) +{ + if (!m_thingManager || !m_thing) { + return m_displayMessage; + } + + return m_thingManager->translate(m_thing->pluginId(), m_displayMessage.toUtf8(), locale); +} + void BrowserItemResult::finish(const BrowserItem &item) { m_item = item; finish(Thing::ThingErrorNoError); } -void BrowserItemResult::finish(Thing::ThingError status) +void BrowserItemResult::finish(Thing::ThingError status, const QString &displayMessage) { m_finished = true; m_status = status; + m_displayMessage = displayMessage; staticMetaObject.invokeMethod(this, "finished", Qt::QueuedConnection); } diff --git a/libnymea/integrations/browseritemresult.h b/libnymea/integrations/browseritemresult.h index aeb47735..75882a43 100644 --- a/libnymea/integrations/browseritemresult.h +++ b/libnymea/integrations/browseritemresult.h @@ -35,12 +35,14 @@ #include "thing.h" +class ThingManager; + class BrowserItemResult : public QObject { Q_OBJECT public: - explicit BrowserItemResult(Thing *thing, const QString &itemId, const QLocale &locale, QObject *parent, quint32 timeout = 0); + explicit BrowserItemResult(Thing *thing, ThingManager *thingManager, const QString &itemId, const QLocale &locale, QObject *parent, quint32 timeout = 0); Thing* thing() const; QString itemId() const; @@ -50,10 +52,12 @@ public: bool isFinished() const; Thing::ThingError status() const; + QString displayMessage() const; + QString translatedDisplayMessage(const QLocale &locale); public slots: void finish(const BrowserItem &item); - void finish(Thing::ThingError status); + void finish(Thing::ThingError status, const QString &displayMessage = QString()); signals: void finished(); @@ -68,6 +72,9 @@ private: bool m_finished = false; Thing::ThingError m_status = Thing::ThingErrorNoError; + QString m_displayMessage; + + ThingManager *m_thingManager = nullptr; }; #endif // BROWSERITEMRESULT_H