Add some more cases to the app-id unit tests
[glib.git] / gio / gio-tool-mime.c
blob92a96c0fb5818d37beaf62eeecb8e2af12fc8347
1 /*
2 * Copyright 2015 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Matthias Clasen <mclasen@redhat.com>
20 #include "config.h"
22 #include <gio/gio.h>
23 #include <gi18n.h>
24 #include <locale.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <errno.h>
30 #include "gio-tool.h"
32 static const GOptionEntry entries[] = {
33 { NULL }
36 static GAppInfo *
37 get_app_info_for_id (const char *id)
39 GList *list, *l;
40 GAppInfo *ret_info;
42 list = g_app_info_get_all ();
43 ret_info = NULL;
44 for (l = list; l != NULL; l = l->next)
46 GAppInfo *info;
48 info = l->data;
49 if (ret_info == NULL && g_strcmp0 (g_app_info_get_id (info), id) == 0)
50 ret_info = info;
51 else
52 g_object_unref (info);
54 g_list_free (list);
56 return ret_info;
59 int
60 handle_mime (int argc, char *argv[], gboolean do_help)
62 GOptionContext *context;
63 GError *error = NULL;
64 gchar *param;
65 const gchar *mimetype;
66 const char *handler;
68 g_set_prgname ("gio mime");
70 /* Translators: commandline placeholder */
71 param = g_strdup_printf ("%s [%s]", _("MIMETYPE"), _("HANDLER"));
72 context = g_option_context_new (param);
73 g_free (param);
74 g_option_context_set_help_enabled (context, FALSE);
75 g_option_context_set_summary (context,
76 _("Get or set the handler for a mimetype."));
77 g_option_context_set_description (context,
78 _("If no handler is given, lists registered and recommended applications\n"
79 "for the mimetype. If a handler is given, it is set as the default\n"
80 "handler for the mimetype."));
81 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
83 if (do_help)
85 show_help (context, NULL);
86 return 0;
89 if (!g_option_context_parse (context, &argc, &argv, &error))
91 show_help (context, error->message);
92 g_error_free (error);
93 return 1;
96 if (argc != 2 && argc != 3)
98 show_help (context, _("Must specify a single mimetype, and maybe a handler"));
99 return 1;
102 g_option_context_free (context);
104 if (argc == 2)
106 GAppInfo *info;
108 mimetype = argv[1];
110 info = g_app_info_get_default_for_type (mimetype, FALSE);
111 if (!info)
113 g_print (_("No default applications for “%s”\n"), mimetype);
115 else
117 GList *list, *l;
119 g_print (_("Default application for “%s”: %s\n"), mimetype, g_app_info_get_id (info));
120 g_object_unref (info);
122 list = g_app_info_get_all_for_type (mimetype);
123 if (list != NULL)
124 g_print (_("Registered applications:\n"));
125 else
126 g_print (_("No registered applications\n"));
127 for (l = list; l != NULL; l = l->next)
129 info = l->data;
130 g_print ("\t%s\n", g_app_info_get_id (info));
131 g_object_unref (info);
133 g_list_free (list);
135 list = g_app_info_get_recommended_for_type (mimetype);
136 if (list != NULL)
137 g_print (_("Recommended applications:\n"));
138 else
139 g_print (_("No recommended applications\n"));
140 for (l = list; l != NULL; l = l->next)
142 info = l->data;
143 g_print ("\t%s\n", g_app_info_get_id (info));
144 g_object_unref (info);
146 g_list_free (list);
149 else
151 GAppInfo *info;
153 mimetype = argv[1];
154 handler = argv[2];
156 info = get_app_info_for_id (handler);
157 if (info == NULL)
159 g_printerr (_("Failed to load info for handler “%s”\n"), handler);
160 return 1;
163 if (g_app_info_set_as_default_for_type (info, mimetype, &error) == FALSE)
165 g_printerr (_("Failed to set “%s” as the default handler for “%s”: %s\n"),
166 handler, mimetype, error->message);
167 g_error_free (error);
168 g_object_unref (info);
169 return 1;
171 g_print ("Set %s as the default for %s\n", g_app_info_get_id (info), mimetype);
172 g_object_unref (info);
175 return 0;