LCOV - code coverage report
Current view: top level - glib/gio - gthemedicon.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 190 201 94.5 %
Date: 2024-03-26 05:16:46 Functions: 22 22 100.0 %
Branches: 73 89 82.0 %

           Branch data     Line data    Source code
       1                 :            : /* GIO - GLib Input, Output and Streaming Library
       2                 :            :  * 
       3                 :            :  * Copyright (C) 2006-2007 Red Hat, Inc.
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :            :  *
       7                 :            :  * This library is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU Lesser General Public
       9                 :            :  * License as published by the Free Software Foundation; either
      10                 :            :  * version 2.1 of the License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This library is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :            :  * Lesser General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU Lesser General
      18                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :            :  *
      20                 :            :  * Author: Alexander Larsson <alexl@redhat.com>
      21                 :            :  */
      22                 :            : 
      23                 :            : #include "config.h"
      24                 :            : 
      25                 :            : #include <string.h>
      26                 :            : 
      27                 :            : #include "gthemedicon.h"
      28                 :            : #include "gicon.h"
      29                 :            : #include "gioerror.h"
      30                 :            : #include "glibintl.h"
      31                 :            : 
      32                 :            : 
      33                 :            : /**
      34                 :            :  * GThemedIcon:
      35                 :            :  *
      36                 :            :  * `GThemedIcon` is an implementation of [iface@Gio.Icon] that supports icon
      37                 :            :  * themes.
      38                 :            :  *
      39                 :            :  * `GThemedIcon` contains a list of all of the icons present in an icon
      40                 :            :  * theme, so that icons can be looked up quickly. `GThemedIcon` does
      41                 :            :  * not provide actual pixmaps for icons, just the icon names.
      42                 :            :  * Ideally something like [method@Gtk.IconTheme.choose_icon] should be used to
      43                 :            :  * resolve the list of names so that fallback icons work nicely with
      44                 :            :  * themes that inherit other themes.
      45                 :            :  **/
      46                 :            : 
      47                 :            : static void g_themed_icon_icon_iface_init (GIconIface *iface);
      48                 :            : 
      49                 :            : struct _GThemedIcon
      50                 :            : {
      51                 :            :   GObject parent_instance;
      52                 :            :   
      53                 :            :   char     **init_names;
      54                 :            :   char     **names;
      55                 :            :   gboolean   use_default_fallbacks;
      56                 :            : };
      57                 :            : 
      58                 :            : struct _GThemedIconClass
      59                 :            : {
      60                 :            :   GObjectClass parent_class;
      61                 :            : };
      62                 :            : 
      63                 :            : enum
      64                 :            : {
      65                 :            :   PROP_0,
      66                 :            :   PROP_NAME,
      67                 :            :   PROP_NAMES,
      68                 :            :   PROP_USE_DEFAULT_FALLBACKS
      69                 :            : };
      70                 :            : 
      71                 :            : static void g_themed_icon_update_names (GThemedIcon *themed);
      72                 :            : 
      73   [ +  +  +  -  :       2027 : G_DEFINE_TYPE_WITH_CODE (GThemedIcon, g_themed_icon, G_TYPE_OBJECT,
                   +  + ]
      74                 :            :                          G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
      75                 :            :                                                 g_themed_icon_icon_iface_init))
      76                 :            : 
      77                 :            : static void
      78                 :         19 : g_themed_icon_get_property (GObject    *object,
      79                 :            :                             guint       prop_id,
      80                 :            :                             GValue     *value,
      81                 :            :                             GParamSpec *pspec)
      82                 :            : {
      83                 :         19 :   GThemedIcon *icon = G_THEMED_ICON (object);
      84                 :            : 
      85      [ +  +  - ]:         19 :   switch (prop_id)
      86                 :            :     {
      87                 :          9 :       case PROP_NAMES:
      88                 :          9 :         g_value_set_boxed (value, icon->init_names);
      89                 :          9 :         break;
      90                 :            : 
      91                 :         10 :       case PROP_USE_DEFAULT_FALLBACKS:
      92                 :         10 :         g_value_set_boolean (value, icon->use_default_fallbacks);
      93                 :         10 :         break;
      94                 :            : 
      95                 :          0 :       default:
      96                 :          0 :         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      97                 :            :     }
      98                 :         19 : }
      99                 :            : 
     100                 :            : static void
     101                 :        846 : g_themed_icon_set_property (GObject      *object,
     102                 :            :                             guint         prop_id,
     103                 :            :                             const GValue *value,
     104                 :            :                             GParamSpec   *pspec)
     105                 :            : {
     106                 :        846 :   GThemedIcon *icon = G_THEMED_ICON (object);
     107                 :            :   gchar **names;
     108                 :            :   const gchar *name;
     109                 :            : 
     110   [ +  +  +  - ]:        846 :   switch (prop_id)
     111                 :            :     {
     112                 :        282 :       case PROP_NAME:
     113                 :        282 :         name = g_value_get_string (value);
     114                 :            : 
     115         [ +  + ]:        282 :         if (!name)
     116                 :        172 :           break;
     117                 :            : 
     118         [ -  + ]:        110 :         if (icon->init_names)
     119                 :          0 :           g_strfreev (icon->init_names);
     120                 :            : 
     121                 :        110 :         icon->init_names = g_new (char *, 2);
     122                 :        110 :         icon->init_names[0] = g_strdup (name);
     123                 :        110 :         icon->init_names[1] = NULL;
     124                 :        110 :         break;
     125                 :            : 
     126                 :        282 :       case PROP_NAMES:
     127                 :        282 :         names = g_value_dup_boxed (value);
     128                 :            : 
     129         [ +  + ]:        282 :         if (!names)
     130                 :        110 :           break;
     131                 :            : 
     132         [ -  + ]:        172 :         if (icon->init_names)
     133                 :          0 :           g_strfreev (icon->init_names);
     134                 :            : 
     135                 :        172 :         icon->init_names = names;
     136                 :        172 :         break;
     137                 :            : 
     138                 :        282 :       case PROP_USE_DEFAULT_FALLBACKS:
     139                 :        282 :         icon->use_default_fallbacks = g_value_get_boolean (value);
     140                 :        282 :         break;
     141                 :            : 
     142                 :          0 :       default:
     143                 :          0 :         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     144                 :            :     }
     145                 :        846 : }
     146                 :            : 
     147                 :            : static void
     148                 :        282 : g_themed_icon_constructed (GObject *object)
     149                 :            : {
     150                 :        282 :   g_themed_icon_update_names (G_THEMED_ICON (object));
     151                 :        282 : }
     152                 :            : 
     153                 :            : static void
     154                 :        282 : g_themed_icon_finalize (GObject *object)
     155                 :            : {
     156                 :            :   GThemedIcon *themed;
     157                 :            : 
     158                 :        282 :   themed = G_THEMED_ICON (object);
     159                 :            : 
     160                 :        282 :   g_strfreev (themed->init_names);
     161                 :        282 :   g_strfreev (themed->names);
     162                 :            : 
     163                 :        282 :   G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize (object);
     164                 :        282 : }
     165                 :            : 
     166                 :            : static void
     167                 :         25 : g_themed_icon_class_init (GThemedIconClass *klass)
     168                 :            : {
     169                 :         25 :   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
     170                 :            :   
     171                 :         25 :   gobject_class->finalize = g_themed_icon_finalize;
     172                 :         25 :   gobject_class->constructed = g_themed_icon_constructed;
     173                 :         25 :   gobject_class->set_property = g_themed_icon_set_property;
     174                 :         25 :   gobject_class->get_property = g_themed_icon_get_property;
     175                 :            : 
     176                 :            :   /**
     177                 :            :    * GThemedIcon:name:
     178                 :            :    *
     179                 :            :    * The icon name.
     180                 :            :    */
     181                 :         25 :   g_object_class_install_property (gobject_class, PROP_NAME,
     182                 :            :                                    g_param_spec_string ("name", NULL, NULL,
     183                 :            :                                                         NULL,
     184                 :            :                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
     185                 :            : 
     186                 :            :   /**
     187                 :            :    * GThemedIcon:names:
     188                 :            :    *
     189                 :            :    * A %NULL-terminated array of icon names.
     190                 :            :    */
     191                 :         25 :   g_object_class_install_property (gobject_class, PROP_NAMES,
     192                 :            :                                    g_param_spec_boxed ("names", NULL, NULL,
     193                 :            :                                                        G_TYPE_STRV,
     194                 :            :                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
     195                 :            : 
     196                 :            :   /**
     197                 :            :    * GThemedIcon:use-default-fallbacks:
     198                 :            :    *
     199                 :            :    * Whether to use the default fallbacks found by shortening the icon name 
     200                 :            :    * at '-' characters. If the "names" array has more than one element, 
     201                 :            :    * ignores any past the first.
     202                 :            :    *
     203                 :            :    * For example, if the icon name was "gnome-dev-cdrom-audio", the array 
     204                 :            :    * would become
     205                 :            :    * |[<!-- language="C" -->
     206                 :            :    * {
     207                 :            :    *   "gnome-dev-cdrom-audio",
     208                 :            :    *   "gnome-dev-cdrom",
     209                 :            :    *   "gnome-dev",
     210                 :            :    *   "gnome",
     211                 :            :    *   NULL
     212                 :            :    * };
     213                 :            :    * ]|
     214                 :            :    */
     215                 :         25 :   g_object_class_install_property (gobject_class, PROP_USE_DEFAULT_FALLBACKS,
     216                 :            :                                    g_param_spec_boolean ("use-default-fallbacks", NULL, NULL,
     217                 :            :                                                          FALSE,
     218                 :            :                                                          G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
     219                 :         25 : }
     220                 :            : 
     221                 :            : static void
     222                 :        282 : g_themed_icon_init (GThemedIcon *themed)
     223                 :            : {
     224                 :        282 :   themed->init_names = NULL;
     225                 :        282 :   themed->names      = NULL;
     226                 :        282 : }
     227                 :            : 
     228                 :            : /**
     229                 :            :  * g_themed_icon_update_names:
     230                 :            :  * @themed: a #GThemedIcon.
     231                 :            :  *
     232                 :            :  * Update the actual icon name list, based on the requested names (from
     233                 :            :  * construction, or later added with g_themed_icon_prepend_name() and
     234                 :            :  * g_themed_icon_append_name()).
     235                 :            :  * The order of the list matters, indicating priority:
     236                 :            :  * - The first requested icon is first in priority.
     237                 :            :  * - If "use-default-fallbacks" is #TRUE, then it is followed by all its
     238                 :            :  *   fallbacks (starting from top to lower context levels).
     239                 :            :  * - Then next requested icons, and optionally their fallbacks, follow.
     240                 :            :  * - Finally all the style variants (symbolic or regular, opposite to whatever
     241                 :            :  *   is the requested style) follow in the same order.
     242                 :            :  *
     243                 :            :  * An icon is not added twice in the list if it was previously added.
     244                 :            :  *
     245                 :            :  * For instance, if requested names are:
     246                 :            :  * [ "some-icon-symbolic", "some-other-icon" ]
     247                 :            :  * and use-default-fallbacks is TRUE, the final name list shall be:
     248                 :            :  * [ "some-icon-symbolic", "some-symbolic", "some-other-icon",
     249                 :            :  *   "some-other", "some", "some-icon", "some-other-icon-symbolic",
     250                 :            :  *   "some-other-symbolic" ]
     251                 :            :  *
     252                 :            :  * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon
     253                 :            :  **/
     254                 :            : static void
     255                 :        293 : g_themed_icon_update_names (GThemedIcon *themed)
     256                 :            : {
     257                 :        293 :   GList *names    = NULL;
     258                 :        293 :   GList *variants = NULL;
     259                 :            :   GList *iter;
     260                 :            :   guint  i;
     261                 :            : 
     262                 :        293 :   g_return_if_fail (themed->init_names != NULL && themed->init_names[0] != NULL);
     263                 :            : 
     264         [ +  + ]:        961 :   for (i = 0; themed->init_names[i]; i++)
     265                 :            :     {
     266                 :            :       gchar    *name;
     267                 :            :       gboolean  is_symbolic;
     268                 :            : 
     269   [ +  -  -  +  :        668 :       is_symbolic = g_str_has_suffix (themed->init_names[i], "-symbolic");
                   +  + ]
     270         [ +  + ]:        668 :       if (is_symbolic)
     271                 :        193 :         name = g_strndup (themed->init_names[i], strlen (themed->init_names[i]) - 9);
     272                 :            :       else
     273                 :        950 :         name = g_strdup (themed->init_names[i]);
     274                 :            : 
     275         [ +  + ]:        668 :       if (g_list_find_custom (names, name, (GCompareFunc) g_strcmp0))
     276                 :            :         {
     277                 :         30 :           g_free (name);
     278                 :         30 :           continue;
     279                 :            :         }
     280                 :            : 
     281         [ +  + ]:        638 :       if (is_symbolic)
     282                 :        326 :         names = g_list_prepend (names, g_strdup (themed->init_names[i]));
     283                 :            :       else
     284                 :        475 :         names = g_list_prepend (names, name);
     285                 :            : 
     286         [ +  + ]:        638 :       if (themed->use_default_fallbacks)
     287                 :            :         {
     288                 :            :           char *dashp;
     289                 :            :           char *last;
     290                 :            : 
     291                 :         10 :           last = name;
     292                 :            : 
     293         [ +  + ]:         23 :           while ((dashp = strrchr (last, '-')) != NULL)
     294                 :            :             {
     295                 :         13 :               gchar *tmp = last;
     296                 :            :               gchar *fallback;
     297                 :            : 
     298                 :         13 :               last = g_strndup (last, dashp - last);
     299         [ +  + ]:         13 :               if (is_symbolic)
     300                 :            :                 {
     301                 :          2 :                   g_free (tmp);
     302                 :          2 :                   fallback = g_strdup_printf ("%s-symbolic", last);
     303                 :            :                 }
     304                 :            :               else
     305                 :         11 :                 fallback = last;
     306         [ -  + ]:         13 :               if (g_list_find_custom (names, fallback, (GCompareFunc) g_strcmp0))
     307                 :            :                 {
     308                 :          0 :                   g_free (fallback);
     309                 :          0 :                   break;
     310                 :            :                 }
     311                 :         13 :               names = g_list_prepend (names, fallback);
     312                 :            :             }
     313         [ +  + ]:         10 :           if (is_symbolic)
     314                 :          2 :             g_free (last);
     315                 :            :         }
     316         [ +  + ]:        628 :       else if (is_symbolic)
     317                 :        161 :         g_free (name);
     318                 :            :     }
     319         [ +  + ]:        944 :   for (iter = names; iter; iter = iter->next)
     320                 :            :     {
     321                 :        651 :       gchar    *name = (gchar *) iter->data;
     322                 :            :       gchar    *variant;
     323                 :            :       gboolean  is_symbolic;
     324                 :            : 
     325   [ +  -  -  +  :        651 :       is_symbolic = g_str_has_suffix (name, "-symbolic");
                   +  + ]
     326         [ +  + ]:        651 :       if (is_symbolic)
     327                 :        165 :         variant = g_strndup (name, strlen (name) - 9);
     328                 :            :       else
     329                 :        486 :         variant = g_strdup_printf ("%s-symbolic", name);
     330   [ +  +  -  + ]:        984 :       if (g_list_find_custom (names, variant, (GCompareFunc) g_strcmp0) ||
     331                 :        333 :           g_list_find_custom (variants, variant, (GCompareFunc) g_strcmp0))
     332                 :            :         {
     333                 :        318 :           g_free (variant);
     334                 :        318 :           continue;
     335                 :            :         }
     336                 :            : 
     337                 :        333 :       variants = g_list_prepend (variants, variant);
     338                 :            :     }
     339                 :        293 :   names = g_list_reverse (names);
     340                 :            : 
     341                 :        293 :   g_strfreev (themed->names);
     342                 :        293 :   themed->names = g_new (char *, g_list_length (names) + g_list_length (variants) + 1);
     343                 :            : 
     344         [ +  + ]:        944 :   for (iter = names, i = 0; iter; iter = iter->next, i++)
     345                 :        651 :     themed->names[i] = iter->data;
     346         [ +  + ]:        626 :   for (iter = variants; iter; iter = iter->next, i++)
     347                 :        333 :     themed->names[i] = iter->data;
     348                 :        293 :   themed->names[i] = NULL;
     349                 :            : 
     350                 :        293 :   g_list_free (names);
     351                 :        293 :   g_list_free (variants);
     352                 :            : 
     353                 :        293 :   g_object_notify (G_OBJECT (themed), "names");
     354                 :            : }
     355                 :            : 
     356                 :            : /**
     357                 :            :  * g_themed_icon_new:
     358                 :            :  * @iconname: a string containing an icon name.
     359                 :            :  * 
     360                 :            :  * Creates a new themed icon for @iconname.
     361                 :            :  * 
     362                 :            :  * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
     363                 :            :  **/
     364                 :            : GIcon *
     365                 :        106 : g_themed_icon_new (const char *iconname)
     366                 :            : {
     367                 :        106 :   g_return_val_if_fail (iconname != NULL, NULL);
     368                 :            : 
     369                 :        106 :   return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, NULL));
     370                 :            : }
     371                 :            : 
     372                 :            : /**
     373                 :            :  * g_themed_icon_new_from_names:
     374                 :            :  * @iconnames: (array length=len): an array of strings containing icon names.
     375                 :            :  * @len: the length of the @iconnames array, or -1 if @iconnames is 
     376                 :            :  *     %NULL-terminated
     377                 :            :  * 
     378                 :            :  * Creates a new themed icon for @iconnames.
     379                 :            :  * 
     380                 :            :  * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon
     381                 :            :  **/
     382                 :            : GIcon *
     383                 :        172 : g_themed_icon_new_from_names (char **iconnames,
     384                 :            :                               int    len)
     385                 :            : {
     386                 :            :   GIcon *icon;
     387                 :            : 
     388                 :        172 :   g_return_val_if_fail (iconnames != NULL, NULL);
     389                 :            : 
     390         [ +  + ]:        172 :   if (len >= 0)
     391                 :            :     {
     392                 :            :       char **names;
     393                 :            :       int i;
     394                 :            : 
     395                 :        171 :       names = g_new (char *, len + 1);
     396                 :            : 
     397         [ +  + ]:        703 :       for (i = 0; i < len; i++)
     398                 :        532 :         names[i] = iconnames[i];
     399                 :            : 
     400                 :        171 :       names[i] = NULL;
     401                 :            : 
     402                 :        171 :       icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", names, NULL));
     403                 :            : 
     404                 :        171 :       g_free (names);
     405                 :            :     }
     406                 :            :   else
     407                 :          1 :     icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", iconnames, NULL));
     408                 :            : 
     409                 :        172 :   return icon;
     410                 :            : }
     411                 :            : 
     412                 :            : /**
     413                 :            :  * g_themed_icon_new_with_default_fallbacks:
     414                 :            :  * @iconname: a string containing an icon name
     415                 :            :  *
     416                 :            :  * Creates a new themed icon for @iconname, and all the names
     417                 :            :  * that can be created by shortening @iconname at '-' characters.
     418                 :            :  * 
     419                 :            :  * In the following example, @icon1 and @icon2 are equivalent:
     420                 :            :  * |[<!-- language="C" -->
     421                 :            :  * const char *names[] = { 
     422                 :            :  *   "gnome-dev-cdrom-audio",
     423                 :            :  *   "gnome-dev-cdrom",
     424                 :            :  *   "gnome-dev",
     425                 :            :  *   "gnome"
     426                 :            :  * };
     427                 :            :  *
     428                 :            :  * icon1 = g_themed_icon_new_from_names (names, 4);
     429                 :            :  * icon2 = g_themed_icon_new_with_default_fallbacks ("gnome-dev-cdrom-audio");
     430                 :            :  * ]|
     431                 :            :  *
     432                 :            :  * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
     433                 :            :  */
     434                 :            : GIcon *
     435                 :          4 : g_themed_icon_new_with_default_fallbacks (const char *iconname)
     436                 :            : {
     437                 :          4 :   g_return_val_if_fail (iconname != NULL, NULL);
     438                 :            : 
     439                 :          4 :   return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, "use-default-fallbacks", TRUE, NULL));
     440                 :            : }
     441                 :            : 
     442                 :            : 
     443                 :            : /**
     444                 :            :  * g_themed_icon_get_names:
     445                 :            :  * @icon: a #GThemedIcon.
     446                 :            :  *
     447                 :            :  * Gets the names of icons from within @icon.
     448                 :            :  *
     449                 :            :  * Returns: (transfer none): a list of icon names.
     450                 :            :  */
     451                 :            : const char * const *
     452                 :          7 : g_themed_icon_get_names (GThemedIcon *icon)
     453                 :            : {
     454                 :          7 :   g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
     455                 :          7 :   return (const char * const *)icon->names;
     456                 :            : }
     457                 :            : 
     458                 :            : /**
     459                 :            :  * g_themed_icon_append_name:
     460                 :            :  * @icon: a #GThemedIcon
     461                 :            :  * @iconname: name of icon to append to list of icons from within @icon.
     462                 :            :  *
     463                 :            :  * Append a name to the list of icons from within @icon.
     464                 :            :  *
     465                 :            :  * Note that doing so invalidates the hash computed by prior calls
     466                 :            :  * to g_icon_hash().
     467                 :            :  */
     468                 :            : void
     469                 :         10 : g_themed_icon_append_name (GThemedIcon *icon, 
     470                 :            :                            const char  *iconname)
     471                 :            : {
     472                 :            :   guint num_names;
     473                 :            : 
     474                 :         10 :   g_return_if_fail (G_IS_THEMED_ICON (icon));
     475                 :         10 :   g_return_if_fail (iconname != NULL);
     476                 :            : 
     477                 :         10 :   num_names = g_strv_length (icon->init_names);
     478                 :         10 :   icon->init_names = g_realloc (icon->init_names, sizeof (char*) * (num_names + 2));
     479                 :         10 :   icon->init_names[num_names] = g_strdup (iconname);
     480                 :         10 :   icon->init_names[num_names + 1] = NULL;
     481                 :            : 
     482                 :         10 :   g_themed_icon_update_names (icon);
     483                 :            : }
     484                 :            : 
     485                 :            : /**
     486                 :            :  * g_themed_icon_prepend_name:
     487                 :            :  * @icon: a #GThemedIcon
     488                 :            :  * @iconname: name of icon to prepend to list of icons from within @icon.
     489                 :            :  *
     490                 :            :  * Prepend a name to the list of icons from within @icon.
     491                 :            :  *
     492                 :            :  * Note that doing so invalidates the hash computed by prior calls
     493                 :            :  * to g_icon_hash().
     494                 :            :  *
     495                 :            :  * Since: 2.18
     496                 :            :  */
     497                 :            : void
     498                 :          1 : g_themed_icon_prepend_name (GThemedIcon *icon, 
     499                 :            :                             const char  *iconname)
     500                 :            : {
     501                 :            :   guint num_names;
     502                 :            :   gchar **names;
     503                 :            :   gint i;
     504                 :            : 
     505                 :          1 :   g_return_if_fail (G_IS_THEMED_ICON (icon));
     506                 :          1 :   g_return_if_fail (iconname != NULL);
     507                 :            : 
     508                 :          1 :   num_names = g_strv_length (icon->init_names);
     509                 :          1 :   names = g_new (char*, num_names + 2);
     510         [ +  + ]:          2 :   for (i = 0; icon->init_names[i]; i++)
     511                 :          1 :     names[i + 1] = icon->init_names[i];
     512                 :          1 :   names[0] = g_strdup (iconname);
     513                 :          1 :   names[num_names + 1] = NULL;
     514                 :            : 
     515                 :          1 :   g_free (icon->init_names);
     516                 :          1 :   icon->init_names = names;
     517                 :            : 
     518                 :          1 :   g_themed_icon_update_names (icon);
     519                 :            : }
     520                 :            : 
     521                 :            : static guint
     522                 :         18 : g_themed_icon_hash (GIcon *icon)
     523                 :            : {
     524                 :         18 :   GThemedIcon *themed = G_THEMED_ICON (icon);
     525                 :            :   guint hash;
     526                 :            :   int i;
     527                 :            : 
     528                 :         18 :   hash = 0;
     529                 :            : 
     530         [ +  + ]:         72 :   for (i = 0; themed->names[i] != NULL; i++)
     531                 :         54 :     hash ^= g_str_hash (themed->names[i]);
     532                 :            :   
     533                 :         18 :   return hash;
     534                 :            : }
     535                 :            : 
     536                 :            : static gboolean
     537                 :         23 : g_themed_icon_equal (GIcon *icon1,
     538                 :            :                      GIcon *icon2)
     539                 :            : {
     540                 :         23 :   GThemedIcon *themed1 = G_THEMED_ICON (icon1);
     541                 :         23 :   GThemedIcon *themed2 = G_THEMED_ICON (icon2);
     542                 :            :   int i;
     543                 :            : 
     544   [ +  +  +  - ]:        107 :   for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
     545                 :            :     {
     546         [ -  + ]:         84 :       if (!g_str_equal (themed1->names[i], themed2->names[i]))
     547                 :          0 :         return FALSE;
     548                 :            :     }
     549                 :            : 
     550   [ +  -  +  - ]:         23 :   return themed1->names[i] == NULL && themed2->names[i] == NULL;
     551                 :            : }
     552                 :            : 
     553                 :            : 
     554                 :            : static gboolean
     555                 :          7 : g_themed_icon_to_tokens (GIcon *icon,
     556                 :            :                          GPtrArray *tokens,
     557                 :            :                          gint  *out_version)
     558                 :            : {
     559                 :          7 :   GThemedIcon *themed_icon = G_THEMED_ICON (icon);
     560                 :            :   int n;
     561                 :            : 
     562                 :          7 :   g_return_val_if_fail (out_version != NULL, FALSE);
     563                 :            : 
     564                 :          7 :   *out_version = 0;
     565                 :            : 
     566         [ +  + ]:         45 :   for (n = 0; themed_icon->names[n] != NULL; n++)
     567                 :         38 :     g_ptr_array_add (tokens,
     568                 :         76 :                      g_strdup (themed_icon->names[n]));
     569                 :            :   
     570                 :          7 :   return TRUE;
     571                 :            : }
     572                 :            : 
     573                 :            : static GIcon *
     574                 :          6 : g_themed_icon_from_tokens (gchar  **tokens,
     575                 :            :                            gint     num_tokens,
     576                 :            :                            gint     version,
     577                 :            :                            GError **error)
     578                 :            : {
     579                 :            :   GIcon *icon;
     580                 :            :   gchar **names;
     581                 :            :   int n;
     582                 :            : 
     583                 :          6 :   icon = NULL;
     584                 :            : 
     585         [ -  + ]:          6 :   if (version != 0)
     586                 :            :     {
     587                 :          0 :       g_set_error (error,
     588                 :            :                    G_IO_ERROR,
     589                 :            :                    G_IO_ERROR_INVALID_ARGUMENT,
     590                 :            :                    _("Can’t handle version %d of GThemedIcon encoding"),
     591                 :            :                    version);
     592                 :          0 :       goto out;
     593                 :            :     }
     594                 :            :   
     595                 :          6 :   names = g_new0 (gchar *, num_tokens + 1);
     596         [ +  + ]:         36 :   for (n = 0; n < num_tokens; n++)
     597                 :         30 :     names[n] = tokens[n];
     598                 :          6 :   names[n] = NULL;
     599                 :            : 
     600                 :          6 :   icon = g_themed_icon_new_from_names (names, num_tokens);
     601                 :          6 :   g_free (names);
     602                 :            : 
     603                 :          6 :  out:
     604                 :          6 :   return icon;
     605                 :            : }
     606                 :            : 
     607                 :            : static GVariant *
     608                 :         12 : g_themed_icon_serialize (GIcon *icon)
     609                 :            : {
     610                 :         12 :   GThemedIcon *themed_icon = G_THEMED_ICON (icon);
     611                 :            : 
     612                 :         12 :   return g_variant_new ("(sv)", "themed", g_variant_new ("^as", themed_icon->names));
     613                 :            : }
     614                 :            : 
     615                 :            : static void
     616                 :         25 : g_themed_icon_icon_iface_init (GIconIface *iface)
     617                 :            : {
     618                 :         25 :   iface->hash = g_themed_icon_hash;
     619                 :         25 :   iface->equal = g_themed_icon_equal;
     620                 :         25 :   iface->to_tokens = g_themed_icon_to_tokens;
     621                 :         25 :   iface->from_tokens = g_themed_icon_from_tokens;
     622                 :         25 :   iface->serialize = g_themed_icon_serialize;
     623                 :         25 : }

Generated by: LCOV version 1.14