Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / Preferences / PreferencesViewModel.cs @ 6aa29f4f

History | View | Annotate | Download (13.9 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
            Accounts.AddFromEnumerable(settings.Accounts);
89
            
90
            var startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
91
            _shortcutPath = Path.Combine(startupPath, "Pithos.lnk");
92

    
93

    
94
            StartOnSystemStartup = File.Exists(_shortcutPath);
95

    
96
        }
97

    
98

    
99
        #region Preferences Properties
100

    
101
        private bool _noProxy;
102
        public bool NoProxy
103
        {
104
            get { return _noProxy; }
105
            set
106
            {
107
                _noProxy = value;
108
                NotifyOfPropertyChange(()=>NoProxy);
109
            }
110
        }
111

    
112

    
113
        private bool _defaultProxy;
114

    
115
        public bool DefaultProxy
116
        {
117
            get { return _defaultProxy; }
118
            set
119
            {
120
                _defaultProxy = value;
121
                NotifyOfPropertyChange(() => DefaultProxy);
122
            }
123
        }
124

    
125

    
126
        private bool _manualProxy;
127

    
128
        public bool ManualProxy
129
        {
130
            get { return _manualProxy; }
131
            set
132
            {
133
                _manualProxy = value;
134
                NotifyOfPropertyChange(() => ManualProxy);
135
            }
136
        }
137
        #endregion
138

    
139
       
140
        #region Commands
141
        
142
        public bool CanSelectiveSyncFolders
143
        {
144
            get { return CurrentAccount != null; }
145
        }
146

    
147
        public void SelectiveSyncFolders()
148
        {
149
            var monitor = Shell.Monitors[CurrentAccount.AccountName];
150
            var folders=monitor.GetRootFolders();
151

    
152
            var model = new SelectiveSynchViewModel(folders,_events,CurrentAccount);
153
            if (_windowManager.ShowDialog(model) == true)
154
            {
155
                
156
            }
157
        }
158
    
159
        public void SaveChanges()
160
        {
161
            DoSave();
162
            TryClose(true);
163
        }
164

    
165
        public void RejectChanges()
166
        {
167
            Settings.Reload();
168
            TryClose(false);
169
        }
170

    
171
        public void ApplyChanges()
172
        {
173
            DoSave();
174
        }
175

    
176
        private void DoSave()
177
        {
178
            Settings.Save();
179
            SetStartupMode();
180

    
181

    
182
            foreach (var account in Settings.Accounts)
183
            {                                
184
                Shell.MonitorAccount(account);
185
            }
186

    
187
            NotifyOfPropertyChange(()=>Settings);
188
        }
189

    
190
        private void SetStartupMode()
191
        {
192
            if (StartOnSystemStartup && !File.Exists(_shortcutPath))
193
            {
194
                CreateShortcut(_shortcutPath);
195
            }
196
            else if (!StartOnSystemStartup && File.Exists(_shortcutPath))
197
            {
198
                if (File.Exists(_shortcutPath))
199
                    File.Delete(_shortcutPath);
200
            }
201
        }
202

    
203
     /*   public void ChangePithosFolder()
204
        {
205
            var browser = new FolderBrowserDialog();
206
            browser.SelectedPath = Settings.PithosPath;
207
            var result = browser.ShowDialog((IWin32Window)GetView());
208
            if (result == DialogResult.OK)
209
            {
210
                var newPath = browser.SelectedPath;
211
                var accountName = CurrentAccount.AccountName;
212
                var monitor = Shell.Monitors[accountName];
213
                monitor.Stop();
214
                
215
                Shell.Monitors.Remove(accountName);
216

    
217
                Directory.Move(Settings.PithosPath, newPath);
218
                Settings.PithosPath = newPath;
219
                Settings.Save();
220

    
221
                Shell.MonitorAccount(CurrentAccount);
222

    
223
                NotifyOfPropertyChange(() => Settings);                
224
            }
225
        }
226
*/
227
       public void AddAccount()
228
       {
229
           var wizard = new AddAccountViewModel();
230
           if (_windowManager.ShowDialog(wizard) == true)
231
           {
232
               var newAccount = new AccountSettings
233
                                    {
234
                                        AccountName = wizard.AccountName,
235
                                        ApiKey=wizard.Token,
236
                                        RootPath=wizard.AccountPath,
237
                                        IsActive=wizard.IsAccountActive,
238
                                        UsePithos=true
239
                                    };
240
               Settings.Accounts.Add(newAccount);
241
               (Accounts as IProducerConsumerCollection<AccountSettings>).TryAdd(newAccount);
242
               CurrentAccount = newAccount;
243
               NotifyOfPropertyChange(() => Accounts);
244
               NotifyOfPropertyChange(() => Settings);   
245
           }
246

    
247

    
248
            
249
       }
250

    
251
        public async void AddPithosAccount()
252
       {
253
            var credentials=await PithosAccount.RetrieveCredentials(Settings.PithosLoginUrl);
254
            var account = Settings.Accounts.FirstOrDefault(act => act.AccountName == credentials.UserName);
255
            if (account == null)
256
            {
257
                account=new AccountSettings{
258
                    AccountName=credentials.UserName,
259
                    ApiKey=credentials.Password,
260
                    UsePithos=true
261
                };
262
                Settings.Accounts.Add(account);
263
                (Accounts as IProducerConsumerCollection<AccountSettings>).TryAdd(account);
264
            }
265
            else
266
            {
267
                account.ApiKey=credentials.Password;
268
            }
269
            //SelectedAccountIndex= Settings.Accounts.IndexOf(account);
270
            CurrentAccount = account;
271
            NotifyOfPropertyChange(() => Accounts);
272
            NotifyOfPropertyChange(()=>Settings);                       
273
       }
