Remove trivial unnecessary build dependency
[apps/core/preloaded/lockscreen.git] / src / time_format.c
1 /*
2  * Copyright (c) 2016 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
17 #include "time_format.h"
18 #include "log.h"
19 #include <system_settings.h>
20 #include <stdlib.h>
21 #include <Ecore.h>
22
23 static bool use24hformat;
24 static char *locale, *tz_timezone;
25 static int init_count;
26 int LOCKSCREEN_EVENT_TIME_FORMAT_CHANGED;
27
28 static void _time_changed(system_settings_key_e key, void *user_data)
29 {
30         int ret = SYSTEM_SETTINGS_ERROR_NOT_SUPPORTED;
31
32         switch (key) {
33                 case SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR:
34                         ret = system_settings_get_value_bool(SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR, &use24hformat);
35                         break;
36                 case SYSTEM_SETTINGS_KEY_LOCALE_TIMEZONE:
37                         free(tz_timezone);
38                         ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_TIMEZONE, &tz_timezone);
39                         break;
40                 case SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE:
41                         free(locale);
42                         ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &locale);
43                         break;
44                 case SYSTEM_SETTINGS_KEY_TIME_CHANGED:
45                         ret = SYSTEM_SETTINGS_ERROR_NONE;
46                         break;
47                 default:
48                         ERR("Unhandled system_setting event: %d", key);
49                         break;
50         }
51
52         if (ret == SYSTEM_SETTINGS_ERROR_NONE) {
53                 ecore_event_add(LOCKSCREEN_EVENT_TIME_FORMAT_CHANGED, NULL, NULL, NULL);
54         }
55 }
56
57 int lockscreen_time_format_init(void)
58 {
59         if (!init_count) {
60                 LOCKSCREEN_EVENT_TIME_FORMAT_CHANGED = ecore_event_type_new();
61                 int ret = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR, _time_changed, NULL);
62                 if (ret != SYSTEM_SETTINGS_ERROR_NONE) {
63                         ERR("system_settings_set_changed_cb failed: %s", get_error_message(ret));
64                         return 1;
65                 }
66 #if 0
67                 ret = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_LOCALE_TIMEZONE, _time_changed, NULL);
68                 if (ret != SYSTEM_SETTINGS_ERROR_NONE) {
69                         ERR("system_settings_set_changed_cb failed: %s", get_error_message(ret));
70                         return 1;
71                 }
72 #endif
73                 ret = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_TIME_CHANGED, _time_changed, NULL);
74                 if (ret != SYSTEM_SETTINGS_ERROR_NONE) {
75                         ERR("system_settings_set_changed_cb failed: %s", get_error_message(ret));
76                         return 1;
77                 }
78                 ret = system_settings_get_value_bool(SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR, &use24hformat);
79
80                 if (ret != SYSTEM_SETTINGS_ERROR_NONE) {
81                         ERR("system_settings_get_value_bool failed: %s", get_error_message(ret));
82                         return 1;
83                 }
84
85                 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &locale);
86                 if (ret != SYSTEM_SETTINGS_ERROR_NONE) {
87                         ERR("system_settings_get_value_string failed: %s", get_error_message(ret));
88                         return 1;
89                 }
90
91                 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_TIMEZONE, &tz_timezone);
92                 if (ret != SYSTEM_SETTINGS_ERROR_NONE) {
93                         free(locale);
94                         ERR("system_settings_get_value_string failed: %s", get_error_message(ret));
95                         return 1;
96                 }
97         }
98
99         init_count++;
100         return 0;
101 }
102
103 void lockscreen_time_format_shutdown(void)
104 {
105         if (init_count) {
106                 init_count--;
107                 if (!init_count) {
108                         free(locale);
109                         free(tz_timezone);
110                         system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR);
111                         system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_LOCALE_TIMEZONE);
112                         system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE);
113                         system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_TIME_CHANGED);
114                         locale = tz_timezone = NULL;
115                 }
116         }
117 }
118
119 const char *lockscreen_time_format_locale_get(void)
120 {
121         return locale;
122 }
123
124 const char *lockscreen_time_format_timezone_get(void)
125 {
126         return tz_timezone;
127 }
128
129 bool lockscreen_time_format_use_24h(void)
130 {
131         return use24hformat;
132 }