tizen 2.3.1 release
[apps/home/settings.git] / src / conf_util / setting_confutil.c
1 /*
2  * setting_confutil
3  *
4  *
5  *
6         $ setting_confutil                              // create a cfg file in default
7         $ setting_confutil export               // export current status to a xml file
8         $ setting_confutil timezone_init        // timezone init
9  */
10 #include <stdio.h>
11
12 #include <setting-cfg.h>
13 #include <setting-debug.h>
14 #include <stdio.h>
15 #include <Elementary.h>
16 #include <setting-common-general-func.h>
17 #include <setting-common-data-slp-setting.h>
18 #include <unistd.h>
19 #include <system_settings.h>
20
21 #include <stdio.h>
22 #include <libxml/parser.h>
23 #include <libxml/tree.h>
24 #include <setting-common-search.h>
25
26 static char *get_timezone();
27
28 static void get_gmt_offset(char *str_buf, int size)
29 {
30         /* timezone string +/-<n> ex. +9, -1 */
31         time_t t = time(0);     /* get unix time. sec. */
32
33         struct tm *data;
34         data = localtime(&t);           /* save time as structure. */
35         setting_retm_if(!data, "data is NULL");
36         data->tm_isdst = 0;                     /* summer time, not applied. */
37         time_t a = mktime(data);
38
39         data = gmtime(&a);
40         setting_retm_if(!data, "data is NULL");
41         data->tm_isdst = 0;                     /* summer time, not applied. */
42         time_t b = mktime(data);
43
44         int gmtoffset_hour = (a - b) / 3600;    /* result : hour. */
45         int gmtoffset_min = ((a - b) % 3600) / 60;      /* result : min. */
46         if (gmtoffset_min != 0) {
47                 gmtoffset_min = 30;
48         }
49
50         snprintf(str_buf, size, "%+d:%02u", gmtoffset_hour, gmtoffset_min);
51         SETTING_TRACE("szTimezone is of a valid format: GMT: %s", str_buf);
52 }
53
54 int generate_setting_cfg()
55 {
56         if (0 == setting_cfg_create(false)) {
57                 SETTING_TRACE_ERROR("Error to create a new config file");
58                 return 0 ;
59         }
60
61         return 1;
62 }
63
64 int migrate_setting_cfg()
65 {
66         SETTING_TRACE_BEGIN;
67         if (0 == setting_cfg_migrate()) {
68                 SETTING_TRACE_ERROR("Fail to migrate config file");
69                 return 0 ;
70         }
71         SETTING_TRACE_END;
72         return 1;
73 }
74
75 void timezone_init()
76 {
77         char *tzpath = get_timezone();
78         int ret = vconf_set_str(VCONFKEY_SETAPPL_TIMEZONE_ID, tzpath + 20);
79         if (ret != 0) {
80                 SETTING_TRACE("fail to set vconf");
81         }
82         char str_buf[256] = {0, };
83         get_gmt_offset(str_buf, 256);
84         SETTING_TRACE(">>> time zone GMT string : %s", str_buf);
85         g_free(tzpath);
86 }
87
88 void get_current_font()
89 {
90         char *value = NULL;
91         int retcode = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_FONT_TYPE, &value);
92         if (retcode != 0) {
93                 SETTING_TRACE("fail to set SYSTEM_SETTINGS_KEY_FONT_TYPE");
94         }
95         SETTING_TRACE(">>> get current font type : %s \n", value);
96 }
97
98 int status_fp(int total, int current, void *data)
99 {
100         SETTING_TRACE(">> total : %d ---- current : %d ", total, current);
101         return 0;
102 }
103
104
105 /**
106 sh-4.1# /opt/usr/apps/org.tizen.setting/bin/setting_conf_util timezone_check
107 debug level init 1(1)
108 >>> time zone : /usr/share/zoneinfo/Asia/Seoul
109 */
110 int main(int argc, char *argv[])
111 {
112         g_type_init();
113
114         elm_init(argc, argv);
115         setting_set_i18n_force(SETTING_PACKAGE, SETTING_LOCALEDIR);
116
117         if ((argc == 2) && (0 == strcmp(argv[1], "export_json"))) {
118                 setting_export_json(status_fp, NULL);
119         } else if ((argc == 2) && (0 == strcmp(argv[1], "import_json"))) {
120                 setting_import_json(status_fp, NULL);
121         } else if ((argc == 2) && (0 == strcmp(argv[1], "timezone_init"))) {
122                 timezone_init();
123         } else if ((argc == 2) && (0 == strcmp(argv[1], "get_current_font"))) {
124                 get_current_font();
125         } else if ((argc == 2) && (0 == strcmp(argv[1], "gen_cfg"))) {
126                 generate_setting_cfg();
127         } else if ((argc == 2) && (0 == strcmp(argv[1], "mig_cfg"))) {
128                 migrate_setting_cfg();
129         }
130 #if SETTING_SEARCH
131         else if ((argc == 2) && (0 == strcmp(argv[1], "search_db_indexing"))) {
132                 /* app db search */
133                 __setting_init_search_index_app();
134
135                 __setting_init_search_index_module();
136         }
137 #endif
138         else {
139                 /* cfg create */
140                 /* TRUE or FALSE */
141                 setting_cfg_create(false);
142         }
143         return 0;
144 }
145
146 /* automatic */
147 static char *get_timezone()
148 {
149         SETTING_TRACE_BEGIN;
150
151         enum { BUFFERSIZE = 1024 };
152         char buf[BUFFERSIZE] = {0, };
153         ssize_t len = readlink("/opt/etc/localtime", buf, sizeof(buf) - 1);
154
155         if (len != -1) {
156                 buf[len] = '\0';
157         } else {
158                 /* handle error condition */
159                 return NULL;
160         }
161         return g_strdup(buf);
162 }
163