2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "../sst_exclusive.h"
27 #include <tzplatform_config.h>
28 #include "system_settings.h"
31 #define WALLPAPER_MAX_COUNT 10
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";
36 static int _get_extended_wallpaper_num(const char *file, unsigned int *num)
39 const char *find_str = sst_ext_walpaper_prefix;
42 if (!(ch = strstr(file, find_str)))
43 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
45 strncpy(buf, file, ch - file);
47 sprintf(buf + (ch - file), "%s%s", "", ch + strlen(find_str));
49 if (!isdigit(buf[0])) {
50 ERR("%s is not number", buf);
51 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
56 return SYSTEM_SETTINGS_ERROR_NONE;
59 static int _copy_extended_wallpaper(const char *dest_path, const char *src_path)
63 RETV_IF(NULL == src_path, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
64 RETV_IF(NULL == dest_path, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
66 src = g_file_new_for_path(src_path);
67 dest = g_file_new_for_path(dest_path);
70 if (!g_file_copy(src, dest, G_FILE_MONITOR_NONE, NULL, NULL, NULL, &error)) {
71 ERR("g_file_copy() Fail(%s)", error->message);
78 if (chmod(dest_path, S_IRWXU | S_IRWXG | S_IRWXO) < 0)
79 ERR("chmod(%s) Fail(%d)", dest_path, errno);
81 return SYSTEM_SETTINGS_ERROR_NONE;
84 static int _remove_oldest_extended_wallpaper()
88 char *min_image_name = NULL;
89 unsigned int min_image_num = UINT_MAX;
90 unsigned int cur_num = 0;
92 dir = opendir(sst_ext_walpaper_dir);
94 ERR("opendir(%s) Fail(%d)", sst_ext_walpaper_dir, errno);
95 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
98 while ((entry = readdir(dir))) {
99 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
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);
106 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
109 if (cur_num < min_image_num) {
110 min_image_num = cur_num;
111 min_image_name = entry->d_name;
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;
124 return SYSTEM_SETTINGS_ERROR_NONE;
127 static bool _check_extended_wallpaper(const char *file_path)
131 RETV_IF(NULL == file_path, false);
132 RETV_IF('\0' == *file_path, false);
134 snprintf(buf, sizeof(buf), "%s/.bgwallpaper", tzplatform_getenv(TZ_USER_CONTENT));
135 return (strstr(file_path, buf) != NULL);
138 int sst_excl_set_wallpaper(const char *key, const char *val)
142 struct dirent *entry;
143 unsigned int max_image_num = 0;
144 unsigned int cur_num = 0;
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;
153 return SYSTEM_SETTINGS_ERROR_NONE;
156 dir = opendir(sst_ext_walpaper_dir);
158 ERR("opendir(%s) Fail(%d)", sst_ext_walpaper_dir, errno);
159 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
162 /* Check a max number of wallpapers */
163 while ((entry = readdir(dir))) {
164 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
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);
171 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
174 if (max_image_num < cur_num)
175 max_image_num = cur_num;
179 /* Numbering rule: Gear is odd number */
180 max_image_num += (max_image_num % 2 == 0) ? 1 : 2;
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);
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;
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;
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;
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;
211 return SYSTEM_SETTINGS_ERROR_NONE;