Remove duplicate codes 58/206358/9
authorhyunho <hhstark.kang@samsung.com>
Fri, 17 May 2019 02:18:32 +0000 (11:18 +0900)
committerhyunho <hhstark.kang@samsung.com>
Wed, 22 May 2019 01:14:20 +0000 (10:14 +0900)
Change-Id: Ic98e199a4e2286320c094ed96bc3ea9711c158dd
Signed-off-by: hyunho <hhstark.kang@samsung.com>
watchface-common/watchface-util.cc
watchface-common/watchface-util.h
watchface-complication-provider/complication-provider.cc
watchface-complication-provider/watchface-complication-provider.cc
watchface-complication/complication.cc
watchface-complication/watchface-complication.cc
watchface-complication/watchface-editable.cc
watchface-editor/editables-editor.cc

index b925b87..362f52f 100644 (file)
@@ -28,7 +28,7 @@
 
 #include <string>
 #include <map>
-#include <iostream>
+#include <list>
 
 #include "watchface-common/watchface-util.h"
 #include "watchface-common/include/watchface-common.h"
@@ -122,6 +122,25 @@ namespace util {
     return ret;
   }
 
+  int CheckPrivilege(string privilege, cynara_result* pcr) {
+    int ret = WATCHFACE_COMPLICATION_ERROR_NONE;
+    if (*pcr == COMPLICATION_CYNARA_DENIED) {
+      LOGE("Permission denied");
+      return WATCHFACE_COMPLICATION_ERROR_PERMISSION_DENIED;
+    } else if (*pcr == COMPLICATION_CYNARA_UNKNOWN) {
+      ret = CheckPrivilege(privilege.c_str());
+      if (ret == WATCHFACE_COMPLICATION_ERROR_NONE) {
+        *pcr = COMPLICATION_CYNARA_ALLOWED;
+      } else if (ret == WATCHFACE_COMPLICATION_ERROR_PERMISSION_DENIED) {
+        *pcr = COMPLICATION_CYNARA_DENIED;
+        return ret;
+      } else {
+        return ret;
+      }
+    }
+    return ret;
+  }
+
   int CheckPrivilege(std::string& privilege, GDBusConnection* connection,
                        std::string& sender_name, std::string& sender_appid) {
     int ret;
@@ -453,14 +472,15 @@ out:
   }
 
   int ConvertPathToAppPath(const char* appid, bundle* data) {
-    char* path;
-    char* dup_path;
-    int ret = bundle_get_str(data, ICON_KEY, &path);
-    if (ret == BUNDLE_ERROR_NONE) {
-      dup_path = strdup(path);
-      bundle_del(data, ICON_KEY);
-      ret = bundle_add_str(
-          data, ICON_KEY,
+    list<string> key_list {ICON_KEY, IMAGE_KEY};
+    for(auto& i : key_list) {
+      char* path;
+      int ret = bundle_get_str(data, i.c_str(), &path);
+      if (ret != BUNDLE_ERROR_NONE)
+        continue;
+      char* dup_path = strdup(path);
+      bundle_del(data, i.c_str());
+      ret = bundle_add_str(data, i.c_str(),
           util::GetAppPath(appid, dup_path).c_str());
       free(dup_path);
       if (ret != BUNDLE_ERROR_NONE) {
@@ -468,23 +488,19 @@ out:
         return WATCHFACE_COMPLICATION_ERROR_IO_ERROR;
       }
     }
-
-    ret = bundle_get_str(data, IMAGE_KEY, &path);
-    if (ret == BUNDLE_ERROR_NONE) {
-      dup_path = strdup(path);
-      bundle_del(data, IMAGE_KEY);
-      ret = bundle_add_str(
-          data, IMAGE_KEY,
-          util::GetAppPath(appid, path).c_str());
-      free(dup_path);
-      if (ret != BUNDLE_ERROR_NONE) {
-        LOGE("fail to add app path");
-        return WATCHFACE_COMPLICATION_ERROR_IO_ERROR;
-      }
-    }
-
     return WATCHFACE_COMPLICATION_ERROR_NONE;
   }
 
+  int ConvertAulError(int ret) {
+    if (ret == AUL_R_OK)
+      return WATCHFACE_COMPLICATION_ERROR_NONE;
+    if (ret == AUL_R_EILLACC)
+      return WATCHFACE_COMPLICATION_ERROR_PERMISSION_DENIED;
+    else if (ret == AUL_R_EINVAL)
+      return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
+    else
+      return WATCHFACE_COMPLICATION_ERROR_IO_ERROR;
+  }
+
 }  // namespace util
 }  // namespace watchface_complication
