Fixed blocking issue
[pithos-ms-client] / trunk / Pithos.Client.WPF / SelectiveSynch / SelectiveSynchViewModel.cs
index 1665017..05eaec8 100644 (file)
@@ -86,7 +86,7 @@ namespace Pithos.Client.WPF.SelectiveSynch
             NotifyOfPropertyChange(() => Checks);\r
         }\r
 \r
-        public SelectiveSynchViewModel(/*PithosMonitor monitor,*/ IEventAggregator events, AccountSettings account, string apiKey)\r
+        public SelectiveSynchViewModel(/*PithosMonitor monitor,*/ IEventAggregator events, AccountSettings account, string apiKey,bool forSelectiveActivation)\r
         {\r
             Account = account;\r
             AccountName = account.AccountName;\r
@@ -94,26 +94,27 @@ namespace Pithos.Client.WPF.SelectiveSynch
             //_monitor = monitor;\r
             _events = events;\r
             _apiKey = apiKey;\r
-            IsEnabled = account.SelectiveSyncEnabled;\r
-            TaskEx.Run(LoadRootNode);\r
+            //IsEnabled = account.SelectiveSyncEnabled;            \r
+            TaskEx.Run(()=>LoadRootNode(forSelectiveActivation));\r
         }\r
 \r
-        private void LoadRootNode()\r
+        private async void LoadRootNode(bool forSelectiveActivation)\r
         {            \r
             //TODO: Check this\r
             var client = new CloudFilesClient(AccountName,_apiKey){AuthenticationUrl=Account.ServerUrl,UsePithos=true};\r
-            client.Authenticate();\r
+            await client.Authenticate().ConfigureAwait(false);\r
             \r
             //NEED to get the local folders here as well,\r
             // and combine them with the cloud folders\r
 \r
 \r
-            var dirs = from container in client.ListContainers(AccountName)  \r
-                       where container.Name != "trash"\r
+            var containerInfos = await client.ListContainers(AccountName).ConfigureAwait(false);\r
+            var dirs = from container in containerInfos  \r
+                       where container.Name.ToUnescapedString() != "trash"\r
                        select new DirectoryRecord\r
                                   {\r
-                                      DisplayName = container.Name,\r
-                                      Uri=new Uri(client.StorageUrl,String.Format(@"{0}/{1}",Account.AccountName, container.Name)),\r
+                                      DisplayName = container.Name.ToUnescapedString(),\r
+                                      Uri=client.StorageUrl.Combine(String.Format(@"{0}/{1}",Account.AccountName, container.Name)),\r
                                       Directories = (from dir in client.ListObjects(AccountName, container.Name)                                                                                                          \r
                                                      select dir).ToTree()\r
                                   };\r
@@ -125,12 +126,12 @@ namespace Pithos.Client.WPF.SelectiveSynch
                              select new DirectoryRecord\r
                              {\r
                                 DisplayName=account.name,\r
-                                Uri=new Uri(client.StorageUrl,"../"+ account.name),\r
-                                Directories=(from container in client.ListContainers(account.name)\r
+                                Uri=client.StorageUrl.Combine("../"+ account.name),\r
+                                Directories=(from container in client.ListContainers(account.name).Result\r
                                             select new DirectoryRecord\r
                                                         {\r
-                                                            DisplayName=container.Name,\r
-                                                            Uri = new Uri(client.StorageUrl, "../" + account.name + "/" + container.Name),\r
+                                                            DisplayName=container.Name.ToUnescapedString(),\r
+                                                            Uri = client.StorageUrl.Combine("../" + account.name + "/" + container.Name),\r
                                                             Directories=(from folder in client.ListObjects(account.name,container.Name)                                                                        \r
                                                                         select folder).ToTree()\r
                                                         }).ToList()\r
@@ -138,7 +139,7 @@ namespace Pithos.Client.WPF.SelectiveSynch
 \r
             var othersNode = new DirectoryRecord\r
                                  {\r
-                                     DisplayName = "Shared to me",\r
+                                     DisplayName = "Shared with me",\r
                                      Directories=accountNodes.ToList()\r
                                  };\r
 \r
@@ -148,6 +149,11 @@ namespace Pithos.Client.WPF.SelectiveSynch
                                    DisplayName = AccountName ,\r
                                    Directories = ownFolders.ToList()\r
                                };\r
+\r
+            // Add Local Folders\r
+            //var localFolders = SelectiveExtensions.LocalFolders(AccountName, Account.RootPath,client.StorageUrl.AbsoluteUri);\r
+            //AppendToTree(localFolders, rootItem);\r
+\r
             \r
             //For each local folder that doesn't exist in the server nodes \r
             //find the best matching parent and add the folder below it.\r
@@ -158,7 +164,7 @@ namespace Pithos.Client.WPF.SelectiveSynch
                                        RootNodes.Add(othersNode);\r
                                    });\r
 \r
