Finished account wizard that allows adding an account either by logging to the Pithos...
[pithos-ms-client] / trunk / Pithos.Client.WPF / Preferences / AddAccountViewModel.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.ComponentModel.Composition;
5 using System.IO;
6 using System.Linq;
7 using System.Text;
8 using System.Windows;
9 using System.Windows.Controls;
10 using System.Windows.Forms;
11 using Pithos.Client.WPF.Properties;
12 using Pithos.Core;
13 using MessageBox = System.Windows.MessageBox;
14 using Screen = Caliburn.Micro.Screen;
15
16 namespace Pithos.Client.WPF.Preferences
17 {
18     [Export(typeof(AddAccountViewModel))]
19     public class AddAccountViewModel:Screen
20     {
21         private string _accountName;
22         public string AccountName
23         {
24             get { return _accountName; }
25             set
26             {
27                 _accountName = value;
28                 NotifyOfPropertyChange(()=>AccountName);
29                 NotifyOfPropertyChange(() => HasCredentials);
30             }
31         }
32
33         private string _token;
34         public string Token
35         {
36             get { return _token; }
37             set
38             {
39                 _token = value;
40                 NotifyOfPropertyChange(()=>Token);
41                 NotifyOfPropertyChange(() => HasCredentials);
42             }
43         }
44
45         private string _accountPath;
46         public string AccountPath
47         {
48             get { return _accountPath; }
49             set
50             {
51                 _accountPath = value;
52                 NotifyOfPropertyChange(() => AccountPath);
53                 NotifyOfPropertyChange(() => HasAccountPath);
54             }
55         }
56
57
58         public bool HasAccountPath
59         {
60             get { return !String.IsNullOrWhiteSpace(AccountPath); }
61         }
62
63         public bool HasCredentials
64         {
65             get { return !(String.IsNullOrWhiteSpace(AccountName) || String.IsNullOrWhiteSpace(Token) ) ; }
66         }
67
68
69         private bool  _isConfirmed;
70
71         public bool IsConfirmed
72         {
73             get { return _isConfirmed; }
74             set
75             {
76                 _isConfirmed = value;
77                 NotifyOfPropertyChange(() => IsConfirmed);
78             }
79         }
80
81
82         private bool _isAccountActive;
83
84         public bool IsAccountActive
85         {
86             get { return _isAccountActive; }
87             set
88             {
89                 _isAccountActive = value;
90                 NotifyOfPropertyChange(() => IsAccountActive);
91             }
92         }
93
94         public void SelectAccount()
95         {
96             using (var dlg = new FolderBrowserDialog())
97             {
98                 //Ask the user to select a folder
99                 //Note: We need a parent window here, which we retrieve with GetView            
100                 var view = (Window)GetView();
101                 if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view)))
102                     return;
103
104                 AccountPath= dlg.SelectedPath;
105             }
106         }
107
108
109         private bool _isRetrieving;
110         public bool IsRetrieving
111         {
112             get { return _isRetrieving; }
113             set
114             {
115                 _isRetrieving = value;
116                 NotifyOfPropertyChange(() => IsRetrieving);
117             }
118         }
119
120         public async void RetrieveCredentials()
121         {
122             IsRetrieving = true;
123             IsConfirmed = false;
124
125             try
126             {
127                 var credentials = await PithosAccount.RetrieveCredentialsAsync(Settings.Default.PithosLoginUrl);
128                 AccountName = credentials.UserName;
129                 Token = credentials.Password;
130
131                 IsConfirmed = true;
132             }
133             catch (Exception exc)
134             {
135                 IsConfirmed = false;
136                 MessageBox.Show(exc.ToString(), "Error");
137                 throw;
138             }
139             IsRetrieving = false;
140
141         }
142         
143
144     }
145 }