Added retry when trying to determine the selective folders of a new account. If this...
authorpkanavos <pkanavos@gmail.com>
Wed, 4 Jul 2012 14:08:55 +0000 (17:08 +0300)
committerpkanavos <pkanavos@gmail.com>
Wed, 4 Jul 2012 14:08:55 +0000 (17:08 +0300)
trunk/Pithos.Client.WPF/Preferences/PreferencesViewModel.cs

index b9cbf78..ed94636 100644 (file)
@@ -405,85 +405,110 @@ namespace Pithos.Client.WPF.Preferences
            var wizard = new AddAccountViewModel();\r
            if (_windowManager.ShowDialog(wizard) == true)\r
            {\r
-               string selectedPath = wizard.AccountPath;\r
-               var initialRootPath = wizard.ShouldCreateOkeanosFolder?\r
-                   Path.Combine(selectedPath, "Okeanos")\r
-                   :selectedPath;\r
-               var actualRootPath= initialRootPath;\r
-               if (wizard.ShouldCreateOkeanosFolder)\r
+               try\r
                {\r
-                   int attempt = 1;\r
-                   while (Directory.Exists(actualRootPath) || File.Exists(actualRootPath))\r
+                   string selectedPath = wizard.AccountPath;\r
+                   var initialRootPath = wizard.ShouldCreateOkeanosFolder?\r
+                                                                             Path.Combine(selectedPath, "Okeanos")\r
+                                             :selectedPath;\r
+                   var actualRootPath= initialRootPath;\r
+                   if (wizard.ShouldCreateOkeanosFolder)\r
                    {\r
-                       actualRootPath = String.Format("{0} {1}", initialRootPath, attempt++);\r
+                       int attempt = 1;\r
+                       while (Directory.Exists(actualRootPath) || File.Exists(actualRootPath))\r
+                       {\r
+                           actualRootPath = String.Format("{0} {1}", initialRootPath, attempt++);\r
+                       }\r
                    }\r
-               }\r
 \r
 \r
 \r
-               var account = Accounts.FirstOrDefault(act => act.AccountName == wizard.AccountName && act.ServerUrl == wizard.CurrentServer);\r
-               if (account != null)\r
-               {\r
-                   if (MessageBox.Show("The account you specified already exists. Do you want to update it?", "The account exists") == MessageBoxResult.Yes)\r
+                   var account = Accounts.FirstOrDefault(act => act.AccountName == wizard.AccountName && act.ServerUrl == wizard.CurrentServer);\r
+                   if (account != null)\r
                    {\r
-                       account.ApiKey = wizard.Token;\r
-                       account.IsExpired = false;\r
-                       CurrentAccount = account;\r
+                       if (MessageBox.Show("The account you specified already exists. Do you want to update it?", "The account exists") == MessageBoxResult.Yes)\r
+                       {\r
+                           account.ApiKey = wizard.Token;\r
+                           account.IsExpired = false;\r
+                           CurrentAccount = account;\r
+                       }\r
                    }\r
-               }\r
-               else\r
-               {\r
-                   var newAccount = new AccountSettings\r
-                                        {\r
-                                            AccountName = wizard.AccountName,\r
-                                            ServerUrl = wizard.CurrentServer,\r
-                                            ApiKey = wizard.Token,\r
-                                            RootPath = actualRootPath,\r
-                                            IsActive = wizard.IsAccountActive,\r
-                                        };\r
-\r
-\r
-                   var client = new CloudFilesClient(newAccount.AccountName, newAccount.ApiKey)\r
-                                    {\r
-                                        AuthenticationUrl = newAccount.ServerUrl, UsePithos = true\r
-                                    };\r
-                   client.Authenticate();\r
-\r
+                   else\r
+                   {\r
+                       var newAccount = new AccountSettings\r
+                                            {\r
+                                                AccountName = wizard.AccountName,\r
+                                                ServerUrl = wizard.CurrentServer,\r
+                                                ApiKey = wizard.Token,\r
+                                                RootPath = actualRootPath,\r
+                                                IsActive = wizard.IsAccountActive,\r
+                                            };\r
 \r
-                   var containers = client.ListContainers(newAccount.AccountName);\r
-                   var containerUris = from container in containers\r
-                                       select String.Format(@"{0}/v1/{1}/{2}",\r
-                                            newAccount.ServerUrl, newAccount.AccountName, container.Name);\r
-                   \r
-                   newAccount.SelectiveFolders.AddRange(containerUris.ToArray());\r
 \r
-                   var objectInfos = (from container in containers\r
-                                      from dir in client.ListObjects(newAccount.AccountName, container.Name)\r
-                                      where container.Name != "trash"\r
-                                      select dir).ToList();\r
-                   var tree = objectInfos.ToTree();\r
+                       var client = new CloudFilesClient(newAccount.AccountName, newAccount.ApiKey)\r
+                                        {\r
+                                            AuthenticationUrl = newAccount.ServerUrl, UsePithos = true\r
+                                        };\r
 \r
-                   var selected = (from root in tree \r
-                                   from child in root \r
-                                   select child.Uri.ToString()).ToArray();\r
-                   newAccount.SelectiveFolders.AddRange(selected);\r
+                       InitializeSelectiveFolders(newAccount, client);\r
 \r
                    \r
-                   //TODO:Add the "pithos" container as a default selection                   \r
+                       //TODO:Add the "pithos" container as a default selection                   \r
 \r
-                   _accountsToAdd.Add(newAccount);\r
-                   var accountVm = new AccountViewModel(newAccount);\r
-                   (Accounts as IProducerConsumerCollection<AccountViewModel>).TryAdd(accountVm);\r
-                   CurrentAccount = accountVm;\r
+                       _accountsToAdd.Add(newAccount);\r
+                       var accountVm = new AccountViewModel(newAccount);\r
+                       (Accounts as IProducerConsumerCollection<AccountViewModel>).TryAdd(accountVm);\r
+                       CurrentAccount = accountVm;\r
+                   }\r
+                   NotifyOfPropertyChange(() => Accounts);\r
+                   NotifyOfPropertyChange(() => Settings);\r
+               }\r
+               catch (WebException exc)\r
+               {\r
+                   Log.ErrorFormat("[Add Account] Connectivity Error: {0}", exc);\r
+                   MessageBox.Show("Unable to connect to Pithos. Please try again later", "Connectivity Error",\r
+                                   MessageBoxButton.OK, MessageBoxImage.Exclamation);\r
                }\r
-               NotifyOfPropertyChange(() => Accounts);\r
-               NotifyOfPropertyChange(() => Settings);   \r
            }\r
 \r
 \r
             \r
        }\r
 \r
