Refactor theme plugin 08/236308/2
authorSangyoon Jang <jeremy.jang@samsung.com>
Tue, 16 Jun 2020 06:44:45 +0000 (15:44 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Tue, 16 Jun 2020 09:40:16 +0000 (18:40 +0900)
Remove duplicated code.

Change-Id: I8c2dc9464f27d5adeda9aa6d3c28d3e8ed84b94e
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/theme_plugin/theme_metadata_plugin.cc

index d004781..c97a52c 100644 (file)
 
 #include "theme/utils/glist_range.h"
 #include "theme/utils/logging.h"
+#include "theme/loader/theme_info.h"
 #include "theme_plugin/theme_parser.h"
 
+using ttm::loader::ThemeInfo;
+using ttm::plugin::ThemeParser;
+
 namespace {
 
 const char kThemeMetadataKey[] = "http://tizen.org/metadata/theme";
@@ -40,27 +44,27 @@ std::string GetThemeFilePath(const char* pkgid) {
   return path;
 }
 
-}  // namespace
-
-bool InstallTheme(const std::string& id, const std::string& pkgid,
-    const std::string& path) {
+bool ParseAndCommitTheme(ThemeParser::ThemeOperation op, const std::string& id,
+    const std::string& pkgid, const std::string& path) {
   uid_t uid;
   int r = pkgmgr_installer_info_get_target_uid(&uid);
   if (r < 0) {
     LOG(ERROR) << "Failed to get target uid";
     return false;
   }
-  ttm::plugin::ThemeParser parser(path);
-  ttm::loader::ThemeInfo theme = parser.Inflate(id, pkgid, uid);
+  ThemeParser parser(path);
+  ThemeInfo theme = parser.Inflate(id, pkgid, uid);
   if (!theme.IsValid()) {
     LOG(ERROR) << "Failed to parse theme";
     return false;
   }
-  if (!parser.Commit(ttm::plugin::ThemeParser::ThemeOperation::ADD, theme))
+  if (!parser.Commit(op, theme))
     return false;
   return true;
 }
 
+}  // namespace
+
 extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(
     const char* pkgid, const char* appid, GList* metadata) {
   std::string root_path = GetThemeFilePath(pkgid);
@@ -69,32 +73,14 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(
 
   for (__metadata_t* md : GListRange<__metadata_t*>(metadata)) {
     if (!strcmp(md->key, kThemeMetadataKey)) {
-      if (!InstallTheme(appid, pkgid, (root_path + md->value)))
+      if (!ParseAndCommitTheme(ThemeParser::ThemeOperation::ADD, appid, pkgid,
+          (root_path + md->value)))
         return -1;
     }
   }
   return 0;
 }
 
-bool UpgradeTheme(const std::string& id, const std::string& pkgid,
-    const std::string& path) {
-  uid_t uid;
-  int r = pkgmgr_installer_info_get_target_uid(&uid);
-  if (r < 0) {
-    LOG(ERROR) << "Failed to get target uid";
-    return false;
-  }
-  ttm::plugin::ThemeParser parser(path);
-    ttm::loader::ThemeInfo theme = parser.Inflate(id, pkgid, uid);
-  if (!theme.IsValid()) {
-    LOG(ERROR) << "Failed to parse theme";
-    return false;
-  }
-  if (!parser.Commit(ttm::plugin::ThemeParser::ThemeOperation::UPDATE, theme))
-    return false;
-  return true;
-}
-
 extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(
     const char* pkgid, const char* appid, GList* metadata) {
   std::string root_path = GetThemeFilePath(pkgid);
@@ -103,32 +89,14 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(
 
   for (__metadata_t* md : GListRange<__metadata_t*>(metadata)) {
     if (!strcmp(md->key, kThemeMetadataKey)) {
-      if (!UpgradeTheme(appid, pkgid, (root_path + md->value)))
+      if (!ParseAndCommitTheme(ThemeParser::ThemeOperation::UPDATE, appid,
+          pkgid, (root_path + md->value)))
         return -1;
     }
   }
   return 0;
 }
 
-bool UninstallTheme(const std::string& id, const std::string& pkgid,
-    const std::string& path) {
-  uid_t uid;
-  int r = pkgmgr_installer_info_get_target_uid(&uid);
-  if (r < 0) {
-    LOG(ERROR) << "Failed to get target uid";
-    return false;
-  }
-  ttm::plugin::ThemeParser parser(path);
-  ttm::loader::ThemeInfo theme = parser.Inflate(id, pkgid, uid);
-  if (!theme.IsValid()) {
-    LOG(ERROR) << "Failed to parse theme";
-    return false;
-  }
-  if (!parser.Commit(ttm::plugin::ThemeParser::ThemeOperation::REMOVE, theme))
-    return false;
-  return true;
-}
-
 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(
     const char* pkgid, const char* appid, GList* metadata) {
   std::string root_path = GetThemeFilePath(pkgid);
@@ -137,7 +105,8 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(
 
   for (__metadata_t* md : GListRange<__metadata_t*>(metadata)) {
     if (!strcmp(md->key, kThemeMetadataKey)) {
-      if (!UninstallTheme(appid, pkgid, (root_path + md->value)))
+      if (!ParseAndCommitTheme(ThemeParser::ThemeOperation::REMOVE, appid,
+          pkgid, (root_path + md->value)))
         return -1;
     }
   }