| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | /* GIO - GLib Input, Output and Streaming Library
 | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * Copyright (C) 2006-2007 Red Hat, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2022-05-18 09:12:45 +01:00
										 |  |  |  * SPDX-License-Identifier: LGPL-2.1-or-later | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  |  * 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 | 
					
						
							| 
									
										
										
										
											2017-05-27 18:21:30 +02:00
										 |  |  |  * version 2.1 of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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 | 
					
						
							| 
									
										
										
										
											2014-01-23 12:58:29 +01:00
										 |  |  |  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Author: Alexander Larsson <alexl@redhat.com> | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-22 15:10:51 +00:00
										 |  |  | #include "config.h"
 | 
					
						
							| 
									
										
										
										
											2008-07-01 06:32:35 +00:00
										 |  |  | #include "gicon.h"
 | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | #include "gloadableicon.h"
 | 
					
						
							| 
									
										
										
										
											2012-08-02 15:50:35 -04:00
										 |  |  | #include "gasyncresult.h"
 | 
					
						
							|  |  |  | #include "gtask.h"
 | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | #include "glibintl.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-28 12:39:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-27 14:00:13 +00:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2023-10-24 10:52:58 +01:00
										 |  |  |  * GLoadableIcon: | 
					
						
							| 
									
										
										
										
											2007-11-27 14:00:13 +00:00
										 |  |  |  *  | 
					
						
							| 
									
										
										
										
											2023-10-24 10:52:58 +01:00
										 |  |  |  * `GLoadableIcon` extends the [iface@Gio.Icon] interface and adds the ability | 
					
						
							|  |  |  |  * to load icons from streams. | 
					
						
							| 
									
										
										
										
											2007-11-27 14:00:13 +00:00
										 |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | static void          g_loadable_icon_real_load_async  (GLoadableIcon        *icon, | 
					
						
							|  |  |  | 						       int                   size, | 
					
						
							|  |  |  | 						       GCancellable         *cancellable, | 
					
						
							|  |  |  | 						       GAsyncReadyCallback   callback, | 
					
						
							|  |  |  | 						       gpointer              user_data); | 
					
						
							|  |  |  | static GInputStream *g_loadable_icon_real_load_finish (GLoadableIcon        *icon, | 
					
						
							|  |  |  | 						       GAsyncResult         *res, | 
					
						
							|  |  |  | 						       char                **type, | 
					
						
							|  |  |  | 						       GError              **error); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-01 10:42:58 +01:00
										 |  |  | typedef GLoadableIconIface GLoadableIconInterface; | 
					
						
							|  |  |  | G_DEFINE_INTERFACE(GLoadableIcon, g_loadable_icon, G_TYPE_ICON) | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2009-12-01 10:42:58 +01:00
										 |  |  | g_loadable_icon_default_init (GLoadableIconIface *iface) | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   iface->load_async = g_loadable_icon_real_load_async; | 
					
						
							|  |  |  |   iface->load_finish = g_loadable_icon_real_load_finish; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * g_loadable_icon_load: | 
					
						
							| 
									
										
										
										
											2007-11-27 14:00:13 +00:00
										 |  |  |  * @icon: a #GLoadableIcon. | 
					
						
							|  |  |  |  * @size: an integer. | 
					
						
							| 
									
										
										
										
											2014-05-21 00:27:36 -07:00
										 |  |  |  * @type: (out) (optional): a location to store the type of the loaded | 
					
						
							|  |  |  |  * icon, %NULL to ignore. | 
					
						
							| 
									
										
										
										
											2016-10-28 18:29:02 -07:00
										 |  |  |  * @cancellable: (nullable): optional #GCancellable object, %NULL to | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  |  * ignore. | 
					
						
							| 
									
										
										
										
											2014-05-21 00:27:36 -07:00
										 |  |  |  * @error: a #GError location to store the error occurring, or %NULL | 
					
						
							|  |  |  |  * to ignore. | 
					
						
							| 
									
										
										
										
											2007-11-27 14:00:13 +00:00
										 |  |  |  *  | 
					
						
							|  |  |  |  * Loads a loadable icon. For the asynchronous version of this function,  | 
					
						
							|  |  |  |  * see g_loadable_icon_load_async(). | 
					
						
							|  |  |  |  *  | 
					
						
							| 
									
										
										
										
											2010-09-24 18:24:41 -03:00
										 |  |  |  * Returns: (transfer full): a #GInputStream to read the icon from. | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  |  **/ | 
					
						
							|  |  |  | GInputStream * | 
					
						
							| 
									
										
										
										
											2007-11-29 07:17:59 +00:00
										 |  |  | g_loadable_icon_load (GLoadableIcon  *icon, | 
					
						
							|  |  |  | 		      int             size, | 
					
						
							|  |  |  | 		      char          **type, | 
					
						
							|  |  |  | 		      GCancellable   *cancellable, | 
					
						
							|  |  |  | 		      GError        **error) | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   GLoadableIconIface *iface; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   iface = G_LOADABLE_ICON_GET_IFACE (icon); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (* iface->load) (icon, size, type, cancellable, error); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * g_loadable_icon_load_async: | 
					
						
							| 
									
										
										
										
											2007-11-27 14:00:13 +00:00
										 |  |  |  * @icon: a #GLoadableIcon. | 
					
						
							|  |  |  |  * @size: an integer. | 
					
						
							| 
									
										
										
										
											2016-10-28 18:29:02 -07:00
										 |  |  |  * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.  | 
					
						
							| 
									
										
										
										
											2023-02-20 14:43:02 -06:00
										 |  |  |  * @callback: (scope async): a #GAsyncReadyCallback | 
					
						
							|  |  |  |  *   to call when the request is satisfied | 
					
						
							|  |  |  |  * @user_data: the data to pass to callback function | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  |  *  | 
					
						
							| 
									
										
										
										
											2007-11-27 14:00:13 +00:00
										 |  |  |  * Loads an icon asynchronously. To finish this function, see  | 
					
						
							|  |  |  |  * g_loadable_icon_load_finish(). For the synchronous, blocking  | 
					
						
							|  |  |  |  * version of this function, see g_loadable_icon_load(). | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  |  **/ | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2007-11-29 07:17:59 +00:00
										 |  |  | g_loadable_icon_load_async (GLoadableIcon       *icon, | 
					
						
							|  |  |  |                             int                  size, | 
					
						
							|  |  |  |                             GCancellable        *cancellable, | 
					
						
							|  |  |  |                             GAsyncReadyCallback  callback, | 
					
						
							|  |  |  |                             gpointer             user_data) | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   GLoadableIconIface *iface; | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   g_return_if_fail (G_IS_LOADABLE_ICON (icon)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   iface = G_LOADABLE_ICON_GET_IFACE (icon); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   (* iface->load_async) (icon, size, cancellable, callback, user_data); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * g_loadable_icon_load_finish: | 
					
						
							| 
									
										
										
										
											2007-11-27 14:00:13 +00:00
										 |  |  |  * @icon: a #GLoadableIcon. | 
					
						
							|  |  |  |  * @res: a #GAsyncResult. | 
					
						
							| 
									
										
										
										
											2014-05-21 00:27:36 -07:00
										 |  |  |  * @type: (out) (optional): a location to store the type of the loaded | 
					
						
							|  |  |  |  *        icon, %NULL to ignore. | 
					
						
							| 
									
										
										
										
											2011-08-29 14:49:32 -04:00
										 |  |  |  * @error: a #GError location to store the error occurring, or %NULL to  | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  |  * ignore. | 
					
						
							| 
									
										
										
										
											2007-11-27 14:00:13 +00:00
										 |  |  |  *  | 
					
						
							|  |  |  |  * Finishes an asynchronous icon load started in g_loadable_icon_load_async(). | 
					
						
							|  |  |  |  *  | 
					
						
							| 
									
										
										
										
											2010-09-24 18:24:41 -03:00
										 |  |  |  * Returns: (transfer full): a #GInputStream to read the icon from. | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  |  **/ | 
					
						
							|  |  |  | GInputStream * | 
					
						
							| 
									
										
										
										
											2007-11-29 07:17:59 +00:00
										 |  |  | g_loadable_icon_load_finish (GLoadableIcon  *icon, | 
					
						
							|  |  |  | 			     GAsyncResult   *res, | 
					
						
							|  |  |  | 			     char          **type, | 
					
						
							|  |  |  | 			     GError        **error) | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   GLoadableIconIface *iface; | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL); | 
					
						
							|  |  |  |   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-10 09:00:45 -04:00
										 |  |  |   if (g_async_result_legacy_propagate_error (res, error)) | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  |   iface = G_LOADABLE_ICON_GET_IFACE (icon); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (* iface->load_finish) (icon, res, type, error); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /********************************************
 | 
					
						
							|  |  |  |  *   Default implementation of async load   * | 
					
						
							|  |  |  |  ********************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |   int size; | 
					
						
							|  |  |  |   char *type; | 
					
						
							|  |  |  | } LoadData; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | load_data_free (LoadData *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   g_free (data->type); | 
					
						
							|  |  |  |   g_free (data); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2012-08-02 15:50:35 -04:00
										 |  |  | load_async_thread (GTask        *task, | 
					
						
							|  |  |  |                    gpointer      source_object, | 
					
						
							|  |  |  |                    gpointer      task_data, | 
					
						
							|  |  |  |                    GCancellable *cancellable) | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-08-02 15:50:35 -04:00
										 |  |  |   GLoadableIcon *icon = source_object; | 
					
						
							|  |  |  |   LoadData *data = task_data; | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  |   GLoadableIconIface *iface; | 
					
						
							|  |  |  |   GInputStream *stream; | 
					
						
							|  |  |  |   GError *error = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-02 15:50:35 -04:00
										 |  |  |   iface = G_LOADABLE_ICON_GET_IFACE (icon); | 
					
						
							|  |  |  |   stream = iface->load (icon, data->size, &data->type, | 
					
						
							|  |  |  |                         cancellable, &error); | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-02 15:50:35 -04:00
										 |  |  |   if (stream) | 
					
						
							|  |  |  |     g_task_return_pointer (task, stream, g_object_unref); | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2012-08-02 15:50:35 -04:00
										 |  |  |     g_task_return_error (task, error); | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2007-11-29 07:17:59 +00:00
										 |  |  | g_loadable_icon_real_load_async (GLoadableIcon       *icon, | 
					
						
							|  |  |  | 				 int                  size, | 
					
						
							|  |  |  | 				 GCancellable        *cancellable, | 
					
						
							|  |  |  | 				 GAsyncReadyCallback  callback, | 
					
						
							|  |  |  | 				 gpointer             user_data) | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-08-02 15:50:35 -04:00
										 |  |  |   GTask *task; | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  |   LoadData *data; | 
					
						
							| 
									
										
										
										
											2012-08-02 15:50:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   task = g_task_new (icon, cancellable, callback, user_data); | 
					
						
							| 
									
										
										
										
											2016-06-16 19:39:38 -04:00
										 |  |  |   g_task_set_source_tag (task, g_loadable_icon_real_load_async); | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  |   data = g_new0 (LoadData, 1); | 
					
						
							| 
									
										
										
										
											2012-08-02 15:50:35 -04:00
										 |  |  |   g_task_set_task_data (task, data, (GDestroyNotify) load_data_free); | 
					
						
							|  |  |  |   g_task_run_in_thread (task, load_async_thread); | 
					
						
							|  |  |  |   g_object_unref (task); | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static GInputStream * | 
					
						
							|  |  |  | g_loadable_icon_real_load_finish (GLoadableIcon        *icon, | 
					
						
							|  |  |  | 				  GAsyncResult         *res, | 
					
						
							|  |  |  | 				  char                **type, | 
					
						
							|  |  |  | 				  GError              **error) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-08-02 15:50:35 -04:00
										 |  |  |   GTask *task; | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  |   LoadData *data; | 
					
						
							| 
									
										
										
										
											2012-08-02 15:50:35 -04:00
										 |  |  |   GInputStream *stream; | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-02 15:50:35 -04:00
										 |  |  |   g_return_val_if_fail (g_task_is_valid (res, icon), NULL); | 
					
						
							| 
									
										
										
										
											2012-06-11 13:44:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-02 15:50:35 -04:00
										 |  |  |   task = G_TASK (res); | 
					
						
							|  |  |  |   data = g_task_get_task_data (task); | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-02 15:50:35 -04:00
										 |  |  |   stream = g_task_propagate_pointer (task, error); | 
					
						
							|  |  |  |   if (stream && type) | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       *type = data->type; | 
					
						
							|  |  |  |       data->type = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-02 15:50:35 -04:00
										 |  |  |   return stream; | 
					
						
							| 
									
										
										
										
											2007-11-26 16:13:05 +00:00
										 |  |  | } |