LCOV - code coverage report
Current view: top level - gio - gcontenttype.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 100.0 % 48 48
Test Date: 2025-12-09 05:20:19 Functions: 100.0 % 16 16
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
       2                 :             : 
       3                 :             : /* GIO - GLib Input, Output and Streaming Library
       4                 :             :  *
       5                 :             :  * Copyright (C) 2006-2007 Red Hat, Inc.
       6                 :             :  *
       7                 :             :  * SPDX-License-Identifier: LGPL-2.1-or-later
       8                 :             :  *
       9                 :             :  * This library is free software; you can redistribute it and/or
      10                 :             :  * modify it under the terms of the GNU Lesser General Public
      11                 :             :  * License as published by the Free Software Foundation; either
      12                 :             :  * version 2.1 of the License, or (at your option) any later version.
      13                 :             :  *
      14                 :             :  * This library is distributed in the hope that it will be useful,
      15                 :             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16                 :             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17                 :             :  * Lesser General Public License for more details.
      18                 :             :  *
      19                 :             :  * You should have received a copy of the GNU Lesser General
      20                 :             :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      21                 :             :  *
      22                 :             :  * Author: Alexander Larsson <alexl@redhat.com>
      23                 :             :  */
      24                 :             : 
      25                 :             : #include "config.h"
      26                 :             : #include "gcontenttypeprivate.h"
      27                 :             : #include "gfile.h"
      28                 :             : 
      29                 :             : 
      30                 :             : 
      31                 :             : /**
      32                 :             :  * g_content_type_set_mime_dirs:
      33                 :             :  * @dirs: (array zero-terminated=1) (nullable): %NULL-terminated list of
      34                 :             :  *    directories to load MIME data from, including any `mime/` subdirectory,
      35                 :             :  *    and with the first directory to try listed first
      36                 :             :  *
      37                 :             :  * Set the list of directories used by GIO to load the MIME database.
      38                 :             :  * If @dirs is %NULL, the directories used are the default:
      39                 :             :  *
      40                 :             :  *  - the `mime` subdirectory of the directory in `$XDG_DATA_HOME`
      41                 :             :  *  - the `mime` subdirectory of every directory in `$XDG_DATA_DIRS`
      42                 :             :  *
      43                 :             :  * This function is intended to be used when writing tests that depend on
      44                 :             :  * information stored in the MIME database, in order to control the data.
      45                 :             :  *
      46                 :             :  * Typically, in case your tests use %G_TEST_OPTION_ISOLATE_DIRS, but they
      47                 :             :  * depend on the system’s MIME database, you should call this function
      48                 :             :  * with @dirs set to %NULL before calling g_test_init(), for instance:
      49                 :             :  *
      50                 :             :  * |[<!-- language="C" -->
      51                 :             :  *   // Load MIME data from the system
      52                 :             :  *   g_content_type_set_mime_dirs (NULL);
      53                 :             :  *   // Isolate the environment
      54                 :             :  *   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
      55                 :             :  *
      56                 :             :  *   …
      57                 :             :  *
      58                 :             :  *   return g_test_run ();
      59                 :             :  * ]|
      60                 :             :  *
      61                 :             :  * Since: 2.60
      62                 :             :  */
      63                 :             : void
      64                 :           1 : g_content_type_set_mime_dirs (const gchar * const *dirs)
      65                 :             : {
      66                 :           1 :   g_content_type_set_mime_dirs_impl (dirs);
      67                 :           1 : }
      68                 :             : 
      69                 :             : /**
      70                 :             :  * g_content_type_get_mime_dirs:
      71                 :             :  *
      72                 :             :  * Get the list of directories which MIME data is loaded from. See
      73                 :             :  * g_content_type_set_mime_dirs() for details.
      74                 :             :  *
      75                 :             :  * Returns: (transfer none) (array zero-terminated=1): %NULL-terminated list of
      76                 :             :  *    directories to load MIME data from, including any `mime/` subdirectory,
      77                 :             :  *    and with the first directory to try listed first
      78                 :             :  * Since: 2.60
      79                 :             :  */
      80                 :             : const gchar * const *
      81                 :           3 : g_content_type_get_mime_dirs (void)
      82                 :             : {
      83                 :           3 :   return g_content_type_get_mime_dirs_impl ();
      84                 :             : }
      85                 :             : 
      86                 :             : /**
      87                 :             :  * g_content_type_equals:
      88                 :             :  * @type1: a content type string
      89                 :             :  * @type2: a content type string
      90                 :             :  *
      91                 :             :  * Compares two content types for equality.
      92                 :             :  *
      93                 :             :  * Returns: %TRUE if the two strings are identical or equivalent,
      94                 :             :  *     %FALSE otherwise.
      95                 :             :  */
      96                 :             : gboolean
      97                 :         378 : g_content_type_equals (const gchar *type1,
      98                 :             :                        const gchar *type2)
      99                 :             : {
     100                 :         378 :   g_return_val_if_fail (type1 != NULL, FALSE);
     101                 :         378 :   g_return_val_if_fail (type2 != NULL, FALSE);
     102                 :             : 
     103                 :         378 :   return g_content_type_equals_impl (type1, type2);
     104                 :             : }
     105                 :             : 
     106                 :             : /**
     107                 :             :  * g_content_type_is_a:
     108                 :             :  * @type: a content type string
     109                 :             :  * @supertype: a content type string
     110                 :             :  *
     111                 :             :  * Determines if @type is a subset of @supertype.
     112                 :             :  *
     113                 :             :  * Returns: %TRUE if @type is a kind of @supertype,
     114                 :             :  *     %FALSE otherwise.
     115                 :             :  */
     116                 :             : gboolean
     117                 :          10 : g_content_type_is_a (const gchar *type,
     118                 :             :                      const gchar *supertype)
     119                 :             : {
     120                 :          10 :   g_return_val_if_fail (type != NULL, FALSE);
     121                 :          10 :   g_return_val_if_fail (supertype != NULL, FALSE);
     122                 :             : 
     123                 :          10 :   return g_content_type_is_a_impl (type, supertype);
     124                 :             : }
     125                 :             : 
     126                 :             : /**
     127                 :             :  * g_content_type_is_mime_type:
     128                 :             :  * @type: a content type string
     129                 :             :  * @mime_type: a mime type string
     130                 :             :  *
     131                 :             :  * Determines if @type is a subset of @mime_type.
     132                 :             :  * Convenience wrapper around g_content_type_is_a().
     133                 :             :  *
     134                 :             :  * Returns: %TRUE if @type is a kind of @mime_type,
     135                 :             :  *     %FALSE otherwise.
     136                 :             :  *
     137                 :             :  * Since: 2.52
     138                 :             :  */
     139                 :             : gboolean
     140                 :           1 : g_content_type_is_mime_type (const gchar *type,
     141                 :             :                              const gchar *mime_type)
     142                 :             : {
     143                 :           1 :   g_return_val_if_fail (type != NULL, FALSE);
     144                 :           1 :   g_return_val_if_fail (mime_type != NULL, FALSE);
     145                 :             : 
     146                 :           1 :   return g_content_type_is_mime_type_impl (type, mime_type);
     147                 :             : }
     148                 :             : 
     149                 :             : /**
     150                 :             :  * g_content_type_is_unknown:
     151                 :             :  * @type: a content type string
     152                 :             :  *
     153                 :             :  * Checks if the content type is the generic "unknown" type.
     154                 :             :  * On UNIX this is the "application/octet-stream" mimetype,
     155                 :             :  * while on win32 it is "*" and on OSX it is a dynamic type
     156                 :             :  * or octet-stream.
     157                 :             :  *
     158                 :             :  * Returns: %TRUE if the type is the unknown type.
     159                 :             :  */
     160                 :             : gboolean
     161                 :           1 : g_content_type_is_unknown (const gchar *type)
     162                 :             : {
     163                 :           1 :   g_return_val_if_fail (type != NULL, FALSE);
     164                 :             : 
     165                 :           1 :   return g_content_type_is_unknown_impl (type);
     166                 :             : }
     167                 :             : 
     168                 :             : /**
     169                 :             :  * g_content_type_get_description:
     170                 :             :  * @type: a content type string
     171                 :             :  *
     172                 :             :  * Gets the human readable description of the content type.
     173                 :             :  *
     174                 :             :  * Returns: a short description of the content type @type. Free the
     175                 :             :  *     returned string with g_free()
     176                 :             :  */
     177                 :             : gchar *
     178                 :           1 : g_content_type_get_description (const gchar *type)
     179                 :             : {
     180                 :           1 :   g_return_val_if_fail (type != NULL, NULL);
     181                 :             : 
     182                 :           1 :   return g_content_type_get_description_impl (type);
     183                 :             : }
     184                 :             : 
     185                 :             : /**
     186                 :             :  * g_content_type_get_mime_type:
     187                 :             :  * @type: a content type string
     188                 :             :  *
     189                 :             :  * Gets the mime type for the content type, if one is registered.
     190                 :             :  *
     191                 :             :  * Returns: (nullable) (transfer full): the registered mime type for the
     192                 :             :  *     given @type, or %NULL if unknown; free with g_free().
     193                 :             :  */
     194                 :             : char *
     195                 :           2 : g_content_type_get_mime_type (const char *type)
     196                 :             : {
     197                 :           2 :   g_return_val_if_fail (type != NULL, NULL);
     198                 :             : 
     199                 :           2 :   return g_content_type_get_mime_type_impl (type);
     200                 :             : }
     201                 :             : 
     202                 :             : /**
     203                 :             :  * g_content_type_get_icon:
     204                 :             :  * @type: a content type string
     205                 :             :  *
     206                 :             :  * Gets the icon for a content type.
     207                 :             :  *
     208                 :             :  * Returns: (transfer full): #GIcon corresponding to the content type. Free the returned
     209                 :             :  *     object with g_object_unref()
     210                 :             :  */
     211                 :             : GIcon *
     212                 :          78 : g_content_type_get_icon (const gchar *type)
     213                 :             : {
     214                 :          78 :   g_return_val_if_fail (type != NULL, NULL);
     215                 :             : 
     216                 :          78 :   return g_content_type_get_icon_impl (type);
     217                 :             : }
     218                 :             : 
     219                 :             : /**
     220                 :             :  * g_content_type_get_symbolic_icon:
     221                 :             :  * @type: a content type string
     222                 :             :  *
     223                 :             :  * Gets the symbolic icon for a content type.
     224                 :             :  *
     225                 :             :  * Returns: (transfer full): symbolic #GIcon corresponding to the content type.
     226                 :             :  *     Free the returned object with g_object_unref()
     227                 :             :  *
     228                 :             :  * Since: 2.34
     229                 :             :  */
     230                 :             : GIcon *
     231                 :          78 : g_content_type_get_symbolic_icon (const gchar *type)
     232                 :             : {
     233                 :          78 :   g_return_val_if_fail (type != NULL, NULL);
     234                 :             : 
     235                 :          78 :   return g_content_type_get_symbolic_icon_impl (type);
     236                 :             : }
     237                 :             : 
     238                 :             : /**
     239                 :             :  * g_content_type_get_generic_icon_name:
     240                 :             :  * @type: a content type string
     241                 :             :  *
     242                 :             :  * Gets the generic icon name for a content type.
     243                 :             :  *
     244                 :             :  * See the
     245                 :             :  * [shared-mime-info](http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec)
     246                 :             :  * specification for more on the generic icon name.
     247                 :             :  *
     248                 :             :  * Returns: (nullable): the registered generic icon name for the given @type,
     249                 :             :  *     or %NULL if unknown. Free with g_free()
     250                 :             :  *
     251                 :             :  * Since: 2.34
     252                 :             :  */
     253                 :             : gchar *
     254                 :         156 : g_content_type_get_generic_icon_name (const gchar *type)
     255                 :             : {
     256                 :         156 :   g_return_val_if_fail (type != NULL, NULL);
     257                 :             : 
     258                 :         156 :   return g_content_type_get_generic_icon_name_impl (type);
     259                 :             : }
     260                 :             : 
     261                 :             : /**
     262                 :             :  * g_content_type_can_be_executable:
     263                 :             :  * @type: a content type string
     264                 :             :  *
     265                 :             :  * Checks if a content type can be executable. Note that for instance
     266                 :             :  * things like text files can be executables (i.e. scripts and batch files).
     267                 :             :  *
     268                 :             :  * Returns: %TRUE if the file type corresponds to a type that
     269                 :             :  *     can be executable, %FALSE otherwise.
     270                 :             :  */
     271                 :             : gboolean
     272                 :           3 : g_content_type_can_be_executable (const gchar *type)
     273                 :             : {
     274                 :           3 :   g_return_val_if_fail (type != NULL, FALSE);
     275                 :             : 
     276                 :           3 :   return g_content_type_can_be_executable_impl (type);
     277                 :             : }
     278                 :             : 
     279                 :             : /**
     280                 :             :  * g_content_type_from_mime_type:
     281                 :             :  * @mime_type: a mime type string
     282                 :             :  *
     283                 :             :  * Tries to find a content type based on the mime type name.
     284                 :             :  *
     285                 :             :  * Returns: (nullable): Newly allocated string with content type or
     286                 :             :  *     %NULL. Free with g_free()
     287                 :             :  *
     288                 :             :  * Since: 2.18
     289                 :             :  **/
     290                 :             : gchar *
     291                 :         167 : g_content_type_from_mime_type (const gchar *mime_type)
     292                 :             : {
     293                 :         167 :   g_return_val_if_fail (mime_type != NULL, NULL);
     294                 :             : 
     295                 :         167 :   return g_content_type_from_mime_type_impl (mime_type);
     296                 :             : }
     297                 :             : 
     298                 :             : /**
     299                 :             :  * g_content_type_guess:
     300                 :             :  * @filename: (nullable) (type filename): a path, or %NULL
     301                 :             :  * @data: (nullable) (array length=data_size): a stream of data, or %NULL
     302                 :             :  * @data_size: the size of @data
     303                 :             :  * @result_uncertain: (out) (optional): return location for the certainty
     304                 :             :  *     of the result, or %NULL
     305                 :             :  *
     306                 :             :  * Guesses the content type based on example data. If the function is
     307                 :             :  * uncertain, @result_uncertain will be set to %TRUE. Either @filename
     308                 :             :  * or @data may be %NULL, in which case the guess will be based solely
     309                 :             :  * on the other argument.
     310                 :             :  *
     311                 :             :  * Returns: a string indicating a guessed content type for the
     312                 :             :  *     given data. Free with g_free()
     313                 :             :  */
     314                 :             : gchar *
     315                 :          50 : g_content_type_guess (const gchar  *filename,
     316                 :             :                       const guchar *data,
     317                 :             :                       gsize         data_size,
     318                 :             :                       gboolean     *result_uncertain)
     319                 :             : {
     320                 :          50 :   return g_content_type_guess_impl (filename, data, data_size, result_uncertain);
     321                 :             : }
     322                 :             : 
     323                 :             : /**
     324                 :             :  * g_content_types_get_registered:
     325                 :             :  *
     326                 :             :  * Gets a list of strings containing all the registered content types
     327                 :             :  * known to the system. The list and its data should be freed using
     328                 :             :  * `g_list_free_full (list, g_free)`.
     329                 :             :  *
     330                 :             :  * Returns: (element-type utf8) (transfer full): list of the registered
     331                 :             :  *     content types
     332                 :             :  */
     333                 :             : GList *
     334                 :           1 : g_content_types_get_registered (void)
     335                 :             : {
     336                 :           1 :   return g_content_types_get_registered_impl ();
     337                 :             : }
     338                 :             : 
     339                 :             : /**
     340                 :             :  * g_content_type_guess_for_tree:
     341                 :             :  * @root: the root of the tree to guess a type for
     342                 :             :  *
     343                 :             :  * Tries to guess the type of the tree with root @root, by
     344                 :             :  * looking at the files it contains. The result is an array
     345                 :             :  * of content types, with the best guess coming first.
     346                 :             :  *
     347                 :             :  * The types returned all have the form x-content/foo, e.g.
     348                 :             :  * x-content/audio-cdda (for audio CDs) or x-content/image-dcf
     349                 :             :  * (for a camera memory card). See the
     350                 :             :  * [shared-mime-info](http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec)
     351                 :             :  * specification for more on x-content types.
     352                 :             :  *
     353                 :             :  * This function is useful in the implementation of
     354                 :             :  * g_mount_guess_content_type().
     355                 :             :  *
     356                 :             :  * Returns: (transfer full) (array zero-terminated=1): an %NULL-terminated
     357                 :             :  *     array of zero or more content types. Free with g_strfreev()
     358                 :             :  *
     359                 :             :  * Since: 2.18
     360                 :             :  */
     361                 :             : gchar **
     362                 :           4 : g_content_type_guess_for_tree (GFile *root)
     363                 :             : {
     364                 :           4 :   g_return_val_if_fail (G_IS_FILE (root), NULL);
     365                 :             : 
     366                 :           4 :   return g_content_type_guess_for_tree_impl (root);
     367                 :             : }
        

Generated by: LCOV version 2.0-1