Removed differences between Pithos and Cloudfiles authentication.
[pithos-ms-client] / trunk / Pithos.Client.WPF / PreferencesViewModel.cs
1 // -----------------------------------------------------------------------
2 // <copyright file="PreferencesViewModel.cs" company="Microsoft">
3 // TODO: Update copyright text.
4 // </copyright>
5 // -----------------------------------------------------------------------
6
7 using System.Collections;
8 using System.ComponentModel.Composition;
9 using System.Diagnostics;
10 using System.IO;
11 using System.IO.IsolatedStorage;
12 using System.Linq.Expressions;
13 using System.Net;
14 using System.Reflection;
15 using System.Runtime.Serialization;
16 using System.Windows;
17 using System.Windows.Forms;
18 using Caliburn.Micro;
19 using Pithos.Client.WPF.Configuration;
20 using Pithos.Core;
21 using Pithos.Interfaces;
22 using Pithos.ShellExtensions;
23 using Screen = Caliburn.Micro.Screen;
24
25 namespace Pithos.Client.WPF
26 {
27     using System;
28     using System.Collections.Generic;
29     using System.Linq;
30     using System.Text;
31
32     /// <summary>
33     /// TODO: Update summary.
34     /// </summary>
35     [Export(typeof(IShell))]
36     public class PreferencesViewModel : Screen, IShell
37     {
38         
39
40
41         public IPithosSettings Settings { get; set; }
42
43
44         public PithosMonitor Monitor { get; private set; }
45
46         public TaskbarViewModel Taskbar { get; set; }
47
48         ShellExtensionController _extensionController=new ShellExtensionController();
49
50         [ImportingConstructor]
51         public PreferencesViewModel(TaskbarViewModel taskbar, IPithosSettings settings, PithosMonitor monitor)
52         {
53             DisplayName = "Pithos Preferences";
54
55             Taskbar=taskbar;
56             Taskbar.Parent = this;
57             
58             Settings=settings;
59             Monitor=monitor;
60
61
62             Taskbar.UsageMessage = "Using 15% of 50 GB";
63             Taskbar.RecentFiles.AddRange(new[]
64                                      {
65                                       new FileEntry{FileName="Moo",FullPath=@"e:\Pithos\moo"}   ,
66                                       new FileEntry{FileName="Mee",FullPath=@"e:\Pithos\mee"}   
67                                      });
68             Taskbar.StatusMessage = "In Synch";
69             Taskbar.UpdateStatus();
70         }
71
72         protected override void OnViewAttached(object view, object context)
73         {
74             var window = (Window)view;
75
76             base.OnViewAttached(view, context);
77         }
78
79
80         protected override void OnViewLoaded(object view)
81         {
82             var window = (Window)view;
83             window.Hide();
84
85             base.OnViewLoaded(view);
86         }
87
88         #region Preferences Properties
89
90         private bool _noProxy;
91         public bool NoProxy
92         {
93             get { return _noProxy; }
94             set
95             {
96                 _noProxy = value;
97                 NotifyOfPropertyChange(()=>NoProxy);
98             }
99         }
100
101
102         private bool _defaultProxy;
103
104         public bool DefaultProxy
105         {
106             get { return _defaultProxy; }
107             set
108             {
109                 _defaultProxy = value;
110                 NotifyOfPropertyChange(() => DefaultProxy);
111             }
112         }
113
114
115         private bool _manualProxy;
116
117         public bool ManualProxy
118         {
119             get { return _manualProxy; }
120             set
121             {
122                 _manualProxy = value;
123                 NotifyOfPropertyChange(() => ManualProxy);
124             }
125         }
126         #endregion
127
128        
129         #region Commands
130         
131
132     
133         public void SaveChanges()
134         {
135             DoSave();
136            
137         }
138
139         public void RejectChanges()
140         {
141             Settings.Reload();
142            
143         }
144
145         public void ApplyChanges()
146         {
147             DoSave();
148         }
149
150         private void DoSave()
151         {
152             Settings.Save();
153             NotifyOfPropertyChange(()=>Settings);
154             Monitor.Start();
155         }
156
157         public void ChangePithosFolder()
158         {
159             var browser = new FolderBrowserDialog();
160             browser.SelectedPath = Settings.PithosPath;
161             var result = browser.ShowDialog((IWin32Window)GetView());
162             if (result == DialogResult.OK)
163             {
164                 var newPath = browser.SelectedPath;
165                 Directory.Move(Settings.PithosPath, newPath);
166                 Settings.PithosPath = newPath;
167                 Settings.Save();
168                 NotifyOfPropertyChange(() => Settings);
169
170             }
171         }
172
173        public void AddAccount()
174         {
175             var newAccount = new AccountSettings();
176             Settings.Accounts.Add(newAccount);
177             SelectedAccountIndex= Settings.Accounts.Count-1;
178             NotifyOfPropertyChange(()=>Settings);
179         }
180
181         public void RemoveAccount()
182         {
183             Settings.Accounts.RemoveAll(account => account.AccountName == CurrentAccount.AccountName);
184             
185             NotifyOfPropertyChange(()=>CurrentAccount);
186             NotifyOfPropertyChange(()=>Settings);
187             //NotifyOfPropertyChange("Settings.Accounts");
188         }
189
190         public bool CanRemoveAccount
191         {
192             get { return (CurrentAccount != null); }
193         }
194
195
196
197         public bool ExtensionsActivated
198         {
199             get { return Settings.ExtensionsActivated; }
200             set
201             {
202                 if (Settings.ExtensionsActivated == value)
203                     return;
204
205                 Settings.ExtensionsActivated = value;
206
207                 if (value)
208                     _extensionController.RegisterExtensions();
209                 else
210                 {
211                     _extensionController.UnregisterExtensions();
212                 }
213                 NotifyOfPropertyChange(() => ExtensionsActivated);
214             }
215         }
216
217         public void RefreshOverlays()
218         {
219             this.Monitor.Workflow.RaiseChangeNotification(Settings.PithosPath);
220         }
221         #endregion
222
223         private int _selectedAccountIndex;
224         public int SelectedAccountIndex
225         {
226             get { return _selectedAccountIndex; }
227             set
228             {
229                 _selectedAccountIndex = value;
230                 NotifyOfPropertyChange(() => CurrentAccount);
231                 NotifyOfPropertyChange(() => CanRemoveAccount);
232                 NotifyOfPropertyChange(()=>SelectedAccountIndex);
233             }
234         }
235
236         private AccountSettings _currentAccount;
237         public AccountSettings CurrentAccount
238         {
239             get { return Settings.Accounts[SelectedAccountIndex]; }
240 /*
241             set
242             {
243                 _currentAccount = value;
244             }
245 */
246         }
247
248
249     }
250 }