Merge branch 'master' of https://code.grnet.gr/git/pithos-ms-client
[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
155             var activeAccount = Settings.Accounts.FirstOrDefault(account => account.IsActive);
156             if (activeAccount == null)
157                 return;
158             if (String.IsNullOrWhiteSpace(activeAccount.AccountName))
159                 return;
160
161             Monitor.ApiKey = activeAccount.ApiKey;
162             Monitor.UserName = activeAccount.AccountName;
163             Monitor.UsePithos = activeAccount.UsePithos;
164
165             Monitor.Start();
166         }
167
168         public void ChangePithosFolder()
169         {
170             var browser = new FolderBrowserDialog();
171             browser.SelectedPath = Settings.PithosPath;
172             var result = browser.ShowDialog((IWin32Window)GetView());
173             if (result == DialogResult.OK)
174             {
175                 var newPath = browser.SelectedPath;
176                 Directory.Move(Settings.PithosPath, newPath);
177                 Settings.PithosPath = newPath;
178                 Settings.Save();
179                 NotifyOfPropertyChange(() => Settings);
180
181             }
182         }
183
184        public void AddAccount()
185         {
186             var newAccount = new AccountSettings();
187             Settings.Accounts.Add(newAccount);
188             SelectedAccountIndex= Settings.Accounts.Count-1;
189             NotifyOfPropertyChange(()=>Settings);
190         }
191
192         public void RemoveAccount()
193         {
194             Settings.Accounts.RemoveAll(account => account.AccountName == CurrentAccount.AccountName);
195             
196             NotifyOfPropertyChange(()=>CurrentAccount);
197             NotifyOfPropertyChange(()=>Settings);
198             //NotifyOfPropertyChange("Settings.Accounts");
199         }
200
201         public bool CanRemoveAccount
202         {
203             get { return (CurrentAccount != null); }
204         }
205
206
207
208         public bool ExtensionsActivated
209         {
210             get { return Settings.ExtensionsActivated; }
211             set
212             {
213                 if (Settings.ExtensionsActivated == value)
214                     return;
215
216                 Settings.ExtensionsActivated = value;
217
218                 if (value)
219                     _extensionController.RegisterExtensions();
220                 else
221                 {
222                     _extensionController.UnregisterExtensions();
223                 }
224                 NotifyOfPropertyChange(() => ExtensionsActivated);
225             }
226         }
227
228         public void RefreshOverlays()
229         {
230             this.Monitor.Workflow.RaiseChangeNotification(Settings.PithosPath);
231         }
232         #endregion
233
234         private int _selectedAccountIndex;
235         public int SelectedAccountIndex
236         {
237             get { return _selectedAccountIndex; }
238             set
239             {
240                 //var accountCount=Settings.Accounts.Count;
241                 //if (accountCount == 0)
242                 //    return;
243                 //if (0 <= value && value < accountCount)
244                 //    _selectedAccountIndex = value;
245                 //else
246                 //    _selectedAccountIndex = 0;
247                 NotifyOfPropertyChange(() => CurrentAccount);
248                 NotifyOfPropertyChange(() => CanRemoveAccount);
249                 NotifyOfPropertyChange(()=>SelectedAccountIndex);
250             }
251         }
252
253         private AccountSettings _currentAccount;
254         public AccountSettings CurrentAccount
255         {
256             get {
257                 if (0 <= SelectedAccountIndex && SelectedAccountIndex < Settings.Accounts.Count)                    
258                     return Settings.Accounts[SelectedAccountIndex];
259                 return null;
260             }
261 /*
262             set
263             {
264                 _currentAccount = value;
265             }
266 */
267         }
268
269
270     }
271 }