Blame gio/gbytesicon.c

Packit Service d3d246
/* GIO - GLib Input, Output and Streaming Library
Packit Service d3d246
 *
Packit Service d3d246
 * Copyright © 2013 Canonical Limited
Packit Service d3d246
 *
Packit Service d3d246
 * This library is free software; you can redistribute it and/or
Packit Service d3d246
 * modify it under the terms of the GNU Lesser General Public
Packit Service d3d246
 * License as published by the Free Software Foundation; either
Packit Service d3d246
 * version 2.1 of the License, or (at your option) any later version.
Packit Service d3d246
 *
Packit Service d3d246
 * This library is distributed in the hope that it will be useful,
Packit Service d3d246
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service d3d246
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service d3d246
 * Lesser General Public License for more details.
Packit Service d3d246
 *
Packit Service d3d246
 * You should have received a copy of the GNU Lesser General
Packit Service d3d246
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit Service d3d246
 *
Packit Service d3d246
 * Author: Ryan Lortie <desrt@desrt.ca>
Packit Service d3d246
 */
Packit Service d3d246
Packit Service d3d246
#include "config.h"
Packit Service d3d246
Packit Service d3d246
#include "gbytesicon.h"
Packit Service d3d246
#include "gbytes.h"
Packit Service d3d246
#include "gicon.h"
Packit Service d3d246
#include "glibintl.h"
Packit Service d3d246
#include "gloadableicon.h"
Packit Service d3d246
#include "gmemoryinputstream.h"
Packit Service d3d246
#include "gtask.h"
Packit Service d3d246
#include "gioerror.h"
Packit Service d3d246
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * SECTION:gbytesicon
Packit Service d3d246
 * @short_description: An icon stored in memory as a GBytes
Packit Service d3d246
 * @include: gio/gio.h
Packit Service d3d246
 * @see_also: #GIcon, #GLoadableIcon, #GBytes
Packit Service d3d246
 *
Packit Service d3d246
 * #GBytesIcon specifies an image held in memory in a common format (usually
Packit Service d3d246
 * png) to be used as icon.
Packit Service d3d246
 *
Packit Service d3d246
 * Since: 2.38
Packit Service d3d246
 **/
Packit Service d3d246
Packit Service d3d246
typedef GObjectClass GBytesIconClass;
Packit Service d3d246
Packit Service d3d246
struct _GBytesIcon
Packit Service d3d246
{
Packit Service d3d246
  GObject parent_instance;
Packit Service d3d246
Packit Service d3d246
  GBytes *bytes;
Packit Service d3d246
};
Packit Service d3d246
Packit Service d3d246
enum
Packit Service d3d246
{
Packit Service d3d246
  PROP_0,
Packit Service d3d246
  PROP_BYTES
Packit Service d3d246
};
Packit Service d3d246
Packit Service d3d246
static void g_bytes_icon_icon_iface_init          (GIconIface          *iface);
Packit Service d3d246
static void g_bytes_icon_loadable_icon_iface_init (GLoadableIconIface  *iface);
Packit Service d3d246
G_DEFINE_TYPE_WITH_CODE (GBytesIcon, g_bytes_icon, G_TYPE_OBJECT,
Packit Service d3d246
                         G_IMPLEMENT_INTERFACE (G_TYPE_ICON, g_bytes_icon_icon_iface_init)
Packit Service d3d246
                         G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON, g_bytes_icon_loadable_icon_iface_init))
Packit Service d3d246
Packit Service d3d246
static void
Packit Service d3d246
g_bytes_icon_get_property (GObject    *object,
Packit Service d3d246
                           guint       prop_id,
Packit Service d3d246
                           GValue     *value,
Packit Service d3d246
                           GParamSpec *pspec)
