copying the latest Sys.Web.Services from trunk.
[mono.git] / mcs / class / System / System.IO / FileSystemWatcher.cs
index 2dba82cd9e6e0de027c36f8eef029c5aeb05d39b..e4018da121066702c4b881f7e59770a51b775630 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
+using System;
 using System.ComponentModel;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
-using System.Security.Permissions;
 using System.Threading;
 
 namespace System.IO {
@@ -57,7 +57,6 @@ namespace System.IO {
                bool disposed;
                string mangledFilter;
                static IFileWatcher watcher;
-               static object lockobj = new object ();
 
                #endregion // Fields
 
@@ -103,10 +102,9 @@ namespace System.IO {
                        InitWatcher ();
                }
 
-               [EnvironmentPermission (SecurityAction.Assert, Read="MONO_MANAGED_WATCHER")]
                void InitWatcher ()
                {
-                       lock (lockobj) {
+                       lock (typeof (FileSystemWatcher)) {
                                if (watcher != null)
                                        return;
 
@@ -116,24 +114,13 @@ namespace System.IO {
                                        mode = InternalSupportsFSW ();
 
                                bool ok = false;
-                               switch (mode) {
-                               case 1: // windows
+                               if (mode == 3)
+                                       ok = KeventWatcher.GetInstance (out watcher);
+                               else if (mode == 2)
+                                       ok = FAMWatcher.GetInstance (out watcher);
+                               else if (mode == 1)
                                        ok = DefaultWatcher.GetInstance (out watcher);
                                        //ok = WindowsWatcher.GetInstance (out watcher);
-                                       break;
-                               case 2: // libfam
-                                       ok = FAMWatcher.GetInstance (out watcher, false);
-                                       break;
-                               case 3: // kevent
-                                       ok = KeventWatcher.GetInstance (out watcher);
-                                       break;
-                               case 4: // libgamin
-                                       ok = FAMWatcher.GetInstance (out watcher, true);
-                                       break;
-                               case 5: // inotify
-                                       ok = InotifyWatcher.GetInstance (out watcher, true);
-                                       break;
-                               }
 
                                if (mode == 0 || !ok)
                                        DefaultWatcher.GetInstance (out watcher);
@@ -354,60 +341,44 @@ namespace System.IO {
                        throw new NotImplementedException (); 
                }
 
-               enum EventType {
-                       FileSystemEvent,
-                       ErrorEvent,
-                       RenameEvent
-               }
-               private void RaiseEvent (Delegate ev, EventArgs arg, EventType evtype)
+               private void RaiseEvent (Delegate ev, EventArgs arg)
                {
                        if (ev == null)
                                return;
 
+                       object [] args = new object [] {this, arg};
+
                        if (synchronizingObject == null) {
-                               Delegate [] delegates = ev.GetInvocationList ();
-                               if (evtype == EventType.RenameEvent) {
-                                       foreach (RenamedEventHandler d in delegates){
-                                               d.BeginInvoke (this, (RenamedEventArgs) arg, null, null);
-                                       }
-                               } else if (evtype == EventType.ErrorEvent) {
-                                       foreach (ErrorEventHandler d in delegates){
-                                               d.BeginInvoke (this, (ErrorEventArgs) arg, null, null);
-                                       }
-                               } else {
-                                       foreach (FileSystemEventHandler d in delegates){
-                                               d.BeginInvoke (this, (FileSystemEventArgs) arg, null, null);
-                                       }
-                               }
+                               ev.DynamicInvoke (args);
                                return;
                        }
                        
-                       synchronizingObject.BeginInvoke (ev, new object [] {this, arg});
+                       synchronizingObject.BeginInvoke (ev, args);
                }
 
                protected void OnChanged (FileSystemEventArgs e)
                {
-                       RaiseEvent (Changed, e, EventType.FileSystemEvent);
+                       RaiseEvent (Changed, e);
                }
 
                protected void OnCreated (FileSystemEventArgs e)
                {
-                       RaiseEvent (Created, e, EventType.FileSystemEvent);
+                       RaiseEvent (Created, e);
                }
 
                protected void OnDeleted (FileSystemEventArgs e)
                {
-                       RaiseEvent (Deleted, e, EventType.FileSystemEvent);
+                       RaiseEvent (Deleted, e);
                }
 
                protected void OnError (ErrorEventArgs e)
                {
-                       RaiseEvent (Error, e, EventType.ErrorEvent);
+                       RaiseEvent (Error, e);
                }
 
                protected void OnRenamed (RenamedEventArgs e)
                {
-                       RaiseEvent (Renamed, e, EventType.RenameEvent);
+                       RaiseEvent (Renamed, e);
                }
 
                public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType)
@@ -465,13 +436,15 @@ namespace System.IO {
                                }
                                lastData.OldName = filename;
                                lastData.ChangeType = WatcherChangeTypes.Renamed;
-                               renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, filename, "");
+                               renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, null, filename);
                                break;
                        case FileAction.RenamedNewName:
                                lastData.Name = filename;
                                lastData.ChangeType = WatcherChangeTypes.Renamed;
-                               if (renamed == null) {
-                                       renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, "", filename);
+                               if (renamed != null) {
+                                       renamed.SetName (filename);
+                               } else {
+                                       renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, filename, null);
                                }
                                OnRenamed (renamed);
                                renamed = null;
@@ -514,11 +487,23 @@ namespace System.IO {
                /* 0 -> not supported   */
                /* 1 -> windows         */
                /* 2 -> FAM             */
-               /* 3 -> Kevent          */
-               /* 4 -> gamin           */
-               /* 5 -> inotify         */
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                static extern int InternalSupportsFSW ();
+               
+               /*[MethodImplAttribute(MethodImplOptions.InternalCall)]
+               static extern IntPtr InternalOpenDirectory (string path, IntPtr reserved);
+
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               static extern IntPtr InternalCloseDirectory (IntPtr handle);
+
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               static extern bool InternalReadDirectoryChanges (IntPtr handle,
+                                                                byte [] buffer,
+                                                                bool includeSubdirectories,
+                                                                NotifyFilters notifyFilter,
+                                                                out NativeOverlapped overlap,
+                                                                OverlappedHandler overlappedCallback);
+
+               */
        }
 }
-