Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / Preferences / PreferencesViewModel.cs @ 20e9a378

History | View | Annotate | Download (12.8 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 void SelectiveSyncFolders()
142
        {
143
            var monitor = Shell.Monitors[CurrentAccount.AccountName];
144
            var folders=monitor.GetRootFolders();
145

    
146
            var model = new SelectiveSynchViewModel(folders,_events,CurrentAccount);
147
            if (_windowManager.ShowDialog(model) == true)
148
            {
149
                
150
            }
151
        }
152
    
153
        public void SaveChanges()
154
        {
155
            DoSave();
156
            TryClose(true);
157
        }
158

    
159
        public void RejectChanges()
160
        {
161
            Settings.Reload();
162
            TryClose(false);
163
        }
164

    
165
        public void ApplyChanges()
166
        {
167
            DoSave();
168
        }
169

    
170
        private void DoSave()
171
        {
172
            Settings.Save();
173
            SetStartupMode();
174

    
175

    
176
            foreach (var account in Settings.Accounts)
177
            {                                
178
                Shell.MonitorAccount(account);
179
            }
180

    
181
            NotifyOfPropertyChange(()=>Settings);
182
        }
183

    
184
        private void SetStartupMode()
185
        {
186
            if (StartOnSystemStartup && !File.Exists(_shortcutPath))
187
            {
188
                CreateShortcut(_shortcutPath);
189
            }
190
            else if (!StartOnSystemStartup && File.Exists(_shortcutPath))
191
            {
192
                if (File.Exists(_shortcutPath))
193
                    File.Delete(_shortcutPath);
194
            }
195
        }
196

    
197
     /*   public void ChangePithosFolder()
198
        {
199
            var browser = new FolderBrowserDialog();
200
            browser.SelectedPath = Settings.PithosPath;
201
            var result = browser.ShowDialog((IWin32Window)GetView());
202
            if (result == DialogResult.OK)
203
            {
204
                var newPath = browser.SelectedPath;
205
                var accountName = CurrentAccount.AccountName;
206
                var monitor = Shell.Monitors[accountName];
207
                monitor.Stop();
208
                
209
                Shell.Monitors.Remove(accountName);
210

    
211
                Directory.Move(Settings.PithosPath, newPath);
212
                Settings.PithosPath = newPath;
213
                Settings.Save();
214

    
215
                Shell.MonitorAccount(CurrentAccount);
216

    
217
                NotifyOfPropertyChange(() => Settings);                
218
            }
219
        }
220
*/
221
       public void AddAccount()
222
        {
223
            var newAccount = new AccountSettings{AccountName="New Account"};
224
            Settings.Accounts.Add(newAccount);
225
            (Accounts as IProducerConsumerCollection<AccountSettings>).TryAdd(newAccount);
226
           CurrentAccount = newAccount;
227
            NotifyOfPropertyChange(() => Accounts);
228
            NotifyOfPropertyChange(()=>Settings);
229
        }
230

    
231
        public async void AddPithosAccount()
232
       {
233
            var credentials=await PithosAccount.RetrieveCredentialsAsync(Settings.PithosLoginUrl);
234
            var account = Settings.Accounts.FirstOrDefault(act => act.AccountName == credentials.UserName);
235
            if (account == null)
236
            {
237
                account=new AccountSettings{
238
                    AccountName=credentials.UserName,
239
                    ApiKey=credentials.Password,
240
                    UsePithos=true
241
                };
242
                Settings.Accounts.Add(account);
243
                (Accounts as IProducerConsumerCollection<AccountSettings>).TryAdd(account);
244
            }
245
            else
246
            {
247
                account.ApiKey=credentials.Password;
248
            }
249
            //SelectedAccountIndex= Settings.Accounts.IndexOf(account);
250
            CurrentAccount = account;
251
            NotifyOfPropertyChange(() => Accounts);
252
            NotifyOfPropertyChange(()=>Settings);                       
253
       }
254

    
255
        public void RemoveAccount()
256
        {
257
            var accountName = CurrentAccount.AccountName;
258
            Settings.Accounts.Remove(CurrentAccount);
259

    
260
            Accounts.TryRemove(CurrentAccount);
261
            
262
            
263
            CurrentAccount = null;
264
            //Accounts = Settings.Accounts;
265
            //Settings.Save();            
266
            Shell.RemoveMonitor(accountName);
267
            
268
            //NotifyOfPropertyChange("Settings.Accounts");
269
        }
270

    
271
        public bool CanRemoveAccount
272
        {
273
            get { return (CurrentAccount != null); }
274
        }
275

    
276

    
277

    
278
        public bool ExtensionsActivated
279
        {
280
            get { return Settings.ExtensionsActivated; }
281
            set
282
            {
283
                if (Settings.ExtensionsActivated == value)
284
                    return;
285

    
286
                Settings.ExtensionsActivated = value;
287

    
288
                //if (value)
289
                //    _extensionController.RegisterExtensions();
290
                //else
291
                //{
292
                //    _extensionController.UnregisterExtensions();
293
                //}
294
                NotifyOfPropertyChange(() => ExtensionsActivated);
295
            }
296
        }
297

    
298
       
299
        #endregion
300

    
301
       /* private int _selectedAccountIndex;
302
        public int SelectedAccountIndex
303
        {
304
            get { return _selectedAccountIndex; }
305
            set
306
            {
307
                //var accountCount=Settings.Accounts.Count;
308
                //if (accountCount == 0)
309
                //    return;
310
                //if (0 <= value && value < accountCount)
311
                //    _selectedAccountIndex = value;
312
                //else
313
                //    _selectedAccountIndex = 0;
314
                _selectedAccountIndex = value;
315
                NotifyOfPropertyChange(() => CurrentAccount);
316
                NotifyOfPropertyChange(() => CanRemoveAccount);
317
                NotifyOfPropertyChange(()=>SelectedAccountIndex);
318
            }
319
        }*/
320

    
321
        private AccountSettings _currentAccount;
322
        private IWindowManager _windowManager;
323
        private string _shortcutPath;
324

    
325

    
326
        
327
        public AccountSettings CurrentAccount
328
        {
329
            get { return _currentAccount; }
330
            set
331
            {
332
                _currentAccount = value;
333
                NotifyOfPropertyChange(()=>CurrentAccount);
334
                NotifyOfPropertyChange(() => CanRemoveAccount);
335
            }
336
        }
337

    
338
/*
339
        public AccountSettings CurrentAccount
340
        {
341
            get {
342
                if (0 <= SelectedAccountIndex && SelectedAccountIndex < Settings.Accounts.Count)                    
343
                    return Settings.Accounts[SelectedAccountIndex];
344
                return null;
345
            }
346

    
347
        }
348
*/
349

    
350

    
351

    
352

    
353
    public void MoveAccountFolder()
354
    {
355

    
356
        using (var dlg = new FolderBrowserDialog())
357
        {
358
            var currentFolder = CurrentAccount.RootPath;
359
            dlg.SelectedPath = currentFolder;
360
            //Ask the user to select a folder
361
            //Note: We need a parent window here, which we retrieve with GetView            
362
            var view = (Window)GetView();            
363
            if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view)))
