Bug 621213 – GDBusProxy and well-known names
[platform/upstream/glib.git] / gio / gasyncinitable.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2009 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24 #include "gasyncinitable.h"
25 #include "gasyncresult.h"
26 #include "gsimpleasyncresult.h"
27 #include "glibintl.h"
28
29 #include "gioalias.h"
30
31 /**
32  * SECTION:gasyncinitable
33  * @short_description: Asynchronously failable object initialization interface
34  * @include: gio/gio.h
35  * @see_also: #GInitable
36  *
37  * This is the asynchronous version of #GInitable; it behaves the same
38  * in all ways except that initialization is asynchronous. For more details
39  * see the descriptions on #GInitable.
40  *
41  * A class may implement both the #GInitable and #GAsyncInitable interfaces.
42  *
43  * Users of objects implementing this are not intended to use the interface
44  * method directly; instead it will be used automatically in various ways.
45  * For C applications you generally just call g_async_initable_new_async()
46  * directly, or indirectly via a foo_thing_new_async() wrapper. This will call
47  * g_async_initable_init_async() under the cover, calling back with %NULL and
48  * a set %GError on failure.
49  */
50
51 static void     g_async_initable_real_init_async  (GAsyncInitable       *initable,
52                                                    int                   io_priority,
53                                                    GCancellable         *cancellable,
54                                                    GAsyncReadyCallback   callback,
55                                                    gpointer              user_data);
56 static gboolean g_async_initable_real_init_finish (GAsyncInitable       *initable,
57                                                    GAsyncResult         *res,
58                                                    GError              **error);
59
60
61 typedef GAsyncInitableIface GAsyncInitableInterface;
62 G_DEFINE_INTERFACE (GAsyncInitable, g_async_initable, G_TYPE_OBJECT)
63
64
65 static void
66 g_async_initable_default_init (GAsyncInitableInterface *iface)
67 {
68   iface->init_async = g_async_initable_real_init_async;
69   iface->init_finish = g_async_initable_real_init_finish;
70 }
71
72 /**
73  * g_async_initable_init_async:
74  * @initable: a #GAsyncInitable.
75  * @io_priority: the <link linkend="io-priority">I/O priority</link>
76  *     of the operation.
77  * @cancellable: optional #GCancellable object, %NULL to ignore.
78  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
79  * @user_data: the data to pass to callback function
80  *
81  * Starts asynchronous initialization of the object implementing the
82  * interface. This must be done before any real use of the object after
83  * initial construction. If the object also implements #GInitable you can
84  * optionally call g_initable_init() instead.
85  *
86  * When the initialization is finished, @callback will be called. You can
87  * then call g_async_initable_init_finish() to get the result of the
88  * initialization.
89  *
90  * Implementations may also support cancellation. If @cancellable is not
91  * %NULL, then initialization can be cancelled by triggering the cancellable
92  * object from another thread. If the operation was cancelled, the error
93  * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL, and
94  * the object doesn't support cancellable initialization, the error
95  * %G_IO_ERROR_NOT_SUPPORTED will be returned.
96  *
97  * If this function is not called, or returns with an error, then all
98  * operations on the object should fail, generally returning the
99  * error %G_IO_ERROR_NOT_INITIALIZED.
100  *
101  * Implementations of this method must be idempotent: i.e. multiple calls
102  * to this function with the same argument should return the same results.
103  * Only the first call initializes the object; further calls return the result
104  * of the first call. This is so that it's safe to implement the singleton
105  * pattern in the GObject constructor function.
106  *
107  * For classes that also support the #GInitable interface, the default
108  * implementation of this method will run the g_initable_init() function
109  * in a thread, so if you want to support asynchronous initialization via
110  * threads, just implement the #GAsyncInitable interface without overriding
111  * any interface methods.
112  *
113  * Since: 2.22
114  */
115 void
116 g_async_initable_init_async (GAsyncInitable      *initable,
117                              int                  io_priority,
118                              GCancellable        *cancellable,
119                              GAsyncReadyCallback  callback,
120                              gpointer             user_data)
121 {
122   GAsyncInitableIface *iface;
123
124   g_return_if_fail (G_IS_ASYNC_INITABLE (initable));
125
126   iface = G_ASYNC_INITABLE_GET_IFACE (initable);
127
128   (* iface->init_async) (initable, io_priority, cancellable, callback, user_data);
129 }
130
131 /**
132  * g_async_initable_init_finish:
133  * @initable: a #GAsyncInitable.
134  * @res: a #GAsyncResult.
135  * @error: a #GError location to store the error occuring, or %NULL to
136  * ignore.
137  *
138  * Finishes asynchronous initialization and returns the result.
139  * See g_async_initable_init_async().
140  *
141  * Returns: %TRUE if successful. If an error has occurred, this function
142  * will return %FALSE and set @error appropriately if present.
143  *
144  * Since: 2.22
145  */
146 gboolean
147 g_async_initable_init_finish (GAsyncInitable  *initable,
148                               GAsyncResult    *res,
149                               GError         **error)
150 {
151   GAsyncInitableIface *iface;
152
153   g_return_val_if_fail (G_IS_ASYNC_INITABLE (initable), FALSE);
154   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
155
156   if (G_IS_SIMPLE_ASYNC_RESULT (res))
157     {
158       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
159       if (g_simple_async_result_propagate_error (simple, error))
160         return FALSE;
161     }
162
163   iface = G_ASYNC_INITABLE_GET_IFACE (initable);
164
165   return (* iface->init_finish) (initable, res, error);
166 }
167
168 static void
169 async_init_thread (GSimpleAsyncResult *res,
170                    GObject            *object,
171                    GCancellable       *cancellable)
172 {
173   GError *error = NULL;
174
175   if (!g_initable_init (G_INITABLE (object), cancellable, &error))
176     {
177       g_simple_async_result_set_from_error (res, error);
178       g_error_free (error);
179     }
180 }
181
182 static void
183 g_async_initable_real_init_async (GAsyncInitable      *initable,
184                                   int                  io_priority,
185                                   GCancellable        *cancellable,
186                                   GAsyncReadyCallback  callback,
187                                   gpointer             user_data)
188 {
189   GSimpleAsyncResult *res;
190
191   g_return_if_fail (G_IS_INITABLE (initable));
192
193   res = g_simple_async_result_new (G_OBJECT (initable), callback, user_data,
194                                    g_async_initable_real_init_async);
195   g_simple_async_result_run_in_thread (res, async_init_thread,
196                                        io_priority, cancellable);
197   g_object_unref (res);
198 }
199
200 static gboolean
201 g_async_initable_real_init_finish (GAsyncInitable  *initable,
202                                    GAsyncResult    *res,
203                                    GError         **error)
204 {
205   return TRUE; /* Errors handled by base impl */
206 }
207
208 /**
209  * g_async_initable_new_async:
210  * @object_type: a #GType supporting #GAsyncInitable.
211  * @io_priority: the <link linkend="io-priority">I/O priority</link>
212  *     of the operation.
213  * @cancellable: optional #GCancellable object, %NULL to ignore.
214  * @callback: a #GAsyncReadyCallback to call when the initialization is
215  *     finished
216  * @user_data: the data to pass to callback function
217  * @first_property_name: the name of the first property, or %NULL if no
218  *     properties
219  * @...:  the value of the first property, followed by other property
220  *    value pairs, and ended by %NULL.
221  *
222  * Helper function for constructing #GAsyncInitiable object. This is
223  * similar to g_object_new() but also initializes the object asynchronously.
224  *
225  * When the initialization is finished, @callback will be called. You can
226  * then call g_async_initable_new_finish() to get the new object and check
227  * for any errors.
228  *
229  * Since: 2.22
230  */
231 void
232 g_async_initable_new_async (GType                object_type,
233                             int                  io_priority,
234                             GCancellable        *cancellable,
235                             GAsyncReadyCallback  callback,
236                             gpointer             user_data,
237                             const gchar         *first_property_name,
238                             ...)
239 {
240   va_list var_args;
241
242   va_start (var_args, first_property_name);
243   g_async_initable_new_valist_async (object_type,
244                                      first_property_name, var_args,
245                                      io_priority, cancellable,
246                                      callback, user_data);
247   va_end (var_args);
248 }
249
250 /**
251  * g_async_initable_newv_async:
252  * @object_type: a #GType supporting #GAsyncInitable.
253  * @n_parameters: the number of parameters in @parameters
254  * @parameters: the parameters to use to construct the object
255  * @io_priority: the <link linkend="io-priority">I/O priority</link>
256  *     of the operation.
257  * @cancellable: optional #GCancellable object, %NULL to ignore.
258  * @callback: a #GAsyncReadyCallback to call when the initialization is
259  *     finished
260  * @user_data: the data to pass to callback function
261  *
262  * Helper function for constructing #GAsyncInitiable object. This is
263  * similar to g_object_newv() but also initializes the object asynchronously.
264  *
265  * When the initialization is finished, @callback will be called. You can
266  * then call g_async_initable_new_finish() to get the new object and check
267  * for any errors.
268  *
269  * Since: 2.22
270  */
271 void
272 g_async_initable_newv_async (GType                object_type,
273                              guint                n_parameters,
274                              GParameter          *parameters,
275                              int                  io_priority,
276                              GCancellable        *cancellable,
277                              GAsyncReadyCallback  callback,
278                              gpointer             user_data)
279 {
280   GObject *obj;
281
282   g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
283
284   obj = g_object_newv (object_type, n_parameters, parameters);
285
286   g_async_initable_init_async (G_ASYNC_INITABLE (obj),
287                                io_priority, cancellable,
288                                callback, user_data);
289 }
290
291 /**
292  * g_async_initable_new_valist_async:
293  * @object_type: a #GType supporting #GAsyncInitable.
294  * @first_property_name: the name of the first property, followed by
295  * the value, and other property value pairs, and ended by %NULL.
296  * @var_args: The var args list generated from @first_property_name.
297  * @io_priority: the <link linkend="io-priority">I/O priority</link>
298  *     of the operation.
299  * @cancellable: optional #GCancellable object, %NULL to ignore.
300  * @callback: a #GAsyncReadyCallback to call when the initialization is
301  *     finished
302  * @user_data: the data to pass to callback function
303  *
304  * Helper function for constructing #GAsyncInitiable object. This is
305  * similar to g_object_new_valist() but also initializes the object
306  * asynchronously.
307  *
308  * When the initialization is finished, @callback will be called. You can
309  * then call g_async_initable_new_finish() to get the new object and check
310  * for any errors.
311  *
312  * Since: 2.22
313  */
314 void
315 g_async_initable_new_valist_async (GType                object_type,
316                                    const gchar         *first_property_name,
317                                    va_list              var_args,
318                                    int                  io_priority,
319                                    GCancellable        *cancellable,
320                                    GAsyncReadyCallback  callback,
321                                    gpointer             user_data)
322 {
323   GObject *obj;
324
325   g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
326
327   obj = g_object_new_valist (object_type,
328                              first_property_name,
329                              var_args);
330
331   g_async_initable_init_async (G_ASYNC_INITABLE (obj),
332                                io_priority, cancellable,
333                                callback, user_data);
334   g_object_unref (obj); /* Passed ownership to async call */
335 }
336
337 /**
338  * g_async_initable_new_finish:
339  * @initable: the #GAsyncInitable from the callback
340  * @res: the #GAsyncResult.from the callback
341  * @error: a #GError location to store the error occuring, or %NULL to
342  *     ignore.
343  *
344  * Finishes the async construction for the various g_async_initable_new calls,
345  * returning the created object or %NULL on error.
346  *
347  * Returns: a newly created #GObject, or %NULL on error. Free with
348  *     g_object_unref().
349  *
350  * Since: 2.22
351  */
352 GObject *
353 g_async_initable_new_finish (GAsyncInitable  *initable,
354                              GAsyncResult    *res,
355                              GError         **error)
356 {
357   if (g_async_initable_init_finish (initable, res, error))
358     return g_object_ref (initable);
359   else
360     return NULL;
361 }
362
363 #define __G_ASYNC_INITABLE_C__
364 #include "gioaliasdef.c"