Commits

Anonymous committed 7443b0b

Fix refresh after rename

Comments (0)

Files changed (3)

plugins/filebrowser/filebrowser/browser.py

         @param item: TreeItem
 
         """
-        busy = wx.BusyCursor() # can take a few seconds
+        busy = wx.BusyCursor() # can take a few seconds on big directories
 
         d = None
         try:
             return False
         return True
 
-    @refreshAfter
     def DoEndEdit(self, item, newlabel):
         """Handle after a user has made changes to a label"""
+        editOk = False
         path = self.GetPyData(item)
         # TODO: check access rights and validate input
         if path:
             newpath = os.path.join(os.path.dirname(path), newlabel)
             try:
+                dobj = None
+                parent = self.GetItemParent(item)
+                if parent.IsOk():
+                    dpath = self.GetPyData(parent)
+                    dobj = ebmlib.GetDirectoryObject(dpath, False, True)
                 os.rename(path, newpath)
-                self.SetPyData(item, newpath) # update stored data
+                editOk = True
+                if dobj:
+                    self.RefreshView([dobj,])
             except OSError:
-                return False # TODO: notify user
-            return True
-        return False
+                editOk = False # TODO: notify user of error
+        return editOk
 
     def DoShowMenu(self, item):
         """Show context menu"""
                     break
 
         # Add any new file objects to the view
+        pathCache = dict()
         needsort = list()
         for fobj in added:
             # apply filters to any new files
             for item in nodes:
                 path = self.GetPyData(item)
                 if path == dpath:
+                    # prevent duplicates from being added
+                    if path not in pathCache:
+                        pathCache[path] = self.GetNodePaths(item)
+                        if fobj.Path in pathCache[path]:
+                            continue
+
                     self.AppendFileNode(item, fobj.Path)
                     if item not in needsort:
                         needsort.append(item)
                     break
 
-        # Resort display
+        # Re-sort display
         for item in needsort:
             self.SortChildren(item)
 
         if self._cpath:
             self.SelectFile(self._cpath)
 
-    def RefreshView(self):
+    def RefreshView(self, paths=None):
         """Refresh file view of monitored directories"""
-        self._monitor.Refresh()
+        self._monitor.Refresh(paths)
 
     def GetMainWindow(self):
         """Get the main window, needed by L{ed_msg.mwcontext}"""

src/ebmlib/_dirmon.py

         else:
             self._watcher.Continue()
 
-    def Refresh(self):
+    def Refresh(self, paths=None):
         """Force a recheck of the monitored directories. This method
         is useful for doing manual control of the refresh cycle. It is
         ignored and does nothing when WatcherThread is set up for automatic
         refresh cycles.
+        @keyword paths: specific paths to refresh or None for all.
 
         """
-        self._watcher.Refresh()
+        self._watcher.Refresh(paths)
 
 #-----------------------------------------------------------------------------#
     
         assert callable(notifier)
         self._notifier = notifier
         self._dirs = list() # Directories being monitored
+        self._refreshDirs = None
 
         self._freq = checkFreq # Monitoring frequency in milliseconds
         self._continue = True
                     self._suspendcond.wait()
 
             with self._lock:
-                for dobj in self._dirs:
+                for dobj in self._PendingRefresh:
                     if not self._continue:
                         return
                     elif self._changePending:
                     # Check for deletions
                     dobjFiles = dobj.Files # optimization
                     dobjIndex = dobjFiles.index # optimization
-                    snapFiles = snapshot.Files
+                    snapFiles = snapshot.Files # optimization
                     for tobj in dobjFiles:
                         if not self._continue:
                             return
             else:
                 # Manually controlled updates
                 with self._refreshCond:
+                    self._refreshDirs = None
                     self._refreshCond.wait()
 
     #---- Implementation ----#
 
+    @property
+    def _PendingRefresh(self):
+        """Get the list of directories pending refresh"""
+        if self._refreshDirs is None:
+            return self._dirs
+        else:
+            return self._refreshDirs
+
     def AddWatchDirectory(self, dpath):
         """Add a directory to the watch list
         @param dpath: directory path (unicode)
         """
         self._freq = float(milli)
 
-    def Refresh(self):
+    def Refresh(self, paths=None):
         """Recheck the monitored directories
         only useful when manually controlling refresh cycle of the monitor.
+        @keyword paths: if None refresh all, else list of specific directories
 
         """
         with self._refreshCond:
+            if paths is not None:
+                self._refreshDirs = list()
+                for dobj in paths:
+                    self._refreshDirs.append(dobj)
             self._refreshCond.notify()
 
     def Shutdown(self):

src/eclib/_filetree.py

             pass
         return files
 
+    def GetNodePaths(self, dirNode):
+        """Get a list of paths contained below the given
+        directory node.
+        @param dirNode: wx.TreeItemId
+        @return: list of paths
+
+        """
+        paths = list()
+        if self.ItemHasChildren(dirNode):
+            append = paths.append
+            getData = self.GetPyData
+            for node in self.GetChildNodes(dirNode):
+                try:
+                    append(getData(node))
+                except wx.PyAssertionError:
+                    pass
+        return paths
+
+    def SortParentDirectory(self, item):
+        """Sort the parent directory of the given item"""
+        parent = self.GetItemParent(item)
+        if parent.IsOk():
+            self.SortChildren(parent)
+
 #-----------------------------------------------------------------------------#
 # Test
 if __name__ == '__main__':