Skip to content

Commit

Permalink
Resharper fixes
Browse files Browse the repository at this point in the history
Fixes suggested by resharper
  • Loading branch information
jimgraham committed Oct 15, 2012
1 parent 663a509 commit 6b47d12
Show file tree
Hide file tree
Showing 16 changed files with 212 additions and 269 deletions.
98 changes: 46 additions & 52 deletions NetSparkle/NetSparkle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ public class Sparkle : IDisposable

private BackgroundWorker _worker = new BackgroundWorker();
private String _AppCastUrl;
private String _AppReferenceAssembly;
private readonly String _AppReferenceAssembly;

private Boolean _DoInitialCheck;
private Boolean _ForceInitialCheck;

private EventWaitHandle _exitHandle;
private EventWaitHandle _loopingHandle;
private readonly EventWaitHandle _exitHandle;
private readonly EventWaitHandle _loopingHandle;

private TimeSpan _CheckFrequency;

private string _downloadTempFileName;
private WebClient _webDownloadClient;
private NetSparkleDiagnostic _diagnostic;
private readonly NetSparkleDiagnostic _diagnostic;

/// <summary>
/// ctor which needs the appcast url
Expand Down Expand Up @@ -108,7 +108,7 @@ public Sparkle(String appcastUrl, String referenceAssembly, bool showDiagnostic)

// enable visual style to ensure that we have XP style or higher
// also in WPF applications
System.Windows.Forms.Application.EnableVisualStyles();
Application.EnableVisualStyles();

// reset vars
ApplicationIcon = null;
Expand Down Expand Up @@ -391,10 +391,10 @@ private void ProfileDataThreadStart(object obj)
inv.CollectInventory();

// build url
String requestUrl = inv.BuildRequestUrl(SystemProfileUrl.ToString() + "?");
String requestUrl = inv.BuildRequestUrl(SystemProfileUrl + "?");

// perform the webrequest
HttpWebRequest request = HttpWebRequest.Create(requestUrl) as HttpWebRequest;
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
if (request != null)
{
request.UseDefaultCredentials = true;
Expand Down Expand Up @@ -449,10 +449,7 @@ public bool IsUpdateRequired(NetSparkleConfiguration config, out NetSparkleAppCa
ReportDiagnosticMessage("No version information in app cast found");
return false;
}
else
{
ReportDiagnosticMessage("Lastest version on the server is " + latestVersion.Version);
}
ReportDiagnosticMessage("Lastest version on the server is " + latestVersion.Version);

// set the last check time
ReportDiagnosticMessage("Touch the last check timestamp");
Expand Down Expand Up @@ -559,8 +556,7 @@ private void InitDownloadAndInstallProcess(NetSparkleAppCastItem item)
_webDownloadClient = null;
}

_webDownloadClient = new WebClient();
_webDownloadClient.UseDefaultCredentials = true;
_webDownloadClient = new WebClient {UseDefaultCredentials = true};
_webDownloadClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(this.ProgressWindow.OnClientDownloadProgressChanged);
_webDownloadClient.DownloadFileCompleted += new AsyncCompletedEventHandler(OnWebDownloadClientDownloadFileCompleted);

Expand All @@ -584,12 +580,14 @@ private void RunDownloadedInstaller()
string installerCMD;

// get the file type
if (Path.GetExtension(_downloadTempFileName).Equals(".exe", StringComparison.CurrentCultureIgnoreCase))
string downloadTempFileExtension = Path.GetExtension(_downloadTempFileName) ?? string.Empty;

if (downloadTempFileExtension.Equals(".exe", StringComparison.CurrentCultureIgnoreCase))
{
// build the command line
installerCMD = _downloadTempFileName;
}
else if (Path.GetExtension(_downloadTempFileName).Equals(".msi", StringComparison.CurrentCultureIgnoreCase))
else if (downloadTempFileExtension.Equals(".msi", StringComparison.CurrentCultureIgnoreCase))
{
// buid the command line
installerCMD = "msiexec /i \"" + _downloadTempFileName + "\"";
Expand All @@ -616,9 +614,7 @@ private void RunDownloadedInstaller()
ReportDiagnosticMessage("Going to execute batch: " + cmd);

// start the installer helper
Process process = new Process();
process.StartInfo.FileName = cmd;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process process = new Process {StartInfo = {FileName = cmd, WindowStyle = ProcessWindowStyle.Hidden}};
process.Start();

// quit the app
Expand Down Expand Up @@ -669,18 +665,17 @@ private bool RemoteCertificateValidation(object sender, X509Certificate certific
// verify if we talk about our app cast dll
HttpWebRequest req = sender as HttpWebRequest;
if (req == null)
return (certificate is X509Certificate2) ? ((X509Certificate2)certificate).Verify() : false;
return (certificate is X509Certificate2) && ((X509Certificate2)certificate).Verify();

// if so just return our trust
if (req.RequestUri.Equals(new Uri(_AppCastUrl)))
return true;
else
return (certificate is X509Certificate2) ? ((X509Certificate2)certificate).Verify() : false;
return (certificate is X509Certificate2) && ((X509Certificate2)certificate).Verify();
}
else
{
// check our cert
return (certificate is X509Certificate2) ? ((X509Certificate2)certificate).Verify() : false;
return (certificate is X509Certificate2) && ((X509Certificate2)certificate).Verify();
}
}

