Add to remove the shortcut when container is removed
[platform/core/security/krate.git] / tools / apps / keyguard / src / main.c
1 /*
2  * Tizen Krate Keyguard application
3  *
4  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #include "keyguard.h"
20 #include "widget.h"
21
22 static app_control_h __req_app_control;
23
24 void _launch_req_app()
25 {
26         app_control_send_launch_request(__req_app_control, NULL, NULL);
27         app_control_destroy(__req_app_control);
28 }
29
30 bool _check_password(const char* password)
31 {
32         unsigned int attempt, max_attempt, expire_sec;
33         int ret;
34
35         ret = auth_passwd_check_passwd(AUTH_PWD_NORMAL, password, &attempt, &max_attempt, &expire_sec);
36
37         return ret == AUTH_PASSWD_API_SUCCESS;
38 }
39
40 bool _has_password()
41 {
42         unsigned int attempt, max_attempt, expire_sec;
43         int ret;
44
45         ret = auth_passwd_check_passwd_state(AUTH_PWD_NORMAL, &attempt, &max_attempt, &expire_sec);
46
47         return ret != AUTH_PASSWD_API_ERROR_NO_PASSWORD;
48 }
49
50 unsigned int _get_left_attempts()
51 {
52         unsigned int attempt = 0, max_attempt = 0, expire_sec;
53
54                 auth_passwd_check_passwd_state(AUTH_PWD_NORMAL, &attempt, &max_attempt, &expire_sec);
55
56         if (max_attempt == 0) {
57                 return 0xffffffff;
58         }
59
60         return max_attempt - attempt;
61 }
62
63 static void __launch_krate_app(const char* krate_name, app_control_h app_control)
64 {
65         krate_manager_h krate_mgr;
66         krate_app_proxy_h krate_app;
67
68         krate_manager_create(&krate_mgr);
69         krate_app_proxy_create(krate_mgr, krate_name, &krate_app);
70         krate_app_proxy_send_launch_request(krate_app, app_control);
71         krate_app_proxy_destroy(krate_app);
72         krate_manager_destroy(krate_mgr);
73 }
74
75 static void __add_shortcut(const char* krate_name)
76 {
77         char uri[PATH_MAX];
78
79         snprintf(uri, sizeof(uri), "krate://enter/%s", krate_name);
80         shortcut_add_to_home(krate_name, LAUNCH_BY_URI, uri, ICON_PATH "/shortcut_icon.png", 0, NULL, NULL);
81 }
82
83 static void __remove_shortcut(const char* krate_name)
84 {
85         shortcut_remove_from_home(krate_name, NULL, NULL);
86 }
87
88 static bool __app_create(void *data)
89 {
90         return true;
91 }
92
93 static void __app_pause(void *data)
94 {
95         return;
96 }
97
98 static void __app_resume(void *data)
99 {
100         return;
101 }
102
103 static void __app_terminate(void *data)
104 {
105         return ;
106 }
107
108 static void __app_control(app_control_h app_control, void *data)
109 {
110         char* uri, *tmp;
111         int ret = 0;
112
113         ret = app_control_get_uri(app_control, &uri);
114         if (ret != APP_CONTROL_ERROR_NONE) {
115                 dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get URI");
116                 ui_app_exit();
117                 return;
118         }
119
120         if (strncmp(uri, "krate://", sizeof("krate://") - 1) != 0) {
121                 dlog_print(DLOG_ERROR, LOG_TAG, "Mismatched URI");
122                 free(uri);
123                 ui_app_exit();
124                 return;
125         }
126
127         tmp = uri + sizeof("krate://") - 1;
128
129         if (strncmp(tmp, "setup/", sizeof("setup/") - 1) == 0) {
130                 char *krate_name;
131
132                 krate_name = tmp + sizeof("setup/") - 1;
133                 __add_shortcut(krate_name);
134                 ui_app_exit();
135                 return;
136         } else if (strncmp(tmp, "cleanup/", sizeof("cleanup/") - 1) == 0) {
137                 char *krate_name;
138
139                 krate_name = tmp + sizeof("cleanup/") - 1;
140                 __remove_shortcut(krate_name);
141                 ui_app_exit();
142                 return;
143         } else if (strncmp(tmp, "enter/", sizeof("enter/") - 1) == 0) {
144                 char* krate_name, *launch_parameter;
145                 char new_uri[PATH_MAX];
146
147                 krate_name = tmp + sizeof("enter/") - 1;
148                 launch_parameter = strchr(krate_name, '/');
149                 if (launch_parameter != NULL) {
150                         *(launch_parameter++) = '\0';
151                         if (launch_parameter[0] == '\0') {
152                                 launch_parameter = KASKIT_PACKAGE;
153                         }
154                 } else {
155                         launch_parameter = KASKIT_PACKAGE;
156                 }
157                 snprintf(new_uri, PATH_MAX, "krate://launch/%s", launch_parameter);
158                 app_control_set_uri(app_control, new_uri);
159                 app_control_set_app_id(app_control, PACKAGE);
160
161                 dlog_print(DLOG_ERROR, LOG_TAG, PACKAGE);
162                 dlog_print(DLOG_ERROR, LOG_TAG, new_uri);
163                 dlog_print(DLOG_ERROR, LOG_TAG, krate_name);
164
165                 __launch_krate_app(krate_name, app_control);
166                 ui_app_exit();
167                 return;
168         } else if (strncmp(tmp, "launch/", sizeof("launch/") - 1) == 0) {
169                 char* app_id;
170
171                 app_id = tmp + sizeof("launch/") - 1;
172                 uri = strchr(app_id, '?');
173                 if (uri != NULL) {
174                         *(uri++) = '\0';
175                         if (strncmp(uri, "uri=", sizeof("uri=") - 1) == 0) {
176                                 tmp += sizeof("uri=") - 1;
177                         }
178                 }
179                 app_control_clone(&__req_app_control, app_control);
180                 app_control_set_uri(__req_app_control, uri);
181                 app_control_set_app_id(__req_app_control, app_id);
182
183                 if (_has_password()) {
184                         _create_keyguard_window();
185                 } else {
186                         _launch_req_app();
187                 }
188                 return;
189         } else {
190                 dlog_print(DLOG_ERROR, LOG_TAG, "Invalid URI");
191                 ui_app_exit();
192         }
193         free(uri);
194 }
195
196 int main(int argc, char *argv[])
197 {
198         int ret = 0;
199
200         ui_app_lifecycle_callback_s event_callback = {0, };
201
202         event_callback.create = __app_create;
203         event_callback.terminate = __app_terminate;
204         event_callback.pause = __app_pause;
205         event_callback.resume = __app_resume;
206         event_callback.app_control = __app_control;
207
208         ret = ui_app_main(argc, argv, &event_callback, NULL);
209         if (ret != APP_ERROR_NONE)
210                 dlog_print(DLOG_ERROR, LOG_TAG, "ui_app_main is failed. err = %d", ret);
211
212         return ret;
213 }