gloadableicon.c 6.64 KB
Newer Older
1 2 3 4 5 6 7
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

21
#include "config.h"
22
#include "gicon.h"
23
#include "gloadableicon.h"
24 25
#include "gasyncresult.h"
#include "gtask.h"
26 27
#include "glibintl.h"

28

29 30 31
/**
 * SECTION:gloadableicon
 * @short_description: Loadable Icons
Matthias Clasen's avatar
Matthias Clasen committed
32
 * @include: gio/gio.h
33 34
 * @see_also: #GIcon, #GThemedIcon
 * 
35 36
 * Extends the #GIcon interface and adds the ability to 
 * load icons from streams.
37 38
 **/

39 40 41 42 43 44 45 46 47 48
static void          g_loadable_icon_real_load_async  (GLoadableIcon        *icon,
						       int                   size,
						       GCancellable         *cancellable,
						       GAsyncReadyCallback   callback,
						       gpointer              user_data);
static GInputStream *g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
						       GAsyncResult         *res,
						       char                **type,
						       GError              **error);

49 50
typedef GLoadableIconIface GLoadableIconInterface;
G_DEFINE_INTERFACE(GLoadableIcon, g_loadable_icon, G_TYPE_ICON)
51 52

static void
53
g_loadable_icon_default_init (GLoadableIconIface *iface)
54 55 56 57 58 59 60
{
  iface->load_async = g_loadable_icon_real_load_async;
  iface->load_finish = g_loadable_icon_real_load_finish;
}

/**
 * g_loadable_icon_load:
61 62
 * @icon: a #GLoadableIcon.
 * @size: an integer.
63 64
 * @type: (out) (optional): a location to store the type of the loaded
 * icon, %NULL to ignore.
65
 * @cancellable: (nullable): optional #GCancellable object, %NULL to
66
 * ignore.
67 68
 * @error: a #GError location to store the error occurring, or %NULL
 * to ignore.
69 70 71 72
 * 
 * Loads a loadable icon. For the asynchronous version of this function, 
 * see g_loadable_icon_load_async().
 * 
73
 * Returns: (transfer full): a #GInputStream to read the icon from.
74 75
 **/
GInputStream *
Matthias Clasen's avatar
Matthias Clasen committed
76 77 78 79 80
g_loadable_icon_load (GLoadableIcon  *icon,
		      int             size,
		      char          **type,
		      GCancellable   *cancellable,
		      GError        **error)
81 82 83 84 85 86 87 88 89 90 91 92
{
  GLoadableIconIface *iface;

  g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);

  iface = G_LOADABLE_ICON_GET_IFACE (icon);

  return (* iface->load) (icon, size, type, cancellable, error);
}

/**
 * g_loadable_icon_load_async:
93 94
 * @icon: a #GLoadableIcon.
 * @size: an integer.
95
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore. 
96 97 98
 * @callback: (scope async): a #GAsyncReadyCallback to call when the
 *            request is satisfied
 * @user_data: (closure): the data to pass to callback function
99
 * 
100 101 102
 * Loads an icon asynchronously. To finish this function, see 
 * g_loadable_icon_load_finish(). For the synchronous, blocking 
 * version of this function, see g_loadable_icon_load().
103 104
 **/
void
Matthias Clasen's avatar
Matthias Clasen committed
105 106 107 108 109
g_loadable_icon_load_async (GLoadableIcon       *icon,
                            int                  size,
                            GCancellable        *cancellable,
                            GAsyncReadyCallback  callback,
                            gpointer             user_data)
110 111 112 113 114 115 116 117 118 119 120 121
{
  GLoadableIconIface *iface;
  
  g_return_if_fail (G_IS_LOADABLE_ICON (icon));

  iface = G_LOADABLE_ICON_GET_IFACE (icon);

  (* iface->load_async) (icon, size, cancellable, callback, user_data);
}

/**
 * g_loadable_icon_load_finish:
122 123
 * @icon: a #GLoadableIcon.
 * @res: a #GAsyncResult.
124 125
 * @type: (out) (optional): a location to store the type of the loaded
 *        icon, %NULL to ignore.
Matthias Clasen's avatar
Matthias Clasen committed
126
 * @error: a #GError location to store the error occurring, or %NULL to 
127
 * ignore.
128 129 130
 * 
 * Finishes an asynchronous icon load started in g_loadable_icon_load_async().
 * 
131
 * Returns: (transfer full): a #GInputStream to read the icon from.
132 133
 **/
GInputStream *
Matthias Clasen's avatar
Matthias Clasen committed
134 135 136 137
g_loadable_icon_load_finish (GLoadableIcon  *icon,
			     GAsyncResult   *res,
			     char          **type,
			     GError        **error)
138 139 140 141 142 143
{
  GLoadableIconIface *iface;
  
  g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);

144 145 146
  if (g_async_result_legacy_propagate_error (res, error))
    return NULL;

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  iface = G_LOADABLE_ICON_GET_IFACE (icon);

  return (* iface->load_finish) (icon, res, type, error);
}

/********************************************
 *   Default implementation of async load   *
 ********************************************/

typedef struct {
  int size;
  char *type;
} LoadData;

static void
load_data_free (LoadData *data)
{
  g_free (data->type);
  g_free (data);
}

static void
169 170 171 172
load_async_thread (GTask        *task,
                   gpointer      source_object,
                   gpointer      task_data,
                   GCancellable *cancellable)
173
{
174 175
  GLoadableIcon *icon = source_object;
  LoadData *data = task_data;
176 177 178 179
  GLoadableIconIface *iface;
  GInputStream *stream;
  GError *error = NULL;

180 181 182
  iface = G_LOADABLE_ICON_GET_IFACE (icon);
  stream = iface->load (icon, data->size, &data->type,
                        cancellable, &error);
183

184 185
  if (stream)
    g_task_return_pointer (task, stream, g_object_unref);
186
  else
187
    g_task_return_error (task, error);
188 189 190 191 192
}



static void
Matthias Clasen's avatar
Matthias Clasen committed
193 194 195 196 197
g_loadable_icon_real_load_async (GLoadableIcon       *icon,
				 int                  size,
				 GCancellable        *cancellable,
				 GAsyncReadyCallback  callback,
				 gpointer             user_data)
198
{
199
  GTask *task;
200
  LoadData *data;
201 202

  task = g_task_new (icon, cancellable, callback, user_data);
203
  g_task_set_source_tag (task, g_loadable_icon_real_load_async);
204
  data = g_new0 (LoadData, 1);
205 206 207
  g_task_set_task_data (task, data, (GDestroyNotify) load_data_free);
  g_task_run_in_thread (task, load_async_thread);
  g_object_unref (task);
208 209 210 211 212 213 214 215
}

static GInputStream *
g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
				  GAsyncResult         *res,
				  char                **type,
				  GError              **error)
{
216
  GTask *task;
217
  LoadData *data;
218
  GInputStream *stream;
219

220
  g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
221

222 223
  task = G_TASK (res);
  data = g_task_get_task_data (task);
224

225 226
  stream = g_task_propagate_pointer (task, error);
  if (stream && type)
227 228 229 230 231
    {
      *type = data->type;
      data->type = NULL;
    }

232
  return stream;
233
}