#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
#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
--- /dev/null
+#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
#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
class ActivityObserver : public Observer<Activity>
{
- virtual ~ActivityObserver() {};
-
- virtual void update(std::shared_ptr<Activity> activity);
};
#endif
-#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;
};
#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
#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
--- /dev/null
+#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
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;
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
--- /dev/null
+#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
--- /dev/null
+#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
--- /dev/null
+#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
#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
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;
--- /dev/null
+#ifndef CONFIGURATION_ITEM_OBSERVER_HPP
+#define CONFIGURATION_ITEM_OBSERVER_HPP
+
+#include "Observer.hpp"
+#include "ConfigurationItem.hpp"
+
+class ConfigurationItemObserver : public Observer<ConfigurationItem>
+{
+
+};
+
+#endif
#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;
#ifndef SUBJECT_HPP_
#define SUBJECT_HPP_
-#include <Condition.hpp>
+#include "Condition.hpp"
#include "ConfigurationItem.hpp"
#include "Observer.hpp"
#include <vector>
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;
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
#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
+++ /dev/null
-#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
--- /dev/null
+#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
#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
-#ifndef __universalswitch_H__
-#define __universalswitch_H__
+#ifndef UNIVERSAL_SWITCH_HPP_
+#define UNIVERSAL_SWITCH_HPP_
#include <libintl.h>
#include <dlog.h>
#define _(str) (gettext(str))
-#endif /* __universalswitch_H__ */
+#endif /* UNIVERSAL_SWITCH_HPP_ */
#include "AccessoriesSwitchProvider.hpp"
AccessoriesSwitchProvider::AccessoriesSwitchProvider()
- : keys(
+ : SwitchProvider("AccessoriesSwitchProvider"), keys(
{"XF86AddFavorite",
"XF86ApplicationLeft",
"XF86ApplicationRight",
"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;
}
-#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",
--- /dev/null
+#include "AdHocActivityDispatcher.hpp"
+
+void AdHocActivityDispatcher::dispatch(std::shared_ptr<Activity> activity) {
+ if (activity)
+ activity->execute();
+}
-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})
--- /dev/null
+#include "CachedActivityBuilderFactory.hpp"
+
+std::shared_ptr<ActivityBuilder> CachedActivityBuilderFactory::getActivityBuilder(const std::string &activityType) {
+}
#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()
{
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;
}
{
providers.push_back(provider);
}
+
+std::vector<std::shared_ptr<SwitchProvider>> CompositeSwitchProvider::getProviders()
+{
+ return providers;
+}
#include "ConfigurationItem.hpp"
+#include <dlog.h>
+
ConfigurationItem::ConfigurationItem(ChangeType type)
: changeType(type)
{}
: 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
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
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
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
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
{
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 <<<");
+}
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");
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);
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()
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 ;
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 <<<");
}
#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)
this->name = name;
}
-void Switch::setDesc(const std::string &desc)
+void Switch::setDescription(const std::string &desc)
{
this->description = desc;
}
+++ /dev/null
-#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;
-}
--- /dev/null
+#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));
+ }
+}
#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;
}
+#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)
{