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