[Release] wrt_0.8.166 for tizen_2.1 branch
[platform/framework/web/wrt.git] / src / wrt-launchpad-daemon / include / menu_db_util.h
1 /*
2  * Copyright (c) 2011 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 #ifndef __MENU_DB_UTIL_H_
18 #define __MENU_DB_UTIL_H_
19
20 #include <ail.h>
21 #include <string.h>
22 #include "simple_util.h"
23
24 #define MAX_PATH_LEN    1024
25
26 #define AUL_APP_INFO_FLD_PKG_NAME               "package"
27 #define AUL_APP_INFO_FLD_APP_PATH               "exec"
28 #define AUL_APP_INFO_FLD_APP_TYPE               "x_slp_packagetype"
29 #define AUL_APP_INFO_FLD_WIDTH                  "x_slp_baselayoutwidth"
30 #define AUL_APP_INFO_FLD_HEIGHT                 "x_slp_baselayoutheight"
31 #define AUL_APP_INFO_FLD_VERTICAL               "x_slp_ishorizontalscale"
32 #define AUL_APP_INFO_FLD_MULTIPLE               "x_slp_multiple"
33 #define AUL_APP_INFO_FLD_TASK_MANAGE    "x_slp_taskmanage"
34 #define AUL_APP_INFO_FLD_MIMETYPE               "mimetype"
35 #define AUL_APP_INFO_FLD_SERVICE                "x_slp_service"
36
37 #define AUL_RETRIEVE_PKG_NAME                   "package = '?'"
38 #define AUL_RETRIEVE_APP_PATH                   "exec = '?'"
39 #define AUL_RETRIEVE_MIMETYPE                   "mimetype like '?'"
40 #define AUL_RETRIEVE_SERVICE                    "x_slp_service like '?'"
41
42 typedef struct {
43     char *pkg_name;             /* package */
44     char *app_path;             /* exec */
45     char *original_app_path;            /* exec */
46     char *pkg_type;             /* x_slp_packagetype */
47     char *hwacc;                /* hwacceleration */
48 } app_info_from_db;
49
50 static inline char *_get_pkgname(app_info_from_db *menu_info)
51 {
52     if (menu_info->pkg_name == NULL) {
53         return NULL;
54     }
55     return menu_info->pkg_name;
56 }
57
58 static inline char *_get_app_path(app_info_from_db *menu_info)
59 {
60     int i = 0;
61     int path_len = -1;
62
63     if (menu_info->app_path == NULL) {
64         return NULL;
65     }
66
67     while (menu_info->app_path[i] != 0) {
68         if (menu_info->app_path[i] == ' '
69             || menu_info->app_path[i] == '\t')
70         {
71             path_len = i;
72             break;
73         }
74         i++;
75     }
76
77     if (path_len == 0) {
78         free(menu_info->app_path);
79         menu_info->app_path = NULL;
80     } else if (path_len > 0) {
81         char *tmp_app_path = (char *)malloc(sizeof(char) * (path_len + 1));
82         if (tmp_app_path == NULL) {
83             return NULL;
84         }
85         snprintf(tmp_app_path, path_len + 1, "%s", menu_info->app_path);
86         free(menu_info->app_path);
87         menu_info->app_path = tmp_app_path;
88     }
89
90     return menu_info->app_path;
91 }
92
93 static inline char *_get_original_app_path(app_info_from_db *menu_info)
94 {
95     if (menu_info->original_app_path == NULL) {
96         return NULL;
97     }
98     return menu_info->original_app_path;
99 }
100
101 static inline void _free_app_info_from_db(app_info_from_db *menu_info)
102 {
103     if (menu_info != NULL) {
104         if (menu_info->pkg_name != NULL) {
105             free(menu_info->pkg_name);
106         }
107         if (menu_info->app_path != NULL) {
108             free(menu_info->app_path);
109         }
110         if (menu_info->original_app_path != NULL) {
111             free(menu_info->original_app_path);
112         }
113         if (menu_info->hwacc != NULL) {
114             free(menu_info->hwacc);
115         }
116         free(menu_info);
117     }
118 }
119
120 static inline app_info_from_db *_get_app_info_from_db_by_pkgname(
121     const char *pkgname)
122 {
123     app_info_from_db *menu_info;
124     ail_appinfo_h handle;
125     ail_error_e ret;
126     char *str = NULL;
127
128     menu_info = (app_info_from_db *)calloc(1, sizeof(app_info_from_db));
129     if (menu_info == NULL) {
130         return NULL;
131     }
132
133     ret = ail_get_appinfo(pkgname, &handle);
134     if (ret != AIL_ERROR_OK) {
135         _free_app_info_from_db(menu_info);
136         return NULL;
137     }
138
139     ret = ail_appinfo_get_str(handle, AIL_PROP_PACKAGE_STR, &str);
140     if (str) {
141         menu_info->pkg_name = strdup(str);
142         str = NULL;
143     }
144
145     ret = ail_appinfo_get_str(handle, AIL_PROP_EXEC_STR, &str);
146     if (str) {
147         menu_info->app_path = strdup(str);
148         str = NULL;
149     }
150
151     if (menu_info->app_path != NULL) {
152         menu_info->original_app_path = strdup(menu_info->app_path);
153     }
154
155     ret = ail_appinfo_get_str(handle, AIL_PROP_X_SLP_PACKAGETYPE_STR, &str);
156     if (str) {
157         menu_info->pkg_type = strdup(str);
158         str = NULL;
159     }
160
161     ret = ail_destroy_appinfo(handle);
162     if (ret != AIL_ERROR_OK) {
163         _E("ail_destroy_appinfo failed");
164     }
165
166     if (!_get_app_path(menu_info)) {
167         _free_app_info_from_db(menu_info);
168         return NULL;
169     }
170
171     return menu_info;
172 }
173
174 static inline ail_cb_ret_e __appinfo_func(const ail_appinfo_h appinfo,
175                                           void *user_data)
176 {
177     app_info_from_db *menu_info = (app_info_from_db *)user_data;
178     char *package;
179
180     ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &package);
181
182     menu_info->pkg_name = strdup(package);
183
184     return AIL_CB_RET_CANCEL;           /*return AIL_CB_RET_CONTINUE;*/
185 }
186
187 static inline app_info_from_db *_get_app_info_from_db_by_apppath(
188     const char *apppath)
189 {
190     app_info_from_db *menu_info = NULL;
191     ail_filter_h filter;
192     ail_error_e ret;
193     int count;
194
195     if (apppath == NULL) {
196         return NULL;
197     }
198
199     menu_info = (app_info_from_db *)calloc(1, sizeof(app_info_from_db));
200     if (menu_info == NULL) {
201         return NULL;
202     }
203
204     ret = ail_filter_new(&filter);
205     if (ret != AIL_ERROR_OK) {
206         _free_app_info_from_db(menu_info);
207         return NULL;
208     }
209
210     ret = ail_filter_add_str(filter, AIL_PROP_X_SLP_EXE_PATH, apppath);
211     if (ret != AIL_ERROR_OK) {
212         ail_filter_destroy(filter);
213         _free_app_info_from_db(menu_info);
214         return NULL;
215     }
216
217     ret = ail_filter_count_appinfo(filter, &count);
218     if (ret != AIL_ERROR_OK) {
219         ail_filter_destroy(filter);
220         _free_app_info_from_db(menu_info);
221         return NULL;
222     }
223     if (count < 1) {
224         ail_filter_destroy(filter);
225         _free_app_info_from_db(menu_info);
226         return NULL;
227     }
228
229     ail_filter_list_appinfo_foreach(filter, __appinfo_func, (void *)menu_info);
230
231     ail_filter_destroy(filter);
232
233     menu_info->app_path = strdup(apppath);
234     menu_info->original_app_path = strdup(apppath);
235
236     return menu_info;
237 }
238
239
240 static inline app_info_from_db *_get_app_info_from_bundle_by_pkgname(
241     const char *pkgname, bundle *kb)
242 {
243     app_info_from_db *menu_info;
244
245     menu_info = (app_info_from_db*)calloc(1, sizeof(app_info_from_db));
246     if (menu_info == NULL) {
247         return NULL;
248     }
249
250     menu_info->pkg_name = strdup(pkgname);
251     menu_info->app_path = strdup(bundle_get_val(kb, AUL_K_EXEC));
252     if (menu_info->app_path != NULL) {
253         menu_info->original_app_path = strdup(menu_info->app_path);
254     }
255     menu_info->pkg_type = strdup(bundle_get_val(kb, AUL_K_PACKAGETYPE));
256     menu_info->hwacc = strdup(bundle_get_val(kb, AUL_K_HWACC));
257
258     if (!_get_app_path(menu_info)) {
259         _free_app_info_from_db(menu_info);
260         return NULL;
261     }
262
263     return menu_info;
264 }
265 #endif //__MENU_DB_UTIL_H_