Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / Preferences / PreferencesViewModel.cs @ 7e26c075

History | View | Annotate | Download (12.5 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();
224
            Settings.Accounts.Add(newAccount);
225
            //SelectedAccountIndex= Settings.Accounts.Count-1;
226
           CurrentAccount = newAccount;
227
            NotifyOfPropertyChange(() => Accounts);
228
            NotifyOfPropertyChange(()=>Settings);
229
        }
230

    
231
        public void AddPithosAccount()
232
       {
233
           var task=PithosAccount.RetrieveCredentialsAsync(Settings.PithosSite)
234
               .ContinueWith(t=>
235
                   {                       
236
                       var credentials=t.Result;
237
                       var account = Settings.Accounts.FirstOrDefault(act => act.AccountName == credentials.UserName);
238
                       if (account == null)
239
                       {
240
                           account=new AccountSettings{
241
                               AccountName=credentials.UserName,
242
                               ApiKey=credentials.Password,
243
                               UsePithos=true
244
                           };
245
                           Settings.Accounts.Add(account);
246
                           (Accounts as IProducerConsumerCollection<AccountSettings>).TryAdd(account);
247
                       }
248
                       else
249
                       {
250
                           account.ApiKey=credentials.Password;
251
                       }
252
                       //SelectedAccountIndex= Settings.Accounts.IndexOf(account);
253
                       CurrentAccount = account;
254
                       NotifyOfPropertyChange(() => Accounts);
255
                       NotifyOfPropertyChange(()=>Settings);                       
256
                   });
257
            ((Task)task).WaitWithPumping();
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
                //if (value)
294
                //    _extensionController.RegisterExtensions();
295
                //else
296
                //{
297
                //    _extensionController.UnregisterExtensions();
298
                //}
299
                NotifyOfPropertyChange(() => ExtensionsActivated);
300
            }
301
        }
302

    
303
       
304
        #endregion
305

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

    
326
        private AccountSettings _currentAccount;
327
        private IWindowManager _windowManager;
328
        private string _shortcutPath;
329

    
330

    
331
        
332
        public AccountSettings CurrentAccount
333
        {
334
            get { return _currentAccount; }
335
            set
336
            {
337
                _currentAccount = value;
338
                NotifyOfPropertyChange(()=>CurrentAccount);
339
                NotifyOfPropertyChange(() => CanRemoveAccount);
340
            }
341
        }
342

    
343
/*
344
        public AccountSettings CurrentAccount
345
        {
346
            get {
347
                if (0 <= SelectedAccountIndex && SelectedAccountIndex < Settings.Accounts.Count)                    
348
                    return Settings.Accounts[SelectedAccountIndex];
349
                return null;
350
            }
351

    
352
        }
353
*/
354

    
355

    
356

    
357

    
358
    public void MoveAccountFolder()
359
    {
360

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

    
371
            var newPath= dlg.SelectedPath;                
372
            //Find the account's monitor and stop it
373
            PithosMonitor monitor;
374
            if (Shell.Monitors.TryGetValue(CurrentAccount.AccountName, out monitor))
375
            {
376
                monitor.Stop();
377

    
378

    
379
                var oldPath = monitor.RootPath;
380
                //The old directory may not exist eg. if we create an account for the first time
381
                if (Directory.Exists(oldPath))
382
                {
383
                    //If it does, do the move
384
                    Directory.Move(oldPath, newPath);
385
                    //We also need to change the path of the existing file states
386
                    if (monitor != null)
387
                        monitor.MoveFileStates(oldPath, newPath);
388
                }
389
            }
390
            //Replace the old rootpath with the new
391
            CurrentAccount.RootPath = newPath;
392
            //TODO: This will save all settings changes. Too coarse grained, need to fix at a later date
393
            Settings.Save();            
394
            //And start the monitor on the new RootPath            
395
            if (monitor != null)
396
            {
397
                monitor.RootPath = newPath;
398
                if (CurrentAccount.IsActive)
399
                    monitor.Start();
400
            }
401
            else
402
                Shell.MonitorAccount(CurrentAccount);
403
            //Finally, notify that the Settings, CurrentAccount have changed
404
            NotifyOfPropertyChange(() => CurrentAccount);
405
            NotifyOfPropertyChange(() => Settings);
406

    
407
        }
408
    }
409
    }
410
}