Initialize variable
[glib.git] / gio / gio-tool-mime.c
blobf564b1fd9fb6b5b5d1389a8da2be6129f02daf41
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.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>
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 g_option_context_free (context);
87 return 0;
90 if (!g_option_context_parse (context, &argc, &argv, &error))
92 show_help (context, error->message);
93 g_error_free (error);
94 g_option_context_free (context);
95 return 1;
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);
102 return 1;
105 g_option_context_free (context);
107 if (argc == 2)
109 GAppInfo *info;
111 mimetype = argv[1];
113 info = g_app_info_get_default_for_type (mimetype, FALSE);
114 if (!info)
116 g_print (_("No default applications for “%s”\n"), mimetype);
118 else
120 GList *list, *l;
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);
126 if (list != NULL)
127 g_print (_("Registered applications:\n"));
128 else
129 g_print (_("No registered applications\n"));
130 for (l = list; l != NULL; l = l->next)
132 info = l->data;
133 g_print ("\t%s\n", g_app_info_get_id (info));
134 g_object_unref (info);
136 g_list_free (list);
138 list = g_app_info_get_recommended_for_type (mimetype);
139 if (list != NULL)
140 g_print (_("Recommended applications:\n"));
141 else
142 g_print (_("No recommended applications\n"));
143 for (l = list; l != NULL; l = l->next)
145 info = l->data;
146 g_print ("\t%s\n", g_app_info_get_id (info));
147 g_object_unref (info);
149 g_list_free (list);
152 else
154 GAppInfo *info;
156 mimetype = argv[1];
157 handler = argv[2];
159 info = get_app_info_for_id (handler);
160 if (info == NULL)
162 print_error (_("Failed to load info for handler “%s”"), handler);
163 return 1;
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);
172 return 1;
174 g_print ("Set %s as the default for %s\n", g_app_info_get_id (info), mimetype);
175 g_object_unref (info);
178 return 0;