[refactoring] revise interface
[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_misc.h"
24 #include "sst_interface.h"
25
26 int sst_locale_get_country(sst_interface *iface, char **value)
27 {
28         char *locale = NULL;
29         if (sst_vconf_get_str_ally(iface->vconf_key, &locale)) {
30                 ERR("sst_vconf_get_str_ally(%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, const char *country)
56 {
57         const char *codeset = "UTF-8";
58
59         char locale[100];
60         snprintf(locale, sizeof(locale), "%s.%s", country, codeset);
61
62         if (vconf_set_str(iface->vconf_key, locale)) {
63                 ERR("vconf_set_str(%s) Fail", iface->vconf_key);
64                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
65         }
66
67         return SYSTEM_SETTINGS_ERROR_NONE;
68 }
69
70 int sst_locale_get_language(sst_interface *iface, char **value)
71 {
72         //It is same data with sst_locale_get_country()
73         int ret = sst_locale_get_country(iface, value);
74         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
75                 ERR("sst_locale_get_country() Fail(%d)", ret);
76                 return ret;
77         }
78
79         return SYSTEM_SETTINGS_ERROR_NONE;
80 }
81
82 int sst_locale_set_language(sst_interface *iface, const char *value)
83 {
84         //It is same data with sst_locale_set_country()
85         int ret = sst_locale_set_country(iface, value);
86         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
87                 ERR("sst_locale_set_country() Fail(%d)", ret);
88                 return ret;
89         }
90
91         return SYSTEM_SETTINGS_ERROR_NONE;
92 }
93
94 int sst_locale_get_timeformat_24hour(sst_interface *iface, bool *value)
95 {
96         int time_fmt;
97
98         if (vconf_get_int(iface->vconf_key, &time_fmt)) {
99                 ERR("vconf_get_int(%s) Fail", iface->vconf_key);
100                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
101         }
102
103         bool result = true;
104         if (time_fmt == VCONFKEY_TIME_FORMAT_12)
105                 result = false;
106         else if (time_fmt == VCONFKEY_TIME_FORMAT_24)
107                 result = true;
108
109         *value = result;
110
111         return SYSTEM_SETTINGS_ERROR_NONE;
112 }
113
114 int sst_locale_set_timeformat_24hour(sst_interface *iface, bool is_24hour)
115 {
116         int vconf_val;
117
118         if (is_24hour)
119                 vconf_val = VCONFKEY_TIME_FORMAT_24;
120         else
121                 vconf_val = VCONFKEY_TIME_FORMAT_12;
122
123         if (vconf_set_int(iface->vconf_key, vconf_val)) {
124                 ERR("vconf_set_int(%s) Fail", iface->vconf_key);
125                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
126         }
127
128         return SYSTEM_SETTINGS_ERROR_NONE;
129 }
130
131 int sst_locale_get_timezone(sst_interface *iface, char **value)
132 {
133         char tzpath[PATH_MAX];
134         ssize_t len = readlink(SETTING_TZONE_SYMLINK_PATH, tzpath, sizeof(tzpath) - 1);
135         if (-1 == len) {
136                 ERR("readlink(%s) Fail(%d)", SETTING_TZONE_SYMLINK_PATH, errno);
137                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
138         }
139         tzpath[len] = '\0';
140
141         // for including last '/', it used sizeof instead of strlen
142         int start = sizeof(SETTING_TIME_ZONEINFO_PATH);
143
144         *value = strdup(&tzpath[start]);
145         return SYSTEM_SETTINGS_ERROR_NONE;
146 }
147
148 int sst_locale_set_timezone(sst_interface *iface, const char *timezone)
149 {
150         char tzpath[PATH_MAX];
151         snprintf(tzpath, sizeof(tzpath), SETTING_TIME_ZONEINFO_PATH"/%s", timezone);
152
153         if (false == sst_misc_file_exists(tzpath)) {
154                 ERR("sst_misc_file_exists(%s) Fail", tzpath);
155                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
156         }
157
158         int ret = alarmmgr_set_timezone(tzpath);
159         if (ALARMMGR_RESULT_SUCCESS != ret)
160                 ERR("alarmmgr_set_timezone(%s) Fail", tzpath);
161
162         if (vconf_set_str(iface->vconf_key, timezone)) {
163                 ERR("vconf_set_str(%s) Fail", iface->vconf_key);
164                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
165         }
166
167         return SYSTEM_SETTINGS_ERROR_NONE;
168 }
169
170 //TODO : checking this function is being used or not.
171 int sst_time_get_changed(sst_interface *iface, int *value)
172 {
173         time_t cur_tick;
174
175         cur_tick = time(NULL);
176         *value = cur_tick;
177
178         return SYSTEM_SETTINGS_ERROR_NONE;
179 }