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;
45 using System.Threading.Tasks;
47 using System.Windows.Forms;
48 using Pithos.Client.WPF.Properties;
50 using MessageBox = System.Windows.MessageBox;
51 using Screen = Caliburn.Micro.Screen;
53 namespace Pithos.Client.WPF.Preferences
55 [Export(typeof(AddAccountViewModel))]
56 public class AddAccountViewModel:Screen
59 private readonly List<string> _servers;
61 public List<string> Servers
63 get { return _servers; }
66 private bool _isValidServer;
67 public bool IsValidServer
69 get { return _isValidServer; }
72 _isValidServer = value;
73 NotifyOfPropertyChange(()=>IsValidServer);
78 private string _currentServer;
79 public string CurrentServer
81 get { return _currentServer; }
84 if (!Uri.IsWellFormedUriString(value, UriKind.Absolute))
86 IsValidServer = false;
87 throw new UriFormatException();
89 _currentServer = value;
91 HasValidCredentials = false;
93 NotifyOfPropertyChange(()=>CurrentServer);
97 private string _accountName;
98 public string AccountName
100 get { return _accountName; }
103 _accountName = value;
104 NotifyOfPropertyChange(()=>AccountName);
105 NotifyOfPropertyChange(() => HasCredentials);
109 private string _token;
112 get { return _token; }
116 NotifyOfPropertyChange(()=>Token);
117 NotifyOfPropertyChange(() => HasCredentials);
121 private string _accountPath;
122 public string AccountPath
124 get { return _accountPath; }
127 _accountPath = value;
128 NotifyOfPropertyChange(() => AccountPath);
129 NotifyOfPropertyChange(() => HasAccountPath);
134 public bool HasAccountPath
136 get { return !String.IsNullOrWhiteSpace(AccountPath); }
139 public bool HasCredentials
141 get { return !(String.IsNullOrWhiteSpace(AccountName) || String.IsNullOrWhiteSpace(Token) ) ; }
145 private bool _isConfirmed;
147 public bool IsConfirmed
149 get { return _isConfirmed; }
152 _isConfirmed = value;
153 HasValidCredentials = false;
154 NotifyOfPropertyChange(() => IsConfirmed);
159 private bool _isAccountActive;
161 public bool IsAccountActive
163 get { return _isAccountActive; }
166 _isAccountActive = value;
167 NotifyOfPropertyChange(() => IsAccountActive);
171 public void SelectAccount()
173 using (var dlg = new FolderBrowserDialog{Description=Resources.AddAccountViewModel_SelectAccount_Please_select_a_folder})
175 //Ask the user to select a folder
176 //Note: We need a parent window here, which we retrieve with GetView
177 var view = (Window)GetView();
178 if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view)))
181 AccountPath= dlg.SelectedPath;
186 public async void RetrieveCredentials()
188 SetBusy("Waiting for credentials.", "Please enter your credentials in the Pithos logon page");
193 var credentials = await PithosAccount.RetrieveCredentials(Settings.Default.PithosLoginUrl);
194 AccountName = credentials.UserName;
195 Token = credentials.Password;
199 catch (PithosException exc)
202 MessageBox.Show(exc.Message, "Unable to retrieve credentials");
204 catch (Exception exc)
207 MessageBox.Show(exc.ToString(), "Error");
214 ((Window) GetView()).Activate();
219 public AddAccountViewModel()
221 _servers=new List<string>
223 Settings.Default.ProductionServer,
224 Settings.Default.DevelopmentServer
226 CurrentServer = _servers[0];
229 private bool _hasValidCredentials;
230 public bool HasValidCredentials
232 get { return _hasValidCredentials; }
235 _hasValidCredentials = value;
236 NotifyOfPropertyChange(()=>HasValidCredentials);
240 private string _validationMessage;
241 public string ValidationMessage
243 get { return _validationMessage; }
246 _validationMessage = value;
247 NotifyOfPropertyChange(()=>ValidationMessage);
251 private bool _isWorking;
252 public bool IsWorking
254 get { return _isWorking; }
258 NotifyOfPropertyChange(()=>IsWorking);
262 private string _busyTitle;
263 public string BusyTitle
265 get { return _busyTitle; }
269 NotifyOfPropertyChange(()=>BusyTitle);
273 private string _busyDetail;
274 public string BusyDetail
276 get { return _busyDetail; }
280 NotifyOfPropertyChange(()=>BusyDetail);
284 private void SetBusy(string title,string detail)
291 private void ClearBusy()
299 public async void TestAccount()
303 SetBusy("Validating Credentials", "");
304 var client = new CloudFilesClient(AccountName, Token) { AuthenticationUrl = CurrentServer,/*Proxy=Proxy */};
305 await TaskEx.Run(() =>
307 client.Authenticate();
308 return client.ListContainers(AccountName);
310 HasValidCredentials = true;
311 ValidationMessage = "Credentials Validated";
315 HasValidCredentials = false;
316 MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop);
317 ValidationMessage = "Credentials validation failed";