apps test: add new "monitor" subcommand
[platform/upstream/glib.git] / gio / tests / apps.c
1 #include <gio/gio.h>
2 #include <gio/gdesktopappinfo.h>
3 #include <locale.h>
4 #include <stdlib.h>
5
6 static void
7 print (const gchar *str)
8 {
9   g_print ("%s\n", str ? str : "nil");
10 }
11
12 static void
13 print_app_list (GList *list)
14 {
15   while (list)
16     {
17       GAppInfo *info = list->data;
18       print (g_app_info_get_id (info));
19       list = g_list_delete_link (list, list);
20       g_object_unref (info);
21     }
22 }
23
24 static void
25 quit (gpointer user_data)
26 {
27   g_print ("appinfo database changed.\n");
28   exit (0);
29 }
30
31 int
32 main (int argc, char **argv)
33 {
34   setlocale (LC_ALL, "");
35
36   if (argv[1] == NULL)
37     ;
38   else if (g_str_equal (argv[1], "list"))
39     {
40       GList *all, *i;
41
42       all = g_app_info_get_all ();
43       for (i = all; i; i = i->next)
44         g_print ("%s%s", g_app_info_get_id (i->data), i->next ? " " : "\n");
45       g_list_free_full (all, g_object_unref);
46     }
47   else if (g_str_equal (argv[1], "search"))
48     {
49       gchar ***results;
50       gint i, j;
51
52       results = g_desktop_app_info_search (argv[2]);
53       for (i = 0; results[i]; i++)
54         {
55           for (j = 0; results[i][j]; j++)
56             g_print ("%s%s", j ? " " : "", results[i][j]);
57           g_print ("\n");
58           g_strfreev (results[i]);
59         }
60       g_free (results);
61     }
62   else if (g_str_equal (argv[1], "implementations"))
63     {
64       GList *results;
65
66       results = g_desktop_app_info_get_implementations (argv[2]);
67       print_app_list (results);
68     }
69   else if (g_str_equal (argv[1], "show-info"))
70     {
71       GAppInfo *info;
72
73       info = (GAppInfo *) g_desktop_app_info_new (argv[2]);
74       if (info)
75         {
76           print (g_app_info_get_id (info));
77           print (g_app_info_get_name (info));
78           print (g_app_info_get_display_name (info));
79           print (g_app_info_get_description (info));
80           g_object_unref (info);
81         }
82     }
83   else if (g_str_equal (argv[1], "default-for-type"))
84     {
85       GAppInfo *info;
86
87       info = g_app_info_get_default_for_type (argv[2], FALSE);
88
89       if (info)
90         {
91           print (g_app_info_get_id (info));
92           g_object_unref (info);
93         }
94     }
95   else if (g_str_equal (argv[1], "recommended-for-type"))
96     {
97       GList *list;
98
99       list = g_app_info_get_recommended_for_type (argv[2]);
100       print_app_list (list);
101     }
102   else if (g_str_equal (argv[1], "all-for-type"))
103     {
104       GList *list;
105
106       list = g_app_info_get_all_for_type (argv[2]);
107       print_app_list (list);
108     }
109
110   else if (g_str_equal (argv[1], "fallback-for-type"))
111     {
112       GList *list;
113
114       list = g_app_info_get_fallback_for_type (argv[2]);
115       print_app_list (list);
116     }
117
118   else if (g_str_equal (argv[1], "should-show"))
119     {
120       GAppInfo *info;
121
122       info = (GAppInfo *) g_desktop_app_info_new (argv[2]);
123       if (info)
124         {
125           g_print ("%s\n", g_app_info_should_show (info) ? "true" : "false");
126           g_object_unref (info);
127         }
128     }
129
130   else if (g_str_equal (argv[1], "monitor"))
131     {
132       GAppInfoMonitor *monitor;
133       GAppInfo *info;
134
135       monitor = g_app_info_monitor_get ();
136
137       info = (GAppInfo *) g_desktop_app_info_new ("this-desktop-file-does-not-exist");
138       g_assert (!info);
139
140       g_signal_connect (monitor, "changed", G_CALLBACK (quit), NULL);
141
142       while (1)
143         g_main_context_iteration (NULL, TRUE);
144     }
145
146   return 0;
147 }