using SafeMobileLib; using System; using System.ComponentModel; using System.IO; using System.Net; using System.Text; using System.Threading; using System.Xml; namespace SharedUI { public class AutoUpdate { private WebClient client; private String path = "http://www.safemobile.com/upload/safedispatch/updates"; private String appPath = ""; private String downloadedFilePath = ""; private bool updateDownloaded = false; private String onlineVersion = ""; private byte[] versionFile; public enum DownloadStatus { FAILED , COMPLETED }; private App app; private Boolean isDevelop; public bool IsDevelop { get { return isDevelop; } set { isDevelop = value; } } public bool UpdateDownloaded { get { return updateDownloaded; } } public string DownloadedFilePath { get { return downloadedFilePath; } } public AutoUpdate(App applicaton) { this.app = applicaton; } public String GetAvailableVersion() { return onlineVersion; } public bool IsUpdateAvailable(String onlineVersion) { Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; Version online = new Version(onlineVersion); if(online.CompareTo(v) > 0) return true; return false; } /// /// Terminates the downloading procedure and deletes the file that was /// in progress. /// public void CancelUpdate() { // cancel downloading file client?.CancelAsync(); } private void CreateWebClient() { client = new WebClient(); // deregister and then register for version file download client.DownloadDataCompleted += Client_DownloadDataCompleted; // Hookup DownloadFileCompleted Event client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); } public void CheckUpdate() { Thread t = new Thread(CheckUpdateThread); t.Start(); } private void CheckUpdateThread() { appPath = path + (IsDevelop ? "/develop" : "/production") + "/" + App.GetAppFolder(app); string url = appPath + "/version.xml"; // Create an instance of WebClient if (client == null) CreateWebClient(); // Start the download of version file client.DownloadDataAsync(new Uri(url)); } private void Client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) { if (e.Error == null) { versionFile = e.Result; //versionFile = Encoding.Default.GetString(e.Result); Utils.WriteLine(Encoding.Default.GetString(e.Result) + " | ", ConsoleColor.Red); using (XmlReader reader = XmlReader.Create(new StringReader(Encoding.Default.GetString(e.Result)))) { reader.ReadToFollowing("version"); //reader.MoveToFirstAttribute(); onlineVersion = reader.ReadElementContentAsString(); Utils.WriteLine("Version is : " + onlineVersion); reader.ReadToFollowing("url"); string updatePath = reader.ReadElementContentAsString(); Utils.WriteLine("Path is : " + updatePath, ConsoleColor.Cyan); var directory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); if (!Directory.Exists(directory + "\\updates")) Directory.CreateDirectory("updates"); downloadedFilePath = directory + "\\updates\\" + updatePath.Substring(updatePath.LastIndexOf('/') + 1); //once you have the path you get the directory with: if (File.Exists(DownloadedFilePath)) updateDownloaded = true; // fire that a new version is availalbe if (IsUpdateAvailable(onlineVersion)) OnNewVersionAvailable?.Invoke(onlineVersion); else OnLatestVersionAvailable?.Invoke(); } } else // fire event OnDownloadFailed?.Invoke(e.Error); } public void Download() { // Create an instance of WebClient if (client == null) CreateWebClient(); // download the version file in case it wasn't been downloaded before if (versionFile == null) { appPath = path + (isDevelop ? "/develop" : "/production") + "/" + App.GetAppFolder(app); string url = appPath + "/version.xml"; try { versionFile = client.DownloadData(new Uri(url)); } catch(Exception ex) { // fire event OnDownloadFailed?.Invoke(new Exception()); return; } } using (XmlReader reader = XmlReader.Create(new StringReader(Encoding.Default.GetString(versionFile)))) { reader.ReadToFollowing("version"); //reader.MoveToFirstAttribute(); onlineVersion = reader.ReadElementContentAsString(); Utils.WriteLine("Version is : " + onlineVersion); reader.ReadToFollowing("url"); string updatePath = reader.ReadElementContentAsString(); Utils.WriteLine("Path is : " + updatePath, ConsoleColor.Cyan); var directory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); if (!Directory.Exists(directory + "\\updates")) Directory.CreateDirectory("updates"); downloadedFilePath = directory + "\\updates\\" + updatePath.Substring(updatePath.LastIndexOf('/') + 1); //once you have the path you get the directory with: if (File.Exists(DownloadedFilePath)) { updateDownloaded = true; OnDownloadCompleted?.Invoke(DownloadedFilePath); return; } client.DownloadFileAsync(new Uri(updatePath), DownloadedFilePath); reader.ReadToFollowing("changelog"); string changelog = reader.ReadElementContentAsString(); Utils.WriteLine("ChangeLog is : " + changelog, ConsoleColor.DarkMagenta); } // client.DownloadFileAsync(new Uri(url), @"c:\temp\image35.png"); } void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { Utils.WriteLine("DOWNLOAD " + (e.Error == null ? " DONE " : e.Error.ToString()), ConsoleColor.Yellow); // fire event for downloading completed if (e.Error == null) OnDownloadCompleted?.Invoke(DownloadedFilePath); else { // delete file because it has 0 kb if (File.Exists(downloadedFilePath)) File.Delete(downloadedFilePath); // fire event OnDownloadFailed?.Invoke(e.Error); } } /// /// Fire event for when the download status is changed /// /// Current downloading status private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) => OnProgressChanged?.Invoke(e); #region EVENTS public delegate void NewVersionAvailableDel(String version); public event NewVersionAvailableDel OnNewVersionAvailable; public delegate void LatestVersionAvailableDel(); public event LatestVersionAvailableDel OnLatestVersionAvailable; public delegate void DownloadCompletedDel(String filePath); public event DownloadCompletedDel OnDownloadCompleted; public delegate void DownloadFailedDel(Exception ex); public event DownloadFailedDel OnDownloadFailed; public delegate void ProgressChangedDel(DownloadProgressChangedEventArgs e); public event ProgressChangedDel OnProgressChanged; #endregion } }