[DataSync] add
authorRyszard Matuszyk <r.matuszyk@samsung.com>
Wed, 14 Jan 2015 12:38:23 +0000 (13:38 +0100)
committerRafal Galka <r.galka@samsung.com>
Wed, 14 Jan 2015 14:16:07 +0000 (23:16 +0900)
Verification: not available. Platform function sync_agent_ds_init not work

Change-Id: I08de072fcdb2824978192b0b9388ff9b3740484e
Signed-off-by: Ryszard Matuszyk <r.matuszyk@samsung.com>
src/datasync/datasync_instance.cc
src/datasync/datasync_manager.cc
src/datasync/datasync_manager.h

index 8165045..ba1a654 100644 (file)
@@ -15,6 +15,8 @@
 namespace extension {
 namespace datasync {
 
+using namespace common;
+
 DatasyncInstance::DatasyncInstance() {
     using namespace std::placeholders;
     #define REGISTER_SYNC(c, x) \
@@ -70,11 +72,9 @@ void DatasyncInstance::GetLastSyncStatistics(const picojson::value &args, picojs
 }
 
 void DatasyncInstance::Add(const picojson::value &args, picojson::object &out) {
-//  TODO: implementation
-
-    picojson::value val{picojson::object{}};
-
-    ReportSuccess(val, out);
+  ReportSuccess(
+      picojson::value(static_cast<double>(DataSyncManager::Instance().Add(
+                                              args.get<picojson::object>()))), out);
 }
 
 void DatasyncInstance::Update(const picojson::value &args, picojson::object &out) {
index bc6d0cf..2bc7e62 100644 (file)
 #include "datasync/sync_info.h"
 #include "datasync/sync_service_info.h"
 #include "datasync/sync_profile_info.h"
+#include "common/converter.h"
 
 #include "tizen/tizen.h"
 
 namespace extension {
 namespace datasync {
 
+using namespace common;
+
 const int MAX_PROFILES_NUM = 5;
 
+const std::string kDefaultEnumKey = "_DEFAULT";
+const std::string kSyncMode = "SyncMode";
+const std::string kSyncType = "SyncType";
+const std::string kSyncInterval = "SyncInterval";
+const std::string kSyncServiceType = "SyncServiceType";
+const std::string kSyncServiceTypeToSrcUri = "SyncServiceTypeToSrcUri";
+const std::string kSyncStatus = "SyncStatus";
+
+const PlatformEnumMap DataSyncManager::platform_enum_map_ = {
+    {kSyncMode,
+     {{kDefaultEnumKey, SYNC_AGENT_SYNC_MODE_MANUAL},
+      {"MANUAL", SYNC_AGENT_SYNC_MODE_MANUAL},
+      {"PERIODIC", SYNC_AGENT_SYNC_MODE_PERIODIC},
+      {"PUSH", SYNC_AGENT_SYNC_MODE_PUSH}}},
+    {kSyncType,
+     {{kDefaultEnumKey, SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH},
+      {"TWO_WAY", SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH},
+      {"SLOW", SYNC_AGENT_SYNC_TYPE_FULL_SYNC},
+      {"ONE_WAY_FROM_CLIENT", SYNC_AGENT_SYNC_TYPE_UPDATE_TO_SERVER},
+      {"REFRESH_FROM_CLIENT", SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_PHONE},
+      {"ONE_WAY_FROM_SERVER", SYNC_AGENT_SYNC_TYPE_UPDATE_TO_PHONE},
+      {"REFRESH_FROM_SERVER", SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_SERVER}}},
+    {kSyncInterval,
+     {{kDefaultEnumKey, SYNC_AGENT_SYNC_INTERVAL_1_WEEK},
+      {"5_MINUTES", SYNC_AGENT_SYNC_INTERVAL_5_MINUTES},
+      {"15_MINUTES", SYNC_AGENT_SYNC_INTERVAL_15_MINUTES},
+      {"1_HOUR", SYNC_AGENT_SYNC_INTERVAL_1_HOUR},
+      {"4_HOURS", SYNC_AGENT_SYNC_INTERVAL_4_HOURS},
+      {"12_HOURS", SYNC_AGENT_SYNC_INTERVAL_12_HOURS},
+      {"1_DAY", SYNC_AGENT_SYNC_INTERVAL_1_DAY},
+      {"1_WEEK", SYNC_AGENT_SYNC_INTERVAL_1_WEEK},
+      {"1_MONTH", SYNC_AGENT_SYNC_INTERVAL_1_MONTH},
+      {"NONE", SYNC_AGENT_SYNC_INTERVAL_NONE}}},
+    {kSyncServiceType,
+     {{kDefaultEnumKey, SYNC_AGENT_CONTACT},
+      {"CONTACT", SYNC_AGENT_CONTACT},
+      {"EVENT", SYNC_AGENT_CALENDAR}}},
+    {kSyncServiceTypeToSrcUri,
+     {{kDefaultEnumKey, SYNC_AGENT_SRC_URI_CONTACT},
+      {"CONTACT", SYNC_AGENT_SRC_URI_CONTACT},
+      {"EVENT", SYNC_AGENT_SRC_URI_CALENDAR}}}};
+
+int DataSyncManager::StrToPlatformEnum(const std::string& field, const std::string& value) {
+  if (platform_enum_map_.find(field) == platform_enum_map_.end()) {
+      throw UnknownException(std::string("Undefined platform enum type ") + field);
+  }
+
+  auto def = platform_enum_map_.at(field);
+  auto def_iter = def.find(value);
+  if (def_iter != def.end()) {
+    return def_iter->second;
+  }
+
+  // default value - if any
+  def_iter = def.find("_DEFAULT");
+  if (def_iter != def.end()) {
+    return def_iter->second;
+  }
+
+  std::string message = "Platform enum value " + value + " not found for " + field;
+  throw InvalidValuesException(message);
+}
+
+
+
 sync_agent_ds_sync_mode_e ConvertToPlatformSyncMode(
     datasync::SyncInfo::SyncMode sync_mode) {
   switch (sync_mode) {
@@ -298,129 +366,97 @@ DataSyncManager::~DataSyncManager() {
   // sync-agent should fix it's deinitialization
 }
 
-ResultOrError<std::string> DataSyncManager::Add(SyncProfileInfo& profile_info) {
-  ds_profile_h profile_h = nullptr;
-  char* profile_name = nullptr;
-
-  // Check if the quota is full first.
-  GList* profile_list = nullptr;
-  GList* iter = nullptr;
-
-  auto exit = common::MakeScopeExit([&profile_name, &profile_h](){
-    if (profile_name) {
-      free(profile_name);
-    }
-
-    if (profile_h) {
-      sync_agent_ds_free_profile_info(profile_h);
-    }
-  });
-
-  sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
+void DataSyncManager::Item(ds_profile_h* profile_h, const picojson::object& args) {
+  sync_agent_ds_error_e ret;
 
-  ret = sync_agent_ds_get_all_profile(&profile_list);
+  std::string profile_name = FromJson<std::string>(args, "profileName").c_str();
+  ret = sync_agent_ds_set_profile_name(profile_h, const_cast<char*>(profile_name.c_str()));
   if (SYNC_AGENT_DS_SUCCESS != ret) {
-    return Error("Exception",
-        "Platform error while getting all profiles");
+    throw UnknownException("Platform error while settting a profile name");
   }
 
-  int num_profiles = g_list_length(profile_list);
-  for (iter = profile_list; iter != nullptr; iter = g_list_next(iter)) {
-    sync_agent_ds_free_profile_info((ds_profile_h)iter->data);
-  }
-  if (profile_list) {
-    g_list_free(profile_list);
-  }
-  LoggerD("numProfiles: %d", num_profiles);
-  if (MAX_PROFILES_NUM == num_profiles) {
-    return Error("OutOfRangeException",
-        "There are already maximum number of profiles!");
-  }
-
-  ret = sync_agent_ds_create_profile_info(&profile_h);
+  const picojson::object& sync_info = FromJson<picojson::object>(args, "syncInfo");
+  std::string url = FromJson<std::string>(sync_info, "url").c_str();
+  std::string id = FromJson<std::string>(sync_info, "id").c_str();
+  std::string password = FromJson<std::string>(sync_info, "password").c_str();
+  ret = sync_agent_ds_set_server_info(profile_h, const_cast<char*>(url.c_str()),
+                                      const_cast<char*>(id.c_str()),
+                                      const_cast<char*>(password.c_str()));
   if (SYNC_AGENT_DS_SUCCESS != ret) {
-    return Error("Exception",
-        "Platform error while creating a profile");
+    throw UnknownException("Platform error while settting a server info");
   }
 
-  ret = sync_agent_ds_set_profile_name(
-      profile_h, const_cast<char*>(profile_info.profile_name().c_str()));
+  int sync_mode = StrToPlatformEnum(kSyncMode, FromJson<std::string>(sync_info, "mode"));
+  int sync_type = StrToPlatformEnum(kSyncType, FromJson<std::string>(sync_info, "type"));
+  int sync_interval = StrToPlatformEnum(kSyncInterval,
+                                        FromJson<std::string>(sync_info, "interval"));
+  LoggerD("syncMode: %d, syncType: %d, syncInterval: %d", sync_mode, sync_type, sync_interval);
+  ret = sync_agent_ds_set_sync_info(profile_h, static_cast<sync_agent_ds_sync_mode_e>(sync_mode),
+                                    static_cast<sync_agent_ds_sync_type_e>(sync_type),
+                                    static_cast<sync_agent_ds_sync_interval_e>(sync_interval));
   if (SYNC_AGENT_DS_SUCCESS != ret) {
-    return Error("Exception",
-        "Platform error while settting a profile name");
+    throw UnknownException("Platform error while settting a sync info");
   }
 
-  ret = sync_agent_ds_set_server_info(
-      profile_h, const_cast<char*>(profile_info.sync_info()->url().c_str()),
-      const_cast<char*>(profile_info.sync_info()->id().c_str()),
-      const_cast<char*>(profile_info.sync_info()->password().c_str()));
-  if (SYNC_AGENT_DS_SUCCESS != ret) {
-    return Error("Exception",
-        "Platform error while settting a server info");
-  }
+  // Set the sync categories.
+  if (IsNull(args, "serviceInfo")) return;
 
-  sync_agent_ds_sync_mode_e sync_mode =
-      ConvertToPlatformSyncMode(profile_info.sync_info()->sync_mode());
-  sync_agent_ds_sync_type_e sync_type =
-      ConvertToPlatformSyncType(profile_info.sync_info()->sync_type());
-  sync_agent_ds_sync_interval_e sync_interval =
-      ConvertToPlatformSyncInterval(profile_info.sync_info()->sync_interval());
-  LoggerD("syncMode: %d, syncType: %d, syncInterval: %d", sync_mode, sync_type, sync_interval);
+  const picojson::array& service_info = FromJson<picojson::array>(args, "serviceInfo");
 
-  ret = sync_agent_ds_set_sync_info(profile_h, sync_mode, sync_type,
-                                    sync_interval);
-  if (SYNC_AGENT_DS_SUCCESS != ret) {
-    return Error("Exception",
-        "Platform error while settting a sync info");
-  }
+  for (auto& item : service_info) {
+    const picojson::object& obj = JsonCast<picojson::object>(item);
 
-  // Set the sync categories.
-  SyncServiceInfoListPtr categories = profile_info.service_info();
-  for (unsigned int i = 0; categories->size() < i; ++i) {
-    sync_agent_ds_service_type_e service_type =
-        ConvertToPlatformSyncServiceType(
-            categories->at(i)->sync_service_type());
-    std::string tgt_uri = categories->at(i)->server_database_uri();
-    sync_agent_ds_src_uri_e src_uri =
-        ConvertToPlatformSourceUri(categories->at(i)->sync_service_type());
-    std::string id = categories->at(i)->id();
-    std::string password = categories->at(i)->password();
-    bool enable = categories->at(i)->enable();
+    bool enable = FromJson<bool>(obj, "enable");
+    int service_type =
+        StrToPlatformEnum(kSyncServiceType, FromJson<std::string>(obj, "serviceType"));
+    std::string tgt_uri = FromJson<std::string>(obj, "serverDatabaseUri");
+    int src_uri = StrToPlatformEnum(kSyncServiceType, FromJson<std::string>(obj, "serviceType"));
 
-    LoggerI("serviceType: %d, tgtURI: %s, enable: %d for index: %d",
-            service_type, tgt_uri.c_str(), enable, i);
+    std::string id = "";
+    if (!IsNull(obj, "id")) id = FromJson<std::string>(obj, "id");
+
+    std::string password = "";
+    if (!IsNull(obj, "password")) password = FromJson<std::string>(obj, "password");
+
+    LoggerI("serviceType: %d, tgtURI: %s, enable: %d", service_type, tgt_uri.c_str(), enable);
 
     ret = sync_agent_ds_set_sync_service_info(
-        profile_h, service_type, enable, src_uri,
-        const_cast<char*>(tgt_uri.c_str()),
+        profile_h, static_cast<sync_agent_ds_service_type_e>(service_type), enable,
+        static_cast<sync_agent_ds_src_uri_e>(src_uri), const_cast<char*>(tgt_uri.c_str()),
         0 == id.size() ? nullptr : const_cast<char*>(id.c_str()),
         0 == password.size() ? nullptr : const_cast<char*>(password.c_str()));
     if (SYNC_AGENT_DS_SUCCESS != ret) {
-      return Error("Exception",
-          "Platform error while settting a sync service info");
+      throw UnknownException("Platform error while settting a sync service info");
     }
   }
+}
 
-  int profile_id;
-  ret = sync_agent_ds_add_profile(profile_h, &profile_id);
+int DataSyncManager::Add(const picojson::object& args) {
+  ds_profile_h profile_h = nullptr;
+
+  int num_profiles = GetProfilesNum();
+  LoggerD("numProfiles: %d", num_profiles);
+
+  if (MAX_PROFILES_NUM == num_profiles) {
+    throw QuotaExceededException("There are already maximum number of profiles!");
+  }
+
+  sync_agent_ds_error_e ret = sync_agent_ds_create_profile_info(&profile_h);
   if (SYNC_AGENT_DS_SUCCESS != ret) {
-    return Error("Exception",
-        "Platform error while adding a profile");
+    throw UnknownException("Platform error while creating a profile");
   }
 
-  LoggerD("profileId from platform: %d", profile_id);
+  Item(&profile_h, args);
 
-  ret = sync_agent_ds_get_profile_name(profile_h, &profile_name);
+  int profile_id;
+  ret = sync_agent_ds_add_profile(profile_h, &profile_id);
   if (SYNC_AGENT_DS_SUCCESS != ret) {
-    return Error("Exception",
-        "Platform error while getting a profile name");
+    throw UnknownException("Platform error while adding a profile");
   }
 
-  LoggerD("profileName: %s, profileId: %d", profile_name, profile_id);
-
-  profile_info.set_profile_id(std::to_string(profile_id));
+  LoggerD("profileId from platform: %d", profile_id);
 
-  return profile_info.profile_id();
+  return profile_id;
 }
 
 ResultOrError<void> DataSyncManager::Update(SyncProfileInfo& profile_info) {
index 6ab5cf5..9e205d6 100644 (file)
 #include "datasync/datasync_error.h"
 #include "datasync/sync_profile_info.h"
 #include "datasync/sync_statistics.h"
+#include "common/picojson.h"
 
 namespace extension {
 namespace datasync {
 
+typedef std::map<std::string, std::map<std::string, int>> PlatformEnumMap;
+
 class DatasyncInstance;
 
 class DataSyncManager {
@@ -31,7 +34,7 @@ class DataSyncManager {
 
   ~DataSyncManager();
 
-  ResultOrError<std::string> Add(SyncProfileInfo& profile_info);
+  int Add(const picojson::object &args);
   ResultOrError<void> Update(SyncProfileInfo& profile_info);
   ResultOrError<void> Remove(const std::string& id);
 
@@ -52,17 +55,22 @@ class DataSyncManager {
 
   static DataSyncManager& Instance();
 
+  static int StrToPlatformEnum(const std::string& field, const std::string& value);
+
  private:
   DataSyncManager();
 
   int StateChangedCallback(sync_agent_event_data_s* request);
   int ProgressCallback(sync_agent_event_data_s* request);
+  void Item(ds_profile_h *profile_h, const picojson::object &args);
 
   ProfileIdToCallbackMap callbacks_;
 
   static bool sync_agent_initialized_;
 
   DISALLOW_COPY_AND_ASSIGN(DataSyncManager);
+
+  static const PlatformEnumMap platform_enum_map_;
 };
 
 }  // namespace datasync