Packit Service d3d246
{
Packit Service d3d246
  GBytesIcon *icon = G_BYTES_ICON (object);
Packit Service d3d246
Packit Service d3d246
  switch (prop_id)
Packit Service d3d246
    {
Packit Service d3d246
      case PROP_BYTES:
Packit Service d3d246
        g_value_set_boxed (value, icon->bytes);
Packit Service d3d246
        break;
Packit Service d3d246
Packit Service d3d246
      default:
Packit Service d3d246
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit Service d3d246
    }
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
static void
Packit Service d3d246
g_bytes_icon_set_property (GObject      *object,
Packit Service d3d246
                          guint         prop_id,
Packit Service d3d246
                          const GValue *value,
Packit Service d3d246
                          GParamSpec   *pspec)
Packit Service d3d246
{
Packit Service d3d246
  GBytesIcon *icon = G_BYTES_ICON (object);
Packit Service d3d246
Packit Service d3d246
  switch (prop_id)
Packit Service d3d246
    {
Packit Service d3d246
      case PROP_BYTES:
Packit Service d3d246
        icon->bytes = g_value_dup_boxed (value);
Packit Service d3d246
        break;
Packit Service d3d246
Packit Service d3d246
      default:
Packit Service d3d246
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit Service d3d246
    }
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
static void
Packit Service d3d246
g_bytes_icon_finalize (GObject *object)
Packit Service d3d246
{
Packit Service d3d246
  GBytesIcon *icon;
Packit Service d3d246
Packit Service d3d246
  icon = G_BYTES_ICON (object);
Packit Service d3d246
Packit Service d3d246
  g_bytes_unref (icon->bytes);
Packit Service d3d246
Packit Service d3d246
  G_OBJECT_CLASS (g_bytes_icon_parent_class)->finalize (object);
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
static void
Packit Service d3d246
g_bytes_icon_class_init (GBytesIconClass *klass)
Packit Service d3d246
{
Packit Service d3d246
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit Service d3d246
Packit Service d3d246
  gobject_class->get_property = g_bytes_icon_get_property;
Packit Service d3d246
  gobject_class->set_property = g_bytes_icon_set_property;
Packit Service d3d246
  gobject_class->finalize = g_bytes_icon_finalize;
Packit Service d3d246
Packit Service d3d246
  /**
Packit Service d3d246
   * GBytesIcon:bytes:
Packit Service d3d246
   *
Packit Service d3d246
   * The bytes containing the icon.
Packit Service d3d246
   */
Packit Service d3d246
  g_object_class_install_property (gobject_class, PROP_BYTES,
Packit Service d3d246
                                   g_param_spec_boxed ("bytes",
Packit Service d3d246
                                                       P_("bytes"),
Packit Service d3d246
                                                       P_("The bytes containing the icon"),
Packit Service d3d246
                                                       G_TYPE_BYTES,
Packit Service d3d246
                                                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
static void
Packit Service d3d246
g_bytes_icon_init (GBytesIcon *bytes)
Packit Service d3d246
{
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * g_bytes_icon_new:
Packit Service d3d246
 * @bytes: a #GBytes.
Packit Service d3d246
 *
Packit Service d3d246
 * Creates a new icon for a bytes.
Packit Service d3d246
 *
Packit Service d3d246
 * Returns: (transfer full) (type GBytesIcon): a #GIcon for the given
Packit Service d3d246
 *   @bytes, or %NULL on error.
Packit Service d3d246
 *
Packit Service d3d246
 * Since: 2.38
Packit Service d3d246
 **/
Packit Service d3d246
GIcon *
Packit Service d3d246
g_bytes_icon_new (GBytes *bytes)
Packit Service d3d246
{
Packit Service d3d246
  g_return_val_if_fail (bytes != NULL, NULL);
Packit Service d3d246
Packit Service d3d246
  return g_object_new (G_TYPE_BYTES_ICON, "bytes", bytes, NULL);
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
/**
Packit Service d3d246
 * g_bytes_icon_get_bytes:
Packit Service d3d246
 * @icon: a #GIcon.
Packit Service d3d246
 *
Packit Service d3d246
 * Gets the #GBytes associated with the given @icon.
Packit Service d3d246
 *
Packit Service d3d246
 * Returns: (transfer none): a #GBytes, or %NULL.
Packit Service d3d246
 *
Packit Service d3d246
 * Since: 2.38
Packit Service d3d246
 **/
Packit Service d3d246
GBytes *
Packit Service d3d246
g_bytes_icon_get_bytes (GBytesIcon *icon)
Packit Service d3d246
{
Packit Service d3d246
  g_return_val_if_fail (G_IS_BYTES_ICON (icon), NULL);
Packit Service d3d246
Packit Service d3d246
  return icon->bytes;
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
static guint
Packit Service d3d246
g_bytes_icon_hash (GIcon *icon)
Packit Service d3d246
{
Packit Service d3d246
  GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
Packit Service d3d246
Packit Service d3d246
  return g_bytes_hash (bytes_icon->bytes);
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
static gboolean
Packit Service d3d246
g_bytes_icon_equal (GIcon *icon1,
Packit Service d3d246
                    GIcon *icon2)
Packit Service d3d246
{
Packit Service d3d246
  GBytesIcon *bytes1 = G_BYTES_ICON (icon1);
Packit Service d3d246
  GBytesIcon *bytes2 = G_BYTES_ICON (icon2);
Packit Service d3d246
Packit Service d3d246
  return g_bytes_equal (bytes1->bytes, bytes2->bytes);
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
static GVariant *
Packit Service d3d246
g_bytes_icon_serialize (GIcon *icon)
Packit Service d3d246
{
Packit Service d3d246
  GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
Packit Service d3d246
Packit Service d3d246
  return g_variant_new ("(sv)", "bytes",
Packit Service d3d246
                        g_variant_new_from_bytes (G_VARIANT_TYPE_BYTESTRING, bytes_icon->bytes, TRUE));
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
static void
Packit Service d3d246
g_bytes_icon_icon_iface_init (GIconIface *iface)
Packit Service d3d246
{
Packit Service d3d246
  iface->hash = g_bytes_icon_hash;
Packit Service d3d246
  iface->equal = g_bytes_icon_equal;
Packit Service d3d246
  iface->serialize = g_bytes_icon_serialize;
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
static GInputStream *
Packit Service d3d246
g_bytes_icon_load (GLoadableIcon  *icon,
Packit Service d3d246
                   int            size,
Packit Service d3d246
                   char          **type,
Packit Service d3d246
                   GCancellable   *cancellable,
Packit Service d3d246
                   GError        **error)
Packit Service d3d246
{
Packit Service d3d246
  GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
Packit Service d3d246
Packit Service d3d246
  if (type)
Packit Service d3d246
    *type = NULL;
Packit Service d3d246
Packit Service d3d246
  return g_memory_input_stream_new_from_bytes (bytes_icon->bytes);
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
static void
Packit Service d3d246
g_bytes_icon_load_async (GLoadableIcon       *icon,
Packit Service d3d246
                         int                  size,
Packit Service d3d246
                         GCancellable        *cancellable,
Packit Service d3d246
                         GAsyncReadyCallback  callback,
Packit Service d3d246
                         gpointer             user_data)
Packit Service d3d246
{
Packit Service d3d246
  GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
Packit Service d3d246
  GTask *task;
Packit Service d3d246
Packit Service d3d246
  task = g_task_new (icon, cancellable, callback, user_data);
Packit Service d3d246
  g_task_set_source_tag (task, g_bytes_icon_load_async);
Packit Service d3d246
  g_task_return_pointer (task, g_memory_input_stream_new_from_bytes (bytes_icon->bytes), g_object_unref);
Packit Service d3d246
  g_object_unref (task);
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
static GInputStream *
Packit Service d3d246
g_bytes_icon_load_finish (GLoadableIcon  *icon,
Packit Service d3d246
                          GAsyncResult   *res,
Packit Service d3d246
                          char          **type,
Packit Service d3d246
                          GError        **error)
Packit Service d3d246
{
Packit Service d3d246
  g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
Packit Service d3d246
Packit Service d3d246
  if (type)
Packit Service d3d246
    *type = NULL;
Packit Service d3d246
Packit Service d3d246
  return g_task_propagate_pointer (G_TASK (res), error);
Packit Service d3d246
}
Packit Service d3d246
Packit Service d3d246
static void
Packit Service d3d246
g_bytes_icon_loadable_icon_iface_init (GLoadableIconIface *iface)
Packit Service d3d246
{
Packit Service d3d246
  iface->load = g_bytes_icon_load;
Packit Service d3d246
  iface->load_async = g_bytes_icon_load_async;
Packit Service d3d246
  iface->load_finish = g_bytes_icon_load_finish;
Packit Service d3d246
}