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 * Conversion parameters: 26 * inFile = GCancellable.html 27 * outPack = gio 28 * outFile = Cancellable 29 * strct = GCancellable 30 * realStrct= 31 * ctorStrct= 32 * clss = Cancellable 33 * interf = 34 * class Code: No 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * prefixes: 40 * - g_cancellable_ 41 * omit structs: 42 * omit prefixes: 43 * omit code: 44 * omit signals: 45 * imports: 46 * - glib.ErrorG 47 * - glib.GException 48 * - glib.Source 49 * structWrap: 50 * - GCancellable* -> Cancellable 51 * - GSource* -> Source 52 * module aliases: 53 * local aliases: 54 * overrides: 55 */ 56 57 module gio.Cancellable; 58 59 public import gtkc.giotypes; 60 61 private import gtkc.gio; 62 private import glib.ConstructionException; 63 private import gobject.ObjectG; 64 65 private import gobject.Signals; 66 public import gtkc.gdktypes; 67 68 private import glib.ErrorG; 69 private import glib.GException; 70 private import glib.Source; 71 72 73 74 private import gobject.ObjectG; 75 76 /** 77 * GCancellable is a thread-safe operation cancellation stack used 78 * throughout GIO to allow for cancellation of synchronous and 79 * asynchronous operations. 80 */ 81 public class Cancellable : ObjectG 82 { 83 84 /** the main Gtk struct */ 85 protected GCancellable* gCancellable; 86 87 88 public GCancellable* getCancellableStruct() 89 { 90 return gCancellable; 91 } 92 93 94 /** the main Gtk struct as a void* */ 95 protected override void* getStruct() 96 { 97 return cast(void*)gCancellable; 98 } 99 100 /** 101 * Sets our main struct and passes it to the parent class 102 */ 103 public this (GCancellable* gCancellable) 104 { 105 super(cast(GObject*)gCancellable); 106 this.gCancellable = gCancellable; 107 } 108 109 protected override void setStruct(GObject* obj) 110 { 111 super.setStruct(obj); 112 gCancellable = cast(GCancellable*)obj; 113 } 114 115 /** 116 */ 117 int[string] connectedSignals; 118 119 void delegate(Cancellable)[] onCancelledListeners; 120 /** 121 * Emitted when the operation has been cancelled. 122 * Can be used by implementations of cancellable operations. If the 123 * operation is cancelled from another thread, the signal will be 124 * emitted in the thread that cancelled the operation, not the 125 * thread that is running the operation. 126 * Note that disconnecting from this signal (or any signal) in a 127 * multi-threaded program is prone to race conditions. For instance 128 * it is possible that a signal handler may be invoked even 129 * after a call to 130 * g_signal_handler_disconnect() for that handler has already 131 * returned. 132 * There is also a problem when cancellation happen 133 * right before connecting to the signal. If this happens the 134 * signal will unexpectedly not be emitted, and checking before 135 * connecting to the signal leaves a race condition where this is 136 * still happening. 137 * In order to make it safe and easy to connect handlers there 138 * are two helper functions: g_cancellable_connect() and 139 * g_cancellable_disconnect() which protect against problems 140 * like this. 141 * $(DDOC_COMMENT example) 142 * Note that the cancelled signal is emitted in the thread that 143 * the user cancelled from, which may be the main thread. So, the 144 * cancellable signal should not do something that can block. 145 */ 146 void addOnCancelled(void delegate(Cancellable) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 147 { 148 if ( !("cancelled" in connectedSignals) ) 149 { 150 Signals.connectData( 151 getStruct(), 152 "cancelled", 153 cast(GCallback)&callBackCancelled, 154 cast(void*)this, 155 null, 156 connectFlags); 157 connectedSignals["cancelled"] = 1; 158 } 159 onCancelledListeners ~= dlg; 160 } 161 extern(C) static void callBackCancelled(GCancellable* cancellableStruct, Cancellable _cancellable) 162 { 163 foreach ( void delegate(Cancellable) dlg ; _cancellable.onCancelledListeners ) 164 { 165 dlg(_cancellable); 166 } 167 } 168 169 170 /** 171 * Creates a new GCancellable object. 172 * Applications that want to start one or more operations 173 * that should be cancellable should create a GCancellable 174 * and pass it to the operations. 175 * One GCancellable can be used in multiple consecutive 176 * operations or in multiple concurrent operations. 177 * Throws: ConstructionException GTK+ fails to create the object. 178 */ 179 public this () 180 { 181 // GCancellable * g_cancellable_new (void); 182 auto p = g_cancellable_new(); 183 if(p is null) 184 { 185 throw new ConstructionException("null returned by g_cancellable_new()"); 186 } 187 this(cast(GCancellable*) p); 188 } 189 190 /** 191 * Checks if a cancellable job has been cancelled. 192 * Returns: TRUE if cancellable is cancelled, FALSE if called with NULL or if item is not cancelled. 193 */ 194 public int isCancelled() 195 { 196 // gboolean g_cancellable_is_cancelled (GCancellable *cancellable); 197 return g_cancellable_is_cancelled(gCancellable); 198 } 199 200 /** 201 * If the cancellable is cancelled, sets the error to notify 202 * that the operation was cancelled. 203 * Returns: TRUE if cancellable was cancelled, FALSE if it was not 204 */ 205 public int setErrorIfCancelled() 206 { 207 // gboolean g_cancellable_set_error_if_cancelled (GCancellable *cancellable, GError **error); 208 GError* err = null; 209 210 auto p = g_cancellable_set_error_if_cancelled(gCancellable, &err); 211 212 if (err !is null) 213 { 214 throw new GException( new ErrorG(err) ); 215 } 216 217 return p; 218 } 219 220 /** 221 * Gets the file descriptor for a cancellable job. This can be used to 222 * implement cancellable operations on Unix systems. The returned fd will 223 * turn readable when cancellable is cancelled. 224 * You are not supposed to read from the fd yourself, just check for 225 * readable status. Reading to unset the readable status is done 226 * with g_cancellable_reset(). 227 * After a successful return from this function, you should use 228 * g_cancellable_release_fd() to free up resources allocated for 229 * the returned file descriptor. 230 * See also g_cancellable_make_pollfd(). 231 * Returns: A valid file descriptor. -1 if the file descriptor is not supported, or on errors. 232 */ 233 public int getFd() 234 { 235 // int g_cancellable_get_fd (GCancellable *cancellable); 236 return g_cancellable_get_fd(gCancellable); 237 } 238 239 /** 240 * Creates a GPollFD corresponding to cancellable; this can be passed 241 * to g_poll() and used to poll for cancellation. This is useful both 242 * for unix systems without a native poll and for portability to 243 * windows. 244 * When this function returns TRUE, you should use 245 * g_cancellable_release_fd() to free up resources allocated for the 246 * pollfd. After a FALSE return, do not call g_cancellable_release_fd(). 247 * If this function returns FALSE, either no cancellable was given or 248 * resource limits prevent this function from allocating the necessary 249 * structures for polling. (On Linux, you will likely have reached 250 * the maximum number of file descriptors.) The suggested way to handle 251 * these cases is to ignore the cancellable. 252 * You are not supposed to read from the fd yourself, just check for 253 * readable status. Reading to unset the readable status is done 254 * with g_cancellable_reset(). 255 * Since 2.22 256 * Params: 257 * pollfd = a pointer to a GPollFD 258 * Returns: TRUE if pollfd was successfully initialized, FALSE on failure to prepare the cancellable. 259 */ 260 public int makePollfd(GPollFD* pollfd) 261 { 262 // gboolean g_cancellable_make_pollfd (GCancellable *cancellable, GPollFD *pollfd); 263 return g_cancellable_make_pollfd(gCancellable, pollfd); 264 } 265 266 /** 267 * Releases a resources previously allocated by g_cancellable_get_fd() 268 * or g_cancellable_make_pollfd(). 269 * For compatibility reasons with older releases, calling this function 270 * is not strictly required, the resources will be automatically freed 271 * when the cancellable is finalized. However, the cancellable will 272 * block scarce file descriptors until it is finalized if this function 273 * is not called. This can cause the application to run out of file 274 * descriptors when many GCancellables are used at the same time. 275 * Since 2.22 276 */ 277 public void releaseFd() 278 { 279 // void g_cancellable_release_fd (GCancellable *cancellable); 280 g_cancellable_release_fd(gCancellable); 281 } 282 283 /** 284 * Creates a source that triggers if cancellable is cancelled and 285 * calls its callback of type GCancellableSourceFunc. This is 286 * primarily useful for attaching to another (non-cancellable) source 287 * with g_source_add_child_source() to add cancellability to it. 288 * For convenience, you can call this with a NULL GCancellable, 289 * in which case the source will never trigger. 290 * Since 2.28 291 * Returns: the new GSource. [transfer full] 292 */ 293 public Source sourceNew() 294 { 295 // GSource * g_cancellable_source_new (GCancellable *cancellable); 296 auto p = g_cancellable_source_new(gCancellable); 297 298 if(p is null) 299 { 300 return null; 301 } 302 303 return ObjectG.getDObject!(Source)(cast(GSource*) p); 304 } 305 306 /** 307 * Gets the top cancellable from the stack. 308 * Returns: a GCancellable from the top of the stack, or NULL if the stack is empty. [transfer none] 309 */ 310 public static Cancellable getCurrent() 311 { 312 // GCancellable * g_cancellable_get_current (void); 313 auto p = g_cancellable_get_current(); 314 315 if(p is null) 316 { 317 return null; 318 } 319 320 return ObjectG.getDObject!(Cancellable)(cast(GCancellable*) p); 321 } 322 323 /** 324 * Pops cancellable off the cancellable stack (verifying that cancellable 325 * is on the top of the stack). 326 */ 327 public void popCurrent() 328 { 329 // void g_cancellable_pop_current (GCancellable *cancellable); 330 g_cancellable_pop_current(gCancellable); 331 } 332 333 /** 334 * Pushes cancellable onto the cancellable stack. The current 335 * cancellable can then be received using g_cancellable_get_current(). 336 * This is useful when implementing cancellable operations in 337 * code that does not allow you to pass down the cancellable object. 338 * This is typically called automatically by e.g. GFile operations, 339 * so you rarely have to call this yourself. 340 */ 341 public void pushCurrent() 342 { 343 // void g_cancellable_push_current (GCancellable *cancellable); 344 g_cancellable_push_current(gCancellable); 345 } 346 347 /** 348 * Resets cancellable to its uncancelled state. 349 * If cancellable is currently in use by any cancellable operation 350 * then the behavior of this function is undefined. 351 */ 352 public void reset() 353 { 354 // void g_cancellable_reset (GCancellable *cancellable); 355 g_cancellable_reset(gCancellable); 356 } 357 358 /** 359 * Convenience function to connect to the "cancelled" 360 * signal. Also handles the race condition that may happen 361 * if the cancellable is cancelled right before connecting. 362 * callback is called at most once, either directly at the 363 * time of the connect if cancellable is already cancelled, 364 * or when cancellable is cancelled in some thread. 365 * data_destroy_func will be called when the handler is 366 * disconnected, or immediately if the cancellable is already 367 * cancelled. 368 * See "cancelled" for details on how to use this. 369 * Since 2.22 370 * Params: 371 * callback = The GCallback to connect. 372 * data = Data to pass to callback. 373 * dataDestroyFunc = Free function for data or NULL. [allow-none] 374 * Returns: The id of the signal handler or 0 if cancellable has already been cancelled. 375 */ 376 public gulong connect(GCallback callback, void* data, GDestroyNotify dataDestroyFunc) 377 { 378 // gulong g_cancellable_connect (GCancellable *cancellable, GCallback callback, gpointer data, GDestroyNotify data_destroy_func); 379 return g_cancellable_connect(gCancellable, callback, data, dataDestroyFunc); 380 } 381 382 /** 383 * Disconnects a handler from a cancellable instance similar to 384 * g_signal_handler_disconnect(). Additionally, in the event that a 385 * signal handler is currently running, this call will block until the 386 * handler has finished. Calling this function from a 387 * "cancelled" signal handler will therefore result in a 388 * deadlock. 389 * This avoids a race condition where a thread cancels at the 390 * same time as the cancellable operation is finished and the 391 * signal handler is removed. See "cancelled" for 392 * details on how to use this. 393 * If cancellable is NULL or handler_id is 0 this function does 394 * nothing. 395 * Since 2.22 396 * Params: 397 * handlerId = Handler id of the handler to be disconnected, or 0. 398 */ 399 public void disconnect(gulong handlerId) 400 { 401 // void g_cancellable_disconnect (GCancellable *cancellable, gulong handler_id); 402 g_cancellable_disconnect(gCancellable, handlerId); 403 } 404 405 /** 406 * Will set cancellable to cancelled, and will emit the 407 * "cancelled" signal. (However, see the warning about 408 * race conditions in the documentation for that signal if you are 409 * planning to connect to it.) 410 * This function is thread-safe. In other words, you can safely call 411 * it from a thread other than the one running the operation that was 412 * passed the cancellable. 413 * The convention within gio is that cancelling an asynchronous 414 * operation causes it to complete asynchronously. That is, if you 415 * cancel the operation from the same thread in which it is running, 416 * then the operation's GAsyncReadyCallback will not be invoked until 417 * the application returns to the main loop. 418 */ 419 public void cancel() 420 { 421 // void g_cancellable_cancel (GCancellable *cancellable); 422 g_cancellable_cancel(gCancellable); 423 } 424 }