1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD 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
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gio.AsyncInitableT;
26 
27 public  import gio.AsyncResultIF;
28 public  import gio.Cancellable;
29 public  import gio.c.functions;
30 public  import gio.c.types;
31 public  import glib.ErrorG;
32 public  import glib.GException;
33 public  import glib.Str;
34 public  import gobject.ObjectG;
35 public  import gtkc.giotypes;
36 
37 
38 /**
39  * This is the asynchronous version of #GInitable; it behaves the same
40  * in all ways except that initialization is asynchronous. For more details
41  * see the descriptions on #GInitable.
42  * 
43  * A class may implement both the #GInitable and #GAsyncInitable interfaces.
44  * 
45  * Users of objects implementing this are not intended to use the interface
46  * method directly; instead it will be used automatically in various ways.
47  * For C applications you generally just call g_async_initable_new_async()
48  * directly, or indirectly via a foo_thing_new_async() wrapper. This will call
49  * g_async_initable_init_async() under the cover, calling back with %NULL and
50  * a set %GError on failure.
51  * 
52  * A typical implementation might look something like this:
53  * 
54  * |[<!-- language="C" -->
55  * enum {
56  * NOT_INITIALIZED,
57  * INITIALIZING,
58  * INITIALIZED
59  * };
60  * 
61  * static void
62  * _foo_ready_cb (Foo *self)
63  * {
64  * GList *l;
65  * 
66  * self->priv->state = INITIALIZED;
67  * 
68  * for (l = self->priv->init_results; l != NULL; l = l->next)
69  * {
70  * GTask *task = l->data;
71  * 
72  * if (self->priv->success)
73  * g_task_return_boolean (task, TRUE);
74  * else
75  * g_task_return_new_error (task, ...);
76  * g_object_unref (task);
77  * }
78  * 
79  * g_list_free (self->priv->init_results);
80  * self->priv->init_results = NULL;
81  * }
82  * 
83  * static void
84  * foo_init_async (GAsyncInitable       *initable,
85  * int                   io_priority,
86  * GCancellable         *cancellable,
87  * GAsyncReadyCallback   callback,
88  * gpointer              user_data)
89  * {
90  * Foo *self = FOO (initable);
91  * GTask *task;
92  * 
93  * task = g_task_new (initable, cancellable, callback, user_data);
94  * 
95  * switch (self->priv->state)
96  * {
97  * case NOT_INITIALIZED:
98  * _foo_get_ready (self);
99  * self->priv->init_results = g_list_append (self->priv->init_results,
100  * task);
101  * self->priv->state = INITIALIZING;
102  * break;
103  * case INITIALIZING:
104  * self->priv->init_results = g_list_append (self->priv->init_results,
105  * task);
106  * break;
107  * case INITIALIZED:
108  * if (!self->priv->success)
109  * g_task_return_new_error (task, ...);
110  * else
111  * g_task_return_boolean (task, TRUE);
112  * g_object_unref (task);
113  * break;
114  * }
115  * }
116  * 
117  * static gboolean
118  * foo_init_finish (GAsyncInitable       *initable,
119  * GAsyncResult         *result,
120  * GError              **error)
121  * {
122  * g_return_val_if_fail (g_task_is_valid (result, initable), FALSE);
123  * 
124  * return g_task_propagate_boolean (G_TASK (result), error);
125  * }
126  * 
127  * static void
128  * foo_async_initable_iface_init (gpointer g_iface,
129  * gpointer data)
130  * {
131  * GAsyncInitableIface *iface = g_iface;
132  * 
133  * iface->init_async = foo_init_async;
134  * iface->init_finish = foo_init_finish;
135  * }
136  * ]|
137  *
138  * Since: 2.22
139  */
140 public template AsyncInitableT(TStruct)
141 {
142 	/** Get the main Gtk struct */
143 	public GAsyncInitable* getAsyncInitableStruct(bool transferOwnership = false)
144 	{
145 		if (transferOwnership)
146 			ownedRef = false;
147 		return cast(GAsyncInitable*)getStruct();
148 	}
149 
150 
151 	/**
152 	 * Helper function for constructing #GAsyncInitable object. This is
153 	 * similar to g_object_new_valist() but also initializes the object
154 	 * asynchronously.
155 	 *
156 	 * When the initialization is finished, @callback will be called. You can
157 	 * then call g_async_initable_new_finish() to get the new object and check
158 	 * for any errors.
159 	 *
160 	 * Params:
161 	 *     objectType = a #GType supporting #GAsyncInitable.
162 	 *     firstPropertyName = the name of the first property, followed by
163 	 *         the value, and other property value pairs, and ended by %NULL.
164 	 *     varArgs = The var args list generated from @first_property_name.
165 	 *     ioPriority = the [I/O priority][io-priority] of the operation
166 	 *     cancellable = optional #GCancellable object, %NULL to ignore.
167 	 *     callback = a #GAsyncReadyCallback to call when the initialization is
168 	 *         finished
169 	 *     userData = the data to pass to callback function
170 	 *
171 	 * Since: 2.22
172 	 */
173 	public static void newValistAsync(GType objectType, string firstPropertyName, void* varArgs, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
174 	{
175 		g_async_initable_new_valist_async(objectType, Str.toStringz(firstPropertyName), varArgs, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
176 	}
177 
178 	/**
179 	 * Helper function for constructing #GAsyncInitable object. This is
180 	 * similar to g_object_newv() but also initializes the object asynchronously.
181 	 *
182 	 * When the initialization is finished, @callback will be called. You can
183 	 * then call g_async_initable_new_finish() to get the new object and check
184 	 * for any errors.
185 	 *
186 	 * Deprecated: Use g_object_new_with_properties() and
187 	 * g_async_initable_init_async() instead. See #GParameter for more information.
188 	 *
189 	 * Params:
190 	 *     objectType = a #GType supporting #GAsyncInitable.
191 	 *     nParameters = the number of parameters in @parameters
192 	 *     parameters = the parameters to use to construct the object
193 	 *     ioPriority = the [I/O priority][io-priority] of the operation
194 	 *     cancellable = optional #GCancellable object, %NULL to ignore.
195 	 *     callback = a #GAsyncReadyCallback to call when the initialization is
196 	 *         finished
197 	 *     userData = the data to pass to callback function
198 	 *
199 	 * Since: 2.22
200 	 */
201 	public static void newvAsync(GType objectType, uint nParameters, GParameter* parameters, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
202 	{
203 		g_async_initable_newv_async(objectType, nParameters, parameters, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
204 	}
205 
206 	/**
207 	 * Starts asynchronous initialization of the object implementing the
208 	 * interface. This must be done before any real use of the object after
209 	 * initial construction. If the object also implements #GInitable you can
210 	 * optionally call g_initable_init() instead.
211 	 *
212 	 * This method is intended for language bindings. If writing in C,
213 	 * g_async_initable_new_async() should typically be used instead.
214 	 *
215 	 * When the initialization is finished, @callback will be called. You can
216 	 * then call g_async_initable_init_finish() to get the result of the
217 	 * initialization.
218 	 *
219 	 * Implementations may also support cancellation. If @cancellable is not
220 	 * %NULL, then initialization can be cancelled by triggering the cancellable
221 	 * object from another thread. If the operation was cancelled, the error
222 	 * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL, and
223 	 * the object doesn't support cancellable initialization, the error
224 	 * %G_IO_ERROR_NOT_SUPPORTED will be returned.
225 	 *
226 	 * As with #GInitable, if the object is not initialized, or initialization
227 	 * returns with an error, then all operations on the object except
228 	 * g_object_ref() and g_object_unref() are considered to be invalid, and
229 	 * have undefined behaviour. They will often fail with g_critical() or
230 	 * g_warning(), but this must not be relied on.
231 	 *
232 	 * Callers should not assume that a class which implements #GAsyncInitable can
233 	 * be initialized multiple times; for more information, see g_initable_init().
234 	 * If a class explicitly supports being initialized multiple times,
235 	 * implementation requires yielding all subsequent calls to init_async() on the
236 	 * results of the first call.
237 	 *
238 	 * For classes that also support the #GInitable interface, the default
239 	 * implementation of this method will run the g_initable_init() function
240 	 * in a thread, so if you want to support asynchronous initialization via
241 	 * threads, just implement the #GAsyncInitable interface without overriding
242 	 * any interface methods.
243 	 *
244 	 * Params:
245 	 *     ioPriority = the [I/O priority][io-priority] of the operation
246 	 *     cancellable = optional #GCancellable object, %NULL to ignore.
247 	 *     callback = a #GAsyncReadyCallback to call when the request is satisfied
248 	 *     userData = the data to pass to callback function
249 	 *
250 	 * Since: 2.22
251 	 */
252 	public void initAsync(int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
253 	{
254 		g_async_initable_init_async(getAsyncInitableStruct(), ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
255 	}
256 
257 	/**
258 	 * Finishes asynchronous initialization and returns the result.
259 	 * See g_async_initable_init_async().
260 	 *
261 	 * Params:
262 	 *     res = a #GAsyncResult.
263 	 *
264 	 * Returns: %TRUE if successful. If an error has occurred, this function
265 	 *     will return %FALSE and set @error appropriately if present.
266 	 *
267 	 * Since: 2.22
268 	 *
269 	 * Throws: GException on failure.
270 	 */
271 	public bool initFinish(AsyncResultIF res)
272 	{
273 		GError* err = null;
274 
275 		auto p = g_async_initable_init_finish(getAsyncInitableStruct(), (res is null) ? null : res.getAsyncResultStruct(), &err) != 0;
276 
277 		if (err !is null)
278 		{
279 			throw new GException( new ErrorG(err) );
280 		}
281 
282 		return p;
283 	}
284 
285 	/**
286 	 * Finishes the async construction for the various g_async_initable_new
287 	 * calls, returning the created object or %NULL on error.
288 	 *
289 	 * Params:
290 	 *     res = the #GAsyncResult from the callback
291 	 *
292 	 * Returns: a newly created #GObject,
293 	 *     or %NULL on error. Free with g_object_unref().
294 	 *
295 	 * Since: 2.22
296 	 *
297 	 * Throws: GException on failure.
298 	 */
299 	public ObjectG newFinish(AsyncResultIF res)
300 	{
301 		GError* err = null;
302 
303 		auto p = g_async_initable_new_finish(getAsyncInitableStruct(), (res is null) ? null : res.getAsyncResultStruct(), &err);
304 
305 		if (err !is null)
306 		{
307 			throw new GException( new ErrorG(err) );
308 		}
309 
310 		if(p is null)
311 		{
312 			return null;
313 		}
314 
315 		return ObjectG.getDObject!(ObjectG)(cast(GObject*) p, true);
316 	}
317 }