Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / Preferences / AddAccountViewModel.cs @ 255f5f86

History | View | Annotate | Download (10.3 kB)

1
#region
2
/* -----------------------------------------------------------------------
3
 * <copyright file="AddAccountViewModel.cs" company="GRNet">
4
 * 
5
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or
8
 * without modification, are permitted provided that the following
9
 * conditions are met:
10
 *
11
 *   1. Redistributions of source code must retain the above
12
 *      copyright notice, this list of conditions and the following
13
 *      disclaimer.
14
 *
15
 *   2. Redistributions in binary form must reproduce the above
16
 *      copyright notice, this list of conditions and the following
17
 *      disclaimer in the documentation and/or other materials
18
 *      provided with the distribution.
19
 *
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
 * POSSIBILITY OF SUCH DAMAGE.
33
 *
34
 * The views and conclusions contained in the software and
35
 * documentation are those of the authors and should not be
36
 * interpreted as representing official policies, either expressed
37
 * or implied, of GRNET S.A.
38
 * </copyright>
39
 * -----------------------------------------------------------------------
40
 */
41
#endregion
42
using System;
43
using System.Collections.Generic;
44
using System.ComponentModel;
45
using System.ComponentModel.Composition;
46
using System.IO;
47
using System.Linq;
48
using System.Net;
49
using System.Text;
50
using System.Threading.Tasks;
51
using System.Windows;
52
using System.Windows.Controls;
53
using System.Windows.Forms;
54
using Caliburn.Micro;
55
using Pithos.Client.WPF.Properties;
56
using Pithos.Core;
57
using Pithos.Network;
58
using MessageBox = System.Windows.MessageBox;
59
using Screen = Caliburn.Micro.Screen;
60

    
61
namespace Pithos.Client.WPF.Preferences
62
{
63
    [Export(typeof(AddAccountViewModel))]
64
    public class AddAccountViewModel:Screen
65
    {
66

    
67
        private readonly List<string> _servers;
68

    
69
        public List<string> Servers
70
        {
71
            get { return _servers; }
72
        }
73

    
74
        private bool _isValidServer;
75
        public bool IsValidServer
76
        {
77
            get { return _isValidServer; }
78
            set
79
            {
80
                _isValidServer = value;
81
                NotifyOfPropertyChange(()=>IsValidServer);
82
            }
83
        }
84

    
85

    
86
        private string _currentServer;
87
        public string CurrentServer
88
        {
89
            get { return _currentServer; }
90
            set
91
            {
92
                if (!Uri.IsWellFormedUriString(value, UriKind.Absolute))
93
                {
94
                    IsValidServer = false;
95
                    throw new UriFormatException();
96
                }
97
                _currentServer = value;
98
                IsValidServer = true;
99
                HasValidCredentials = false;
100
                IsConfirmed = false;
101
                NotifyOfPropertyChange(()=>CurrentServer);
102
            }
103
        }
104

    
105
        private string _accountName;
106
        public string AccountName
107
        {
108
            get { return _accountName; }
109
            set
110
            {
111
                _accountName = value;
112
                NotifyOfPropertyChange(()=>AccountName);
113
                NotifyOfPropertyChange(() => HasCredentials);
114
            }
115
        }
116

    
117
        private string _token;
118
        public string Token
119
        {
120
            get { return _token; }
121
            set
122
            {
123
                _token = value;
124
                NotifyOfPropertyChange(()=>Token);
125
                NotifyOfPropertyChange(() => HasCredentials);
126
            }
127
        }
128

    
129
        private string _accountPath;
130
        public string AccountPath
131
        {
132
            get { return _accountPath; }
133
            set
134
            {
135
                _accountPath = value;
136
                NotifyOfPropertyChange(() => AccountPath);
137
                NotifyOfPropertyChange(() => HasAccountPath);
138
            }
139
        }
140

    
141

    
142
        public bool HasAccountPath
143
        {
144
            get { return !String.IsNullOrWhiteSpace(AccountPath); }
145
        }
146

    
147
        public bool HasCredentials
148
        {
149
            get { return !(String.IsNullOrWhiteSpace(AccountName) || String.IsNullOrWhiteSpace(Token) ) ; }
150
        }
151

    
152

    
153
        private bool  _isConfirmed;
154

    
155
        public bool IsConfirmed
156
        {
157
            get { return _isConfirmed; }
158
            set
159
            {
160
                _isConfirmed = value;
161
                HasValidCredentials = false;
162
                NotifyOfPropertyChange(() => IsConfirmed);
163
            }
164
        }
165

    
166

    
167
        private bool _isAccountActive;
168

    
169
        public bool IsAccountActive
170
        {
171
            get { return _isAccountActive; }
172
            set
173
            {
174
                _isAccountActive = value;
175
                NotifyOfPropertyChange(() => IsAccountActive);
176
            }
177
        }
178

    
179
        public void SelectAccount()
180
        {
181
            using (var dlg = new FolderBrowserDialog{Description="Please select a folder to store local files. Pithos will create a new folder called Okeanos under the folder you specify."})
182
            {
183
                //Ask the user to select a folder
184
                //Note: We need a parent window here, which we retrieve with GetView            
185
                var view = (Window)GetView();
186
                if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view)))
187
                    return;
188

    
189
                AccountPath= dlg.SelectedPath;
190
            }
