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