[refactoring] reduce length of value name and log
[platform/core/api/system-settings.git] / src / sst_screen.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_screen.h"
17
18 #include <dlfcn.h>
19 #include <pkgmgr-info.h>
20 #include <vconf.h>
21 #include "sst.h"
22 #include "sst_vconf.h"
23 #include "sst_utils.h"
24 #include "sst_exclusive.h"
25
26 int sst_screen_set_backlight_time(sst_interface *iface, void *value)
27 {
28         int *vconf_value = *(int **)value;
29
30         if (*vconf_value <= 0 || 600 < *vconf_value) {
31                 ERR("Invalid Value(%d)", *vconf_value);
32                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
33         }
34
35         if (vconf_set_int(iface->vconf_key, *vconf_value)) {
36                 ERR("vconf_set_int(%s, %d) Fail", iface->vconf_key, *vconf_value);
37                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
38         }
39
40         return SYSTEM_SETTINGS_ERROR_NONE;
41 }
42
43 static bool _screen_is_valid_img(char *path)
44 {
45         void *handle = NULL;
46         char *error;
47         bool ret = false;
48         bool (*image_type_check)(char *path);
49
50         handle = dlopen(SETTING_UTILS_SO_FILE_PATH, RTLD_LAZY | RTLD_GLOBAL);
51         if (NULL == handle) {
52                 ERR("dlopen(%s) Fail(%d)", SETTING_UTILS_SO_FILE_PATH, errno);
53                 return false;
54         }
55
56         image_type_check = dlsym(handle, "sstu_is_valid_image");
57         if ((error = dlerror()) != NULL) {
58                 ERR("dlsym(sstu_is_valid_image) Fail(%s)", error);
59                 dlclose(handle);
60                 return false;
61         }
62
63         ret = image_type_check(path);
64         dlclose(handle);
65         return ret;
66 }
67
68 int sst_screen_set_home_wallpaper(sst_interface *iface, void *value)
69 {
70         char *vconf_value = value;
71
72         bool valid = _screen_is_valid_img(vconf_value);
73         if (false == valid) {
74                 ERR("Invalid image file(%s)", vconf_value);
75                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
76         }
77
78         return sst_excl_set_wallpaper(iface->vconf_key, vconf_value);
79 }
80
81 int sst_screen_set_lock_wallpaper(sst_interface *iface, void *value)
82 {
83         char *vconf_value = value;
84
85         bool valid = _screen_is_valid_img(vconf_value);
86         if (false == valid) {
87                 ERR("Invalid image file(%s)", vconf_value);
88                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
89         }
90
91         if (vconf_set_str(iface->vconf_key, vconf_value)) {
92                 ERR("vconf_set_str(%s, %s) Fail", iface->vconf_key, vconf_value);
93                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
94         }
95
96         return SYSTEM_SETTINGS_ERROR_NONE;
97 }
98
99 static int _category_func(const char *name, void *user_data)
100 {
101         const char *category = "lock-screen";
102         bool *found = user_data;
103
104         RETV_IF(NULL == name, PMINFO_R_OK);
105
106         if (SST_EQUAL == strcmp(name, category)) {
107                 *found = true;
108                 return PMINFO_R_ERROR; //not Error. It stops iteration.
109         } else {
110                 DBG("Unexpeced category(%s)", name);
111                 return PMINFO_R_OK;
112         }
113
114         return PMINFO_R_OK;
115 }
116
117 /**
118  *
119  * set 'swipe type' if current lockscreen app is 'com.samsung.lockscreen'
120  *
121  */
122 int sst_screen_set_lockscreen(sst_interface *iface, void *value)
123 {
124         int ret;
125         char *app_id = value; //ex) com.samsung.lockscreen
126
127         RETV_IF(NULL == value, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
128
129         pkgmgrinfo_appinfo_h handle = NULL;
130         ret = pkgmgrinfo_appinfo_get_appinfo(app_id, &handle);
131         if (PMINFO_R_OK != ret) {
132                 ERR("pkgmgrinfo_appinfo_get_appinfo(%s) Fail(%d)", app_id, ret);
133                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
134         }
135
136         bool is_lock_screen = false;
137         ret = pkgmgrinfo_appinfo_foreach_category(handle, _category_func, &is_lock_screen);
138         if (ret != PMINFO_R_OK) {
139                 ERR("pkgmgrinfo_appinfo_foreach_category() Fail(%d)", ret);
140                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
141                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
142         }
143         pkgmgrinfo_appinfo_destroy_appinfo(handle);
144
145         if (false == is_lock_screen) {
146                 ERR("Invalid Application(%s)", app_id);
147                 //Todo: It should be enable.
148                 //return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
149         }
150
151         int locktype = -1;
152         if (vconf_get_int(VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT, &locktype)) {
153                 ERR("vconf_get_int(lock_type) Fail");
154                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
155         }
156
157         if (locktype == SETTING_SCREEN_LOCK_TYPE_PASSWORD) {
158                 ERR("Unsupported lock type");
159                 return SYSTEM_SETTINGS_ERROR_LOCKSCREEN_APP_PASSWORD_MODE;
160         }
161
162         if (vconf_set_str(VCONFKEY_SETAPPL_3RD_LOCK_PKG_NAME_STR, app_id)) {
163                 ERR("vconf_set_str(3rd lock pkg) Fail");
164                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
165         }
166
167         if (SST_EQUAL == strcmp(app_id, "com.samsung.lockscreen")) {
168                 const char *lock_type_key = VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT;
169                 if (vconf_set_int(lock_type_key, SETTING_SCREEN_LOCK_TYPE_SWIPE)) {
170                         ERR("vconf_set_int(%s) Fail", lock_type_key);
171                         return SYSTEM_SETTINGS_ERROR_IO_ERROR;
172                 }
173         }
174         return SYSTEM_SETTINGS_ERROR_NONE;
175 }
176
177 int sst_screen_get_lockscreen(sst_interface *iface, void **value)
178 {
179         char *pkg_name = NULL;
180
181         int locktype = -1;
182         if (vconf_get_int(VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT, &locktype)) {
183                 ERR("vconf_get_int(lock_type) Fail");
184                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
185         }
186
187         if (sst_vconf_get_string(iface->vconf_key, &pkg_name)) {
188                 ERR("sst_vconf_get_string(%s) Fail", iface->vconf_key);
189                 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
190         }
191
192         if (SST_EQUAL == strcmp(pkg_name, "com.samsung.lockscreen")
193                         && locktype == SETTING_SCREEN_LOCK_TYPE_PASSWORD) {
194                 //if current lock type is 'password', return SYSTEM_SETTINGS_ERROR_LOCKSCREEN_APP_PASSWORD_MODE
195                 free(pkg_name);
196                 return SYSTEM_SETTINGS_ERROR_LOCKSCREEN_APP_PASSWORD_MODE;
197         }
198
199         *value = pkg_name;
200         return SYSTEM_SETTINGS_ERROR_NONE;
201 }