Coverage Report

Created: 2025-06-13 06:55

/src/glib/gio/gloadableicon.c
Line
Count
Source (jump to first uncovered line)
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
#include "gicon.h"
25
#include "gloadableicon.h"
26
#include "gasyncresult.h"
27
#include "gtask.h"
28
#include "glibintl.h"
29
30
31
/**
32
 * SECTION:gloadableicon
33
 * @short_description: Loadable Icons
34
 * @include: gio/gio.h
35
 * @see_also: #GIcon, #GThemedIcon
36
 * 
37
 * Extends the #GIcon interface and adds the ability to 
38
 * load icons from streams.
39
 **/
40
41
static void          g_loadable_icon_real_load_async  (GLoadableIcon        *icon,
42
                   int                   size,
43
                   GCancellable         *cancellable,
44
                   GAsyncReadyCallback   callback,
45
                   gpointer              user_data);
46
static GInputStream *g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
47
                   GAsyncResult         *res,
48
                   char                **type,
49
                   GError              **error);
50
51
typedef GLoadableIconIface GLoadableIconInterface;
52
G_DEFINE_INTERFACE(GLoadableIcon, g_loadable_icon, G_TYPE_ICON)
53
54
static void
55
g_loadable_icon_default_init (GLoadableIconIface *iface)
56
0
{
57
0
  iface->load_async = g_loadable_icon_real_load_async;
58
0
  iface->load_finish = g_loadable_icon_real_load_finish;
59
0
}
60
61
/**
62
 * g_loadable_icon_load:
63
 * @icon: a #GLoadableIcon.
64
 * @size: an integer.
65
 * @type: (out) (optional): a location to store the type of the loaded
66
 * icon, %NULL to ignore.
67
 * @cancellable: (nullable): optional #GCancellable object, %NULL to
68
 * ignore.
69
 * @error: a #GError location to store the error occurring, or %NULL
70
 * to ignore.
71
 * 
72
 * Loads a loadable icon. For the asynchronous version of this function, 
73
 * see g_loadable_icon_load_async().
74
 * 
75
 * Returns: (transfer full): a #GInputStream to read the icon from.
76
 **/
77
GInputStream *
78
g_loadable_icon_load (GLoadableIcon  *icon,
79
          int             size,
80
          char          **type,
81
          GCancellable   *cancellable,
82
          GError        **error)
83
0
{
84
0
  GLoadableIconIface *iface;
85
86
0
  g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
87
88
0
  iface = G_LOADABLE_ICON_GET_IFACE (icon);
89
90
0
  return (* iface->load) (icon, size, type, cancellable, error);
91
0
}
92
93
/**
94
 * g_loadable_icon_load_async:
95
 * @icon: a #GLoadableIcon.
96
 * @size: an integer.
97
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore. 
98
 * @callback: (scope async): a #GAsyncReadyCallback
99
 *   to call when the request is satisfied
100
 * @user_data: the data to pass to callback function
101
 * 
102
 * Loads an icon asynchronously. To finish this function, see 
103
 * g_loadable_icon_load_finish(). For the synchronous, blocking 
104
 * version of this function, see g_loadable_icon_load().
105
 **/
106
void
107
g_loadable_icon_load_async (GLoadableIcon       *icon,
108
                            int                  size,
109
                            GCancellable        *cancellable,
110
                            GAsyncReadyCallback  callback,
111
                            gpointer             user_data)
112
0
{
113
0
  GLoadableIconIface *iface;
114
  
115
0
  g_return_if_fail (G_IS_LOADABLE_ICON (icon));
116
117
0
  iface = G_LOADABLE_ICON_GET_IFACE (icon);
118
119
0
  (* iface->load_async) (icon, size, cancellable, callback, user_data);
120
0
}
121
122
/**
123
 * g_loadable_icon_load_finish:
124
 * @icon: a #GLoadableIcon.
125
 * @res: a #GAsyncResult.
126
 * @type: (out) (optional): a location to store the type of the loaded
127
 *        icon, %NULL to ignore.
128
 * @error: a #GError location to store the error occurring, or %NULL to 
129
 * ignore.
130
 * 
131
 * Finishes an asynchronous icon load started in g_loadable_icon_load_async().
132
 * 
133
 * Returns: (transfer full): a #GInputStream to read the icon from.
134
 **/
135
GInputStream *
136
g_loadable_icon_load_finish (GLoadableIcon  *icon,
137
           GAsyncResult   *res,
138
           char          **type,
139
           GError        **error)
140
0
{
141
0
  GLoadableIconIface *iface;
142
  
143
0
  g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
144
0
  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
145
146
0
  if (g_async_result_legacy_propagate_error (res, error))
147
0
    return NULL;
148
149
0
  iface = G_LOADABLE_ICON_GET_IFACE (icon);
150
151
0
  return (* iface->load_finish) (icon, res, type, error);
152
0
}
153
154
/********************************************
155
 *   Default implementation of async load   *
156
 ********************************************/
157
158
typedef struct {
159
  int size;
160
  char *type;
161
} LoadData;
162
163
static void
164
load_data_free (LoadData *data)
165
0
{
166
0
  g_free (data->type);
167
0
  g_free (data);
168
0
}
169
170
static void
171
load_async_thread (GTask        *task,
172
                   gpointer      source_object,
173
                   gpointer      task_data,
174
                   GCancellable *cancellable)
175
0
{
176
0
  GLoadableIcon *icon = source_object;
177
0
  LoadData *data = task_data;
178
0
  GLoadableIconIface *iface;
179
0
  GInputStream *stream;
180
0
  GError *error = NULL;
181
182
0
  iface = G_LOADABLE_ICON_GET_IFACE (icon);
183
0
  stream = iface->load (icon, data->size, &data->type,
184
0
                        cancellable, &error);
185
186
0
  if (stream)
187
0
    g_task_return_pointer (task, stream, g_object_unref);
188
0
  else
189
0
    g_task_return_error (task, error);
190
0
}
191
192
193
194
static void
195
g_loadable_icon_real_load_async (GLoadableIcon       *icon,
196
         int                  size,
197
         GCancellable        *cancellable,
198
         GAsyncReadyCallback  callback,
199
         gpointer             user_data)
200
0
{
201
0
  GTask *task;
202
0
  LoadData *data;
203
204
0
  task = g_task_new (icon, cancellable, callback, user_data);
205
0
  g_task_set_source_tag (task, g_loadable_icon_real_load_async);
206
0
  data = g_new0 (LoadData, 1);
207
0
  g_task_set_task_data (task, data, (GDestroyNotify) load_data_free);
208
0
  g_task_run_in_thread (task, load_async_thread);
209
0
  g_object_unref (task);
210
0
}
211
212
static GInputStream *
213
g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
214
          GAsyncResult         *res,
215
          char                **type,
216
          GError              **error)
217
0
{
218
0
  GTask *task;
219
0
  LoadData *data;
220
0
  GInputStream *stream;
221
222
0
  g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
223
224
0
  task = G_TASK (res);
225
0
  data = g_task_get_task_data (task);
226
227
0
  stream = g_task_propagate_pointer (task, error);
228
0
  if (stream && type)
229
0
    {
230
0
      *type = data->type;
231
0
      data->type = NULL;
232
0
    }
233
234
0
  return stream;
235
0
}