[glib] Introduce GBytesIcon



commit 9cc222c0bfc65034143753a64b081b7811ee48f1
Author: Ryan Lortie <desrt desrt ca>
Date:   Sat Apr 20 17:23:31 2013 -0400

    Introduce GBytesIcon
    
    GBytesIcon is an icon that has a GBytes inside of it where the GBytes
    contains some sort of encoded image in a widely-recognised file format.
    Ideally this will be a PNG.
    
    It implements GLoadableIcon, so GTK will already understand how to use
    it, but we will add another patch there to make things more efficient.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=688820

 docs/reference/gio/gio-docs.xml     |   1 +
 docs/reference/gio/gio-sections.txt |  14 ++
 gio/Makefile.am                     |   2 +
 gio/gbytesicon.c                    | 253 ++++++++++++++++++++++++++++++++++++
 gio/gbytesicon.h                    |  54 ++++++++
 gio/gio.h                           |   1 +
 gio/giotypes.h                      |   1 +
 7 files changed, 326 insertions(+)
---
diff --git a/docs/reference/gio/gio-docs.xml b/docs/reference/gio/gio-docs.xml
index 3c66dad..d323d3c 100644
--- a/docs/reference/gio/gio-docs.xml
+++ b/docs/reference/gio/gio-docs.xml
@@ -97,6 +97,7 @@
         <title>Icons</title>
         <xi:include href="xml/gicon.xml"/>
         <xi:include href="xml/gfileicon.xml"/>
+        <xi:include href="xml/gbytesicon.xml"/>
         <xi:include href="xml/gloadableicon.xml"/>
         <xi:include href="xml/gthemedicon.xml"/>
         <xi:include href="xml/gemblemedicon.xml"/>
diff --git a/docs/reference/gio/gio-sections.txt b/docs/reference/gio/gio-sections.txt
index b2beadf..bac8f82 100644
--- a/docs/reference/gio/gio-sections.txt
+++ b/docs/reference/gio/gio-sections.txt
@@ -515,6 +515,20 @@ g_file_icon_get_type
 </SECTION>
 
 <SECTION>
+<FILE>gbytesicon</FILE>
+<TITLE>GBytesIcon</TITLE>
+GBytesIcon
+g_bytes_icon_new
+g_bytes_icon_get_bytes
+<SUBSECTION Standard>
+G_BYTES_ICON
+G_IS_BYTES_ICON
+G_TYPE_BYTES_ICON
+<SUBSECTION Private>
+g_bytes_icon_get_type
+</SECTION>
+
+<SECTION>
 <FILE>gemblemedicon</FILE>
 <TITLE>GEmblemedIcon</TITLE>
 GEmblemedIcon
diff --git a/gio/Makefile.am b/gio/Makefile.am
index 94d7f59..ff66263 100644
--- a/gio/Makefile.am
+++ b/gio/Makefile.am
@@ -339,6 +339,7 @@ libgio_2_0_la_SOURCES =             \
        gasyncresult.c          \
        gbufferedinputstream.c  \
        gbufferedoutputstream.c \
+       gbytesicon.c            \
        gcancellable.c          \
        gcharsetconverter.c     \
        gconverter.c            \
@@ -519,6 +520,7 @@ gio_headers =                       \
        gasyncresult.h          \
        gbufferedinputstream.h  \
        gbufferedoutputstream.h \
+       gbytesicon.h            \
        gcancellable.h          \
        gcontenttype.h          \
        gcharsetconverter.h     \
diff --git a/gio/gbytesicon.c b/gio/gbytesicon.c
new file mode 100644
index 0000000..2f4b557
--- /dev/null
+++ b/gio/gbytesicon.c
@@ -0,0 +1,253 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright © 2013 Canonical Limited
+ *
+ * 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 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, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * 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_object_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.
+ *
+ * Returns: (transfer full) (type GBytesIcon): a #GIcon for the given
+ *   @bytes, or %NULL on error.
+ *
+ * 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, or %NULL.
+ *
+ * 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 void
+g_bytes_icon_icon_iface_init (GIconIface *iface)
+{
+  iface->hash = g_bytes_icon_hash;
+  iface->equal = g_bytes_icon_equal;
+}
+
+static GInputStream *
+g_bytes_icon_load (GLoadableIcon  *icon,
+                   int            size,
+                   char          **type,
+                   GCancellable   *cancellable,
+                   GError        **error)
+{
+  GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
+
+  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_return_pointer (task, g_memory_input_stream_new_from_bytes (bytes_icon->bytes), g_object_unref);
+}
+
+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;
+}
diff --git a/gio/gbytesicon.h b/gio/gbytesicon.h
new file mode 100644
index 0000000..bf7753b
--- /dev/null
+++ b/gio/gbytesicon.h
@@ -0,0 +1,54 @@
+/* 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
+ * version 2 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, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Ryan Lortie <desrt desrt ca>
+ */
+
+#ifndef __G_BYTES_ICON_H__
+#define __G_BYTES_ICON_H__
+
+#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
+#error "Only <gio/gio.h> can be included directly."
+#endif
+
+#include <gio/giotypes.h>
+
+G_BEGIN_DECLS
+
+#define G_TYPE_BYTES_ICON         (g_bytes_icon_get_type ())
+#define G_BYTES_ICON(inst)        (G_TYPE_CHECK_INSTANCE_CAST ((inst), G_TYPE_BYTES_ICON, GBytesIcon))
+#define G_IS_BYTES_ICON(inst)     (G_TYPE_CHECK_INSTANCE_TYPE ((inst), G_TYPE_BYTES_ICON))
+
+/**
+ * GBytesIcon:
+ *
+ * Gets an icon for a #GBytes. Implements #GLoadableIcon.
+ **/
+GLIB_AVAILABLE_IN_2_38
+GType   g_bytes_icon_get_type   (void) G_GNUC_CONST;
+
+GLIB_AVAILABLE_IN_2_38
+GIcon * g_bytes_icon_new        (GBytes     *bytes);
+
+GLIB_AVAILABLE_IN_2_38
+GBytes * g_bytes_icon_get_bytes (GBytesIcon *icon);
+
+G_END_DECLS
+
+#endif /* __G_BYTES_ICON_H__ */
diff --git a/gio/gio.h b/gio/gio.h
index 1f83062..d27b911 100644
--- a/gio/gio.h
+++ b/gio/gio.h
@@ -38,6 +38,7 @@
 #include <gio/gasyncresult.h>
 #include <gio/gbufferedinputstream.h>
 #include <gio/gbufferedoutputstream.h>
+#include <gio/gbytesicon.h>
 #include <gio/gcancellable.h>
 #include <gio/gcharsetconverter.h>
 #include <gio/gcontenttype.h>
diff --git a/gio/giotypes.h b/gio/giotypes.h
index 2696091..d09bfae 100644
--- a/gio/giotypes.h
+++ b/gio/giotypes.h
@@ -117,6 +117,7 @@ typedef struct _GIOExtension                  GIOExtension;
 typedef struct _GIOSchedulerJob               GIOSchedulerJob;
 typedef struct _GIOStreamAdapter              GIOStreamAdapter;
 typedef struct _GLoadableIcon                 GLoadableIcon; /* Dummy typedef */
+typedef struct _GBytesIcon                    GBytesIcon;
 typedef struct _GMemoryInputStream            GMemoryInputStream;
 typedef struct _GMemoryOutputStream           GMemoryOutputStream;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]