b6ecaf6ed8537f0a9ef91939ffaf5f33e25e8d63
[platform/core/appfw/ail.git] / tool / src / ail_filter.c
1 /*
2  * ail
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 #include <stdio.h>
23 #include <getopt.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "ail.h"
28 #include "ail_private.h"
29
30 enum {
31         _CMD_UNKNOWN,
32         _CMD_COUNT,
33         _CMD_FILTER,
34 };
35
36 static void usage(const char *name)
37 {
38         fprintf(stderr, "\n");
39         fprintf(stderr, "  Usage: %s -c command [options]\n", name);
40         fprintf(stderr, "    command:\n");
41         fprintf(stderr, "      c : count appinfos by option\n");
42         fprintf(stderr, "      f : filter appinfos by option\n");
43         fprintf(stderr, "\n");
44         fprintf(stderr, "    option:\n");
45         fprintf(stderr, "      --package=<pacakge name>\n");
46         fprintf(stderr, "      --exec=<exec file>\n");
47         fprintf(stderr, "      --name=<title name>\n");
48         fprintf(stderr, "      --type=<type>\n");
49         fprintf(stderr, "      --icon=<icon file>\n");
50         fprintf(stderr, "      --category=<category>\n");
51         fprintf(stderr, "      --version=<version>\n");
52         fprintf(stderr, "      --mimetype=<mimetype>\n");
53         fprintf(stderr, "      --nodisplay=<{0|1}>\n");
54         fprintf(stderr, "      --service=<service>\n");
55         fprintf(stderr, "      --packagetype=<package type>\n");
56         fprintf(stderr, "      --packagecategories=<package category>\n");
57         fprintf(stderr, "      --packageid=<package id>\n");
58         fprintf(stderr, "      --svc=<action:scheme:mime>\n");
59         fprintf(stderr, "      --taskmanage=<{true|false}>\n");
60         fprintf(stderr, "      --multiple=<{true|false}>\n");
61         fprintf(stderr, "      --removable=<{true|false}>\n");
62         fprintf(stderr, "\n");
63         fprintf(stderr, "    Example:\n");
64         fprintf(stderr, "       %s -n menu-screen -r 1\n", name);
65         fprintf(stderr, "\n");
66 }
67
68 struct _ail_map_t {
69         int prop;
70         const char *property;
71 };
72
73 static struct _ail_map_t prop_map[] = {
74         {E_AIL_PROP_PACKAGE_STR,                AIL_PROP_PACKAGE_STR},
75         {E_AIL_PROP_EXEC_STR,                   AIL_PROP_EXEC_STR},
76         {E_AIL_PROP_NAME_STR,                   AIL_PROP_NAME_STR},
77         {E_AIL_PROP_TYPE_STR,                   AIL_PROP_TYPE_STR},
78         {E_AIL_PROP_ICON_STR,                   AIL_PROP_ICON_STR},
79         {E_AIL_PROP_CATEGORIES_STR,             AIL_PROP_CATEGORIES_STR},
80         {E_AIL_PROP_VERSION_STR,                AIL_PROP_VERSION_STR},
81         {E_AIL_PROP_MIMETYPE_STR,               AIL_PROP_MIMETYPE_STR},
82         {E_AIL_PROP_X_SLP_SERVICE_STR,          AIL_PROP_X_SLP_SERVICE_STR},
83         {E_AIL_PROP_X_SLP_PACKAGETYPE_STR,      AIL_PROP_X_SLP_PACKAGETYPE_STR},
84         {E_AIL_PROP_X_SLP_PACKAGECATEGORIES_STR, AIL_PROP_X_SLP_PACKAGECATEGORIES_STR},
85         {E_AIL_PROP_X_SLP_PACKAGEID_STR,        AIL_PROP_X_SLP_PACKAGEID_STR},
86         {E_AIL_PROP_X_SLP_SVC_STR,              AIL_PROP_X_SLP_SVC_STR},
87         {E_AIL_PROP_X_SLP_EXE_PATH,             AIL_PROP_X_SLP_EXE_PATH},
88         {E_AIL_PROP_NODISPLAY_BOOL, AIL_PROP_NODISPLAY_BOOL},
89         {E_AIL_PROP_X_SLP_TASKMANAGE_BOOL, AIL_PROP_NODISPLAY_BOOL},
90         {E_AIL_PROP_X_SLP_MULTIPLE_BOOL, AIL_PROP_X_SLP_MULTIPLE_BOOL},
91         {E_AIL_PROP_X_SLP_REMOVABLE_BOOL, AIL_PROP_X_SLP_REMOVABLE_BOOL},
92         {E_AIL_PROP_X_SLP_APPID_STR, AIL_PROP_X_SLP_APPID_STR},
93         {E_AIL_PROP_X_SLP_PKGID_STR, AIL_PROP_X_SLP_PKGID_STR},
94         {E_AIL_PROP_X_SLP_DOMAIN_STR, AIL_PROP_X_SLP_DOMAIN_STR},
95         {E_AIL_PROP_X_SLP_SUBMODEMAINID_STR, AIL_PROP_X_SLP_SUBMODEMAINID_STR},
96         {E_AIL_PROP_X_SLP_INSTALLEDSTORAGE_STR, AIL_PROP_X_SLP_INSTALLEDSTORAGE_STR},
97         {E_AIL_PROP_X_SLP_TEMP_INT, AIL_PROP_X_SLP_TEMP_INT},
98         {E_AIL_PROP_X_SLP_INSTALLEDTIME_INT, AIL_PROP_X_SLP_INSTALLEDTIME_INT},
99         {E_AIL_PROP_NODISPLAY_BOOL, AIL_PROP_NODISPLAY_BOOL},
100         {E_AIL_PROP_X_SLP_TASKMANAGE_BOOL, AIL_PROP_X_SLP_TASKMANAGE_BOOL},
101         {E_AIL_PROP_X_SLP_MULTIPLE_BOOL, AIL_PROP_X_SLP_MULTIPLE_BOOL},
102         {E_AIL_PROP_X_SLP_REMOVABLE_BOOL, AIL_PROP_X_SLP_REMOVABLE_BOOL},
103         {E_AIL_PROP_X_SLP_ISHORIZONTALSCALE_BOOL, AIL_PROP_X_SLP_ISHORIZONTALSCALE_BOOL},
104         {E_AIL_PROP_X_SLP_ENABLED_BOOL, AIL_PROP_X_SLP_ENABLED_BOOL},
105         {E_AIL_PROP_X_SLP_SUBMODE_BOOL, AIL_PROP_X_SLP_SUBMODE_BOOL}
106 };
107
108 static const char *_ail_convert_to_property(int prop)
109 {
110         int i = 0;
111
112         if (prop < E_AIL_PROP_STR_MIN || prop > E_AIL_PROP_BOOL_MAX)
113                 return NULL;
114
115         for (i=0 ; i < (E_AIL_PROP_BOOL_MAX + 1) ; i++) {
116                 if (prop == prop_map[i].prop) {
117                         return prop_map[i].property;
118                 }
119         }
120
121         return NULL;
122 }
123
124
125
126 static int _get_cmd(const char *arg)
127 {
128         int r;
129         int a;
130
131         if(!arg)
132                 a = 0;
133         else
134                 a = (int)*arg;
135
136         switch (a) {
137         case 'c':
138         case 'C':
139                 r = _CMD_COUNT;
140                 break;
141         case 'f':
142         case 'F':
143                 r = _CMD_FILTER;
144                 break;
145         default:
146                 r = _CMD_UNKNOWN;
147                 break;
148         }
149
150         return r;
151 }
152
153 ail_cb_ret_e appinfo_list_func(const ail_appinfo_h appinfo,  void *user_data)
154 {
155         char *rs = NULL;
156         int t=-1;
157         bool b = 0;
158         int n = 0;
159         struct element e;
160         int i=0;
161         struct element *p;
162         ail_error_e error = AIL_ERROR_OK;
163         ail_cb_ret_e ret = AIL_CB_RET_CONTINUE;
164         p = &e;
165
166         bool err = false;
167         ret = AIL_CB_RET_CONTINUE;
168         for(i = 0; i< E_AIL_PROP_BOOL_MAX+1 && err==false; i ++) {
169                 e.prop = i;
170                 ELEMENT_TYPE(p, t);
171                 switch(t) {
172                         case VAL_TYPE_BOOL:
173                                 error = ail_appinfo_get_bool(appinfo, _ail_convert_to_property(i), &b);
174                                 if (error) ret = AIL_CB_RET_CANCEL;
175                                 printf("%s|",b?"true":"false");
176                                 break;
177                         case VAL_TYPE_INT:
178                                 ail_appinfo_get_int(appinfo, _ail_convert_to_property(i), &n);
179                                 if (error) ret = AIL_CB_RET_CANCEL;
180                                 printf("%d|", n);
181                                 break;
182                         case VAL_TYPE_STR:
183                                 ail_appinfo_get_str(appinfo, _ail_convert_to_property(i), &rs);
184                                 if (error) ret = AIL_CB_RET_CANCEL;
185                                 printf("%s|", rs);
186                                 break;
187                         default:
188                                 fprintf(stderr, "$$$\n");
189                                 err = true;
190                                 break;
191                 }
192         }
193         printf("\n");
194         return ret;
195 }
196
197
198
199 int main(int argc, char *argv[])
200 {
201         int o;
202         bool err;
203         ail_filter_h f;
204         int oi;
205         int c;
206         static struct element e;
207         int t;
208
209         f = NULL;
210         oi = -1;
211         c = _CMD_UNKNOWN;
212
213         static const struct option longopts[] = {
214                 { "help", 0, NULL, 'h' },
215                 { "command", 1, NULL, 'c' },
216                 { "package", 1, &(e.prop), E_AIL_PROP_PACKAGE_STR},
217                 { "name", 1, &(e.prop), E_AIL_PROP_NAME_STR },
218                 { "mimetype", 1, &(e.prop), E_AIL_PROP_MIMETYPE_STR},
219                 { "removable", 1, &(e.prop), E_AIL_PROP_X_SLP_REMOVABLE_BOOL },
220                 { "exec", 1, &(e.prop), E_AIL_PROP_EXEC_STR},
221                 { "type", 1, &(e.prop), E_AIL_PROP_TYPE_STR},
222                 { "icon", 1, &(e.prop), E_AIL_PROP_ICON_STR},
223                 { "categories", 1, &(e.prop), E_AIL_PROP_CATEGORIES_STR},
224                 { "version", 1, &(e.prop), E_AIL_PROP_VERSION_STR},
225                 { "mimetype", 1, &(e.prop), E_AIL_PROP_MIMETYPE_STR},
226                 { "nodisplay", 1, &(e.prop), E_AIL_PROP_NODISPLAY_BOOL},
227                 { "service", 1, &(e.prop), E_AIL_PROP_X_SLP_SERVICE_STR},
228                 { "packagetype", 1, &(e.prop), E_AIL_PROP_X_SLP_PACKAGETYPE_STR},
229                 { "packagecategories", 1, &(e.prop), E_AIL_PROP_X_SLP_PACKAGECATEGORIES_STR},
230                 { "packageid", 1, &(e.prop), E_AIL_PROP_X_SLP_PACKAGEID_STR},
231                 { "svc", 1, &(e.prop), E_AIL_PROP_X_SLP_SVC_STR},
232                 { "taskmanage", 1, &(e.prop), E_AIL_PROP_X_SLP_TASKMANAGE_BOOL},
233                 { "multiple", 1, &(e.prop), E_AIL_PROP_X_SLP_MULTIPLE_BOOL},
234                 { "removable", 1, &(e.prop), E_AIL_PROP_X_SLP_REMOVABLE_BOOL},
235                 { "appid", 1, &(e.prop), E_AIL_PROP_X_SLP_APPID_STR},
236                 { "pkgid", 1, &(e.prop), E_AIL_PROP_X_SLP_PKGID_STR},
237                 { "submode", 1, &(e.prop), E_AIL_PROP_X_SLP_SUBMODE_BOOL},
238                 { "submodemainid", 1, &(e.prop), E_AIL_PROP_X_SLP_SUBMODEMAINID_STR},
239                 { "installedstorage", 1, &(e.prop), E_AIL_PROP_X_SLP_INSTALLEDSTORAGE_STR},
240                 { "domain", 1, &(e.prop), E_AIL_PROP_X_SLP_DOMAIN_STR},
241                 { 0, 0, 0, 0 },
242         };
243
244         if (ail_filter_new(&f) != AIL_ERROR_OK)
245                 return -1;
246
247         err = false;
248
249         while (!err && (o = getopt_long(argc, argv, "c:", longopts, &oi)) >= 0) {
250                 bool b;
251                 struct element *p = &e;
252
253                 ELEMENT_TYPE(p, t);
254
255                 switch (o) {
256                         case 'c':
257                                 c = _get_cmd(optarg);
258                                 break;
259                         case 0:
260                                 switch (t) {
261                                         case VAL_TYPE_BOOL:
262                                                 if(!strcasecmp(optarg, "true") || !strcasecmp(optarg, "1"))
263                                                         b = true;
264                                                 else if (!strcasecmp(optarg, "false") || !strcasecmp(optarg, "0"))
265                                                         b = false;
266                                                 else {
267                                                         err = true;
268                                                         break;
269                                                 }
270                                                 if (ail_filter_add_bool(f, _ail_convert_to_property(e.prop), b) != AIL_ERROR_OK)
271                                                         err = true;
272                                                 break;
273
274                                         case VAL_TYPE_INT:
275                                                 if (ail_filter_add_int(f, _ail_convert_to_property(e.prop), atoi(optarg)) != AIL_ERROR_OK)
276                                                         err = true;
277                                                 break;
278
279                                         case VAL_TYPE_STR:
280                                                 if (ail_filter_add_str(f, _ail_convert_to_property(e.prop), optarg) != AIL_ERROR_OK)
281                                                         err = true;
282                                                 break;
283
284                                         default:
285                                                 err = true;
286                                                 break;
287                                 }
288                                 break;
289                         default:
290                                 err = true;
291                                 break;
292                 }
293         }
294
295         if (err) {
296                 usage(argv[0]);
297         }
298         else {
299                 int n = -1;
300                 switch (c) {
301                         case _CMD_COUNT:
302                                 if (ail_filter_count_appinfo(f, &n) != AIL_ERROR_OK){
303                                         fprintf(stderr, "Error: failed to count appinfo\n");
304                                 }
305                                 else
306                                         fprintf(stderr, "count=%d\n", n);
307                                 break;
308                         case _CMD_FILTER:
309                                 ail_filter_list_appinfo_foreach(f, appinfo_list_func, NULL);
310                                 break;
311                         default:
312                                 break;
313                 }
314         }
315
316         if (f)
317                 ail_filter_destroy(f);
318
319         return 0;
320 }