Add include stdlib.h for free()
[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                 free(locale);
47                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
48         }
49
50         *value = strdup(country);
51         free(locale);
52
53         return SYSTEM_SETTINGS_ERROR_NONE;
54 }
55
56 int sst_locale_set_country(sst_interface *iface, const char *country)
57 {
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, char **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, const char *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, bool *value)
96 {
97         int time_fmt;
98
99         if (vconf_get_int(iface->vconf_key, &time_fmt)) {
100                 ERR("vconf_get_int(%s) Fail", iface->vconf_key);
101                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
102         }
103
104         bool result = true;
105         if (time_fmt == VCONFKEY_TIME_FORMAT_12)
106                 result = false;
107         else if (time_fmt == VCONFKEY_TIME_FORMAT_24)
108                 result = true;
109
110         *value = result;
111
112         return SYSTEM_SETTINGS_ERROR_NONE;
113 }
114
115 int sst_locale_set_timeformat_24hour(sst_interface *iface, bool is_24hour)
116 {
117         int vconf_val;
118
119         if (is_24hour)
120                 vconf_val = VCONFKEY_TIME_FORMAT_24;
121         else
122                 vconf_val = VCONFKEY_TIME_FORMAT_12;
123
124         if (vconf_set_int(iface->vconf_key, vconf_val)) {
125                 ERR("vconf_set_int(%s) Fail", iface->vconf_key);
126                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
127         }
128
129         return SYSTEM_SETTINGS_ERROR_NONE;
130 }
131
132 int sst_locale_get_timezone(sst_interface *iface, char **value)
133 {
134         char tzpath[PATH_MAX];
135         ssize_t len = readlink(SETTING_TZONE_SYMLINK_PATH, tzpath, sizeof(tzpath) - 1);
136         if (-1 == len) {
137                 ERR("readlink(%s) Fail(%d)", SETTING_TZONE_SYMLINK_PATH, errno);
138                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
139         }
140         tzpath[len] = '\0';
141
142         // for including last '/', it used sizeof instead of strlen
143         int start = sizeof(SETTING_TIME_ZONEINFO_PATH);
144
145         *value = strdup(&tzpath[start]);
146         return SYSTEM_SETTINGS_ERROR_NONE;
147 }
148
149 int sst_locale_set_timezone(sst_interface *iface, const char *timezone)
150 {
151         char tzpath[PATH_MAX];
152         snprintf(tzpath, sizeof(tzpath), SETTING_TIME_ZONEINFO_PATH"/%s", timezone);
153
154         if (false == sst_misc_file_exists(tzpath)) {
155                 ERR("sst_misc_file_exists(%s) Fail", tzpath);
156                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
157         }
158
159         int ret = alarmmgr_set_timezone(tzpath);
160         if (ALARMMGR_RESULT_SUCCESS != ret)
161                 ERR("alarmmgr_set_timezone(%s) Fail", tzpath);
162
163         if (vconf_set_str(iface->vconf_key, timezone)) {
164                 ERR("vconf_set_str(%s) Fail", iface->vconf_key);
165                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
166         }
167
168         return SYSTEM_SETTINGS_ERROR_NONE;
169 }
170
171 //TODO : checking this function is being used or not.
172 int sst_time_get_changed(sst_interface *iface, int *value)
173 {
174         time_t cur_tick;
175
176         cur_tick = time(NULL);
177         *value = cur_tick;
178
179         return SYSTEM_SETTINGS_ERROR_NONE;
180 }