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