Add a displayMessage for errors to browsing operations

pull/275/head
Michael Zanetti 2020-03-25 00:22:34 +01:00
parent b01e8c8ee4
commit d362ce913e
13 changed files with 204 additions and 48 deletions

View File

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

View File

@ -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<Device::DeviceError>());
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<Device::DeviceError>());
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<Device::DeviceError>());
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<ParamList>());
returns.insert("deviceError", enumRef<Device::DeviceError>());
returns.insert("o:displayMessage", enumValueName(String));
registerMethod("ExecuteBrowserItemAction", description, params, returns);
// Notifications
@ -788,7 +808,7 @@ JsonReply *DeviceHandler::BrowseDevice(const QVariantMap &params, 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 &params, 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 &params, 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 &params, const JsonCon
return jsonReply;
}
JsonReply *DeviceHandler::ExecuteBrowserItem(const QVariantMap &params)
JsonReply *DeviceHandler::ExecuteBrowserItem(const QVariantMap &params, 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 &params)
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<Device::ThingError>(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 &params)
return jsonReply;
}
JsonReply *DeviceHandler::ExecuteBrowserItemAction(const QVariantMap &params)
JsonReply *DeviceHandler::ExecuteBrowserItemAction(const QVariantMap &params, 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 &params)
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<Device::ThingError>(info->status()).replace("Thing", "Device"));
if (!info->displayMessage().isEmpty()) {
data.insert("displayMessage", info->translatedDisplayMessage(context.locale()));
}
jsonReply->setData(data);
jsonReply->finished();
});

View File

@ -167,8 +167,8 @@ public:
Q_INVOKABLE JsonReply *GetBrowserItem(const QVariantMap &params, const JsonContext &context) const;
Q_INVOKABLE JsonReply *ExecuteAction(const QVariantMap &params, const JsonContext &context);
Q_INVOKABLE JsonReply *ExecuteBrowserItem(const QVariantMap &params);
Q_INVOKABLE JsonReply *ExecuteBrowserItemAction(const QVariantMap &params);
Q_INVOKABLE JsonReply *ExecuteBrowserItem(const QVariantMap &params, const JsonContext &context);
Q_INVOKABLE JsonReply *ExecuteBrowserItemAction(const QVariantMap &params, const JsonContext &context);
static QVariantMap packBrowserItem(const BrowserItem &item);

View File

@ -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<Thing::ThingError>());
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<Thing::ThingError>());
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<Thing::ThingError>());
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<ParamList>());
returns.insert("thingError", enumRef<Thing::ThingError>());
returns.insert("o:displayMessage", enumValueName(String));
registerMethod("ExecuteBrowserItemAction", description, params, returns);
// Notifications
@ -800,7 +816,7 @@ JsonReply *IntegrationsHandler::BrowseThing(const QVariantMap &params, 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 &params, 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 &params, 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 &params, const J
return jsonReply;
}
JsonReply *IntegrationsHandler::ExecuteBrowserItem(const QVariantMap &params)
JsonReply *IntegrationsHandler::ExecuteBrowserItem(const QVariantMap &params, 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 &params)
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<Thing::ThingError>(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 &params)
return jsonReply;
}
JsonReply *IntegrationsHandler::ExecuteBrowserItemAction(const QVariantMap &params)
JsonReply *IntegrationsHandler::ExecuteBrowserItemAction(const QVariantMap &params, 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 &para
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<Thing::ThingError>(info->status()));
if (!info->displayMessage().isEmpty()) {
data.insert("displayMessage", info->translatedDisplayMessage(context.locale()));
}
jsonReply->setData(data);
jsonReply->finished();
});

View File

@ -69,8 +69,8 @@ public:
Q_INVOKABLE JsonReply *GetBrowserItem(const QVariantMap &params, const JsonContext &context) const;
Q_INVOKABLE JsonReply *ExecuteAction(const QVariantMap &params, const JsonContext &context);
Q_INVOKABLE JsonReply *ExecuteBrowserItem(const QVariantMap &params);
Q_INVOKABLE JsonReply *ExecuteBrowserItemAction(const QVariantMap &params);
Q_INVOKABLE JsonReply *ExecuteBrowserItem(const QVariantMap &params, const JsonContext &context);
Q_INVOKABLE JsonReply *ExecuteBrowserItemAction(const QVariantMap &params, const JsonContext &context);
static QVariantMap packBrowserItem(const BrowserItem &item);

View File

@ -29,13 +29,15 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "browseractioninfo.h"
#include "thingmanager.h"
#include <QTimer>
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);
}

View File

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

View File

@ -29,14 +29,16 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "browseresult.h"
#include "thingmanager.h"
#include <QTimer>
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);
}

View File

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

View File

@ -29,13 +29,15 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "browseritemactioninfo.h"
#include "thingmanager.h"
#include <QTimer>
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);
}

View File

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

View File

@ -29,14 +29,16 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "browseritemresult.h"
#include "thingmanager.h"
#include <QTimer>
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);
}

View File

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