root / trunk / Pithos.Client.WPF / Preferences / AddAccountViewModel.cs @ 5e31048f
History | View | Annotate | Download (8.2 kB)
1 | 77f3c897 | Panagiotis Kanavos | using System; |
---|---|---|---|
2 | 77f3c897 | Panagiotis Kanavos | using System.Collections.Generic; |
3 | 3ddbb7b8 | Panagiotis Kanavos | using System.ComponentModel; |
4 | 77f3c897 | Panagiotis Kanavos | using System.ComponentModel.Composition; |
5 | 3ddbb7b8 | Panagiotis Kanavos | using System.IO; |
6 | 77f3c897 | Panagiotis Kanavos | using System.Linq; |
7 | 77f3c897 | Panagiotis Kanavos | using System.Text; |
8 | f734ab5b | Panagiotis Kanavos | using System.Threading.Tasks; |
9 | 3ddbb7b8 | Panagiotis Kanavos | using System.Windows; |
10 | 3ddbb7b8 | Panagiotis Kanavos | using System.Windows.Controls; |
11 | 3ddbb7b8 | Panagiotis Kanavos | using System.Windows.Forms; |
12 | f734ab5b | Panagiotis Kanavos | using Caliburn.Micro; |
13 | 3ddbb7b8 | Panagiotis Kanavos | using Pithos.Client.WPF.Properties; |
14 | 3ddbb7b8 | Panagiotis Kanavos | using Pithos.Core; |
15 | f734ab5b | Panagiotis Kanavos | using Pithos.Network; |
16 | 3ddbb7b8 | Panagiotis Kanavos | using MessageBox = System.Windows.MessageBox; |
17 | 3ddbb7b8 | Panagiotis Kanavos | using Screen = Caliburn.Micro.Screen; |
18 | 77f3c897 | Panagiotis Kanavos | |
19 | 77f3c897 | Panagiotis Kanavos | namespace Pithos.Client.WPF.Preferences |
20 | 77f3c897 | Panagiotis Kanavos | { |
21 | 77f3c897 | Panagiotis Kanavos | [Export(typeof(AddAccountViewModel))] |
22 | 77f3c897 | Panagiotis Kanavos | public class AddAccountViewModel:Screen |
23 | 77f3c897 | Panagiotis Kanavos | { |
24 | f734ab5b | Panagiotis Kanavos | |
25 | f734ab5b | Panagiotis Kanavos | private readonly List<string> _servers; |
26 | f734ab5b | Panagiotis Kanavos | |
27 | f734ab5b | Panagiotis Kanavos | public List<string> Servers |
28 | f734ab5b | Panagiotis Kanavos | { |
29 | f734ab5b | Panagiotis Kanavos | get { return _servers; } |
30 | f734ab5b | Panagiotis Kanavos | } |
31 | f734ab5b | Panagiotis Kanavos | |
32 | f734ab5b | Panagiotis Kanavos | private bool _isValidServer; |
33 | f734ab5b | Panagiotis Kanavos | public bool IsValidServer |
34 | f734ab5b | Panagiotis Kanavos | { |
35 | f734ab5b | Panagiotis Kanavos | get { return _isValidServer; } |
36 | f734ab5b | Panagiotis Kanavos | set |
37 | f734ab5b | Panagiotis Kanavos | { |
38 | f734ab5b | Panagiotis Kanavos | _isValidServer = value; |
39 | f734ab5b | Panagiotis Kanavos | NotifyOfPropertyChange(()=>IsValidServer); |
40 | f734ab5b | Panagiotis Kanavos | } |
41 | f734ab5b | Panagiotis Kanavos | } |
42 | f734ab5b | Panagiotis Kanavos | |
43 | f734ab5b | Panagiotis Kanavos | |
44 | f734ab5b | Panagiotis Kanavos | private string _currentServer; |
45 | f734ab5b | Panagiotis Kanavos | public string CurrentServer |
46 | f734ab5b | Panagiotis Kanavos | { |
47 | f734ab5b | Panagiotis Kanavos | get { return _currentServer; } |
48 | f734ab5b | Panagiotis Kanavos | set |
49 | f734ab5b | Panagiotis Kanavos | { |
50 | f734ab5b | Panagiotis Kanavos | if (!Uri.IsWellFormedUriString(value, UriKind.Absolute)) |
51 | f734ab5b | Panagiotis Kanavos | { |
52 | f734ab5b | Panagiotis Kanavos | IsValidServer = false; |
53 | f734ab5b | Panagiotis Kanavos | throw new UriFormatException(); |
54 | f734ab5b | Panagiotis Kanavos | } |
55 | f734ab5b | Panagiotis Kanavos | _currentServer = value; |
56 | f734ab5b | Panagiotis Kanavos | IsValidServer = true; |
57 | f734ab5b | Panagiotis Kanavos | HasValidCredentials = false; |
58 | f734ab5b | Panagiotis Kanavos | IsConfirmed = false; |
59 | f734ab5b | Panagiotis Kanavos | NotifyOfPropertyChange(()=>CurrentServer); |
60 | f734ab5b | Panagiotis Kanavos | } |
61 | f734ab5b | Panagiotis Kanavos | } |
62 | f734ab5b | Panagiotis Kanavos | |
63 | 77f3c897 | Panagiotis Kanavos | private string _accountName; |
64 | 77f3c897 | Panagiotis Kanavos | public string AccountName |
65 | 77f3c897 | Panagiotis Kanavos | { |
66 | 77f3c897 | Panagiotis Kanavos | get { return _accountName; } |
67 | 77f3c897 | Panagiotis Kanavos | set |
68 | 77f3c897 | Panagiotis Kanavos | { |
69 | 77f3c897 | Panagiotis Kanavos | _accountName = value; |
70 | 77f3c897 | Panagiotis Kanavos | NotifyOfPropertyChange(()=>AccountName); |
71 | 3ddbb7b8 | Panagiotis Kanavos | NotifyOfPropertyChange(() => HasCredentials); |
72 | 77f3c897 | Panagiotis Kanavos | } |
73 | 77f3c897 | Panagiotis Kanavos | } |
74 | 77f3c897 | Panagiotis Kanavos | |
75 | 77f3c897 | Panagiotis Kanavos | private string _token; |
76 | 77f3c897 | Panagiotis Kanavos | public string Token |
77 | 77f3c897 | Panagiotis Kanavos | { |
78 | 77f3c897 | Panagiotis Kanavos | get { return _token; } |
79 | 77f3c897 | Panagiotis Kanavos | set |
80 | 77f3c897 | Panagiotis Kanavos | { |
81 | 77f3c897 | Panagiotis Kanavos | _token = value; |
82 | 77f3c897 | Panagiotis Kanavos | NotifyOfPropertyChange(()=>Token); |
83 | 3ddbb7b8 | Panagiotis Kanavos | NotifyOfPropertyChange(() => HasCredentials); |
84 | 77f3c897 | Panagiotis Kanavos | } |
85 | 77f3c897 | Panagiotis Kanavos | } |
86 | 77f3c897 | Panagiotis Kanavos | |
87 | 77f3c897 | Panagiotis Kanavos | private string _accountPath; |
88 | 77f3c897 | Panagiotis Kanavos | public string AccountPath |
89 | 77f3c897 | Panagiotis Kanavos | { |
90 | 77f3c897 | Panagiotis Kanavos | get { return _accountPath; } |
91 | 77f3c897 | Panagiotis Kanavos | set |
92 | 77f3c897 | Panagiotis Kanavos | { |
93 | 77f3c897 | Panagiotis Kanavos | _accountPath = value; |
94 | 3ddbb7b8 | Panagiotis Kanavos | NotifyOfPropertyChange(() => AccountPath); |
95 | 3ddbb7b8 | Panagiotis Kanavos | NotifyOfPropertyChange(() => HasAccountPath); |
96 | 77f3c897 | Panagiotis Kanavos | } |
97 | 77f3c897 | Panagiotis Kanavos | } |
98 | 3ddbb7b8 | Panagiotis Kanavos | |
99 | 3ddbb7b8 | Panagiotis Kanavos | |
100 | 3ddbb7b8 | Panagiotis Kanavos | public bool HasAccountPath |
101 | 3ddbb7b8 | Panagiotis Kanavos | { |
102 | 3ddbb7b8 | Panagiotis Kanavos | get { return !String.IsNullOrWhiteSpace(AccountPath); } |
103 | 3ddbb7b8 | Panagiotis Kanavos | } |
104 | 3ddbb7b8 | Panagiotis Kanavos | |
105 | 3ddbb7b8 | Panagiotis Kanavos | public bool HasCredentials |
106 | 3ddbb7b8 | Panagiotis Kanavos | { |
107 | 3ddbb7b8 | Panagiotis Kanavos | get { return !(String.IsNullOrWhiteSpace(AccountName) || String.IsNullOrWhiteSpace(Token) ) ; } |
108 | 3ddbb7b8 | Panagiotis Kanavos | } |
109 | 3ddbb7b8 | Panagiotis Kanavos | |
110 | 3ddbb7b8 | Panagiotis Kanavos | |
111 | 3ddbb7b8 | Panagiotis Kanavos | private bool _isConfirmed; |
112 | 3ddbb7b8 | Panagiotis Kanavos | |
113 | 3ddbb7b8 | Panagiotis Kanavos | public bool IsConfirmed |
114 | 3ddbb7b8 | Panagiotis Kanavos | { |
115 | 3ddbb7b8 | Panagiotis Kanavos | get { return _isConfirmed; } |
116 | 3ddbb7b8 | Panagiotis Kanavos | set |
117 | 3ddbb7b8 | Panagiotis Kanavos | { |
118 | 3ddbb7b8 | Panagiotis Kanavos | _isConfirmed = value; |
119 | f734ab5b | Panagiotis Kanavos | HasValidCredentials = false; |
120 | 3ddbb7b8 | Panagiotis Kanavos | NotifyOfPropertyChange(() => IsConfirmed); |
121 | 3ddbb7b8 | Panagiotis Kanavos | } |
122 | 3ddbb7b8 | Panagiotis Kanavos | } |
123 | 3ddbb7b8 | Panagiotis Kanavos | |
124 | 3ddbb7b8 | Panagiotis Kanavos | |
125 | 3ddbb7b8 | Panagiotis Kanavos | private bool _isAccountActive; |
126 | 3ddbb7b8 | Panagiotis Kanavos | |
127 | 3ddbb7b8 | Panagiotis Kanavos | public bool IsAccountActive |
128 | 3ddbb7b8 | Panagiotis Kanavos | { |
129 | 3ddbb7b8 | Panagiotis Kanavos | get { return _isAccountActive; } |
130 | 3ddbb7b8 | Panagiotis Kanavos | set |
131 | 3ddbb7b8 | Panagiotis Kanavos | { |
132 | 3ddbb7b8 | Panagiotis Kanavos | _isAccountActive = value; |
133 | 3ddbb7b8 | Panagiotis Kanavos | NotifyOfPropertyChange(() => IsAccountActive); |
134 | 3ddbb7b8 | Panagiotis Kanavos | } |
135 | 3ddbb7b8 | Panagiotis Kanavos | } |
136 | 3ddbb7b8 | Panagiotis Kanavos | |
137 | 3ddbb7b8 | Panagiotis Kanavos | public void SelectAccount() |
138 | 3ddbb7b8 | Panagiotis Kanavos | { |
139 | 3ddbb7b8 | Panagiotis Kanavos | using (var dlg = new FolderBrowserDialog()) |
140 | 3ddbb7b8 | Panagiotis Kanavos | { |
141 | 3ddbb7b8 | Panagiotis Kanavos | //Ask the user to select a folder |
142 | 3ddbb7b8 | Panagiotis Kanavos | //Note: We need a parent window here, which we retrieve with GetView |
143 | 3ddbb7b8 | Panagiotis Kanavos | var view = (Window)GetView(); |
144 | 3ddbb7b8 | Panagiotis Kanavos | if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view))) |
145 | 3ddbb7b8 | Panagiotis Kanavos | return; |
146 | 3ddbb7b8 | Panagiotis Kanavos | |
147 | 3ddbb7b8 | Panagiotis Kanavos | AccountPath= dlg.SelectedPath; |
148 | 3ddbb7b8 | Panagiotis Kanavos | } |
149 | 3ddbb7b8 | Panagiotis Kanavos | } |
150 | 3ddbb7b8 | Panagiotis Kanavos | |
151 | 3ddbb7b8 | Panagiotis Kanavos | |
152 | 3ddbb7b8 | Panagiotis Kanavos | public async void RetrieveCredentials() |
153 | 3ddbb7b8 | Panagiotis Kanavos | { |
154 | 8dd2bdfb | Panagiotis Kanavos | SetBusy("Waiting for credentials.", "Please enter your credentials in the Pithos logon page"); |
155 | 3ddbb7b8 | Panagiotis Kanavos | IsConfirmed = false; |
156 | 3ddbb7b8 | Panagiotis Kanavos | |
157 | 3ddbb7b8 | Panagiotis Kanavos | try |
158 | 3ddbb7b8 | Panagiotis Kanavos | { |
159 | 03ee454f | Panagiotis Kanavos | var credentials = await PithosAccount.RetrieveCredentials(Settings.Default.PithosLoginUrl); |
160 | 3ddbb7b8 | Panagiotis Kanavos | AccountName = credentials.UserName; |
161 | 3ddbb7b8 | Panagiotis Kanavos | Token = credentials.Password; |
162 | 3ddbb7b8 | Panagiotis Kanavos | |
163 | 3ddbb7b8 | Panagiotis Kanavos | IsConfirmed = true; |
164 | 3ddbb7b8 | Panagiotis Kanavos | } |
165 | 3ddbb7b8 | Panagiotis Kanavos | catch (Exception exc) |
166 | 3ddbb7b8 | Panagiotis Kanavos | { |
167 | 3ddbb7b8 | Panagiotis Kanavos | IsConfirmed = false; |
168 | 3ddbb7b8 | Panagiotis Kanavos | MessageBox.Show(exc.ToString(), "Error"); |
169 | 3ddbb7b8 | Panagiotis Kanavos | throw; |
170 | 3ddbb7b8 | Panagiotis Kanavos | } |
171 | 8dd2bdfb | Panagiotis Kanavos | finally |
172 | 8dd2bdfb | Panagiotis Kanavos | { |
173 | 8dd2bdfb | Panagiotis Kanavos | ClearBusy(); |
174 | 8dd2bdfb | Panagiotis Kanavos | |
175 | 8dd2bdfb | Panagiotis Kanavos | (this.GetView() as Window).Activate(); |
176 | 8dd2bdfb | Panagiotis Kanavos | } |
177 | 3ddbb7b8 | Panagiotis Kanavos | |
178 | 3ddbb7b8 | Panagiotis Kanavos | } |
179 | f734ab5b | Panagiotis Kanavos | |
180 | f734ab5b | Panagiotis Kanavos | public AddAccountViewModel() |
181 | f734ab5b | Panagiotis Kanavos | { |
182 | f734ab5b | Panagiotis Kanavos | _servers=new List<string> |
183 | f734ab5b | Panagiotis Kanavos | { |
184 | f734ab5b | Panagiotis Kanavos | Settings.Default.ProductionServer, |
185 | f734ab5b | Panagiotis Kanavos | Settings.Default.DevelopmentServer |
186 | f734ab5b | Panagiotis Kanavos | }; |
187 | f734ab5b | Panagiotis Kanavos | CurrentServer = _servers[0]; |
188 | f734ab5b | Panagiotis Kanavos | } |
189 | f734ab5b | Panagiotis Kanavos | |
190 | f734ab5b | Panagiotis Kanavos | private bool _hasValidCredentials; |
191 | f734ab5b | Panagiotis Kanavos | public bool HasValidCredentials |
192 | f734ab5b | Panagiotis Kanavos | { |
193 | f734ab5b | Panagiotis Kanavos | get { return _hasValidCredentials; } |
194 | f734ab5b | Panagiotis Kanavos | set |
195 | f734ab5b | Panagiotis Kanavos | { |
196 | f734ab5b | Panagiotis Kanavos | _hasValidCredentials = value; |
197 | f734ab5b | Panagiotis Kanavos | NotifyOfPropertyChange(()=>HasValidCredentials); |
198 | f734ab5b | Panagiotis Kanavos | } |
199 | f734ab5b | Panagiotis Kanavos | } |
200 | f734ab5b | Panagiotis Kanavos | |
201 | 06f11e8b | Panagiotis Kanavos | private string _validationMessage; |
202 | 06f11e8b | Panagiotis Kanavos | public string ValidationMessage |
203 | 06f11e8b | Panagiotis Kanavos | { |
204 | 06f11e8b | Panagiotis Kanavos | get { return _validationMessage; } |
205 | 06f11e8b | Panagiotis Kanavos | set |
206 | 06f11e8b | Panagiotis Kanavos | { |
207 | 06f11e8b | Panagiotis Kanavos | _validationMessage = value; |
208 | 06f11e8b | Panagiotis Kanavos | NotifyOfPropertyChange(()=>ValidationMessage); |
209 | 06f11e8b | Panagiotis Kanavos | } |
210 | 06f11e8b | Panagiotis Kanavos | } |
211 | f734ab5b | Panagiotis Kanavos | |
212 | 8dd2bdfb | Panagiotis Kanavos | private bool _isWorking; |
213 | 8dd2bdfb | Panagiotis Kanavos | public bool IsWorking |
214 | f734ab5b | Panagiotis Kanavos | { |
215 | 8dd2bdfb | Panagiotis Kanavos | get { return _isWorking; } |
216 | f734ab5b | Panagiotis Kanavos | set |
217 | f734ab5b | Panagiotis Kanavos | { |
218 | 8dd2bdfb | Panagiotis Kanavos | _isWorking = value; |
219 | 8dd2bdfb | Panagiotis Kanavos | NotifyOfPropertyChange(()=>IsWorking); |
220 | f734ab5b | Panagiotis Kanavos | } |
221 | f734ab5b | Panagiotis Kanavos | } |
222 | f734ab5b | Panagiotis Kanavos | |
223 | 8dd2bdfb | Panagiotis Kanavos | private string _busyTitle; |
224 | 8dd2bdfb | Panagiotis Kanavos | public string BusyTitle |
225 | 8dd2bdfb | Panagiotis Kanavos | { |
226 | 8dd2bdfb | Panagiotis Kanavos | get { return _busyTitle; } |
227 | 8dd2bdfb | Panagiotis Kanavos | set |
228 | 8dd2bdfb | Panagiotis Kanavos | { |
229 | 8dd2bdfb | Panagiotis Kanavos | _busyTitle = value; |
230 | 8dd2bdfb | Panagiotis Kanavos | NotifyOfPropertyChange(()=>BusyTitle); |
231 | 8dd2bdfb | Panagiotis Kanavos | } |
232 | 8dd2bdfb | Panagiotis Kanavos | } |
233 | 8dd2bdfb | Panagiotis Kanavos | |
234 | 8dd2bdfb | Panagiotis Kanavos | private string _busyDetail; |
235 | 8dd2bdfb | Panagiotis Kanavos | public string BusyDetail |
236 | 8dd2bdfb | Panagiotis Kanavos | { |
237 | 8dd2bdfb | Panagiotis Kanavos | get { return _busyDetail; } |
238 | 8dd2bdfb | Panagiotis Kanavos | set |
239 | 8dd2bdfb | Panagiotis Kanavos | { |
240 | 8dd2bdfb | Panagiotis Kanavos | _busyDetail = value; |
241 | 8dd2bdfb | Panagiotis Kanavos | NotifyOfPropertyChange(()=>BusyDetail); |
242 | 8dd2bdfb | Panagiotis Kanavos | } |
243 | 8dd2bdfb | Panagiotis Kanavos | } |
244 | 8dd2bdfb | Panagiotis Kanavos | |
245 | 8dd2bdfb | Panagiotis Kanavos | private void SetBusy(string title,string detail) |
246 | 8dd2bdfb | Panagiotis Kanavos | { |
247 | 8dd2bdfb | Panagiotis Kanavos | IsWorking = true; |
248 | 8dd2bdfb | Panagiotis Kanavos | BusyTitle = title; |
249 | 8dd2bdfb | Panagiotis Kanavos | BusyDetail = detail; |
250 | 8dd2bdfb | Panagiotis Kanavos | } |
251 | 8dd2bdfb | Panagiotis Kanavos | |
252 | 8dd2bdfb | Panagiotis Kanavos | private void ClearBusy() |
253 | 8dd2bdfb | Panagiotis Kanavos | { |
254 | 8dd2bdfb | Panagiotis Kanavos | IsWorking = false; |
255 | 8dd2bdfb | Panagiotis Kanavos | BusyTitle = ""; |
256 | 8dd2bdfb | Panagiotis Kanavos | BusyDetail = ""; |
257 | 8dd2bdfb | Panagiotis Kanavos | |
258 | 8dd2bdfb | Panagiotis Kanavos | } |
259 | 8dd2bdfb | Panagiotis Kanavos | |
260 | f734ab5b | Panagiotis Kanavos | public async void TestAccount() |
261 | f734ab5b | Panagiotis Kanavos | { |
262 | f734ab5b | Panagiotis Kanavos | try |
263 | f734ab5b | Panagiotis Kanavos | { |
264 | 06f11e8b | Panagiotis Kanavos | SetBusy("Validating Credentials", ""); |
265 | 06f11e8b | Panagiotis Kanavos | var client = new CloudFilesClient(AccountName, Token) { AuthenticationUrl = CurrentServer }; |
266 | 06f11e8b | Panagiotis Kanavos | var containers = await TaskEx.Run(() => |
267 | f734ab5b | Panagiotis Kanavos | { |
268 | f734ab5b | Panagiotis Kanavos | client.Authenticate(); |
269 | f734ab5b | Panagiotis Kanavos | return client.ListContainers(AccountName); |
270 | f734ab5b | Panagiotis Kanavos | }); |
271 | 06f11e8b | Panagiotis Kanavos | HasValidCredentials = true; |
272 | 06f11e8b | Panagiotis Kanavos | ValidationMessage = "Credentials Validated"; |
273 | 06f11e8b | Panagiotis Kanavos | } |
274 | 06f11e8b | Panagiotis Kanavos | /* |
275 | 06f11e8b | Panagiotis Kanavos | catch (AggregateException exc) |
276 | 06f11e8b | Panagiotis Kanavos | { |
277 | 06f11e8b | Panagiotis Kanavos | exc.Handle(ex=> |
278 | 06f11e8b | Panagiotis Kanavos | { |
279 | 06f11e8b | Panagiotis Kanavos | HasValidCredentials = false; |
280 | 06f11e8b | Panagiotis Kanavos | MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop); |
281 | 06f11e8b | Panagiotis Kanavos | }); |
282 | f734ab5b | Panagiotis Kanavos | } |
283 | 06f11e8b | Panagiotis Kanavos | */ |
284 | 5e31048f | Panagiotis Kanavos | catch |
285 | f734ab5b | Panagiotis Kanavos | { |
286 | f734ab5b | Panagiotis Kanavos | HasValidCredentials = false; |
287 | f734ab5b | Panagiotis Kanavos | MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop); |
288 | 06f11e8b | Panagiotis Kanavos | ValidationMessage = "Credentials validation failed"; |
289 | f734ab5b | Panagiotis Kanavos | } |
290 | f734ab5b | Panagiotis Kanavos | finally |
291 | f734ab5b | Panagiotis Kanavos | { |
292 | 8dd2bdfb | Panagiotis Kanavos | ClearBusy(); |
293 | f734ab5b | Panagiotis Kanavos | } |
294 | f734ab5b | Panagiotis Kanavos | } |
295 | 3ddbb7b8 | Panagiotis Kanavos | |
296 | 77f3c897 | Panagiotis Kanavos | } |
297 | 77f3c897 | Panagiotis Kanavos | } |