+        private void InitializeSelectiveFolders(AccountSettings newAccount, CloudFilesClient client,int retries=3)\r
+        {\r
+            try\r
+            {\r
+                client.Authenticate();\r
+\r
+                var containers = client.ListContainers(newAccount.AccountName);\r
+                var containerUris = from container in containers\r
+                                    select String.Format(@"{0}/v1/{1}/{2}",\r
+                                                         newAccount.ServerUrl, newAccount.AccountName,\r
+                                                         container.Name);\r
+\r
+                newAccount.SelectiveFolders.AddRange(containerUris.ToArray());\r
+\r
+                var objectInfos = (from container in containers\r
+                                   from dir in client.ListObjects(newAccount.AccountName, container.Name)\r
+                                   where container.Name != "trash"\r
+                                   select dir).ToList();\r
+                var tree = objectInfos.ToTree();\r
+\r
+                var selected = (from root in tree\r
+                                from child in root\r
+                                select child.Uri.ToString()).ToArray();\r
+                newAccount.SelectiveFolders.AddRange(selected);                \r
+            }\r
+            catch (WebException)\r
+            {\r
+                if (retries>0)\r
+                    InitializeSelectiveFolders(newAccount,client,retries-1);\r
+                else\r
+                    throw;\r
+            }\r
+        }\r
+\r
 /*\r
         public void AddPithosAccount()\r
        {\r