using System;
using System.Windows;
using System.Diagnostics;
using System.Reflection;
namespace JottiQ
{
public static class Updater
{
/* Settings - adjust as necessary. */
private static string FileUpdater = "dcuhelper.exe";
private static string FileConfig = "JottiQ.dcupdate";
private static string UpdaterLabelName = "JottiQ";
private static string UpdaterDialogCaption = "Update check";
private static string UpdaterAdditionalFlags = "-dontofferdcupdaterpage";
private static int UpdaterCheckingInterval = 2; /* days since last check */
private static string UpdaterNotAvailableCaption = "You silly monkey.";
private static string UpdaterNotAvailableMessage = "Updater support is not present.";
/* Interface for persistent storage so we don't litter the rest of the code
* with hard-coded references to the Settings stuff.
*/
private static bool UpdaterEnabled
{
get { return Properties.Settings.Default.UpdaterEnabled; }
}
private static DateTime UpdaterLastCheck
{
get { return Properties.Settings.Default.UpdaterLastCheck; }
set
{
Properties.Settings.Default.UpdaterLastCheck = value;
Properties.Settings.Default.Save();
}
}
/* Implementation of the class goes below here - no changes needed. */
private static String GetInstallationLocation()
{
string appDir = Assembly.GetExecutingAssembly().Location;
appDir = System.IO.Path.GetDirectoryName(appDir);
if (!appDir.EndsWith(@"\"))
appDir = appDir + @"\";
return appDir;
}
public static bool IsUpdaterAvailable()
{
bool dcUpdaterAvailable =
System.IO.File.Exists(GetInstallationLocation() + FileUpdater) &&
System.IO.File.Exists(GetInstallationLocation() + FileConfig);
return dcUpdaterAvailable;
}
public static void CheckForUpdates(bool explicitCheck)
{
if (!explicitCheck)
{
/* See if we are allowed to do automatic checks. */
if (UpdaterEnabled == false)
return;
/* See if last check was less than X days ago. */
TimeSpan ts = DateTime.Now.Subtract(UpdaterLastCheck);
if (ts.Days < UpdaterCheckingInterval)
return;
}
if (IsUpdaterAvailable())
{
System.Diagnostics.ProcessStartInfo updater =
new System.Diagnostics.ProcessStartInfo(); updater.FileName = GetInstallationLocation() + FileUpdater;
updater.WorkingDirectory = GetInstallationLocation();
updater.Arguments = "-i \"" + UpdaterLabelName + "\" \".\" \"" + (explicitCheck ? UpdaterDialogCaption : ".") + "\" " + UpdaterAdditionalFlags;
/* Old versions of dcuhelper.exe pop up an ugly command-line window. Setting the WindowStyle to Hidden hides that ugly monstrosity.
*
* PLEASE REMOVE this line if you have a recent version of dcuhelper.exe (later than ~January 1, 2011), since at some point
* mouser redesigned the dialog in dcuhelper.exe, which actually broke the Hidden trick. At July 12, this bug was found and fixed,
* so the Hidden switch can once again safely be used with dcuhelper.exe - although it is pointless as the ugly monstrosity has
* been amputated somewhere along the way. Oh joy.
*
* Short story: safe for dcuhelper.exe versions: <= v1.05.01 (Oct 8, 2010) && >= v1.10.01 (July 12, 2011)
*/
updater.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
/* Finally start dcuhelper.exe to do our check. */
Process.Start(updater);
/* Make sure to note down that we checked for an update. */
UpdaterLastCheck = DateTime.Now;
}
else if (explicitCheck)
MessageBox.Show(UpdaterNotAvailableMessage, UpdaterNotAvailableCaption, MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}