2 /* -----------------------------------------------------------------------
3 * <copyright file="AddAccountViewModel.cs" company="GRNet">
5 * Copyright 2011-2012 GRNET S.A. All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * 1. Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the following
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.
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.
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.
39 * -----------------------------------------------------------------------
43 using System.Collections.Generic;
44 using System.ComponentModel.Composition;
47 using System.Threading.Tasks;
49 using System.Windows.Forms;
50 using Pithos.Client.WPF.Properties;
52 using MessageBox = System.Windows.MessageBox;
53 using Screen = Caliburn.Micro.Screen;
55 namespace Pithos.Client.WPF.Preferences
57 [Export(typeof(AddAccountViewModel))]
58 public class AddAccountViewModel:Screen
61 private readonly List<string> _servers;
63 public List<string> Servers
65 get { return _servers; }
68 private bool _isValidServer;
69 public bool IsValidServer
71 get { return _isValidServer; }
74 _isValidServer = value;
75 NotifyOfPropertyChange(()=>IsValidServer);
80 private string _currentServer;
81 public string CurrentServer
83 get { return _currentServer; }
86 if (!Uri.IsWellFormedUriString(value, UriKind.Absolute))
88 IsValidServer = false;
89 throw new UriFormatException();
91 _currentServer = value;
93 HasValidCredentials = false;
95 NotifyOfPropertyChange(()=>CurrentServer);
99 private string _accountName;
100 public string AccountName
102 get { return _accountName; }
105 _accountName = value;
106 NotifyOfPropertyChange(()=>AccountName);
107 NotifyOfPropertyChange(() => HasCredentials);
111 private string _token;
114 get { return _token; }
118 NotifyOfPropertyChange(()=>Token);
119 NotifyOfPropertyChange(() => HasCredentials);
123 private string _accountPath;
124 public string AccountPath
126 get { return _accountPath; }
129 _accountPath = value;
130 NotifyOfPropertyChange(() => AccountPath);
131 NotifyOfPropertyChange(() => HasAccountPath);
136 public bool HasAccountPath
138 get { return !String.IsNullOrWhiteSpace(AccountPath); }
141 public bool HasCredentials
143 get { return !(String.IsNullOrWhiteSpace(AccountName) || String.IsNullOrWhiteSpace(Token) ) ; }
147 private bool _isConfirmed;
149 public bool IsConfirmed
151 get { return _isConfirmed; }
154 _isConfirmed = value;
155 HasValidCredentials = false;
156 NotifyOfPropertyChange(() => IsConfirmed);
161 private bool _isAccountActive;
163 public bool IsAccountActive
165 get { return _isAccountActive; }
168 _isAccountActive = value;
169 NotifyOfPropertyChange(() => IsAccountActive);
174 private bool _shouldCreateOkeanosFolder;
175 public bool ShouldCreateOkeanosFolder
177 get { return _shouldCreateOkeanosFolder; }
180 _shouldCreateOkeanosFolder = value;
181 NotifyOfPropertyChange(()=>ShouldCreateOkeanosFolder);
185 public void SelectAccount()
187 using (var dlg = new FolderBrowserDialog{Description=Resources.AddAccountViewModel_SelectAccount_Please_select_a_folder})
189 //Ask the user to select a folder
190 //Note: We need a parent window here, which we retrieve with GetView
191 var view = (Window)GetView();
192 if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view)))
195 AccountPath= dlg.SelectedPath;
197 ShouldCreateOkeanosFolder=Directory.EnumerateFileSystemEntries(AccountPath).Any();
202 public void RetrieveCredentials()
204 SetBusy("Waiting for credentials.", "Please enter your credentials in the Pithos logon page");
209 var loginUri=new Uri(new Uri(CurrentServer) , "login");
210 var credentials = PithosAccount.RetrieveCredentials(loginUri.ToString());
211 if (credentials == null)
213 AccountName = credentials.UserName;
214 Token = credentials.Password;
219 catch (PithosException exc)
222 MessageBox.Show(exc.Message, "Unable to retrieve credentials");
224 catch (Exception exc)
227 MessageBox.Show(exc.ToString(), "Error");
234 ((Window) GetView()).Activate();
238 TaskEx.Run(TestAccount);
242 public AddAccountViewModel()
244 _servers=new List<string>
246 Settings.Default.ProductionServer,
247 Settings.Default.DevelopmentServer
249 CurrentServer = _servers[0];
252 private bool _hasValidCredentials;
253 public bool HasValidCredentials
255 get { return _hasValidCredentials; }
258 _hasValidCredentials = value;
259 NotifyOfPropertyChange(()=>HasValidCredentials);
263 private string _validationMessage;
264 public string ValidationMessage
266 get { return _validationMessage; }
269 _validationMessage = value;
270 NotifyOfPropertyChange(()=>ValidationMessage);
274 private bool _isWorking;
275 public bool IsWorking
277 get { return _isWorking; }
281 NotifyOfPropertyChange(()=>IsWorking);
285 private string _busyTitle;
286 public string BusyTitle
288 get { return _busyTitle; }
292 NotifyOfPropertyChange(()=>BusyTitle);
296 private string _busyDetail;
297 public string BusyDetail
299 get { return _busyDetail; }
303 NotifyOfPropertyChange(()=>BusyDetail);
307 private void SetBusy(string title,string detail)
314 private void ClearBusy()
322 public async void TestAccount()
326 SetBusy("Validating Credentials", "");
327 var client = new CloudFilesClient(AccountName, Token) { AuthenticationUrl = CurrentServer,/*Proxy=Proxy */};
328 await TaskEx.Run(() =>
330 client.Authenticate();
331 return client.ListContainers(AccountName);
333 HasValidCredentials = true;
334 ValidationMessage = "Credentials Validated";
338 HasValidCredentials = false;
339 MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop);
340 ValidationMessage = "Credentials validation failed";