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