Fix Prevent and remove compile warning messages
[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;
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         return NULL;
121 }
122
123 static int _get_cmd(const char *arg)
124 {
125         int r;
126         int a;
127
128         if (!arg)
129                 a = 0;
130         else
131                 a = (int)*arg;
132
133         switch (a) {
134         case 'c':
135         case 'C':
136                 r = _CMD_COUNT;
137                 break;
138         case 'f':
139         case 'F':
140                 r = _CMD_FILTER;
141                 break;
142         default:
143                 r = _CMD_UNKNOWN;
144                 break;
145         }
146
147         return r;
148 }
149
150 static ail_cb_ret_e appinfo_list_func(const ail_appinfo_h appinfo,  void *user_data, uid_t uid)
151 {
152         char *rs = NULL;
153         int t = -1;
154         bool b = 0;
155         int n = 0;
156         struct element e;
157         int i = 0;
158         struct element *p;
159         bool err = false;
160         ail_error_e error = AIL_ERROR_OK;
161         ail_cb_ret_e ret = AIL_CB_RET_CONTINUE;
162         p = &e;
163
164         ret = AIL_CB_RET_CONTINUE;
165         for (i = 0; i < E_AIL_PROP_BOOL_MAX + 1 && err == false; i++) {
166                 e.prop = i;
167                 ELEMENT_TYPE(p, t);
168                 switch (t) {
169                 case VAL_TYPE_BOOL:
170                         error = ail_appinfo_get_bool(appinfo, _ail_convert_to_property(i), &b);
171                         if (error)
172                                 ret = AIL_CB_RET_CANCEL;
173                         printf("%s|", b ? "true" : "false");
174                         break;
175                 case VAL_TYPE_INT:
176                         ail_appinfo_get_int(appinfo, _ail_convert_to_property(i), &n);
177                         if (error)
178                                 ret = AIL_CB_RET_CANCEL;
179                         printf("%d|", n);
180                         break;
181                 case VAL_TYPE_STR:
182                         ail_appinfo_get_str(appinfo, _ail_convert_to_property(i), &rs);
183                         if (error)
184                                 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
194         printf("\n");
195         return ret;
196 }
197
198 int main(int argc, char *argv[])
199 {
200         int o;
201         bool err;
202         ail_filter_h f;
203         int oi;
204         int c;
205         static struct element e;
206         int t;
207
208         f = NULL;
209         oi = -1;
210         c = _CMD_UNKNOWN;
211
212         static const struct option longopts[] = {
213                 { "help", 0, NULL, 'h' },
214                 { "command", 1, NULL, 'c' },
215                 { "package", 1, &(e.prop), E_AIL_PROP_PACKAGE_STR},
216                 { "name", 1, &(e.prop), E_AIL_PROP_NAME_STR },
217                 { "mimetype", 1, &(e.prop), E_AIL_PROP_MIMETYPE_STR},
218                 { "removable", 1, &(e.prop), E_AIL_PROP_X_SLP_REMOVABLE_BOOL },
219                 { "exec", 1, &(e.prop), E_AIL_PROP_EXEC_STR},
220                 { "type", 1, &(e.prop), E_AIL_PROP_TYPE_STR},
221                 { "icon", 1, &(e.prop), E_AIL_PROP_ICON_STR},
222                 { "categories", 1, &(e.prop), E_AIL_PROP_CATEGORIES_STR},
223                 { "version", 1, &(e.prop), E_AIL_PROP_VERSION_STR},
224                 { "mimetype", 1, &(e.prop), E_AIL_PROP_MIMETYPE_STR},
225                 { "nodisplay", 1, &(e.prop), E_AIL_PROP_NODISPLAY_BOOL},
226                 { "service", 1, &(e.prop), E_AIL_PROP_X_SLP_SERVICE_STR},
227                 { "packagetype", 1, &(e.prop), E_AIL_PROP_X_SLP_PACKAGETYPE_STR},
228                 { "packagecategories", 1, &(e.prop), E_AIL_PROP_X_SLP_PACKAGECATEGORIES_STR},
229                 { "packageid", 1, &(e.prop), E_AIL_PROP_X_SLP_PACKAGEID_STR},
230                 { "svc", 1, &(e.prop), E_AIL_PROP_X_SLP_SVC_STR},
231                 { "taskmanage", 1, &(e.prop), E_AIL_PROP_X_SLP_TASKMANAGE_BOOL},
232                 { "multiple", 1, &(e.prop), E_AIL_PROP_X_SLP_MULTIPLE_BOOL},
233                 { "removable", 1, &(e.prop), E_AIL_PROP_X_SLP_REMOVABLE_BOOL},
234                 { "appid", 1, &(e.prop), E_AIL_PROP_X_SLP_APPID_STR},
235                 { "pkgid", 1, &(e.prop), E_AIL_PROP_X_SLP_PKGID_STR},
236                 { "submode", 1, &(e.prop), E_AIL_PROP_X_SLP_SUBMODE_BOOL},
237                 { "submodemainid", 1, &(e.prop), E_AIL_PROP_X_SLP_SUBMODEMAINID_STR},
238                 { "installedstorage", 1, &(e.prop), E_AIL_PROP_X_SLP_INSTALLEDSTORAGE_STR},
239                 { "domain", 1, &(e.prop), E_AIL_PROP_X_SLP_DOMAIN_STR},
240                 { 0, 0, 0, 0 },
241         };
242
243         if (ail_filter_new(&f) != AIL_ERROR_OK)
244                 return -1;
245
246         err = false;
247
248         while (!err && (o = getopt_long(argc, argv, "c:", longopts, &oi)) >= 0) {
249                 bool b;
250                 struct element *p = &e;
251
252                 ELEMENT_TYPE(p, t);
253
254                 switch (o) {
255                 case 'c':
256                         c = _get_cmd(optarg);
257                         break;
258                 case 0:
259                         switch (t) {
260                         case VAL_TYPE_BOOL:
261                                 if (!strcasecmp(optarg, "true") || !strcasecmp(optarg, "1"))
262                                         b = true;
263                                 else if (!strcasecmp(optarg, "false") || !strcasecmp(optarg, "0"))
264                                         b = false;
265                                 else {
266                                         err = true;
267                                         break;
268                                 }
269
270                                 if (ail_filter_add_bool(f, _ail_convert_to_property(e.prop), b) != AIL_ERROR_OK)
271                                         err = true;
272                                 break;
273                         case VAL_TYPE_INT:
274                                 if (ail_filter_add_int(f, _ail_convert_to_property(e.prop), atoi(optarg)) != AIL_ERROR_OK)
275                                         err = true;
276                                 break;
277                         case VAL_TYPE_STR:
278                                 if (ail_filter_add_str(f, _ail_convert_to_property(e.prop), optarg) != AIL_ERROR_OK)
279                                         err = true;
280                                 break;
281                         default:
282                                 err = true;
283                                 break;
284                         }
285                         break;
286                 default:
287                         err = true;
288                         break;
289                 }
290         }
291
292         if (err)
293                 usage(argv[0]);
294         else {
295                 int n = -1;
296                 switch (c) {
297                 case _CMD_COUNT:
298                         if (ail_filter_count_appinfo(f, &n) != AIL_ERROR_OK)
299                                 fprintf(stderr, "Error: failed to count appinfo\n");
300                         else
301                                 fprintf(stderr, "count=%d\n", n);
302                         break;
303                 case _CMD_FILTER:
304                         ail_filter_list_appinfo_foreach(f, appinfo_list_func, NULL);
305                         break;
306                 default:
307                         break;
308                 }
309         }
310
311         if (f)
312                 ail_filter_destroy(f);
313
314         return 0;
315 }