From 54ce4e6ee17e570001bbd93a7326b028a89f33b5 Mon Sep 17 00:00:00 2001 From: stubbfel Date: Tue, 3 Sep 2013 14:04:29 +0200 Subject: [PATCH] add pdflaunch --- CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj | 2 + .../CampusAppWP8/File/Exams/ExamFile.cs | 94 +++++++++++++++++++ .../CampusAppWP8/Model/BinaryModel.cs | 62 ++++++++++++ .../CampusAppWP8/Model/Exams/ExamModel.cs | 2 +- CampusAppWP8/CampusAppWP8/Model/MainModel.cs | 29 ++---- CampusAppWP8/CampusAppWP8/Model/XmlModel.cs | 2 +- .../CampusAppWP8/Pages/Exams/Exams.xaml | 21 ++--- .../CampusAppWP8/Pages/Exams/Exams.xaml.cs | 94 +++++++++++++++---- .../Resources/Constants.Designer.cs | 9 ++ .../CampusAppWP8/Resources/Constants.resx | 3 + CampusAppWP8/CampusAppWP8/Utility/File.cs | 41 ++++---- .../CampusAppWP8/Utility/HttpRequest.cs | 4 + 12 files changed, 286 insertions(+), 77 deletions(-) create mode 100644 CampusAppWP8/CampusAppWP8/File/Exams/ExamFile.cs create mode 100644 CampusAppWP8/CampusAppWP8/Model/BinaryModel.cs diff --git a/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj b/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj index 78353d3f..8c591a6c 100644 --- a/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj +++ b/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj @@ -105,6 +105,8 @@ + + diff --git a/CampusAppWP8/CampusAppWP8/File/Exams/ExamFile.cs b/CampusAppWP8/CampusAppWP8/File/Exams/ExamFile.cs new file mode 100644 index 00000000..bbd8ceb4 --- /dev/null +++ b/CampusAppWP8/CampusAppWP8/File/Exams/ExamFile.cs @@ -0,0 +1,94 @@ +//----------------------------------------------------------------------- +// +// Company copyright tag. +// +// stubbfel +// 03.09.2013 +//---------------------------------------------------------------------- + +namespace CampusAppWP8.File.Exams +{ + using System.IO; + using CampusAppWP8.Model; + using Windows.Storage; + + /// Exam file. + /// Stubbfel, 03.09.2013. + public class ExamFile : BinaryModel + { + /// The storage file. + private StorageFile storageFile; + + /// Initializes a new instance of the ExamFile class. + /// Stubbfel, 03.09.2013. + /// Filename of the file. + /// URL of the document. + public ExamFile(string fileName, string url) + : base(ModelType.FileAndFeed, fileName, url) + { + this.IsFileUpToDateOnLoad += new IsFileUpToDate(this.CheckIsFileUpToDate); + this.IsModelUpToDateOnLoad += new IsModelUpToDate(this.CheckIsModelUpToDate); + this.IsFileUpToDateOnSave += new IsFileUpToDate(this.CheckIsFileUpToDate); + } + + /// Executes the file operation. + /// Stubbfel, 03.09.2013. + public async void LaunchFile() + { + if (this.storageFile == null) + { + this.storageFile = await this.file.AsStorageFile(); + } + + if (this.storageFile != null) + { + var options = new Windows.System.LauncherOptions(); + Windows.System.Launcher.LaunchFileAsync(this.storageFile); + } + } + + /// Saves the and launch file. + /// Stubbfel, 03.09.2013. + public void SaveAndLaunchFile() + { + if (this.file.Exist()) + { + this.LaunchFile(); + } + else + { + this.OnSaved += new ExamFile.OnIO(this.LaunchFile); + this.SaveData(); + } + } + + /// Check is model up to date. + /// Stubbfel, 03.09.2013. + /// The model. + /// true if it succeeds, false if it fails. + private bool CheckIsModelUpToDate(byte[] model) + { + if (model == null) + { + return false; + } + + return true; + } + + /// Check is file up to date. + /// Stubbfel, 03.09.2013. + /// The model. + /// Information describing the file. + /// true if it succeeds, false if it fails. + private bool CheckIsFileUpToDate(byte[] model, FileInfo fileInfo) + { + if (fileInfo == null || !fileInfo.Exists || fileInfo.Length < 1) + { + return false; + } + + return true; + } + } +} diff --git a/CampusAppWP8/CampusAppWP8/Model/BinaryModel.cs b/CampusAppWP8/CampusAppWP8/Model/BinaryModel.cs new file mode 100644 index 00000000..6cf9fe7b --- /dev/null +++ b/CampusAppWP8/CampusAppWP8/Model/BinaryModel.cs @@ -0,0 +1,62 @@ +//----------------------------------------------------------------------- +// +// Company copyright tag. +// +// stubbfel +// 03.09.2013 +//---------------------------------------------------------------------- + +namespace CampusAppWP8.Model +{ + /// Binary model. + /// Stubbfel, 03.09.2013. + public abstract class BinaryModel : MainModel + { + /// Initializes a new instance of the BinaryModel class. + /// Stubbfel, 03.09.2013. + /// Type of the model. + /// Filename of the file. + /// URL of the document. + public BinaryModel(ModelType modelType, string fileName, string url) + : base(modelType, fileName, url) + { + } + + /// Initializes a new instance of the BinaryModel class. + /// Stubbfel, 03.09.2013. + /// Type of the model. + /// Name of the source. + public BinaryModel(ModelType modelType, string sourceName) + : base(modelType, sourceName) + { + } + + /// Deserialize model. + /// Stubbfel, 03.09.2013. + /// Information describing the model. + /// true if it succeeds, false if it fails. + protected override bool DeserializeModel(byte[] modelData) + { + bool retValue = true; + + if (modelData != null) + { + this.Model = modelData; + } + else + { + retValue = false; + } + + return retValue; + } + + /// Gets the serialize model. + /// Stubbfel, 03.09.2013. + /// an byte Array. + protected override byte[] SerializeModel() + { + return this.Model; + } + } +} diff --git a/CampusAppWP8/CampusAppWP8/Model/Exams/ExamModel.cs b/CampusAppWP8/CampusAppWP8/Model/Exams/ExamModel.cs index 5b68c879..57752d26 100644 --- a/CampusAppWP8/CampusAppWP8/Model/Exams/ExamModel.cs +++ b/CampusAppWP8/CampusAppWP8/Model/Exams/ExamModel.cs @@ -7,8 +7,8 @@ //---------------------------------------------------------------------- namespace CampusAppWP8.Model.Exams { - using CampusAppWP8.Utility; using System.Xml.Serialization; + using CampusAppWP8.Utility; /// Exam model. /// Stubbfel, 02.09.2013. diff --git a/CampusAppWP8/CampusAppWP8/Model/MainModel.cs b/CampusAppWP8/CampusAppWP8/Model/MainModel.cs index a88d6a99..6a01db2b 100644 --- a/CampusAppWP8/CampusAppWP8/Model/MainModel.cs +++ b/CampusAppWP8/CampusAppWP8/Model/MainModel.cs @@ -11,7 +11,6 @@ namespace CampusAppWP8 using System.Collections.Generic; using System.IO; using System.Net; - using System.Text; using CampusAppWP8.Model.Utility; using CampusAppWP8.Utility; @@ -19,8 +18,13 @@ namespace CampusAppWP8 /// Base model io handling class. /// /// model type - public abstract class MainModel : IDisposable + public abstract class MainModel { + /// + /// File object. + /// + protected CampusAppWP8.Utility.File file = null; + /// /// Model io type. /// @@ -31,11 +35,6 @@ namespace CampusAppWP8 /// private T model = default(T); - /// - /// File object. - /// - private CampusAppWP8.Utility.File file = null; - /// /// Web object. /// @@ -88,14 +87,6 @@ namespace CampusAppWP8 } } - /// - /// Finalizes an instance of the class. - /// - ~MainModel() - { - this.SaveData(); - } - /// /// Delegate of the OnIO callback function. /// @@ -238,14 +229,6 @@ namespace CampusAppWP8 } } - /// - /// Called before finalizing. Can maybe be removed. - /// - public void Dispose() - { - this.SaveData(); - } - /// /// Forces a update from web. /// diff --git a/CampusAppWP8/CampusAppWP8/Model/XmlModel.cs b/CampusAppWP8/CampusAppWP8/Model/XmlModel.cs index e606f082..5a2cbdbe 100644 --- a/CampusAppWP8/CampusAppWP8/Model/XmlModel.cs +++ b/CampusAppWP8/CampusAppWP8/Model/XmlModel.cs @@ -15,7 +15,7 @@ namespace CampusAppWP8.Model /// Xml model io handler class. /// /// model type - public class XmlModel : MainModel + public abstract class XmlModel : MainModel { /// /// Initializes a new instance of the class. diff --git a/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml b/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml index d8774e03..c4a7d7cd 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml +++ b/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml @@ -29,12 +29,9 @@ - + @@ -51,12 +48,9 @@ - + @@ -73,12 +67,9 @@ - + diff --git a/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs index 78c5c024..6bcaebe2 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs @@ -8,22 +8,25 @@ namespace CampusAppWP8.Pages.Exams { using System.Linq; using System.Windows; + using System.Windows.Controls; using System.Windows.Navigation; using CampusAppWP8.Feed.Exams; + using CampusAppWP8.File.Exams; using CampusAppWP8.Resources; using CampusAppWP8.Utility; using CampusAppWP8.Utility.Lui.MessageBoxes; using Microsoft.Phone.Controls; - using System.Net; - using System.IO; - /// Exams. + /// class of ExamsPage. /// Stubbfel, 02.09.2013. public partial class Exams : PhoneApplicationPage { /// The feed. private ExamFeed feed; + /// The exam file. + private ExamFile file; + /// Initializes a new instance of the Exams class. /// Stubbfel, 02.09.2013. public Exams() @@ -56,8 +59,13 @@ namespace CampusAppWP8.Pages.Exams { if (NavigationMode.Back == e.NavigationMode) { + App.SaveToIsolatedStorage(Constants.ExamPageModelKey, -1); this.feed.SaveData(); } + else + { + App.SaveToIsolatedStorage(Constants.ExamPageModelKey, this.ExamPivot.SelectedIndex); + } } /// Method initialize the Feed. @@ -76,7 +84,14 @@ namespace CampusAppWP8.Pages.Exams { this.SetupExamList(); this.ProgressBar.Visibility = System.Windows.Visibility.Collapsed; - HttpRequest api = new HttpRequest(); + } + + /// Executes the PDF reader operation. + /// Stubbfel, 03.09.2013. + private void LaunchPDFReader() + { + this.ProgressBar.Visibility = System.Windows.Visibility.Collapsed; + this.file.SaveAndLaunchFile(); } /// Sets up the exam list. @@ -109,24 +124,28 @@ namespace CampusAppWP8.Pages.Exams /// The calculated selected index. private int CalcSelectedIndex() { - int result = 0; - Model.Setting.UserProfilModel.DegreeType degree = Settings.UserProfil.Degree; - - switch (degree) + int result = App.LoadFromIsolatedStorage(Constants.ExamPageModelKey); + if (result < 0 || result > 2) { - case Model.Setting.UserProfilModel.DegreeType.BACHELOR: - result = 0; - break; - case Model.Setting.UserProfilModel.DegreeType.MASTER: - result = 1; - break; - case Model.Setting.UserProfilModel.DegreeType.DIPLOM: - result = 2; - break; - default: - result = 0; - break; + Model.Setting.UserProfilModel.DegreeType degree = Settings.UserProfil.Degree; + + switch (degree) + { + case Model.Setting.UserProfilModel.DegreeType.BACHELOR: + result = 0; + break; + case Model.Setting.UserProfilModel.DegreeType.MASTER: + result = 1; + break; + case Model.Setting.UserProfilModel.DegreeType.DIPLOM: + result = 2; + break; + default: + result = 0; + break; + } } + return result; } @@ -145,5 +164,40 @@ namespace CampusAppWP8.Pages.Exams MessageBoxResult result = MessageBoxes.ShowMainModelErrorMessageBox(AppResources.MsgBox_ErrorMainModelLoadFile); this.ProgressBar.Visibility = System.Windows.Visibility.Collapsed; } + + /// Event handler. Called by Button for click events. + /// Stubbfel, 03.09.2013. + /// Source of the event. + /// Routed event information. + private void Button_Click(object sender, RoutedEventArgs e) + { + Button button = sender as Button; + if (button == null) + { + return; + } + + string url = button.Tag as string; + if (url == null) + { + return; + } + + // create filename + string[] filenames = url.Split('/'); + string filename = url; + + if (filenames.Length > 0) + { + filename = filenames[filenames.Length - 1]; + } + + this.file = new ExamFile(filename, url); + this.file.OnLoaded += new ExamFile.OnIO(this.LaunchPDFReader); + this.file.OnFailedWeb += new ExamFile.OnFailed(this.FeedIsFailWeb); + this.file.OnFailedFile += new ExamFile.OnFailed(this.FeedIsFailFile); + this.file.LoadData(); + this.ProgressBar.Visibility = System.Windows.Visibility.Visible; + } } } \ No newline at end of file diff --git a/CampusAppWP8/CampusAppWP8/Resources/Constants.Designer.cs b/CampusAppWP8/CampusAppWP8/Resources/Constants.Designer.cs index bc4f1e44..ca04fe9e 100644 --- a/CampusAppWP8/CampusAppWP8/Resources/Constants.Designer.cs +++ b/CampusAppWP8/CampusAppWP8/Resources/Constants.Designer.cs @@ -132,6 +132,15 @@ namespace CampusAppWP8.Resources { } } + /// + /// Sucht eine lokalisierte Zeichenfolge, die ExamsPage.LastPivotIndex ähnelt. + /// + public static string ExamPageModelKey { + get { + return ResourceManager.GetString("ExamPageModelKey", resourceCulture); + } + } + /// /// Sucht eine lokalisierte Zeichenfolge, die links ähnelt. /// diff --git a/CampusAppWP8/CampusAppWP8/Resources/Constants.resx b/CampusAppWP8/CampusAppWP8/Resources/Constants.resx index cedfe545..827f5f98 100644 --- a/CampusAppWP8/CampusAppWP8/Resources/Constants.resx +++ b/CampusAppWP8/CampusAppWP8/Resources/Constants.resx @@ -447,4 +447,7 @@ https://www.zv.tu-cottbus.de/CMS-Webservice/Pruefungsordnung/Uebersicht + + ExamsPage.LastPivotIndex + \ No newline at end of file diff --git a/CampusAppWP8/CampusAppWP8/Utility/File.cs b/CampusAppWP8/CampusAppWP8/Utility/File.cs index 61c6e1a1..3a444a0d 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/File.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/File.cs @@ -10,6 +10,7 @@ namespace CampusAppWP8.Utility using System; using System.IO; using System.Threading; + using System.Threading.Tasks; using Windows.Storage; /// @@ -27,12 +28,9 @@ namespace CampusAppWP8.Utility /// private string filename = string.Empty; - /// - /// Initializes a new instance of the class. - /// - /// file name - /// read type - /// write type + /// Initializes a new instance of the class. + /// Stubbfel, 03.09.2013. + /// file name. public File(string filename) { this.filename = filename; @@ -43,11 +41,9 @@ namespace CampusAppWP8.Utility /// public delegate void WriteCallbackFunc(); - /// - /// Read data from file to a string. - /// - /// read type - /// data string + /// Read data from file to a string. + /// Stubbfel, 03.09.2013. + /// data string. public byte[] ReadFile() { byte[] retValue = null; @@ -60,13 +56,11 @@ namespace CampusAppWP8.Utility return retValue; } - /// - /// Write bytes to the file. - /// - /// data byte array. - /// callback function, called after writing is done. + /// Write bytes to the file. + /// Stubbfel, 03.09.2013. + /// data byte array. + /// callback function, called after writing is done. /// callback function, called when writing failed. - /// write type. public void WriteFile(byte[] data, WriteCallbackFunc onSavedCallback, WriteCallbackFunc onFailedCallback) { Thread th = new Thread(delegate() { this.WriteAsync(data, onSavedCallback, onFailedCallback); }); @@ -92,6 +86,19 @@ namespace CampusAppWP8.Utility return this.GetFileInfo().Exists; } + /// Converts this object to a storage file. + /// Stubbfel, 03.09.2013. + /// Storage File + public async Task AsStorageFile() + { + if (this.Exist()) + { + return await File.LocalFolder.GetFileAsync(this.filename); + } + + return null; + } + /// /// Read data synchronous from file. /// diff --git a/CampusAppWP8/CampusAppWP8/Utility/HttpRequest.cs b/CampusAppWP8/CampusAppWP8/Utility/HttpRequest.cs index 87ce11dc..e8cd4db7 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/HttpRequest.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/HttpRequest.cs @@ -60,6 +60,10 @@ namespace CampusAppWP8.Utility client.DownloadStringAsync(url); } + /// Method realize the http-get-method resource. + /// Stubbfel, 03.09.2013. + /// Url of the resource. + /// The action. public void HttpGet(Uri url, Action action) { WebClient client = new WebClient();