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