Logging changes, first changes to multi account support
[pithos-ms-client] / trunk / Pithos.Client.WPF / PreferencesViewModel.cs
index a21698e..d6a25cb 100644 (file)
@@ -16,12 +16,13 @@ using System.Runtime.InteropServices;
 using System.Runtime.Serialization;
 using System.Windows;
 using System.Windows.Forms;
+using System.Windows.Interop;
 using Caliburn.Micro;
 using Hardcodet.Wpf.TaskbarNotification;
 using Pithos.Client.WPF.Configuration;
 using Pithos.Core;
 using Pithos.Interfaces;
-
+using IWin32Window = System.Windows.Forms.IWin32Window;
 using Screen = Caliburn.Micro.Screen;
 
 namespace Pithos.Client.WPF
@@ -41,10 +42,31 @@ namespace Pithos.Client.WPF
         private IEventAggregator _events;
 
 
-        public PithosSettings Settings { get; set; }
+        private PithosSettings _settings;
+        public PithosSettings Settings
+        {
+            get { return _settings; }
+            set
+            {
+                _settings = value;
+                foreach (var account in _settings.Accounts.Where(account=>account.IsActive))
+                {
+                    if (String.IsNullOrWhiteSpace(account.RootPath))
+                    {
+                        account.RootPath = _settings.PithosPath;
+                        _settings.Save();
+                    }
+                }
+                NotifyOfPropertyChange(()=>Settings);
+            }
+        }
 
 
-        public PithosMonitor Monitor { get; private set; }
+        private Dictionary<string,PithosMonitor> _monitors=new Dictionary<string, PithosMonitor>();
+        public Dictionary<string, PithosMonitor> Monitors
+        {
+            get { return _monitors; }            
+        }
 
         private TaskbarViewModel _taskbar;
         public TaskbarViewModel Taskbar
@@ -59,7 +81,7 @@ namespace Pithos.Client.WPF
         //ShellExtensionController _extensionController=new ShellExtensionController();
 
         [ImportingConstructor]
-        public PreferencesViewModel(IEventAggregator events, TaskbarViewModel taskbar, PithosSettings settings, PithosMonitor monitor)
+        public PreferencesViewModel(IEventAggregator events, TaskbarViewModel taskbar, PithosSettings settings)
         {
             _events = events;
             _events.Subscribe(this);
@@ -69,8 +91,7 @@ namespace Pithos.Client.WPF
             Taskbar=taskbar;
             Taskbar.Parent = this;
             
-            Settings=settings;
-            Monitor=monitor;
+            Settings=settings;            
 
 
             Taskbar.UsageMessage = "Using 15% of 50 GB";
@@ -166,11 +187,12 @@ namespace Pithos.Client.WPF
             if (String.IsNullOrWhiteSpace(activeAccount.AccountName))
                 return;
 
-            Monitor.ApiKey = activeAccount.ApiKey;
-            Monitor.UserName = activeAccount.AccountName;
-            Monitor.UsePithos = activeAccount.UsePithos;
+            var monitor = Monitors[activeAccount.AccountName];
+            monitor.ApiKey = activeAccount.ApiKey;
+            monitor.UserName = activeAccount.AccountName;
+            monitor.UsePithos = activeAccount.UsePithos;
 
-            Monitor.Start();
+            monitor.Start();
         }
 
         public void ChangePithosFolder()
@@ -313,12 +335,7 @@ namespace Pithos.Client.WPF
                     return Settings.Accounts[SelectedAccountIndex];
                 return null;
             }
-/*
-            set
-            {
-                _currentAccount = value;
-            }
-*/
+
         }
 
 
@@ -348,5 +365,46 @@ namespace Pithos.Client.WPF
             tv.TaskbarView.ShowBalloonTip(notification.Title, notification.Message, icon);
         }
 
+
+    public void MoveAccountFolder()
+    {
+
+        using (var dlg = new FolderBrowserDialog())
+        {
+            var currentFolder = CurrentAccount.RootPath;
+            dlg.SelectedPath = currentFolder;
+            //Ask the user to select a folder
+            //Note: We need a parent window here, which we retrieve with GetView            
+            var view = (Window)GetView();            
+            if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view)))
+                return;            
+
+            var newPath= dlg.SelectedPath;                
+            //Find the account's monitor and stop it
+            var monitor = Monitors[CurrentAccount.AccountName];            
+            monitor.Stop();
+                            
+            var oldPath = monitor.RootPath;                
+            //The old directory may not exist eg. if we create an account for the first time
+            if (Directory.Exists(oldPath))
+            {
+                //If it does, do the move
+                Directory.Move(oldPath, newPath);
+                //We also need to change the path of the existing file states
+                monitor.MoveFileStates(oldPath, newPath);
+            }
+            //Replace the old rootpath with the new
+            CurrentAccount.RootPath = newPath;
+            //TODO: This will save all settings changes. Too coarse grained, need to fix at a later date
+            Settings.Save();            
+            //And start the monitor on the new RootPath            
+            monitor.RootPath = newPath;
+            monitor.Start();
+            //Finally, notify that the Settings, CurrentAccount have changed
+            NotifyOfPropertyChange(() => CurrentAccount);
+            NotifyOfPropertyChange(() => Settings);
+
+        }
+    }
     }
 }