Add function to get complication label 11/175211/13
authorSukHyung, Kang <shine.kang@samsung.com>
Mon, 9 Apr 2018 08:26:09 +0000 (17:26 +0900)
committerHwanKyu Jhun <h.jhun@samsung.com>
Thu, 19 Apr 2018 03:42:04 +0000 (03:42 +0000)
Change-Id: Ied5d7b7efaeaffbf8d1a82dd099c1d15afafb67c
Signed-off-by: SukHyung, Kang <shine.kang@samsung.com>
watchface-complication/db-manager.cc
watchface-complication/db-manager.h

index fc9879e..a6f44ec 100644 (file)
@@ -20,6 +20,7 @@
 #include <tzplatform_config.h>
 #include <pkgmgr-info.h>
 #include <pkgmgr_installer_info.h>
+#include <vconf.h>
 
 #include "watchface-complication/db-manager.h"
 
@@ -29,6 +30,7 @@
 
 #define LOG_TAG "WATCHFACE_COMPLICATION"
 
+#define DEFAULT_LOCALE "No Locale"
 #define ROOT_USER 0
 
 namespace watchface_complication {
@@ -334,6 +336,107 @@ std::list<std::string> DBManager::GetRequiredPrivlegeList(std::string& provider_
   return privlege_list;
 }
 
+char* DBManager::GetSystemLocale() {
+  char* lang;
+  char* locale;
+  char* ptr;
+  char* tmp;
+
+  lang = vconf_get_str(VCONFKEY_LANGSET);
+  if (lang == NULL) {
+    locale = strdup(DEFAULT_LOCALE);
+    if (locale == NULL) {
+      LOGE("out of memory");
+      return NULL;
+    }
+    return locale;
+  }
+
+  tmp = strtok_r(lang, ".", &ptr);
+  if (tmp == NULL) {
+    LOGE("failed to get language");
+    free(lang);
+    return NULL;
+  }
+
+  locale = strdup(tmp);
+  if (locale == NULL) {
+    LOGE("out of memory");
+    free(lang);
+    return NULL;
+  }
+
+  for (int i = 0; i < (int)strlen(locale); i++) {
+    if (locale[i] == '_')
+      locale[i] = '-';
+
+    if (isupper(locale[i]))
+      locale[i] = tolower(locale[i]);
+  }
+
+  free(lang);
+
+  return locale;
+}
+
+std::string DBManager::GetLabel(const char* provider_id) {
+  int ret;
+  std::string label;
+  sqlite3_stmt *stmt;
+  sqlite3* db;
+  char* locale = NULL;
+
+  static const char query[] =
+    "SELECT provider_label FROM provider_localized_info "
+    "WHERE provider_id=? AND locale=?";
+
+  db = OpenDB();
+  if (db == NULL) {
+    LOGE("parser db not exist");
+    return std::string();
+  }
+
+  ret = sqlite3_prepare_v2(db, query, strlen(query),
+                        &stmt, NULL);
+  if (ret != SQLITE_OK) {
+    LOGE("prepare error: %s", sqlite3_errmsg(db));
+    CloseDB(db);
+    return std::string();
+  }
+
+  locale = GetSystemLocale();
+  if (locale == NULL) {
+    sqlite3_finalize(stmt);
+    CloseDB(db);
+    return std::string();
+  }
+
+  sqlite3_bind_text(stmt, 1, provider_id, -1, SQLITE_TRANSIENT);
+  sqlite3_bind_text(stmt, 2, locale, -1, SQLITE_TRANSIENT);
+
+  if (sqlite3_step(stmt) == SQLITE_ROW)
+    label = std::string(reinterpret_cast<const char*>(
+        sqlite3_column_text(stmt, 0)));
+
+  if (label.empty()) {
+    sqlite3_reset(stmt);
+    sqlite3_clear_bindings(stmt);
+
+    sqlite3_bind_text(stmt, 1, provider_id, -1, SQLITE_TRANSIENT);
+    sqlite3_bind_text(stmt, 2, DEFAULT_LOCALE, -1, SQLITE_TRANSIENT);
+
+    if (sqlite3_step(stmt) == SQLITE_ROW)
+      label = std::string(reinterpret_cast<const char*>(
+          sqlite3_column_text(stmt, 0)));
+  }
+
+  sqlite3_finalize(stmt);
+  CloseDB(db);
+  free(locale);
+
+  return label;
+}
+
 const char* DBManager::GetParserDataPath() {
   uid_t target_uid;
   const char *path;
index 68d06f0..fe97e81 100644 (file)
@@ -53,6 +53,7 @@ class EXPORT_API DBManager {
     int support_types);
   static int GetSupportTypes(std::string& provider_id, int* types);
   static int GetProviderPeriod(std::string& provider_id, int* period);
+  static std::string GetLabel(const char* provider_id);
 
  private:
   DBManager();
@@ -60,8 +61,9 @@ class EXPORT_API DBManager {
   static sqlite3* OpenDB();
   static void CloseDB(sqlite3* db);
   static const char* GetParserDataPath();
+  static char* GetSystemLocale();
 };
 
 }  // namespace watchface_complication
 
-#endif  // WATCHFACE_COMPLICATION_DB_MANAGER_H_
\ No newline at end of file
+#endif  // WATCHFACE_COMPLICATION_DB_MANAGER_H_