1 /*
2  * This file is part of d-dazzle.
3  *
4  * d-dazzle 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  * d-dazzle 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 d-dazzle; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 module dazzle.RecursiveFileMonitor;
20 
21 private import dazzle.c.functions;
22 public  import dazzle.c.types;
23 private import gio.AsyncResultIF;
24 private import gio.Cancellable;
25 private import gio.FileIF;
26 private import glib.ConstructionException;
27 private import glib.ErrorG;
28 private import glib.GException;
29 private import gobject.ObjectG;
30 private import gobject.Signals;
31 private import std.algorithm;
32 
33 
34 /** */
35 public class RecursiveFileMonitor : ObjectG
36 {
37 	/** the main Gtk struct */
38 	protected DzlRecursiveFileMonitor* dzlRecursiveFileMonitor;
39 
40 	/** Get the main Gtk struct */
41 	public DzlRecursiveFileMonitor* getRecursiveFileMonitorStruct(bool transferOwnership = false)
42 	{
43 		if (transferOwnership)
44 			ownedRef = false;
45 		return dzlRecursiveFileMonitor;
46 	}
47 
48 	/** the main Gtk struct as a void* */
49 	protected override void* getStruct()
50 	{
51 		return cast(void*)dzlRecursiveFileMonitor;
52 	}
53 
54 	/**
55 	 * Sets our main struct and passes it to the parent class.
56 	 */
57 	public this (DzlRecursiveFileMonitor* dzlRecursiveFileMonitor, bool ownedRef = false)
58 	{
59 		this.dzlRecursiveFileMonitor = dzlRecursiveFileMonitor;
60 		super(cast(GObject*)dzlRecursiveFileMonitor, ownedRef);
61 	}
62 
63 
64 	/** */
65 	public static GType getType()
66 	{
67 		return dzl_recursive_file_monitor_get_type();
68 	}
69 
70 	/** */
71 	public this(FileIF root)
72 	{
73 		auto p = dzl_recursive_file_monitor_new((root is null) ? null : root.getFileStruct());
74 
75 		if(p is null)
76 		{
77 			throw new ConstructionException("null returned by new");
78 		}
79 
80 		this(cast(DzlRecursiveFileMonitor*) p, true);
81 	}
82 
83 	/**
84 	 * Cancels the recursive file monitor.
85 	 *
86 	 * Since: 3.28
87 	 */
88 	public void cancel()
89 	{
90 		dzl_recursive_file_monitor_cancel(dzlRecursiveFileMonitor);
91 	}
92 
93 	/**
94 	 * Gets the root directory used forthe file monitor.
95 	 *
96 	 * Returns: a #GFile
97 	 *
98 	 * Since: 3.28
99 	 */
100 	public FileIF getRoot()
101 	{
102 		auto p = dzl_recursive_file_monitor_get_root(dzlRecursiveFileMonitor);
103 
104 		if(p is null)
105 		{
106 			return null;
107 		}
108 
109 		return ObjectG.getDObject!(FileIF)(cast(GFile*) p);
110 	}
111 
112 	/**
113 	 * Sets a callback function to determine if a #GFile should be ignored
114 	 * from signal emission.
115 	 *
116 	 * @ignore_func will always be called from the applications main thread.
117 	 *
118 	 * If @ignore_func is %NULL, it is set to the default which does not
119 	 * ignore any files or directories.
120 	 *
121 	 * Params:
122 	 *     ignoreFunc = a #DzlRecursiveIgnoreFunc
123 	 *     ignoreFuncData = closure data for @ignore_func
124 	 *     ignoreFuncDataDestroy = destroy notify for @ignore_func_data
125 	 *
126 	 * Since: 3.28
127 	 */
128 	public void setIgnoreFunc(DzlRecursiveIgnoreFunc ignoreFunc, void* ignoreFuncData, GDestroyNotify ignoreFuncDataDestroy)
129 	{
130 		dzl_recursive_file_monitor_set_ignore_func(dzlRecursiveFileMonitor, ignoreFunc, ignoreFuncData, ignoreFuncDataDestroy);
131 	}
132 
133 	/** */
134 	public void startAsync(Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
135 	{
136 		dzl_recursive_file_monitor_start_async(dzlRecursiveFileMonitor, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
137 	}
138 
139 	/** */
140 	public bool startFinish(AsyncResultIF result)
141 	{
142 		GError* err = null;
143 
144 		auto p = dzl_recursive_file_monitor_start_finish(dzlRecursiveFileMonitor, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
145 
146 		if (err !is null)
147 		{
148 			throw new GException( new ErrorG(err) );
149 		}
150 
151 		return p;
152 	}
153 
154 	/**
155 	 * This event is similar to #GFileMonitor::changed but can be fired from
156 	 * any of the monitored directories in the recursive mount.
157 	 *
158 	 * Params:
159 	 *     file = a #GFile
160 	 *     otherFile = a #GFile for the other file when applicable
161 	 *     event = the #GFileMonitorEvent event
162 	 *
163 	 * Since: 3.28
164 	 */
165 	gulong addOnChanged(void delegate(FileIF, FileIF, GFileMonitorEvent, RecursiveFileMonitor) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
166 	{
167 		return Signals.connect(this, "changed", dlg, connectFlags ^ ConnectFlags.SWAPPED);
168 	}
169 }