Expand All @@ -695,14 +690,14 @@ public bool CheckForUpdates(bool isUserInterfaceShown)
UpdateSystemProfileInformation(config);

// check if update is required
NetSparkleAppCastItem latestVersion = null;
NetSparkleAppCastItem latestVersion;
if (IsUpdateRequired(config, out latestVersion))
{
// show the update window
ReportDiagnosticMessage("Update needed from version " + config.InstalledVersion + " to version " + latestVersion.Version);

// send notification if needed
UpdateDetectedEventArgs ev = new UpdateDetectedEventArgs() { NextAction = NextUpdateAction.ShowStandardUserInterface, ApplicationConfig = config, LatestVersion = latestVersion };
UpdateDetectedEventArgs ev = new UpdateDetectedEventArgs { NextAction = NextUpdateAction.ShowStandardUserInterface, ApplicationConfig = config, LatestVersion = latestVersion };
if (UpdateDetected != null)
UpdateDetected(this, ev);

Expand Down Expand Up @@ -860,7 +855,7 @@ void OnWorkerDoWork(object sender, DoWorkEventArgs e)
UpdateSystemProfileInformation(config);

// check if update is required
NetSparkleAppCastItem latestVersion = null;
NetSparkleAppCastItem latestVersion;
bUpdateRequired = IsUpdateRequired(config, out latestVersion);
if (!bUpdateRequired)
goto WaitSection;
Expand All @@ -869,7 +864,7 @@ void OnWorkerDoWork(object sender, DoWorkEventArgs e)
ReportDiagnosticMessage("Update needed from version " + config.InstalledVersion + " to version " + latestVersion.Version);

// send notification if needed
UpdateDetectedEventArgs ev = new UpdateDetectedEventArgs() { NextAction = NextUpdateAction.ShowStandardUserInterface, ApplicationConfig = config, LatestVersion = latestVersion };
UpdateDetectedEventArgs ev = new UpdateDetectedEventArgs { NextAction = NextUpdateAction.ShowStandardUserInterface, ApplicationConfig = config, LatestVersion = latestVersion };
if (UpdateDetected != null)
UpdateDetected(this, ev);

Expand Down Expand Up @@ -910,35 +905,34 @@ void OnWorkerDoWork(object sender, DoWorkEventArgs e)

// wait for
if (!goIntoLoop)
break;
else
{
// build the event array
WaitHandle[] handles = new WaitHandle[1];
handles[0] = _exitHandle;
break;
}

// wait for any
int i = WaitHandle.WaitAny(handles, _CheckFrequency);
if (WaitHandle.WaitTimeout == i)
{
ReportDiagnosticMessage(String.Format("{0} minutes are over", _CheckFrequency.TotalMinutes));
continue;
}
// build the event array
WaitHandle[] handles = new WaitHandle[1];
handles[0] = _exitHandle;

// check the exit hadnle
if (i == 0)
{
ReportDiagnosticMessage("Got exit signal");
break;
}
// wait for any
int i = WaitHandle.WaitAny(handles, _CheckFrequency);
if (WaitHandle.WaitTimeout == i)
{
ReportDiagnosticMessage(String.Format("{0} minutes are over", _CheckFrequency.TotalMinutes));
continue;
}

// check an other check needed
if (i == 1)
{
ReportDiagnosticMessage("Got force update check signal");
checkTSP = false;
continue;
}
// check the exit hadnle
if (i == 0)
{
ReportDiagnosticMessage("Got exit signal");
break;
}

// check an other check needed
if (i == 1)
{
ReportDiagnosticMessage("Got force update check signal");
checkTSP = false;
}
} while (goIntoLoop);

