The implementation of SwitchManager constructor 39/127939/5
authorLukasz Wlazly <l.wlazly@partner.samsung.com>
Fri, 28 Apr 2017 09:21:55 +0000 (11:21 +0200)
committerLukasz Wlazly <l.wlazly@partner.samsung.com>
Fri, 12 May 2017 10:18:54 +0000 (12:18 +0200)
Change-Id: I32b50147732a11cae46a663d2f80097be89d75f5

37 files changed:
inc/AccessoriesSwitchProvider.hpp
inc/ActivityBuilder.hpp
inc/ActivityBuilderFactory.hpp [new file with mode: 0644]
inc/ActivityDispatcher.hpp
inc/ActivityObserver.hpp
inc/ActivityTypes.hpp [moved from inc/ActivityType.hpp with 58% similarity]
inc/AdHocActivityDispatcher.hpp
inc/AsyncActivityConstructor.hpp
inc/CachedActivityBuilderFactory.hpp [new file with mode: 0644]
inc/CompositeSwitchProvider.hpp
inc/Condition.hpp
inc/ConditionAll.hpp [new file with mode: 0644]
inc/ConditionAny.hpp [new file with mode: 0644]
inc/ConditionEqual.hpp [new file with mode: 0644]
inc/Configuration.hpp
inc/ConfigurationItem.hpp
inc/ConfigurationItemObserver.hpp [new file with mode: 0644]
inc/SQLiteConfiguration.hpp
inc/Subject.hpp
inc/Switch.hpp
inc/SwitchGroup.hpp [deleted file]
inc/SwitchManager.hpp [new file with mode: 0644]
inc/SwitchProvider.hpp
inc/universalswitch.hpp
src/AccessoriesSwitchProvider.cpp
src/ActivityTypes.cpp [moved from src/ActivityType.cpp with 85% similarity]
src/AdHocActivityDispatcher.cpp [new file with mode: 0644]
src/CMakeLists.txt
src/CachedActivityBuilderFactory.cpp [new file with mode: 0644]
src/CompositeSwitchProvider.cpp
src/ConfigurationItem.cpp
src/SQLiteConfiguration.cpp
src/Switch.cpp
src/SwitchGroup.cpp [deleted file]
src/SwitchManager.cpp [new file with mode: 0644]
src/SwitchProvider.cpp
src/universalswitch.cpp

index d57058e..ecacf4b 100644 (file)
@@ -3,20 +3,25 @@
 
 #include "SwitchProvider.hpp"
 
+#include <Elementary.h>
+
 #include <vector>
 #include <string>
+#include <memory>
 
-class AccessoriesSwitchProvider : public SwitchProvider
+class AccessoriesSwitchProvider : public SwitchProvider, std::enable_shared_from_this<AccessoriesSwitchProvider>
 {
 public:
-       static std::shared_ptr<AccessoriesSwitchProvider> instance();
+       AccessoriesSwitchProvider();
+
+       SwitchCollection findAllSwitches() override;
+       std::shared_ptr<Switch> findSwitchById(const std::string &id) override;
 
 private:
-       AccessoriesSwitchProvider();
        static Eina_Bool onEcoreEventKeyDownCb(void *data, int type, void *ev);
-       static std::shared_ptr<AccessoriesSwitchProvider> initialize();
 
        const std::vector<std::string> keys;
+       SwitchCollection switches;
 };
 
 #endif
index 53cd5fe..bda5d87 100644 (file)
@@ -2,13 +2,14 @@
 #define ACTIVITY_BUILDER_HPP
 
 #include "Activity.hpp"
+#include "Subject.hpp"
 #include "UIElement.hpp"
 
 class ActivityBuilder : public Subject<Activity>
 {
 public:
-       void createNewActivity();
-       void buildUIElement();
+       virtual void createNewActivity() = 0;
+       virtual void buildUIElement() = 0;
 };
 
 #endif
diff --git a/inc/ActivityBuilderFactory.hpp b/inc/ActivityBuilderFactory.hpp
new file mode 100644 (file)
index 0000000..39f02ed
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef ACTIVITY_BUILDER_FACTORY_HPP
+#define ACTIVITY_BUILDER_FACTORY_HPP
+
+#include "ActivityBuilder.hpp"
+
+#include <memory>
+
+class ActivityBuilderFactory
+{
+public:
+       virtual ~ActivityBuilderFactory(){};
+       virtual std::shared_ptr<ActivityBuilder> getActivityBuilder(const std::string &activityType) = 0;
+};
+
+#endif
index cc1ecf8..ae9815f 100644 (file)
@@ -5,11 +5,12 @@
 
 #include <memory>
 
-class ActivityDispatcher : public ActivityObserver
+class ActivityDispatcher
 {
-       virtual ~ActivityDispatcher();
+public:
+       virtual ~ActivityDispatcher(){};
 
-       virtual void dispatch(std::shared_ptr<Activity> activity);
+       virtual void dispatch(std::shared_ptr<Activity> activity) = 0;
 };
 
 #endif
index 676c5d2..2cce477 100644 (file)
@@ -6,9 +6,6 @@
 
 class ActivityObserver : public Observer<Activity>
 {
-       virtual ~ActivityObserver() {};
-
-       virtual void update(std::shared_ptr<Activity> activity);
 };
 
 #endif
similarity index 58%
rename from inc/ActivityType.hpp
rename to inc/ActivityTypes.hpp
index c71ebf6..751c0c4 100644 (file)
@@ -1,17 +1,17 @@
-#ifndef ACTIVITY_TYPE_HPP
-#define ACTIVITY_TYPE_HPP
+#ifndef ACTIVITY_TYPES_HPP
+#define ACTIVITY_TYPES_HPP
 
 #include <set>
 #include <string>
 
-class ActivityType
+class ActivityTypes
 {
 public:
        static std::set<std::string> getActivityCollection();
 
 private:
-       ActivityType();
-       ~ActivityType(){};
+       ActivityTypes();
+       ~ActivityTypes(){};
 
        std::set<std::string> activities;
 };
