/
Siding.xaml.cs
348 lines (310 loc) · 13.2 KB
/
Siding.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;
using Microsoft.Phone.Shell;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using Microsoft.Live;
namespace directSiding
{
public partial class Siding : PhoneApplicationPage
{
Uri _uriLogin = new Uri("https://intrawww.ing.puc.cl/siding/index.phtml", UriKind.Absolute);
Uri _uriCursos = new Uri("https://intrawww.ing.puc.cl/siding/dirdes/ingcursos/cursos/vista.phtml", UriKind.Absolute);
string _skyDriveFolderName = "directSIDING";
string _skyDriveFolderID = string.Empty;
IsolatedStorageSettings settings;
LiveConnectClient client;
string filename;
Stack<Uri> _history;
// The Uri of the current file that is trying to load
Uri _fileToDownload;
// The browser is navigating
bool _navigating;
// BackgroundWorker for downloading files in the background
private BackgroundWorker _bw;
private WebClient _webClient;
public Siding()
{
InitializeComponent();
settings = IsolatedStorageSettings.ApplicationSettings;
browser.IsScriptEnabled = true;
_history = new Stack<Uri>();
_navigating = false;
_bw = new BackgroundWorker();
_bw.DoWork += _bw_DoWork;
_webClient = new WebClient();
_webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(_webClient_DownloadProgressChanged);
_webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(_webClient_OpenReadCompleted);
var encoding = System.Text.Encoding.GetEncoding("iso-8859-1");
string postData = "login="+ settings["username"]+"&passwd="+ settings["password"]+"&sw=&sh=&cd=";
string headers = "Content-Type: application/x-www-form-urlencoded\r\n"+
"Connection: keep-alive\r\n" +
"Referer: http://www.ing.puc.cl/\r\n"+
"Accept-Language: es-cl,es;q=0.8,en-us;q=0.5,en;q=0.3\r\n"+
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
browser.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(browser_firstTime);
browser.Navigate(_uriLogin, encoding.GetBytes(postData), headers);
}
private void _bw_DoWork(object sender, DoWorkEventArgs e)
{
var worker = sender as BackgroundWorker;
Dispatcher.BeginInvoke(() =>
{
progressBar.Visibility = System.Windows.Visibility.Visible;
ApplicationTitle.Text = "Descargando...";
});
_webClient.OpenReadAsync(_fileToDownload, e.Argument);
}
void _webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
try
{
if (e.Result != null)
{
// Download file
var contentDisposition = (sender as WebClient).ResponseHeaders["Content-Disposition"];
filename = "filename=\"";
var i = contentDisposition.IndexOf(filename) + filename.Length;
var j = contentDisposition.IndexOf("\"", i);
filename = "/shared/transfers/" + contentDisposition.Substring(i, j - i);
using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
using (var f = new IsolatedStorageFileStream(filename, System.IO.FileMode.Create, storage))
{
long fileNameLength = (long)e.Result.Length;
byte[] byteImage = new byte[fileNameLength];
e.Result.Read(byteImage, 0, byteImage.Length);
f.Write(byteImage, 0, byteImage.Length);
}
// Check if the folder exists
client = new LiveConnectClient(App.Session);
client.GetCompleted += client_GetFolderInfoCompleted;
client.GetAsync("me/skydrive/files?filter=folders,albums");
}
}
catch (Exception) { }
}
private void client_GetFolderInfoCompleted(object sender, LiveOperationCompletedEventArgs e)
{
if (e.Error == null)
{
var folders = e.Result["data"] as List<object>;
foreach (IDictionary<string, object> folder in folders)
{
if (folder["name"].ToString() == _skyDriveFolderName)
{
_skyDriveFolderID = folder["id"].ToString();
break;
}
}
if (_skyDriveFolderID == string.Empty)
{
var skyDriveFolderData = new Dictionary<string, object>();
skyDriveFolderData.Add("name", _skyDriveFolderName);
client.PostCompleted += client_CreateFolderCompleted;
client.PostAsync("me/skydrive", skyDriveFolderData);
}
else
uploadFile();
}
else
{
MessageBox.Show(e.Error.Message);
}
}
private void client_CreateFolderCompleted(object sender, LiveOperationCompletedEventArgs e)
{
if (e.Error == null)
{
var folder = e.Result;
_skyDriveFolderID = folder["id"].ToString();
uploadFile();
}
else
{
MessageBox.Show(e.Error.Message, "No pudimos usar Skydrive :(", MessageBoxButton.OK);
}
}
private void uploadFile()
{
if (String.IsNullOrEmpty(_skyDriveFolderID))
return;
Dispatcher.BeginInvoke(() =>
{
ApplicationTitle.Text = "Subiendo a Skydrive...";
});
this.client.BackgroundUploadCompleted += client_BackgroundUploadCompleted;
// Upload file
try
{
client.BackgroundUploadAsync(_skyDriveFolderID, new Uri(filename, UriKind.Relative), OverwriteOption.Overwrite);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
void client_BackgroundUploadCompleted(object sender, LiveOperationCompletedEventArgs e)
{
if (e.Error == null)
{
Dispatcher.BeginInvoke(() =>
{
progressBar.Visibility = System.Windows.Visibility.Collapsed;
ApplicationTitle.Text = "Listo :)";
});
// Remove the file from the IS
using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
{
storage.DeleteFile(filename);
}
ThreadPool.QueueUserWorkItem(updateTitle);
var file = e.Result;
var browser = new Microsoft.Phone.Tasks.WebBrowserTask();
browser.Uri = new Uri(file["source"] as string, UriKind.Absolute);
browser.Show();
}
}
void _webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
try
{
progressBar.Value = (double)e.ProgressPercentage;
}
catch (Exception)
{
}
}
void browser_firstTime(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
var cookies = browser.GetCookies();
var cookiesStr = "";
foreach (var cookie in cookies)
{
cookiesStr = cookie.ToString();
}
_webClient.Headers[HttpRequestHeader.Cookie] = cookiesStr;
browser.Navigated -= browser_firstTime;
if ((bool)(Application.Current as App).settings["redirect"])
{
_history.Pop();
browser.Navigate(_uriCursos);
}
}
private void btnRefreshOrStop_Click(object sender, EventArgs e)
{
if (_navigating)
{
// Cancel navigation
try
{
browser.InvokeScript("eval", "document.execCommand('Stop');");
}
catch (Exception) { }
_navigating = false;
progressBar.Visibility = System.Windows.Visibility.Collapsed;
// Restore refresh btn
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IconUri = new Uri("/Images/appbar.refresh.rest.png", UriKind.Relative);
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = "Actualizar";
}
else
{
// Reload the Uri
browser.Navigate(_history.Peek());
}
}
private void btnConfig_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/Config.xaml", UriKind.Relative));
}
private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
if (_history.Count > 1)
{
_history.Pop();
browser.Navigate(_history.Pop());
e.Cancel = true;
}
}
private void browser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
ThreadPool.QueueUserWorkItem(updateTitle);
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
NavigationService.RemoveBackEntry();
}
private void browser_NavigationFailed(object sender, System.Windows.Navigation.NavigationFailedEventArgs e)
{
var message = "Algo salió mal.\r\nRevisa que tengas conexión a Internet.\r\nSi usas WP7, asegurate de tener instalado el certificado correspondiente.\r\n";
if (e.Exception != null)
message += e.Exception.Message;
MessageBox.Show(message, "¡Ups!", MessageBoxButton.OK);
}
private void updateTitle(Object o)
{
Thread.Sleep(500);
Dispatcher.BeginInvoke(() =>
{
try
{
string title = (string)browser.InvokeScript("eval", "document.querySelector('html body table tbody tr td.ColorFondoZonaTrabajo table tbody tr td table tbody tr td.ColorFondoResaltado b').innerHTML");
ApplicationTitle.Text = "DIRECTSIDING - " + title;
_navigating = false;
progressBar.Visibility = System.Windows.Visibility.Collapsed;
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IconUri = new Uri("/Images/appbar.refresh.rest.png", UriKind.Relative);
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = "Actualizar";
//ApplicationBar.Mode = ApplicationBarMode.Minimized;
}
catch (SystemException)
{
ThreadPool.QueueUserWorkItem(updateTitle);
}
});
}
private void browser_Navigating(object sender, NavigatingEventArgs e)
{
if (e.Uri.AbsoluteUri.Contains("id_archivo="))
{
var uri = e.Uri.AbsoluteUri;
var curso = "id_curso_ic=";
var i = uri.IndexOf(curso) + curso.Length;
var archivo = "&id_archivo=";
var j = uri.IndexOf(archivo);
var idCurso = uri.Substring(i, j - i);
var idArchivo = uri.Substring(j + archivo.Length);
_fileToDownload = e.Uri;
if (App.Session != null)
{
_bw.RunWorkerAsync(String.Format("{0}-{1}", idCurso, idArchivo));
}
else
{
var res = MessageBox.Show("Requieres iniciar sesión en Skydrive. Ve a la página de configuración", "No puedes descargar archivos", MessageBoxButton.OKCancel);
if(res == MessageBoxResult.OK)
NavigationService.Navigate(new Uri("/Config.xaml", UriKind.Relative));
}
e.Cancel = true;
}
else
{
_history.Push(e.Uri);
_navigating = true;
progressBar.Visibility = System.Windows.Visibility.Visible;
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IconUri = new Uri("/Images/appbar.cancel.rest.png", UriKind.Relative);
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = "Detener";
//ApplicationBar.Mode = ApplicationBarMode.Default;
}
}
}
}