[Banshee-List] patch for file system monitoring



Hello everyone,

The attached patch is against the latest cvs. It should fix all the
remaining issues with  a monitoring code.
? watch_megafix_v2.patch
? burn-sharp/.deps
? burn-sharp/.libs
? burn-sharp/glue.lo
? burn-sharp/libnautilusburnglue.la
? libbanshee/.deps
? libbanshee/.libs
? libbanshee/cd-detect.lo
? libbanshee/cd-rip.lo
? libbanshee/gst-encode.lo
? libbanshee/gst-init.lo
? libbanshee/gst-misc.lo
? libbanshee/gst-player-engine.lo
? libbanshee/inotify-glue.lo
? libbanshee/libbanshee.la
? libbanshee/xing/.deps
? po/.intltool-merge-cache
Index: src/Core.cs
===================================================================
RCS file: /cvs/gnome/banshee/src/Core.cs,v
retrieving revision 1.25
diff -p -u -2 -r1.25 Core.cs
--- src/Core.cs	13 Nov 2005 20:13:28 -0000	1.25
+++ src/Core.cs	18 Nov 2005 20:26:07 -0000
@@ -244,4 +244,10 @@ namespace Banshee
             }
         }
+
+        public Watcher FSWatcher {
+            get {
+                return fs_watcher;
+            }
+        }
         
         public static void ProxyToMainThread(EventHandler handler)
Index: src/PlayerInterface.cs
===================================================================
RCS file: /cvs/gnome/banshee/src/PlayerInterface.cs,v
retrieving revision 1.100
diff -p -u -2 -r1.100 PlayerInterface.cs
--- src/PlayerInterface.cs	17 Nov 2005 09:11:02 -0000	1.100
+++ src/PlayerInterface.cs	18 Nov 2005 20:26:22 -0000
@@ -2407,4 +2407,22 @@ namespace Banshee
         }
         
+        public PlaylistModel PlaylistModel {
+            get {
+                return playlistModel;
+            }
+        }
+        
+        public PlaylistView PlaylistView {
+            get {
+                return playlistView;
+            }
+        }
+        
+        public SourceView SourceView {
+            get {
+                return sourceView;
+            }
+        }
+        
         private void ShowPlaylistView()
         {
Index: src/PlaylistModel.cs
===================================================================
RCS file: /cvs/gnome/banshee/src/PlaylistModel.cs,v
retrieving revision 1.21
diff -p -u -2 -r1.21 PlaylistModel.cs
--- src/PlaylistModel.cs	18 Nov 2005 18:56:17 -0000	1.21
+++ src/PlaylistModel.cs	18 Nov 2005 20:26:24 -0000
@@ -442,4 +442,13 @@ namespace Banshee
         }
         
+        public void RemoveTrack(TreeIter iter)
+        {
+            TrackInfo ti = IterTrackInfo(iter);
+            totalDuration -= ti.Duration;
+            ti.TreeIter = TreeIter.Zero;
+            Remove(ref iter);
+            RaiseUpdated(this, new EventArgs());
+        }
+        
         // --- Event Raise Handlers ---
 
Index: src/FileSystemMonitor/FileSystemWatcherWatch.cs
===================================================================
RCS file: /cvs/gnome/banshee/src/FileSystemMonitor/FileSystemWatcherWatch.cs,v
retrieving revision 1.2
diff -p -u -2 -r1.2 FileSystemWatcherWatch.cs
--- src/FileSystemMonitor/FileSystemWatcherWatch.cs	14 Nov 2005 21:55:36 -0000	1.2
+++ src/FileSystemMonitor/FileSystemWatcherWatch.cs	18 Nov 2005 20:26:27 -0000
@@ -3,14 +3,14 @@ using System.Collections;
 using System.IO;
 