Expand Down Expand Up @@ -989,7 +983,7 @@ void OnWebDownloadClientDownloadFileCompleted(object sender, AsyncCompletedEvent

// get the assembly reference from which we start the update progress
// only from this trusted assembly the public key can be used
Assembly refassembly = System.Reflection.Assembly.GetEntryAssembly();
Assembly refassembly = Assembly.GetEntryAssembly();
if (refassembly != null)
{
// Check if we found the public key in our entry assembly
Expand Down
98 changes: 54 additions & 44 deletions NetSparkle/NetSparkleAppCast.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Net;
Expand All @@ -13,8 +10,8 @@ namespace AppLimit.NetSparkle
/// </summary>
public class NetSparkleAppCast
{
private NetSparkleConfiguration _config;
private String _castUrl;
private readonly NetSparkleConfiguration _config;
private readonly String _castUrl;

private const String itemNode = "item";
private const String enclosureNode = "enclosure";
Expand Down Expand Up @@ -43,65 +40,78 @@ public NetSparkleAppCastItem GetLatestVersion()
NetSparkleAppCastItem latestVersion = null;

// build a http web request stream
WebRequest request = HttpWebRequest.Create(_castUrl);
WebRequest request = WebRequest.Create(_castUrl);
request.UseDefaultCredentials = true;

// request the cast and build the stream
WebResponse response = request.GetResponse();
using (Stream inputstream = response.GetResponseStream())
{
NetSparkleAppCastItem currentItem = null;
XmlTextReader reader = new XmlTextReader(inputstream);
while (reader.Read())
if (inputstream == null)
{
if (reader.NodeType == XmlNodeType.Element)
return null;
}
using (XmlTextReader reader = new XmlTextReader(inputstream))
{
while (reader.Read())
{
switch (reader.Name)
if (reader.NodeType == XmlNodeType.Element)
{
case itemNode:
{
currentItem = new NetSparkleAppCastItem();
break;
}
case releaseNotesLinkNode:
{
currentItem.ReleaseNotesLink = reader.ReadString();
currentItem.ReleaseNotesLink = currentItem.ReleaseNotesLink.Trim('\n');
break;
}
case enclosureNode:
{
currentItem.Version = reader.GetAttribute(versionAttribute);
currentItem.DownloadLink = reader.GetAttribute(urlAttribute);
currentItem.DSASignature = reader.GetAttribute(dasSignature);

break;
}
switch (reader.Name)
{
case itemNode:
{
currentItem = new NetSparkleAppCastItem();
break;
}
case releaseNotesLinkNode:
{
if (currentItem != null)
{
currentItem.ReleaseNotesLink = reader.ReadString();
currentItem.ReleaseNotesLink = currentItem.ReleaseNotesLink.Trim('\n');
}
break;
}
case enclosureNode:
{
if (currentItem != null)
{
currentItem.Version = reader.GetAttribute(versionAttribute);
currentItem.DownloadLink = reader.GetAttribute(urlAttribute);
currentItem.DSASignature = reader.GetAttribute(dasSignature);
}
break;
}
}
}
}
else if (reader.NodeType == XmlNodeType.EndElement)
{
switch (reader.Name)
else if (reader.NodeType == XmlNodeType.EndElement)
{
case itemNode:
{
if (latestVersion == null)
latestVersion = currentItem;
else if (currentItem.CompareTo(latestVersion) > 0)
switch (reader.Name)
{
case itemNode:
{
latestVersion = currentItem;
if (latestVersion == null)
latestVersion = currentItem;
else if (currentItem.CompareTo(latestVersion) > 0)
{
latestVersion = currentItem;
}
break;
}
break;
}
}
}
}
}
}

// add some other attributes
latestVersion.AppName = _config.ApplicationName;
latestVersion.AppVersionInstalled = _config.InstalledVersion;

if (latestVersion != null)
{
latestVersion.AppName = _config.ApplicationName;
latestVersion.AppVersionInstalled = _config.InstalledVersion;
}
// go ahead
return latestVersion;
}
Expand Down
10 changes: 2 additions & 8 deletions NetSparkle/NetSparkleAssemblyAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using AppLimit.NetSparkle.Interfaces;
using AppLimit.NetSparkle.Interfaces;

namespace AppLimit.NetSparkle
{
Expand All @@ -13,7 +7,7 @@ namespace AppLimit.NetSparkle
/// </summary>
public class NetSparkleAssemblyAccessor : INetSparkleAssemblyAccessor
{
INetSparkleAssemblyAccessor _internalAccessor = null;
readonly INetSparkleAssemblyAccessor _internalAccessor;

/// <summary>
/// Constructor
Expand Down
Loading

0 comments on commit 6b47d12

Please sign in to comment.