Add new functions to handling running components
[platform/core/appfw/aul-1.git] / include / menu_db_util.h
1 /*
2  * Copyright (c) 2000 - 2015 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
17 #pragma once
18
19 #include <pkgmgr-info.h>
20 #include <string.h>
21 #include <stdio.h>
22
23 #include "aul_util.h"
24
25 #define MAX_PATH_LEN    1024
26
27 #define AUL_APP_INFO_FLD_PKG_NAME               "package"
28 #define AUL_APP_INFO_FLD_APP_PATH               "exec"
29 #define AUL_APP_INFO_FLD_APP_TYPE               "x_slp_packagetype"
30 #define AUL_APP_INFO_FLD_WIDTH                  "x_slp_baselayoutwidth"
31 #define AUL_APP_INFO_FLD_HEIGHT                 "x_slp_baselayoutheight"
32 #define AUL_APP_INFO_FLD_VERTICAL               "x_slp_ishorizontalscale"
33 #define AUL_APP_INFO_FLD_MULTIPLE               "x_slp_multiple"
34 #define AUL_APP_INFO_FLD_TASK_MANAGE    "x_slp_taskmanage"
35 #define AUL_APP_INFO_FLD_MIMETYPE               "mimetype"
36 #define AUL_APP_INFO_FLD_SERVICE                "x_slp_service"
37
38 #define AUL_RETRIEVE_PKG_NAME                   "package = '?'"
39 #define AUL_RETRIEVE_APP_PATH                   "exec = '?'"
40 #define AUL_RETRIEVE_MIMETYPE                   "mimetype like '?'"
41 #define AUL_RETRIEVE_SERVICE                    "x_slp_service like '?'"
42
43 typedef struct {
44         char *appid;            /* appid */
45         char *app_path;         /* exec */
46         char *original_app_path;        /* exec */
47         char *pkg_type;         /* x_slp_packagetype */
48         char *hwacc;            /* hwacceleration */
49         char *pkg_id;
50 } app_info_from_db;
51
52 static inline char *_get_appid(app_info_from_db *menu_info)
53 {
54         return menu_info ? menu_info->appid : NULL;
55 }
56
57 static inline char *_get_pkgid(app_info_from_db *menu_info)
58 {
59         return menu_info ? menu_info->pkg_id : NULL;
60 }
61
62 static inline char *_get_app_path(app_info_from_db *menu_info)
63 {
64         int i = 0;
65         int path_len = -1;
66
67         if (!menu_info || menu_info->app_path == NULL)
68                 return NULL;
69
70         while (menu_info->app_path[i] != 0) {
71                 if (menu_info->app_path[i] == ' '
72                     || menu_info->app_path[i] == '\t') {
73                         path_len = i;
74                         break;
75                 }
76                 i++;
77         }
78
79         if (path_len == 0) {
80                 free(menu_info->app_path);
81                 menu_info->app_path = NULL;
82         } else if (path_len > 0) {
83                 char *tmp_app_path = malloc(sizeof(char) * (path_len + 1));
84                 if (tmp_app_path == NULL)
85                         return NULL;
86                 snprintf(tmp_app_path, path_len + 1, "%s", menu_info->app_path);
87                 free(menu_info->app_path);
88                 menu_info->app_path = tmp_app_path;
89         }
90
91         return menu_info->app_path;
92 }
93
94 static inline char *_get_original_app_path(app_info_from_db *menu_info)
95 {
96         return menu_info ? menu_info->original_app_path : NULL;
97 }
98
99 static inline void _free_app_info_from_db(app_info_from_db *menu_info)
100 {
101         if (menu_info != NULL) {
102                 if (menu_info->appid != NULL)
103                         free(menu_info->appid);
104                 if (menu_info->app_path != NULL)
105                         free(menu_info->app_path);
106                 if (menu_info->original_app_path != NULL)
107                         free(menu_info->original_app_path);
108                 if (menu_info->pkg_type != NULL)
109                         free(menu_info->pkg_type);
110                 if (menu_info->hwacc != NULL)
111                         free(menu_info->hwacc);
112                 if (menu_info->pkg_id != NULL)
113                         free(menu_info->pkg_id);
114                 free(menu_info);
115         }
116 }
117
118 static inline app_info_from_db *_get_app_info_from_db_by_pkgname(
119                                                         const char *appid)
120 {
121         app_info_from_db *menu_info = NULL;
122         pkgmgrinfo_appinfo_h handle = NULL;
123         int ret = PMINFO_R_OK;
124         char *exec = NULL;
125         char *apptype = NULL;
126
127         menu_info = calloc(1, sizeof(app_info_from_db));
128         if (menu_info == NULL)
129                 return NULL;
130
131         if (appid == NULL) {
132                 _free_app_info_from_db(menu_info);
133                 return NULL;
134         }
135
136
137         if (getuid() != GLOBAL_USER)
138                 ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, getuid(), &handle);
139         else
140                 ret = pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
141
142         if (ret != PMINFO_R_OK) {
143                 _free_app_info_from_db(menu_info);
144                 return NULL;
145         }
146
147         menu_info->appid = strdup(appid);
148
149         ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
150         if (ret != PMINFO_R_OK)
151                 _E("fail to get exec from appinfo handle");
152
153         if (exec)
154                 menu_info->app_path = strdup(exec);
155
156         if (menu_info->app_path != NULL)
157                 menu_info->original_app_path = strdup(menu_info->app_path);
158
159         ret = pkgmgrinfo_appinfo_get_apptype(handle, &apptype);
160         if (ret != PMINFO_R_OK)
161                 _E("fail to get apptype from appinfo handle");
162
163         if (apptype)
164                 menu_info->pkg_type = strdup(apptype);
165
166         ret = pkgmgrinfo_appinfo_destroy_appinfo(handle);
167         if (ret != PMINFO_R_OK)
168                 _E("pkgmgrinfo_appinfo_destroy_appinfo failed");
169
170         if (!_get_app_path(menu_info)) {
171                 _free_app_info_from_db(menu_info);
172                 return NULL;
173         }
174
175         return menu_info;
176 }
177
178 static inline int __appinfo_func(const pkgmgrinfo_appinfo_h appinfo,
179                 void *user_data)
180 {
181         app_info_from_db *menu_info = (app_info_from_db *)user_data;
182         char *apppath;
183         char *pkgid;
184         int ret = PMINFO_R_OK;
185
186         if (!menu_info)
187                 return ret;
188
189         ret = pkgmgrinfo_appinfo_get_exec(appinfo, &apppath);
190         if (ret == PMINFO_R_OK && apppath) {
191                 menu_info->app_path = strdup(apppath);
192                 if (menu_info->app_path == NULL) {
193                         _E("Out of memory");
194                         return PMINFO_R_ERROR;
195                 }
196         }
197
198         ret = pkgmgrinfo_appinfo_get_pkgid(appinfo, &pkgid);
199         if (ret == PMINFO_R_OK && pkgid) {
200                 menu_info->pkg_id = strdup(pkgid);
201                 if (menu_info->pkg_id == NULL) {
202                         _E("Out of memory");
203                         return PMINFO_R_ERROR;
204                 }
205         }
206
207         return ret;
208 }
209
210 static inline app_info_from_db *_get_app_info_from_db_by_appid_user(
211                 const char *appid, uid_t uid)
212 {
213         app_info_from_db *menu_info;
214         pkgmgrinfo_appinfo_filter_h filter;
215         int ret = PMINFO_R_OK;
216
217         if (uid == 0) {
218                 _E("request from root, treat as global user");
219                 uid = GLOBAL_USER;
220         }
221
222         if (appid == NULL)
223                 return NULL;
224
225         menu_info = calloc(1, sizeof(app_info_from_db));
226         if (menu_info == NULL)
227                 return NULL;
228
229         ret = pkgmgrinfo_appinfo_filter_create(&filter);
230         if (ret != PMINFO_R_OK) {
231                 _free_app_info_from_db(menu_info);
232                 return NULL;
233         }
234
235         ret = pkgmgrinfo_appinfo_filter_add_string(filter,
236                         PMINFO_APPINFO_PROP_APP_ID, appid);
237         if (ret != PMINFO_R_OK) {
238                 pkgmgrinfo_appinfo_filter_destroy(filter);
239                 _free_app_info_from_db(menu_info);
240                 return NULL;
241         }
242
243         if (uid != GLOBAL_USER)
244                 ret = pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(filter,
245                                 __appinfo_func, (void *)menu_info, uid);
246         else
247                 ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(filter,
248                                 __appinfo_func, (void *)menu_info);
249
250         if ((ret != PMINFO_R_OK) || (menu_info->app_path == NULL)) {
251                 pkgmgrinfo_appinfo_filter_destroy(filter);
252                 _free_app_info_from_db(menu_info);
253                 return NULL;
254         }
255
256         pkgmgrinfo_appinfo_filter_destroy(filter);
257
258         menu_info->appid = strdup(appid);
259         menu_info->original_app_path = strdup(menu_info->app_path);
260
261         return menu_info;
262
263 }
264
265 static inline app_info_from_db *_get_app_info_from_db_by_appid(
266                                                         const char *appid)
267 {
268         return _get_app_info_from_db_by_appid_user(appid, GLOBAL_USER);
269 }
270
271