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