nautilus integration: "share" extension for syncing folders
authorGilbert Röhrbein <gilbert@ifsr.de>
Sat, 22 Oct 2016 21:20:27 +0000 (23:20 +0200)
committerGilbert Röhrbein <gilbert@ifsr.de>
Sat, 22 Oct 2016 21:26:44 +0000 (23:26 +0200)
Issue #4608

* folder state is either OK or SYNC usually
* this extension does not know about if a folder is shareable
* a OK folder is shareable
* a SYNC folder could be uploaded or not uploaded
* this commit looks into file entries below this folder
* if one exists with state OK or SYNC
   this folder must have been uploaded
* better would be if the server (gui) tells us
   if the folder is uploaded

shell_integration/nautilus/syncstate.py

index f4e7908363c4ad396671a2be7084a7aa87e6f0a6..3979e86ce1ae01d35759e5a908bc879c90a53f86 100644 (file)
@@ -157,32 +157,65 @@ class MenuExtension(GObject.GObject, Nautilus.MenuProvider):
     def __init__(self):
         GObject.GObject.__init__(self)
 
+    def check_registered_paths(self, filename):
+        topLevelFolder = False
+        internalFile = False
+        for reg_path in socketConnect.registered_paths:
+            if filename == reg_path:
+                topLevelFolder = True
+                break
+            if filename.startswith(reg_path):
+                internalFile = True
+                # you can't have a registered path below another so it is save to break here
+                break
+        return (topLevelFolder, internalFile)
+
     def get_file_items(self, window, files):
+        # Show the menu extension to share a file or folder
+        #
+        # Show if file is OK.
+        # Ignore top level folders.
+        # Also show extension for folders
+        #  if there is a OK or SYNC underneath.
+        # This is only
+
         if len(files) != 1:
             return
         file = files[0]
         items = []
 
-        # Internal or external file?!
-        syncedFile = False
-        for reg_path in socketConnect.registered_paths:
-            topLevelFolder = False
-            filename = get_local_path(file.get_uri())
-            # Check if its a folder (ends with an /), if yes add a "/"
-            # otherwise it will not find the entry in the table
-            if os.path.isdir(filename + "/"):
-                filename += "/"
-                # Check if toplevel folder, we need to ignore those as they cannot be shared
-                if filename == reg_path:
-                    topLevelFolder=True                
-            # Only show the menu extension if the file is synced and the sync
-            # status is ok. Not for ignored files etc.
-            # ignore top level folders
-            if filename.startswith(reg_path) and topLevelFolder == False and socketConnect.nautilusVFSFile_table[filename]['state'].startswith('OK'):
-                syncedFile = True
-
-        # If it is neither in a synced folder or is a directory
-        if not syncedFile:
+        filename = get_local_path(file.get_uri())
+        # Check if its a folder (ends with an /), if yes add a "/"
+        # otherwise it will not find the entry in the table
+        isDir = os.path.isdir(filename + "/")
+        if isDir:
+            filename += "/"
+
+        # Check if toplevel folder, we need to ignore those as they cannot be shared
+        topLevelFolder, internalFile = self.check_registered_paths(filename)
+        if topLevelFolder or not internalFile:
+            return items
+
+        entry = socketConnect.nautilusVFSFile_table.get(filename)
+        if not entry:
+            return items
+
+        shareable = False
+        state = entry['state']
+        state_ok = state.startswith('OK')
+        state_sync = state.startswith('SYNC')
+        if state_ok:
+            shareable = True
+        elif state_sync and isDir:
+            # some file below is OK or SYNC
+            for key, value in socketConnect.nautilusVFSFile_table.items():
+                if key != filename and key.startswith(filename):
+                    state = value['state']
+                    if state.startswith('OK') or state.startswith('SYNC'):
+                        shareable = True
+                        break
+
+        if not shareable:
             return items
 
         # Create a menu item