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.VolumeT;
26 
27 public  import gio.AsyncResultIF;
28 public  import gio.Cancellable;
29 public  import gio.Drive;
30 public  import gio.DriveIF;
31 public  import gio.File;
32 public  import gio.FileIF;
33 public  import gio.Icon;
34 public  import gio.IconIF;
35 public  import gio.Mount;
36 public  import gio.MountIF;
37 public  import gio.MountOperation;
38 public  import glib.ErrorG;
39 public  import glib.GException;
40 public  import glib.Str;
41 public  import gobject.ObjectG;
42 public  import gobject.Signals;
43 public  import gtkc.gdktypes;
44 public  import gtkc.gio;
45 public  import gtkc.giotypes;
46 public  import std.algorithm;
47 
48 
49 /**
50  * The #GVolume interface represents user-visible objects that can be
51  * mounted. Note, when porting from GnomeVFS, #GVolume is the moral
52  * equivalent of #GnomeVFSDrive.
53  * 
54  * Mounting a #GVolume instance is an asynchronous operation. For more
55  * information about asynchronous operations, see #GAsyncResult and
56  * #GTask. To mount a #GVolume, first call g_volume_mount() with (at
57  * least) the #GVolume instance, optionally a #GMountOperation object
58  * and a #GAsyncReadyCallback.
59  * 
60  * Typically, one will only want to pass %NULL for the
61  * #GMountOperation if automounting all volumes when a desktop session
62  * starts since it's not desirable to put up a lot of dialogs asking
63  * for credentials.
64  * 
65  * The callback will be fired when the operation has resolved (either
66  * with success or failure), and a #GAsyncReady structure will be
67  * passed to the callback.  That callback should then call
68  * g_volume_mount_finish() with the #GVolume instance and the
69  * #GAsyncReady data to see if the operation was completed
70  * successfully.  If an @error is present when g_volume_mount_finish()
71  * is called, then it will be filled with any error information.
72  * 
73  * ## Volume Identifiers # {#volume-identifier}
74  * 
75  * It is sometimes necessary to directly access the underlying
76  * operating system object behind a volume (e.g. for passing a volume
77  * to an application via the commandline). For this purpose, GIO
78  * allows to obtain an 'identifier' for the volume. There can be
79  * different kinds of identifiers, such as Hal UDIs, filesystem labels,
80  * traditional Unix devices (e.g. `/dev/sda2`), UUIDs. GIO uses predefined
81  * strings as names for the different kinds of identifiers:
82  * #G_VOLUME_IDENTIFIER_KIND_HAL_UDI, #G_VOLUME_IDENTIFIER_KIND_LABEL, etc.
83  * Use g_volume_get_identifier() to obtain an identifier for a volume.
84  * 
85  * 
86  * Note that #G_VOLUME_IDENTIFIER_KIND_HAL_UDI will only be available
87  * when the gvfs hal volume monitor is in use. Other volume monitors
88  * will generally be able to provide the #G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE
89  * identifier, which can be used to obtain a hal device by means of
90  * libhal_manager_find_device_string_match().
91  */
92 public template VolumeT(TStruct)
93 {
94 	/** Get the main Gtk struct */
95 	public GVolume* getVolumeStruct()
96 	{
97 		return cast(GVolume*)getStruct();
98 	}
99 
100 
101 	/**
102 	 * Checks if a volume can be ejected.
103 	 *
104 	 * Return: %TRUE if the @volume can be ejected. %FALSE otherwise
105 	 */
106 	public bool canEject()
107 	{
108 		return g_volume_can_eject(getVolumeStruct()) != 0;
109 	}
110 
111 	/**
112 	 * Checks if a volume can be mounted.
113 	 *
114 	 * Return: %TRUE if the @volume can be mounted. %FALSE otherwise
115 	 */
116 	public bool canMount()
117 	{
118 		return g_volume_can_mount(getVolumeStruct()) != 0;
119 	}
120 
121 	/**
122 	 * Ejects a volume. This is an asynchronous operation, and is
123 	 * finished by calling g_volume_eject_finish() with the @volume
124 	 * and #GAsyncResult returned in the @callback.
125 	 *
126 	 * Deprecated: Use g_volume_eject_with_operation() instead.
127 	 *
128 	 * Params:
129 	 *     flags = flags affecting the unmount if required for eject
130 	 *     cancellable = optional #GCancellable object, %NULL to ignore
131 	 *     callback = a #GAsyncReadyCallback, or %NULL
132 	 *     userData = user data that gets passed to @callback
133 	 */
134 	public void eject(GMountUnmountFlags flags, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
135 	{
136 		g_volume_eject(getVolumeStruct(), flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
137 	}
138 
139 	/**
140 	 * Finishes ejecting a volume. If any errors occurred during the operation,
141 	 * @error will be set to contain the errors and %FALSE will be returned.
142 	 *
143 	 * Deprecated: Use g_volume_eject_with_operation_finish() instead.
144 	 *
145 	 * Params:
146 	 *     result = a #GAsyncResult
147 	 *
148 	 * Return: %TRUE, %FALSE if operation failed
149 	 *
150 	 * Throws: GException on failure.
151 	 */
152 	public bool ejectFinish(AsyncResultIF result)
153 	{
154 		GError* err = null;
155 		
156 		auto p = g_volume_eject_finish(getVolumeStruct(), (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
157 		
158 		if (err !is null)
159 		{
160 			throw new GException( new ErrorG(err) );
161 		}
162 		
163 		return p;
164 	}
165 
166 	/**
167 	 * Ejects a volume. This is an asynchronous operation, and is
168 	 * finished by calling g_volume_eject_with_operation_finish() with the @volume
169 	 * and #GAsyncResult data returned in the @callback.
170 	 *
171 	 * Params:
172 	 *     flags = flags affecting the unmount if required for eject
173 	 *     mountOperation = a #GMountOperation or %NULL to
174 	 *         avoid user interaction
175 	 *     cancellable = optional #GCancellable object, %NULL to ignore
176 	 *     callback = a #GAsyncReadyCallback, or %NULL
177 	 *     userData = user data passed to @callback
178 	 *
179 	 * Since: 2.22
180 	 */
181 	public void ejectWithOperation(GMountUnmountFlags flags, MountOperation mountOperation, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
182 	{
183 		g_volume_eject_with_operation(getVolumeStruct(), flags, (mountOperation is null) ? null : mountOperation.getMountOperationStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
184 	}
185 
186 	/**
187 	 * Finishes ejecting a volume. If any errors occurred during the operation,
188 	 * @error will be set to contain the errors and %FALSE will be returned.
189 	 *
190 	 * Params:
191 	 *     result = a #GAsyncResult
192 	 *
193 	 * Return: %TRUE if the volume was successfully ejected. %FALSE otherwise
194 	 *
195 	 * Since: 2.22
196 	 *
197 	 * Throws: GException on failure.
198 	 */
199 	public bool ejectWithOperationFinish(AsyncResultIF result)
200 	{
201 		GError* err = null;
202 		
203 		auto p = g_volume_eject_with_operation_finish(getVolumeStruct(), (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
204 		
205 		if (err !is null)
206 		{
207 			throw new GException( new ErrorG(err) );
208 		}
209 		
210 		return p;
211 	}
212 
213 	/**
214 	 * Gets the kinds of [identifiers][volume-identifier] that @volume has.
215 	 * Use g_volume_get_identifier() to obtain the identifiers themselves.
216 	 *
217 	 * Return: a %NULL-terminated array
218 	 *     of strings containing kinds of identifiers. Use g_strfreev() to free.
219 	 */
220 	public string[] enumerateIdentifiers()
221 	{
222 		auto retStr = g_volume_enumerate_identifiers(getVolumeStruct());
223 		
224 		scope(exit) Str.freeStringArray(retStr);
225 		return Str.toStringArray(retStr);
226 	}
227 
228 	/**
229 	 * Gets the activation root for a #GVolume if it is known ahead of
230 	 * mount time. Returns %NULL otherwise. If not %NULL and if @volume
231 	 * is mounted, then the result of g_mount_get_root() on the
232 	 * #GMount object obtained from g_volume_get_mount() will always
233 	 * either be equal or a prefix of what this function returns. In
234 	 * other words, in code
235 	 *
236 	 * |[<!-- language="C" -->
237 	 * GMount *mount;
238 	 * GFile *mount_root
239 	 * GFile *volume_activation_root;
240 	 *
241 	 * mount = g_volume_get_mount (volume); // mounted, so never NULL
242 	 * mount_root = g_mount_get_root (mount);
243 	 * volume_activation_root = g_volume_get_activation_root (volume); // assume not NULL
244 	 * ]|
245 	 * then the expression
246 	 * |[<!-- language="C" -->
247 	 * (g_file_has_prefix (volume_activation_root, mount_root) ||
248 	 * g_file_equal (volume_activation_root, mount_root))
249 	 * ]|
250 	 * will always be %TRUE.
251 	 *
252 	 * Activation roots are typically used in #GVolumeMonitor
253 	 * implementations to find the underlying mount to shadow, see
254 	 * g_mount_is_shadowed() for more details.
255 	 *
256 	 * Return: the activation root of @volume
257 	 *     or %NULL. Use g_object_unref() to free.
258 	 *
259 	 * Since: 2.18
260 	 */
261 	public FileIF getActivationRoot()
262 	{
263 		auto p = g_volume_get_activation_root(getVolumeStruct());
264 		
265 		if(p is null)
266 		{
267 			return null;
268 		}
269 		
270 		return ObjectG.getDObject!(File, FileIF)(cast(GFile*) p, true);
271 	}
272 
273 	/**
274 	 * Gets the drive for the @volume.
275 	 *
276 	 * Return: a #GDrive or %NULL if @volume is not
277 	 *     associated with a drive. The returned object should be unreffed
278 	 *     with g_object_unref() when no longer needed.
279 	 */
280 	public DriveIF getDrive()
281 	{
282 		auto p = g_volume_get_drive(getVolumeStruct());
283 		
284 		if(p is null)
285 		{
286 			return null;
287 		}
288 		
289 		return ObjectG.getDObject!(Drive, DriveIF)(cast(GDrive*) p, true);
290 	}
291 
292 	/**
293 	 * Gets the icon for @volume.
294 	 *
295 	 * Return: a #GIcon.
296 	 *     The returned object should be unreffed with g_object_unref()
297 	 *     when no longer needed.
298 	 */
299 	public IconIF getIcon()
300 	{
301 		auto p = g_volume_get_icon(getVolumeStruct());
302 		
303 		if(p is null)
304 		{
305 			return null;
306 		}
307 		
308 		return ObjectG.getDObject!(Icon, IconIF)(cast(GIcon*) p, true);
309 	}
310 
311 	/**
312 	 * Gets the identifier of the given kind for @volume.
313 	 * See the [introduction][volume-identifier] for more
314 	 * information about volume identifiers.
315 	 *
316 	 * Params:
317 	 *     kind = the kind of identifier to return
318 	 *
319 	 * Return: a newly allocated string containing the
320 	 *     requested identfier, or %NULL if the #GVolume
321 	 *     doesn't have this kind of identifier
322 	 */
323 	public string getIdentifier(string kind)
324 	{
325 		auto retStr = g_volume_get_identifier(getVolumeStruct(), Str.toStringz(kind));
326 		
327 		scope(exit) Str.freeString(retStr);
328 		return Str.toString(retStr);
329 	}
330 
331 	/**
332 	 * Gets the mount for the @volume.
333 	 *
334 	 * Return: a #GMount or %NULL if @volume isn't mounted.
335 	 *     The returned object should be unreffed with g_object_unref()
336 	 *     when no longer needed.
337 	 */
338 	public MountIF getMount()
339 	{
340 		auto p = g_volume_get_mount(getVolumeStruct());
341 		
342 		if(p is null)
343 		{
344 			return null;
345 		}
346 		
347 		return ObjectG.getDObject!(Mount, MountIF)(cast(GMount*) p, true);
348 	}
349 
350 	/**
351 	 * Gets the name of @volume.
352 	 *
353 	 * Return: the name for the given @volume. The returned string should
354 	 *     be freed with g_free() when no longer needed.
355 	 */
356 	public string getName()
357 	{
358 		auto retStr = g_volume_get_name(getVolumeStruct());
359 		
360 		scope(exit) Str.freeString(retStr);
361 		return Str.toString(retStr);
362 	}
363 
364 	/**
365 	 * Gets the sort key for @volume, if any.
366 	 *
367 	 * Return: Sorting key for @volume or %NULL if no such key is available
368 	 *
369 	 * Since: 2.32
370 	 */
371 	public string getSortKey()
372 	{
373 		return Str.toString(g_volume_get_sort_key(getVolumeStruct()));
374 	}
375 
376 	/**
377 	 * Gets the symbolic icon for @volume.
378 	 *
379 	 * Return: a #GIcon.
380 	 *     The returned object should be unreffed with g_object_unref()
381 	 *     when no longer needed.
382 	 *
383 	 * Since: 2.34
384 	 */
385 	public IconIF getSymbolicIcon()
386 	{
387 		auto p = g_volume_get_symbolic_icon(getVolumeStruct());
388 		
389 		if(p is null)
390 		{
391 			return null;
392 		}
393 		
394 		return ObjectG.getDObject!(Icon, IconIF)(cast(GIcon*) p, true);
395 	}
396 
397 	/**
398 	 * Gets the UUID for the @volume. The reference is typically based on
399 	 * the file system UUID for the volume in question and should be
400 	 * considered an opaque string. Returns %NULL if there is no UUID
401 	 * available.
402 	 *
403 	 * Return: the UUID for @volume or %NULL if no UUID can be computed.
404 	 *     The returned string should be freed with g_free()
405 	 *     when no longer needed.
406 	 */
407 	public string getUuid()
408 	{
409 		auto retStr = g_volume_get_uuid(getVolumeStruct());
410 		
411 		scope(exit) Str.freeString(retStr);
412 		return Str.toString(retStr);
413 	}
414 
415 	/**
416 	 * Mounts a volume. This is an asynchronous operation, and is
417 	 * finished by calling g_volume_mount_finish() with the @volume
418 	 * and #GAsyncResult returned in the @callback.
419 	 *
420 	 * Params:
421 	 *     flags = flags affecting the operation
422 	 *     mountOperation = a #GMountOperation or %NULL to avoid user interaction
423 	 *     cancellable = optional #GCancellable object, %NULL to ignore
424 	 *     callback = a #GAsyncReadyCallback, or %NULL
425 	 *     userData = user data that gets passed to @callback
426 	 */
427 	public void mount(GMountMountFlags flags, MountOperation mountOperation, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
428 	{
429 		g_volume_mount(getVolumeStruct(), flags, (mountOperation is null) ? null : mountOperation.getMountOperationStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
430 	}
431 
432 	/**
433 	 * Finishes mounting a volume. If any errors occurred during the operation,
434 	 * @error will be set to contain the errors and %FALSE will be returned.
435 	 *
436 	 * If the mount operation succeeded, g_volume_get_mount() on @volume
437 	 * is guaranteed to return the mount right after calling this
438 	 * function; there's no need to listen for the 'mount-added' signal on
439 	 * #GVolumeMonitor.
440 	 *
441 	 * Params:
442 	 *     result = a #GAsyncResult
443 	 *
444 	 * Return: %TRUE, %FALSE if operation failed
445 	 *
446 	 * Throws: GException on failure.
447 	 */
448 	public bool mountFinish(AsyncResultIF result)
449 	{
450 		GError* err = null;
451 		
452 		auto p = g_volume_mount_finish(getVolumeStruct(), (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
453 		
454 		if (err !is null)
455 		{
456 			throw new GException( new ErrorG(err) );
457 		}
458 		
459 		return p;
460 	}
461 
462 	/**
463 	 * Returns whether the volume should be automatically mounted.
464 	 *
465 	 * Return: %TRUE if the volume should be automatically mounted
466 	 */
467 	public bool shouldAutomount()
468 	{
469 		return g_volume_should_automount(getVolumeStruct()) != 0;
470 	}
471 
472 	protected class OnChangedDelegateWrapper
473 	{
474 		void delegate(VolumeIF) dlg;
475 		gulong handlerId;
476 		ConnectFlags flags;
477 		this(void delegate(VolumeIF) dlg, gulong handlerId, ConnectFlags flags)
478 		{
479 			this.dlg = dlg;
480 			this.handlerId = handlerId;
481 			this.flags = flags;
482 		}
483 	}
484 	protected OnChangedDelegateWrapper[] onChangedListeners;
485 
486 	/**
487 	 * Emitted when the volume has been changed.
488 	 */
489 	gulong addOnChanged(void delegate(VolumeIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
490 	{
491 		onChangedListeners ~= new OnChangedDelegateWrapper(dlg, 0, connectFlags);
492 		onChangedListeners[onChangedListeners.length - 1].handlerId = Signals.connectData(
493 			this,
494 			"changed",
495 			cast(GCallback)&callBackChanged,
496 			cast(void*)onChangedListeners[onChangedListeners.length - 1],
497 			cast(GClosureNotify)&callBackChangedDestroy,
498 			connectFlags);
499 		return onChangedListeners[onChangedListeners.length - 1].handlerId;
500 	}
501 	
502 	extern(C) static void callBackChanged(GVolume* volumeStruct,OnChangedDelegateWrapper wrapper)
503 	{
504 		wrapper.dlg(wrapper.outer);
505 	}
506 	
507 	extern(C) static void callBackChangedDestroy(OnChangedDelegateWrapper wrapper, GClosure* closure)
508 	{
509 		wrapper.outer.internalRemoveOnChanged(wrapper);
510 	}
511 
512 	protected void internalRemoveOnChanged(OnChangedDelegateWrapper source)
513 	{
514 		foreach(index, wrapper; onChangedListeners)
515 		{
516 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
517 			{
518 				onChangedListeners[index] = null;
519 				onChangedListeners = std.algorithm.remove(onChangedListeners, index);
520 				break;
521 			}
522 		}
523 	}
524 	
525 
526 	protected class OnRemovedDelegateWrapper
527 	{
528 		void delegate(VolumeIF) dlg;
529 		gulong handlerId;
530 		ConnectFlags flags;
531 		this(void delegate(VolumeIF) dlg, gulong handlerId, ConnectFlags flags)
532 		{
533 			this.dlg = dlg;
534 			this.handlerId = handlerId;
535 			this.flags = flags;
536 		}
537 	}
538 	protected OnRemovedDelegateWrapper[] onRemovedListeners;
539 
540 	/**
541 	 * This signal is emitted when the #GVolume have been removed. If
542 	 * the recipient is holding references to the object they should
543 	 * release them so the object can be finalized.
544 	 */
545 	gulong addOnRemoved(void delegate(VolumeIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
546 	{
547 		onRemovedListeners ~= new OnRemovedDelegateWrapper(dlg, 0, connectFlags);
548 		onRemovedListeners[onRemovedListeners.length - 1].handlerId = Signals.connectData(
549 			this,
550 			"removed",
551 			cast(GCallback)&callBackRemoved,
552 			cast(void*)onRemovedListeners[onRemovedListeners.length - 1],
553 			cast(GClosureNotify)&callBackRemovedDestroy,
554 			connectFlags);
555 		return onRemovedListeners[onRemovedListeners.length - 1].handlerId;
556 	}
557 	
558 	extern(C) static void callBackRemoved(GVolume* volumeStruct,OnRemovedDelegateWrapper wrapper)
559 	{
560 		wrapper.dlg(wrapper.outer);
561 	}
562 	
563 	extern(C) static void callBackRemovedDestroy(OnRemovedDelegateWrapper wrapper, GClosure* closure)
564 	{
565 		wrapper.outer.internalRemoveOnRemoved(wrapper);
566 	}
567 
568 	protected void internalRemoveOnRemoved(OnRemovedDelegateWrapper source)
569 	{
570 		foreach(index, wrapper; onRemovedListeners)
571 		{
572 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
573 			{
574 				onRemovedListeners[index] = null;
575 				onRemovedListeners = std.algorithm.remove(onRemovedListeners, index);
576 				break;
577 			}
578 		}
579 	}
580 	
581 }