Apply coding rule
[platform/core/security/krate.git] / tools / apps / setup-wizard / src / util.c
1 /*
2  * Tizen Krate Setup-Wizard 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 <notification.h>
20 #include "krate-setup.h"
21
22 #define KRATE_METADATA_PATH "data/KrateManifest.xml"
23
24 static char *__get_krate_metadata(void)
25 {
26         FILE *fp = NULL;
27         char *res_path = NULL;
28         char *metadata = NULL;
29         char metadata_path[PATH_MAX] = "\0";
30         long fsize = 0;
31         int ret = -1;
32
33         res_path = app_get_resource_path();
34         if (res_path == NULL) {
35                 dlog_print(DLOG_ERROR, LOG_TAG, "failed to get resource path");
36                 return NULL;
37         }
38         snprintf(metadata_path, PATH_MAX, "%s%s", res_path, KRATE_METADATA_PATH);
39         free(res_path);
40
41         fp = fopen(metadata_path, "r");
42         if (fp != NULL) {
43                 if (fseek(fp, 0, SEEK_END) == -1)
44                         dlog_print(DLOG_ERROR, LOG_TAG, "failed to fseek");
45
46                 fsize = ftell(fp);
47                 if (fsize == -1) {
48                         dlog_print(DLOG_ERROR, LOG_TAG, "failed to get file size");
49                         fclose(fp);
50                         return NULL;
51                 }
52
53                 metadata = malloc(fsize + 1);
54                 if (metadata == NULL) {
55                         dlog_print(DLOG_ERROR, LOG_TAG, "failed to allocate memory");
56                         fclose(fp);
57                         return NULL;
58                 }
59                 memset(metadata, 0, fsize + 1);
60                 if (fseek(fp, 0, SEEK_SET) == -1) {
61                         dlog_print(DLOG_ERROR, LOG_TAG, "failed to fseek");
62                         fclose(fp);
63                         free(metadata);
64                         return NULL;
65                 }
66
67                 ret = fread(metadata, fsize, 1, fp);
68                 if (ret < 0) {
69                         dlog_print(DLOG_ERROR, LOG_TAG, "failed to read metadata file");
70                         fclose(fp);
71                         free(metadata);
72                         return NULL;
73                 }
74         } else {
75                 dlog_print(DLOG_ERROR, LOG_TAG, "failed to open metadata file");
76                 return NULL;
77         }
78
79         fclose(fp);
80
81         return metadata;
82 }
83
84 int _send_krate_create_request(appdata_s *ad)
85 {
86         int ret;
87         char *metadata = NULL;
88
89         metadata = __get_krate_metadata();
90         if (metadata == NULL) {
91                 dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get krate metadata");
92                 return -1;
93         }
94
95         ret = krate_manager_create_krate(ad->krate_manager, ad->krate_name, metadata);
96         if (ret != KRATE_ERROR_NONE) {
97                 free(metadata);
98                 return -1;
99         }
100
101         free(metadata);
102         return 0;
103 }
104
105 int _send_krate_remove_request(appdata_s *ad)
106 {
107         int ret;
108
109         ret = krate_manager_destroy_krate(ad->krate_manager, ad->krate_name);
110         if (ret != KRATE_ERROR_NONE) {
111                 return -1;
112         }
113
114         return 0;
115 }
116
117 static int __set_notification(notification_h noti_handle, app_control_h app_control)
118 {
119         int ret = 0;
120         char *mode = NULL;
121         char *noti_text[2][2] = {
122                 {"IDS_TPLATFORM_HEADER_SEPARATE_ZONE_ABB", "IDS_TPLATFORM_SBODY_TAP_HERE_TO_CREATE_PS_ABB"},
123                 {"IDS_TPLATFORM_HEADER_SEPARATE_ZONE_ABB", "IDS_TPLATFORM_SBODY_TAP_HERE_TO_DELETE_PS_ABB"}
124         };
125         char **text = NULL;
126         char noti_content_text[PATH_MAX] = "";
127         char *krate_name = NULL;
128
129         if (app_control_get_extra_data(app_control, "mode", &mode) != APP_CONTROL_ERROR_NONE)
130                 return -1;
131
132         if (!strcmp(mode, "create"))
133                 text = noti_text[0];
134         else
135                 text = noti_text[1];
136
137         ret = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_TITLE, __(text[0]), NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
138         if (ret != NOTIFICATION_ERROR_NONE)
139                 return -1;
140
141         ret = app_control_get_extra_data(app_control, "krate", &krate_name);
142         if (ret != APP_CONTROL_ERROR_NONE)
143                 return -1;
144
145         snprintf(noti_content_text, PATH_MAX, __(text[1]), krate_name);
146
147         ret = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_CONTENT, noti_content_text, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
148         if (ret != NOTIFICATION_ERROR_NONE)
149                 return -1;
150
151         ret = notification_set_display_applist(noti_handle, NOTIFICATION_DISPLAY_APP_ALL);
152         if (ret != NOTIFICATION_ERROR_NONE)
153                 return -1;
154
155         ret = notification_set_image(noti_handle, NOTIFICATION_IMAGE_TYPE_THUMBNAIL, DPM_SYSPOPUP_ICON_PATH);
156         if (ret != NOTIFICATION_ERROR_NONE)
157                 return -1;
158
159         ret = notification_set_launch_option(noti_handle, NOTIFICATION_LAUNCH_OPTION_APP_CONTROL, app_control);
160         if (ret != NOTIFICATION_ERROR_NONE)
161                 return -1;
162
163         return ret;
164 }
165
166 void _create_notification(app_control_h app_control)
167 {
168         notification_h noti_handle = NULL;
169         int ret = 0;
170
171         noti_handle = notification_create(NOTIFICATION_TYPE_NOTI);
172
173         ret = __set_notification(noti_handle, app_control);
174         if (ret != NOTIFICATION_ERROR_NONE) {
175                 notification_free(noti_handle);
176                 app_control_destroy(app_control);
177                 return;
178         }
179
180         notification_post(noti_handle);
181         notification_free(noti_handle);
182         app_control_destroy(app_control);
183
184         return;
185 }