1. apply systemd unit files
[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 *pkg_type;         /* x_slp_packagetype */
50 } app_info_from_db;
51
52 static inline char *_get_pkgname(app_info_from_db *menu_info)
53 {
54         if (menu_info->pkg_name == NULL)
55                 return NULL;
56         return menu_info->pkg_name;
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->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         if (menu_info->original_app_path == NULL)
94                 return NULL;
95         return menu_info->original_app_path;
96 }
97
98 static inline void _free_app_info_from_db(app_info_from_db *menu_info)
99 {
100         if (menu_info != NULL) {
101                 if (menu_info->pkg_name != NULL)
102                         free(menu_info->pkg_name);
103                 if (menu_info->app_path != NULL)
104                         free(menu_info->app_path);
105                 if (menu_info->original_app_path != NULL)
106                         free(menu_info->original_app_path);
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
169         ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &package);
170
171         menu_info->pkg_name = strdup(package);
172         
173         return AIL_CB_RET_CANCEL;       /*return AIL_CB_RET_CONTINUE;*/ 
174 }
175
176 static inline app_info_from_db *_get_app_info_from_db_by_apppath(
177                                                         const char *apppath)
178 {
179         app_info_from_db *menu_info = NULL;
180         ail_filter_h filter;
181         ail_error_e ret;
182         int count;
183         
184         if (apppath == NULL)
185                 return NULL;
186
187         menu_info = calloc(1, sizeof(app_info_from_db));
188         if (menu_info == NULL)
189                 return NULL;
190
191         ret = ail_filter_new(&filter);
192         if (ret != AIL_ERROR_OK) {
193                 _free_app_info_from_db(menu_info);
194                 return NULL;
195         }
196
197         ret = ail_filter_add_str(filter, AIL_PROP_X_SLP_EXE_PATH, apppath);
198         if (ret != AIL_ERROR_OK) {
199                 ail_filter_destroy(filter);
200                 _free_app_info_from_db(menu_info);
201                 return NULL;
202         }
203
204         ret = ail_filter_count_appinfo(filter, &count);
205         if (ret != AIL_ERROR_OK) {
206                 ail_filter_destroy(filter);
207                 _free_app_info_from_db(menu_info);
208                 return NULL;
209         }
210         if (count < 1) {
211                 ail_filter_destroy(filter);
212                 _free_app_info_from_db(menu_info);
213                 return NULL;
214         }
215
216         ail_filter_list_appinfo_foreach(filter, __appinfo_func, (void *)menu_info);
217
218         ail_filter_destroy(filter);
219
220         menu_info->app_path = strdup(apppath);
221         menu_info->original_app_path = strdup(apppath);
222
223         return menu_info;
224         
225 }
226