index 630d9de..90eda91 100644 (file)
@@ -55,6 +55,7 @@ namespace util {
 
   EXPORT_API bool CheckWatchFeatureEnabled();
   EXPORT_API int CheckPrivilege(const char* privilege);
+  EXPORT_API int CheckPrivilege(std::string privilege, cynara_result *pcr);
   EXPORT_API int CheckPrivilege(std::string& privilege,
                                      GDBusConnection *connection,
                                      std::string& sender_name,
@@ -75,6 +76,7 @@ namespace util {
   EXPORT_API int ConvertPathToAppPath(const char* appid, bundle* data);
   EXPORT_API bool IsValidData(tizen_base::Bundle shared_data);
   EXPORT_API int GetDataType(tizen_base::Bundle shared_data);
+  EXPORT_API int ConvertAulError(int ret);
 }  // namespace util
 }  // namespace watchface_complication
 
index 90de87b..ae43393 100644 (file)
@@ -324,24 +324,9 @@ int ComplicationProvider::NotifyDataUpdate() {
   LOGI("notify %s", impl_->provider_id_.c_str());
 
   static cynara_result pcr = COMPLICATION_CYNARA_UNKNOWN;
-
-  int ret = WATCHFACE_COMPLICATION_ERROR_NONE;
-  if (pcr == COMPLICATION_CYNARA_DENIED) {
-    LOGE("Permission denied");
-    return WATCHFACE_COMPLICATION_ERROR_PERMISSION_DENIED;
-
-  } else if (pcr == COMPLICATION_CYNARA_UNKNOWN) {
-    ret = util::CheckPrivilege(PRIVILEGE_DATASHARING);
-    if (ret == WATCHFACE_COMPLICATION_ERROR_NONE) {
-      pcr = COMPLICATION_CYNARA_ALLOWED;
-    } else if (ret == WATCHFACE_COMPLICATION_ERROR_PERMISSION_DENIED) {
-      pcr = COMPLICATION_CYNARA_DENIED;
-      return ret;
-    } else {
-      return ret;
-    }
-  }
-
+  int ret = util::CheckPrivilege(PRIVILEGE_DATASHARING, &pcr);
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+    return ret;
   if (!impl_->gdbus_.get()->EmitSignal(IGDBus::Complication, "",
       impl_->provider_id_, -1, util::GetCmdStr(util::CompNotifyDataUpdate),
       g_variant_new("(s)", impl_->provider_id_.c_str())))
index a456983..cffd198 100644 (file)
@@ -215,6 +215,18 @@ extern "C" EXPORT_API int watchface_complication_provider_notify_update(
   return ws->NotifyDataUpdate();
 }
 
+static int __convert_app_control_error(int error) {
+  if (error == APP_CONTROL_ERROR_NONE)
+    return WATCHFACE_COMPLICATION_ERROR_NONE;
+
+  if (error == APP_CONTROL_ERROR_OUT_OF_MEMORY) {
+    LOGE("Out of memory");
+    return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
+  }
+  LOGE("Fail to get data from control (%d)", error);
+  return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
+}
+
 extern "C" EXPORT_API int watchface_complication_provider_setup_reply_to_editor(
     app_control_h handle, bundle* context) {
   if (!watchface_complication::util::CheckWatchFeatureEnabled())
@@ -233,35 +245,28 @@ extern "C" EXPORT_API int watchface_complication_provider_setup_reply_to_editor(
     LOGE("Fail to get editor appid");
     return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
   }
+  string editor_appid_str = editor_appid;
+  free(editor_appid);
 
   ret = app_control_get_extra_data(handle, SETUP_EDITABLE_ID_KEY, &editable_id);
-  if (ret != APP_CONTROL_ERROR_NONE) {
-    free(editor_appid);
-
-    if (ret == APP_CONTROL_ERROR_OUT_OF_MEMORY) {
-      LOGE("Out of memory");
-      return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-    }
-    LOGE("Fail to get setup editable ID (%d)", ret);
-    return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
-  }
+  ret = __convert_app_control_error(ret);
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+    return ret;
+  string editable_id_str = editable_id;
+  free(editable_id);
 
-  if (context != NULL) {
+  if (context != nullptr) {
     ret = bundle_encode(context, &raw_data, &str_len);
     if (ret != 0) {
       LOGE("Fail to encode data");
-      free(editable_id);
-      free(editor_appid);
       return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
     }
   }
 
   emit_signal_ret = ComplicationProvider::SetupReplyToEditor(
-      std::string(editor_appid),
-      strtol(editable_id, NULL, 10),
-      raw_data == NULL ? "" : reinterpret_cast<const char*>(raw_data));
-  free(editor_appid);
-  free(editable_id);
+      editor_appid_str,
+      stoi(editable_id_str),
+      raw_data == nullptr ? "" : reinterpret_cast<const char*>(raw_data));
   free(raw_data);
 
   if (emit_signal_ret == false)
@@ -284,21 +289,16 @@ extern "C" EXPORT_API int watchface_complication_provider_setup_is_editing(
   }
 
   ret = app_control_get_extra_data(handle, SETUP_EDITOR_APPID_KEY, &value);
-  if (ret != APP_CONTROL_ERROR_NONE) {
-    if (ret == APP_CONTROL_ERROR_OUT_OF_MEMORY) {
-      LOGE("Out of memory");
-      return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-    } else if (ret == APP_CONTROL_ERROR_KEY_NOT_FOUND) {
+  if (ret == APP_CONTROL_ERROR_KEY_NOT_FOUND) {
       *is_editing = false;
       return WATCHFACE_COMPLICATION_ERROR_NONE;
-    }
-    LOGE("Fail to get setup editor appid (%d)", ret);
-    return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
   }
+  ret = __convert_app_control_error(ret);
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+    return ret;
 
-  *is_editing = true;
   free(value);
-
+  *is_editing = true;
   return WATCHFACE_COMPLICATION_ERROR_NONE;
 }
 
@@ -317,30 +317,19 @@ extern "C" EXPORT_API int watchface_complication_provider_setup_get_context(
   }
 
   ret = app_control_get_extra_data(handle, SETUP_EDITOR_APPID_KEY, &value);
-  if (ret != APP_CONTROL_ERROR_NONE) {
-    if (ret == APP_CONTROL_ERROR_OUT_OF_MEMORY) {
-      LOGE("Out of memory");
-      return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-    }
-    LOGE("Fail to get setup editor appid (%d)", ret);
-    return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
-  }
+  ret = __convert_app_control_error(ret);
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+    return ret;
   free(value);
 
   ret = app_control_get_extra_data(handle, SETUP_CONTEXT_DATA_KEY, &value);
-  if (ret != APP_CONTROL_ERROR_NONE) {
-    if (ret == APP_CONTROL_ERROR_OUT_OF_MEMORY) {
-      LOGE("Out of memory");
-      return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-    } else if (ret == APP_CONTROL_ERROR_KEY_NOT_FOUND) {
-      *context = NULL;
+  if (ret == APP_CONTROL_ERROR_KEY_NOT_FOUND) {
+      *context = nullptr;
       return WATCHFACE_COMPLICATION_ERROR_NONE;
-    } else {
-      LOGE("Fail to get context data (%d)", ret);
-      return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
-    }
   }
-
+  ret = __convert_app_control_error(ret);
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+    return ret;
   data = bundle_decode(reinterpret_cast<const bundle_raw*>(value),
     strlen(value));
   *context = data;
@@ -648,14 +637,9 @@ static int _get_value_from_touch_launch_data(app_control_h handle,
   int ret;
 
   ret = app_control_get_extra_data(handle, TOUCH_LAUNCH_DATA_KEY, &bundle_str);
-  if (ret != APP_CONTROL_ERROR_NONE) {
-    if (ret == APP_CONTROL_ERROR_OUT_OF_MEMORY) {
-      LOGE("Out of memory");
-      return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-    }
-    LOGE("Fail to get data (%d)", ret);
-    return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
-  }
+  ret = __convert_app_control_error(ret);
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+    return ret;
 
   data = bundle_decode(reinterpret_cast<const bundle_raw*>(bundle_str),
     strlen(bundle_str));
index b1c4b14..2753a0f 100644 (file)
@@ -676,15 +676,9 @@ int Complication::SendDataUpdateRequest(bool launch_option) {
               util::GetAppId().c_str(),
               provider_appid.c_str(), getuid());
     LOGI("Launch the provider app: %d, %s", ret, provider_appid.c_str());
-
-    if (ret != AUL_R_OK) {
-      if (ret == AUL_R_EILLACC)
-        return WATCHFACE_COMPLICATION_ERROR_PERMISSION_DENIED;
-      else if (ret == AUL_R_EINVAL)
-        return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
-      else
-        return WATCHFACE_COMPLICATION_ERROR_IO_ERROR;
-    }
+    ret = util::ConvertAulError(ret);
+    if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+      return ret;
   }
 
   if (impl_->context_data_ != nullptr)
@@ -793,16 +787,8 @@ int Complication::TouchLaunch(watchface_complication_event_type_e event_type) {
           reinterpret_cast<char*>(launch_data.ToRaw().first.get()));
 
   LOGI("Touch launch the %s : %d", provider_appid.c_str(), ret);
-  if (ret != AUL_R_OK) {
-    if (ret == AUL_R_EILLACC)
-      return WATCHFACE_COMPLICATION_ERROR_PERMISSION_DENIED;
-    else if (ret == AUL_R_EINVAL)
-      return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
-    else
-      return WATCHFACE_COMPLICATION_ERROR_IO_ERROR;
-  }
 
-  return WATCHFACE_COMPLICATION_ERROR_NONE;
+  return util::ConvertAulError(ret);
 }
 
 bool Complication::Impl::CheckCachedPrivilege(std::string privilege) {
index ca26ae6..7476e45 100644 (file)
@@ -187,23 +187,9 @@ extern "C" EXPORT_API int watchface_complication_add_updated_cb(
     return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
 
   static cynara_result pcr = COMPLICATION_CYNARA_UNKNOWN;
-  int ret = WATCHFACE_COMPLICATION_ERROR_NONE;
-
-  if (pcr == COMPLICATION_CYNARA_DENIED) {
-    LOGE("Permission denied");
-    return WATCHFACE_COMPLICATION_ERROR_PERMISSION_DENIED;
-  } else if (pcr == COMPLICATION_CYNARA_UNKNOWN) {
-    ret = watchface_complication::util::CheckPrivilege(PRIVILEGE_DATASHARING);
-    if (ret == WATCHFACE_COMPLICATION_ERROR_NONE) {
-      pcr = COMPLICATION_CYNARA_ALLOWED;
-    } else if (ret == WATCHFACE_COMPLICATION_ERROR_PERMISSION_DENIED) {
-      pcr = COMPLICATION_CYNARA_DENIED;
-      return ret;
-    } else {
-      return ret;
-    }
-  }
-
+  int ret = util::CheckPrivilege(PRIVILEGE_DATASHARING, &pcr);
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+    return ret;
   auto sh = static_cast<SharedHandle<WatchComplicationStub>*>(handle);
   auto ptr = SharedHandle<WatchComplicationStub>::Share(sh);
   unique_ptr<CallbackInfo> ci = unique_ptr<CallbackInfo>(
index d9addbc..1d3bb79 100644 (file)
 
 #define LOG_TAG "WATCHFACE_COMPLICATION"
 
-using watchface_complication::EditablesContainer;
-using watchface_complication::IEditable;
-using watchface_complication::DesignElement;
-using watchface_complication::SharedHandle;
-using watchface_complication::EditablesManager;
 using namespace std;
 using namespace tizen_base;
+using namespace watchface_complication;
 struct complication_candidates_list_ {
   GList* candidates_list;
 };
@@ -295,23 +291,9 @@ extern "C" EXPORT_API int watchface_editable_request_edit(
     return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
 
   static cynara_result pcr = COMPLICATION_CYNARA_UNKNOWN;
-  int ret = WATCHFACE_COMPLICATION_ERROR_NONE;
-
-  if (pcr == COMPLICATION_CYNARA_DENIED) {
-    LOGE("Permission denied");
-    return WATCHFACE_COMPLICATION_ERROR_PERMISSION_DENIED;
-
-  } else if (pcr == COMPLICATION_CYNARA_UNKNOWN) {
-    ret = watchface_complication::util::CheckPrivilege(PRIVILEGE_DATASHARING);
-    if (ret == WATCHFACE_COMPLICATION_ERROR_NONE) {
-      pcr = COMPLICATION_CYNARA_ALLOWED;
-    } else if (ret == WATCHFACE_COMPLICATION_ERROR_PERMISSION_DENIED) {
-      pcr = COMPLICATION_CYNARA_DENIED;
-      return ret;
-    } else {
-      return ret;
-    }
-  }
+  int ret = util::CheckPrivilege(PRIVILEGE_DATASHARING, &pcr);
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+    return ret;
 
   EditablesContainerStub* ec = static_cast<EditablesContainerStub*>(handle);
   unique_ptr<UpdateCallbackInfo> ci = unique_ptr<UpdateCallbackInfo>(
index 1fd9ce9..0674d88 100644 (file)
@@ -60,25 +60,7 @@ void EditablesEditor::Impl::OnAppear(const std::string& name,
 
 int EditablesEditor::Impl::CheckPrivilege() {
   static cynara_result pcr = COMPLICATION_CYNARA_UNKNOWN;
-
-  int ret = WATCHFACE_COMPLICATION_ERROR_NONE;
-
-  if (pcr == COMPLICATION_CYNARA_DENIED) {
-    LOGE("Permission denied");
-    return WATCHFACE_COMPLICATION_ERROR_PERMISSION_DENIED;
-
-  } else if (pcr == COMPLICATION_CYNARA_UNKNOWN) {
-    ret = util::CheckPrivilege(PRIVILEGE_DATASHARING);
-    if (ret == WATCHFACE_COMPLICATION_ERROR_NONE) {
-      pcr = COMPLICATION_CYNARA_ALLOWED;
-    } else if (ret == WATCHFACE_COMPLICATION_ERROR_PERMISSION_DENIED) {
-      pcr = COMPLICATION_CYNARA_DENIED;
-      return ret;
-    } else {
-      return ret;
-    }
-  }
-  return ret;
+  return util::CheckPrivilege(PRIVILEGE_DATASHARING, &pcr);
 }
 
 void EditablesEditor::Impl::OnSignal(GDBusConnection* connection,