Implement theme loader APIs
authorJunghyun Yeon <jungh.yeon@samsung.com>
Fri, 15 May 2020 07:31:33 +0000 (16:31 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Mon, 25 May 2020 04:28:15 +0000 (13:28 +0900)
Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
src/theme/api/theme_error.h [new file with mode: 0644]
src/theme/api/theme_loader.cc

diff --git a/src/theme/api/theme_error.h b/src/theme/api/theme_error.h
new file mode 100644 (file)
index 0000000..db10582
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#ifndef __TIZEN_APPFW_THEME_MANAGER_ERROR_H__
+#define __TIZEN_APPFW_THEME_MANAGER_ERROR_H__
+
+
+#include <tizen.h>
+
+
+/**
+ * @file theme_error.h
+ */
+
+
+/**
+ * @addtogroup THEME_MANAGER_MODULE
+ * @{
+ */
+
+
+/**
+ * @brief Enumeration for theme manager errors.
+ * @since_tizen 6.0
+ */
+typedef enum _theme_manager_error {
+       THEME_MANAGER_ERROR_NONE = TIZEN_ERROR_NONE, /**< Success */
+       THEME_MANAGER_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */
+       THEME_MANAGER_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */
+       THEME_MANAGER_IO_ERROR = TIZEN_ERROR_IO_ERROR, /**< I/O error */
+       THEME_MANAGER_PERMISSION_DENIED = TIZEN_ERROR_PERMISSION_DENIED, /**< Permission denied */
+} theme_manager_error_e;
+
+
+/**
+ * @}
+ */
+
+
+#endif /* __TIZEN_APPFW_THEME_MANAGER_ERROR_H__ */
+
index 0029fe4..47d0cb4 100644 (file)
 
 #include "theme/api/theme_loader.h"
 
-int theme_loader_create(theme_loader_h *handle) {
-  return 0;
-}
+#include "theme/api/theme_error.h"
+
+#include "theme/loader/theme_event.h"
+#include "theme/loader/theme_info.h"
+#include "theme/loader/theme_info_loader.h"
+
+#include "theme/utils/logging.h"
+
+#include <memory>
+#include <string>
+
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG "THEME"
+
+#ifdef EXPORT_API
+#undef EXPORT_API
+#endif
+#define EXPORT_API __attribute__((visibility("default")))
+
+namespace {
+
+class Handle {
+ public:
+
+  bool SetThemeInfo(std::shared_ptr<ttm::loader::ThemeInfo> theme_info) {
+    if (theme_info == nullptr)
+      return false;
+    theme_info_ = theme_info;
+    return true;
+  }
+
+  bool SetThemeInfoLoader(std::shared_ptr<ttm::loader::ThemeInfoLoader> loader) {
+    if (loader == nullptr)
+      return false;
+    theme_info_loader_ = loader;
+    return true;
+  }
+
+  std::shared_ptr<ttm::loader::ThemeInfoLoader> GetThemeInfoLoader() {
+    return theme_info_loader_;
+  }
 
-int theme_loader_destroy(theme_loader_h handle) {
-  return 0;
+  std::shared_ptr<ttm::loader::ThemeInfo> GetThemeInfo() {
+    return theme_info_;
+  }
+
+ private:
+  std::shared_ptr<ttm::loader::ThemeInfo> theme_info_;
+  std::shared_ptr<ttm::loader::ThemeInfoLoader> theme_info_loader_;
+};
+
+class ThemeEventHandler : public ttm::loader::IThemeEvent {
+ public:
+  explicit ThemeEventHandler(theme_loader_loaded_cb loaded_cb, theme_loader_unloaded_cb unloaded_cb, void* user_data)
+    : loaded_cb_(loaded_cb), unloaded_cb_(unloaded_cb), userdata_(user_data) {}
+  ~ThemeEventHandler(){}
+  void OnThemeLoaded(const ttm::loader::ThemeInfo,
+      const tizen_base::Bundle& args) {
+
+    // TODO: how to convert args into theme_h handle
+
+    // TODO: invoke loaded_cb_ with userdata
+  }
+
+  void OnTheneUnloaded(const std::string& id) {
+    unloaded_cb_(id.c_str(), userdata_);
+  }
+
+ private:
+  theme_loader_loaded_cb loaded_cb_;
+  theme_loader_unloaded_cb unloaded_cb_;
+  void* userdata_;
+};
+
+}  // namespace
+
+extern "C" EXPORT_API int theme_loader_create(theme_loader_h *handle) {
+  if (handle == nullptr) {
+    LOG(ERROR) << "Invalid parameter";
+    return THEME_MANAGER_INVALID_PARAMETER;
+  }
+
+  Handle* p;
+  p = new (std::nothrow) Handle;
+  if (p == nullptr) {
+    LOG(ERROR) << "Out of memory";
+    return THEME_MANAGER_OUT_OF_MEMORY;
+  }
+  std::shared_ptr<ttm::loader::ThemeInfoLoader> theme_info_loader(nullptr);
+  p->SetThemeInfoLoader(theme_info_loader);
+  *handle = static_cast<theme_loader_h>(p);
+
+  return THEME_MANAGER_ERROR_NONE;
 }
 
-int theme_loader_add_event(theme_loader_h handle,
-    theme_loader_loaded_cb loaded, theme_loader_unloaded_cb unloaded,
-    void *data, theme_loader_event_h* event_h) {
-  return 0;
+extern "C" EXPORT_API int theme_loader_destroy(theme_loader_h handle) {
+  if (handle == nullptr) {
+    LOG(ERROR) << "Invalid parameter";
+    return THEME_MANAGER_INVALID_PARAMETER;
+  }
+
+  Handle* p = static_cast<Handle*>(handle);
+  delete p;
+
+  return THEME_MANAGER_ERROR_NONE;
 }
 
-int theme_loader_remove_event(theme_loader_h handle,
-    theme_loader_event_h event_handle) {
-  return 0;
+extern "C" EXPORT_API int theme_loader_load_current(theme_loader_h handle) {
+  if (handle == nullptr) {
+    LOG(ERROR) << "Invalid parameter";
+    return THEME_MANAGER_INVALID_PARAMETER;
+  }
+
+  Handle* p = static_cast<Handle*>(handle);
+  std::shared_ptr<ttm::loader::ThemeInfoLoader> loader = p->GetThemeInfoLoader();
+  std::shared_ptr<ttm::loader::ThemeInfo> theme_info = loader->LoadCurrent();
+  p->SetThemeInfo(theme_info);
+
+  return THEME_MANAGER_ERROR_NONE;
+;
 }
 
-int theme_loader_load_current(theme_loader_h handle) {
-  return 0;
+extern "C" EXPORT_API int theme_loader_load(theme_loader_h handle, const char *id) {
+  if (handle == nullptr || id == nullptr) {
+    LOG(ERROR) << "Invalid parameter";
+    return THEME_MANAGER_INVALID_PARAMETER;
+  }
+
+  Handle* p = static_cast<Handle*>(handle);
+  std::shared_ptr<ttm::loader::ThemeInfoLoader> loader = p->GetThemeInfoLoader();
+  std::shared_ptr<ttm::loader::ThemeInfo> theme_info = loader->Load(std::string(id));
+  p->SetThemeInfo(theme_info);
+
+  return THEME_MANAGER_ERROR_NONE;
 }
 
-int theme_loader_load(theme_loader_h handle, const char *id) {
-  return 0;
+extern "C" EXPORT_API int theme_loader_query_id(theme_loader_h handle, char ***ids, int *count) {
+  if (handle == nullptr) {
+    LOG(ERROR) << "Invalid parameter";
+    return THEME_MANAGER_INVALID_PARAMETER;
+  }
+
+  Handle* p = static_cast<Handle*>(handle);
+  std::shared_ptr<ttm::loader::ThemeInfoLoader> loader = p->GetThemeInfoLoader();
+  std::vector<std::string> id_list = loader->QueryThemeId();
+
+  *ids = static_cast<char **>(calloc(id_list.size(), sizeof(**ids)));
+  if (*ids == nullptr) {
+    LOG(ERROR) << "Out of memory";
+    return THEME_MANAGER_OUT_OF_MEMORY;
+  }
+
+  int idx = 0;
+  for (auto& id : id_list) {
+    char *id_char = strdup(id.c_str());
+    if (id_char == nullptr) {
+      LOG(ERROR) << "Out of memory";
+      return THEME_MANAGER_OUT_OF_MEMORY;
+    }
+
+    *ids[idx] = id_char;
+    idx++;
+  }
+  *count = idx;
+
+  return THEME_MANAGER_ERROR_NONE;
 }
 
-int theme_loader_query_id(theme_loader_h handle, char ***ids, int *count) {
-  return 0;
+extern "C" EXPORT_API int theme_loader_add_event(theme_loader_h handle,
+    theme_loader_loaded_cb loaded, theme_loader_unloaded_cb unloaded,
+    void *data, theme_loader_event_h* event_h) {
+  if (handle == nullptr || loaded == nullptr ||
+      unloaded == nullptr || event_h == nullptr) {
+    LOG(ERROR) << "Invalid parameter";
+    return THEME_MANAGER_INVALID_PARAMETER;
+  }
+
+  std::shared_ptr<ttm::loader::IThemeEvent>* event_handler =
+      new (std::nothrow) std::shared_ptr<ttm::loader::IThemeEvent>(
+            new (std::nothrow) ThemeEventHandler(loaded, unloaded, data));
+  if (event_handler == nullptr) {
+    LOG(ERROR) << "Out of memory";
+    return THEME_MANAGER_OUT_OF_MEMORY;
+  }
+
+  Handle* p = static_cast<Handle*>(handle);
+  std::shared_ptr<ttm::loader::ThemeInfoLoader> loader = p->GetThemeInfoLoader();
+  auto result = loader->AddEvent(*event_handler);
+  // TODO: what should we do with returned std::string result?
+
+  *event_h = static_cast<theme_loader_event_h>(event_handler);
+
+  return THEME_MANAGER_ERROR_NONE;
 }
+
+extern "C" EXPORT_API int theme_loader_remove_event(theme_loader_h handle,
+    theme_loader_event_h event_handle) {
+  if (handle == nullptr) {
+    LOG(ERROR) << "Invalid parameter";
+    return THEME_MANAGER_INVALID_PARAMETER;
+  }
+
+  return THEME_MANAGER_ERROR_NONE;
+}
\ No newline at end of file