root / trunk / Pithos.Client.WPF / Preferences / PreferencesViewModel.cs @ 78ebfd2d
History | View | Annotate | Download (15.5 kB)
1 |
// ----------------------------------------------------------------------- |
---|---|
2 |
// <copyright file="PreferencesViewModel.cs" company="GRNet"> |
3 |
// Copyright 2011 GRNET S.A. All rights reserved. |
4 |
// |
5 |
// Redistribution and use in source and binary forms, with or |
6 |
// without modification, are permitted provided that the following |
7 |
// conditions are met: |
8 |
// |
9 |
// 1. Redistributions of source code must retain the above |
10 |
// copyright notice, this list of conditions and the following |
11 |
// disclaimer. |
12 |
// |
13 |
// 2. Redistributions in binary form must reproduce the above |
14 |
// copyright notice, this list of conditions and the following |
15 |
// disclaimer in the documentation and/or other materials |
16 |
// provided with the distribution. |
17 |
// |
18 |
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
19 |
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
20 |
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
21 |
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
22 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
25 |
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
26 |
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 |
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
28 |
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 |
// POSSIBILITY OF SUCH DAMAGE. |
30 |
// |
31 |
// The views and conclusions contained in the software and |
32 |
// documentation are those of the authors and should not be |
33 |
// interpreted as representing official policies, either expressed |
34 |
// or implied, of GRNET S.A. |
35 |
|
36 |
// </copyright> |
37 |
// ----------------------------------------------------------------------- |
38 |
|
39 |
using System.Collections.Concurrent; |
40 |
using System.ComponentModel.Composition; |
41 |
using System.IO; |
42 |
using System.Reflection; |
43 |
using System.Windows; |
44 |
using System.Windows.Forms; |
45 |
using Caliburn.Micro; |
46 |
using IWshRuntimeLibrary; |
47 |
using Pithos.Client.WPF.Configuration; |
48 |
using Pithos.Client.WPF.Preferences; |
49 |
using Pithos.Client.WPF.SelectiveSynch; |
50 |
using Pithos.Core; |
51 |
using Pithos.Interfaces; |
52 |
using File = System.IO.File; |
53 |
using Screen = Caliburn.Micro.Screen; |
54 |
|
55 |
namespace Pithos.Client.WPF |
56 |
{ |
57 |
using System; |
58 |
using System.Linq; |
59 |
using System.Threading.Tasks; |
60 |
|
61 |
/// <summary> |
62 |
/// TODO: Update summary. |
63 |
/// </summary> |
64 |
[Export] |
65 |
public class PreferencesViewModel : Screen |
66 |
{ |
67 |
private IEventAggregator _events; |
68 |
|
69 |
|
70 |
private PithosSettings _settings; |
71 |
public PithosSettings Settings |
72 |
{ |
73 |
get { return _settings; } |
74 |
set |
75 |
{ |
76 |
_settings = value; |
77 |
NotifyOfPropertyChange(()=>Settings); |
78 |
} |
79 |
} |
80 |
|
81 |
private ObservableConcurrentCollection<AccountSettings> _accounts; |
82 |
public ObservableConcurrentCollection<AccountSettings> Accounts |
83 |
{ |
84 |
get { return _accounts; } |
85 |
set |
86 |
{ |
87 |
_accounts = value; |
88 |
NotifyOfPropertyChange(()=>Accounts); |
89 |
} |
90 |
} |
91 |
|
92 |
public bool StartOnSystemStartup { get; set; } |
93 |
|
94 |
private static void CreateShortcut(string shortcutPath) |
95 |
{ |
96 |
//TODO: ###THROWS PERMISSIONS ERROR### |
97 |
var wshShell = new WshShellClass(); |
98 |
var shortcut = (IWshRuntimeLibrary.IWshShortcut) wshShell.CreateShortcut( |
99 |
shortcutPath); |
100 |
|
101 |
var exePath = Assembly.GetExecutingAssembly().Location; |
102 |
shortcut.TargetPath = exePath; |
103 |
shortcut.WorkingDirectory = Path.GetDirectoryName(exePath); |
104 |
shortcut.Description = "Pithos"; |
105 |
shortcut.Save(); |
106 |
} |
107 |
|
108 |
public ShellViewModel Shell { get; set; } |
109 |
//ShellExtensionController _extensionController=new ShellExtensionController(); |
110 |
|
111 |
public PreferencesViewModel(IWindowManager windowManager, IEventAggregator events, ShellViewModel shell, PithosSettings settings) |
112 |
{ |
113 |
_windowManager = windowManager; |
114 |
_events = events; |
115 |
|
116 |
DisplayName = "Pithos Preferences"; |
117 |
Shell = shell; |
118 |
|
119 |
Settings=settings; |
120 |
Accounts = new ObservableConcurrentCollection<AccountSettings>(); |
121 |
if (settings.Accounts == null) |
122 |
{ |
123 |
settings.Accounts=new AccountsCollection(); |
124 |
settings.Save(); |
125 |
} |
126 |
Accounts.AddFromEnumerable(settings.Accounts); |
127 |
|
128 |
var startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); |
129 |
_shortcutPath = Path.Combine(startupPath, "Pithos.lnk"); |
130 |
|
131 |
|
132 |
StartOnSystemStartup = File.Exists(_shortcutPath); |
133 |
|
134 |
} |
135 |
|
136 |
|
137 |
#region Preferences Properties |
138 |
|
139 |
private bool _noProxy; |
140 |
public bool NoProxy |
141 |
{ |
142 |
get { return _noProxy; } |
143 |
set |
144 |
{ |
145 |
_noProxy = value; |
146 |
NotifyOfPropertyChange(()=>NoProxy); |
147 |
} |
148 |
} |
149 |
|
150 |
|
151 |
private bool _defaultProxy; |
152 |
|
153 |
public bool DefaultProxy |
154 |
{ |
155 |
get { return _defaultProxy; } |
156 |
set |
157 |
{ |
158 |
_defaultProxy = value; |
159 |
NotifyOfPropertyChange(() => DefaultProxy); |
160 |
} |
161 |
} |
162 |
|
163 |
|
164 |
private bool _manualProxy; |
165 |
|
166 |
public bool ManualProxy |
167 |
{ |
168 |
get { return _manualProxy; } |
169 |
set |
170 |
{ |
171 |
_manualProxy = value; |
172 |
NotifyOfPropertyChange(() => ManualProxy); |
173 |
} |
174 |
} |
175 |
#endregion |
176 |
|
177 |
|
178 |
#region Commands |
179 |
|
180 |
public bool CanSelectiveSyncFolders |
181 |
{ |
182 |
get { return CurrentAccount != null; } |
183 |
} |
184 |
|
185 |
public void SelectiveSyncFolders() |
186 |
{ |
187 |
var monitor = Shell.Monitors[CurrentAccount.AccountName]; |
188 |
var folders=monitor.GetRootFolders(); |
189 |
|
190 |
var model = new SelectiveSynchViewModel(folders,_events,CurrentAccount); |
191 |
if (_windowManager.ShowDialog(model) == true) |
192 |
{ |
193 |
|
194 |
} |
195 |
} |
196 |
|
197 |
public void SaveChanges() |
198 |
{ |
199 |
DoSave(); |
200 |
TryClose(true); |
201 |
} |
202 |
|
203 |
public void RejectChanges() |
204 |
{ |
205 |
Settings.Reload(); |
206 |
TryClose(false); |
207 |
} |
208 |
|
209 |
public void ApplyChanges() |
210 |
{ |
211 |
DoSave(); |
212 |
} |
213 |
|
214 |
private void DoSave() |
215 |
{ |
216 |
Settings.Save(); |
217 |
SetStartupMode(); |
218 |
|
219 |
|
220 |
foreach (var account in Settings.Accounts) |
221 |
{ |
222 |
Shell.MonitorAccount(account); |
223 |
} |
224 |
|
225 |
NotifyOfPropertyChange(()=>Settings); |
226 |
} |
227 |
|
228 |
private void SetStartupMode() |
229 |
{ |
230 |
if (StartOnSystemStartup && !File.Exists(_shortcutPath)) |
231 |
{ |
232 |
CreateShortcut(_shortcutPath); |
233 |
} |
234 |
else if (!StartOnSystemStartup && File.Exists(_shortcutPath)) |
235 |
{ |
236 |
if (File.Exists(_shortcutPath)) |
237 |
File.Delete(_shortcutPath); |
238 |
} |
239 |
} |
240 |
|
241 |
/* public void ChangePithosFolder() |
242 |
{ |
243 |
var browser = new FolderBrowserDialog(); |
244 |
browser.SelectedPath = Settings.PithosPath; |
245 |
var result = browser.ShowDialog((IWin32Window)GetView()); |
246 |
if (result == DialogResult.OK) |
247 |
{ |
248 |
var newPath = browser.SelectedPath; |
249 |
var accountName = CurrentAccount.AccountName; |
250 |
var monitor = Shell.Monitors[accountName]; |
251 |
monitor.Stop(); |
252 |
|
253 |
Shell.Monitors.Remove(accountName); |
254 |
|
255 |
Directory.Move(Settings.PithosPath, newPath); |
256 |
Settings.PithosPath = newPath; |
257 |
Settings.Save(); |
258 |
|
259 |
Shell.MonitorAccount(CurrentAccount); |
260 |
|
261 |
NotifyOfPropertyChange(() => Settings); |
262 |
} |
263 |
} |
264 |
*/ |
265 |
public void AddAccount() |
266 |
{ |
267 |
var wizard = new AddAccountViewModel(); |
268 |
if (_windowManager.ShowDialog(wizard) == true) |
269 |
{ |
270 |
var newAccount = new AccountSettings |
271 |
{ |
272 |
AccountName = wizard.AccountName, |
273 |
ServerUrl=wizard.CurrentServer, |
274 |
ApiKey=wizard.Token, |
275 |
RootPath=wizard.AccountPath, |
276 |
IsActive=wizard.IsAccountActive |
277 |
}; |
278 |
Settings.Accounts.Add(newAccount); |
279 |
(Accounts as IProducerConsumerCollection<AccountSettings>).TryAdd(newAccount); |
280 |
CurrentAccount = newAccount; |
281 |
NotifyOfPropertyChange(() => Accounts); |
282 |
NotifyOfPropertyChange(() => Settings); |
283 |
} |
284 |
|
285 |
|
286 |
|
287 |
} |
288 |
|
289 |
public async void AddPithosAccount() |
290 |
{ |
291 |
var credentials=await PithosAccount.RetrieveCredentials(Settings.PithosLoginUrl); |
292 |
var account = Settings.Accounts.FirstOrDefault(act => act.AccountName == credentials.UserName); |
293 |
if (account == null) |
294 |
{ |
295 |
account=new AccountSettings{ |
296 |
AccountName=credentials.UserName, |
297 |
ApiKey=credentials.Password |
298 |
}; |
299 |
Settings.Accounts.Add(account); |
300 |
(Accounts as IProducerConsumerCollection<AccountSettings>).TryAdd(account); |
301 |
} |
302 |
else |
303 |
{ |
304 |
account.ApiKey=credentials.Password; |
305 |
} |
306 |
//SelectedAccountIndex= Settings.Accounts.IndexOf(account); |
307 |
CurrentAccount = account; |
308 |
NotifyOfPropertyChange(() => Accounts); |
309 |
NotifyOfPropertyChange(()=>Settings); |
310 |
} |
311 |
|
312 |
public void RemoveAccount() |
313 |
{ |
314 |
var accountName = CurrentAccount.AccountName; |
315 |
Settings.Accounts.Remove(CurrentAccount); |
316 |
|
317 |
Accounts.TryRemove(CurrentAccount); |
318 |
|
319 |
|
320 |
CurrentAccount = null; |
321 |
//Accounts = Settings.Accounts; |
322 |
//Settings.Save(); |
323 |
Shell.RemoveMonitor(accountName); |
324 |
NotifyOfPropertyChange(() => Accounts); |
325 |
NotifyOfPropertyChange(() => Settings); |
326 |
|
327 |
//NotifyOfPropertyChange("Settings.Accounts"); |
328 |
} |
329 |
|
330 |
public bool CanRemoveAccount |
331 |
{ |
332 |
get { return (CurrentAccount != null); } |
333 |
} |
334 |
|
335 |
|
336 |
|
337 |
public bool ExtensionsActivated |
338 |
{ |
339 |
get { return Settings.ExtensionsActivated; } |
340 |
set |
341 |
{ |
342 |
if (Settings.ExtensionsActivated == value) |
343 |
return; |
344 |
|
345 |
Settings.ExtensionsActivated = value; |
346 |
|
347 |
/* |
348 |
if (value) |
349 |
_extensionController.RegisterExtensions(); |
350 |
else |
351 |
{ |
352 |
_extensionController.UnregisterExtensions(); |
353 |
} |
354 |
*/ |
355 |
NotifyOfPropertyChange(() => ExtensionsActivated); |
356 |
} |
357 |
} |
358 |
|
359 |
|
360 |
#endregion |
361 |
|
362 |
/* private int _selectedAccountIndex; |
363 |
public int SelectedAccountIndex |
364 |
{ |
365 |
get { return _selectedAccountIndex; } |
366 |
set |
367 |
{ |
368 |
//var accountCount=Settings.Accounts.Count; |
369 |
//if (accountCount == 0) |
370 |
// return; |
371 |
//if (0 <= value && value < accountCount) |
372 |
// _selectedAccountIndex = value; |
373 |
//else |
374 |
// _selectedAccountIndex = 0; |
375 |
_selectedAccountIndex = value; |
376 |
NotifyOfPropertyChange(() => CurrentAccount); |
377 |
NotifyOfPropertyChange(() => CanRemoveAccount); |
378 |
NotifyOfPropertyChange(()=>SelectedAccountIndex); |
379 |
} |
380 |
}*/ |
381 |
|
382 |
private AccountSettings _currentAccount; |
383 |
private IWindowManager _windowManager; |
384 |
private string _shortcutPath; |
385 |
|
386 |
|
387 |
|
388 |
public AccountSettings CurrentAccount |
389 |
{ |
390 |
get { return _currentAccount; } |
391 |
set |
392 |
{ |
393 |
_currentAccount = value; |
394 |
NotifyOfPropertyChange(()=>CurrentAccount); |
395 |
NotifyOfPropertyChange(() => CanRemoveAccount); |
396 |
NotifyOfPropertyChange(() => CanSelectiveSyncFolders); |
397 |
NotifyOfPropertyChange(() => CanMoveAccountFolder); |
398 |
} |
399 |
} |
400 |
|
401 |
/* |
402 |
public AccountSettings CurrentAccount |
403 |
{ |
404 |
get { |
405 |
if (0 <= SelectedAccountIndex && SelectedAccountIndex < Settings.Accounts.Count) |
406 |
return Settings.Accounts[SelectedAccountIndex]; |
407 |
return null; |
408 |
} |
409 |
|
410 |
} |
411 |
*/ |
412 |
|
413 |
|
414 |
public bool CanMoveAccountFolder |
415 |
{ |
416 |
get { return CurrentAccount != null; } |
417 |
} |
418 |
|
419 |
public void MoveAccountFolder() |
420 |
{ |
421 |
|
422 |
using (var dlg = new FolderBrowserDialog()) |
423 |
{ |
424 |
var currentFolder = CurrentAccount.RootPath; |
425 |
dlg.SelectedPath = currentFolder; |
426 |
//Ask the user to select a folder |
427 |
//Note: We need a parent window here, which we retrieve with GetView |
428 |
var view = (Window)GetView(); |
429 |
if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view))) |
430 |
return; |
431 |
|
432 |
var newPath= dlg.SelectedPath; |
433 |
//Find the account's monitor and stop it |
434 |
PithosMonitor monitor; |
435 |
if (Shell.Monitors.TryGetValue(CurrentAccount.AccountName, out monitor)) |
436 |
{ |
437 |
monitor.Stop(); |
438 |
|
439 |
|
440 |
var oldPath = monitor.RootPath; |
441 |
//The old directory may not exist eg. if we create an account for the first time |
442 |
if (Directory.Exists(oldPath)) |
443 |
{ |
444 |
//If it does, do the move |
445 |
|
446 |
//Now Create all of the directories |
447 |
foreach (string dirPath in Directory.EnumerateDirectories(oldPath, "*", |
448 |
SearchOption.AllDirectories)) |
449 |
Directory.CreateDirectory(dirPath.Replace(oldPath, newPath)); |
450 |
|
451 |
//Copy all the files |
452 |
foreach (string newFilePath in Directory.EnumerateFiles(oldPath, "*.*", |
453 |
SearchOption.AllDirectories)) |
454 |
File.Copy(newFilePath, newFilePath.Replace(oldPath, newPath)); |
455 |
|
456 |
Directory.Delete(oldPath, true); |
457 |
|
458 |
//We also need to change the path of the existing file states |
459 |
if (monitor != null) |
460 |
monitor.MoveFileStates(oldPath, newPath); |
461 |
} |
462 |
} |
463 |
//Replace the old rootpath with the new |
464 |
CurrentAccount.RootPath = newPath; |
465 |
//TODO: This will save all settings changes. Too coarse grained, need to fix at a later date |
466 |
Settings.Save(); |
467 |
//And start the monitor on the new RootPath |
468 |
if (monitor != null) |
469 |
{ |
470 |
monitor.RootPath = newPath; |
471 |
if (CurrentAccount.IsActive) |
472 |
monitor.Start(); |
473 |
} |
474 |
else |
475 |
Shell.MonitorAccount(CurrentAccount); |
476 |
//Finally, notify that the Settings, CurrentAccount have changed |
477 |
NotifyOfPropertyChange(() => CurrentAccount); |
478 |
NotifyOfPropertyChange(() => Settings); |
479 |
|
480 |
} |
481 |
} |
482 |
} |
483 |
} |