aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Updates
diff options
context:
space:
mode:
authorLukePulverenti <luke.pulverenti@gmail.com>2013-02-20 20:33:05 -0500
committerLukePulverenti <luke.pulverenti@gmail.com>2013-02-20 20:33:05 -0500
commit767cdc1f6f6a63ce997fc9476911e2c361f9d402 (patch)
tree49add55976f895441167c66cfa95e5c7688d18ce /MediaBrowser.Common/Updates
parent845554722efaed872948a9e0f7202e3ef52f1b6e (diff)
Pushing missing changes
Diffstat (limited to 'MediaBrowser.Common/Updates')
-rw-r--r--MediaBrowser.Common/Updates/ApplicationUpdateCheck.cs92
-rw-r--r--MediaBrowser.Common/Updates/ApplicationUpdater.cs93
-rw-r--r--MediaBrowser.Common/Updates/ClickOnceHelper.cs217
3 files changed, 402 insertions, 0 deletions
diff --git a/MediaBrowser.Common/Updates/ApplicationUpdateCheck.cs b/MediaBrowser.Common/Updates/ApplicationUpdateCheck.cs
new file mode 100644
index 0000000000..7501d7321b
--- /dev/null
+++ b/MediaBrowser.Common/Updates/ApplicationUpdateCheck.cs
@@ -0,0 +1,92 @@
+using MediaBrowser.Model.Tasks;
+using System;
+using System.Deployment.Application;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Common.Updates
+{
+ /// <summary>
+ /// Class ApplicationUpdateCheck
+ /// </summary>
+ public class ApplicationUpdateCheck
+ {
+ /// <summary>
+ /// The _task completion source
+ /// </summary>
+ private TaskCompletionSource<CheckForUpdateCompletedEventArgs> _taskCompletionSource;
+
+ /// <summary>
+ /// The _progress
+ /// </summary>
+ private IProgress<TaskProgress> _progress;
+
+ /// <summary>
+ /// Checks for application update.
+ /// </summary>
+ /// <param name="cancellationToken">The cancellation token.</param>
+ /// <param name="progress">The progress.</param>
+ /// <returns>Task{CheckForUpdateCompletedEventArgs}.</returns>
+ /// <exception cref="System.InvalidOperationException">Current deployment is not a ClickOnce deployment</exception>
+ public Task<CheckForUpdateCompletedEventArgs> CheckForApplicationUpdate(CancellationToken cancellationToken, IProgress<TaskProgress> progress)
+ {
+ if (!ApplicationDeployment.IsNetworkDeployed)
+ {
+ throw new InvalidOperationException("Current deployment is not network deployed.");
+ }
+
+ _progress = progress;
+
+ _taskCompletionSource = new TaskCompletionSource<CheckForUpdateCompletedEventArgs>();
+
+ var deployment = ApplicationDeployment.CurrentDeployment;
+
+ cancellationToken.Register(deployment.CheckForUpdateAsyncCancel);
+
+ cancellationToken.ThrowIfCancellationRequested();
+
+ deployment.CheckForUpdateCompleted += deployment_CheckForUpdateCompleted;
+ deployment.CheckForUpdateProgressChanged += deployment_CheckForUpdateProgressChanged;
+
+ deployment.CheckForUpdateAsync();
+
+ return _taskCompletionSource.Task;
+ }
+
+ /// <summary>
+ /// Handles the CheckForUpdateCompleted event of the deployment control.
+ /// </summary>
+ /// <param name="sender">The source of the event.</param>
+ /// <param name="e">The <see cref="CheckForUpdateCompletedEventArgs" /> instance containing the event data.</param>
+ void deployment_CheckForUpdateCompleted(object sender, CheckForUpdateCompletedEventArgs e)
+ {
+ var deployment = ApplicationDeployment.CurrentDeployment;
+
+ deployment.CheckForUpdateCompleted -= deployment_CheckForUpdateCompleted;
+ deployment.CheckForUpdateProgressChanged -= deployment_CheckForUpdateProgressChanged;
+
+ if (e.Error != null)
+ {
+ _taskCompletionSource.SetException(e.Error);
+ }
+ else if (e.Cancelled)
+ {
+ _taskCompletionSource.SetCanceled();
+ }
+ else
+ {
+ _taskCompletionSource.SetResult(e);
+ }
+ }
+
+ /// <summary>
+ /// Handles the CheckForUpdateProgressChanged event of the deployment control.
+ /// </summary>
+ /// <param name="sender">The source of the event.</param>
+ /// <param name="e">The <see cref="DeploymentProgressChangedEventArgs" /> instance containing the event data.</param>
+ void deployment_CheckForUpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e)
+ {
+ _progress.Report(new TaskProgress { PercentComplete = e.ProgressPercentage });
+ }
+ }
+}
diff --git a/MediaBrowser.Common/Updates/ApplicationUpdater.cs b/MediaBrowser.Common/Updates/ApplicationUpdater.cs
new file mode 100644
index 0000000000..d8ae87b375
--- /dev/null
+++ b/MediaBrowser.Common/Updates/ApplicationUpdater.cs
@@ -0,0 +1,93 @@
+using MediaBrowser.Model.Tasks;
+using System;
+using System.ComponentModel;
+using System.Deployment.Application;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Common.Updates
+{
+ /// <summary>
+ /// Class ApplicationUpdater
+ /// </summary>
+ public class ApplicationUpdater
+ {
+ /// <summary>
+ /// The _task completion source
+ /// </summary>
+ private TaskCompletionSource<AsyncCompletedEventArgs> _taskCompletionSource;
+
+ /// <summary>
+ /// The _progress
+ /// </summary>
+ private IProgress<TaskProgress> _progress;
+
+ /// <summary>
+ /// Updates the application
+ /// </summary>
+ /// <param name="cancellationToken">The cancellation token.</param>
+ /// <param name="progress">The progress.</param>
+ /// <returns>Task{AsyncCompletedEventArgs}.</returns>
+ /// <exception cref="System.InvalidOperationException">Current deployment is not network deployed.</exception>
+ public Task<AsyncCompletedEventArgs> UpdateApplication(CancellationToken cancellationToken, IProgress<TaskProgress> progress)
+ {
+ if (!ApplicationDeployment.IsNetworkDeployed)
+ {
+ throw new InvalidOperationException("Current deployment is not network deployed.");
+ }
+
+ _progress = progress;
+
+ _taskCompletionSource = new TaskCompletionSource<AsyncCompletedEventArgs>();
+
+ var deployment = ApplicationDeployment.CurrentDeployment;
+
+ cancellationToken.Register(deployment.UpdateAsyncCancel);
+
+ cancellationToken.ThrowIfCancellationRequested();
+
+ deployment.UpdateCompleted += deployment_UpdateCompleted;
+ deployment.UpdateProgressChanged += deployment_UpdateProgressChanged;
+
+ deployment.UpdateAsync();
+
+ return _taskCompletionSource.Task;
+ }
+
+ /// <summary>
+ /// Handles the UpdateCompleted event of the deployment control.
+ /// </summary>
+ /// <param name="sender">The source of the event.</param>
+ /// <param name="e">The <see cref="AsyncCompletedEventArgs" /> instance containing the event data.</param>
+ void deployment_UpdateCompleted(object sender, AsyncCompletedEventArgs e)
+ {
+ var deployment = ApplicationDeployment.CurrentDeployment;
+
+ deployment.UpdateCompleted -= deployment_UpdateCompleted;
+ deployment.UpdateProgressChanged -= deployment_UpdateProgressChanged;
+
+ if (e.Error != null)
+ {
+ _taskCompletionSource.SetException(e.Error);
+ }
+ else if (e.Cancelled)
+ {
+ _taskCompletionSource.SetCanceled();
+ }
+ else
+ {
+ _taskCompletionSource.SetResult(e);
+ }
+ }
+
+ /// <summary>
+ /// Handles the UpdateProgressChanged event of the deployment control.
+ /// </summary>
+ /// <param name="sender">The source of the event.</param>
+ /// <param name="e">The <see cref="DeploymentProgressChangedEventArgs" /> instance containing the event data.</param>
+ void deployment_UpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e)
+ {
+ _progress.Report(new TaskProgress { PercentComplete = e.ProgressPercentage });
+ }
+ }
+}
diff --git a/MediaBrowser.Common/Updates/ClickOnceHelper.cs b/MediaBrowser.Common/Updates/ClickOnceHelper.cs
new file mode 100644
index 0000000000..7dd0cf9c57
--- /dev/null
+++ b/MediaBrowser.Common/Updates/ClickOnceHelper.cs
@@ -0,0 +1,217 @@
+using Microsoft.Win32;
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using System.Security.AccessControl;
+
+namespace MediaBrowser.Common.Updates
+{
+ /// <summary>
+ /// Class ClickOnceHelper
+ /// </summary>
+ public class ClickOnceHelper
+ {
+ /// <summary>
+ /// The uninstall string
+ /// </summary>
+ private const string UninstallString = "UninstallString";
+ /// <summary>
+ /// The display name key
+ /// </summary>
+ private const string DisplayNameKey = "DisplayName";
+ /// <summary>
+ /// The uninstall string file
+ /// </summary>
+ private const string UninstallStringFile = "UninstallString.bat";
+ /// <summary>
+ /// The appref extension
+ /// </summary>
+ private const string ApprefExtension = ".appref-ms";
+ /// <summary>
+ /// The uninstall registry key
+ /// </summary>
+ private readonly RegistryKey UninstallRegistryKey;
+
+ /// <summary>
+ /// Gets the location.
+ /// </summary>
+ /// <value>The location.</value>
+ private static string Location
+ {
+ get { return Assembly.GetExecutingAssembly().Location; }
+ }
+
+ /// <summary>
+ /// Gets the name of the publisher.
+ /// </summary>
+ /// <value>The name of the publisher.</value>
+ public string PublisherName { get; private set; }
+ /// <summary>
+ /// Gets the name of the product.
+ /// </summary>
+ /// <value>The name of the product.</value>
+ public string ProductName { get; private set; }
+ /// <summary>
+ /// Gets the uninstall file.
+ /// </summary>
+ /// <value>The uninstall file.</value>
+ public string UninstallFile { get; private set; }
+ /// <summary>
+ /// Gets the name of the suite.
+ /// </summary>
+ /// <value>The name of the suite.</value>
+ public string SuiteName { get; private set; }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ClickOnceHelper" /> class.
+ /// </summary>
+ /// <param name="publisherName">Name of the publisher.</param>
+ /// <param name="productName">Name of the product.</param>
+ /// <param name="suiteName">Name of the suite.</param>
+ public ClickOnceHelper(string publisherName, string productName, string suiteName)
+ {
+ PublisherName = publisherName;
+ ProductName = productName;
+ SuiteName = suiteName;
+
+ var publisherFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), PublisherName);
+
+ if (!Directory.Exists(publisherFolder))
+ {
+ Directory.CreateDirectory(publisherFolder);
+ }
+
+ UninstallFile = Path.Combine(publisherFolder, UninstallStringFile);
+
+ UninstallRegistryKey = GetUninstallRegistryKeyByProductName(ProductName);
+ }
+
+ /// <summary>
+ /// Gets the shortcut path.
+ /// </summary>
+ /// <returns>System.String.</returns>
+ private string GetShortcutPath()
+ {
+ var allProgramsPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
+
+ var shortcutPath = Path.Combine(allProgramsPath, PublisherName);
+
+ if (!string.IsNullOrEmpty(SuiteName))
+ {
+ shortcutPath = Path.Combine(shortcutPath, SuiteName);
+ }
+
+ return Path.Combine(shortcutPath, ProductName) + ApprefExtension;
+ }
+
+ /// <summary>
+ /// Gets the startup shortcut path.
+ /// </summary>
+ /// <returns>System.String.</returns>
+ private string GetStartupShortcutPath()
+ {
+ var startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
+
+ return Path.Combine(startupPath, ProductName) + ApprefExtension;
+ }
+
+ /// <summary>
+ /// Adds the shortcut to startup.
+ /// </summary>
+ public void AddShortcutToStartup()
+ {
+ var startupPath = GetStartupShortcutPath();
+
+ if (!File.Exists(startupPath))
+ {
+ File.Copy(GetShortcutPath(), startupPath);
+ }
+ }
+
+ /// <summary>
+ /// Removes the shortcut from startup.
+ /// </summary>
+ public void RemoveShortcutFromStartup()
+ {
+ var startupPath = GetStartupShortcutPath();
+
+ if (File.Exists(startupPath))
+ {
+ File.Delete(startupPath);
+ }
+ }
+
+ /// <summary>
+ /// Updates the uninstall parameters.
+ /// </summary>
+ /// <param name="uninstallerFileName">Name of the uninstaller file.</param>
+ public void UpdateUninstallParameters(string uninstallerFileName)
+ {
+ if (UninstallRegistryKey != null)
+ {
+ UpdateUninstallString(uninstallerFileName);
+ }
+ }
+
+ /// <summary>
+ /// Gets the name of the uninstall registry key by product.
+ /// </summary>
+ /// <param name="productName">Name of the product.</param>
+ /// <returns>RegistryKey.</returns>
+ private RegistryKey GetUninstallRegistryKeyByProductName(string productName)
+ {
+ var subKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall");
+
+ if (subKey == null)
+ {
+ return null;
+ }
+
+ return subKey.GetSubKeyNames()
+ .Select(name => subKey.OpenSubKey(name, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.QueryValues | RegistryRights.ReadKey | RegistryRights.SetValue))
+ .Where(application => application != null)
+ .FirstOrDefault(application => application.GetValueNames().Where(appKey => appKey.Equals(DisplayNameKey)).Any(appKey => application.GetValue(appKey).Equals(productName)));
+ }
+
+ /// <summary>
+ /// Updates the uninstall string.
+ /// </summary>
+ /// <param name="uninstallerFileName">Name of the uninstaller file.</param>
+ private void UpdateUninstallString(string uninstallerFileName)
+ {
+ var uninstallString = (string)UninstallRegistryKey.GetValue(UninstallString);
+
+ if (!string.IsNullOrEmpty(UninstallFile) && uninstallString.StartsWith("rundll32.exe", StringComparison.OrdinalIgnoreCase))
+ {
+ File.WriteAllText(UninstallFile, uninstallString);
+ }
+
+ var str = String.Format("\"{0}\" uninstall", Path.Combine(Path.GetDirectoryName(Location), uninstallerFileName));
+
+ UninstallRegistryKey.SetValue(UninstallString, str);
+ }
+
+ /// <summary>
+ /// Uninstalls this instance.
+ /// </summary>
+ public void Uninstall()
+ {
+ RemoveShortcutFromStartup();
+
+ var uninstallString = File.ReadAllText(UninstallFile);
+
+ new Process
+ {
+ StartInfo =
+ {
+ Arguments = uninstallString.Substring(uninstallString.IndexOf(" ", StringComparison.OrdinalIgnoreCase) + 1),
+ FileName = uninstallString.Substring(0, uninstallString.IndexOf(" ", StringComparison.OrdinalIgnoreCase)),
+ UseShellExecute = false
+ }
+
+ }.Start();
+ }
+ }
+}