Implements theme_info_loader
authorInkyun Kil <inkyun.kil@samsung.com>
Tue, 19 May 2020 02:28:43 +0000 (11:28 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Mon, 25 May 2020 04:28:15 +0000 (13:28 +0900)
Signed-off-by: Inkyun Kil <inkyun.kil@samsung.com>
src/theme/api/theme_loader.cc
src/theme/dbus/request_broker.cc
src/theme/loader/theme_info_loader.cc
src/theme/loader/theme_info_loader.h

index ccd898b..032d778 100644 (file)
@@ -71,7 +71,7 @@ extern "C" EXPORT_API int theme_loader_create(theme_loader_h *handle) {
     return THEME_MANAGER_INVALID_PARAMETER;
   }
 
-  auto* p = new (std::nothrow) ThemeInfoLoader(nullptr);
+  auto* p = new (std::nothrow) ThemeInfoLoader();
   if (p == nullptr) {
     LOG(ERROR) << "Out of memory";
     return THEME_MANAGER_OUT_OF_MEMORY;
index c5e8d5a..228a017 100644 (file)
@@ -23,6 +23,7 @@
 #define THEME_MANAGER_OBJECT_PATH "/org/tizen/theme_manager"
 #define THEME_MANAGER_INTERFACE_NAME "org.tizen.theme_manager"
 
+
 namespace ttm {
 namespace dbus {
 
index 278880c..995c8b1 100644 (file)
 
 #include <string>
 
+#include "theme/loader/theme_info.h"
 #include "theme/loader/theme_info_loader.h"
+#include "theme/dbus/request_broker.h"
+
+#include "theme/utils/logging.h"
 
 namespace ttm {
 namespace loader {
 
 tizen_base::Bundle ThemeInfoLoader::OnRequest(dbus::Command cmd,
     const tizen_base::Bundle& args) {
-  return {};
+  switch (cmd) {
+    case dbus::Command::GET_IDS:
+    case dbus::Command::SET_ID:
+    case dbus::Command::GET:
+    case dbus::Command::RESULT:
+    case dbus::Command::ADD:
+    case dbus::Command::UPDATE:
+    case dbus::Command::REMOVE:
+    case dbus::Command::CHANGED:
+    case dbus::Command::REMOVED:
+      break;
+    default:
+      LOG(ERROR) << "Invalid command type";
+      break;
+  }
+
+  tizen_base::Bundle b;
+  return b;
 }
 
 std::string ThemeInfoLoader::AddEvent(std::shared_ptr<IThemeEvent> ev) {
-  return "";
+  std::size_t hash = std::hash<std::shared_ptr<IThemeEvent>>{}(ev);
+  std::string key(std::to_string(hash));
+  listeners_.insert(
+    std::pair<std::string, std::shared_ptr<IThemeEvent>>(key, ev));
+
+  return key;
 }
 
 int ThemeInfoLoader::RemoveEvent(const std::string& id) {
+  std::map<std::string, std::shared_ptr<IThemeEvent>>::iterator it;
+
+  it = listeners_.find(id);
+  if (it == listeners_.end()) {
+    LOG(ERROR) << "Invalid id : " << id;
+    return -1;
+  }
+
+  listeners_.erase(it);
   return 0;
 }
 
 std::vector<std::string> ThemeInfoLoader::QueryThemeId() {
-  return {};
+  tizen_base::Bundle b;
+  std::string val("get_ids");
+  b.Add("__QUERY__", val.c_str());
+
+  tizen_base::Bundle reply =
+    dbus::RequestBroker::GetInst().SendData(dbus::Command::GET_IDS, b);
+
+  return reply.GetStringArray(val);
 }
 
 void ThemeInfoLoader::SetCurrent(const std::string& id) {
+  tizen_base::Bundle b;
+  b.Add("__SET_CURRENT__", id);
+
+  tizen_base::Bundle reply =
+    dbus::RequestBroker::GetInst().SendData(dbus::Command::SET_ID, b);
 }
 
-std::shared_ptr<ThemeInfo> ThemeInfoLoader::LoadCurrent() const {
-  return nullptr;
+std::shared_ptr<ThemeInfo> ThemeInfoLoader::LoadCurrent() {
+  if (cache_)
+    return cache_;
+
+  tizen_base::Bundle b;
+  b.Add("__LOAD__", "__CURRENT__");
+
+  tizen_base::Bundle reply =
+    dbus::RequestBroker::GetInst().SendData(dbus::Command::GET, b);
+
+  cache_ = std::make_shared<ThemeInfo>(reply);
+
+  return cache_;
 }
 
 std::shared_ptr<ThemeInfo> ThemeInfoLoader::Load(const std::string& id) const {
-  return nullptr;
+  if (cache_ && id.compare(cache_->GetId()) == 0)
+    return cache_;
+
+  tizen_base::Bundle b;
+  b.Add("__LOAD__", id);
+
+  tizen_base::Bundle reply =
+    dbus::RequestBroker::GetInst().SendData(dbus::Command::GET, b);
+
+  std::shared_ptr<ThemeInfo> info = std::make_shared<ThemeInfo>(reply);
+
+  return info;
 }
 
 }  // namespace loader
index 8d7ff32..4f70b67 100644 (file)
@@ -22,6 +22,7 @@
 #include <string>
 #include <utility>
 #include <vector>
+#include <map>
 
 #include "theme/dbus/request_handler.h"
 #include "theme/loader/theme_event.h"
@@ -32,8 +33,6 @@ namespace loader {
 
 class ThemeInfoLoader : public dbus::IRequestHandler {
  public:
-  explicit ThemeInfoLoader(std::shared_ptr<ThemeInfo> cache)
-      : IRequestHandler(), cache_(std::move(cache)) { }
   tizen_base::Bundle OnRequest(dbus::Command cmd,
     const tizen_base::Bundle& args) override;
 
@@ -42,12 +41,12 @@ class ThemeInfoLoader : public dbus::IRequestHandler {
 
   std::vector<std::string> QueryThemeId();
   void SetCurrent(const std::string& id);
-  std::shared_ptr<ThemeInfo> LoadCurrent() const;
+  std::shared_ptr<ThemeInfo> LoadCurrent();
   std::shared_ptr<ThemeInfo> Load(const std::string& id) const;
 
  private:
   std::shared_ptr<ThemeInfo> cache_;
-  std::list<std::shared_ptr<IThemeEvent>> listeners_;
+  std::map<std::string, std::shared_ptr<IThemeEvent>> listeners_;
 };
 
 }  // namespace loader