From f1937cd3d6ded60a0fe71a0b9cf2422affe38dbf Mon Sep 17 00:00:00 2001
From: Patrick Simpson 
Date: Wed, 20 Sep 2017 14:56:54 +0300
Subject: [PATCH] [KOE-142] Now suppressing moving of any shared folder or into
 a shared folder. This prevents deletion of folders moved into shared folders.
 Also added the SuppressHierarchyChanges property to enable/disable it.
---
 .../SharedFolders/FeatureSharedFolders.cs     | 52 +++++++++++++------
 .../ZPush/FolderRegistration.cs               | 19 +++++++
 2 files changed, 56 insertions(+), 15 deletions(-)
diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/FeatureSharedFolders.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/FeatureSharedFolders.cs
index 4970a03..3d2bd9a 100644
--- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/FeatureSharedFolders.cs
+++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/FeatureSharedFolders.cs
@@ -314,12 +314,32 @@ namespace Acacia.Features.SharedFolders
             }
         }
 
+        [AcaciaOption("If enabled, modifications to the local hierarchy of shared folders is suppressed. " +
+                      "This should only be disabled for debug purposes.")]
+        public bool SuppressHierarchyChanges
+        {
+            get { return GetOption(OPTION_SUPPRESS_HIERARCHY_CHANGES); }
+            set { SetOption(OPTION_SUPPRESS_HIERARCHY_CHANGES, value); }
+        }
+        private static readonly BoolOption OPTION_SUPPRESS_HIERARCHY_CHANGES =
+                new BoolOption("SuppressHierarchyChanges", true);
+
         private void SetupHierarchyChangeSuppression()
         {
-            Watcher.WatchFolder(new SharedFolderRegistration(this), 
-                    OnSharedFolderDiscovered, 
-                    OnSharedFolderChanged,
-                    OnSharedFolderRemoved);
+            if (SuppressHierarchyChanges)
+            {
+                Watcher.WatchFolder(new SharedFolderRegistration(this),
+                        OnSharedFolderDiscovered,
+                        OnSharedFolderChanged,
+                        OnSharedFolderRemoved);
+
+                // Register for any folder move
+                Watcher.WatchFolder(new FolderRegistrationAny(this),
+                        (folder) =>
+                            {
+                                folder.BeforeFolderMove += Folder_BeforeFolderMove;
+                            });
+            }
         }
 
         private void OnSharedFolderDiscovered(IFolder folder)
@@ -340,8 +360,6 @@ namespace Acacia.Features.SharedFolders
             }
             else
             {
-                folder.BeforeFolderMove += Folder_BeforeFolderMove;
-
                 // Check if it was renamed before the events were fully set up
                 CheckSharedFolderRename(folder);
             }
@@ -349,15 +367,19 @@ namespace Acacia.Features.SharedFolders
 
         private void Folder_BeforeFolderMove(IFolder src, IFolder moveTo, ref bool cancel)
         {
-            Logger.Instance.Fatal(this, "SHARED FOLDER MOVE: {0}", moveTo.Name);
-            
-            MessageBox.Show(ThisAddIn.Instance.Window,
-                            Properties.Resources.SharedFolders_LocalFolder_Body,
-                            Properties.Resources.SharedFolders_LocalFolder_Title,
-                            MessageBoxButtons.OK,
-                            MessageBoxIcon.Warning
-                        );
-            cancel = true;
+            if (src.SyncId?.IsShared == true || moveTo.SyncId?.IsShared == true)
+            {
+                // Suppress any move of or into a shared folder
+                Logger.Instance.Warning(this, "Shared folder move: {0} - {1}", src.Name, moveTo.Name);
+
+                MessageBox.Show(ThisAddIn.Instance.Window,
+                                Properties.Resources.SharedFolders_LocalFolder_Body,
+                                Properties.Resources.SharedFolders_LocalFolder_Title,
+                                MessageBoxButtons.OK,
+                                MessageBoxIcon.Warning
+                            );
+                cancel = true;
+            }
         }
 
         private void OnSharedFolderChanged(IFolder folder)
diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/FolderRegistration.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/FolderRegistration.cs
index 3d5f2b2..fe8c32e 100644
--- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/FolderRegistration.cs
+++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/FolderRegistration.cs
@@ -36,6 +36,25 @@ namespace Acacia.ZPush
         abstract public bool IsApplicable(IFolder folder);
     }
 
+    public class FolderRegistrationAny : FolderRegistration
+    {
+        public FolderRegistrationAny(Feature feature)
+        :
+        base(feature)
+        {
+        }
+
+        public override bool IsApplicable(IFolder folder)
+        {
+            return folder != null;
+        }
+
+        public override string ToString()
+        {
+            return Feature.Name;
+        }
+    }
+
     public class FolderRegistrationTyped : FolderRegistration
     {
         private readonly ItemType _itemType;