[refactoring] reduce length of value name and log
[platform/core/api/system-settings.git] / src / sst_time_N_locale.c
1 /*
2  * Copyright (c) 2011-2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "sst_time_N_locale.h"
17
18 #include <stdio.h>
19 #include <alarm.h>
20 #include <vconf.h>
21 #include "sst.h"
22 #include "sst_vconf.h"
23 #include "sst_utils.h"
24 #include "sst_interface.h"
25
26 int sst_locale_get_country(sst_interface *iface, void **value)
27 {
28         char *locale = NULL;
29         if (sst_vconf_get_string(iface->vconf_key, &locale)) {
30                 ERR("sst_vconf_get_string(%s) Fail", iface->vconf_key);
31                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
32         }
33
34         // remove encoding ex) en_US.UTF-8 => en_US
35         int i;
36         char country[21];
37         for (i = 0; i < sizeof(country); i++) {
38                 country[i] = locale[i];
39                 if ('.' == country[i])
40                         country[i] = 0;
41                 if (0 == country[i])
42                         break;
43         }
44         if (sizeof(country) <= i) {
45                 ERR("Invalid locale(%s)", locale);
46                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
47         }
48
49         *value = strdup(country);
50         free(locale);
51
52         return SYSTEM_SETTINGS_ERROR_NONE;
53 }
54
55 int sst_locale_set_country(sst_interface *iface, void *value)
56 {
57         char *country = value;
58         const char *codeset = "UTF-8";
59
60         char locale[100];
61         snprintf(locale, sizeof(locale), "%s.%s", country, codeset);
62
63         if (vconf_set_str(iface->vconf_key, locale)) {
64                 ERR("vconf_set_str(%s) Fail", iface->vconf_key);
65                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
66         }
67
68         return SYSTEM_SETTINGS_ERROR_NONE;
69 }
70
71 int sst_locale_get_language(sst_interface *iface, void **value)
72 {
73         //It is same data with sst_locale_get_country()
74         int ret = sst_locale_get_country(iface, value);
75         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
76                 ERR("sst_locale_get_country() Fail(%d)", ret);
77                 return ret;
78         }
79
80         return SYSTEM_SETTINGS_ERROR_NONE;
81 }
82
83 int sst_locale_set_language(sst_interface *iface, void *value)
84 {
85         //It is same data with sst_locale_set_country()
86         int ret = sst_locale_set_country(iface, value);
87         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
88                 ERR("sst_locale_set_country() Fail(%d)", ret);
89                 return ret;
90         }
91
92         return SYSTEM_SETTINGS_ERROR_NONE;
93 }
94
95 int sst_locale_get_timeformat_24hour(sst_interface *iface, void **value)
96 {
97         int int_val;
98
99         if (vconf_get_int(iface->vconf_key, &int_val)) {
100                 ERR("vconf_get_int(%s) Fail", iface->vconf_key);
101                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
102         }
103
104         bool ret_value = true;
105         if (int_val == VCONFKEY_TIME_FORMAT_12)
106                 ret_value = false;
107         else if (int_val == VCONFKEY_TIME_FORMAT_24)
108                 ret_value = true;
109
110         *value = (void*)ret_value;
111
112         return SYSTEM_SETTINGS_ERROR_NONE;
113 }
114
115 int sst_locale_set_timeformat_24hour(sst_interface *iface, void *value)
116 {
117         bool is_24hour = *(bool*)value;
118         int vconf_val;
119
120         if (is_24hour)
121                 vconf_val = VCONFKEY_TIME_FORMAT_24;
122         else
123                 vconf_val = VCONFKEY_TIME_FORMAT_12;
124
125         if (vconf_set_int(iface->vconf_key, vconf_val)) {
126                 ERR("vconf_set_int(%s) Fail", iface->vconf_key);
127                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
128         }
129
130         return SYSTEM_SETTINGS_ERROR_NONE;
131 }
132
133 int sst_locale_get_timezone(sst_interface *iface, void **value)
134 {
135         char tzpath[PATH_MAX];
136         ssize_t len = readlink(SETTING_TZONE_SYMLINK_PATH, tzpath, sizeof(tzpath) - 1);
137         if (-1 == len) {
138                 ERR("readlink(%s) Fail(%d)", SETTING_TZONE_SYMLINK_PATH, errno);
139                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
140         }
141         tzpath[len] = '\0';
142
143         // for including last '/', it used sizeof instead of strlen
144         int start = sizeof(SETTING_TIME_ZONEINFO_PATH);
145
146         *value = strdup(&tzpath[start]);
147         return SYSTEM_SETTINGS_ERROR_NONE;
148 }
149
150 int sst_locale_set_timezone(sst_interface *iface, void *value)
151 {
152         char *timezone = value;
153
154         char tzpath[PATH_MAX];
155         snprintf(tzpath, sizeof(tzpath), SETTING_TIME_ZONEINFO_PATH"/%s", timezone);
156
157         if (false == sst_utils_exist(tzpath)) {
158                 ERR("sst_utils_exist(%s) Fail", tzpath);
159                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
160         }
161
162         int ret = alarmmgr_set_timezone(tzpath);
163         if (ALARMMGR_RESULT_SUCCESS != ret)
164                 ERR("alarmmgr_set_timezone(%s) Fail", tzpath);
165
166         if (vconf_set_str(iface->vconf_key, timezone)) {
167                 ERR("vconf_set_str(%s) Fail", iface->vconf_key);
168                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
169         }
170
171         return SYSTEM_SETTINGS_ERROR_NONE;
172 }