-            SetInitialSelections(Account);\r
+            SetInitialSelections(Account,forSelectiveActivation);\r
             \r
             IsBusy = false;\r
         }\r
@@ -206,7 +212,7 @@ namespace Pithos.Client.WPF.SelectiveSynch
             }\r
         }\r
 \r
-        private void SetInitialSelections(AccountSettings account)\r
+        private void SetInitialSelections(AccountSettings account,bool forSelectiveActivation)\r
         {\r
             var selections = account.SelectiveFolders;\r
 \r
@@ -234,12 +240,24 @@ namespace Pithos.Client.WPF.SelectiveSynch
             //var shouldBeChecked = allNodes.Except(selects).ToList();\r
             \r
             //WARNING: Using IsChecked marks the item as ADDED\r
-            selects.Apply(record=>record.IsExplicitlyChecked=true);\r
+            selects.Apply(record => record.IsExplicitlyChecked = !forSelectiveActivation);\r
+\r
+            //If any of the root nodes has at least one selected child, set it to gray\r
+            SetRootNodeSelections(forSelectiveActivation);\r
 \r
             //shouldBeChecked.Apply(record => record.IsChecked = true);\r
-            \r
-            \r
 \r
+\r
+\r
+        }\r
+\r
+        private void SetRootNodeSelections(bool forSelectiveActivation)\r
+        {\r
+            var selectedRoots = from DirectoryRecord rootRecord in RootNodes\r
+                                from DirectoryRecord record in rootRecord\r
+                                where record.IsChecked == true\r
+                                select rootRecord;\r
+            selectedRoots.Apply(record => record.IsExplicitlyChecked = !forSelectiveActivation);\r
         }\r
 \r
         protected string AccountName { get; set; }\r
@@ -264,7 +282,7 @@ namespace Pithos.Client.WPF.SelectiveSynch
                           where record.Removed && record.Uri != null\r
                          select record.Uri).ToArray();\r
             //TODO: Include Uris for the containers as well\r
-            _events.Publish(new SelectiveSynchChanges{Enabled=IsEnabled, Account=Account,Uris=uris,Added=added,Removed=removed});\r
+            _events.Publish(new SelectiveSynchChanges{Enabled=true, Account=Account,Uris=uris,Added=added,Removed=removed});\r
             \r
 \r
             \r
@@ -272,6 +290,7 @@ namespace Pithos.Client.WPF.SelectiveSynch
             TryClose(true);\r
         }\r
 \r
+/*\r
         private bool _isEnabled;\r
         public bool IsEnabled\r
         {\r
@@ -282,11 +301,12 @@ namespace Pithos.Client.WPF.SelectiveSynch
                 NotifyOfPropertyChange(()=>IsEnabled);\r
             }\r
         }\r
+*/\r
 \r
         private void SaveSettings(IEnumerable<Uri> uris)\r
         {\r
             var selections = uris.Select(uri => uri.ToString()).ToArray();\r
-            Account.SelectiveSyncEnabled = IsEnabled;\r
+            //Account.SelectiveSyncEnabled = IsEnabled;\r
             Account.SelectiveFolders.Clear();\r
             Account.SelectiveFolders.AddRange(selections);\r
             Settings.Default.Save();            \r