Updated 'SparkleAboutController.cs':

- The version comparison code can now handle multiple digit version-sections (e.g. '1.1.11' and '1.11.11').
- Empty delegate pattern has been used, along with Action (instead of custom parameterless delegates), to clean up event definition and firing locations.
- Improved version detection speed by approx 1000 milliseconds. ;)
This commit is contained in:
InvertedAcceleration 2012-03-30 15:53:49 +01:00
parent ca9c6f8e15
commit 088dd482f5

View file

@ -1,108 +1,120 @@
// SparkleShare, a collaboration and sharing tool. // SparkleShare, a collaboration and sharing tool.
// Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com> // Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System; using System.Net;
using System.Net; using SparkleLib;
using System.Threading;
using System.Timers; namespace SparkleShare {
using SparkleLib; public class SparkleAboutController {
namespace SparkleShare { public event Action ShowWindowEvent = delegate { };
public event Action HideWindowEvent = delegate { };
public class SparkleAboutController { public event Action VersionUpToDateEvent = delegate { };
public event Action CheckingForNewVersionEvent = delegate { };
public event ShowWindowEventHandler ShowWindowEvent;
public delegate void ShowWindowEventHandler (); public event NewVersionEventDelegate NewVersionEvent = delegate { };
public delegate void NewVersionEventDelegate (string new_version_string);
public event HideWindowEventHandler HideWindowEvent;
public delegate void HideWindowEventHandler (); public string RunningVersion {
get {
public event NewVersionEventHandler NewVersionEvent; return SparkleBackend.Version;
public delegate void NewVersionEventHandler (string new_version); }
}
public event VersionUpToDateEventHandler VersionUpToDateEvent;
public delegate void VersionUpToDateEventHandler (); public SparkleAboutController ()
{
public event CheckingForNewVersionEventHandler CheckingForNewVersionEvent; Program.Controller.ShowAboutWindowEvent += delegate {
public delegate void CheckingForNewVersionEventHandler (); ShowWindowEvent ();
CheckForNewVersion ();
};
public string RunningVersion { }
get {
return SparkleBackend.Version; public void WindowClosed ()
} {
} HideWindowEvent ();
}
public SparkleAboutController () private void CheckForNewVersion ()
{ {
Program.Controller.ShowAboutWindowEvent += delegate { CheckingForNewVersionEvent ();
if (ShowWindowEvent != null)
ShowWindowEvent (); WebClient web_client = new WebClient ();
Uri uri = new Uri ("http://www.sparkleshare.org/version");
CheckForNewVersion ();
}; web_client.DownloadStringCompleted += delegate (object o, DownloadStringCompletedEventArgs args) {
} if (args.Error != null)
return;
public void WindowClosed () string latest_version_string = args.Result.Trim ();
{
if (HideWindowEvent != null) if (UpdateRequired (RunningVersion, latest_version_string)) {
HideWindowEvent (); NewVersionEvent (latest_version_string);
} } else {
VersionUpToDateEvent ();
}
public void CheckForNewVersion () };
{
if (CheckingForNewVersionEvent != null) web_client.DownloadStringAsync (uri);
CheckingForNewVersionEvent (); }
WebClient web_client = new WebClient (); private bool UpdateRequired (string running_version_string, string latest_version_string)
Uri uri = new Uri ("http://www.sparkleshare.org/version"); {
if (running_version_string == null)
web_client.DownloadStringCompleted += delegate (object o, DownloadStringCompletedEventArgs args) { throw new ArgumentNullException ("running_version_string");
if (args.Error != null)
return; if (string.IsNullOrWhiteSpace (running_version_string))
throw new ArgumentException ("running_version_string");
int running_version = int.Parse (
"" + RunningVersion [0] + RunningVersion [2] + RunningVersion [4] if (latest_version_string == null)
); throw new ArgumentNullException ("latest_version_string");
string result = args.Result.Trim (); if (string.IsNullOrWhiteSpace (latest_version_string))
int new_version = int.Parse ( throw new ArgumentException ("latest_version_string");
"" + result [0] + result [2] + result [4]
); int running_major;
int running_minor;
// Add a little delay, making it seems we're int running_build;
// actually doing hard work try {
Thread.Sleep (1000); string[] running_split = running_version_string.Split ('.');
running_major = int.Parse (running_split [0]);
if (running_version >= new_version) { running_minor = int.Parse (running_split [1]);
if (VersionUpToDateEvent != null) running_build = int.Parse (running_split [2]);
VersionUpToDateEvent (); } catch (Exception ex) {
throw new FormatException ("running_version_string", ex);
} else { }
if (NewVersionEvent != null)
NewVersionEvent (result); int latest_major;
} int latest_minor;
}; int latest_build;
try {
web_client.DownloadStringAsync (uri); string[] latest_split = latest_version_string.Split ('.');
} latest_major = int.Parse (latest_split [0]);
} latest_minor = int.Parse (latest_split [1]);
} latest_build = int.Parse (latest_split [2]);
} catch (Exception ex) {
throw new FormatException ("latest_version_string", ex);
}
bool higher_major = latest_major > running_major;
bool higher_minor = latest_major == running_major && latest_minor > running_minor;
bool higher_build = latest_major == running_major && latest_minor == running_minor && latest_build > running_build;
return higher_major || higher_minor || higher_build;
}
}
}