Revision 31c97141 trunk/Pithos.Client.WPF/Preferences/PreferencesViewModel.cs

b/trunk/Pithos.Client.WPF/Preferences/PreferencesViewModel.cs
45 45

  
46 46
using System.Collections.Concurrent;
47 47
using System.ComponentModel.Composition;
48
using System.Diagnostics;
48 49
using System.IO;
49 50
using System.Net;
50 51
using System.Threading.Tasks;
......
69 70
    {
70 71
        private IEventAggregator _events;
71 72

  
73
        //Logging in the Pithos client is provided by log4net
74
        private static readonly log4net.ILog Log = log4net.LogManager.GetLogger("Pithos");
72 75

  
73 76
        private PithosSettings _settings;
74 77
        public PithosSettings Settings
......
81 84
            }
82 85
        }
83 86

  
84
        private ObservableConcurrentCollection<AccountSettings> _accounts;
85
        public ObservableConcurrentCollection<AccountSettings> Accounts
87
        private ObservableConcurrentCollection<AccountViewModel> _accounts;
88
        public ObservableConcurrentCollection<AccountViewModel> Accounts
86 89
        {
87 90
            get { return _accounts; }
88 91
            set 
......
106 109
            Shell = shell;
107 110
            
108 111
            Settings=settings;
109
            Accounts = new ObservableConcurrentCollection<AccountSettings>();
112
            Accounts = new ObservableConcurrentCollection<AccountViewModel>();
110 113
            if (settings.Accounts == null)
111 114
            {
112 115
                settings.Accounts=new AccountsCollection();
113 116
                settings.Save();
114 117
            }
115
            Accounts.AddFromEnumerable(settings.Accounts);
118
            var accountVMs = from account in settings.Accounts
119
                             select new AccountViewModel(account);
120

  
121
            Accounts.AddFromEnumerable(accountVMs);
116 122
            
117 123
            var startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
118 124
            _shortcutPath = Path.Combine(startupPath, "Pithos.lnk");
......
205 211
            var monitor = Shell.Monitors[CurrentAccount.AccountName];
206 212
            
207 213

  
208
            var model = new SelectiveSynchViewModel(monitor,_events,CurrentAccount);
214
            var model = new SelectiveSynchViewModel(monitor,_events,CurrentAccount.Account);
209 215
            if (_windowManager.ShowDialog(model) == true)
210 216
            {
211 217
                
......
213 219
        }
214 220

  
215 221
        public async Task RefreshApiKey()
216
        {            
217
            await Shell.TryAuthorize(CurrentAccount.AccountName, 3);
218
            NotifyOfPropertyChange(()=>CurrentAccount);
222
        {
223
            _events.Publish(new Notification { Title = "Authorization failed", Message = "Your API Key has probably expired. You will be directed to a page where you can renew it", Level = TraceLevel.Error });
224

  
225
            //var userName = CurrentAccount.AccountName;
226
            try
227
            {
228

  
229
                var credentials = await PithosAccount.RetrieveCredentials(Settings.PithosLoginUrl);
230

  
231
                var account = Accounts.First(act => act.AccountName == credentials.UserName);
232
                //The server may return credentials for a different account
233
                var monitor =  Shell.Monitors[account.AccountName];
234
                account.ApiKey = credentials.Password;
235
                monitor.ApiKey = credentials.Password;
236
                account.IsExpired = false;
237
                Settings.Save();
238
                TaskEx.Delay(10000).ContinueWith(_ =>Shell.MonitorAccount(account.Account));
239
                NotifyOfPropertyChange(() => Accounts);
240
            }
241
            catch (AggregateException exc)
242
            {
243
                string message = String.Format("API Key retrieval failed");
244
                Log.Error(message, exc.InnerException);
245
                _events.Publish(new Notification { Title = "Authorization failed", Message = message, Level = TraceLevel.Error });
246
            }
247
            catch (Exception exc)
248
            {
249
                string message = String.Format("API Key retrieval failed");
250
                Log.Error(message, exc);
251
                _events.Publish(new Notification { Title = "Authorization failed", Message = message, Level = TraceLevel.Error });
252
            }
253

  
219 254
        }
255

  
220 256
    
221 257
        public void SaveChanges()
222 258
        {
......
298 334
                                        IsActive=wizard.IsAccountActive
299 335
                                    };
300 336
               Settings.Accounts.Add(newAccount);
301
               (Accounts as IProducerConsumerCollection<AccountSettings>).TryAdd(newAccount);
302
               CurrentAccount = newAccount;
337
               var accountVM = new AccountViewModel(newAccount);
338
               (Accounts as IProducerConsumerCollection<AccountViewModel>).TryAdd(accountVM);
339
               CurrentAccount = accountVM;
303 340
               NotifyOfPropertyChange(() => Accounts);
304 341
               NotifyOfPropertyChange(() => Settings);   
305 342
           }
......
312 349
       {
313 350
            var credentials=await PithosAccount.RetrieveCredentials(Settings.PithosLoginUrl);
314 351
            var account = Settings.Accounts.FirstOrDefault(act => act.AccountName == credentials.UserName);
352
            var accountVM = new AccountViewModel(account);
315 353
            if (account == null)
316 354
            {
317 355
                account=new AccountSettings{
......
319 357
                    ApiKey=credentials.Password
320 358
                };
321 359
                Settings.Accounts.Add(account);
322
                (Accounts as IProducerConsumerCollection<AccountSettings>).TryAdd(account);
360
                accountVM = new AccountViewModel(account);
361
                (Accounts as IProducerConsumerCollection<AccountViewModel>).TryAdd(accountVM);
323 362
            }
324 363
            else
325 364
            {
326 365
                account.ApiKey=credentials.Password;
327 366
            }
328 367
            //SelectedAccountIndex= Settings.Accounts.IndexOf(account);
329
            CurrentAccount = account;
368
            CurrentAccount = accountVM;
330 369
            NotifyOfPropertyChange(() => Accounts);
331 370
            NotifyOfPropertyChange(()=>Settings);                       
332 371
       }
......
334 373
        public void RemoveAccount()
335 374
        {
336 375
            var accountName = CurrentAccount.AccountName;
337
            Settings.Accounts.Remove(CurrentAccount);
376
            Settings.Accounts.Remove(CurrentAccount.Account);
338 377

  
339 378
            Accounts.TryRemove(CurrentAccount);
340 379
            
......
401 440
            }
402 441
        }*/
403 442

  
404
        private AccountSettings _currentAccount;
443
        private AccountViewModel _currentAccount;
405 444
        private IWindowManager _windowManager;
406 445
        private string _shortcutPath;
407 446

  
408 447

  
409 448
        
410
        public AccountSettings CurrentAccount
449
        public AccountViewModel CurrentAccount
411 450
        {
412 451
            get { return _currentAccount; }
413 452
            set
......
494 533
                    monitor.Start();
495 534
            }
496 535
            else
497
                Shell.MonitorAccount(CurrentAccount);
536
                Shell.MonitorAccount(CurrentAccount.Account);
498 537
            //Finally, notify that the Settings, CurrentAccount have changed
499 538
            NotifyOfPropertyChange(() => CurrentAccount);
500 539
            NotifyOfPropertyChange(() => Settings);

Also available in: Unified diff