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.FileMonitor; 26 27 private import gio.FileIF; 28 private import gio.c.functions; 29 public import gio.c.types; 30 private import gobject.ObjectG; 31 private import gobject.Signals; 32 public import gtkc.giotypes; 33 private import std.algorithm; 34 35 36 /** 37 * Monitors a file or directory for changes. 38 * 39 * To obtain a #GFileMonitor for a file or directory, use 40 * g_file_monitor(), g_file_monitor_file(), or 41 * g_file_monitor_directory(). 42 * 43 * To get informed about changes to the file or directory you are 44 * monitoring, connect to the #GFileMonitor::changed signal. The 45 * signal will be emitted in the 46 * [thread-default main context][g-main-context-push-thread-default] 47 * of the thread that the monitor was created in 48 * (though if the global default main context is blocked, this may 49 * cause notifications to be blocked even if the thread-default 50 * context is still running). 51 */ 52 public class FileMonitor : ObjectG 53 { 54 /** the main Gtk struct */ 55 protected GFileMonitor* gFileMonitor; 56 57 /** Get the main Gtk struct */ 58 public GFileMonitor* getFileMonitorStruct(bool transferOwnership = false) 59 { 60 if (transferOwnership) 61 ownedRef = false; 62 return gFileMonitor; 63 } 64 65 /** the main Gtk struct as a void* */ 66 protected override void* getStruct() 67 { 68 return cast(void*)gFileMonitor; 69 } 70 71 /** 72 * Sets our main struct and passes it to the parent class. 73 */ 74 public this (GFileMonitor* gFileMonitor, bool ownedRef = false) 75 { 76 this.gFileMonitor = gFileMonitor; 77 super(cast(GObject*)gFileMonitor, ownedRef); 78 } 79 80 81 /** */ 82 public static GType getType() 83 { 84 return g_file_monitor_get_type(); 85 } 86 87 /** 88 * Cancels a file monitor. 89 * 90 * Returns: always %TRUE 91 */ 92 public bool cancel() 93 { 94 return g_file_monitor_cancel(gFileMonitor) != 0; 95 } 96 97 /** 98 * Emits the #GFileMonitor::changed signal if a change 99 * has taken place. Should be called from file monitor 100 * implementations only. 101 * 102 * Implementations are responsible to call this method from the 103 * [thread-default main context][g-main-context-push-thread-default] of the 104 * thread that the monitor was created in. 105 * 106 * Params: 107 * child = a #GFile. 108 * otherFile = a #GFile. 109 * eventType = a set of #GFileMonitorEvent flags. 110 */ 111 public void emitEvent(FileIF child, FileIF otherFile, GFileMonitorEvent eventType) 112 { 113 g_file_monitor_emit_event(gFileMonitor, (child is null) ? null : child.getFileStruct(), (otherFile is null) ? null : otherFile.getFileStruct(), eventType); 114 } 115 116 /** 117 * Returns whether the monitor is canceled. 118 * 119 * Returns: %TRUE if monitor is canceled. %FALSE otherwise. 120 */ 121 public bool isCancelled() 122 { 123 return g_file_monitor_is_cancelled(gFileMonitor) != 0; 124 } 125 126 /** 127 * Sets the rate limit to which the @monitor will report 128 * consecutive change events to the same file. 129 * 130 * Params: 131 * limitMsecs = a non-negative integer with the limit in milliseconds 132 * to poll for changes 133 */ 134 public void setRateLimit(int limitMsecs) 135 { 136 g_file_monitor_set_rate_limit(gFileMonitor, limitMsecs); 137 } 138 139 protected class OnChangedDelegateWrapper 140 { 141 void delegate(FileIF, FileIF, GFileMonitorEvent, FileMonitor) dlg; 142 gulong handlerId; 143 144 this(void delegate(FileIF, FileIF, GFileMonitorEvent, FileMonitor) dlg) 145 { 146 this.dlg = dlg; 147 onChangedListeners ~= this; 148 } 149 150 void remove(OnChangedDelegateWrapper source) 151 { 152 foreach(index, wrapper; onChangedListeners) 153 { 154 if (wrapper.handlerId == source.handlerId) 155 { 156 onChangedListeners[index] = null; 157 onChangedListeners = std.algorithm.remove(onChangedListeners, index); 158 break; 159 } 160 } 161 } 162 } 163 OnChangedDelegateWrapper[] onChangedListeners; 164 165 /** 166 * Emitted when @file has been changed. 167 * 168 * If using %G_FILE_MONITOR_WATCH_MOVES on a directory monitor, and 169 * the information is available (and if supported by the backend), 170 * @event_type may be %G_FILE_MONITOR_EVENT_RENAMED, 171 * %G_FILE_MONITOR_EVENT_MOVED_IN or %G_FILE_MONITOR_EVENT_MOVED_OUT. 172 * 173 * In all cases @file will be a child of the monitored directory. For 174 * renames, @file will be the old name and @other_file is the new 175 * name. For "moved in" events, @file is the name of the file that 176 * appeared and @other_file is the old name that it was moved from (in 177 * another directory). For "moved out" events, @file is the name of 178 * the file that used to be in this directory and @other_file is the 179 * name of the file at its new location. 180 * 181 * It makes sense to treat %G_FILE_MONITOR_EVENT_MOVED_IN as 182 * equivalent to %G_FILE_MONITOR_EVENT_CREATED and 183 * %G_FILE_MONITOR_EVENT_MOVED_OUT as equivalent to 184 * %G_FILE_MONITOR_EVENT_DELETED, with extra information. 185 * %G_FILE_MONITOR_EVENT_RENAMED is equivalent to a delete/create 186 * pair. This is exactly how the events will be reported in the case 187 * that the %G_FILE_MONITOR_WATCH_MOVES flag is not in use. 188 * 189 * If using the deprecated flag %G_FILE_MONITOR_SEND_MOVED flag and @event_type is 190 * #G_FILE_MONITOR_EVENT_MOVED, @file will be set to a #GFile containing the 191 * old path, and @other_file will be set to a #GFile containing the new path. 192 * 193 * In all the other cases, @other_file will be set to #NULL. 194 * 195 * Params: 196 * file = a #GFile. 197 * otherFile = a #GFile or #NULL. 198 * eventType = a #GFileMonitorEvent. 199 */ 200 gulong addOnChanged(void delegate(FileIF, FileIF, GFileMonitorEvent, FileMonitor) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 201 { 202 auto wrapper = new OnChangedDelegateWrapper(dlg); 203 wrapper.handlerId = Signals.connectData( 204 this, 205 "changed", 206 cast(GCallback)&callBackChanged, 207 cast(void*)wrapper, 208 cast(GClosureNotify)&callBackChangedDestroy, 209 connectFlags); 210 return wrapper.handlerId; 211 } 212 213 extern(C) static void callBackChanged(GFileMonitor* filemonitorStruct, GFile* file, GFile* otherFile, GFileMonitorEvent eventType, OnChangedDelegateWrapper wrapper) 214 { 215 wrapper.dlg(ObjectG.getDObject!(FileIF)(file), ObjectG.getDObject!(FileIF)(otherFile), eventType, wrapper.outer); 216 } 217 218 extern(C) static void callBackChangedDestroy(OnChangedDelegateWrapper wrapper, GClosure* closure) 219 { 220 wrapper.remove(wrapper); 221 } 222 }