index 69f9652..8dc003e 100644 (file)
@@ -1,10 +1,12 @@
 #ifndef ADHOC_ACTIVITY_DISPATVHER_HPP
 #define ADHOC_ACTIVITY_DISPATVHER_HPP
 
-#include "Activity.hpp"
+#include "ActivityDispatcher.hpp"
 
 class AdHocActivityDispatcher : public ActivityDispatcher
 {
+public:
+       void dispatch(std::shared_ptr<Activity> activity) override;
 };
 
 #endif
index 2f81295..cee233d 100644 (file)
@@ -1,10 +1,13 @@
 #ifndef ASYNC_ACTIVITY_CONSTRUCTOR_HPP
 #define ASYNC_ACTIVITY_CONSTRUCTOR_HPP
 
+#include "ActivityBuilder.hpp"
+#include "ActivityObserver.hpp"
+
 class AsyncActivityConstructor
 {
 public:
-       void construct(std::shared_ptr<ActivityBuilder> builder, std::shared_ptr<ActivityObserver> observer);
+       void construct(const std::shared_ptr<ActivityBuilder> &builder, const std::shared_ptr<ActivityObserver> &observer);
 };
 
 #endif
diff --git a/inc/CachedActivityBuilderFactory.hpp b/inc/CachedActivityBuilderFactory.hpp
new file mode 100644 (file)
index 0000000..13a14b1
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef CACHED_ACTIVITY_BUILDER_FACTORY_HPP
+#define CACHED_ACTIVITY_BUILDER_FACTORY_HPP
+
+#include "ActivityBuilderFactory.hpp"
+
+class CachedActivityBuilderFactory : public ActivityBuilderFactory
+{
+public:
+       std::shared_ptr<ActivityBuilder> getActivityBuilder(const std::string &activityType) override;
+};
+
+#endif
index 916ce04..540a7b6 100644 (file)
@@ -8,13 +8,12 @@
 class CompositeSwitchProvider : public SwitchProvider
 {
 public:
-       GroupCollection findAllGroups() override;
-       GroupPtr findGroupByName(const std::string &name) override;
-       SwitchCollection findAllSwitches() override;
-       SwitchCollection findSwitchesByGroup(const std::string &groupName) override;
-       SwitchPtr findSwitchByGroupAndName(const std::string &groupName, const std::string &switchName) override;
+       CompositeSwitchProvider();
 
+       SwitchCollection findAllSwitches() override;
+       std::shared_ptr<Switch> findSwitchById(const std::string &id) override;
        void add(std::shared_ptr<SwitchProvider> provider);
+       std::vector<std::shared_ptr<SwitchProvider>> getProviders();
 
 private:
        std::vector<std::shared_ptr<SwitchProvider>> providers;
index 0ec84c8..d777f71 100644 (file)
@@ -4,10 +4,8 @@
 template<typename T>
 class Condition {
 public:
-       bool matching(std::shared_ptr<T> item) {
-               //TODO(full implementation)
-               return true;
-       }
+       virtual ~Condition(){};
+       virtual bool matching(const T *item) const = 0;
 };
 
 #endif
diff --git a/inc/ConditionAll.hpp b/inc/ConditionAll.hpp
new file mode 100644 (file)
index 0000000..7dde971
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef CONDITION_ALL_HPP
+#define CONDITION_ALL_HPP
+
+#include "Condition.hpp"
+
+template<typename T>
+class ConditionAll : public Condition<T>
+{
+public:
+       bool matching(const T *item) const override {
+               return true;
+       }
+};
+
+#endif
diff --git a/inc/ConditionAny.hpp b/inc/ConditionAny.hpp
new file mode 100644 (file)
index 0000000..43406bc
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef CONDITION_ANY_HPP
+#define CONDITION_ANY_HPP
+
+#include "Condition.hpp"
+
+template<typename T>
+class ConditionAny : public Condition<T>
+{
+public:
+       ConditionAny(std::vector<std::shared_ptr<T>> items)
+               : items(std::move(items))
+       {}
+
+       bool matching(const T* item) const override {
+               for (auto &it : items)
+                       if (*item == *it)
+                               return true;
+               return false;
+       }
+
+private:
+       std::vector<std::shared_ptr<T>> items;
+};
+
+#endif
diff --git a/inc/ConditionEqual.hpp b/inc/ConditionEqual.hpp
new file mode 100644 (file)
index 0000000..54e62b2
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef CONDITION_EQUAL_HPP
+#define CONDITION_EQUAL_HPP
+
+#include "Condition.hpp"
+
+template<typename T>
+class ConditionEqual : public Condition<T>
+{
+public:
+       ConditionEqual(std::shared_ptr<T> item)
+               :item(std::move(item))
+       {}
+
+       bool matching(const T *item) const override {
+               return *item == *(this->item);
+       }
+
+private:
+       std::shared_ptr<T> item;
+};
+
+#endif
index 3913be9..ddfb011 100644 (file)
@@ -1,22 +1,22 @@
 #ifndef CONFIGURATION_HPP_
 #define CONFIGURATION_HPP_
 
-#include <memory>
-#include <vector>
-
 #include "Subject.hpp"
 #include "ConfigurationItem.hpp"
 #include "Condition.hpp"
 
+#include <memory>
+#include <vector>
+
 class Configuration : public Subject<ConfigurationItem>
 {
 public:
        virtual ~Configuration() {};
-       virtual void add(std::shared_ptr<ConfigurationItem> const &item) = 0;
-       virtual void update(std::shared_ptr<ConfigurationItem> const &item) = 0;
+       virtual void add(const std::shared_ptr<ConfigurationItem> &item) = 0;
+       virtual void update(const std::shared_ptr<ConfigurationItem> &item) = 0;
        virtual void remove(const std::string &key, const std::string &group) = 0;
-       virtual std::vector<std::shared_ptr<ConfigurationItem>> find(std::shared_ptr<Condition<ConfigurationItem>> condition) const = 0;
-
+       virtual std::shared_ptr<ConfigurationItem> find(const std::string &name, const std::string &group) const = 0;
+       virtual std::vector<std::shared_ptr<ConfigurationItem>> findAll() const = 0;
 };
 
 #endif
index e433f69..918c530 100644 (file)
@@ -13,23 +13,28 @@ enum class ChangeType {
 class ConfigurationItem
 {
 public:
+       ConfigurationItem() = default;
        ConfigurationItem(ChangeType type);
        ConfigurationItem(std::string discoveryName, std::string groupName, std::string value, ChangeType type);
        ConfigurationItem(std::string discoveryName, std::string displayName, std::string value);
 
+       bool operator==(const ConfigurationItem &item) const;
+
        std::string getKey() const;
-       void setKey(std::string discoveryName);
+       void setKey(const std::string &discoveryName);
        std::string getGroup() const;
-       void setGroup(std::string groupName);
+       void setGroup(const std::string &groupName);
        std::string getName() const;
-       void setName(std::string displayName);
+       void setName(const std::string &displayName);
        std::string getDescription() const;
-       void setDescription(std::string description);
+       void setDescription(const std::string &description);
        std::string getValue() const;
-       void setValue(std::string val);
+       void setValue(const std::string &val);
        ChangeType getChangeType() const;
        void setChangeType(ChangeType type);
 
+       void construct(int argc, char **argv, char **azColName);
+
 private:
        std::string key;
        std::string group;
diff --git a/inc/ConfigurationItemObserver.hpp b/inc/ConfigurationItemObserver.hpp
new file mode 100644 (file)
index 0000000..b6aadba
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef CONFIGURATION_ITEM_OBSERVER_HPP
+#define CONFIGURATION_ITEM_OBSERVER_HPP
+
+#include "Observer.hpp"
+#include "ConfigurationItem.hpp"
+
+class ConfigurationItemObserver : public Observer<ConfigurationItem>
+{
+
+};
+
+#endif
index f0f5c97..f930964 100644 (file)
@@ -1,43 +1,42 @@
 #ifndef SQLITE_CONFIGURATION_HPP
 #define SQLITE_CONFIGURATION_HPP
 
-#include <string>
-#include <exception>
+#include "Configuration.hpp"
+
 #include <sqlite3.h>
 #include <openssl/aes.h>
 #include <openssl/crypto.h>
 #include <openssl/rand.h>
 #include <openssl/evp.h>
 
-#include "Configuration.hpp"
+#include <string>
+#include <functional>
 
-class SQLiteConfiguration final : public Configuration
+class SQLiteConfiguration : public Configuration
 {
 public:
        static SQLiteConfiguration &instance();
 
-       void add(std::shared_ptr<ConfigurationItem> const &item) override;
-       void update(std::shared_ptr<ConfigurationItem> const &item) override;
+       void add(const std::shared_ptr<ConfigurationItem> &item) override;
+       void update(const std::shared_ptr<ConfigurationItem> &item) override;
        void remove(const std::string &key, const std::string &group) override;
-       std::vector<std::shared_ptr<ConfigurationItem>> find(std::shared_ptr<Condition<ConfigurationItem>> condition) const override;
+       std::shared_ptr<ConfigurationItem> find(const std::string &name, const std::string &group) const override;
+       std::vector<std::shared_ptr<ConfigurationItem>> findAll() const override;
 
        void printRecords() const;
        void clearTable();
 
+       using execCallback_t = std::function<void(int, char **, char **)>;
 private:
        SQLiteConfiguration();
        ~SQLiteConfiguration();
 
        void initialize();
 
-       typedef int (*execCallback_t)(void *, int, char **, char **);
-       bool executeQuery(const std::string &query, execCallback_t cb, void *data);
-       bool executeQuery(const std::string &query);
+       bool executeQuery(const std::string &query, const execCallback_t &cb) const;
+       bool executeQuery(const std::string &query) const;
        void createTable();
-       std::shared_ptr<ConfigurationItem> buildItem(const std::string &key, const std::string &group);
 
-       static int debugPrintRecordsCb(void *data, int argc, char **argv, char **azColName);
-       static int buildItemCb(void *data, int argc, char **argv, char **azColName);
        static void update_callback(void *data, int operation, char const *database, char const *table, sqlite_int64 rowid);
 
        const std::string password;
index a1ed10d..1e7196b 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef SUBJECT_HPP_
 #define SUBJECT_HPP_
 
-#include <Condition.hpp>
+#include "Condition.hpp"
 #include "ConfigurationItem.hpp"
 #include "Observer.hpp"
 #include <vector>
@@ -11,15 +11,15 @@ template <typename T>
 class Subject
 {
 public:
-       void attach(std::shared_ptr<Observer<T>> observer, std::shared_ptr<Condition<T>> rule)
+       void attach(std::weak_ptr<Observer<T>> observer, std::unique_ptr<Condition<T>> cond)
        {
-               observers.emplace_back({std::move(observer), std::move(rule)});
+               observers.emplace_back(std::move(observer), std::move(cond));
        }
 
-       void detach(const std::shared_ptr<Observer<T>> &observer, const std::shared_ptr<Condition<T>> &rule)
+       void detach(const Observer<T> *observer, const Condition<T> *cond)
        {
                for (auto it = observers.begin(); it != observers.end(); ++it)
-                       if (it->first == observer && it->second == rule) {
+                       if (it->first.get() == observer && it->second.get() == cond) {
                                *it = std::move(observers.back());
                                observers.pop_back();
                                break;
@@ -29,12 +29,14 @@ public:
 protected:
        void notify(std::shared_ptr<T> item)
        {
-               for (auto &it : observers)
-                       if (it.second->matching(item))
-                               it.first->update(item);
+               for (auto &it : observers) {
+                       auto observer = it.first.lock();
+                       if (it.second->matching(item.get()))
+                               observer->update(item);
+               }
        }
 
-       std::vector<std::pair<std::shared_ptr<Observer<T>>, std::shared_ptr<Condition<T>>>> observers;
+       std::vector<std::pair<std::weak_ptr<Observer<T>>, std::unique_ptr<Condition<T>>>> observers;
 };
 
 #endif
index 33dbc37..07dbb7c 100644 (file)
@@ -1,30 +1,33 @@
 #ifndef SWITCH_HPP
 #define SWITCH_HPP
 
-#include "SwitchGroup.hpp"
 #include "SwitchProvider.hpp"
 
-class SwitchGroup;
+#include <string>
+#include <memory>
+
 class SwitchProvider;
+
 class Switch
 {
 public:
-       Switch(std::string key, std::shared_ptr<const SwitchGroup> group, std::shared_ptr<const SwitchProvider> provider);
+       Switch(std::string id, std::weak_ptr<const SwitchProvider> provider);
 
-       std::shared_ptr<const SwitchGroup> getGroup() const ;
-       std::shared_ptr<const SwitchProvider> getProvider() const;
+       std::weak_ptr<const SwitchProvider> getProvider() const;
+       bool operator==(const Switch &sw) const;
 
-       std::string getKey() const;
+       std::string getId() const;
+       std::string getName() const;
+       std::string getDescription() const;
 
        void setName(const std::string &name);
-       void setDesc(const std::string &desc);
+       void setDescription(const std::string &desc);
 
 private:
-       const std::string key;
+       const std::string id;
        std::string name;
        std::string description;
-       std::shared_ptr<const SwitchGroup> group;
-       std::shared_ptr<const SwitchProvider> provider;
+       std::weak_ptr<const SwitchProvider> provider;
 };
 
 #endif
diff --git a/inc/SwitchGroup.hpp b/inc/SwitchGroup.hpp
deleted file mode 100644 (file)
index 6d73679..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-#ifndef SWITCH_GROUP_HPP
-#define SWITCH_GROUP_HPP
-
-#include <string>
-#include <vector>
-#include <memory>
-
-#include "Switch.hpp"
-
-class Switch;
-class SwitchGroup
-{
-public:
-       SwitchGroup(std::string key);
-       void addSwitch(std::shared_ptr<Switch> sw);
-       std::string getKey();
-       std::vector<std::shared_ptr<Switch>> getSwitches();
-
-private:
-       const std::string key;
-       std::string name;
-       std::vector<std::shared_ptr<Switch>> switches;
-};
-
-
-#endif
diff --git a/inc/SwitchManager.hpp b/inc/SwitchManager.hpp
new file mode 100644 (file)
index 0000000..24f986b
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef SWITCH_MANAGER_HPP
+#define SWITCH_MANAGER_HPP
+
+#include "ConfigurationItemObserver.hpp"
+#include "SwitchObserver.hpp"
+#include "SwitchProvider.hpp"
+#include "AsyncActivityConstructor.hpp"
+#include "ActivityDispatcher.hpp"
+#include "Configuration.hpp"
+#include "Condition.hpp"
+#include "ActivityBuilderFactory.hpp"
+
+#include <memory>
+
+class SwitchManager : public ConfigurationItemObserver, public SwitchObserver, public ActivityObserver, std::enable_shared_from_this<SwitchManager>
+{
+public:
+       SwitchManager(std::shared_ptr<SwitchProvider> provider,
+                     std::shared_ptr<Configuration> configuration,
+                     std::shared_ptr<ActivityBuilderFactory> factory);
+
+private:
+       std::shared_ptr<SwitchProvider> switchProvider;
+       std::shared_ptr<Configuration> configuration;
+       std::shared_ptr<ActivityBuilderFactory> activityBuilderFactory;
+       std::shared_ptr<AsyncActivityConstructor> asyncActivityConstrutor;
+       std::shared_ptr<ActivityDispatcher> activityDispatcher;
+};
+
+#endif
index baab2e4..ce2803f 100644 (file)
@@ -2,34 +2,29 @@
 #define SWITCH_PROVIDER_HPP
 
 #include "Switch.hpp"
-#include "SwitchGroup.hpp"
 #include "Subject.hpp"
 
-#include <Elementary.h>
-
+#include <string>
 #include <vector>
 #include <memory>
 
 class Switch;
-class SwitchGroup;
 
 class SwitchProvider : public Subject<Switch>
 {
 public:
-       using GroupCollection = std::vector<std::shared_ptr<SwitchGroup>>;
        using SwitchCollection = std::vector<std::shared_ptr<Switch>>;
        using SwitchPtr = std::shared_ptr<const Switch>;
-       using GroupPtr = std::shared_ptr<const SwitchGroup>;
 
        virtual ~SwitchProvider() {};
-       virtual GroupCollection findAllGroups();
-       virtual GroupPtr findGroupByName(const std::string &name);
-       virtual SwitchCollection findAllSwitches();
-       virtual SwitchCollection findSwitchesByGroup(const std::string &groupName);
-       virtual SwitchPtr findSwitchByGroupAndName(const std::string &groupName, const std::string &switchName);
+       virtual SwitchCollection findAllSwitches() = 0;
+       virtual std::shared_ptr<Switch> findSwitchById(const std::string &id) = 0;
+
+       std::string getId() const;
 
 protected:
-       GroupCollection groups;
+       SwitchProvider(std::string id);
+       const std::string id;
 };
 
 #endif
index feeb1ad..b65206f 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef __universalswitch_H__
-#define __universalswitch_H__
+#ifndef UNIVERSAL_SWITCH_HPP_
+#define UNIVERSAL_SWITCH_HPP_
 
 #include <libintl.h>
 #include <dlog.h>
@@ -69,4 +69,4 @@
 
 #define _(str) (gettext(str))
 
-#endif /* __universalswitch_H__ */
+#endif /* UNIVERSAL_SWITCH_HPP_ */
index d4bcd10..986342a 100644 (file)
@@ -1,7 +1,7 @@
 #include "AccessoriesSwitchProvider.hpp"
 
 AccessoriesSwitchProvider::AccessoriesSwitchProvider()
-       : keys(
+       : SwitchProvider("AccessoriesSwitchProvider"), keys(
 {"XF86AddFavorite",
        "XF86ApplicationLeft",
        "XF86ApplicationRight",
@@ -674,41 +674,38 @@ AccessoriesSwitchProvider::AccessoriesSwitchProvider()
        "thorn",
        "ydiaeresis"
 })
-{}
-
-std::shared_ptr<AccessoriesSwitchProvider> AccessoriesSwitchProvider::instance()
 {
-       static auto instance = initialize();
-       return instance;
+       for (auto &key : keys)
+               switches.emplace_back(std::make_shared<Switch>(key, shared_from_this()));
+
+       ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, onEcoreEventKeyDownCb, this);
 }
 
-Eina_Bool AccessoriesSwitchProvider::onEcoreEventKeyDownCb(void *data, int type, void *ev)
+SwitchProvider::SwitchCollection AccessoriesSwitchProvider::findAllSwitches()
 {
-       auto event = static_cast<Ecore_Event_Key *>(ev);
-       std::string key(event->key);
-       auto switches = AccessoriesSwitchProvider::instance()->groups[0]->getSwitches();
+       return switches;
+}
 
-       for (auto &s : switches)
-               if (s->getKey() == key) {
-                       AccessoriesSwitchProvider::instance()->notify(s);
-                       break;
-               }
+std::shared_ptr<Switch> AccessoriesSwitchProvider::findSwitchById(const std::string &id)
+{
+       for (auto &sw : switches) {
+               if (sw->getId() == id)
+                       return sw;
+       }
 
-       return ECORE_CALLBACK_PASS_ON;
+       return nullptr;
 }
 
-std::shared_ptr<AccessoriesSwitchProvider> AccessoriesSwitchProvider::initialize()
+Eina_Bool AccessoriesSwitchProvider::onEcoreEventKeyDownCb(void *data, int type, void *ev)
 {
-       auto provider = std::shared_ptr<AccessoriesSwitchProvider>(new AccessoriesSwitchProvider);
-       auto group = std::make_shared<SwitchGroup>("accessories");
+       auto event = static_cast<Ecore_Event_Key *>(ev);
+       std::string id(event->key);
 
-       for (auto &key : provider->keys) {
-               auto s = std::make_shared<Switch>(key, group, provider);
-               group->addSwitch(s);
-       }
+       auto accessoriesSwitchProvider = reinterpret_cast<AccessoriesSwitchProvider*>(data);
+       auto sw = accessoriesSwitchProvider->findSwitchById(id);
 
-       provider->groups.push_back(group);
-       ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, onEcoreEventKeyDownCb, nullptr);
+       if (sw)
+               accessoriesSwitchProvider->notify(sw);
 
-       return provider;
+       return ECORE_CALLBACK_PASS_ON;
 }
similarity index 85%
rename from src/ActivityType.cpp
rename to src/ActivityTypes.cpp
index 118ee03..4576e53 100644 (file)
@@ -1,11 +1,11 @@
-#include "ActivityType.hpp"
+#include "ActivityTypes.hpp"
 
-std::set<std::string> ActivityType::getActivityCollection() {
-       static ActivityType activityType;
-       return activityType.activities;
+std::set<std::string> ActivityTypes::getActivityCollection() {
+       static ActivityTypes activityTypes;
+       return activityTypes.activities;
 }
 
-ActivityType::ActivityType() {
+ActivityTypes::ActivityTypes() {
        for (auto name : {"ACTIVITY_SELECT",
        "ACTIVITY_TOUCH",
        "ACTIVITY_MORE_OPTIONS",
diff --git a/src/AdHocActivityDispatcher.cpp b/src/AdHocActivityDispatcher.cpp
new file mode 100644 (file)
index 0000000..2deb598
--- /dev/null
@@ -0,0 +1,6 @@
+#include "AdHocActivityDispatcher.hpp"
+
+void AdHocActivityDispatcher::dispatch(std::shared_ptr<Activity> activity) {
+       if (activity)
+               activity->execute();
+}
index 93e8e78..f8ef960 100644 (file)
@@ -1,11 +1,23 @@
-FILE(GLOB SRCS "*.cpp")
-
-INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/inc)
-
+SET(SRCS
+       AccessoriesSwitchProvider.cpp
+       ActivityTypes.cpp
+       AdHocActivityDispatcher.cpp
+       CachedActivityBuilderFactory.cpp
+       Configuration.cpp
+       QueryBuilder.cpp
+       Switch.cpp
+       SwitchProvider.cpp
+       universalswitch.cpp
+       Activity.cpp
+       CompositeSwitchProvider.cpp
+       ConfigurationItem.cpp
+       SQLiteConfiguration.cpp
+       SwitchManager.cpp
+)
 
 ADD_LIBRARY(universal-switch-obj OBJECT ${SRCS})
 
-INCLUDE_DIRECTORIES(${pkgs_INCLUDE_DIRS})
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/inc ${pkgs_INCLUDE_DIRS})
 
 ADD_EXECUTABLE(${PROJECT_NAME}  $<TARGET_OBJECTS:universal-switch-obj>)
 TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LIBRARIES})
diff --git a/src/CachedActivityBuilderFactory.cpp b/src/CachedActivityBuilderFactory.cpp
new file mode 100644 (file)
index 0000000..6d91340
--- /dev/null
@@ -0,0 +1,4 @@
+#include "CachedActivityBuilderFactory.hpp"
+
+std::shared_ptr<ActivityBuilder> CachedActivityBuilderFactory::getActivityBuilder(const std::string &activityType) {
+}
index 6ceb97c..d7a686d 100644 (file)
@@ -1,27 +1,8 @@
 #include "CompositeSwitchProvider.hpp"
 
-CompositeSwitchProvider::GroupCollection CompositeSwitchProvider::findAllGroups()
-{
-       GroupCollection result;
-
-       for (auto &provider : providers) {
-               GroupCollection groups = provider->findAllGroups();
-               result.insert(std::end(result), std::begin(groups), std::end(groups));
-       }
-
-       return result;
-}
-
-std::shared_ptr<const SwitchGroup> CompositeSwitchProvider::findGroupByName(const std::string &name)
-{
-       for (auto &provider : providers) {
-               auto group = provider->findGroupByName(name);
-               if (group)
-                       return group;
-       }
-
-       return nullptr;
-}
+CompositeSwitchProvider::CompositeSwitchProvider()
+       : SwitchProvider("CompositeSwitchProvider")
+{}
 
 CompositeSwitchProvider::SwitchCollection CompositeSwitchProvider::findAllSwitches()
 {
@@ -35,22 +16,11 @@ CompositeSwitchProvider::SwitchCollection CompositeSwitchProvider::findAllSwitch
        return result;
 }
 
-CompositeSwitchProvider::SwitchCollection CompositeSwitchProvider::findSwitchesByGroup(const std::string &groupName)
+std::shared_ptr<Switch> CompositeSwitchProvider::findSwitchById(const std::string &id)
 {
-       SwitchCollection result;
-
        for (auto &provider : providers) {
-               auto switches = provider->findSwitchesByGroup(groupName);
-               result.insert(std::end(result), std::begin(switches), std::end(switches));
-       }
+               auto sw = provider->findSwitchById(id);
 
-       return result;
-}
-
-SwitchProvider::SwitchPtr CompositeSwitchProvider::findSwitchByGroupAndName(const std::string &groupName, const std::string &switchName)
-{
-       for (auto &provider : providers) {
-               auto sw = provider->findSwitchByGroupAndName(groupName, switchName);
                if (sw)
                        return sw;
        }
@@ -62,3 +32,8 @@ void CompositeSwitchProvider::add(std::shared_ptr<SwitchProvider> provider)
 {
        providers.push_back(provider);
 }
+
+std::vector<std::shared_ptr<SwitchProvider>> CompositeSwitchProvider::getProviders()
+{
+       return providers;
+}
index 4c06bd8..a260fe3 100644 (file)
@@ -1,5 +1,7 @@
 #include "ConfigurationItem.hpp"
 
+#include <dlog.h>
+
 ConfigurationItem::ConfigurationItem(ChangeType type)
        : changeType(type)
 {}
@@ -12,14 +14,20 @@ ConfigurationItem::ConfigurationItem(std::string discoveryName, std::string disp
        : key(std::move(discoveryName)), name(std::move(displayName)), value(std::move(val))
 {}
 
+bool ConfigurationItem::operator==(const ConfigurationItem &item) const
+{
+       return key == item.getKey() && group == item.getGroup() && name == item.getName()
+           && description == item.getDescription() && value == item.getValue() && changeType == item.getChangeType();
+}
+
 std::string ConfigurationItem::getKey() const
 {
        return key;
 }
 
-void ConfigurationItem::setKey(std::string discoveryName)
+void ConfigurationItem::setKey(const std::string &discoveryName)
 {
-       key = std::move(discoveryName);
+       key = discoveryName;
 }
 
 std::string ConfigurationItem::getGroup() const
@@ -27,9 +35,9 @@ std::string ConfigurationItem::getGroup() const
        return group;
 }
 
-void ConfigurationItem::setGroup(std::string groupName)
+void ConfigurationItem::setGroup(const std::string &groupName)
 {
-       group = std::move(groupName);
+       group = groupName;
 }
 
 std::string ConfigurationItem::getName() const
@@ -37,9 +45,9 @@ std::string ConfigurationItem::getName() const
        return name;
 }
 
-void ConfigurationItem::setName(std::string displayName)
+void ConfigurationItem::setName(const std::string &displayName)
 {
-       name = std::move(displayName);
+       name = displayName;
 }
 
 std::string ConfigurationItem::getDescription() const
@@ -47,9 +55,9 @@ std::string ConfigurationItem::getDescription() const
        return description;
 }
 
-void ConfigurationItem::setDescription(std::string description)
+void ConfigurationItem::setDescription(const std::string &description)
 {
-       this->description = std::move(description);
+       this->description = description;
 }
 
 std::string ConfigurationItem::getValue() const
@@ -57,9 +65,9 @@ std::string ConfigurationItem::getValue() const
        return value;
 }
 
-void ConfigurationItem::setValue(std::string val)
+void ConfigurationItem::setValue(const std::string &val)
 {
-       value = std::move(val);
+       value = val;
 }
 
 ChangeType ConfigurationItem::getChangeType() const
@@ -71,3 +79,22 @@ void ConfigurationItem::setChangeType(ChangeType type)
 {
        changeType = type;
 }
+
+void ConfigurationItem::construct(int argc, char **argv, char **azColName)
+{
+       dlog_print(DLOG_INFO, "universal_switch", ">>> buildItemCb <<<");
+
+       if (argc < 5)
+               return;
+
+       dlog_print(DLOG_INFO, "universal_switch", "buildItemCb: %s, %s", argv[0], argv[2]);
+
+       setKey(argv[0]);
+       setGroup(argv[1]);
+       setName(argv[2]);
+       setDescription(argv[3]);
+       setValue(argv[4]);
+
+       dlog_print(DLOG_INFO, "universal_switch", "buildItemCb: %s, %s", getKey().c_str(), getValue().c_str());
+       dlog_print(DLOG_INFO, "universal_switch", ">>> buildItemCb done <<<");
+}
index 42ed406..0cb3190 100644 (file)
@@ -67,7 +67,7 @@ void SQLiteConfiguration::update(std::shared_ptr<ConfigurationItem> const &item)
 
 void SQLiteConfiguration::remove(const std::string &key, const std::string &group)
 {
-       auto item = buildItem(key, group);
+       auto item = find(key, group);
 
        if (!item) {
                INFO("empty item");
@@ -84,16 +84,49 @@ void SQLiteConfiguration::remove(const std::string &key, const std::string &grou
                notify(item);
 }
 
-std::vector<std::shared_ptr<ConfigurationItem>> SQLiteConfiguration::find(std::shared_ptr<Condition<ConfigurationItem>> condition) const
+std::shared_ptr<ConfigurationItem> SQLiteConfiguration::find(const std::string &name, const std::string &group) const
 {
-//TODO
+       QueryBuilder builder;
+       auto query = builder.select("*").from(tableName).where(discoveryCol, "=", name).qAnd(groupCol, "=", group).build();
+       dlog_print(DLOG_INFO, "universal_switch", "find: %s", query.c_str());
+
+       auto item = std::make_shared<ConfigurationItem>();
+
+       if (!executeQuery(query, [=](int argc, char **argv, char **azColName) { item->construct(argc, argv, azColName); }))
+               return nullptr;
+
+       dlog_print(DLOG_INFO, "universal_switch", "find: %s, %s", item->getKey().c_str(), item->getValue().c_str());
+       return item;
+}
+
+std::vector<std::shared_ptr<ConfigurationItem>> SQLiteConfiguration::findAll() const {
+       QueryBuilder builder;
+       auto query = builder.select("*").from(tableName).build();
+
+       auto collection = std::vector<std::shared_ptr<ConfigurationItem>>();
+
+       if (!executeQuery(query, [&](int argc, char **argv, char **azColName) {
+                                    auto item = std::make_shared<ConfigurationItem>();
+                                    item->construct(argc, argv, azColName);
+                                    collection.emplace_back(item); }))
+               return {};
+
+       return collection;
+
+}
+
+static int call_functor(void *data, int argc, char **argv, char **colName)
+{
+       auto &callback = *(const SQLiteConfiguration::execCallback_t*)data;
+       callback(argc, argv, colName);
+       return 0;
 }
 
-bool SQLiteConfiguration::executeQuery(const std::string &query, execCallback_t cb, void *data)
+bool SQLiteConfiguration::executeQuery(const std::string &query, const execCallback_t &cb) const
 {
        INFO("executeQuery: %s", query.c_str());
        char *errorMessage = nullptr;
-       auto result = sqlite3_exec(dbHandler, query.c_str(), cb, data, &errorMessage);
+       auto result = sqlite3_exec(dbHandler, query.c_str(), call_functor, const_cast<execCallback_t*>(&cb), &errorMessage);
        if (result != SQLITE_OK) {
                ERROR("sqlite ERROR: %s", errorMessage);
                sqlite3_free(errorMessage);
@@ -102,9 +135,22 @@ bool SQLiteConfiguration::executeQuery(const std::string &query, execCallback_t
        return true;
 }
 
-bool SQLiteConfiguration::executeQuery(const std::string &query)
+void SQLiteConfiguration::printRecords() const
 {
-       return executeQuery(query, nullptr, nullptr);
+       QueryBuilder builder;
+       auto query = builder.select("*").from(tableName).build();
+
+       executeQuery(query, [](int argc, char **argv, char **colName) {
+               dlog_print(DLOG_INFO, "universal_switch", "Table");
+
+               for (int i = 0; i < argc; i++)
+                       dlog_print(DLOG_INFO, "universal_switch", "%s = %s | ", colName[i], argv[i] ? argv[i] : "NULL");
+       });
+}
+
+bool SQLiteConfiguration::executeQuery(const std::string &query) const
+{
+       return executeQuery(query, nullptr);
 }
 
 void SQLiteConfiguration::createTable()
@@ -120,22 +166,6 @@ void SQLiteConfiguration::createTable()
        executeQuery(query);
 }
 
-std::shared_ptr<ConfigurationItem> SQLiteConfiguration::buildItem(const std::string &key, const std::string &group)
-{
-       QueryBuilder builder;
-       auto query = builder.select("*").from(tableName).where(discoveryCol, "=", key).qAnd(groupCol, "=", group).build();
-       INFO("buildItem: %s", query.c_str());
-
-       auto item = std::make_shared<ConfigurationItem>(ChangeType::REMOVE);
-
-       if (!executeQuery(query, buildItemCb, item.get())) {
-               return nullptr;
-       }
-
-       INFO("buildItem: %s, %s", item->getKey().c_str(), item->getValue().c_str());
-       return item;
-}
-
 void SQLiteConfiguration::clearTable()
 {
        auto query = "DELETE FROM " + tableName ;
@@ -143,42 +173,10 @@ void SQLiteConfiguration::clearTable()
        executeQuery(query);
 }
 
-void SQLiteConfiguration::printRecords() const
-{
-       QueryBuilder builder;
-       auto query = builder.select("*").from(tableName).build();
-       INFO("ShowRecords: %s", query.c_str());
-       char *errorMessage = nullptr;
-       auto result = sqlite3_exec(dbHandler, query.c_str(), debugPrintRecordsCb, nullptr, &errorMessage);
-       if (result != SQLITE_OK) {
-               ERROR("%s", errorMessage);
-               sqlite3_free(errorMessage);
-       }
-}
-
-int SQLiteConfiguration::debugPrintRecordsCb(void *data, int argc, char **argv, char **colName)
-{
-       INFO("Table");
-
-       for (int i = 0; i < argc; i++)
-               INFO("%s = %s | ", colName[i], argv[i] ? argv[i] : "NULL");
-
-       return 0;
-}
-
-int SQLiteConfiguration::buildItemCb(void *data, int argc, char **argv, char **azColName)
-{
-       INFO("buildItemCb: %s, %s", argv[0], argv[4]);
-
-       auto item = reinterpret_cast<ConfigurationItem *>(data);
-       item->setKey(argv[0]);
-       item->setValue(argv[4]);
-
-       INFO("buildItemCb: %s, %s", item->getKey().c_str(), item->getValue().c_str());
-
-       return 0;
-}
-
 void SQLiteConfiguration::update_callback(void *data, int operation, char const *database, char const *table, sqlite_int64 rowid)
 {
+       INFO(">>> update callback <<<");
+       INFO("insert: %d, delete: %d, update: %d, current: %d", SQLITE_INSERT, SQLITE_DELETE, SQLITE_UPDATE, operation);
+       INFO("operation: %d, db: %s, table: %s, %ld", operation, database, table, rowid);
+       INFO(">>> update callback done <<<");
 }
index 3c4ab42..dc8db16 100644 (file)
@@ -1,22 +1,33 @@
 #include "Switch.hpp"
 
-Switch::Switch(std::string key, std::shared_ptr<const SwitchGroup> group, std::shared_ptr<const SwitchProvider> provider)
-       : key(std::move(key)), group(std::move(group)), provider(std::move(provider))
+Switch::Switch(std::string id, std::weak_ptr<const SwitchProvider> provider)
+       : id(std::move(id)), provider(std::move(provider))
 {}
 
-std::shared_ptr<const SwitchGroup> Switch::getGroup() const
+std::weak_ptr<const SwitchProvider> Switch::getProvider() const
 {
-       return group;
+       return provider;
 }
 
-std::shared_ptr<const SwitchProvider> Switch::getProvider() const
+bool Switch::operator==(const Switch &sw) const
 {
-       return provider;
+       return id == sw.getId() && name == getName() && description == getDescription();
+}
+
+std::string Switch::getId() const
+{
+       auto p = provider.lock();
+       return p->getId() + "_" + id;
+}
+
+std::string Switch::getName() const
+{
+       return name;
 }
 
-std::string Switch::getKey() const
+std::string Switch::getDescription() const
 {
-       return key;
+       return description;
 }
 
 void Switch::setName(const std::string &name)
@@ -24,7 +35,7 @@ void Switch::setName(const std::string &name)
        this->name = name;
 }
 
-void Switch::setDesc(const std::string &desc)
+void Switch::setDescription(const std::string &desc)
 {
        this->description = desc;
 }
diff --git a/src/SwitchGroup.cpp b/src/SwitchGroup.cpp
deleted file mode 100644 (file)
index 9fafcbc..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-#include "SwitchGroup.hpp"
-
-SwitchGroup::SwitchGroup(std::string key)
-       : key(std::move(key))
-{}
-
-void SwitchGroup::addSwitch(std::shared_ptr<Switch> sw)
-{
-       switches.push_back(std::move(sw));
-}
-
-std::string SwitchGroup::getKey()
-{
-       return key;
-}
-
-std::vector<std::shared_ptr<Switch>> SwitchGroup::getSwitches()
-{
-       return switches;
-}
diff --git a/src/SwitchManager.cpp b/src/SwitchManager.cpp
new file mode 100644 (file)
index 0000000..3bae117
--- /dev/null
@@ -0,0 +1,30 @@
+#include "SwitchManager.hpp"
+
+#include "ConditionAll.hpp"
+#include "ConditionEqual.hpp"
+#include "AdHocActivityDispatcher.hpp"
+
+SwitchManager::SwitchManager(std::shared_ptr<SwitchProvider> provider,
+                                        std::shared_ptr<Configuration> configuration,
+                                        std::shared_ptr<ActivityBuilderFactory> factory)
+       : switchProvider(std::move(provider)),
+         configuration(std::move(configuration)),
+         activityBuilderFactory(std::move(factory))
+{
+       asyncActivityConstrutor = std::make_shared<AsyncActivityConstructor>();
+       activityDispatcher = std::make_shared<AdHocActivityDispatcher>();
+
+       auto cond = std::unique_ptr<Condition<ConfigurationItem>>(new ConditionAll<ConfigurationItem>);
+       configuration->attach(shared_from_this(), std::move(cond));
+
+       auto confItems = configuration->findAll();
+
+       for (auto &i : confItems) {
+               auto sw = provider->findSwitchById(i->getKey());
+               if (!sw)
+                       continue;
+
+               auto cond = std::unique_ptr<Condition<Switch>>(new ConditionEqual<Switch>(sw));
+               provider->attach(shared_from_this(), std::move(cond));
+       }
+}
index 3a3493d..a4bfd1b 100644 (file)
@@ -1,51 +1,10 @@
 #include <SwitchProvider.hpp>
 
-SwitchProvider::GroupCollection SwitchProvider::findAllGroups()
-{
-       return groups;
-}
-
-std::shared_ptr<const SwitchGroup> SwitchProvider::findGroupByName(const std::string &name)
-{
-       for (auto &group : groups)
-               if (group->getKey() == name)
-                       return group;
-
-       return nullptr;
-}
-
-SwitchProvider::SwitchCollection SwitchProvider::findAllSwitches()
-{
-       SwitchCollection switches;
-
-       for (auto &group : groups) {
-               auto groupSwitches = group->getSwitches();
-               switches.insert(std::end(switches), std::begin(groupSwitches), std::end(groupSwitches));
-       }
+SwitchProvider::SwitchProvider(std::string id)
+       : id(std::move(id))
+{}
 
-       return switches;
-}
-
-SwitchProvider::SwitchCollection SwitchProvider::findSwitchesByGroup(const std::string &groupName)
+std::string SwitchProvider::getId() const
 {
-       for (auto &group : groups)
-               if (group->getKey() == groupName)
-                       return group->getSwitches();
-
-       return SwitchCollection();
-}
-
-std::shared_ptr<const Switch> SwitchProvider::findSwitchByGroupAndName(const std::string &groupName, const std::string &switchName)
-{
-       for (auto &group : groups) {
-               if (group->getKey() != groupName)
-                       continue;
-
-               for (auto &sw : group.get()->getSwitches())
-                       if (sw->getKey() == switchName)
-                               return sw;
-
-       }
-
-       return nullptr;
+       return id;
 }
index d8570b4..10b1a6c 100644 (file)
@@ -1,9 +1,10 @@
+#include "universalswitch.hpp"
+#include "SQLiteConfiguration.hpp"
+
 #include <tizen.h>
 #include <service_app.h>
-#include <SQLiteConfiguration.hpp>
-#include <universalswitch.hpp>
+
 #include <iostream>
-#include "SQLiteConfiguration.hpp"
 
 bool service_app_create(void *data)
 {