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