1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright © 2013 Canonical Limited
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* 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
* version 2.1 of the License, or (at your option) any later version.
*
* 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
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Ryan Lortie <desrt@desrt.ca>
*/
#include "config.h"
#include "gbytesicon.h"
#include "gbytes.h"
#include "gicon.h"
#include "glibintl.h"
#include "gloadableicon.h"
#include "gmemoryinputstream.h"
#include "gtask.h"
#include "gioerror.h"
/**
* SECTION:gbytesicon
* @short_description: An icon stored in memory as a GBytes
* @include: gio/gio.h
* @see_also: #GIcon, #GLoadableIcon, #GBytes
*
* #GBytesIcon specifies an image held in memory in a common format (usually
* png) to be used as icon.
*
* Since: 2.38
**/
typedef GObjectClass GBytesIconClass;
struct _GBytesIcon
{
GObject parent_instance;
GBytes *bytes;
};
enum
{
PROP_0,
PROP_BYTES
};
static void g_bytes_icon_icon_iface_init (GIconIface *iface);
static void g_bytes_icon_loadable_icon_iface_init (GLoadableIconIface *iface);
G_DEFINE_TYPE_WITH_CODE (GBytesIcon, g_bytes_icon, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_ICON, g_bytes_icon_icon_iface_init)
G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON, g_bytes_icon_loadable_icon_iface_init))
static void
g_bytes_icon_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GBytesIcon *icon = G_BYTES_ICON (object);
switch (prop_id)
{
case PROP_BYTES:
g_value_set_boxed (value, icon->bytes);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
g_bytes_icon_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GBytesIcon *icon = G_BYTES_ICON (object);
switch (prop_id)
{
case PROP_BYTES:
icon->bytes = g_value_dup_boxed (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
g_bytes_icon_finalize (GObject *object)
{
GBytesIcon *icon;
icon = G_BYTES_ICON (object);
g_bytes_unref (icon->bytes);
G_OBJECT_CLASS (g_bytes_icon_parent_class)->finalize (object);
}
static void
g_bytes_icon_class_init (GBytesIconClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->get_property = g_bytes_icon_get_property;
gobject_class->set_property = g_bytes_icon_set_property;
gobject_class->finalize = g_bytes_icon_finalize;
/**
* GBytesIcon:bytes:
*
* The bytes containing the icon.
*/
g_object_class_install_property (gobject_class, PROP_BYTES,
g_param_spec_boxed ("bytes",
P_("bytes"),
P_("The bytes containing the icon"),
G_TYPE_BYTES,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}
static void
g_bytes_icon_init (GBytesIcon *bytes)
{
}
/**
* g_bytes_icon_new:
* @bytes: a #GBytes.
*
* Creates a new icon for a bytes.
*
* This cannot fail, but loading and interpreting the bytes may fail later on
* (for example, if g_loadable_icon_load() is called) if the image is invalid.
*
* Returns: (transfer full) (type GBytesIcon): a #GIcon for the given
* @bytes.
*
* Since: 2.38
**/
GIcon *
g_bytes_icon_new (GBytes *bytes)
{
g_return_val_if_fail (bytes != NULL, NULL);
return g_object_new (G_TYPE_BYTES_ICON, "bytes", bytes, NULL);
}
/**
* g_bytes_icon_get_bytes:
* @icon: a #GIcon.
*
* Gets the #GBytes associated with the given @icon.
*
* Returns: (transfer none): a #GBytes.
*
* Since: 2.38
**/
GBytes *
g_bytes_icon_get_bytes (GBytesIcon *icon)
{
g_return_val_if_fail (G_IS_BYTES_ICON (icon), NULL);
return icon->bytes;
}
static guint
g_bytes_icon_hash (GIcon *icon)
{
GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
return g_bytes_hash (bytes_icon->bytes);
}
static gboolean
g_bytes_icon_equal (GIcon *icon1,
GIcon *icon2)
{
GBytesIcon *bytes1 = G_BYTES_ICON (icon1);
GBytesIcon *bytes2 = G_BYTES_ICON (icon2);
return g_bytes_equal (bytes1->bytes, bytes2->bytes);
}
static GVariant *
g_bytes_icon_serialize (GIcon *icon)
{
GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
return g_variant_new ("(sv)", "bytes",
g_variant_new_from_bytes (G_VARIANT_TYPE_BYTESTRING, bytes_icon->bytes, TRUE));
}
static void
g_bytes_icon_icon_iface_init (GIconIface *iface)
{
iface->hash = g_bytes_icon_hash;
iface->equal = g_bytes_icon_equal;
iface->serialize = g_bytes_icon_serialize;
}
static GInputStream *
g_bytes_icon_load (GLoadableIcon *icon,
int size,
char **type,
GCancellable *cancellable,
GError **error)
{
GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
if (type)
*type = NULL;
return g_memory_input_stream_new_from_bytes (bytes_icon->bytes);
}
static void
g_bytes_icon_load_async (GLoadableIcon *icon,
int size,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
GTask *task;
task = g_task_new (icon, cancellable, callback, user_data);
g_task_set_source_tag (task, g_bytes_icon_load_async);
g_task_return_pointer (task, g_memory_input_stream_new_from_bytes (bytes_icon->bytes), g_object_unref);
g_object_unref (task);
}
static GInputStream *
g_bytes_icon_load_finish (GLoadableIcon *icon,
GAsyncResult *res,
char **type,
GError **error)
{
g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
if (type)
*type = NULL;
return g_task_propagate_pointer (G_TASK (res), error);
}
static void
g_bytes_icon_loadable_icon_iface_init (GLoadableIconIface *iface)
{
iface->load = g_bytes_icon_load;
iface->load_async = g_bytes_icon_load_async;
iface->load_finish = g_bytes_icon_load_finish;
}
|