From 775543248438c73a44736d5af927c7743d87e45c Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Fri, 30 Oct 2015 15:51:57 +0100 Subject: [PATCH] Added IMetaDataPlugin base class for parsing plugin name, description, etc... from json metadata file. --- accommodation/accommodation.cpp | 15 ++++--- accommodation/accommodation.h | 11 ++++-- accommodation/accommodation.json | 2 +- core/core.h | 1 + core/core.pro | 6 ++- core/imetadataplugin.cpp | 67 ++++++++++++++++++++++++++++++++ core/imetadataplugin.h | 34 ++++++++++++++++ core/iplugin.h | 1 + 8 files changed, 125 insertions(+), 12 deletions(-) create mode 100644 core/imetadataplugin.cpp create mode 100644 core/imetadataplugin.h diff --git a/accommodation/accommodation.cpp b/accommodation/accommodation.cpp index 34f993a..1a24ac4 100644 --- a/accommodation/accommodation.cpp +++ b/accommodation/accommodation.cpp @@ -11,6 +11,14 @@ Accommodation::Accommodation() { } +void Accommodation::initServiceUi() +{ + m_ui = new AccommodationForm(); + AccommodationService *service = new AccommodationService(); + m_service = service; +} + +/* QString Accommodation::pluginName() { return "Ubytovani"; @@ -18,14 +26,11 @@ QString Accommodation::pluginName() void Accommodation::init(const QJsonObject &metaData) { - qDebug() << "init accomodation" << metaData; - m_ui = new AccommodationForm(); - AccommodationService *service = new AccommodationService(); - m_service = service; + } QString Accommodation::pluginId() { return "ACCOMMODATION"; } - +*/ diff --git a/accommodation/accommodation.h b/accommodation/accommodation.h index 68c46a8..8d55730 100644 --- a/accommodation/accommodation.h +++ b/accommodation/accommodation.h @@ -6,7 +6,7 @@ #include #include -class ACCOMMODATIONSHARED_EXPORT Accommodation : public QObject, IPlugin +class ACCOMMODATIONSHARED_EXPORT Accommodation : public QObject, IMetaDataPlugin { Q_OBJECT @@ -16,9 +16,12 @@ class ACCOMMODATIONSHARED_EXPORT Accommodation : public QObject, IPlugin public: Accommodation(); - QString pluginName() Q_DECL_OVERRIDE; - void init(const QJsonObject &metaData) Q_DECL_OVERRIDE; - QString pluginId() Q_DECL_OVERRIDE; + // QString pluginName() Q_DECL_OVERRIDE; + // void init(const QJsonObject &metaData) Q_DECL_OVERRIDE; + // QString pluginId() Q_DECL_OVERRIDE; + +protected: + void initServiceUi() Q_DECL_OVERRIDE; }; diff --git a/accommodation/accommodation.json b/accommodation/accommodation.json index 7d4873e..c889e70 100644 --- a/accommodation/accommodation.json +++ b/accommodation/accommodation.json @@ -2,7 +2,7 @@ "id" : "ACCOMMODATION", "name" : { "default" : "Accommodation", - "CZ" : "Ubytování" + "CZ" : "Kemp" }, "descriptoin" : { "default" : "", diff --git a/core/core.h b/core/core.h index 676943c..4245fba 100644 --- a/core/core.h +++ b/core/core.h @@ -3,5 +3,6 @@ #include "context.h" #include "iplugin.h" +#include "imetadataplugin.h" #endif // CORE_H diff --git a/core/core.pro b/core/core.pro index 7ba4db5..890d0c4 100644 --- a/core/core.pro +++ b/core/core.pro @@ -13,14 +13,16 @@ DEFINES += CORE_LIBRARY SOURCES += \ data/user.cpp \ - context.cpp + context.cpp \ + imetadataplugin.cpp HEADERS += core.h\ core_global.h \ iplugin.h \ service.h \ data/user.h \ - context.h + context.h \ + imetadataplugin.h unix { target.path = /usr/lib diff --git a/core/imetadataplugin.cpp b/core/imetadataplugin.cpp new file mode 100644 index 0000000..ef96efc --- /dev/null +++ b/core/imetadataplugin.cpp @@ -0,0 +1,67 @@ +#include "imetadataplugin.h" + +#include +#include +#include + +IMetaDataPlugin::IMetaDataPlugin() +{ +} + +IMetaDataPlugin::~IMetaDataPlugin() +{ +} + +QString IMetaDataPlugin::pluginName() +{ + return m_name; +} + +QString IMetaDataPlugin::pluginId() +{ + return m_id; +} + +QString IMetaDataPlugin::pluginDescription() +{ + return m_description; +} + +void IMetaDataPlugin::init(const QJsonObject &metaData) +{ + parseMetaData(metaData); + initServiceUi(); +} + +void IMetaDataPlugin::parseMetaData(const QJsonObject &metaData) +{ + qDebug() << metaData; + QJsonValue data = metaData["MetaData"]; + if (!data.isObject()) { + return; + } + + m_name = parseLocaleText(data.toObject()["name"].toObject()); + m_description = parseLocaleText(data.toObject()["description"].toObject()); + m_id = data.toObject()["id"].toString(); +} + +QString IMetaDataPlugin::parseLocaleText(const QJsonObject &object) +{ + QString locale = QLocale::system().name(); + QString ret; + + foreach (QString key, object.keys()) { + if (locale.contains(key, Qt::CaseInsensitive)) { + ret = object[key].toString(); + break; + } + } + + if (ret.isEmpty()) { + ret = object["default"].toString(); + } + + return ret; +} + diff --git a/core/imetadataplugin.h b/core/imetadataplugin.h new file mode 100644 index 0000000..463f5aa --- /dev/null +++ b/core/imetadataplugin.h @@ -0,0 +1,34 @@ +#ifndef IMETADATAPLUGIN_H +#define IMETADATAPLUGIN_H + +#include + +#include "core_global.h" +#include "iplugin.h" + +class CORESHARED_EXPORT IMetaDataPlugin : public IPlugin +{ +public: + IMetaDataPlugin(); + ~IMetaDataPlugin(); + + // IPlugin interface +public: + virtual QString pluginName(); + virtual QString pluginId(); + virtual QString pluginDescription(); + virtual void init(const QJsonObject &metaData); + +protected: + virtual void initServiceUi() = 0; + virtual void parseMetaData(const QJsonObject &metaData); + +private: + QString m_name; + QString m_id; + QString m_description; + + QString parseLocaleText(const QJsonObject &object); +}; + +#endif // IMETADATAPLUGIN_H diff --git a/core/iplugin.h b/core/iplugin.h index c0ceae5..0682550 100644 --- a/core/iplugin.h +++ b/core/iplugin.h @@ -19,6 +19,7 @@ public: virtual ~IPlugin() { } virtual QString pluginName() = 0; virtual QString pluginId() = 0; + virtual QString pluginDescription() = 0; virtual void init(const QJsonObject &metaData) = 0; virtual QWidget *ui() { return m_ui;