Adding the ability to set and get locale
authorBrian Jones <brian.j.jones@intel.com>
Mon, 17 Nov 2014 22:54:45 +0000 (14:54 -0800)
committerBrian Jones <brian.j.jones@intel.com>
Fri, 21 Nov 2014 22:37:36 +0000 (14:37 -0800)
system_setting/system_setting.gyp
system_setting/system_setting_api.js
system_setting/system_setting_instance.h
system_setting/system_setting_instance_tizen.cc
system_setting/system_setting_locale.cc [new file with mode: 0644]
system_setting/system_setting_locale.h [new file with mode: 0644]

index fb95789..6923f18 100644 (file)
@@ -14,6 +14,8 @@
         'system_setting_instance.h',
         'system_setting_instance_desktop.cc',
         'system_setting_instance_tizen.cc',
+        'system_setting_locale.cc',
+        'system_setting_locale.h',
       ],
       'conditions': [
         ['tizen == 1', {
index be99871..f6e65ae 100644 (file)
@@ -5,7 +5,8 @@ var systemSettingTypes = {
   'HOME_SCREEN': 0,
   'LOCK_SCREEN': 1,
   'INCOMING_CALL': 2,
-  'NOTIFICATION_EMAIL': 3
+  'NOTIFICATION_EMAIL': 3,
+  'LOCALE' : 4
 };
 var _callbacks = {};
 var _next_reply_id = 0;
index 934294d..8f3a194 100644 (file)
@@ -22,6 +22,7 @@ class SystemSettingInstance : public common::Instance {
     LOCK_SCREEN = 1,
     INCOMING_CALL = 2,
     NOTIFICATION_EMAIL = 3,
+    LOCALE = 4
   };
 
   // common::Instance implementation.
index 56f4e18..8771ed6 100644 (file)
@@ -3,6 +3,7 @@
 // found in the LICENSE file.
 
 #include "system_setting/system_setting_instance.h"
+#include "system_setting/system_setting_locale.h"
 
 #include <system_settings.h>
 #include <vconf.h>
@@ -27,12 +28,17 @@ void SystemSettingInstance::HandleSetProperty(const picojson::value& msg) {
     case NOTIFICATION_EMAIL:
       key = SYSTEM_SETTINGS_KEY_EMAIL_ALERT_RINGTONE;
       break;
+    case LOCALE:
+      system_setting::setLocale(value);
+      break;
     default:
       std::cout<< "Invalid Key : should not reach here";
       break;
   }
 
-  int ret = system_settings_set_value_string(key, value);
+  int ret = 0;
+  if (type != LOCALE)
+    ret = system_settings_set_value_string(key, value);
   OnPropertyHandled(reply_id, value, ret);
 }
 
@@ -41,6 +47,8 @@ void SystemSettingInstance::HandleGetProperty(const picojson::value& msg) {
     (msg.get("_type").get<double>());
   const char* reply_id = msg.get("_reply_id").to_str().c_str();
   system_settings_key_e key = SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE;
+  char* value = NULL;
+  std::string locale_str;
   switch (type) {
     case HOME_SCREEN:
       key = SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN;
@@ -54,13 +62,19 @@ void SystemSettingInstance::HandleGetProperty(const picojson::value& msg) {
     case NOTIFICATION_EMAIL:
       key = SYSTEM_SETTINGS_KEY_EMAIL_ALERT_RINGTONE;
       break;
-  default:
-    std::cout<< "Invalid Key :should not reach here";
-    break;
+    case LOCALE:
+      locale_str = system_setting::getLocale();
+      value = reinterpret_cast<char *>(malloc(locale_str.length() + 1));
+      std::snprintf(value, sizeof(value), locale_str.c_str());
+      break;
+    default:
+      std::cout<< "Invalid Key :should not reach here";
+      break;
   }
 
-  char* value = NULL;
-  int ret = system_settings_get_value_string(key, &value);
+  int ret = 0;
+  if (type != LOCALE)
+    ret = system_settings_get_value_string(key, &value);
   OnPropertyHandled(reply_id, value, ret);
   free(value);
 }
diff --git a/system_setting/system_setting_locale.cc b/system_setting/system_setting_locale.cc
new file mode 100644 (file)
index 0000000..32c1009
--- /dev/null
@@ -0,0 +1,110 @@
+// Copyright (c) 2014 Intel Corporation. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "system_setting/system_setting_locale.h"
+
+#include <gio/gio.h>
+#include <iostream>
+
+#define LOGGER_E(msg) std::cerr << "\n[Error] " << msg << std::endl;
+#define LOGGER_D(msg) std::cerr << "\n[DEBUG] " << msg << std::endl;
+
+namespace {
+
+const char sDBusProp[] = "org.freedesktop.DBus.Properties";
+const char sInterfaceName[] = "org.freedesktop.locale1";
+const char sObjectPath[] = "/org/freedesktop/locale1";
+
+}  // anonymous namespace
+
+namespace system_setting {
+
+std::string getLocale() {
+  GError* error = nullptr;
+
+  GDBusProxy* proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
+                                                    G_DBUS_PROXY_FLAGS_NONE,
+                                                    NULL,
+                                                    sInterfaceName,
+                                                    sObjectPath,
+                                                    sDBusProp,
+                                                    NULL,
+                                                    &error);
+
+  GVariant* property = g_dbus_proxy_call_sync(proxy,
+                                              "Get",
+                                              g_variant_new("(ss)",
+                                                            sInterfaceName,
+                                                            "Locale"),
+                                              G_DBUS_CALL_FLAGS_NONE,
+                                              -1,
+                                              NULL,
+                                              &error);
+
+  if (error)  {
+    LOGGER_E("Error getting locale: " << error->message);
+    g_error_free(error);
+    return NULL;
+  }
+
+  GVariant* local_var;
+
+  g_variant_get(property, "(v)", &local_var);
+  LOGGER_D("locale type string: " << g_variant_get_type_string(local_var));
+
+  GVariantIter* iter;
+  g_variant_get(local_var, "as", &iter);
+  char* locale;
+  g_variant_iter_next(iter, "s", &locale);
+
+  LOGGER_D("system locale is: " << locale);
+
+  if (local_var)
+    g_variant_unref(local_var);
+
+  if (property)
+    g_variant_unref(property);
+
+  if (iter)
+    g_variant_iter_free(iter);
+
+  return std::string(locale).substr(5);
+}
+
+void setLocale(const std::string& locale_str) {
+  LOGGER_D("Trying to set locale to: " << locale_str);
+  GError* error = NULL;
+  GDBusProxy* proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
+                                                    G_DBUS_PROXY_FLAGS_NONE,
+                                                    NULL,
+                                                    sInterfaceName,
+                                                    sObjectPath,
+                                                    sInterfaceName,
+                                                    NULL,
+                                                    &error);
+
+  GVariantBuilder builder;
+  g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
+  g_variant_builder_add(&builder, "s", locale_str.c_str());
+  GVariant* val = g_dbus_proxy_call_sync(proxy,
+                                         "SetLocale",
+                                         g_variant_new("(asb)",
+                                                       &builder,
+                                                       false),
+                                         G_DBUS_CALL_FLAGS_NONE,
+                                         -1,
+                                         NULL,
+                                         &error);
+
+  if (error)  {
+    LOGGER_E("Error setting locale: " << error->message);
+    g_error_free(error);
+  }
+
+  if (val)
+    g_variant_unref(val);
+
+  g_object_unref(proxy);
+}
+}  // namespace system_setting
diff --git a/system_setting/system_setting_locale.h b/system_setting/system_setting_locale.h
new file mode 100644 (file)
index 0000000..04df108
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright (c) 2014 Intel Corporation. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SYSTEM_SETTING_SYSTEM_SETTING_LOCALE_H_
+#define SYSTEM_SETTING_SYSTEM_SETTING_LOCALE_H_
+
+#include <string>
+
+namespace system_setting {
+
+std::string getLocale();
+void setLocale(const std::string& locale_str);
+
+}  // namespace system_setting
+
+#endif  // SYSTEM_SETTING_SYSTEM_SETTING_LOCALE_H_