Fix to check SYSTEM_SETTINGS_KEY_AUTOMATIC_TIME_UPDATE feature
[platform/core/api/system-settings.git] / src / wearable_excl / sst_wearable.c
1 /*
2  * Copyright (c) 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_exclusive.h"
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <ctype.h>
21 #include <fcntl.h>
22 #include <limits.h>
23 #include <dirent.h>
24 #include <sys/stat.h>
25 #include <gio/gio.h>
26 #include <vconf.h>
27 #include <tzplatform_config.h>
28 #include "system_settings.h"
29 #include "sst.h"
30
31 #define WALLPAPER_MAX_COUNT 10
32
33 static const char* const sst_ext_walpaper_prefix = "extended_wallpaper_";
34 static const char* const sst_ext_walpaper_dir = _TZ_SYS_DATA"/setting/wallpaper";
35
36 static int _get_extended_wallpaper_num(const char *file, unsigned int *num)
37 {
38         char buf[256];
39         const char *find_str = sst_ext_walpaper_prefix;
40         char *ch;
41
42         if (!(ch = strstr(file, find_str)))
43                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
44
45         strncpy(buf, file, ch - file);
46         buf[ch - file] = 0;
47         sprintf(buf + (ch - file), "%s%s", "", ch + strlen(find_str));
48
49         if (!isdigit(buf[0])) {
50                 ERR("%s is not number", buf);
51                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
52         }
53
54         *num = atoi(buf);
55
56         return SYSTEM_SETTINGS_ERROR_NONE;
57 }
58
59 static int _copy_extended_wallpaper(const char *dest_path, const char *src_path)
60 {
61         GFile *src, *dest;
62
63         RETV_IF(NULL == src_path, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
64         RETV_IF(NULL == dest_path, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
65
66         src = g_file_new_for_path(src_path);
67         dest = g_file_new_for_path(dest_path);
68
69         GError *error = NULL;
70         if (!g_file_copy(src, dest, G_FILE_MONITOR_NONE, NULL, NULL, NULL, &error)) {
71                 ERR("g_file_copy() Fail(%s)", error->message);
72                 g_error_free(error);
73         }
74
75         g_object_unref(dest);
76         g_object_unref(src);
77
78         if (chmod(dest_path, S_IRWXU | S_IRWXG | S_IRWXO) < 0)
79                 ERR("chmod(%s) Fail(%d)", dest_path, errno);
80
81         return SYSTEM_SETTINGS_ERROR_NONE;
82 }
83
84 static int _remove_oldest_extended_wallpaper()
85 {
86         DIR *dir;
87         struct dirent *entry;
88         char *min_image_name = NULL;
89         unsigned int min_image_num = UINT_MAX;
90         unsigned int cur_num = 0;
91
92         dir = opendir(sst_ext_walpaper_dir);
93         if (NULL == dir) {
94                 ERR("opendir(%s) Fail(%d)", sst_ext_walpaper_dir, errno);
95                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
96         }
97
98         while ((entry = readdir(dir))) {
99                 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
100                         continue;
101
102                 int ret = _get_extended_wallpaper_num(entry->d_name, &cur_num);
103                 if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
104                         ERR("_get_extended_wallpaper_num(%s) Fail(%d)", entry->d_name, ret);
105                         closedir(dir);
106                         return SYSTEM_SETTINGS_ERROR_IO_ERROR;
107                 }
108
109                 if (cur_num < min_image_num) {
110                         min_image_num = cur_num;
111                         min_image_name = entry->d_name;
112                 }
113         }
114
115         char buf[PATH_MAX];
116         if (min_image_name) {
117                 snprintf(buf, sizeof(buf) - 1, "%s/%s", sst_ext_walpaper_dir, min_image_name);
118                 if (remove(buf) < 0) {
119                         ERR("remove(%s) Fail(%d)", buf, errno);
120                         return SYSTEM_SETTINGS_ERROR_IO_ERROR;
121                 }
122         }
123
124         return SYSTEM_SETTINGS_ERROR_NONE;
125 }
126
127 static bool _check_extended_wallpaper(const char *file_path)
128 {
129         char buf[PATH_MAX];
130
131         RETV_IF(NULL == file_path, false);
132         RETV_IF('\0' == *file_path, false);
133
134         snprintf(buf, sizeof(buf), "%s/.bgwallpaper", tzplatform_getenv(TZ_USER_CONTENT));
135         return (strstr(file_path, buf) != NULL);
136 }
137
138 int sst_excl_set_wallpaper(const char *key, const char *val)
139 {
140         int ret;
141         DIR *dir;
142         struct dirent *entry;
143         unsigned int max_image_num = 0;
144         unsigned int cur_num = 0;
145         int image_count = 0;
146
147         if (false == _check_extended_wallpaper(val)) {
148                 if (vconf_set_str(key, val)) {
149                         ERR("vconf_set_str(%s) Fail", key);
150                         return SYSTEM_SETTINGS_ERROR_IO_ERROR;
151                 }
152
153                 return SYSTEM_SETTINGS_ERROR_NONE;
154         }
155
156         dir = opendir(sst_ext_walpaper_dir);
157         if (NULL == dir) {
158                 ERR("opendir(%s) Fail(%d)", sst_ext_walpaper_dir, errno);
159                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
160         }
161
162         /* Check a max number of wallpapers */
163         while ((entry = readdir(dir))) {
164                 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
165                         continue;
166
167                 ret = _get_extended_wallpaper_num(entry->d_name, &cur_num);
168                 if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
169                         ERR("_get_extended_wallpaper_num(%s) Fail(%d)", entry->d_name, ret);
170                         closedir(dir);
171                         return SYSTEM_SETTINGS_ERROR_IO_ERROR;
172                 }
173
174                 if (max_image_num < cur_num)
175                         max_image_num = cur_num;
176         }
177         closedir(dir);
178
179         /* Numbering rule: Gear is odd number */
180         max_image_num += (max_image_num % 2 == 0) ? 1 : 2;
181
182         char file_name_buffer[PATH_MAX];
183         snprintf(file_name_buffer, sizeof(file_name_buffer) - 1, "%s/%s%d.jpg",
184                 sst_ext_walpaper_dir, sst_ext_walpaper_prefix, max_image_num);
185
186         ret = _copy_extended_wallpaper(file_name_buffer, val);
187         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
188                 ERR("_copy_extended_wallpaper(%s, %s) Fail", file_name_buffer, val);
189                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
190         }
191
192         if (WALLPAPER_MAX_COUNT <= image_count) {
193                 ret = _remove_oldest_extended_wallpaper();
194                 if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
195                         ERR("_remove_oldest_extended_wallpaper() Fail(%d)", ret);
196                         remove(file_name_buffer);
197                         return SYSTEM_SETTINGS_ERROR_IO_ERROR;
198                 }
199         }
200
201         if (vconf_set_str(key, file_name_buffer)) {
202                 ERR("vconf_set_str(%s, %s) Fail", key, file_name_buffer);
203                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
204         }
205
206         const char *noti_key = VCONFKEY_SETAPPL_WALLPAPER_CHANGED_NOTI_INT;
207         if (vconf_set_int(noti_key, VCONFKEY_WALLPAPER_CHANGED_NOTI_GEAR)) {
208                 ERR("vconf_set_int(%s, %d) Fail", noti_key, VCONFKEY_WALLPAPER_CHANGED_NOTI_GEAR);
209                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
210         }
211         return SYSTEM_SETTINGS_ERROR_NONE;
212 }