LCOV - code coverage report
Current view: top level - glib/gio - gio-tool-mime.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 0 76 0.0 %
Date: 2024-04-16 05:15:53 Functions: 0 2 0.0 %
Branches: 0 30 0.0 %

           Branch data     Line data    Source code
       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                 :          0 : get_app_info_for_id (const char *id)
      40                 :            : {
      41                 :            :   GList *list, *l;
      42                 :            :   GAppInfo *ret_info;
      43                 :            : 
      44                 :          0 :   list = g_app_info_get_all ();
      45                 :          0 :   ret_info = NULL;
      46         [ #  # ]:          0 :   for (l = list; l != NULL; l = l->next)
      47                 :            :     {
      48                 :            :       GAppInfo *info;
      49                 :            : 
      50                 :          0 :       info = l->data;
      51   [ #  #  #  # ]:          0 :       if (ret_info == NULL && g_strcmp0 (g_app_info_get_id (info), id) == 0)
      52                 :          0 :         ret_info = info;
      53                 :            :       else
      54                 :          0 :         g_object_unref (info);
      55                 :            :     }
      56                 :          0 :   g_list_free (list);
      57                 :            : 
      58                 :          0 :   return ret_info;
      59                 :            : }
      60                 :            : 
      61                 :            : int
      62                 :          0 : handle_mime (int argc, char *argv[], gboolean do_help)
      63                 :            : {
      64                 :            :   GOptionContext *context;
      65                 :          0 :   GError *error = NULL;
      66                 :            :   gchar *param;
      67                 :            :   const gchar *mimetype;
      68                 :            :   const char *handler;
      69                 :            : 
      70                 :          0 :   g_set_prgname ("gio mime");
      71                 :            : 
      72                 :            :   /* Translators: commandline placeholder */
      73                 :          0 :   param = g_strdup_printf ("%s [%s]", _("MIMETYPE"), _("HANDLER"));
      74                 :          0 :   context = g_option_context_new (param);
      75                 :          0 :   g_free (param);
      76                 :          0 :   g_option_context_set_help_enabled (context, FALSE);
      77                 :          0 :   g_option_context_set_summary (context,
      78                 :          0 :       _("Get or set the handler for a mimetype."));
      79                 :          0 :   g_option_context_set_description (context,
      80                 :          0 :       _("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                 :          0 :   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
      84                 :            : 
      85         [ #  # ]:          0 :   if (do_help)
      86                 :            :     {
      87                 :          0 :       show_help (context, NULL);
      88                 :          0 :       g_option_context_free (context);
      89                 :          0 :       return 0;
      90                 :            :     }
      91                 :            : 
      92         [ #  # ]:          0 :   if (!g_option_context_parse (context, &argc, &argv, &error))
      93                 :            :     {
      94                 :          0 :       show_help (context, error->message);
      95                 :          0 :       g_error_free (error);
      96                 :          0 :       g_option_context_free (context);
      97                 :          0 :       return 1;
      98                 :            :     }
      99                 :            : 
     100   [ #  #  #  # ]:          0 :   if (argc != 2 && argc != 3)
     101                 :            :     {
     102                 :          0 :       show_help (context, _("Must specify a single mimetype, and maybe a handler"));
     103                 :          0 :       g_option_context_free (context);
     104                 :          0 :       return 1;
     105                 :            :     }
     106                 :            : 
     107                 :          0 :   g_option_context_free (context);
     108                 :            : 
     109         [ #  # ]:          0 :   if (argc == 2)
     110                 :            :     {
     111                 :            :       GAppInfo *info;
     112                 :            : 
     113                 :          0 :       mimetype = argv[1];
     114                 :            : 
     115                 :          0 :       info = g_app_info_get_default_for_type (mimetype, FALSE);
     116         [ #  # ]:          0 :       if (!info)
     117                 :            :         {
     118                 :          0 :           g_print (_("No default applications for “%s”\n"), mimetype);
     119                 :            :         }
     120                 :            :       else
     121                 :            :         {
     122                 :            :           GList *list, *l;
     123                 :            : 
     124                 :          0 :           g_print (_("Default application for “%s”: %s\n"), mimetype, g_app_info_get_id (info));
     125                 :          0 :           g_object_unref (info);
     126                 :            : 
     127                 :          0 :           list = g_app_info_get_all_for_type (mimetype);
     128         [ #  # ]:          0 :           if (list != NULL)
     129                 :          0 :             g_print (_("Registered applications:\n"));
     130                 :            :           else
     131                 :          0 :             g_print (_("No registered applications\n"));
     132         [ #  # ]:          0 :           for (l = list; l != NULL; l = l->next)
     133                 :            :             {
     134                 :          0 :               info = l->data;
     135                 :          0 :               g_print ("\t%s\n", g_app_info_get_id (info));
     136                 :          0 :               g_object_unref (info);
     137                 :            :             }
     138                 :          0 :           g_list_free (list);
     139                 :            : 
     140                 :          0 :           list = g_app_info_get_recommended_for_type (mimetype);
     141         [ #  # ]:          0 :           if (list != NULL)
     142                 :          0 :             g_print (_("Recommended applications:\n"));
     143                 :            :           else
     144                 :          0 :             g_print (_("No recommended applications\n"));
     145         [ #  # ]:          0 :           for (l = list; l != NULL; l = l->next)
     146                 :            :             {
     147                 :          0 :               info = l->data;
     148                 :          0 :               g_print ("\t%s\n", g_app_info_get_id (info));
     149                 :          0 :               g_object_unref (info);
     150                 :            :             }
     151                 :          0 :           g_list_free (list);
     152                 :            :         }
     153                 :            :     }
     154                 :            :   else
     155                 :            :     {
     156                 :            :       GAppInfo *info;
     157                 :            : 
     158                 :          0 :       mimetype = argv[1];
     159                 :          0 :       handler = argv[2];
     160                 :            : 
     161                 :          0 :       info = get_app_info_for_id (handler);
     162         [ #  # ]:          0 :       if (info == NULL)
     163                 :            :         {
     164                 :          0 :           print_error (_("Failed to load info for handler “%s”"), handler);
     165                 :          0 :           return 1;
     166                 :            :         }
     167                 :            : 
     168         [ #  # ]:          0 :       if (g_app_info_set_as_default_for_type (info, mimetype, &error) == FALSE)
     169                 :            :         {
     170                 :          0 :           print_error (_("Failed to set “%s” as the default handler for “%s”: %s\n"),
     171                 :          0 :                        handler, mimetype, error->message);
     172                 :          0 :           g_error_free (error);
     173                 :          0 :           g_object_unref (info);
     174                 :          0 :           return 1;
     175                 :            :         }
     176                 :          0 :       g_print ("Set %s as the default for %s\n", g_app_info_get_id (info), mimetype);
     177                 :          0 :       g_object_unref (info);
     178                 :            :     }
     179                 :            : 
     180                 :          0 :   return 0;
     181                 :            : }

Generated by: LCOV version 1.14