root / trunk / Pithos.Client.WPF / Preferences / PreferencesViewModel.cs @ 84b880b5
History | View | Annotate | Download (33 kB)
1 |
#region |
---|---|
2 |
/* ----------------------------------------------------------------------- |
3 |
* <copyright file="PreferencesViewModel.cs" company="GRNet"> |
4 |
* |
5 |
* Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 |
* |
7 |
* Redistribution and use in source and binary forms, with or |
8 |
* without modification, are permitted provided that the following |
9 |
* conditions are met: |
10 |
* |
11 |
* 1. Redistributions of source code must retain the above |
12 |
* copyright notice, this list of conditions and the following |
13 |
* disclaimer. |
14 |
* |
15 |
* 2. Redistributions in binary form must reproduce the above |
16 |
* copyright notice, this list of conditions and the following |
17 |
* disclaimer in the documentation and/or other materials |
18 |
* provided with the distribution. |
19 |
* |
20 |
* |
21 |
* THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
22 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
24 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
25 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
28 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
29 |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
31 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
32 |
* POSSIBILITY OF SUCH DAMAGE. |
33 |
* |
34 |
* The views and conclusions contained in the software and |
35 |
* documentation are those of the authors and should not be |
36 |
* interpreted as representing official policies, either expressed |
37 |
* or implied, of GRNET S.A. |
38 |
* </copyright> |
39 |
* ----------------------------------------------------------------------- |
40 |
*/ |
41 |
#endregion |
42 |
|
43 |
|
44 |
using System.Collections.Concurrent; |
45 |
using System.Collections.Generic; |
46 |
using System.Collections.Specialized; |
47 |
using System.ComponentModel.Composition; |
48 |
using System.Diagnostics; |
49 |
using System.IO; |
50 |
using System.Net; |
51 |
using System.Reflection; |
52 |
using System.Threading.Tasks; |
53 |
using System.Windows; |
54 |
using System.Windows.Forms; |
55 |
using Caliburn.Micro; |
56 |
using Pithos.Client.WPF.Configuration; |
57 |
using Pithos.Client.WPF.Properties; |
58 |
using Pithos.Client.WPF.SelectiveSynch; |
59 |
using Pithos.Client.WPF.Utils; |
60 |
using Pithos.Core; |
61 |
using Pithos.Core.Agents; |
62 |
using Pithos.Interfaces; |
63 |
using System; |
64 |
using System.Linq; |
65 |
using Pithos.Network; |
66 |
using MessageBox = System.Windows.MessageBox; |
67 |
using Screen = Caliburn.Micro.Screen; |
68 |
|
69 |
namespace Pithos.Client.WPF.Preferences |
70 |
{ |
71 |
/// <summary> |
72 |
/// The preferences screen displays user and account settings and updates the PithosMonitor |
73 |
/// classes when account settings change. |
74 |
/// </summary> |
75 |
/// <remarks> |
76 |
/// The class is a single ViewModel for all Preferences tabs. It can be broken in separate |
77 |
/// ViewModels, one for each tab. |
78 |
/// </remarks> |
79 |
[Export, PartCreationPolicy(CreationPolicy.Shared)] |
80 |
public class PreferencesViewModel : Screen |
81 |
{ |
82 |
private readonly IEventAggregator _events; |
83 |
|
84 |
//Logging in the Pithos client is provided by log4net |
85 |
private static readonly log4net.ILog Log = |
86 |
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
87 |
|
88 |
private PithosSettings _settings; |
89 |
|
90 |
public PithosSettings Settings |
91 |
{ |
92 |
get { return _settings; } |
93 |
set |
94 |
{ |
95 |
_settings = value; |
96 |
NotifyOfPropertyChange(() => Settings); |
97 |
} |
98 |
} |
99 |
|
100 |
private ObservableConcurrentCollection<AccountViewModel> _accounts; |
101 |
|
102 |
public ObservableConcurrentCollection<AccountViewModel> Accounts |
103 |
{ |
104 |
get { return _accounts; } |
105 |
set |
106 |
{ |
107 |
_accounts = value; |
108 |
NotifyOfPropertyChange(() => Accounts); |
109 |
} |
110 |
} |
111 |
|
112 |
public bool IsBusy |
113 |
{ |
114 |
get { return _isBusy; } |
115 |
set |
116 |
{ |
117 |
if (value.Equals(_isBusy)) return; |
118 |
_isBusy = value; |
119 |
NotifyOfPropertyChange(() => IsBusy); |
120 |
} |
121 |
} |
122 |
|
123 |
public bool StartOnSystemStartup { get; set; } |
124 |
|
125 |
|
126 |
|
127 |
public ShellViewModel Shell { get; set; } |
128 |
//ShellExtensionController _extensionController=new ShellExtensionController(); |
129 |
|
130 |
[ImportingConstructor] |
131 |
public PreferencesViewModel(IWindowManager windowManager, IEventAggregator events, ShellViewModel shell, |
132 |
PithosSettings settings) |
133 |
{ |
134 |
this.DisplayName = "Pithos+ Preferences"; |
135 |
|
136 |
// ReSharper disable DoNotCallOverridableMethodsInConstructor |
137 |
//Caliburn.Micro uses DisplayName for the view's title |
138 |
DisplayName = "Pithos+ Preferences"; |
139 |
// ReSharper restore DoNotCallOverridableMethodsInConstructor |
140 |
|
141 |
_windowManager = windowManager; |
142 |
_events = events; |
143 |
|
144 |
Shell = shell; |
145 |
|
146 |
Settings = settings; |
147 |
Accounts = new ObservableConcurrentCollection<AccountViewModel>(); |
148 |
|
149 |
|
150 |
var startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); |
151 |
_shortcutPath = Path.Combine(startupPath, "Pithos.lnk"); |
152 |
|
153 |
|
154 |
StartOnSystemStartup = File.Exists(_shortcutPath); |
155 |
|
156 |
//SelectedTab = currentTab; |
157 |
} |
158 |
|
159 |
protected override void OnViewLoaded(object view) |
160 |
{ |
161 |
base.OnViewLoaded(view); |
162 |
|
163 |
Settings.Reload(); |
164 |
|
165 |
Accounts.Clear(); |
166 |
|
167 |
if (Settings.Accounts == null) |
168 |
{ |
169 |
Settings.Accounts = new AccountsCollection(); |
170 |
Settings.Save(); |
171 |
} |
172 |
var accountVMs = from account in Settings.Accounts |
173 |
select new AccountViewModel(account); |
174 |
|
175 |
Accounts.AddFromEnumerable(accountVMs); |
176 |
if (Accounts.Count > 0) |
177 |
CurrentAccount = Accounts.First(); |
178 |
|
179 |
} |
180 |
|
181 |
private string _selectedTab = "General"; |
182 |
|
183 |
public string SelectedTab |
184 |
{ |
185 |
get { return _selectedTab; } |
186 |
set |
187 |
{ |
188 |
_selectedTab = value ?? "GeneralTab"; |
189 |
NotifyOfPropertyChange(() => SelectedTab); |
190 |
NotifyOfPropertyChange(() => AccountTabSelected); |
191 |
} |
192 |
} |
193 |
|
194 |
|
195 |
public bool AccountTabSelected |
196 |
{ |
197 |
get { return _selectedTab == "AccountTab"; } |
198 |
} |
199 |
|
200 |
|
201 |
#region Preferences Properties |
202 |
|
203 |
private bool _noProxy; |
204 |
|
205 |
public bool NoProxy |
206 |
{ |
207 |
get { return _noProxy; } |
208 |
set |
209 |
{ |
210 |
_noProxy = value; |
211 |
NotifyOfPropertyChange(() => NoProxy); |
212 |
} |
213 |
} |
214 |
|
215 |
|
216 |
private bool _defaultProxy; |
217 |
|
218 |
public bool DefaultProxy |
219 |
{ |
220 |
get { return _defaultProxy; } |
221 |
set |
222 |
{ |
223 |
_defaultProxy = value; |
224 |
NotifyOfPropertyChange(() => DefaultProxy); |
225 |
} |
226 |
} |
227 |
|
228 |
|
229 |
private bool _manualProxy; |
230 |
|
231 |
public bool ManualProxy |
232 |
{ |
233 |
get { return _manualProxy; } |
234 |
set |
235 |
{ |
236 |
_manualProxy = value; |
237 |
NotifyOfPropertyChange(() => ManualProxy); |
238 |
} |
239 |
} |
240 |
|
241 |
#endregion |
242 |
|
243 |
|
244 |
public int StartupDelay |
245 |
{ |
246 |
get { return (int) Settings.StartupDelay.TotalMinutes; } |
247 |
set |
248 |
{ |
249 |
if (value < 0) |
250 |
throw new ArgumentOutOfRangeException("value", |
251 |
Resources. |
252 |
PreferencesViewModel_StartupDelay_Greater_or_equal_to_0); |
253 |
Settings.StartupDelay = TimeSpan.FromMinutes(value); |
254 |
NotifyOfPropertyChange(() => StartupDelay); |
255 |
} |
256 |
} |
257 |
|
258 |
#region Commands |
259 |
|
260 |
public bool CanSelectiveSyncFolders |
261 |
{ |
262 |
get { return CurrentAccount != null && CurrentAccount.SelectiveSyncEnabled; } |
263 |
} |
264 |
|
265 |
public void SelectiveSyncFolders() |
266 |
{ |
267 |
//var monitor = Shell.Monitors[CurrentAccount.AccountKey]; |
268 |
|
269 |
|
270 |
var model = new SelectiveSynchViewModel(_events, CurrentAccount.Account, CurrentAccount.ApiKey, false); |
271 |
if (_windowManager.ShowDialog(model) == true) |
272 |
{ |
273 |
|
274 |
} |
275 |
} |
276 |
|
277 |
/* private bool _networkTracing; |
278 |
public bool NetworkTracing |
279 |
{ |
280 |
get { return _networkTracing; } |
281 |
set |
282 |
{ |
283 |
_networkTracing = value; |
284 |
|
285 |
} |
286 |
}*/ |
287 |
|
288 |
private DisposeGuard GetBusyGuard() |
289 |
{ |
290 |
return new DisposeGuard(()=>IsBusy=true,()=>IsBusy=false); |
291 |
} |
292 |
|
293 |
public async Task RefreshApiKey() |
294 |
{ |
295 |
//_events.Publish(new Notification { Title = "Authorization failed", Message = "Your API Key has probably expired. You will be directed to a page where you can renew it", Level = TraceLevel.Error }); |
296 |
if (CurrentAccount == null) |
297 |
return; |
298 |
using (GetBusyGuard()) |
299 |
{ |
300 |
try |
301 |
{ |
302 |
//var loginUri = PithosAccount.GetLoginUri(CurrentAccount.ServerUrl); |
303 |
var loginUri = PithosAccount.GetLoginUri(CurrentAccount.ServerUrl); |
304 |
//var credentials = PithosAccount.RetrieveCredentials(loginUri.ToString(), name); |
305 |
var credentials = PithosAccount.RetrieveCredentials(loginUri.ToString()); |
306 |
if (credentials == null) |
307 |
return; |
308 |
|
309 |
//var name = CurrentAccount.AccountName; |
310 |
var AuthToken = credentials.Password; |
311 |
var name = PithosAccount.GetUserID(new Uri(CurrentAccount.ServerUrl), AuthToken); |
312 |
//MessageBox.Show(CurrentAccount.ServerUrl); |
313 |
//The server will return credentials for a different account, not just the current account |
314 |
//We need to find the correct account first |
315 |
AccountViewModel account = null; |
316 |
Guid token; |
317 |
if (Guid.TryParse(name, out token)) |
318 |
{ |
319 |
account = |
320 |
Accounts.First( |
321 |
act => |
322 |
act.AccountName == name && act.ServerUrl == CurrentAccount.ServerUrl); |
323 |
} |
324 |
else |
325 |
{ |
326 |
account = |
327 |
Accounts.First( |
328 |
act => act.AccountName == name && act.ServerUrl == CurrentAccount.ServerUrl); |
329 |
account.AccountName = credentials.UserName; |
330 |
account.DisplayName = name; |
331 |
Shell.RemoveMonitor(account.ServerUrl, name); |
332 |
CurrentAccount.AccountName = credentials.UserName; |
333 |
CurrentAccount.DisplayName = name; |
334 |
await Shell.MonitorAccount(account.Account); |
335 |
} |
336 |
account.ApiKey = credentials.Password; |
337 |
account.IsExpired = false; |
338 |
Settings.Save(); |
339 |
TaskEx.Delay(10000).ContinueWith(_ => Shell.MonitorAccount(account.Account)); |
340 |
NotifyOfPropertyChange(() => Accounts); |
341 |
} |
342 |
// if (Guid.TryParse(name, out token)) |
343 |
// { |
344 |
// account = |
345 |
// Accounts.First( |
346 |
// act => |
347 |
// act.AccountName == credentials.UserName && act.ServerUrl == CurrentAccount.ServerUrl); |
348 |
// } |
349 |
// else |
350 |
// { |
351 |
// account = |
352 |
// Accounts.First( |
353 |
// act => act.AccountName == name && act.ServerUrl == CurrentAccount.ServerUrl); |
354 |
// account.AccountName = credentials.UserName; |
355 |
// account.DisplayName = name; |
356 |
// Shell.RemoveMonitor(account.ServerUrl, name); |
357 |
// CurrentAccount.AccountName = credentials.UserName; |
358 |
// CurrentAccount.DisplayName = name; |
359 |
// await Shell.MonitorAccount(account.Account); |
360 |
// } |
361 |
// account.ApiKey = credentials.Password; |
362 |
// account.IsExpired = false; |
363 |
// Settings.Save(); |
364 |
// TaskEx.Delay(10000).ContinueWith(_ => Shell.MonitorAccount(account.Account)); |
365 |
// NotifyOfPropertyChange(() => Accounts); |
366 |
//} |
367 |
catch (AggregateException exc) |
368 |
{ |
369 |
string message = String.Format("API Key retrieval failed"); |
370 |
Log.Error(message, exc.InnerException); |
371 |
_events.Publish(new Notification |
372 |
{Title = "Authorization failed", Message = message, Level = TraceLevel.Error}); |
373 |
} |
374 |
catch (Exception exc) |
375 |
{ |
376 |
string message = String.Format("API Key retrieval failed"); |
377 |
Log.Error(message, exc); |
378 |
_events.Publish(new Notification |
379 |
{Title = "Authorization failed", Message = message, Level = TraceLevel.Error}); |
380 |
} |
381 |
} |
382 |
} |
383 |
|
384 |
public bool CanWipeAccount |
385 |
{ |
386 |
get { return CurrentAccount != null |
387 |
&& Shell.Monitors.ContainsKey(CurrentAccount.AccountKey) |
388 |
&& Shell.Monitors[CurrentAccount.AccountKey].CloudClient!=null; |
389 |
} |
390 |
} |
391 |
|
392 |
public async Task WipeAccount() |
393 |
{ |
394 |
PithosMonitor aMonitor; |
395 |
if (Shell.Monitors.TryGetValue(CurrentAccount.AccountKey, out aMonitor)) |
396 |
{ |
397 |
if (aMonitor.CloudClient == null) |
398 |
{ |
399 |
Log.ErrorFormat("Tried to wipe account [{0}] before authenticating",CurrentAccount.AccountKey); |
400 |
return; |
401 |
} |
402 |
var message = String.Format("You are about to wipe all data in the {0} account at {1}. Are you sure?", |
403 |
CurrentAccount.AccountName, CurrentAccount.ServerUrl); |
404 |
if (MessageBoxResult.Yes != |
405 |
MessageBox.Show(message, "Warning! Account data will be wiped", MessageBoxButton.YesNo, |
406 |
MessageBoxImage.Hand, MessageBoxResult.No)) |
407 |
return; |
408 |
|
409 |
using(GetBusyGuard()) |
410 |
{ |
411 |
|
412 |
await aMonitor.CloudClient.WipeContainer("", new Uri("pithos", UriKind.Relative)); |
413 |
aMonitor.Stop(); |
414 |
await aMonitor.Start(); |
415 |
MessageBox.Show("Account wiped"); |
416 |
} |
417 |
|
418 |
} |
419 |
} |
420 |
|
421 |
|
422 |
public void OpenLogPath() |
423 |
{ |
424 |
Shell.OpenLogPath(); |
425 |
} |
426 |
|
427 |
public void OpenLogConsole() |
428 |
{ |
429 |
var logView = IoC.Get<LogConsole.LogConsoleViewModel>(); |
430 |
_windowManager.ShowWindow(logView); |
431 |
} |
432 |
|
433 |
public async Task SaveChanges() |
434 |
{ |
435 |
using (GetBusyGuard()) |
436 |
{ |
437 |
|
438 |
await DoSave(); |
439 |
TryClose( /*true*/); |
440 |
} |
441 |
} |
442 |
|
443 |
public void RejectChanges() |
444 |
{ |
445 |
Settings.Reload(); |
446 |
TryClose( /*false*/); |
447 |
} |
448 |
|
449 |
public async Task ApplyChanges() |
450 |
{ |
451 |
using (GetBusyGuard()) |
452 |
{ |
453 |
await DoSave(); |
454 |
} |
455 |
} |
456 |
|
457 |
private async Task DoSave() |
458 |
{ |
459 |
//SetStartupMode(); |
460 |
|
461 |
//Ensure we save the settings changes first |
462 |
foreach (var account in _accountsToRemove) |
463 |
{ |
464 |
Settings.Accounts.Remove(account); |
465 |
} |
466 |
|
467 |
foreach (var account in _accountsToAdd) |
468 |
{ |
469 |
Settings.Accounts.Add(account); |
470 |
} |
471 |
|
472 |
Settings.Save(); |
473 |
|
474 |
|
475 |
try |
476 |
{ |
477 |
foreach (var account in _accountsToRemove) |
478 |
{ |
479 |
Shell.RemoveMonitor(account.ServerUrl, account.AccountName); |
480 |
Shell.RemoveAccountFromDatabase(account); |
481 |
} |
482 |
|
483 |
foreach (var account in Settings.Accounts) |
484 |
{ |
485 |
await Shell.MonitorAccount(account); |
486 |
} |
487 |
|
488 |
var poller = IoC.Get<PollAgent>(); |
489 |
poller.SynchNow(); |
490 |
} |
491 |
finally |
492 |
{ |
493 |
_accountsToRemove.Clear(); |
494 |
_accountsToAdd.Clear(); |
495 |
} |
496 |
|
497 |
NotifyOfPropertyChange(() => Settings); |
498 |
|
499 |
if (IgnoreCertificateErrors) |
500 |
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true; |
501 |
else |
502 |
{ |
503 |
ServicePointManager.ServerCertificateValidationCallback = null; |
504 |
} |
505 |
} |
506 |
|
507 |
/* public void ChangePithosFolder() |
508 |
{ |
509 |
var browser = new FolderBrowserDialog(); |
510 |
browser.SelectedPath = Settings.PithosPath; |
511 |
var result = browser.ShowDialog((IWin32Window)GetView()); |
512 |
if (result == DialogResult.OK) |
513 |
{ |
514 |
var newPath = browser.SelectedPath; |
515 |
var accountName = CurrentAccount.AccountName; |
516 |
var monitor = Shell.Monitors[accountName]; |
517 |
monitor.Stop(); |
518 |
|
519 |
Shell.Monitors.Remove(accountName); |
520 |
|
521 |
Directory.Move(Settings.PithosPath, newPath); |
522 |
Settings.PithosPath = newPath; |
523 |
Settings.Save(); |
524 |
|
525 |
Shell.MonitorAccount(CurrentAccount); |
526 |
|
527 |
NotifyOfPropertyChange(() => Settings); |
528 |
} |
529 |
} |
530 |
*/ |
531 |
|
532 |
|
533 |
private readonly List<AccountSettings> _accountsToAdd = new List<AccountSettings>(); |
534 |
|
535 |
public async Task AddAccount() |
536 |
{ |
537 |
var wizard = new AddAccountViewModel(); |
538 |
if (_windowManager.ShowDialog(wizard) == true) |
539 |
{ |
540 |
try |
541 |
{ |
542 |
string selectedPath = wizard.AccountPath; |
543 |
var initialRootPath = wizard.ShouldCreateOkeanosFolder |
544 |
? Path.Combine(selectedPath, "okeanos") |
545 |
: selectedPath; |
546 |
var actualRootPath = initialRootPath; |
547 |
if (wizard.ShouldCreateOkeanosFolder) |
548 |
{ |
549 |
int attempt = 1; |
550 |
while (Directory.Exists(actualRootPath) || File.Exists(actualRootPath)) |
551 |
{ |
552 |
actualRootPath = String.Format("{0} {1}", initialRootPath, attempt++); |
553 |
} |
554 |
Directory.CreateDirectory(actualRootPath); |
555 |
} |
556 |
|
557 |
|
558 |
|
559 |
var account = |
560 |
Accounts.FirstOrDefault( |
561 |
act => act.AccountName == wizard.AccountName && act.ServerUrl == wizard.CurrentServer); |
562 |
if (account != null) |
563 |
{ |
564 |
if ( |
565 |
MessageBox.Show("The account you specified already exists. Do you want to update it?", |
566 |
"The account exists") == MessageBoxResult.Yes) |
567 |
{ |
568 |
account.ApiKey = wizard.Token; |
569 |
account.IsExpired = false; |
570 |
CurrentAccount = account; |
571 |
} |
572 |
} |
573 |
else |
574 |
{ |
575 |
var newAccount = new AccountSettings |
576 |
{ |
577 |
AccountName = wizard.AccountName, |
578 |
ServerUrl = wizard.CurrentServer, |
579 |
ApiKey = wizard.Token, |
580 |
RootPath = actualRootPath, |
581 |
IsActive = wizard.IsAccountActive, |
582 |
}; |
583 |
|
584 |
|
585 |
var client = new CloudFilesClient(newAccount.AccountName, newAccount.ApiKey) |
586 |
{ |
587 |
AuthenticationUrl = newAccount.ServerUrl, |
588 |
UsePithos = true |
589 |
}; |
590 |
await client.Authenticate().ConfigureAwait(false); |
591 |
|
592 |
Guid token; |
593 |
if (Guid.TryParse(wizard.AccountName, out token)) |
594 |
{ |
595 |
newAccount.AccountToken = token; |
596 |
newAccount.DisplayName = await client.ResolveName(token).ConfigureAwait(false); |
597 |
} |
598 |
|
599 |
|
600 |
await InitializeSelectiveFolders(newAccount, client); |
601 |
|
602 |
|
603 |
//TODO:Add the "pithos" container as a default selection |
604 |
|
605 |
_accountsToAdd.Add(newAccount); |
606 |
var accountVm = new AccountViewModel(newAccount); |
607 |
(Accounts as IProducerConsumerCollection<AccountViewModel>).TryAdd(accountVm); |
608 |
CurrentAccount = accountVm; |
609 |
} |
610 |
NotifyOfPropertyChange(() => Accounts); |
611 |
NotifyOfPropertyChange(() => Settings); |
612 |
} |
613 |
catch (WebException exc) |
614 |
{ |
615 |
Log.ErrorFormat("[Add Account] Connectivity Error: {0}", exc); |
616 |
MessageBox.Show("Unable to connect to Pithos. Please try again later", "Connectivity Error", |
617 |
MessageBoxButton.OK, MessageBoxImage.Exclamation); |
618 |
} |
619 |
} |
620 |
|
621 |
|
622 |
|
623 |
} |
624 |
|
625 |
private async Task InitializeSelectiveFolders(AccountSettings newAccount, CloudFilesClient client, |
626 |
int retries = 3) |
627 |
{ |
628 |
while (true) |
629 |
{ |
630 |
try |
631 |
{ |
632 |
|
633 |
|
634 |
var containers = await client.ListContainers(newAccount.AccountName).ConfigureAwait(false); |
635 |
var containerUris = from container in containers |
636 |
select String.Format(@"{0}/v1/{1}/{2}", |
637 |
newAccount.ServerUrl, newAccount.AccountName, |
638 |
container.Name); |
639 |
lock (newAccount.SelectiveFolders) |
640 |
{ |
641 |
newAccount.SelectiveFolders.AddRange(containerUris.ToArray()); |
642 |
} |
643 |
|
644 |
var objectInfos = (from container in containers |
645 |
from dir in client.ListObjects(newAccount.AccountName, container.Name) |
646 |
where container.Name.ToString() != "trash" |
647 |
select dir).ToList(); |
648 |
var tree = objectInfos.ToTree(); |
649 |
|
650 |
var selected = (from root in tree |
651 |
from child in root |
652 |
select child.Uri.ToString()).ToArray(); |
653 |
lock (newAccount.SelectiveFolders) |
654 |
{ |
655 |
newAccount.SelectiveFolders.AddRange(selected); |
656 |
} |
657 |
return; |
658 |
} |
659 |
catch (WebException) |
660 |
{ |
661 |
if (retries > 0) |
662 |
retries--; |
663 |
else |
664 |
throw; |
665 |
} |
666 |
} |
667 |
} |
668 |
|
669 |
/* |
670 |
public void AddPithosAccount() |
671 |
{ |
672 |
var credentials=PithosAccount.RetrieveCredentials(null); |
673 |
if (credentials == null) |
674 |
return; |
675 |
var account = Settings.Accounts.FirstOrDefault(act => act.AccountName == credentials.UserName); |
676 |
var accountVM = new AccountViewModel(account); |
677 |
if (account == null) |
678 |
{ |
679 |
account=new AccountSettings{ |
680 |
AccountName=credentials.UserName, |
681 |
ApiKey=credentials.Password |
682 |
}; |
683 |
Settings.Accounts.Add(account); |
684 |
accountVM = new AccountViewModel(account); |
685 |
(Accounts as IProducerConsumerCollection<AccountViewModel>).TryAdd(accountVM); |
686 |
} |
687 |
else |
688 |
{ |
689 |
account.ApiKey=credentials.Password; |
690 |
} |
691 |
//SelectedAccountIndex= Settings.Accounts.IndexOf(account); |
692 |
CurrentAccount = accountVM; |
693 |
NotifyOfPropertyChange(() => Accounts); |
694 |
NotifyOfPropertyChange(()=>Settings); |
695 |
} |
696 |
*/ |
697 |
|
698 |
|
699 |
private readonly List<AccountSettings> _accountsToRemove = new List<AccountSettings>(); |
700 |
|
701 |
public void RemoveAccount() |
702 |
{ |
703 |
Accounts.TryRemove(CurrentAccount); |
704 |
_accountsToRemove.Add(CurrentAccount.Account); |
705 |
|
706 |
CurrentAccount = null; |
707 |
NotifyOfPropertyChange(() => Accounts); |
708 |
|
709 |
|
710 |
//NotifyOfPropertyChange("Settings.Accounts"); |
711 |
} |
712 |
|
713 |
public bool CanRemoveAccount |
714 |
{ |
715 |
get { return (CurrentAccount != null); } |
716 |
} |
717 |
|
718 |
public bool CanClearAccountCache |
719 |
{ |
720 |
get { return (CurrentAccount != null); } |
721 |
} |
722 |
|
723 |
public void ClearAccountCache() |
724 |
{ |
725 |
if (MessageBoxResult.Yes == |
726 |
MessageBox.Show("You are about to delete all partially downloaded files from the account's cache.\n" + |
727 |
" You will have to download all partially downloaded data again\n" + |
728 |
"This change can not be undone\n\n" + |
729 |
"Do you wish to delete all partially downloaded data?", |
730 |
"Warning! Clearing account cache", |
731 |
MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No)) |
732 |
{ |
733 |
|
734 |
var cachePath = Path.Combine(CurrentAccount.RootPath, FolderConstants.CacheFolder); |
735 |
var dir = new DirectoryInfo(cachePath); |
736 |
//The file may not exist if we just created the account |
737 |
if (!dir.Exists) |
738 |
return; |
739 |
dir.EnumerateFiles().Apply(file => file.Delete()); |
740 |
dir.EnumerateDirectories().Apply(folder => folder.Delete(true)); |
741 |
} |
742 |
} |
743 |
|
744 |
|
745 |
public bool ExtensionsActivated |
746 |
{ |
747 |
get { return Settings.ExtensionsActivated; } |
748 |
set |
749 |
{ |
750 |
if (Settings.ExtensionsActivated == value) |
751 |
return; |
752 |
|
753 |
Settings.ExtensionsActivated = value; |
754 |
|
755 |
/* |
756 |
if (value) |
757 |
_extensionController.RegisterExtensions(); |
758 |
else |
759 |
{ |
760 |
_extensionController.UnregisterExtensions(); |
761 |
} |
762 |
*/ |
763 |
NotifyOfPropertyChange(() => ExtensionsActivated); |
764 |
} |
765 |
} |
766 |
|
767 |
public bool DebugLoggingEnabled |
768 |
{ |
769 |
get { return Settings.DebugLoggingEnabled; } |
770 |
set |
771 |
{ |
772 |
Settings.DebugLoggingEnabled = value; |
773 |
NotifyOfPropertyChange(() => DebugLoggingEnabled); |
774 |
} |
775 |
} |
776 |
|
777 |
public bool IgnoreCertificateErrors |
778 |
{ |
779 |
get { return Settings.IgnoreCertificateErrors; } |
780 |
set |
781 |
{ |
782 |
Settings.IgnoreCertificateErrors = value; |
783 |
NotifyOfPropertyChange(() => IgnoreCertificateErrors); |
784 |
} |
785 |
} |
786 |
|
787 |
#endregion |
788 |
|
789 |
/* private int _selectedAccountIndex; |
790 |
public int SelectedAccountIndex |
791 |
{ |
792 |
get { return _selectedAccountIndex; } |
793 |
set |
794 |
{ |
795 |
//var accountCount=Settings.Accounts.Count; |
796 |
//if (accountCount == 0) |
797 |
// return; |
798 |
//if (0 <= value && value < accountCount) |
799 |
// _selectedAccountIndex = value; |
800 |
//else |
801 |
// _selectedAccountIndex = 0; |
802 |
_selectedAccountIndex = value; |
803 |
NotifyOfPropertyChange(() => CurrentAccount); |
804 |
NotifyOfPropertyChange(() => CanRemoveAccount); |
805 |
NotifyOfPropertyChange(()=>SelectedAccountIndex); |
806 |
} |
807 |
}*/ |
808 |
|
809 |
private AccountViewModel _currentAccount; |
810 |
private readonly IWindowManager _windowManager; |
811 |
private readonly string _shortcutPath; |
812 |
private bool _isBusy; |
813 |
|
814 |
|
815 |
public AccountViewModel CurrentAccount |
816 |
{ |
817 |
get { return _currentAccount; } |
818 |
set |
819 |
{ |
820 |
_currentAccount = value; |
821 |
|
822 |
if (_currentAccount != null) |
823 |
_currentAccount.PropertyChanged += (o, e) => NotifyOfPropertyChange(() => CanSelectiveSyncFolders); |
824 |
|
825 |
NotifyOfPropertyChange(() => CurrentAccount); |
826 |
NotifyOfPropertyChange(() => CanRemoveAccount); |
827 |
NotifyOfPropertyChange(() => CanSelectiveSyncFolders); |
828 |
NotifyOfPropertyChange(() => CanMoveAccountFolder); |
829 |
NotifyOfPropertyChange(() => CanClearAccountCache); |
830 |
NotifyOfPropertyChange(() => CanWipeAccount); |
831 |
} |
832 |
} |
833 |
|
834 |
/* |
835 |
public AccountSettings CurrentAccount |
836 |
{ |
837 |
get { |
838 |
if (0 <= SelectedAccountIndex && SelectedAccountIndex < Settings.Accounts.Count) |
839 |
return Settings.Accounts[SelectedAccountIndex]; |
840 |
return null; |
841 |
} |
842 |
|
843 |
} |
844 |
*/ |
845 |
|
846 |
|
847 |
public bool CanMoveAccountFolder |
848 |
{ |
849 |
get { return CurrentAccount != null; } |
850 |
} |
851 |
|
852 |
public async Task MoveAccountFolder() |
853 |
{ |
854 |
|
855 |
using (var dlg = new FolderBrowserDialog()) |
856 |
{ |
857 |
var currentFolder = CurrentAccount.RootPath; |
858 |
dlg.SelectedPath = currentFolder; |
859 |
//Ask the user to select a folder |
860 |
//Note: We need a parent window here, which we retrieve with GetView |
861 |
var view = (Window) GetView(); |
862 |
if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view))) |
863 |
return; |
864 |
|
865 |
var newPath = dlg.SelectedPath; |
866 |
//Find the account's monitor and stop it |
867 |
PithosMonitor monitor; |
868 |
if (Shell.Monitors.TryGetValue(CurrentAccount.AccountKey, out monitor)) |
869 |
{ |
870 |
//Problem: Doesn't stop the poll agent |
871 |
monitor.Stop(); |
872 |
|
873 |
monitor.MoveRootFolder(newPath); |
874 |
|
875 |
|
876 |
} |
877 |
//Replace the old rootpath with the new |
878 |
CurrentAccount.RootPath = newPath; |
879 |
//TODO: This will save all settings changes. Too coarse grained, need to fix at a later date |
880 |
Settings.Save(); |
881 |
//And start the monitor on the new RootPath |
882 |
if (monitor != null) |
883 |
{ |
884 |
monitor.RootPath = newPath; |
885 |
if (CurrentAccount.IsActive) |
886 |
await Shell.StartMonitor(monitor); |
887 |
|
888 |
} |
889 |
else |
890 |
await Shell.MonitorAccount(CurrentAccount.Account); |
891 |
//Finally, notify that the Settings, CurrentAccount have changed |
892 |
NotifyOfPropertyChange(() => CurrentAccount); |
893 |
NotifyOfPropertyChange(() => Settings); |
894 |
|
895 |
} |
896 |
} |
897 |
|
898 |
} |
899 |
|
900 |
public class DisposeGuard : IDisposable |
901 |
{ |
902 |
private readonly System.Action _disposeCall; |
903 |
|
904 |
public DisposeGuard(System.Action start, System.Action dispose) |
905 |
{ |
906 |
_disposeCall = dispose; |
907 |
start(); |
908 |
} |
909 |
|
910 |
public void Dispose() |
911 |
{ |
912 |
_disposeCall(); |
913 |
GC.SuppressFinalize(this); |
914 |
} |
915 |
} |
916 |
} |