Skip to content

Commit

Permalink
When detecting installed torrent clients, more gracefully handle empt…
Browse files Browse the repository at this point in the history
…y paths, and make the logging a little less noisy when normalizing paths.
  • Loading branch information
DavidMoore committed Aug 31, 2019
1 parent e2dd375 commit 24d8cc5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 17 deletions.
7 changes: 4 additions & 3 deletions Code/IPFilter/Apps/BitTorrentApplication.cs
Expand Up @@ -33,15 +33,16 @@ public Task<ApplicationDetectionResult> DetectAsync()
using (key)
{
var installLocation = (string)key.GetValue("InstallLocation");
if (installLocation == null) return Task.FromResult(ApplicationDetectionResult.NotFound());
if (string.IsNullOrWhiteSpace(installLocation)) return Task.FromResult(ApplicationDetectionResult.NotFound());

var displayName = (string)key.GetValue("DisplayName") ?? DefaultDisplayName;
var version = (string)key.GetValue("DisplayVersion") ?? "Unknown";

var result = ApplicationDetectionResult.Create(this, displayName, installLocation);
result.Version = version;

if (!result.InstallLocation.Exists) result.IsPresent = false;
if (result.InstallLocation == null || !result.InstallLocation.Exists) return Task.FromResult(ApplicationDetectionResult.NotFound());;

result.Version = version;

return Task.FromResult(result);
}
Expand Down
2 changes: 1 addition & 1 deletion Code/IPFilter/Apps/EmuleApplication.cs
Expand Up @@ -24,7 +24,7 @@ public Task<ApplicationDetectionResult> DetectAsync()
if (key == null) return Task.FromResult(ApplicationDetectionResult.NotFound());

var installLocation = (string)key.GetValue("Install Path");
if (installLocation == null) return Task.FromResult(ApplicationDetectionResult.NotFound());
if (string.IsNullOrWhiteSpace(installLocation)) return Task.FromResult(ApplicationDetectionResult.NotFound());

var result = ApplicationDetectionResult.Create(this, DefaultDisplayName, installLocation);

Expand Down
2 changes: 1 addition & 1 deletion Code/IPFilter/Apps/QBitTorrent.cs
Expand Up @@ -33,7 +33,7 @@ public Task<ApplicationDetectionResult> DetectAsync()

var result = ApplicationDetectionResult.Create(this, "qBittorrent", installLocation);

if (!result.InstallLocation.Exists) return Task.FromResult(ApplicationDetectionResult.NotFound());
if (result.InstallLocation == null || !result.InstallLocation.Exists) return Task.FromResult(ApplicationDetectionResult.NotFound());

var applicationPath = Path.Combine(result.InstallLocation.FullName, "qbittorrent.exe");
if (!File.Exists(applicationPath)) return Task.FromResult(ApplicationDetectionResult.NotFound());
Expand Down
4 changes: 2 additions & 2 deletions Code/IPFilter/Apps/UTorrentApplication.cs
Expand Up @@ -2,7 +2,7 @@ namespace IPFilter.Apps
{
class UTorrentApplication : BitTorrentApplication
{
protected override string DefaultDisplayName { get { return "µTorrent"; } }
protected override string FolderName { get { return "uTorrent"; } }
protected override string DefaultDisplayName => "µTorrent";
protected override string FolderName => "uTorrent";
}
}
16 changes: 6 additions & 10 deletions Code/IPFilter/Native/PathHelper.cs
Expand Up @@ -16,9 +16,7 @@ public static DirectoryInfo GetDirectoryInfo(string path)

public static DirectoryInfo GetDirectoryInfo([NotNull] string path, CultureInfo cultureInfo)
{
Trace.WriteLine($"Parsing path: '{path}'");

if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("Path is null or empty.", nameof(path));
if (string.IsNullOrWhiteSpace(path)) return null;

try
{
Expand All @@ -30,20 +28,18 @@ public static DirectoryInfo GetDirectoryInfo([NotNull] string path, CultureInfo
// the yen character will actually get corrected to the backslash.

// Get the culture's codepage
Trace.TraceInformation("Culture: " + cultureInfo);
Trace.TraceInformation("Codepage: " + cultureInfo.TextInfo.ANSICodePage);
var encoding = Encoding.GetEncoding(cultureInfo.TextInfo.ANSICodePage);

var normalizedPath = encoding.GetString(encoding.GetBytes(path));

Trace.WriteLine("Normalized path: " + path);


return new DirectoryInfo(normalizedPath);
}
catch (Exception ex)
{
Trace.TraceWarning("Couldn't normalize the path: " + ex);
Trace.TraceWarning("Falling back to just using the un-normalized path that was passed in as '" + path + "'");
Trace.TraceWarning($"Couldn't normalize the path '{path}'. Culture: {cultureInfo}, CodePage: {cultureInfo.TextInfo.ANSICodePage}");
Trace.TraceWarning("Error: " + ex);
Trace.TraceInformation($"Falling back to un-normalized path of '{path}'");

return new DirectoryInfo(path);
}
}
Expand Down

0 comments on commit 24d8cc5

Please sign in to comment.