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.1 of the License, 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>
32 static const GOptionEntry entries[] = {
37 get_app_info_for_id (const char *id)
42 list = g_app_info_get_all ();
44 for (l = list; l != NULL; l = l->next)
49 if (ret_info == NULL && g_strcmp0 (g_app_info_get_id (info), id) == 0)
52 g_object_unref (info);
60 handle_mime (int argc, char *argv[], gboolean do_help)
62 GOptionContext *context;
65 const gchar *mimetype;
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);
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);
85 show_help (context, NULL);
86 g_option_context_free (context);
90 if (!g_option_context_parse (context, &argc, &argv, &error))
92 show_help (context, error->message);
94 g_option_context_free (context);
98 if (argc != 2 && argc != 3)
100 show_help (context, _("Must specify a single mimetype, and maybe a handler"));
101 g_option_context_free (context);
105 g_option_context_free (context);
113 info = g_app_info_get_default_for_type (mimetype, FALSE);
116 g_print (_("No default applications for “%s”\n"), mimetype);
122 g_print (_("Default application for “%s”: %s\n"), mimetype, g_app_info_get_id (info));
123 g_object_unref (info);
125 list = g_app_info_get_all_for_type (mimetype);
127 g_print (_("Registered applications:\n"));
129 g_print (_("No registered applications\n"));
130 for (l = list; l != NULL; l = l->next)
133 g_print ("\t%s\n", g_app_info_get_id (info));
134 g_object_unref (info);
138 list = g_app_info_get_recommended_for_type (mimetype);
140 g_print (_("Recommended applications:\n"));
142 g_print (_("No recommended applications\n"));
143 for (l = list; l != NULL; l = l->next)
146 g_print ("\t%s\n", g_app_info_get_id (info));
147 g_object_unref (info);
159 info = get_app_info_for_id (handler);
162 print_error (_("Failed to load info for handler “%s”"), handler);
166 if (g_app_info_set_as_default_for_type (info, mimetype, &error) == FALSE)
168 print_error (_("Failed to set “%s” as the default handler for “%s”: %s\n"),
169 handler, mimetype, error->message);
170 g_error_free (error);
171 g_object_unref (info);
174 g_print ("Set %s as the default for %s\n", g_app_info_get_id (info), mimetype);
175 g_object_unref (info);