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>
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
);
89 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
91 show_help (context
, error
->message
);
96 if (argc
!= 2 && argc
!= 3)
98 show_help (context
, _("Must specify a single mimetype, and maybe a handler"));
102 g_option_context_free (context
);
110 info
= g_app_info_get_default_for_type (mimetype
, FALSE
);
113 g_print (_("No default applications for “%s”\n"), mimetype
);
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
);
124 g_print (_("Registered applications:\n"));
126 g_print (_("No registered applications\n"));
127 for (l
= list
; l
!= NULL
; l
= l
->next
)
130 g_print ("\t%s\n", g_app_info_get_id (info
));
131 g_object_unref (info
);
135 list
= g_app_info_get_recommended_for_type (mimetype
);
137 g_print (_("Recommended applications:\n"));
139 g_print (_("No recommended applications\n"));
140 for (l
= list
; l
!= NULL
; l
= l
->next
)
143 g_print ("\t%s\n", g_app_info_get_id (info
));
144 g_object_unref (info
);
156 info
= get_app_info_for_id (handler
);
159 g_printerr (_("Failed to load info for handler “%s”\n"), handler
);
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
);
171 g_print ("Set %s as the default for %s\n", g_app_info_get_id (info
), mimetype
);
172 g_object_unref (info
);