/// <summary> /// This method checks if an update is required. During this process the appcast /// will be downloaded and checked against the reference assembly. Ensure that /// the calling process has access to the internet and read access to the /// reference assembly. This method is also called from the background loops. /// </summary> /// <param name="config">the configuration</param> /// <param name="latestVersion">returns the latest version</param> /// <returns><c>true</c> if an update is required</returns> public bool IsUpdateRequired(KryptonSparkleConfiguration config, out KryptonSparkleAppCastItem latestVersion) { // report ReportDiagnosticMessage("Downloading and checking appcast"); // init the appcast KryptonSparkleAppCast cast = new KryptonSparkleAppCast(_AppCastUrl, config); // check if any updates are available try { latestVersion = cast.GetLatestVersion(); } catch (Exception e) { // show the exeception message ReportDiagnosticMessage("Error during app cast download: " + e.Message); // just null the version info latestVersion = null; } if (latestVersion == null) { ReportDiagnosticMessage("No version information in app cast found"); return(false); } ReportDiagnosticMessage("Lastest version on the server is " + latestVersion.Version); // set the last check time ReportDiagnosticMessage("Touch the last check timestamp"); config.TouchCheckTime(); // check if the available update has to be skipped if (latestVersion.Version.Equals(config.SkipThisVersion)) { ReportDiagnosticMessage("Latest update has to be skipped (user decided to skip version " + config.SkipThisVersion + ")"); return(false); } // check if the version will be the same then the installed version Version v1 = new Version(config.InstalledVersion); Version v2 = new Version(latestVersion.Version); if (v2 <= v1) { ReportDiagnosticMessage("Installed version is valid, no update needed (" + config.InstalledVersion + ")"); return(false); } // ok we need an update return(true); }
/// <summary> /// Updates from appcast /// </summary> /// <param name="currentItem">the current (top-most) item in the app-cast</param> private void Update(KryptonSparkleAppCastItem currentItem) { if (currentItem != null) { // show the update ui if (EnableSilentMode) { InitDownloadAndInstallProcess(currentItem); } else { ShowUpdateNeededUI(currentItem); } } }
/// <summary> /// This method shows the update ui and allows to perform the /// update process /// </summary> /// <param name="currentItem">the item to show the UI for</param> public void ShowUpdateNeededUI(KryptonSparkleAppCastItem currentItem) { if (this.UserWindow == null) { // create the form this.UserWindow = new KryptonSparkleForm(currentItem, ApplicationIcon, ApplicationWindowIcon); } this.UserWindow.CurrentItem = currentItem; if (this.HideReleaseNotes) { this.UserWindow.HideReleaseNotes(); } // clear if already set. this.UserWindow.UserResponded -= new EventHandler(OnUserWindowUserResponded); this.UserWindow.UserResponded += new EventHandler(OnUserWindowUserResponded); this.UserWindow.Show(); }
/// <summary> /// Starts the download process /// </summary> /// <param name="item">the appcast item to download</param> private void InitDownloadAndInstallProcess(KryptonSparkleAppCastItem item) { // get the filename of the download lin string[] segments = item.DownloadLink.Split('/'); string fileName = segments[segments.Length - 1]; // get temp path _downloadTempFileName = Environment.ExpandEnvironmentVariables("%temp%\\" + fileName); if (this.ProgressWindow == null) { this.ProgressWindow = new KryptonSparkleDownloadProgress(this, item, ApplicationIcon, ApplicationWindowIcon, EnableSilentMode); } else { this.ProgressWindow.InstallAndRelaunch -= new EventHandler(OnProgressWindowInstallAndRelaunch); } this.ProgressWindow.TempFileName = _downloadTempFileName; this.ProgressWindow.InstallAndRelaunch += new EventHandler(OnProgressWindowInstallAndRelaunch); // set up the download client // start async download if (_webDownloadClient != null) { _webDownloadClient.DownloadProgressChanged -= new DownloadProgressChangedEventHandler(this.ProgressWindow.OnClientDownloadProgressChanged); _webDownloadClient.DownloadFileCompleted -= new AsyncCompletedEventHandler(OnWebDownloadClientDownloadFileCompleted); _webDownloadClient = null; } _webDownloadClient = new WebClient { UseDefaultCredentials = true, Proxy = { Credentials = CredentialCache.DefaultNetworkCredentials }, }; _webDownloadClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(this.ProgressWindow.OnClientDownloadProgressChanged); _webDownloadClient.DownloadFileCompleted += new AsyncCompletedEventHandler(OnWebDownloadClientDownloadFileCompleted); Uri url = new Uri(item.DownloadLink); _webDownloadClient.DownloadFileAsync(url, _downloadTempFileName); this.ProgressWindow.ShowDialog(); }
/// <summary> /// Gets the latest version /// </summary> /// <returns>the AppCast item corresponding to the latest version</returns> public KryptonSparkleAppCastItem GetLatestVersion() { KryptonSparkleAppCastItem latestVersion = null; // build a http web request stream WebRequest request = WebRequest.Create(_castUrl); request.UseDefaultCredentials = true; request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; // request the cast and build the stream WebResponse response = request.GetResponse(); using (Stream inputstream = response.GetResponseStream()) { KryptonSparkleAppCastItem currentItem = null; if (inputstream == null) { return(null); } using (XmlTextReader reader = new XmlTextReader(inputstream)) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: switch (reader.Name) { case itemNode: { currentItem = new KryptonSparkleAppCastItem(); break; } case releaseNotesLinkNode: { if (currentItem != null) { currentItem.ReleaseNotesLink = reader.ReadString(); currentItem.ReleaseNotesLink = currentItem.ReleaseNotesLink.Trim('\n'); } break; } case enclosureNode: { if (currentItem != null) { currentItem.Version = reader.GetAttribute(versionAttribute); currentItem.DownloadLink = reader.GetAttribute(urlAttribute); currentItem.DSASignature = reader.GetAttribute(dasSignature); } break; } } break; case XmlNodeType.EndElement: switch (reader.Name) { case itemNode: { if (latestVersion == null) { latestVersion = currentItem; } else if (currentItem.CompareTo(latestVersion) > 0) { latestVersion = currentItem; } break; } } break; } } } } // add some other attributes if (latestVersion != null) { latestVersion.AppName = _config.ApplicationName; latestVersion.AppVersionInstalled = _config.InstalledVersion; } // go ahead return(latestVersion); }