Fix get default value logic 54/185154/6
authorhyunho <hhstark.kang@samsung.com>
Thu, 26 Jul 2018 09:01:15 +0000 (18:01 +0900)
committerhyunho <hhstark.kang@samsung.com>
Fri, 27 Jul 2018 01:59:11 +0000 (10:59 +0900)
Change-Id: I73aa36ac9e5ee6b1e72c7f3f79168ef278b97d73
Signed-off-by: hyunho <hhstark.kang@samsung.com>
parser/complication_parser_plugin.cc
parser/complication_parser_plugin_internal.cc

index 8582858..41d6464 100644 (file)
@@ -18,6 +18,7 @@
 #include <stdlib.h>
 #include <stdbool.h>
 #include <string.h>
+#include <time.h>
 #include <sys/types.h>
 
 #include <libxml/tree.h>
 
 #include "complication_parser_plugin_internal.h"
 
-#define DEFAULT_SHORT_TEXT "default-shorttext"
-#define DEFAULT_LONG_TEXT "default-longtext"
+#define DEFAULT_SHORT_TEXT "default-short-text"
+#define DEFAULT_LONG_TEXT "default-long-text"
 #define DEFAULT_ICON "default-icon"
 #define DEFAULT_TITLE "default-title"
 #define DEFAULT_HOUR "default-hour"
 #define DEFAULT_MINUTE "default-minute"
 #define DEFAULT_SECOND "default-second"
-#define DEFAULT_CURRENT "default-current"
+#define DEFAULT_CURRENT "default-value"
 #define DEFAULT_MAX "default-max"
 #define DEFAULT_MIN "default-min"
 #define DEFAULT_IMAGE "default-image"
@@ -215,6 +216,56 @@ static bool _is_valid_element(int type, char* element_name) {
   return is_valid;
 }
 
+static int _get_time_value(const char* time_type, const char* time_val,
+    char* prev_value, char** default_value) {
+  time_t rawtime;
+  struct tm * timeinfo;
+
+  if (time_type == NULL || time_val == NULL || default_value == NULL) {
+    LOGE("Invalid param");
+    return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
+  }
+
+  if (prev_value == NULL)
+    time(&rawtime);
+  else
+    rawtime = strtol(prev_value, NULL, 10);
+
+  timeinfo = localtime(&rawtime);
+  if (strcmp(DEFAULT_HOUR, time_type) == 0) {
+    int hour = atoi(time_val);
+    if (hour < 0 || hour >= 24) {
+      LOGE("Invalid param %s (%d)", time_type, hour);
+      return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
+    }
+    timeinfo->tm_hour = hour;
+  } else if (strcmp(DEFAULT_MINUTE, time_type) == 0) {
+    int minute = atoi(time_val);
+    if (minute < 0 || minute >= 60) {
+      LOGE("Invalid param %s (%d)", time_type, minute);
+      return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
+    }
+    timeinfo->tm_min = minute;
+  } else if (strcmp(DEFAULT_SECOND, time_type) == 0) {
+    int second = atoi(time_val);
+    if (second < 0 || second >= 60) {
+      LOGE("Invalid param %s (%d)", time_type, second);
+      return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
+    }
+    timeinfo->tm_sec = second;
+  }
+
+  time_t default_time = mktime(timeinfo);
+  char num_str[32] = {0,};
+  snprintf(num_str, sizeof(num_str), "%ld", default_time);
+  *default_value = num_str;
+  LOGD("%d/%d/%d %d:%d:%d (%s)",
+      timeinfo->tm_year, timeinfo->tm_mon, timeinfo->tm_mday, timeinfo->tm_hour,
+      timeinfo->tm_min, timeinfo->tm_sec, *default_value);
+
+  return WATCHFACE_COMPLICATION_ERROR_NONE;
+}
+
 static int _parse_support_type(xmlNode* node, sqlite3* db, const char* pkgid,
       const char* appid, const char* providerid, int period) {
   int ret;
@@ -230,6 +281,8 @@ static int _parse_support_type(xmlNode* node, sqlite3* db, const char* pkgid,
   bundle_raw* raw = NULL;
   int len = 0;
   const char* bundle_key;
+  char* default_value = NULL;
+  char* prev_value;
 
   static const char query[] =
     "INSERT INTO complication_provider ( "
@@ -275,10 +328,25 @@ static int _parse_support_type(xmlNode* node, sqlite3* db, const char* pkgid,
           goto out;
         }
 
-        ret = bundle_add_str(default_data, bundle_key,
-            (char *)tmpdata->children->content);
+        if (strcmp(bundle_key, TIME_KEY) == 0) {
+          prev_value = NULL;
+          bundle_get_str(default_data, TIME_KEY, &prev_value);
+          ret = _get_time_value(
+              (const char*)tmpdata->name,
+              (const char*)tmpdata->children->content,
+              prev_value, &default_value);
+          if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+            goto out;
+
+          if (prev_value)
+            bundle_del(default_data, TIME_KEY);
+        } else {
+          default_value = (char*)tmpdata->children->content;
+        }
+
+        ret = bundle_add_str(default_data, bundle_key, default_value);
         if (ret != BUNDLE_ERROR_NONE) {
-          LOGE("bundle add error");
+          LOGE("bundle add error : %d", ret);
           goto out;
         }
       }
index 3f3e178..d368933 100644 (file)
@@ -180,11 +180,14 @@ int check_db(uid_t uid) {
   if (path == NULL)
     return -1;
 
+  LOGD("check db (%s)", path);
   ret = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, NULL);
   if (ret != SQLITE_OK) {
     ret = _create_db(db, path);
-    if (ret != SQLITE_OK)
+    if (ret != SQLITE_OK) {
+      LOGE("Fail to create db %d", ret);
       return -1;
+    }
   }
 
   if (_check_table_exist(db)) {
@@ -192,6 +195,7 @@ int check_db(uid_t uid) {
       if (db)
         sqlite3_close_v2(db);
 
+      LOGE("Fail to create table");
       return -1;
     }
   }