Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / Preferences / AddAccountViewModel.cs @ f734ab5b

History | View | Annotate | Download (6.7 kB)

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.Threading.Tasks;
9
using System.Windows;
10
using System.Windows.Controls;
11
using System.Windows.Forms;
12
using Caliburn.Micro;
13
using Pithos.Client.WPF.Properties;
14
using Pithos.Core;
15
using Pithos.Network;
16
using MessageBox = System.Windows.MessageBox;
17
using Screen = Caliburn.Micro.Screen;
18

    
19
namespace Pithos.Client.WPF.Preferences
20
{
21
    [Export(typeof(AddAccountViewModel))]
22
    public class AddAccountViewModel:Screen
23
    {
24

    
25
        private readonly List<string> _servers;
26

    
27
        public List<string> Servers
28
        {
29
            get { return _servers; }
30
        }
31

    
32
        private bool _isValidServer;
33
        public bool IsValidServer
34
        {
35
            get { return _isValidServer; }
36
            set
37
            {
38
                _isValidServer = value;
39
                NotifyOfPropertyChange(()=>IsValidServer);
40
            }
41
        }
42

    
43

    
44
        private string _currentServer;
45
        public string CurrentServer
46
        {
47
            get { return _currentServer; }
48
            set
49
            {
50
                if (!Uri.IsWellFormedUriString(value, UriKind.Absolute))
51
                {
52
                    IsValidServer = false;
53
                    throw new UriFormatException();
54
                }
55
                _currentServer = value;
56
                IsValidServer = true;
57
                HasValidCredentials = false;
58
                IsConfirmed = false;
59
                NotifyOfPropertyChange(()=>CurrentServer);
60
            }
61
        }
62

    
63
        private string _accountName;
64
        public string AccountName
65
        {
66
            get { return _accountName; }
67
            set
68
            {
69
                _accountName = value;
70
                NotifyOfPropertyChange(()=>AccountName);
71
                NotifyOfPropertyChange(() => HasCredentials);
72
            }
73
        }
74

    
75
        private string _token;
76
        public string Token
77
        {
78
            get { return _token; }
79
            set
80
            {
81
                _token = value;
82
                NotifyOfPropertyChange(()=>Token);
83
                NotifyOfPropertyChange(() => HasCredentials);
84
            }
85
        }
86

    
87
        private string _accountPath;
88
        public string AccountPath
89
        {
90
            get { return _accountPath; }
91
            set
92
            {
93
                _accountPath = value;
94
                NotifyOfPropertyChange(() => AccountPath);
95
                NotifyOfPropertyChange(() => HasAccountPath);
96
            }
97
        }
98

    
99

    
100
        public bool HasAccountPath
101
        {
102
            get { return !String.IsNullOrWhiteSpace(AccountPath); }
103
        }
104

    
105
        public bool HasCredentials
106
        {
107
            get { return !(String.IsNullOrWhiteSpace(AccountName) || String.IsNullOrWhiteSpace(Token) ) ; }
108
        }
109

    
110

    
111
        private bool  _isConfirmed;
112

    
113
        public bool IsConfirmed
114
        {
115
            get { return _isConfirmed; }
116
            set
117
            {
118
                _isConfirmed = value;
119
                HasValidCredentials = false;
120
                NotifyOfPropertyChange(() => IsConfirmed);
121
            }
122
        }
123

    
124

    
125
        private bool _isAccountActive;
126

    
127
        public bool IsAccountActive
128
        {
129
            get { return _isAccountActive; }
130
            set
131
            {
132
                _isAccountActive = value;
133
                NotifyOfPropertyChange(() => IsAccountActive);
134
            }
135
        }
136

    
137
        public void SelectAccount()
138
        {
139
            using (var dlg = new FolderBrowserDialog())
140
            {
141
                //Ask the user to select a folder
142
                //Note: We need a parent window here, which we retrieve with GetView            
143
                var view = (Window)GetView();
144
                if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view)))
145
                    return;
146

    
147
                AccountPath= dlg.SelectedPath;
148
            }
149
        }
150

    
151

    
152
        private bool _isRetrieving;
153
        public bool IsRetrieving
154
        {
155
            get { return _isRetrieving; }
156
            set
157
            {
158
                _isRetrieving = value;
159
                NotifyOfPropertyChange(() => IsRetrieving);
160
            }
161
        }
162

    
163
        public async void RetrieveCredentials()
164
        {
165
            IsRetrieving = true;
166
            IsConfirmed = false;
167

    
168
            try
169
            {
170
                var credentials = await PithosAccount.RetrieveCredentials(Settings.Default.PithosLoginUrl);
171
                AccountName = credentials.UserName;
172
                Token = credentials.Password;
173

    
174
                IsConfirmed = true;
175
            }
176
            catch (Exception exc)
177
            {
178
                IsConfirmed = false;
179
                MessageBox.Show(exc.ToString(), "Error");
180
                throw;
181
            }
182
            IsRetrieving = false;
183

    
184
        }
185

    
186
        public AddAccountViewModel()
187
        {
188
            _servers=new List<string>
189
                         {
190
                             Settings.Default.ProductionServer, 
191
                             Settings.Default.DevelopmentServer
192
                         };
193
            CurrentServer = _servers[0];
194
        }
195

    
196
        private bool _hasValidCredentials;
197
        public bool HasValidCredentials
198
        {
199
            get { return _hasValidCredentials; }
200
            set
201
            {
202
                _hasValidCredentials = value;
203
                NotifyOfPropertyChange(()=>HasValidCredentials);
204
            }
205
        }
206

    
207

    
208
        private bool _isValidating;
209
        public bool IsValidating
210
        {
211
            get { return _isValidating; }
212
            set
213
            {
214
                _isValidating = value;
215
                NotifyOfPropertyChange(()=>IsValidating);
216
            }
217
        }
218

    
219
        public async void TestAccount()
220
        {
221
            try
222
            {
223
                IsValidating = true;
224
                var client = new CloudFilesClient(AccountName, Token) {AuthenticationUrl = CurrentServer};                
225
                var containers = await TaskEx.Run(()=>
226
                                                      {
227
                                                          client.Authenticate();
228
                                                          return client.ListContainers(AccountName);
229
                                                      });
230
                HasValidCredentials = true;                
231
            }
232
            catch (Exception ex)
233
            {
234
                HasValidCredentials = false;
235
                MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop);
236
                throw;
237
            }
238
            finally
239
            {
240
                IsValidating = false;
241
            }
242
        }
243

    
244
    }
245
}