• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  * Author: Matthias Clasen <mclasen@redhat.com>
18  */
19 
20 #include "config.h"
21 
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>
29 
30 #include "gio-tool.h"
31 
32 static const GOptionEntry entries[] = {
33   { NULL }
34 };
35 
36 static GAppInfo *
get_app_info_for_id(const char * id)37 get_app_info_for_id (const char *id)
38 {
39   GList *list, *l;
40   GAppInfo *ret_info;
41 
42   list = g_app_info_get_all ();
43   ret_info = NULL;
44   for (l = list; l != NULL; l = l->next)
45     {
46       GAppInfo *info;
47 
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);
53     }
54   g_list_free (list);
55 
56   return ret_info;
57 }
58 
59 int
handle_mime(int argc,char * argv[],gboolean do_help)60 handle_mime (int argc, char *argv[], gboolean do_help)
61 {
62   GOptionContext *context;
63   GError *error = NULL;
64   gchar *param;
65   const gchar *mimetype;
66   const char *handler;
67 
68   g_set_prgname ("gio mime");
69 
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);
82 
83   if (do_help)
84     {
85       show_help (context, NULL);
86       g_option_context_free (context);
87       return 0;
88     }
89 
90   if (!g_option_context_parse (context, &argc, &argv, &error))
91     {
92       show_help (context, error->message);
93       g_error_free (error);
94       g_option_context_free (context);
95       return 1;
96     }
97 
98   if (argc != 2 && argc != 3)
99     {
100       show_help (context, _("Must specify a single mimetype, and maybe a handler"));
101       g_option_context_free (context);
102       return 1;
103     }
104 
105   g_option_context_free (context);
106 
107   if (argc == 2)
108     {
109       GAppInfo *info;
110 
111       mimetype = argv[1];
112 
113       info = g_app_info_get_default_for_type (mimetype, FALSE);
114       if (!info)
115         {
116           g_print (_("No default applications for “%s”\n"), mimetype);
117         }
118       else
119         {
120           GList *list, *l;
121 
122           g_print (_("Default application for “%s”: %s\n"), mimetype, g_app_info_get_id (info));
123           g_object_unref (info);
124 
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)
131             {
132               info = l->data;
133               g_print ("\t%s\n", g_app_info_get_id (info));
134               g_object_unref (info);
135             }
136           g_list_free (list);
137 
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)
144             {
145               info = l->data;
146               g_print ("\t%s\n", g_app_info_get_id (info));
147               g_object_unref (info);
148             }
149           g_list_free (list);
150         }
151     }
152   else
153     {
154       GAppInfo *info;
155 
156       mimetype = argv[1];
157       handler = argv[2];
158 
159       info = get_app_info_for_id (handler);
160       if (info == NULL)
161         {
162           print_error (_("Failed to load info for handler “%s”"), handler);
163           return 1;
164         }
165 
166       if (g_app_info_set_as_default_for_type (info, mimetype, &error) == FALSE)
167         {
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;
173         }
174       g_print ("Set %s as the default for %s\n", g_app_info_get_id (info), mimetype);
175       g_object_unref (info);
176     }
177 
178   return 0;
179 }
180