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