root / trunk / Pithos.Client.WPF / PreferencesViewModel.cs @ 5120f3cb
History | View | Annotate | Download (12.6 kB)
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.InteropServices; |
16 |
using System.Runtime.Serialization; |
17 |
using System.Windows; |
18 |
using System.Windows.Forms; |
19 |
using System.Windows.Interop; |
20 |
using Caliburn.Micro; |
21 |
using Hardcodet.Wpf.TaskbarNotification; |
22 |
using Pithos.Client.WPF.Configuration; |
23 |
using Pithos.Core; |
24 |
using Pithos.Interfaces; |
25 |
using IWin32Window = System.Windows.Forms.IWin32Window; |
26 |
using Screen = Caliburn.Micro.Screen; |
27 |
|
28 |
namespace Pithos.Client.WPF |
29 |
{ |
30 |
using System; |
31 |
using System.Collections.Generic; |
32 |
using System.Linq; |
33 |
using System.Text; |
34 |
using System.Threading.Tasks; |
35 |
|
36 |
/// <summary> |
37 |
/// TODO: Update summary. |
38 |
/// </summary> |
39 |
[Export(typeof(IShell))] |
40 |
public class PreferencesViewModel : Screen, IShell, IHandle<Notification> |
41 |
{ |
42 |
private IEventAggregator _events; |
43 |
|
44 |
|
45 |
private PithosSettings _settings; |
46 |
public PithosSettings Settings |
47 |
{ |
48 |
get { return _settings; } |
49 |
set |
50 |
{ |
51 |
_settings = value; |
52 |
foreach (var account in _settings.Accounts.Where(account=>account.IsActive)) |
53 |
{ |
54 |
if (String.IsNullOrWhiteSpace(account.RootPath)) |
55 |
{ |
56 |
account.RootPath = _settings.PithosPath; |
57 |
_settings.Save(); |
58 |
} |
59 |
} |
60 |
NotifyOfPropertyChange(()=>Settings); |
61 |
} |
62 |
} |
63 |
|
64 |
|
65 |
private Dictionary<string,PithosMonitor> _monitors=new Dictionary<string, PithosMonitor>(); |
66 |
public Dictionary<string, PithosMonitor> Monitors |
67 |
{ |
68 |
get { return _monitors; } |
69 |
} |
70 |
|
71 |
private TaskbarViewModel _taskbar; |
72 |
public TaskbarViewModel Taskbar |
73 |
{ |
74 |
get { return _taskbar; } |
75 |
set |
76 |
{ |
77 |
_taskbar = value; |
78 |
} |
79 |
} |
80 |
|
81 |
//ShellExtensionController _extensionController=new ShellExtensionController(); |
82 |
|
83 |
[ImportingConstructor] |
84 |
public PreferencesViewModel(IEventAggregator events, TaskbarViewModel taskbar, PithosSettings settings) |
85 |
{ |
86 |
_events = events; |
87 |
_events.Subscribe(this); |
88 |
|
89 |
DisplayName = "Pithos Preferences"; |
90 |
|
91 |
Taskbar=taskbar; |
92 |
Taskbar.Parent = this; |
93 |
|
94 |
Settings=settings; |
95 |
|
96 |
|
97 |
Taskbar.UsageMessage = "Using 15% of 50 GB"; |
98 |
Taskbar.StatusMessage = "In Synch"; |
99 |
} |
100 |
|
101 |
protected override void OnViewAttached(object view, object context) |
102 |
{ |
103 |
var window = (Window)view; |
104 |
|
105 |
base.OnViewAttached(view, context); |
106 |
} |
107 |
|
108 |
|
109 |
protected override void OnViewLoaded(object view) |
110 |
{ |
111 |
var window = (Window)view; |
112 |
window.Hide(); |
113 |
Taskbar.UpdateStatus(); |
114 |
base.OnViewLoaded(view); |
115 |
} |
116 |
|
117 |
#region Preferences Properties |
118 |
|
119 |
private bool _noProxy; |
120 |
public bool NoProxy |
121 |
{ |
122 |
get { return _noProxy; } |
123 |
set |
124 |
{ |
125 |
_noProxy = value; |
126 |
NotifyOfPropertyChange(()=>NoProxy); |
127 |
} |
128 |
} |
129 |
|
130 |
|
131 |
private bool _defaultProxy; |
132 |
|
133 |
public bool DefaultProxy |
134 |
{ |
135 |
get { return _defaultProxy; } |
136 |
set |
137 |
{ |
138 |
_defaultProxy = value; |
139 |
NotifyOfPropertyChange(() => DefaultProxy); |
140 |
} |
141 |
} |
142 |
|
143 |
|
144 |
private bool _manualProxy; |
145 |
|
146 |
public bool ManualProxy |
147 |
{ |
148 |
get { return _manualProxy; } |
149 |
set |
150 |
{ |
151 |
_manualProxy = value; |
152 |
NotifyOfPropertyChange(() => ManualProxy); |
153 |
} |
154 |
} |
155 |
#endregion |
156 |
|
157 |
|
158 |
#region Commands |
159 |
|
160 |
|
161 |
|
162 |
public void SaveChanges() |
163 |
{ |
164 |
DoSave(); |
165 |
|
166 |
} |
167 |
|
168 |
public void RejectChanges() |
169 |
{ |
170 |
Settings.Reload(); |
171 |
|
172 |
} |
173 |
|
174 |
public void ApplyChanges() |
175 |
{ |
176 |
DoSave(); |
177 |
} |
178 |
|
179 |
private void DoSave() |
180 |
{ |
181 |
Settings.Save(); |
182 |
NotifyOfPropertyChange(()=>Settings); |
183 |
|
184 |
var activeAccount = Settings.Accounts.FirstOrDefault(account => account.IsActive); |
185 |
if (activeAccount == null) |
186 |
return; |
187 |
if (String.IsNullOrWhiteSpace(activeAccount.AccountName)) |
188 |
return; |
189 |
|
190 |
var monitor = Monitors[activeAccount.AccountName]; |
191 |
monitor.ApiKey = activeAccount.ApiKey; |
192 |
monitor.UserName = activeAccount.AccountName; |
193 |
monitor.UsePithos = activeAccount.UsePithos; |
194 |
|
195 |
monitor.Start(); |
196 |
} |
197 |
|
198 |
public void ChangePithosFolder() |
199 |
{ |
200 |
var browser = new FolderBrowserDialog(); |
201 |
browser.SelectedPath = Settings.PithosPath; |
202 |
var result = browser.ShowDialog((IWin32Window)GetView()); |
203 |
if (result == DialogResult.OK) |
204 |
{ |
205 |
var newPath = browser.SelectedPath; |
206 |
Directory.Move(Settings.PithosPath, newPath); |
207 |
Settings.PithosPath = newPath; |
208 |
Settings.Save(); |
209 |
NotifyOfPropertyChange(() => Settings); |
210 |
|
211 |
} |
212 |
} |
213 |
|
214 |
public void AddAccount() |
215 |
{ |
216 |
var newAccount = new AccountSettings(); |
217 |
Settings.Accounts.Add(newAccount); |
218 |
SelectedAccountIndex= Settings.Accounts.Count-1; |
219 |
NotifyOfPropertyChange(()=>Settings); |
220 |
} |
221 |
|
222 |
public void AddPithosAccount() |
223 |
{ |
224 |
var task=PithosAccount.RetrieveCredentialsAsync(Settings.PithosSite) |
225 |
.ContinueWith(t=> |
226 |
{ |
227 |
var credentials=t.Result; |
228 |
var account = Settings.Accounts.FirstOrDefault(act => act.AccountName == credentials.UserName); |
229 |
if (account == null) |
230 |
{ |
231 |
account=new AccountSettings{ |
232 |
AccountName=credentials.UserName, |
233 |
ApiKey=credentials.Password, |
234 |
UsePithos=true |
235 |
}; |
236 |
Settings.Accounts.Add(account); |
237 |
} |
238 |
else |
239 |
{ |
240 |
account.ApiKey=credentials.Password; |
241 |
} |
242 |
SelectedAccountIndex= Settings.Accounts.IndexOf(account); |
243 |
NotifyOfPropertyChange(()=>Settings); |
244 |
}); |
245 |
((Task)task).WaitWithPumping(); |
246 |
} |
247 |
|
248 |
public void RemoveAccount() |
249 |
{ |
250 |
Settings.Accounts.RemoveAll(account => account.AccountName == CurrentAccount.AccountName); |
251 |
|
252 |
NotifyOfPropertyChange(()=>CurrentAccount); |
253 |
NotifyOfPropertyChange(()=>Settings); |
254 |
//NotifyOfPropertyChange("Settings.Accounts"); |
255 |
} |
256 |
|
257 |
public bool CanRemoveAccount |
258 |
{ |
259 |
get { return (CurrentAccount != null); } |
260 |
} |
261 |
|
262 |
|
263 |
|
264 |
public bool ExtensionsActivated |
265 |
{ |
266 |
get { return Settings.ExtensionsActivated; } |
267 |
set |
268 |
{ |
269 |
if (Settings.ExtensionsActivated == value) |
270 |
return; |
271 |
|
272 |
Settings.ExtensionsActivated = value; |
273 |
|
274 |
//if (value) |
275 |
// _extensionController.RegisterExtensions(); |
276 |
//else |
277 |
//{ |
278 |
// _extensionController.UnregisterExtensions(); |
279 |
//} |
280 |
NotifyOfPropertyChange(() => ExtensionsActivated); |
281 |
} |
282 |
} |
283 |
|
284 |
public void RefreshOverlays() |
285 |
{ |
286 |
string path=Settings.PithosPath; |
287 |
if (String.IsNullOrWhiteSpace(path)) |
288 |
throw new ArgumentNullException("path", "The path parameter must not be emtpy"); |
289 |
|
290 |
if (!Directory.Exists(path) && !File.Exists(path)) |
291 |
throw new FileNotFoundException("The specified file or path does not exist", path); |
292 |
|
293 |
|
294 |
IntPtr pathPointer = Marshal.StringToCoTaskMemAuto(path); |
295 |
|
296 |
try |
297 |
{ |
298 |
NativeMethods.SHChangeNotify(HChangeNotifyEventID.SHCNE_UPDATEITEM, |
299 |
HChangeNotifyFlags.SHCNF_PATHW | HChangeNotifyFlags.SHCNF_FLUSHNOWAIT, |
300 |
pathPointer, IntPtr.Zero); |
301 |
} |
302 |
finally |
303 |
{ |
304 |
Marshal.FreeHGlobal(pathPointer); |
305 |
} |
306 |
|
307 |
} |
308 |
#endregion |
309 |
|
310 |
private int _selectedAccountIndex; |
311 |
public int SelectedAccountIndex |
312 |
{ |
313 |
get { return _selectedAccountIndex; } |
314 |
set |
315 |
{ |
316 |
//var accountCount=Settings.Accounts.Count; |
317 |
//if (accountCount == 0) |
318 |
// return; |
319 |
//if (0 <= value && value < accountCount) |
320 |
// _selectedAccountIndex = value; |
321 |
//else |
322 |
// _selectedAccountIndex = 0; |
323 |
_selectedAccountIndex = value; |
324 |
NotifyOfPropertyChange(() => CurrentAccount); |
325 |
NotifyOfPropertyChange(() => CanRemoveAccount); |
326 |
NotifyOfPropertyChange(()=>SelectedAccountIndex); |
327 |
} |
328 |
} |
329 |
|
330 |
private AccountSettings _currentAccount; |
331 |
public AccountSettings CurrentAccount |
332 |
{ |
333 |
get { |
334 |
if (0 <= SelectedAccountIndex && SelectedAccountIndex < Settings.Accounts.Count) |
335 |
return Settings.Accounts[SelectedAccountIndex]; |
336 |
return null; |
337 |
} |
338 |
|
339 |
} |
340 |
|
341 |
|
342 |
public void Handle(Notification notification) |
343 |
{ |
344 |
if (!Settings.ShowDesktopNotifications) |
345 |
return; |
346 |
BalloonIcon icon = BalloonIcon.None; |
347 |
switch (notification.Level) |
348 |
{ |
349 |
case TraceLevel.Error: |
350 |
icon = BalloonIcon.Error; |
351 |
break; |
352 |
case TraceLevel.Info: |
353 |
case TraceLevel.Verbose: |
354 |
icon = BalloonIcon.Info; |
355 |
break; |
356 |
case TraceLevel.Warning: |
357 |
icon = BalloonIcon.Warning; |
358 |
break; |
359 |
default: |
360 |
icon = BalloonIcon.None; |
361 |
break; |
362 |
} |
363 |
|
364 |
var tv = (PreferencesView)this.GetView(); |
365 |
tv.TaskbarView.ShowBalloonTip(notification.Title, notification.Message, icon); |
366 |
} |
367 |
|
368 |
|
369 |
public void MoveAccountFolder() |
370 |
{ |
371 |
|
372 |
using (var dlg = new FolderBrowserDialog()) |
373 |
{ |
374 |
var currentFolder = CurrentAccount.RootPath; |
375 |
dlg.SelectedPath = currentFolder; |
376 |
//Ask the user to select a folder |
377 |
//Note: We need a parent window here, which we retrieve with GetView |
378 |
var view = (Window)GetView(); |
379 |
if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view))) |
380 |
return; |
381 |
|
382 |
var newPath= dlg.SelectedPath; |
383 |
//Find the account's monitor and stop it |
384 |
var monitor = Monitors[CurrentAccount.AccountName]; |
385 |
monitor.Stop(); |
386 |
|
387 |
var oldPath = monitor.RootPath; |
388 |
//The old directory may not exist eg. if we create an account for the first time |
389 |
if (Directory.Exists(oldPath)) |
390 |
{ |
391 |
//If it does, do the move |
392 |
Directory.Move(oldPath, newPath); |
393 |
//We also need to change the path of the existing file states |
394 |
monitor.MoveFileStates(oldPath, newPath); |
395 |
} |
396 |
//Replace the old rootpath with the new |
397 |
CurrentAccount.RootPath = newPath; |
398 |
//TODO: This will save all settings changes. Too coarse grained, need to fix at a later date |
399 |
Settings.Save(); |
400 |
//And start the monitor on the new RootPath |
401 |
monitor.RootPath = newPath; |
402 |
monitor.Start(); |
403 |
//Finally, notify that the Settings, CurrentAccount have changed |
404 |
NotifyOfPropertyChange(() => CurrentAccount); |
405 |
NotifyOfPropertyChange(() => Settings); |
406 |
|
407 |
} |
408 |
} |
409 |
} |
410 |
} |