Add g_key_file_save_to_file()
[glib.git] / gio / gbytesicon.c
blob2182f11d3d30c6aae3f584a6e19b07d0c6ac5e08
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2013 Canonical Limited
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Ryan Lortie <desrt@desrt.ca>
23 #include "config.h"
25 #include "gbytesicon.h"
26 #include "gbytes.h"
27 #include "gicon.h"
28 #include "glibintl.h"
29 #include "gloadableicon.h"
30 #include "gmemoryinputstream.h"
31 #include "gtask.h"
32 #include "gioerror.h"
35 /**
36 * SECTION:gbytesicon
37 * @short_description: An icon stored in memory as a GBytes
38 * @include: gio/gio.h
39 * @see_also: #GIcon, #GLoadableIcon, #GBytes
41 * #GBytesIcon specifies an image held in memory in a common format (usually
42 * png) to be used as icon.
44 * Since: 2.38
45 **/
47 typedef GObjectClass GBytesIconClass;
49 struct _GBytesIcon
51 GObject parent_instance;
53 GBytes *bytes;
56 enum
58 PROP_0,
59 PROP_BYTES
62 static void g_bytes_icon_icon_iface_init (GIconIface *iface);
63 static void g_bytes_icon_loadable_icon_iface_init (GLoadableIconIface *iface);
64 G_DEFINE_TYPE_WITH_CODE (GBytesIcon, g_bytes_icon, G_TYPE_OBJECT,
65 G_IMPLEMENT_INTERFACE (G_TYPE_ICON, g_bytes_icon_icon_iface_init)
66 G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON, g_bytes_icon_loadable_icon_iface_init))
68 static void
69 g_bytes_icon_get_property (GObject *object,
70 guint prop_id,
71 GValue *value,
72 GParamSpec *pspec)
74 GBytesIcon *icon = G_BYTES_ICON (object);
76 switch (prop_id)
78 case PROP_BYTES:
79 g_value_set_boxed (value, icon->bytes);
80 break;
82 default:
83 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87 static void
88 g_bytes_icon_set_property (GObject *object,
89 guint prop_id,
90 const GValue *value,
91 GParamSpec *pspec)
93 GBytesIcon *icon = G_BYTES_ICON (object);
95 switch (prop_id)
97 case PROP_BYTES:
98 icon->bytes = g_value_dup_boxed (value);
99 break;
101 default:
102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106 static void
107 g_bytes_icon_finalize (GObject *object)
109 GBytesIcon *icon;
111 icon = G_BYTES_ICON (object);
113 g_bytes_unref (icon->bytes);
115 G_OBJECT_CLASS (g_bytes_icon_parent_class)->finalize (object);
118 static void
119 g_bytes_icon_class_init (GBytesIconClass *klass)
121 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
123 gobject_class->get_property = g_bytes_icon_get_property;
124 gobject_class->set_property = g_bytes_icon_set_property;
125 gobject_class->finalize = g_bytes_icon_finalize;
128 * GBytesIcon:bytes:
130 * The bytes containing the icon.
132 g_object_class_install_property (gobject_class, PROP_BYTES,
133 g_param_spec_boxed ("bytes",
134 P_("bytes"),
135 P_("The bytes containing the icon"),
136 G_TYPE_BYTES,
137 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
140 static void
141 g_bytes_icon_init (GBytesIcon *bytes)
146 * g_bytes_icon_new:
147 * @bytes: a #GBytes.
149 * Creates a new icon for a bytes.
151 * Returns: (transfer full) (type GBytesIcon): a #GIcon for the given
152 * @bytes, or %NULL on error.
154 * Since: 2.38
156 GIcon *
157 g_bytes_icon_new (GBytes *bytes)
159 g_return_val_if_fail (bytes != NULL, NULL);
161 return g_object_new (G_TYPE_BYTES_ICON, "bytes", bytes, NULL);
165 * g_bytes_icon_get_bytes:
166 * @icon: a #GIcon.
168 * Gets the #GBytes associated with the given @icon.
170 * Returns: (transfer none): a #GBytes, or %NULL.
172 * Since: 2.38
174 GBytes *
175 g_bytes_icon_get_bytes (GBytesIcon *icon)
177 g_return_val_if_fail (G_IS_BYTES_ICON (icon), NULL);
179 return icon->bytes;
182 static guint
183 g_bytes_icon_hash (GIcon *icon)
185 GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
187 return g_bytes_hash (bytes_icon->bytes);
190 static gboolean
191 g_bytes_icon_equal (GIcon *icon1,
192 GIcon *icon2)
194 GBytesIcon *bytes1 = G_BYTES_ICON (icon1);
195 GBytesIcon *bytes2 = G_BYTES_ICON (icon2);
197 return g_bytes_equal (bytes1->bytes, bytes2->bytes);
200 static GVariant *
201 g_bytes_icon_serialize (GIcon *icon)
203 GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
205 return g_variant_new ("(sv)", "bytes",
206 g_variant_new_from_bytes (G_VARIANT_TYPE_BYTESTRING, bytes_icon->bytes, TRUE));
209 static void
210 g_bytes_icon_icon_iface_init (GIconIface *iface)
212 iface->hash = g_bytes_icon_hash;
213 iface->equal = g_bytes_icon_equal;
214 iface->serialize = g_bytes_icon_serialize;
217 static GInputStream *
218 g_bytes_icon_load (GLoadableIcon *icon,
219 int size,
220 char **type,
221 GCancellable *cancellable,
222 GError **error)
224 GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
226 if (type)
227 *type = NULL;
229 return g_memory_input_stream_new_from_bytes (bytes_icon->bytes);
232 static void
233 g_bytes_icon_load_async (GLoadableIcon *icon,
234 int size,
235 GCancellable *cancellable,
236 GAsyncReadyCallback callback,
237 gpointer user_data)
239 GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
240 GTask *task;
242 task = g_task_new (icon, cancellable, callback, user_data);
243 g_task_return_pointer (task, g_memory_input_stream_new_from_bytes (bytes_icon->bytes), g_object_unref);
244 g_object_unref (task);
247 static GInputStream *
248 g_bytes_icon_load_finish (GLoadableIcon *icon,
249 GAsyncResult *res,
250 char **type,
251 GError **error)
253 g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
255 if (type)
256 *type = NULL;
258 return g_task_propagate_pointer (G_TASK (res), error);
261 static void
262 g_bytes_icon_loadable_icon_iface_init (GLoadableIconIface *iface)
264 iface->load = g_bytes_icon_load;
265 iface->load_async = g_bytes_icon_load_async;
266 iface->load_finish = g_bytes_icon_load_finish;