Upload Tizen2.0 source
[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 multiple;           /* x_slp_multiple */
51         int task_manage;        /* x_slp_taskmanage */
52         char *pkg_type;
53 } app_info_from_db;
54
55 static inline char *_get_pkgname(app_info_from_db *menu_info)
56 {
57         if (menu_info->pkg_name == NULL)
58                 return NULL;
59         return menu_info->pkg_name;
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->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         if (menu_info->original_app_path == NULL)
97                 return NULL;
98         return menu_info->original_app_path;
99 }
100
101 static inline char *_get_app_app_type(app_info_from_db *menu_info)
102 {
103         return menu_info->app_type;
104 }
105
106 static inline int _is_app_multi_inst(app_info_from_db *menu_info)
107 {
108         if (menu_info->multiple)
109                 return 1;
110         else
111                 return 0;
112 }
113
114 static inline int _get_app_task_manage(app_info_from_db *menu_info)
115 {
116         return menu_info->task_manage;
117 }
118
119 static inline void _free_app_info_from_db(app_info_from_db *menu_info)
120 {
121         if (menu_info != NULL) {
122                 if (menu_info->pkg_name != NULL)
123                         free(menu_info->pkg_name);
124                 if (menu_info->app_path != NULL)
125                         free(menu_info->app_path);
126                 if (menu_info->original_app_path != NULL)
127                         free(menu_info->original_app_path);
128                 if (menu_info->app_type != NULL)
129                         free(menu_info->app_type);
130                 free(menu_info);
131         }
132 }
133
134 static inline app_info_from_db *_get_app_info_from_db_by_pkgname(
135                                                         const char *pkgname)
136 {
137         app_info_from_db *menu_info;
138         ail_appinfo_h handle;
139         ail_error_e ret;
140         char *str = NULL;
141
142         menu_info = calloc(1, sizeof(app_info_from_db));
143         if (menu_info == NULL) {
144                 return NULL;
145         }
146
147         ret = ail_package_get_appinfo(pkgname, &handle);
148         if (ret != AIL_ERROR_OK) {
149                 _free_app_info_from_db(menu_info);
150                 return NULL;
151         }
152
153         ret = ail_appinfo_get_str(handle, AIL_PROP_PACKAGE_STR, &str);
154         if (str) {
155                 menu_info->pkg_name = strdup(str);      
156                 str = NULL;
157         }
158
159         ret = ail_appinfo_get_str(handle, AIL_PROP_EXEC_STR, &str);
160         if (str) {
161                 menu_info->app_path = strdup(str);
162                 str = NULL;
163         }
164
165         if (menu_info->app_path != NULL)
166                 menu_info->original_app_path = strdup(menu_info->app_path);
167
168         ret = ail_appinfo_get_bool(handle, AIL_PROP_X_SLP_MULTIPLE_BOOL,
169                 (bool *)&menu_info->multiple);
170
171         ret = ail_appinfo_get_bool(handle, AIL_PROP_X_SLP_TASKMANAGE_BOOL,
172                 (bool *)&menu_info->task_manage);
173         
174         ret = ail_appinfo_get_str(handle, AIL_PROP_TYPE_STR, &str);
175         if (str) {
176                 menu_info->app_type = strdup(str);
177                 str = NULL;
178         }
179
180         ret = ail_appinfo_get_str(handle, AIL_PROP_X_SLP_PACKAGETYPE_STR, &str);
181         if (str) {
182                 menu_info->pkg_type = strdup(str);
183                 str = NULL;
184         }
185         
186         ret = ail_destroy_appinfo(handle);
187         if (ret != AIL_ERROR_OK) {
188                 _E("ail_destroy_appinfo failed");
189         }
190
191         if (!_get_app_path(menu_info)) {
192                 _free_app_info_from_db(menu_info);
193                 return NULL;
194         }
195
196         return menu_info;
197 }
198
199 static inline ail_cb_ret_e __appinfo_func(const ail_appinfo_h appinfo, void *user_data)
200 {
201         app_info_from_db *menu_info = (app_info_from_db *)user_data;
202         char *package;
203
204         ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &package);
205
206         menu_info->pkg_name = strdup(package);
207         
208         return AIL_CB_RET_CANCEL;       /*return AIL_CB_RET_CONTINUE;*/ 
209 }
210
211 static inline app_info_from_db *_get_app_info_from_db_by_apppath(
212                                                         const char *apppath)
213 {
214         app_info_from_db *menu_info = NULL;
215         ail_filter_h filter;
216         ail_error_e ret;
217         int count;
218         
219         if (apppath == NULL)
220                 return NULL;
221
222         menu_info = calloc(1, sizeof(app_info_from_db));
223         if (menu_info == NULL)
224                 return NULL;
225
226         ret = ail_filter_new(&filter);
227         if (ret != AIL_ERROR_OK) {
228                 _free_app_info_from_db(menu_info);
229                 return NULL;
230         }
231
232         ret = ail_filter_add_str(filter, AIL_PROP_X_SLP_EXE_PATH, apppath);
233         if (ret != AIL_ERROR_OK) {
234                 ail_filter_destroy(filter);
235                 _free_app_info_from_db(menu_info);
236                 return NULL;
237         }
238
239         ret = ail_filter_count_appinfo(filter, &count);
240         if (ret != AIL_ERROR_OK) {
241                 ail_filter_destroy(filter);
242                 _free_app_info_from_db(menu_info);
243                 return NULL;
244         }
245         if (count < 1) {
246                 ail_filter_destroy(filter);
247                 _free_app_info_from_db(menu_info);
248                 return NULL;
249         }
250
251         ail_filter_list_appinfo_foreach(filter, __appinfo_func, (void *)menu_info);
252
253         ail_filter_destroy(filter);
254
255         menu_info->app_path = strdup(apppath);
256         menu_info->original_app_path = strdup(apppath);
257
258         return menu_info;
259         
260 }
261