274

    
275
        public void RemoveAccount()
276
        {
277
            var accountName = CurrentAccount.AccountName;
278
            Settings.Accounts.Remove(CurrentAccount);
279

    
280
            Accounts.TryRemove(CurrentAccount);
281
            
282
            
283
            CurrentAccount = null;
284
            //Accounts = Settings.Accounts;
285
            //Settings.Save();            
286
            Shell.RemoveMonitor(accountName);
287
            NotifyOfPropertyChange(() => Accounts);
288
            NotifyOfPropertyChange(() => Settings);                       
289
            
290
            //NotifyOfPropertyChange("Settings.Accounts");
291
        }
292

    
293
        public bool CanRemoveAccount
294
        {
295
            get { return (CurrentAccount != null); }
296
        }
297

    
298

    
299

    
300
        public bool ExtensionsActivated
301
        {
302
            get { return Settings.ExtensionsActivated; }
303
            set
304
            {
305
                if (Settings.ExtensionsActivated == value)
306
                    return;
307

    
308
                Settings.ExtensionsActivated = value;
309

    
310
/*
311
                if (value)
312
                    _extensionController.RegisterExtensions();
313
                else
314
                {
315
                    _extensionController.UnregisterExtensions();
316
                }
317
*/
318
                NotifyOfPropertyChange(() => ExtensionsActivated);
319
            }
320
        }
321

    
322
       
323
        #endregion
324

    
325
       /* private int _selectedAccountIndex;
326
        public int SelectedAccountIndex
327
        {
328
            get { return _selectedAccountIndex; }
329
            set
330
            {
331
                //var accountCount=Settings.Accounts.Count;
332
                //if (accountCount == 0)
333
                //    return;
334
                //if (0 <= value && value < accountCount)
335
                //    _selectedAccountIndex = value;
336
                //else
337
                //    _selectedAccountIndex = 0;
338
                _selectedAccountIndex = value;
339
                NotifyOfPropertyChange(() => CurrentAccount);
340
                NotifyOfPropertyChange(() => CanRemoveAccount);
341
                NotifyOfPropertyChange(()=>SelectedAccountIndex);
342
            }
343
        }*/
344

    
345
        private AccountSettings _currentAccount;
346
        private IWindowManager _windowManager;
347
        private string _shortcutPath;
348

    
349

    
350
        
351
        public AccountSettings CurrentAccount
352
        {
353
            get { return _currentAccount; }
354
            set
355
            {
356
                _currentAccount = value;
357
                NotifyOfPropertyChange(()=>CurrentAccount);
358
                NotifyOfPropertyChange(() => CanRemoveAccount);
359
                NotifyOfPropertyChange(() => CanSelectiveSyncFolders);
360
                NotifyOfPropertyChange(() => CanMoveAccountFolder);
361
            }
362
        }
363

    
364
/*
365
        public AccountSettings CurrentAccount
366
        {
367
            get {
368
                if (0 <= SelectedAccountIndex && SelectedAccountIndex < Settings.Accounts.Count)                    
369
                    return Settings.Accounts[SelectedAccountIndex];
370
                return null;
371
            }
372

    
373
        }
374
*/
375

    
376

    
377
        public bool CanMoveAccountFolder
378
        {
379
            get { return CurrentAccount != null; }
380
        }
381

    
382
    public void MoveAccountFolder()
383
    {
384

    
385
        using (var dlg = new FolderBrowserDialog())
386
        {
387
            var currentFolder = CurrentAccount.RootPath;
388
            dlg.SelectedPath = currentFolder;
389
            //Ask the user to select a folder
390
            //Note: We need a parent window here, which we retrieve with GetView            
391
            var view = (Window)GetView();            
392
            if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view)))
393
                return;            
394

    
395
            var newPath= dlg.SelectedPath;                
396
            //Find the account's monitor and stop it
397
            PithosMonitor monitor;
398
            if (Shell.Monitors.TryGetValue(CurrentAccount.AccountName, out monitor))
399
            {
400
                monitor.Stop();
401

    
402

    
403
                var oldPath = monitor.RootPath;
404
                //The old directory may not exist eg. if we create an account for the first time
405
                if (Directory.Exists(oldPath))
406
                {
407
                    //If it does, do the move
408

    
409
                    //Now Create all of the directories
410
                    foreach (string dirPath in Directory.EnumerateDirectories(oldPath, "*",
411
                                                           SearchOption.AllDirectories))
412
                        Directory.CreateDirectory(dirPath.Replace(oldPath, newPath));
413

    
414
                    //Copy all the files
415
                    foreach (string newFilePath in Directory.EnumerateFiles(oldPath, "*.*",
416
                                                                            SearchOption.AllDirectories))
417
                        File.Copy(newFilePath, newFilePath.Replace(oldPath, newPath));
418

    
419
                    Directory.Delete(oldPath, true);
420

    
421
                    //We also need to change the path of the existing file states
422
                    if (monitor != null)
423
                        monitor.MoveFileStates(oldPath, newPath);
424
                }
425
            }
426
            //Replace the old rootpath with the new
427
            CurrentAccount.RootPath = newPath;
428
            //TODO: This will save all settings changes. Too coarse grained, need to fix at a later date
429
            Settings.Save();            
430
            //And start the monitor on the new RootPath            
431
            if (monitor != null)
432
            {
433
                monitor.RootPath = newPath;
434
                if (CurrentAccount.IsActive)
435
                    monitor.Start();
436
            }
437
            else
438
                Shell.MonitorAccount(CurrentAccount);
439
            //Finally, notify that the Settings, CurrentAccount have changed
440
            NotifyOfPropertyChange(() => CurrentAccount);
441
            NotifyOfPropertyChange(() => Settings);
442

    
443
        }
444
    }
445
    }
446
}