Blame gio/gbytesicon.c

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