191
        }
192

    
193

    
194
        public async void RetrieveCredentials()
195
        {
196
            SetBusy("Waiting for credentials.", "Please enter your credentials in the Pithos logon page");
197
            IsConfirmed = false;
198

    
199
            try
200
            {
201
                var credentials = await PithosAccount.RetrieveCredentials(Settings.Default.PithosLoginUrl);
202
                AccountName = credentials.UserName;
203
                Token = credentials.Password;
204

    
205
                IsConfirmed = true;
206
            }
207
            catch (PithosException exc)
208
            {
209
                ClearBusy();
210
                MessageBox.Show(exc.Message, "Unable to retrieve credentials");
211
            }
212
            catch (Exception exc)
213
            {
214
                IsConfirmed = false;
215
                MessageBox.Show(exc.ToString(), "Error");
216
                throw;
217
            }
218
            finally
219
            {
220
                ClearBusy();
221
                
222
                (this.GetView() as Window).Activate();
223
            }
224

    
225
        }
226

    
227
        public AddAccountViewModel()
228
        {
229
            _servers=new List<string>
230
                         {
231
                             Settings.Default.ProductionServer, 
232
                             Settings.Default.DevelopmentServer
233
                         };
234
            CurrentServer = _servers[0];
235
        }
236

    
237
        private bool _hasValidCredentials;
238
        public bool HasValidCredentials
239
        {
240
            get { return _hasValidCredentials; }
241
            set
242
            {
243
                _hasValidCredentials = value;
244
                NotifyOfPropertyChange(()=>HasValidCredentials);
245
            }
246
        }
247

    
248
        private string _validationMessage;
249
        public string ValidationMessage
250
        {
251
            get { return _validationMessage; }
252
            set
253
            {
254
                _validationMessage = value;
255
                NotifyOfPropertyChange(()=>ValidationMessage);
256
            }
257
        }
258

    
259
        private bool _isWorking;
260
        public bool IsWorking
261
        {
262
            get { return _isWorking; }
263
            set
264
            {
265
                _isWorking = value;
266
                NotifyOfPropertyChange(()=>IsWorking);
267
            }
268
        }
269

    
270
        private string _busyTitle;
271
        public string BusyTitle
272
        {
273
            get { return _busyTitle; }
274
            set
275
            {
276
                _busyTitle = value;
277
                NotifyOfPropertyChange(()=>BusyTitle);
278
            }
279
        }
280

    
281
        private string _busyDetail;
282
        public string BusyDetail
283
        {
284
            get { return _busyDetail; }
285
            set
286
            {
287
                _busyDetail = value;
288
                NotifyOfPropertyChange(()=>BusyDetail);
289
            }
290
        }
291

    
292
        private void SetBusy(string title,string detail)
293
        {
294
            IsWorking = true;
295
            BusyTitle = title;
296
            BusyDetail = detail;
297
        }
298

    
299
        private void ClearBusy()
300
        {
301
            IsWorking = false;
302
            BusyTitle = "";
303
            BusyDetail = "";
304
            
305
        }
306

    
307
        public async void TestAccount()
308
        {
309
            try
310
            {
311
                SetBusy("Validating Credentials", "");
312
                var client = new CloudFilesClient(AccountName, Token) { AuthenticationUrl = CurrentServer,/*Proxy=Proxy */};                
313
                var containers = await TaskEx.Run(() =>
314
                                                      {
315
                                                          client.Authenticate();
316
                                                          return client.ListContainers(AccountName);
317
                                                      });
318
                HasValidCredentials = true;
319
                ValidationMessage = "Credentials Validated";
320
            }
321
/*
322
            catch (AggregateException exc)
323
            {
324
                exc.Handle(ex=>
325
                               {
326
                                   HasValidCredentials = false;
327
                                   MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop);                                                   
328
                               });
329
            }
330
*/
331
            catch
332
            {
333
                HasValidCredentials = false;
334
                MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop);
335
                ValidationMessage = "Credentials validation failed";
336
            }
337
            finally
338
            {
339
                ClearBusy();
340
            }
341
        }
342

    
343
    }
344
}