root / trunk / Pithos.Client.WPF / Preferences / PreferencesViewModel.cs @ 255f5f86
History | View | Annotate | Download (15 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 |
// </copyright> |
44 |
// ----------------------------------------------------------------------- |
45 |
|
46 |
using System.Collections.Concurrent; |
47 |
using System.ComponentModel.Composition; |
48 |
using System.IO; |
49 |
using System.Net; |
50 |
using System.Windows; |
51 |
using System.Windows.Forms; |
52 |
using Caliburn.Micro; |
53 |
using Pithos.Client.WPF.Configuration; |
54 |
using Pithos.Client.WPF.SelectiveSynch; |
55 |
using Pithos.Core; |
56 |
using Pithos.Interfaces; |
57 |
using System; |
58 |
using System.Linq; |
59 |
using Screen = Caliburn.Micro.Screen; |
60 |
|
61 |
namespace Pithos.Client.WPF.Preferences |
62 |
{ |
63 |
/// <summary> |
64 |
/// TODO: Update summary. |
65 |
/// </summary> |
66 |
[Export] |
67 |
public class PreferencesViewModel : Screen |
68 |
{ |
69 |
private IEventAggregator _events; |
70 |
|
71 |
|
72 |
private PithosSettings _settings; |
73 |
public PithosSettings Settings |
74 |
{ |
75 |
get { return _settings; } |
76 |
set |
77 |
{ |
78 |
_settings = value; |
79 |
NotifyOfPropertyChange(()=>Settings); |
80 |
} |
81 |
} |
82 |
|
83 |
private ObservableConcurrentCollection<AccountSettings> _accounts; |
84 |
public ObservableConcurrentCollection<AccountSettings> Accounts |
85 |
{ |
86 |
get { return _accounts; } |
87 |
set |
88 |
{ |
89 |
_accounts = value; |
90 |
NotifyOfPropertyChange(()=>Accounts); |
91 |
} |
92 |
} |
93 |
|
94 |
public bool StartOnSystemStartup { get; set; } |
95 |
|
96 |
public ShellViewModel Shell { get; set; } |
97 |
//ShellExtensionController _extensionController=new ShellExtensionController(); |
98 |
|
99 |
public PreferencesViewModel(IWindowManager windowManager, IEventAggregator events, ShellViewModel shell, PithosSettings settings) |
100 |
{ |
101 |
_windowManager = windowManager; |
102 |
_events = events; |
103 |
|
104 |
DisplayName = "Pithos Preferences"; |
105 |
Shell = shell; |
106 |
|
107 |
Settings=settings; |
108 |
Accounts = new ObservableConcurrentCollection<AccountSettings>(); |
109 |
if (settings.Accounts == null) |
110 |
{ |
111 |
settings.Accounts=new AccountsCollection(); |
112 |
settings.Save(); |
113 |
} |
114 |
Accounts.AddFromEnumerable(settings.Accounts); |
115 |
|
116 |
var startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); |
117 |
_shortcutPath = Path.Combine(startupPath, "Pithos.lnk"); |
118 |
|
119 |
|
120 |
StartOnSystemStartup = File.Exists(_shortcutPath); |
121 |
|
122 |
} |
123 |
|
124 |
|
125 |
#region Preferences Properties |
126 |
|
127 |
private bool _noProxy; |
128 |
public bool NoProxy |
129 |
{ |
130 |
get { return _noProxy; } |
131 |
set |
132 |
{ |
133 |
_noProxy = value; |
134 |
NotifyOfPropertyChange(()=>NoProxy); |
135 |
} |
136 |
} |
137 |
|
138 |
|
139 |
private bool _defaultProxy; |
140 |
|
141 |
public bool DefaultProxy |
142 |
{ |
143 |
get { return _defaultProxy; } |
144 |
set |
145 |
{ |
146 |
_defaultProxy = value; |
147 |
NotifyOfPropertyChange(() => DefaultProxy); |
148 |
} |
149 |
} |
150 |
|
151 |
|
152 |
private bool _manualProxy; |
153 |
|
154 |
public bool ManualProxy |
155 |
{ |
156 |
get { return _manualProxy; } |
157 |
set |
158 |
{ |
159 |
_manualProxy = value; |
160 |
NotifyOfPropertyChange(() => ManualProxy); |
161 |
} |
162 |
} |
163 |
#endregion |
164 |
|
165 |
|
166 |
#region Commands |
167 |
|
168 |
public bool CanSelectiveSyncFolders |
169 |
{ |
170 |
get { return CurrentAccount != null; } |
171 |
} |
172 |
|
173 |
public void SelectiveSyncFolders() |
174 |
{ |
175 |
var monitor = Shell.Monitors[CurrentAccount.AccountName]; |
176 |
var folders=monitor.GetRootFolders(); |
177 |
|
178 |
var model = new SelectiveSynchViewModel(folders,_events,CurrentAccount); |
179 |
if (_windowManager.ShowDialog(model) == true) |
180 |
{ |
181 |
|
182 |
} |
183 |
} |
184 |
|
185 |
public void SaveChanges() |
186 |
{ |
187 |
DoSave(); |
188 |
TryClose(true); |
189 |
} |
190 |
|
191 |
public void RejectChanges() |
192 |
{ |
193 |
Settings.Reload(); |
194 |
TryClose(false); |
195 |
} |
196 |
|
197 |
public void ApplyChanges() |
198 |
{ |
199 |
DoSave(); |
200 |
} |
201 |
|
202 |
private void DoSave() |
203 |
{ |
204 |
Settings.Save(); |
205 |
//SetStartupMode(); |
206 |
|
207 |
foreach (var account in Settings.Accounts) |
208 |
{ |
209 |
Shell.MonitorAccount(account); |
210 |
} |
211 |
|
212 |
NotifyOfPropertyChange(()=>Settings); |
213 |
} |
214 |
|
215 |
/* public void ChangePithosFolder() |
216 |
{ |
217 |
var browser = new FolderBrowserDialog(); |
218 |
browser.SelectedPath = Settings.PithosPath; |
219 |
var result = browser.ShowDialog((IWin32Window)GetView()); |
220 |
if (result == DialogResult.OK) |
221 |
{ |
222 |
var newPath = browser.SelectedPath; |
223 |
var accountName = CurrentAccount.AccountName; |
224 |
var monitor = Shell.Monitors[accountName]; |
225 |
monitor.Stop(); |
226 |
|
227 |
Shell.Monitors.Remove(accountName); |
228 |
|
229 |
Directory.Move(Settings.PithosPath, newPath); |
230 |
Settings.PithosPath = newPath; |
231 |
Settings.Save(); |
232 |
|
233 |
Shell.MonitorAccount(CurrentAccount); |
234 |
|
235 |
NotifyOfPropertyChange(() => Settings); |
236 |
} |
237 |
} |
238 |
*/ |
239 |
|
240 |
|
241 |
|
242 |
public void AddAccount() |
243 |
{ |
244 |
var wizard = new AddAccountViewModel(); |
245 |
if (_windowManager.ShowDialog(wizard) == true) |
246 |
{ |
247 |
string selectedPath = wizard.AccountPath; |
248 |
var initialRootPath = Path.Combine(selectedPath, "Okeanos"); |
249 |
var actualRootPath= initialRootPath; |
250 |
int attempt = 1; |
251 |
while (Directory.Exists(actualRootPath) || File.Exists(actualRootPath)) |
252 |
{ |
253 |
actualRootPath = String.Format("{0} {1}", initialRootPath,attempt++); |
254 |
} |
255 |
|
256 |
var newAccount = new AccountSettings |
257 |
{ |
258 |
AccountName = wizard.AccountName, |
259 |
ServerUrl=wizard.CurrentServer, |
260 |
ApiKey=wizard.Token, |
261 |
RootPath = actualRootPath, |
262 |
IsActive=wizard.IsAccountActive |
263 |
}; |
264 |
Settings.Accounts.Add(newAccount); |
265 |
(Accounts as IProducerConsumerCollection<AccountSettings>).TryAdd(newAccount); |
266 |
CurrentAccount = newAccount; |
267 |
NotifyOfPropertyChange(() => Accounts); |
268 |
NotifyOfPropertyChange(() => Settings); |
269 |
} |
270 |
|
271 |
|
272 |
|
273 |
} |
274 |
|
275 |
public async void AddPithosAccount() |
276 |
{ |
277 |
var credentials=await PithosAccount.RetrieveCredentials(Settings.PithosLoginUrl); |
278 |
var account = Settings.Accounts.FirstOrDefault(act => act.AccountName == credentials.UserName); |
279 |
if (account == null) |
280 |
{ |
281 |
account=new AccountSettings{ |
282 |
AccountName=credentials.UserName, |
283 |
ApiKey=credentials.Password |
284 |
}; |
285 |
Settings.Accounts.Add(account); |
286 |
(Accounts as IProducerConsumerCollection<AccountSettings>).TryAdd(account); |
287 |
} |
288 |
else |
289 |
{ |
290 |
account.ApiKey=credentials.Password; |
291 |
} |
292 |
//SelectedAccountIndex= Settings.Accounts.IndexOf(account); |
293 |
CurrentAccount = account; |
294 |
NotifyOfPropertyChange(() => Accounts); |
295 |
NotifyOfPropertyChange(()=>Settings); |
296 |
} |
297 |
|
298 |
public void RemoveAccount() |
299 |
{ |
300 |
var accountName = CurrentAccount.AccountName; |
301 |
Settings.Accounts.Remove(CurrentAccount); |
302 |
|
303 |
Accounts.TryRemove(CurrentAccount); |
304 |
|
305 |
|
306 |
CurrentAccount = null; |
307 |
//Accounts = Settings.Accounts; |
308 |
//Settings.Save(); |
309 |
Shell.RemoveMonitor(accountName); |
310 |
NotifyOfPropertyChange(() => Accounts); |
311 |
NotifyOfPropertyChange(() => Settings); |
312 |
|
313 |
//NotifyOfPropertyChange("Settings.Accounts"); |
314 |
} |
315 |
|
316 |
public bool CanRemoveAccount |
317 |
{ |
318 |
get { return (CurrentAccount != null); } |
319 |
} |
320 |
|
321 |
|
322 |
|
323 |
public bool ExtensionsActivated |
324 |
{ |
325 |
get { return Settings.ExtensionsActivated; } |
326 |
set |
327 |
{ |
328 |
if (Settings.ExtensionsActivated == value) |
329 |
return; |
330 |
|
331 |
Settings.ExtensionsActivated = value; |
332 |
|
333 |
/* |
334 |
if (value) |
335 |
_extensionController.RegisterExtensions(); |
336 |
else |
337 |
{ |
338 |
_extensionController.UnregisterExtensions(); |
339 |
} |
340 |
*/ |
341 |
NotifyOfPropertyChange(() => ExtensionsActivated); |
342 |
} |
343 |
} |
344 |
|
345 |
|
346 |
#endregion |
347 |
|
348 |
/* private int _selectedAccountIndex; |
349 |
public int SelectedAccountIndex |
350 |
{ |
351 |
get { return _selectedAccountIndex; } |
352 |
set |
353 |
{ |
354 |
//var accountCount=Settings.Accounts.Count; |
355 |
//if (accountCount == 0) |
356 |
// return; |
357 |
//if (0 <= value && value < accountCount) |
358 |
// _selectedAccountIndex = value; |
359 |
//else |
360 |
// _selectedAccountIndex = 0; |
361 |
_selectedAccountIndex = value; |
362 |
NotifyOfPropertyChange(() => CurrentAccount); |
363 |
NotifyOfPropertyChange(() => CanRemoveAccount); |
364 |
NotifyOfPropertyChange(()=>SelectedAccountIndex); |
365 |
} |
366 |
}*/ |
367 |
|
368 |
private AccountSettings _currentAccount; |
369 |
private IWindowManager _windowManager; |
370 |
private string _shortcutPath; |
371 |
|
372 |
|
373 |
|
374 |
public AccountSettings CurrentAccount |
375 |
{ |
376 |
get { return _currentAccount; } |
377 |
set |
378 |
{ |
379 |
_currentAccount = value; |
380 |
NotifyOfPropertyChange(()=>CurrentAccount); |
381 |
NotifyOfPropertyChange(() => CanRemoveAccount); |
382 |
NotifyOfPropertyChange(() => CanSelectiveSyncFolders); |
383 |
NotifyOfPropertyChange(() => CanMoveAccountFolder); |
384 |
} |
385 |
} |
386 |
|
387 |
/* |
388 |
public AccountSettings CurrentAccount |
389 |
{ |
390 |
get { |
391 |
if (0 <= SelectedAccountIndex && SelectedAccountIndex < Settings.Accounts.Count) |
392 |
return Settings.Accounts[SelectedAccountIndex]; |
393 |
return null; |
394 |
} |
395 |
|
396 |
} |
397 |
*/ |
398 |
|
399 |
|
400 |
public bool CanMoveAccountFolder |
401 |
{ |
402 |
get { return CurrentAccount != null; } |
403 |
} |
404 |
|
405 |
public void MoveAccountFolder() |
406 |
{ |
407 |
|
408 |
using (var dlg = new FolderBrowserDialog()) |
409 |
{ |
410 |
var currentFolder = CurrentAccount.RootPath; |
411 |
dlg.SelectedPath = currentFolder; |
412 |
//Ask the user to select a folder |
413 |
//Note: We need a parent window here, which we retrieve with GetView |
414 |
var view = (Window)GetView(); |
415 |
if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view))) |
416 |
return; |
417 |
|
418 |
var newPath= dlg.SelectedPath; |
419 |
//Find the account's monitor and stop it |
420 |
PithosMonitor monitor; |
421 |
if (Shell.Monitors.TryGetValue(CurrentAccount.AccountName, out monitor)) |
422 |
{ |
423 |
monitor.Stop(); |
424 |
|
425 |
|
426 |
var oldPath = monitor.RootPath; |
427 |
//The old directory may not exist eg. if we create an account for the first time |
428 |
if (Directory.Exists(oldPath)) |
429 |
{ |
430 |
//If it does, do the move |
431 |
|
432 |
//Now Create all of the directories |
433 |
foreach (string dirPath in Directory.EnumerateDirectories(oldPath, "*", |
434 |
SearchOption.AllDirectories)) |
435 |
Directory.CreateDirectory(dirPath.Replace(oldPath, newPath)); |
436 |
|
437 |
//Copy all the files |
438 |
foreach (string newFilePath in Directory.EnumerateFiles(oldPath, "*.*", |
439 |
SearchOption.AllDirectories)) |
440 |
File.Copy(newFilePath, newFilePath.Replace(oldPath, newPath)); |
441 |
|
442 |
Directory.Delete(oldPath, true); |
443 |
|
444 |
//We also need to change the path of the existing file states |
445 |
if (monitor != null) |
446 |
monitor.MoveFileStates(oldPath, newPath); |
447 |
} |
448 |
} |
449 |
//Replace the old rootpath with the new |
450 |
CurrentAccount.RootPath = newPath; |
451 |
//TODO: This will save all settings changes. Too coarse grained, need to fix at a later date |
452 |
Settings.Save(); |
453 |
//And start the monitor on the new RootPath |
454 |
if (monitor != null) |
455 |
{ |
456 |
monitor.RootPath = newPath; |
457 |
if (CurrentAccount.IsActive) |
458 |
monitor.Start(); |
459 |
} |
460 |
else |
461 |
Shell.MonitorAccount(CurrentAccount); |
462 |
//Finally, notify that the Settings, CurrentAccount have changed |
463 |
NotifyOfPropertyChange(() => CurrentAccount); |
464 |
NotifyOfPropertyChange(() => Settings); |
465 |
|
466 |
} |
467 |
} |
468 |
} |
469 |
} |