private void DoReloadCourses(string url) { try { _showCalendarViewerAfterReload = false; UpdateProgress(TaskbarItemProgressState.Indeterminate, 0, "Connecting to Server (Checking for Changes)"); // // Check for Changes // // make HTTP request WebRequest request = WebRequest.Create(url); request.Proxy = null; // use HEAD to determine chnages request.Method = "HEAD"; // get response HttpWebResponse response = (HttpWebResponse)request.GetResponse(); long contentLength = response.ContentLength; DateTime lastChange = response.LastModified; // check for changes // (it seems the FHV timetable service generates the timetables // on-the-fly and always returns the current date as "LastModified" // this makes caching impossible, but we are prepared!) if (_dataService.CalendarLastDownload >= lastChange) { return; } // // Download the calendar // // We need to load the file UpdateProgress(TaskbarItemProgressState.Indeterminate, 0, "Connecting to Server (Download)"); request = WebRequest.Create(url); request.Proxy = null; // get response response = (HttpWebResponse)request.GetResponse(); // get stream for reading ByteCountingStream remoteStream = new ByteCountingStream(response.GetResponseStream()); Encoding encoding = Encoding.UTF8; StreamReader reader = new StreamReader(remoteStream, encoding); // create parser UpdateProgress(TaskbarItemProgressState.Indeterminate, 0, "Setup default data"); FhvICalParser parser = new FhvICalParser(_dataService); parser.Prepare(); // load data UpdateProgress( contentLength == -1 ? TaskbarItemProgressState.Indeterminate : TaskbarItemProgressState.Normal, 0, "Reload courses from event"); string line; while ((line = reader.ReadLine()) != null) { if (contentLength != -1) { double percentage = remoteStream.ReadByteCount / (double)contentLength; if (percentage > 1) { UpdateProgress(TaskbarItemProgressState.Indeterminate, 0, "Reload courses from event"); } else { UpdateProgress(percentage); } } parser.ProcessLine(line); } // store everything in the new database UpdateProgress(TaskbarItemProgressState.Indeterminate, 0, "Saving data to local storage"); if(_dataService.ResetDatabase(parser.ParsedData)) { // update settings for next download _dataService.CalendarFileSize = remoteStream.ReadByteCount; _dataService.CalendarLastDownload = lastChange; } else { DownloadError("Could not save loaded data in the local storage"); } //#region Debug Code //StringBuilder builder = new StringBuilder(); //foreach (var wing in parser.ParsedData) //{ // builder.AppendLine(wing.Name); // foreach (var level in wing.Level) // { // builder.AppendLine(" " + level.Name); // foreach (var room in level.Room) // { // builder.AppendLine(" " + room.RoomId); // foreach (var course in room.Course) // { // builder.AppendLine(string.Format(" {0}({1} - {2})", course.Module, course.StartTime, course.EndTime)); // } // } // } //} //DispatcherHelper.UIDispatcher.BeginInvoke(new Action( // () => // { // DebugOutput = builder.ToString(); // })); //#endregion } catch (Exception e) { DownloadError(e.Message); Debug.Fail(e.Message, e.ToString()); } finally { UpdateProgress(TaskbarItemProgressState.None, 0, "Ready"); // We have a lot of unneeded objects in our memory. let the GC get rid of them GC.Collect(); } }
private void DoReloadCourses(string url) { try { _showCalendarViewerAfterReload = false; _progressViewModel.UpdateProgress(TaskbarItemProgressState.Indeterminate, 0, "Connecting to Server (Checking for Changes)"); // // Check for Changes // // make HTTP request WebRequest request = WebRequest.Create(url); request.Proxy = null; // use HEAD to determine chnages request.Method = "HEAD"; // get response HttpWebResponse response = (HttpWebResponse)request.GetResponse(); long contentLength = response.ContentLength; DateTime lastChange = response.LastModified; // check for changes // (it seems the FHV timetable service generates the timetables // on-the-fly and always returns the current date as "LastModified" // this makes caching impossible, but we are prepared!) if (_dataService.CalendarLastDownload >= lastChange) { return; } // // Download the calendar // // We need to load the file _progressViewModel.UpdateProgress(TaskbarItemProgressState.Indeterminate, 0, "Connecting to Server (Download)"); request = WebRequest.Create(url); request.Proxy = null; // get response response = (HttpWebResponse)request.GetResponse(); // get stream for reading ByteCountingStream remoteStream = new ByteCountingStream(response.GetResponseStream()); Encoding encoding = Encoding.UTF8; StreamReader reader = new StreamReader(remoteStream, encoding); // create parser _progressViewModel.UpdateProgress(TaskbarItemProgressState.Indeterminate, 0, "Setup default data"); FhvICalParser parser = new FhvICalParser(_dataService); parser.Prepare(); // load data _progressViewModel.UpdateProgress( contentLength == -1 ? TaskbarItemProgressState.Indeterminate : TaskbarItemProgressState.Normal, 0, "Reload courses from event"); string line; while ((line = reader.ReadLine()) != null) { if (contentLength != -1) { double percentage = remoteStream.ReadByteCount / (double)contentLength; if (percentage > 1) { _progressViewModel.UpdateProgress(TaskbarItemProgressState.Indeterminate, 0, "Reload courses from event"); } else { _progressViewModel.UpdateProgress(percentage); } } parser.ProcessLine(line); } // store everything in the new database _progressViewModel.UpdateProgress(TaskbarItemProgressState.Indeterminate, 0, "Saving data to local storage"); if (_dataService.ResetDatabase(parser.ParsedData)) { // update settings for next download _dataService.CalendarFileSize = remoteStream.ReadByteCount; _dataService.CalendarLastDownload = lastChange; _progressViewModel.InvokeDataReloaded(EventArgs.Empty); } else { DownloadError("Could not save loaded data in the local storage"); } } catch (Exception e) { DownloadError(e.Message); } finally { _progressViewModel.UpdateProgress(TaskbarItemProgressState.None, 0, "Ready"); // We have a lot of unneeded objects in our memory. let the GC get rid of them GC.Collect(); } }