tizen 2.3 release
[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 #ifndef __MENU_DB_UTIL_H_
23 #define __MENU_DB_UTIL_H_
24
25 #include <ail.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include "simple_util.h"
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 *pkg_name;         /* package */
50         char *app_path;         /* exec */
51         char *original_app_path;        /* exec */
52         char *pkg_type;         /* x_slp_packagetype */
53         char *hwacc;            /* hwacceleration */
54         char *taskmanage;       /* taskmanage */
55         char *pkg_id;
56 } app_info_from_db;
57
58 static inline char *_get_pkgname(app_info_from_db *menu_info)
59 {
60         return menu_info ? menu_info->pkg_name : NULL;
61 }
62
63 static inline char *_get_pkgid(app_info_from_db *menu_info)
64 {
65         return menu_info ? menu_info->pkg_id : NULL;
66 }
67
68 static inline char *_get_app_path(app_info_from_db *menu_info)
69 {
70         int i = 0;
71         int path_len = -1;
72
73         if (!menu_info || menu_info->app_path == NULL)
74                 return NULL;
75
76         while (menu_info->app_path[i] != 0) {
77                 if (menu_info->app_path[i] == ' '
78                     || menu_info->app_path[i] == '\t') {
79                         path_len = i;
80                         break;
81                 }
82                 i++;
83         }
84
85         if (path_len == 0) {
86                 free(menu_info->app_path);
87                 menu_info->app_path = NULL;
88         } else if (path_len > 0) {
89                 char *tmp_app_path = malloc(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->pkg_name != NULL)
109                         free(menu_info->pkg_name);
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->taskmanage != NULL)
119                         free(menu_info->taskmanage);
120                 if (menu_info->pkg_id != NULL)
121                         free(menu_info->pkg_id);
122                 free(menu_info);
123         }
124 }
125
126 static inline app_info_from_db *_get_app_info_from_db_by_pkgname(
127                                                         const char *pkgname)
128 {
129         app_info_from_db *menu_info;
130         ail_appinfo_h handle;
131         ail_error_e ret;
132         char *str = NULL;
133
134         menu_info = calloc(1, sizeof(app_info_from_db));
135         if (menu_info == NULL) {
136                 return NULL;
137         }
138
139         ret = ail_get_appinfo(pkgname, &handle);
140         if (ret != AIL_ERROR_OK) {
141                 _free_app_info_from_db(menu_info);
142                 return NULL;
143         }
144
145         ret = ail_appinfo_get_str(handle, AIL_PROP_PACKAGE_STR, &str);
146         if (str) {
147                 menu_info->pkg_name = strdup(str);
148                 str = NULL;
149         }
150
151         ret = ail_appinfo_get_str(handle, AIL_PROP_EXEC_STR, &str);
152         if (str) {
153                 menu_info->app_path = strdup(str);
154                 str = NULL;
155         }
156
157         if (menu_info->app_path != NULL)
158                 menu_info->original_app_path = strdup(menu_info->app_path);
159
160         ret = ail_appinfo_get_str(handle, AIL_PROP_X_SLP_PACKAGETYPE_STR, &str);
161         if (str) {
162                 menu_info->pkg_type = strdup(str);
163                 str = NULL;
164         }
165
166         ret = ail_destroy_appinfo(handle);
167         if (ret != AIL_ERROR_OK) {
168                 _E("ail_destroy_appinfo failed");
169         }
170
171         if (!_get_app_path(menu_info)) {
172                 _free_app_info_from_db(menu_info);
173                 return NULL;
174         }
175
176         return menu_info;
177 }
178
179 static inline ail_cb_ret_e __appinfo_func(const ail_appinfo_h appinfo, void *user_data)
180 {
181         app_info_from_db *menu_info = (app_info_from_db *)user_data;
182         char *package;
183         char *pkgid;
184         ail_cb_ret_e ret = AIL_CB_RET_CONTINUE;
185
186         if (!menu_info)
187                 return ret;
188
189         ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &package);
190         if (package) {
191                 menu_info->pkg_name = strdup(package);
192                 ret = AIL_CB_RET_CANCEL;
193         }
194
195         ail_appinfo_get_str(appinfo, AIL_PROP_X_SLP_PKGID_STR, &pkgid);
196         if (pkgid) {
197                 menu_info->pkg_id = strdup(pkgid);
198         }
199
200         return ret;
201 }
202
203 static inline app_info_from_db *_get_app_info_from_db_by_apppath(
204                                                         const char *apppath)
205 {
206         app_info_from_db *menu_info = NULL;
207         ail_filter_h filter;
208         ail_error_e ret;
209         int count;
210
211         if (apppath == NULL)
212                 return NULL;
213
214         menu_info = calloc(1, sizeof(app_info_from_db));
215         if (menu_info == NULL)
216                 return NULL;
217
218         ret = ail_filter_new(&filter);
219         if (ret != AIL_ERROR_OK) {
220                 _free_app_info_from_db(menu_info);
221                 return NULL;
222         }
223
224         ret = ail_filter_add_str(filter, AIL_PROP_X_SLP_EXE_PATH, apppath);
225         if (ret != AIL_ERROR_OK) {
226                 ail_filter_destroy(filter);
227                 _free_app_info_from_db(menu_info);
228                 return NULL;
229         }
230
231         ret = ail_filter_count_appinfo(filter, &count);
232         if (ret != AIL_ERROR_OK) {
233                 ail_filter_destroy(filter);
234                 _free_app_info_from_db(menu_info);
235                 return NULL;
236         }
237         if (count < 1) {
238                 ail_filter_destroy(filter);
239                 _free_app_info_from_db(menu_info);
240                 return NULL;
241         }
242
243         ail_filter_list_appinfo_foreach(filter, __appinfo_func, (void *)menu_info);
244
245         ail_filter_destroy(filter);
246
247         menu_info->app_path = strdup(apppath);
248         menu_info->original_app_path = strdup(apppath);
249
250         return menu_info;
251
252 }
253
254 #endif