/
EditAccount.xaml.cs
312 lines (272 loc) · 11.4 KB
/
EditAccount.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Windows.Media.Animation;
using OwnCloud.Data;
using OwnCloud.Data.DAV;
using OwnCloud.Net;
using OwnCloud.Extensions;
namespace OwnCloud
{
public partial class EditAccount : PhoneApplicationPage
{
private TemporaryData _accountForm = new TemporaryData();
private bool _editMode = false;
public struct AsyncHttpResponse
{
public HttpWebRequest Request;
public Account AssociatedAccount;
public AsyncHttpResponse(HttpWebRequest request, Account account)
{
Request = request;
AssociatedAccount = account;
}
}
public EditAccount()
{
DataContext = new AccountDataContext();
InitializeComponent();
// NavigationContext is not available in constructor
this.Loaded += new RoutedEventHandler(PageLoaded);
// Translate unsupported XAML bindings
ApplicationBar.TranslateButtons();
}
private void ProtocolButtonTap(object sender, System.Windows.Input.GestureEventArgs e)
{
var button = sender as Button;
button.Content = button.Content.ToString() == "http" ? "https" : "http";
}
private void PageUnloaded(object sender, RoutedEventArgs e)
{
// Save form details if the user has switched
// and want to come back
// exclude Password
Account form = (DataContext as AccountDataContext).CurrentAccount.GetCopy();
form.Password = "";
_accountForm.Store(_editMode ? "EditAccountForm" : "AddAccountForm", form);
// Clear data context
DataContext = null;
}
EventHandler overlayFadeOut_Completed;
EventHandler overlayFadeIn_Completed;
Storyboard overlayFadeIn;
Storyboard overlayFadeOut;
private void SaveTap(object sender, EventArgs e)
{
// this works but the on blur event is called by leaving the page
// and will manipulate the current object so locking is required for
// credentials
(sender as ApplicationBarIconButton).UpdateBindingSource();
Account account = (DataContext as AccountDataContext).CurrentAccount;
if (!account.CanSave())
{
return;
}
// show overlay
this.Overlay.Visibility = System.Windows.Visibility.Visible;
overlayFadeIn = this.Resources["TestConnectionOverlayFadeIn"] as Storyboard;
overlayFadeOut = this.Resources["TestConnectionOverlayFadeOut"] as Storyboard;
// aware memory restitant event handler
if (overlayFadeOut_Completed == null)
{
overlayFadeOut_Completed = new EventHandler(delegate
{
this.OverlayProgressBar.IsIndeterminate = false;
this.Overlay.Visibility = System.Windows.Visibility.Collapsed;
});
overlayFadeOut.Completed += overlayFadeOut_Completed;
}
if (overlayFadeIn_Completed == null)
{
overlayFadeIn_Completed = new EventHandler(OverlayFadeIn);
overlayFadeIn.Completed += overlayFadeIn_Completed;
}
// start overlay
overlayFadeIn.Begin();
}
private void OverlayFadeIn(object obj, EventArgs e)
{
// Test Connection
Account account = (DataContext as AccountDataContext).CurrentAccount;
this.OverlayProgressBar.IsIndeterminate = true;
try
{
HttpWebRequest request = HttpWebRequest.CreateHttp(String.Format("{0:g}://{1:g}", account.Protocol, account.ServerDomain));
request.BeginGetResponse(new AsyncCallback(OnHTTPResponse), new AsyncHttpResponse(request, account));
}
catch (Exception)
{
// uri malformed
OnConnectFailed(account);
}
}
private void OnHTTPResponse(IAsyncResult result)
{
AsyncHttpResponse state = (AsyncHttpResponse)result.AsyncState;
var success = false;
try
{
state.Request.EndGetResponse(result);
success = true;
}
catch (WebException)
{
if (state.AssociatedAccount.Protocol == "https")
{
// try to fetch certificate
try
{
var tls = new TLSHandshake();
tls.SetServerNameExtension(state.AssociatedAccount.Hostname);
var socket = new StreamSocket();
socket.Connect(state.AssociatedAccount.Hostname, TLSHandshake.TLS_HTTP_PORT);
socket.Write(tls.CreateClientHello());
DateTime startTime = DateTime.Now;
while (true)
{
var data = socket.Read();
if (data.Length > 0)
{
var cert = tls.FindPacket(data, TLSHandshake.TLS_HANDSHAKE_CERTIFICATE);
if (cert.Length > 0)
{
var details = tls.GetCertificateDetails(cert);
if (details.Count > 0)
{
var certDetails = details[0];
if (certDetails.ContainsKey("CN"))
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("EditAccountPage_Connection_Rejected".Translate(state.AssociatedAccount.Hostname, certDetails["CN"], certDetails["ValidAfter"], certDetails["ValidTo"]), "EditAccountPage_Connection_Rejected_Caption".Translate(), MessageBoxButton.OK);
overlayFadeOut.Begin();
});
return;
}
}
break;
}
}
if (DateTime.Now.Subtract(startTime).TotalSeconds > 5)
{
break;
}
}
}
catch (Exception)
{
// Host not reachable, no SSL host or TLS version not supported
}
}
}
catch (Exception)
{
// HTTPWebRequest has failed
}
Dispatcher.BeginInvoke(() =>
{
if (success)
{
overlayFadeOut.Begin();
StoreAccount(state.AssociatedAccount);
}
else
{
overlayFadeOut.Begin();
OnConnectFailed(state.AssociatedAccount);
}
});
}
private void OnConnectFailed(Account account)
{
if (MessageBox.Show("EditAccountPage_Confirm_Store".Translate(account.Protocol, account.ServerDomain), "EditAccountPage_Confirm_Store_Caption".Translate(), MessageBoxButton.OKCancel) == MessageBoxResult.OK)
{
overlayFadeOut.Begin();
StoreAccount(account);
}
else
{
overlayFadeOut.Begin();
}
}
private void StoreAccount(Account account)
{
// encrypt data
account.StoreCredentials();
// edit/insert
if (!_editMode) {
App.DataContext.Accounts.InsertOnSubmit(account);
}
App.DataContext.SubmitChanges();
// temporary data can be deleted
_accountForm.Remove(_editMode ? "EditAccountForm" : "AddAccountForm");
NavigationService.GoBack();
}
private void PageLoaded(object sender, RoutedEventArgs e)
{
if (NavigationContext.QueryString.ContainsKey("mode") && NavigationContext.QueryString.ContainsKey("account"))
{
_editMode = NavigationContext.QueryString["mode"] == "edit";
try
{
var account = App.DataContext.LoadAccount(NavigationContext.QueryString["account"]);
account.RestoreCredentials();
(DataContext as AccountDataContext).CurrentAccount = account;
}
catch (Exception)
{
// should not happen
}
}
else
{
_editMode = false;
}
(DataContext as AccountDataContext).PageMode = _editMode ? "EditAccountPage_EditAccount".Translate() : "EditAccountPage_AddAccount".Translate();
// If there are any stored form details
// include here if DataContext is empty
if ((DataContext as AccountDataContext).CurrentAccount == null)
{
Account account = (Account)_accountForm.Restore(_editMode ? "EditAccountForm" : "AddAccountForm", new Account());
account.RestoreCredentials();
(DataContext as AccountDataContext).CurrentAccount = account;
}
}
private void Button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
Account account = (DataContext as AccountDataContext).CurrentAccount;
// Some Web DAV Test
var dav_request = DAVRequestHeader.CreateListing(account.WebDAVPath);
dav_request.Headers[Header.Depth] = HeaderAttribute.MethodDepth.ApplyInfinityNoRoot;
var dav = new WebDAV(account.GetUri(), account.GetCredentials());
dav.StartRequest(dav_request, DAVRequestBody.CreateAllPropertiesListing(), null, DAVResult);
}
private void DAVResult(DAVRequestResult result, object userObj)
{
if (result.Request.ErrorOccured) return;
Utility.DebugXML(result.GetRawResponse());
foreach (DAVRequestResult.Item item in result.Items)
{
Utility.Debug(String.Format("Name={0:g}, Reference={1:g}, Local={7:g}, Last Modified={2:g}, Quota={3:g}/{4:g}, ETag={5:g}, Type={6:g}",
item.DisplayName,
item.Reference,
item.LastModified,
item.QuotaUsed,
item.QuotaAvailable,
item.ETag,
item.ResourceType,
item.LocalReference
));
}
}
}
}