8b6a8147533e694c0bd6789f0761b0eed399470e
[framework/appfw/aul-1.git] / include / menu_db_util.h
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23 #include <ail.h>
24 #include <string.h>
25 #include "simple_util.h"
26
27 #define MAX_PATH_LEN    1024
28
29 #define AUL_APP_INFO_FLD_PKG_NAME               "package"
30 #define AUL_APP_INFO_FLD_APP_PATH               "exec"
31 #define AUL_APP_INFO_FLD_APP_TYPE               "x_slp_packagetype"
32 #define AUL_APP_INFO_FLD_WIDTH                  "x_slp_baselayoutwidth"
33 #define AUL_APP_INFO_FLD_HEIGHT                 "x_slp_baselayoutheight"
34 #define AUL_APP_INFO_FLD_VERTICAL               "x_slp_ishorizontalscale"
35 #define AUL_APP_INFO_FLD_MULTIPLE               "x_slp_multiple"
36 #define AUL_APP_INFO_FLD_TASK_MANAGE    "x_slp_taskmanage"
37 #define AUL_APP_INFO_FLD_MIMETYPE               "mimetype"
38 #define AUL_APP_INFO_FLD_SERVICE                "x_slp_service"
39
40 #define AUL_RETRIEVE_PKG_NAME                   "package = '?'"
41 #define AUL_RETRIEVE_APP_PATH                   "exec = '?'"
42 #define AUL_RETRIEVE_MIMETYPE                   "mimetype like '?'"
43 #define AUL_RETRIEVE_SERVICE                    "x_slp_service like '?'"
44
45 typedef struct {
46         char *pkg_name;         /* package */
47         char *app_path;         /* exec */
48         char *original_app_path;        /* exec */
49         char *app_type;         /* x_slp_packagetype */
50         int width;              /* x_slp_baselayoutwidth */
51         int height;             /* x_slp_baselayoutheight */
52         int vertical;           /* x_slp_ishorizontalscale */
53         int multiple;           /* x_slp_multiple */
54         int task_manage;        /* x_slp_taskmanage */
55         char *pkg_type;
56 } app_info_from_db;
57
58 static inline char *_get_pkgname(app_info_from_db *menu_info)
59 {
60         if (menu_info->pkg_name == NULL)
61                 return NULL;
62         return menu_info->pkg_name;
63 }
64
65 static inline char *_get_app_path(app_info_from_db *menu_info)
66 {
67         int i = 0;
68         int path_len = -1;
69
70         if (menu_info->app_path == NULL)
71                 return NULL;
72
73         while (menu_info->app_path[i] != 0) {
74                 if (menu_info->app_path[i] == ' '
75                     || menu_info->app_path[i] == '\t') {
76                         path_len = i;
77                         break;
78                 }
79                 i++;
80         }
81
82         if (path_len == 0) {
83                 free(menu_info->app_path);
84                 menu_info->app_path = NULL;
85         } else if (path_len > 0) {
86                 char *tmp_app_path = malloc(sizeof(char) * (path_len + 1));
87                 if(tmp_app_path == NULL)
88                         return NULL;
89                 snprintf(tmp_app_path, path_len + 1, "%s", menu_info->app_path);
90                 free(menu_info->app_path);
91                 menu_info->app_path = tmp_app_path;
92         }
93
94         return menu_info->app_path;
95 }
96
97 static inline char *_get_original_app_path(app_info_from_db *menu_info)
98 {
99         if (menu_info->original_app_path == NULL)
100                 return NULL;
101         return menu_info->original_app_path;
102 }
103
104 static inline char *_get_app_app_type(app_info_from_db *menu_info)
105 {
106         return menu_info->app_type;
107 }
108
109 static inline int _get_app_width(app_info_from_db *menu_info)
110 {
111         return menu_info->width;
112 }
113
114 static inline int _get_app_height(app_info_from_db *menu_info)
115 {
116         return menu_info->height;
117 }
118
119 static inline int _is_app_scalable_with_height(app_info_from_db *menu_info)
120 {
121         if (menu_info->vertical)
122                 return 0;
123         else
124                 return 1;
125 }
126
127 static inline int _is_app_multi_inst(app_info_from_db *menu_info)
128 {
129         if (menu_info->multiple)
130                 return 1;
131         else
132                 return 0;
133 }
134
135 static inline int _get_app_task_manage(app_info_from_db *menu_info)
136 {
137         return menu_info->task_manage;
138 }
139
140 static inline void _free_app_info_from_db(app_info_from_db *menu_info)
141 {
142         if (menu_info != NULL) {
143                 if (menu_info->pkg_name != NULL)
144                         free(menu_info->pkg_name);
145                 if (menu_info->app_path != NULL)
146                         free(menu_info->app_path);
147                 if (menu_info->original_app_path != NULL)
148                         free(menu_info->original_app_path);
149                 if (menu_info->app_type != NULL)
150                         free(menu_info->app_type);
151                 free(menu_info);
152         }
153 }
154
155 static inline app_info_from_db *_get_app_info_from_db_by_pkgname(
156                                                         const char *pkgname)
157 {
158         app_info_from_db *menu_info;
159         ail_appinfo_h handle;
160         ail_error_e ret;
161         char *str = NULL;
162
163         menu_info = calloc(1, sizeof(app_info_from_db));
164         if (menu_info == NULL) {
165                 return NULL;
166         }
167
168         ret = ail_package_get_appinfo(pkgname, &handle);
169         if (ret != AIL_ERROR_OK) {
170                 _free_app_info_from_db(menu_info);
171                 return NULL;
172         }
173
174         ret = ail_appinfo_get_str(handle, AIL_PROP_PACKAGE_STR, &str);
175         if (str) {
176                 menu_info->pkg_name = strdup(str);      
177                 str = NULL;
178         }
179
180         ret = ail_appinfo_get_str(handle, AIL_PROP_EXEC_STR, &str);
181         if (str) {
182                 menu_info->app_path = strdup(str);
183                 str = NULL;
184         }
185
186         if (menu_info->app_path != NULL)
187                 menu_info->original_app_path = strdup(menu_info->app_path);
188
189         ret = ail_appinfo_get_int(handle, AIL_PROP_X_SLP_BASELAYOUTWIDTH_INT, &menu_info->width);
190         
191         ret = ail_appinfo_get_int(handle, AIL_PROP_X_SLP_BASELAYOUTHEIGHT_INT, &menu_info->height);
192         
193         ret = ail_appinfo_get_bool(handle, AIL_PROP_X_SLP_ISHORIZONTALSCALE_BOOL, &menu_info->vertical);
194         
195         ret = ail_appinfo_get_bool(handle, AIL_PROP_X_SLP_MULTIPLE_BOOL, &menu_info->multiple);
196         
197         ret = ail_appinfo_get_bool(handle, AIL_PROP_X_SLP_TASKMANAGE_BOOL, &menu_info->task_manage);
198         
199         ret = ail_appinfo_get_str(handle, AIL_PROP_TYPE_STR, &str);
200         if (str) {
201                 menu_info->app_type = strdup(str);
202                 str = NULL;
203         }
204
205         ret = ail_appinfo_get_str(handle, AIL_PROP_X_SLP_PACKAGETYPE_STR, &str);
206         if (str) {
207                 menu_info->pkg_type = strdup(str);
208                 str = NULL;
209         }
210         
211         ret = ail_package_destroy_appinfo(handle);
212         if (ret != AIL_ERROR_OK) {
213                 _E("ail_package_destroy_appinfo failed");
214         }
215
216         if (!_get_app_path(menu_info)) {
217                 _free_app_info_from_db(menu_info);
218                 return NULL;
219         }
220
221         return menu_info;
222 }
223
224 static inline ail_cb_ret_e __appinfo_func(const ail_appinfo_h appinfo, void *user_data)
225 {
226         app_info_from_db *menu_info = (app_info_from_db *)user_data;
227         char *package;
228
229         ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &package);
230
231         menu_info->pkg_name = strdup(package);
232         
233         return AIL_CB_RET_CANCEL;       /*return AIL_CB_RET_CONTINUE;*/ 
234 }
235
236 static inline app_info_from_db *_get_app_info_from_db_by_apppath(
237                                                         const char *apppath)
238 {
239         app_info_from_db *menu_info = NULL;
240         ail_filter_h filter;
241         ail_error_e ret;
242         int count;
243         
244         if (apppath == NULL)
245                 return NULL;
246
247         menu_info = calloc(1, sizeof(app_info_from_db));
248         if (menu_info == NULL)
249                 return NULL;
250
251         ret = ail_filter_new(&filter);
252         if (ret != AIL_ERROR_OK) {
253                 _free_app_info_from_db(menu_info);
254                 return NULL;
255         }
256
257         ret = ail_filter_add_str(filter, AIL_PROP_X_SLP_EXE_PATH, apppath);
258         if (ret != AIL_ERROR_OK) {
259                 ail_filter_destroy(filter);
260                 _free_app_info_from_db(menu_info);
261                 return NULL;
262         }
263
264         ret = ail_filter_count_appinfo(filter, &count);
265         if (ret != AIL_ERROR_OK) {
266                 ail_filter_destroy(filter);
267                 _free_app_info_from_db(menu_info);
268                 return NULL;
269         }
270         if (count < 1) {
271                 ail_filter_destroy(filter);
272                 _free_app_info_from_db(menu_info);
273                 return NULL;
274         }
275
276         ail_filter_list_appinfo_foreach(filter, __appinfo_func, (void *)menu_info);
277
278         ail_filter_destroy(filter);
279
280         menu_info->app_path = strdup(apppath);
281         menu_info->original_app_path = strdup(apppath);
282
283         return menu_info;
284         
285 }
286