/// <summary>Downloads the application updates.</summary> /// <param name="application">The Sui containing the update info.</param> /// <param name="bitsJob">The bits job that will download the update.</param> static void DownloadUpdates(Sui application, ref BitsJob bitsJob) { if (application == null) { throw new ArgumentNullException("application"); } if (bitsJob == null) { throw new ArgumentNullException("bitsJob"); } for (int y = 0; y < application.Updates.Count; y++) { // Create download directory consisting of application name and update title string downloadDir = Path.Combine(downloadDirectory, application.Updates[y].Name[0].Value); Directory.CreateDirectory(downloadDir); for (int z = 0; z < application.Updates[y].Files.Count; z++) { if (application.Updates[y].Files[z].Action == FileAction.Delete || application.Updates[y].Files[z].Action == FileAction.UnregisterThenDelete || application.Updates[y].Files[z].Action == FileAction.CompareOnly) { continue; } string destination = Path.GetFileName(application.Updates[y].Files[z].Destination); if (string.IsNullOrWhiteSpace(destination)) { throw new InvalidOperationException(); } if (Utilities.GetHash(Path.Combine(downloadDir, destination)) == application.Updates[y].Files[z].Hash) { continue; } try { File.Delete(Path.Combine(downloadDir, destination)); } catch (IOException) { } bitsJob.AddFile( new Uri(application.Updates[y].Files[z].Source).AbsoluteUri, Path.Combine(downloadDir, destination)); } } }
/// <summary>Adds an update to the update history.</summary> /// <param name="appInfo">The application information.</param> /// <param name="updateInfo">The update information.</param> /// <param name="failed"><c>True</c> if the update failed, otherwise <c>False</c>.</param> static void AddHistory(Sui appInfo, Update updateInfo, bool failed = false) { var hist = new Suh(updateInfo.Name, appInfo.AppInfo.Publisher, updateInfo.Description) { HelpUrl = appInfo.AppInfo.HelpUrl, AppUrl = appInfo.AppInfo.AppUrl, Status = failed == false ? UpdateStatus.Successful : UpdateStatus.Failed, InfoUrl = updateInfo.InfoUrl, InstallDate = DateTime.Now.ToShortDateString(), ReleaseDate = updateInfo.ReleaseDate, Importance = updateInfo.Importance, }; if (UpdateInstalled != null) { UpdateInstalled(null, new UpdateInstalledEventArgs(hist)); } }
/// <summary>Searches for updates while blocking the calling thread.</summary> /// <param name="applications">The collection of applications to check for updates.</param> /// <param name="downloadFolder">The directory where update might be downloaded to.</param> public static void SearchForUpdates(IEnumerable <Sua> applications, string downloadFolder) { downloadDirectory = downloadFolder; IsSearching = true; importantCount = 0; optionalCount = 0; recommendedCount = 0; var applicationsFound = new Collection <Sui>(); try { // Check if machine is connected to the internet var client = new TcpClient(@"sevenupdate.com", 80); client.Close(); } catch (Exception ex) { if (!(ex is SocketException || ex is WebException)) { throw; } if (ErrorOccurred != null) { ErrorOccurred(null, new ErrorOccurredEventArgs(ex.Message, ErrorType.FatalNetworkError)); } return; } if (applications == null) { throw new ArgumentNullException("applications"); } // let's download and load the SUI's from the User config. foreach (var t in applications) { if (!t.IsEnabled) { } else { // Loads a SUI that was downloaded Sui app = null; try { app = Utilities.Deserialize <Sui>(Utilities.DownloadFile(t.SuiUrl)); } catch (WebException ex) { Utilities.ReportError(ex, ErrorType.SearchError, t.SuiUrl); } catch (Exception ex) { Utilities.ReportError(ex, ErrorType.SearchError, t.SuiUrl); throw; } if (app != null) { app.AppInfo = t; try { // Check to see if any updates are available if (CheckForUpdates(ref app)) { applicationsFound.Add(app); } } catch (Exception ex) { Utilities.ReportError(ex, ErrorType.SearchError); if ( !(ex is FileNotFoundException || ex is FileFormatException || ex is ProtoException || ex is NullReferenceException)) { throw; } } } } } // Search is complete! IsSearching = false; if (SearchCompleted != null) { SearchCompleted( null, new SearchCompletedEventArgs(applicationsFound, importantCount, recommendedCount, optionalCount)); } }
/// <summary>Checks for updates.</summary> /// <param name="app">A collection of applications to check for updates.</param> /// <returns>Returns <c>True</c> if found updates, otherwise <c>False</c>.</returns> static bool CheckForUpdates(ref Sui app) { app.AppInfo.Directory = Utilities.IsRegistryKey(app.AppInfo.Directory) ? Utilities.GetRegistryValue( app.AppInfo.Directory, app.AppInfo.ValueName, app.AppInfo.Platform) : Utilities.ConvertPath(app.AppInfo.Directory, true, app.AppInfo.Platform); if (!Directory.Exists(app.AppInfo.Directory)) { return(false); } for (int y = 0; y < app.Updates.Count; y++) { Update updates = app.Updates[y]; ulong size = IterateUpdate( ref updates, app.AppInfo.Directory, app.AppInfo.ValueName, app.AppInfo.Platform); app.Updates[y] = updates; bool remove = true; // Checks to see if the update only contains execute and delete actions if (app.Updates[y].Files.Count > 0) { // ReSharper disable ForCanBeConvertedToForeach for (int z = 0; z < app.Updates[y].Files.Count; z++) { app.Updates[y].Files[z].Destination = Utilities.ExpandInstallLocation( app.Updates[y].Files[z].Destination, app.AppInfo.Directory, app.AppInfo.Platform, app.AppInfo.ValueName); app.Updates[y].Files[z].Source = Utilities.ExpandDownloadUrl( app.Updates[y].Files[z].Source, app.Updates[y].DownloadUrl, app.AppInfo.Platform); if (app.Updates[y].Files[z].Action != FileAction.ExecuteThenDelete) { remove = false; } } // ReSharper restore ForCanBeConvertedToForeach } // If the update does not have any files or if the update only contains execute and delete, then let's // remove the update. if (app.Updates[y].Files.Count == 0 || remove) { app.Updates.Remove(app.Updates[y]); if (app.Updates.Count == 0) { break; } y--; continue; } app.Updates[y].Size = size; switch (app.Updates[y].Importance) { case Importance.Important: importantCount++; break; case Importance.Recommended: recommendedCount++; break; case Importance.Optional: case Importance.Locale: optionalCount++; break; } } if (app.Updates.Count > 0) { // Found updates, return return(true); } app = null; // No updates, let's return return(false); }