-namespace Banshee.FileSystemMonitor
-{
-    public sealed class FileSystemWatcherWatch : Watch
-    {
-        private Hashtable watchMap;
+namespace Banshee.FileSystemMonitor {
+
+    public sealed class FileSystemWatcherWatch : Watch {
+    
+        private Hashtable watch_map;
         private bool verbose = false;
         
         public FileSystemWatcherWatch(ArrayList im, ArrayList rm, string folder) : base(im, rm, folder)
         {
-            watchMap = new Hashtable();
+            watch_map = new Hashtable();
             RecurseDirectory(PathUtil.FileUriStringToPath(musicFolder));
         }
@@ -18,5 +18,5 @@ namespace Banshee.FileSystemMonitor
         public override bool IsWatching(string path)
         {
-            return watchMap.ContainsKey(path);
+            return watch_map.ContainsKey(path);
         }
         
@@ -44,5 +44,5 @@ namespace Banshee.FileSystemMonitor
             watcher.EnableRaisingEvents = true;
             
-            watchMap.Add(path, watcher);
+            watch_map.Add(path, watcher);
             
             return true;
@@ -56,5 +56,5 @@ namespace Banshee.FileSystemMonitor
         {
             lock(this) {
-                UniqueAdd(toImport, e.FullPath);
+                UniqueAdd(to_import, e.FullPath);
             }
         }
@@ -63,5 +63,5 @@ namespace Banshee.FileSystemMonitor
         {
             lock(this) {
-                UniqueAdd(toRemove, e.FullPath);
+                UniqueAdd(to_remove, e.FullPath);
             }
         }
@@ -70,6 +70,6 @@ namespace Banshee.FileSystemMonitor
         {
             lock(this) {
-                UniqueAdd(toImport, e.FullPath);
-                UniqueAdd(toRemove, e.OldFullPath);
+                UniqueAdd(to_import, e.FullPath);
+                UniqueAdd(to_remove, e.OldFullPath);
             }
         }
Index: src/FileSystemMonitor/InotifyWatch.cs
===================================================================
RCS file: /cvs/gnome/banshee/src/FileSystemMonitor/InotifyWatch.cs,v
retrieving revision 1.2
diff -p -u -2 -r1.2 InotifyWatch.cs
--- src/FileSystemMonitor/InotifyWatch.cs	14 Nov 2005 21:55:36 -0000	1.2
+++ src/FileSystemMonitor/InotifyWatch.cs	18 Nov 2005 20:26:28 -0000
@@ -3,8 +3,8 @@ using System.Collections;
 using System.IO;
 
-namespace Banshee.FileSystemMonitor
-{
-    public sealed class InotifyWatch : Watch 
-    {
+namespace Banshee.FileSystemMonitor {
+
+    public sealed class InotifyWatch : Watch  {
+    
         private bool verbose = false;
                  
@@ -32,5 +32,5 @@ namespace Banshee.FileSystemMonitor
             }
                 
-            Console.WriteLine ("Adding watch to {0}", path);
+            //Console.WriteLine ("Adding watch to {0}", path);
             
             Inotify.Subscribe(path, OnInotifyEvent, Inotify.EventType.CloseWrite | 
@@ -49,19 +49,19 @@ namespace Banshee.FileSystemMonitor
             string srcpath, Inotify.EventType type)
         {
-            Console.WriteLine("Got event ({03}) {0}: {1}/{2}", type, path, subitem, srcpath);
+            //Console.WriteLine("Got event ({03}) {0}: {1}/{2}", type, path, subitem, srcpath);
 
-            string fullPath = Path.Combine(path, subitem);
+            string full_path = Path.Combine(path, subitem);
 
             lock(this) {
                 if(HasFlag(type, Inotify.EventType.MovedTo) || HasFlag(type, Inotify.EventType.CloseWrite)) {
-                    UniqueAdd(toImport, fullPath);
+                    UniqueAdd(to_import, full_path);
                         
                     if(srcpath != null) {
-                        UniqueAdd(toRemove, srcpath);
+                        UniqueAdd(to_remove, srcpath);
                     }
                 } else if(HasFlag(type, Inotify.EventType.Create)) { /*HasFlag (type, Inotify.EventType.IsDirectory)) */
-                    UniqueAdd(toImport, fullPath);
+                    UniqueAdd(to_import, full_path);
                 } else if(HasFlag(type, Inotify.EventType.Delete) || HasFlag(type, Inotify.EventType.MovedFrom)) {
-                    UniqueAdd(toRemove, fullPath);
+                    UniqueAdd(to_remove, full_path);
                 }
             }
Index: src/FileSystemMonitor/Watch.cs
===================================================================
RCS file: /cvs/gnome/banshee/src/FileSystemMonitor/Watch.cs,v
retrieving revision 1.1
diff -p -u -2 -r1.1 Watch.cs
--- src/FileSystemMonitor/Watch.cs	13 Nov 2005 20:13:01 -0000	1.1
+++ src/FileSystemMonitor/Watch.cs	18 Nov 2005 20:26:28 -0000
@@ -10,6 +10,8 @@ namespace Banshee.FileSystemMonitor
         protected string musicFolder;
         
-        protected ArrayList toImport;
-        protected ArrayList toRemove;
+        protected ArrayList to_import;
+        protected ArrayList to_remove;
+        
+        protected bool is_hot;
         
         public Watch(ArrayList im, ArrayList rm, string folder)
@@ -17,6 +19,8 @@ namespace Banshee.FileSystemMonitor
             musicFolder = folder;
             
-            toImport = im;
-            toRemove = rm;
+            to_import = im;
+            to_remove = rm;
+            
+            is_hot = false;
         }
         
@@ -44,30 +48,38 @@ namespace Banshee.FileSystemMonitor
         }
                                    
-        protected void UniqueAdd(ArrayList aList, string item)
+        protected void UniqueAdd(ArrayList list, string item)
 		{
             string cur;
             bool added = false;
+            ArrayList remove_from_list = new ArrayList();
             
-            for(int i = 0; i < aList.Count; i++) {
-                cur = aList[i] as string;
+            is_hot = true;
             
-                if(IsSubfolder(item, cur))
+            for(int i = 0; i < list.Count; i++) {
+                cur = list[i] as string;
+            
+                if(IsDescendant(item, cur))
                     return;
                 
-                if(IsSubfolder(cur, item)) {
-                    aList[i] = item;
-                    added = true;
+                if(IsDescendant(cur, item)) {
+                    if(!added) {
+                        list[i] = item;
+                        added = true;
+                    } else {
+                        remove_from_list.Add(cur);
+                    }
                 }
             }
             
+            foreach(object o in remove_from_list)
+                list.Remove(o);
+            
             if(!added)
-                aList.Add(item);
+                list.Add(item);
 		}
 		
-		protected bool IsSubfolder(string f1, string f2)
+		protected bool IsDescendant(string f1, string f2)
 		{
-            CompareInfo comp =  CultureInfo.InvariantCulture.CompareInfo;
-
-            return comp.IsPrefix(f1, f2);
+            return CultureInfo.InvariantCulture.CompareInfo.IsPrefix(f1, f2 + '/');
 		}
 		
@@ -77,4 +89,13 @@ namespace Banshee.FileSystemMonitor
         
         public abstract void Stop();
+        
+        public bool IsHot {
+            get {
+                return is_hot;
+            }
+            set {
+                is_hot = value;
+            }
+        }
     }
 }
Index: src/FileSystemMonitor/Watcher.cs
===================================================================
RCS file: /cvs/gnome/banshee/src/FileSystemMonitor/Watcher.cs,v
retrieving revision 1.1
diff -p -u -2 -r1.1 Watcher.cs
--- src/FileSystemMonitor/Watcher.cs	13 Nov 2005 20:13:01 -0000	1.1
+++ src/FileSystemMonitor/Watcher.cs	18 Nov 2005 20:26:29 -0000
@@ -4,30 +4,34 @@ using System.Data;
 using System.Threading;
 
-namespace Banshee.FileSystemMonitor
-{
-    public class Watcher : IDisposable
-    {
-        private ArrayList toImport;
-        private ArrayList toRemove;
+using Gtk;
+
+namespace Banshee.FileSystemMonitor {
+
+    public class Watcher : IDisposable {
+    
+        private ArrayList to_import;
+        private ArrayList to_remove;
         
-        private Thread updateThread;
+        private Thread update_thread;
         
         private Watch watch;
         
+        private LibraryTransaction transaction;
+        
         public Watcher(string watchDirectory)
         {
-            toImport = new ArrayList();
-            toRemove = new ArrayList();
+            to_import = new ArrayList();
+            to_remove = new ArrayList();
         
-            updateThread = new Thread(new ThreadStart(Update));
+            update_thread = new Thread(new ThreadStart(Update));
         
             if(Inotify.Enabled) {
-                Console.WriteLine("The power of inotify!");
-                watch = new InotifyWatch(toImport, toRemove, watchDirectory);
+                //Console.WriteLine("The power of inotify!");
+                watch = new InotifyWatch(to_import, to_remove, watchDirectory);
             } else {
-                watch = new FileSystemWatcherWatch(toImport, toRemove, watchDirectory);
+                watch = new FileSystemWatcherWatch(to_import, to_remove, watchDirectory);
             }
             
-            updateThread.Start();
+            update_thread.Start();
         }
         
@@ -36,63 +40,114 @@ namespace Banshee.FileSystemMonitor
             while(true) {
     		    lock(watch) {
-                    if(toRemove.Count != 0) {
-                        Console.WriteLine("toRemove begin");
-                        
-                        string query = " FROM TRACKS WHERE";                     
-    		    
-            		    foreach(string s in toRemove) {
-            		        Console.WriteLine(s as string);
-                            query +=" Uri LIKE \"file://" + s + "/%\"";
+                    if(to_remove.Count != 0 && !watch.IsHot) {                 
+
+                        string query = "";                     
+	    
+                        foreach(string s in to_remove) {
+                            query += " Uri LIKE \"file://" + s + "/%\"";
                             query += " OR Uri LIKE \"file://" + s + "\"";
-                            
+                    
                             query += " OR";                            
                         }
+                            
+                        if(query != String.Empty) {
+                            query = query.Substring(0, query.Length - 3);
+                
+                            transaction = new LibraryTrackRemoveTransaction();
+                                            
+                            string select_query = "SELECT TrackId FROM Tracks WHERE" + query;
+
+                            IDataReader reader = Core.Library.Db.Query(select_query);
+                                                                                    
+                            while(reader.Read()) {
+                                TrackInfo ti = 
+                                        Core.Library.Tracks[Convert.ToInt32(reader[0])] as TrackInfo;
+                                (transaction as LibraryTrackRemoveTransaction).RemoveQueue.Add(ti);
+                            }
+                            
+                            Core.ProxyToMainThread( delegate 
+                            {
+                                TrackRemoveTransaction transaction = 
+                                    Core.Instance.FSWatcher.Transaction as TrackRemoveTransaction;
+                                
+                                PlaylistModel pmodel = Core.Instance.PlayerInterface.PlaylistModel;
+                                                                                              
+                                foreach(TrackInfo ti in transaction.RemoveQueue) {
+                                    /*for(int i = 0, n = pmodel.Count(); i < n; i++) {
+                                        TreeIter iter;
+                                        if(!pmodel.IterNthChild(out iter, i))
+                                            continue;
+                        
+                                        TrackInfo pti = pmodel.IterTrackInfo(iter);
+                                        
+                                        if(pti.Uid == ti.Uid) {
+                                            pmodel.Remove(ref iter);
+                                            break;
+                                        }
+                                    }*/
+                                    pmodel.RemoveTrack(ti.TreeIter);
+                                }
+                                 
+                                transaction.Finished += OnLibraryTrackRemoveFinished;   
+                                transaction.Register();
+                            });
+                        }
                         
-                        Console.WriteLine("toRemove end");
-                        
-                        query = query.Substring(0, query.Length - 3);
-                        
-                        string selectQuery = "SELECT TrackID, Uri" + query;
-                        string deleteQuery = "DELETE" + query;
-                                               
-                        IDataReader reader = Core.Library.Db.Query(selectQuery);
-                        while(reader.Read()) {
-                            Core.Library.Remove(Convert.ToInt32(reader[0] as string), 
-                                                new System.Uri(reader[1] as string));
-                        }                       
-                        Core.Library.Db.Execute(deleteQuery);
-                      
-                        toRemove.Clear();
+                    to_remove.Clear();
                     }
-                    
-                    if(toImport.Count != 0) {
-                        FileLoadTransaction transaction = 
-                            new FileLoadTransaction(null, true, true);
+                                        
+                    if(to_import.Count != 0 && !watch.IsHot) {
+                        transaction = new FileLoadTransaction(null, true, true);
                             
-                        Console.WriteLine("toImport begin");
-
-              		    foreach(string s in toImport) {
-                            Console.WriteLine(s);
-                            transaction.AddPath(s);
+              		    foreach(string s in to_import) {
+                            (transaction as FileLoadTransaction).AddPath(s);
                             watch.RecurseDirectory(s);
                         }
                         
-                        Console.WriteLine("toImport end");
-                        
-                        transaction.Register();
+                        Core.ProxyToMainThread( delegate
+                        {
+                            FileLoadTransaction transaction = 
+                                Core.Instance.FSWatcher.Transaction as FileLoadTransaction;
+                            
+                            transaction.HaveTrackInfo += 
+                                Core.Instance.FSWatcher.OnLoaderHaveTrackInfo;
+
+                            transaction.Register();
+                        });
                         
-                        toImport.Clear();
+                    to_import.Clear();
+                    }
+                    
+                    if(watch.IsHot) {
+                        watch.IsHot = false;
                     }
                 }
                 
-                Thread.Sleep(5000);
+                Thread.Sleep(2500);
 		    }
 		}
+
+        public void OnLoaderHaveTrackInfo(object o, HaveTrackInfoArgs args)
+        {                
+            Core.Instance.PlayerInterface.PlaylistModel.QueueAddTrack(args.TrackInfo);
+        }
+
+        public void OnLibraryTrackRemoveFinished(object o, EventArgs args)
+        {
+            Core.Instance.PlayerInterface.PlaylistView.QueueDraw();
+            Core.Instance.PlayerInterface.SourceView.QueueDraw();
+        }
 		
 		public void Dispose()
 		{
-            updateThread.Abort();
+            update_thread.Abort();
             watch.Stop();
 		}
+        
+        public LibraryTransaction Transaction {
+            get {
+                return transaction;
+            }
+        }
     }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]