From 64c4c797552b75242292d0da88f6f9589ef0044a Mon Sep 17 00:00:00 2001 From: Jaex Date: Thu, 3 Jul 2014 16:48:35 +0300 Subject: [PATCH] SettingsBase first time run and previous version check support --- HelpersLib/Helpers/Helpers.cs | 28 ++++++++++++++ HelpersLib/SettingsBase.cs | 23 +++++++++-- HelpersLib/UpdateChecker/UpdateChecker.cs | 2 + HelpersLib/UpdateChecker/UpdateInfo.cs | 10 ++++- ShareX/Forms/UploadTestForm.cs | 2 +- ShareX/Program.cs | 47 ++++++++--------------- ShareX/TaskHelpers.cs | 2 - 7 files changed, 73 insertions(+), 41 deletions(-) diff --git a/HelpersLib/Helpers/Helpers.cs b/HelpersLib/Helpers/Helpers.cs index 38ad457ec..44b8489de 100644 --- a/HelpersLib/Helpers/Helpers.cs +++ b/HelpersLib/Helpers/Helpers.cs @@ -335,6 +335,29 @@ public static void OpenFolderWithFile(string filePath) } } + /// + /// If version1 newer than version2 = 1 + /// If version1 equal to version2 = 0 + /// If version1 older than version2 = -1 + /// + public static int CompareVersion(string version1, string version2) + { + return NormalizeVersion(version1).CompareTo(NormalizeVersion(version2)); + } + + /// + /// If version newer than ApplicationVersion = 1 + /// If version equal to ApplicationVersion = 0 + /// If version older than ApplicationVersion = -1 + /// + public static int CompareApplicationVersion(string version) + { + return CompareVersion(version, Application.ProductVersion); + } + + /// + /// If latestVersion newer than currentVersion = true + /// public static bool CheckVersion(Version currentVersion, Version latestVersion) { return NormalizeVersion(latestVersion).CompareTo(NormalizeVersion(currentVersion)) > 0; @@ -345,6 +368,11 @@ private static Version NormalizeVersion(Version version) return new Version(Math.Max(version.Major, 0), Math.Max(version.Minor, 0), Math.Max(version.Build, 0), Math.Max(version.Revision, 0)); } + private static Version NormalizeVersion(string version) + { + return NormalizeVersion(Version.Parse(version)); + } + public static bool IsWindowsXP() { return OSVersion.Major == 5 && OSVersion.Minor == 1; diff --git a/HelpersLib/SettingsBase.cs b/HelpersLib/SettingsBase.cs index 8d96d0505..e74b4d018 100644 --- a/HelpersLib/SettingsBase.cs +++ b/HelpersLib/SettingsBase.cs @@ -37,11 +37,26 @@ public abstract class SettingsBase where T : SettingsBase, new() { public static readonly SerializationType SerializationType = SerializationType.Json; - [Browsable(false), XmlIgnore, JsonIgnore] public string FilePath { get; private set; } public string ApplicationVersion { get; set; } + public bool IsFirstTimeRun + { + get + { + return string.IsNullOrEmpty(ApplicationVersion); + } + } + + public bool IsPreviousVersion + { + get + { + return !IsFirstTimeRun && Helpers.CompareApplicationVersion(ApplicationVersion) < 0; + } + } + public static T Load(string filePath) { T setting = SettingsHelper.Load(filePath, SerializationType); @@ -55,11 +70,11 @@ public static T Load(Stream stream) return setting; } - public virtual bool Save(string filePath) + public bool Save(string filePath) { FilePath = filePath; ApplicationVersion = Application.ProductVersion; - return SettingsHelper.Save(this, filePath, SerializationType); + return SettingsHelper.Save(this, FilePath, SerializationType); } private void Save() @@ -77,7 +92,7 @@ private void SaveAsync() SaveAsync(FilePath); } - public virtual void Save(Stream stream) + public void Save(Stream stream) { SettingsHelper.Save(this, stream, SerializationType); } diff --git a/HelpersLib/UpdateChecker/UpdateChecker.cs b/HelpersLib/UpdateChecker/UpdateChecker.cs index 6bf433d02..6a90d1030 100644 --- a/HelpersLib/UpdateChecker/UpdateChecker.cs +++ b/HelpersLib/UpdateChecker/UpdateChecker.cs @@ -25,6 +25,7 @@ using System; using System.Net; +using System.Windows.Forms; namespace HelpersLib { @@ -37,6 +38,7 @@ public abstract class UpdateChecker public UpdateChecker() { + CurrentVersion = Version.Parse(Application.ProductVersion); ReleaseType = ReleaseChannelType.Stable; } diff --git a/HelpersLib/UpdateChecker/UpdateInfo.cs b/HelpersLib/UpdateChecker/UpdateInfo.cs index 766fe8a54..9c00f42be 100644 --- a/HelpersLib/UpdateChecker/UpdateInfo.cs +++ b/HelpersLib/UpdateChecker/UpdateInfo.cs @@ -25,6 +25,7 @@ using System; using System.Web; +using System.Windows.Forms; namespace HelpersLib { @@ -65,8 +66,13 @@ public UpdateInfo() public void RefreshStatus() { - if (Status != UpdateStatus.UpdateCheckFailed && CurrentVersion != null && LatestVersion != null && - !string.IsNullOrEmpty(DownloadURL) && (forceUpdate || Helpers.CheckVersion(CurrentVersion, LatestVersion))) + if (CurrentVersion == null) + { + CurrentVersion = Version.Parse(Application.ProductVersion); + } + + if (Status != UpdateStatus.UpdateCheckFailed && CurrentVersion != null && LatestVersion != null && !string.IsNullOrEmpty(DownloadURL) && + (forceUpdate || Helpers.CheckVersion(CurrentVersion, LatestVersion))) { Status = UpdateStatus.UpdateAvailable; } diff --git a/ShareX/Forms/UploadTestForm.cs b/ShareX/Forms/UploadTestForm.cs index d75baa19f..5bf86dcf5 100644 --- a/ShareX/Forms/UploadTestForm.cs +++ b/ShareX/Forms/UploadTestForm.cs @@ -77,7 +77,7 @@ public UploadTestForm() if (string.IsNullOrEmpty(TestText)) { - TestText = Program.ApplicationName + " text upload test"; + TestText = "ShareX text upload test"; } if (string.IsNullOrEmpty(TestURL)) diff --git a/ShareX/Program.cs b/ShareX/Program.cs index b37130293..4957d2e11 100644 --- a/ShareX/Program.cs +++ b/ShareX/Program.cs @@ -39,47 +39,18 @@ namespace ShareX { internal static class Program { - public static readonly string ApplicationName = Application.ProductName; - public static readonly Version AssemblyVersion = Assembly.GetExecutingAssembly().GetName().Version; - - public static string Title - { - get - { - string title = string.Format("{0} {1}.{2}", ApplicationName, AssemblyVersion.Major, AssemblyVersion.Minor); - if (AssemblyVersion.Build > 0) title += "." + AssemblyVersion.Build; - if (IsPortable) title += " Portable"; - return title; - } - } - - public static string AssemblyCopyright - { - get - { - object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false); - - if (attributes.Length == 0) - { - return string.Empty; - } - - return ((AssemblyCopyrightAttribute)attributes[0]).Copyright; - } - } - #region Paths public static readonly string StartupPath = Application.StartupPath; - public static readonly string DefaultPersonalPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), ApplicationName); - private static readonly string PortablePersonalPath = Path.Combine(StartupPath, ApplicationName); + public static readonly string DefaultPersonalPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "ShareX"); + private static readonly string PortablePersonalPath = Path.Combine(StartupPath, "ShareX"); private static readonly string PersonalPathConfig = Path.Combine(StartupPath, "PersonalPath.cfg"); private static readonly string ApplicationConfigFilename = "ApplicationConfig.json"; private static readonly string UploadersConfigFilename = "UploadersConfig.json"; private static readonly string HotkeysConfigFilename = "HotkeysConfig.json"; private static readonly string HistoryFilename = "History.xml"; - private static readonly string LogFileName = ApplicationName + "-Log-{0:yyyy-MM}.txt"; + private static readonly string LogFileName = "ShareX-Log-{0:yyyy-MM}.txt"; public static string CustomPersonalPath { get; private set; } @@ -242,6 +213,18 @@ public static string ToolsFolder #endregion Paths + public static string Title + { + get + { + Version version = Version.Parse(Application.ProductVersion); + string title = string.Format("ShareX {0}.{1}", version.Major, version.Minor); + if (version.Build > 0) title += "." + version.Build; + if (IsPortable) title += " Portable"; + return title; + } + } + public static bool IsMultiInstance { get; private set; } public static bool IsPortable { get; private set; } public static bool IsSilentRun { get; private set; } diff --git a/ShareX/TaskHelpers.cs b/ShareX/TaskHelpers.cs index 9b51af057..1d92dc3dd 100644 --- a/ShareX/TaskHelpers.cs +++ b/ShareX/TaskHelpers.cs @@ -395,7 +395,6 @@ public static Icon GetProgressIcon(int percentage) public static UpdateChecker CheckUpdate() { UpdateChecker updateChecker = new GitHubUpdateChecker("ShareX", "ShareX"); - updateChecker.CurrentVersion = Program.AssemblyVersion; updateChecker.Proxy = ProxyInfo.Current.GetWebProxy(); updateChecker.CheckUpdate(); @@ -403,7 +402,6 @@ public static UpdateChecker CheckUpdate() if (updateChecker.UpdateInfo == null || updateChecker.UpdateInfo.Status == UpdateStatus.UpdateCheckFailed) { updateChecker = new XMLUpdateChecker("http://getsharex.com/Update.xml", "ShareX"); - updateChecker.CurrentVersion = Program.AssemblyVersion; updateChecker.Proxy = ProxyInfo.Current.GetWebProxy(); updateChecker.CheckUpdate(); }