364
                return;            
365

    
366
            var newPath= dlg.SelectedPath;                
367
            //Find the account's monitor and stop it
368
            PithosMonitor monitor;
369
            if (Shell.Monitors.TryGetValue(CurrentAccount.AccountName, out monitor))
370
            {
371
                monitor.Stop();
372

    
373

    
374
                var oldPath = monitor.RootPath;
375
                //The old directory may not exist eg. if we create an account for the first time
376
                if (Directory.Exists(oldPath))
377
                {
378
                    //If it does, do the move
379

    
380
                    //Now Create all of the directories
381
                    foreach (string dirPath in Directory.EnumerateDirectories(oldPath, "*",
382
                                                           SearchOption.AllDirectories))
383
                        Directory.CreateDirectory(dirPath.Replace(oldPath, newPath));
384

    
385
                    //Copy all the files
386
                    foreach (string newFilePath in Directory.EnumerateFiles(oldPath, "*.*",
387
                                                                            SearchOption.AllDirectories))
388
                        File.Copy(newFilePath, newFilePath.Replace(oldPath, newPath));
389

    
390
                    Directory.Delete(oldPath, true);
391

    
392
                    //We also need to change the path of the existing file states
393
                    if (monitor != null)
394
                        monitor.MoveFileStates(oldPath, newPath);
395
                }
396
            }
397
            //Replace the old rootpath with the new
398
            CurrentAccount.RootPath = newPath;
399
            //TODO: This will save all settings changes. Too coarse grained, need to fix at a later date
400
            Settings.Save();            
401
            //And start the monitor on the new RootPath            
402
            if (monitor != null)
403
            {
404
                monitor.RootPath = newPath;
405
                if (CurrentAccount.IsActive)
406
                    monitor.Start();
407
            }
408
            else
409
                Shell.MonitorAccount(CurrentAccount);
410
            //Finally, notify that the Settings, CurrentAccount have changed
411
            NotifyOfPropertyChange(() => CurrentAccount);
412
            NotifyOfPropertyChange(() => Settings);
413

    
414
        }
415
    }
416
    }
417
}