tests: expand 'apps' tool
[platform/upstream/glib.git] / gio / tests / apps.c
1 #include <gio/gio.h>
2 #include <gio/gdesktopappinfo.h>
3 #include <locale.h>
4
5 static void
6 print (const gchar *str)
7 {
8   g_print ("%s\n", str ? str : "nil");
9 }
10
11 static void
12 print_app_list (GList *list)
13 {
14   while (list)
15     {
16       GAppInfo *info = list->data;
17       print (g_app_info_get_id (info));
18       list = g_list_delete_link (list, list);
19       g_object_unref (info);
20     }
21 }
22
23 int
24 main (int argc, char **argv)
25 {
26   setlocale (LC_ALL, "");
27
28   if (argv[1] == NULL)
29     ;
30   else if (g_str_equal (argv[1], "list"))
31     {
32       GList *all, *i;
33
34       all = g_app_info_get_all ();
35       for (i = all; i; i = i->next)
36         g_print ("%s%s", g_app_info_get_id (i->data), i->next ? " " : "\n");
37       g_list_free_full (all, g_object_unref);
38     }
39   else if (g_str_equal (argv[1], "search"))
40     {
41       gchar ***results;
42       gint i, j;
43
44       results = g_desktop_app_info_search (argv[2]);
45       for (i = 0; results[i]; i++)
46         {
47           for (j = 0; results[i][j]; j++)
48             g_print ("%s%s", j ? " " : "", results[i][j]);
49           g_print ("\n");
50           g_strfreev (results[i]);
51         }
52       g_free (results);
53     }
54   else if (g_str_equal (argv[1], "show-info"))
55     {
56       GAppInfo *info;
57
58       info = (GAppInfo *) g_desktop_app_info_new (argv[2]);
59       if (info)
60         {
61           print (g_app_info_get_id (info));
62           print (g_app_info_get_name (info));
63           print (g_app_info_get_display_name (info));
64           print (g_app_info_get_description (info));
65           g_object_unref (info);
66         }
67     }
68   else if (g_str_equal (argv[1], "default-for-type"))
69     {
70       GAppInfo *info;
71
72       info = g_app_info_get_default_for_type (argv[2], FALSE);
73
74       if (info)
75         {
76           print (g_app_info_get_id (info));
77           g_object_unref (info);
78         }
79     }
80   else if (g_str_equal (argv[1], "recommended-for-type"))
81     {
82       GList *list;
83
84       list = g_app_info_get_recommended_for_type (argv[2]);
85       print_app_list (list);
86     }
87   else if (g_str_equal (argv[1], "all-for-type"))
88     {
89       GList *list;
90
91       list = g_app_info_get_all_for_type (argv[2]);
92       print_app_list (list);
93     }
94
95   else if (g_str_equal (argv[1], "fallback-for-type"))
96     {
97       GList *list;
98
99       list = g_app_info_get_fallback_for_type (argv[2]);
100       print_app_list (list);
101     }
102
103   return 0;
104 }