Skip to content
This repository was archived by the owner on Mar 9, 2021. It is now read-only.

Commit a0c2a57

Browse files
committed
Code refactoring
- Addresses C# inspections found by ReSharper.
1 parent 703bf37 commit a0c2a57

File tree

155 files changed

+1765
-1746
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

155 files changed

+1765
-1746
lines changed

src/TumblThree/SharedAssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
[assembly: ComVisible(false)]
1414
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]
15-
[assembly: AssemblyVersion("1.0.5.80")]
16-
[assembly: AssemblyFileVersion("1.0.5.80")]
15+
[assembly: AssemblyVersion("1.0.5.81")]
16+
[assembly: AssemblyFileVersion("1.0.5.81")]

src/TumblThree/TumblThree.Applications/App.config

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml
2+
version="1.0"
3+
encoding="utf-8"?>
24
<configuration>
35
<startup>
46
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
@@ -11,4 +13,4 @@
1113
<system.net>
1214
<defaultProxy enabled="true" useDefaultCredentials="true" />
1315
</system.net>
14-
</configuration>
16+
</configuration>

src/TumblThree/TumblThree.Applications/Auth/OAuth.cs

+22-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
5+
using System.Net;
46
using System.Security.Cryptography;
7+
using System.Text;
58

69
namespace TumblThree.Applications.Auth
710
{
@@ -135,6 +138,7 @@ public string this[string ix]
135138
{
136139
return _params[ix];
137140
}
141+
138142
throw new ArgumentException(ix);
139143
}
140144
set
@@ -143,6 +147,7 @@ public string this[string ix]
143147
{
144148
throw new ArgumentException(ix);
145149
}
150+
146151
_params[ix] = value;
147152
}
148153
}
@@ -203,7 +208,7 @@ private void NewRequest()
203208
/// <returns>the nonce</returns>
204209
private string GenerateNonce()
205210
{
206-
var sb = new System.Text.StringBuilder();
211+
var sb = new StringBuilder();
207212
for (var i = 0; i < 8; i++)
208213
{
209214
int g = _random.Next(3);
@@ -219,6 +224,7 @@ private string GenerateNonce()
219224
break;
220225
}
221226
}
227+
222228
return sb.ToString();
223229
}
224230

@@ -284,7 +290,7 @@ private Dictionary<string, string> ExtractQueryParameters(string queryString)
284290
/// <returns>the Url-encoded version of that string</returns>
285291
public static string UrlEncode(string value)
286292
{
287-
var result = new System.Text.StringBuilder();
293+
var result = new StringBuilder();
288294
foreach (char symbol in value)
289295
{
290296
if (unreservedChars.IndexOf(symbol) != -1)
@@ -296,6 +302,7 @@ public static string UrlEncode(string value)
296302
result.Append('%' + string.Format("{0:X2}", (int)symbol));
297303
}
298304
}
305+
299306
return result.ToString();
300307
}
301308

@@ -323,7 +330,7 @@ public static string UrlEncode(string value)
323330
/// <returns>a string representing the parameters</returns>
324331
private static string EncodeRequestParameters(ICollection<KeyValuePair<string, string>> p)
325332
{
326-
var sb = new System.Text.StringBuilder();
333+
var sb = new StringBuilder();
327334
foreach (KeyValuePair<string, string> item in p.OrderBy(x => x.Key))
328335
{
329336
if (!string.IsNullOrEmpty(item.Value) &&
@@ -383,13 +390,13 @@ public OAuthResponse AcquireRequestToken(string uri, string method)
383390
NewRequest();
384391
string authHeader = GetAuthorizationHeader(uri, method);
385392
// prepare the token request
386-
var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
393+
var request = (HttpWebRequest)WebRequest.Create(uri);
387394
request.Headers.Add("Authorization", authHeader);
388395
request.Method = method;
389396

390-
using (var response = (System.Net.HttpWebResponse)request.GetResponse())
397+
using (var response = (HttpWebResponse)request.GetResponse())
391398
{
392-
using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
399+
using (var reader = new StreamReader(response.GetResponseStream()))
393400
{
394401
var r = new OAuthResponse(reader.ReadToEnd());
395402
this["token"] = r["oauth_token"];
@@ -407,6 +414,7 @@ public OAuthResponse AcquireRequestToken(string uri, string method)
407414
catch
408415
{
409416
}
417+
410418
return r;
411419
}
412420
}
@@ -454,13 +462,13 @@ public OAuthResponse AcquireAccessToken(string uri, string method, string pin)
454462
string authHeader = GetAuthorizationHeader(uri, method);
455463

456464
// prepare the token request
457-
var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
465+
var request = (HttpWebRequest)WebRequest.Create(uri);
458466
request.Headers.Add("Authorization", authHeader);
459467
request.Method = method;
460468

461-
using (var response = (System.Net.HttpWebResponse)request.GetResponse())
469+
using (var response = (HttpWebResponse)request.GetResponse())
462470
{
463-
using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
471+
using (var reader = new StreamReader(response.GetResponseStream()))
464472
{
465473
var r = new OAuthResponse(reader.ReadToEnd());
466474
this["token"] = r["oauth_token"];
@@ -517,7 +525,7 @@ private void Sign(string uri, string method)
517525
string signatureBase = GetSignatureBase(uri, method);
518526
HashAlgorithm hash = GetHash();
519527

520-
byte[] dataBuffer = System.Text.Encoding.ASCII.GetBytes(signatureBase);
528+
byte[] dataBuffer = Encoding.ASCII.GetBytes(signatureBase);
521529
byte[] hashBytes = hash.ComputeHash(dataBuffer);
522530

523531
this["signature"] = Convert.ToBase64String(hashBytes);
@@ -541,7 +549,7 @@ private string GetSignatureBase(string url, string method)
541549
normUrl += uri.AbsolutePath;
542550

543551
// the sigbase starts with the method and the encoded URI
544-
var sb = new System.Text.StringBuilder();
552+
var sb = new StringBuilder();
545553
sb.Append(method)
546554
.Append('&')
547555
.Append(UrlEncode(normUrl))
@@ -566,7 +574,7 @@ private string GetSignatureBase(string url, string method)
566574
}
567575

568576
// concat+format all those params
569-
var sb1 = new System.Text.StringBuilder();
577+
var sb1 = new StringBuilder();
570578
foreach (KeyValuePair<string, string> item in p.OrderBy(x => x.Key))
571579
{
572580
// even "empty" params need to be encoded this way.
@@ -591,7 +599,7 @@ private HashAlgorithm GetHash()
591599
UrlEncode(this["token_secret"]));
592600
var hmacsha1 = new HMACSHA1
593601
{
594-
Key = System.Text.Encoding.ASCII.GetBytes(keystring)
602+
Key = Encoding.ASCII.GetBytes(keystring)
595603
};
596604
return hmacsha1;
597605
}
@@ -614,6 +622,7 @@ public OAuthResponse(string alltext)
614622
string[] kv = pair.Split('=');
615623
_params.Add(kv[0], kv[1]);
616624
}
625+
617626
// expected keys:
618627
// oauth_token, oauth_token_secret, user_id, screen_name, etc
619628
}

src/TumblThree/TumblThree.Applications/ClipboardMonitor.cs

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ protected virtual void Dispose(bool disposing)
6262
hwndSource.RemoveHook(WndProc);
6363
hwndSource.Dispose();
6464
}
65+
6566
// Free any unmanaged objects here.
6667
//
6768
disposed = true;

src/TumblThree/TumblThree.Applications/Controllers/CrawlerController.cs

+22-10
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
using System.Threading;
66
using System.Threading.Tasks;
77
using System.Waf.Applications;
8+
89
using TumblThree.Applications.Crawler;
910
using TumblThree.Applications.DataModels;
1011
using TumblThree.Applications.Services;
1112
using TumblThree.Applications.ViewModels;
12-
using TumblThree.Domain.Models;
13+
using TumblThree.Domain.Models.Blogs;
1314
using TumblThree.Domain.Queue;
1415

1516
namespace TumblThree.Applications.Controllers
@@ -71,11 +72,13 @@ public void Shutdown()
7172
{
7273
stopCommand.Execute(null);
7374
}
75+
7476
Task.WaitAll(runningTasks.ToArray());
7577
}
7678
catch (AggregateException)
7779
{
7880
}
81+
7982
foreach (IBlog blog in managerService.BlogFiles)
8083
{
8184
if (blog.Dirty)
@@ -156,9 +159,18 @@ private async Task Crawl()
156159
runningTasks.Add(Task.Run(() => RunCrawlerTasks(cancellation.Token, pause.Token)));
157160
}
158161

159-
try { await Task.WhenAll(runningTasks.ToArray()); }
160-
catch { }
161-
finally { crawlerCancellationToken.Dispose(); runningTasks.Clear(); }
162+
try
163+
{
164+
await Task.WhenAll(runningTasks.ToArray());
165+
}
166+
catch
167+
{
168+
}
169+
finally
170+
{
171+
crawlerCancellationToken.Dispose();
172+
runningTasks.Clear();
173+
}
162174
}
163175

164176
private async Task RunCrawlerTasks(CancellationToken ct, PauseToken pt)
@@ -176,13 +188,13 @@ private async Task RunCrawlerTasks(CancellationToken ct, PauseToken pt)
176188
}
177189

178190
Monitor.Enter(lockObject);
179-
if (crawlerService.ActiveItems.Count() < QueueManager.Items.Count())
191+
if (crawlerService.ActiveItems.Count < QueueManager.Items.Count)
180192
{
181193
IEnumerable<QueueListItem> queueList = QueueManager.Items.Except(crawlerService.ActiveItems);
182194
QueueListItem nextQueueItem = queueList.First();
183195
IBlog blog = nextQueueItem.Blog;
184196

185-
ICrawler crawler = crawlerFactory.GetCrawler(blog, ct, pt, new Progress<DownloadProgress>(), shellService, crawlerService, managerService);
197+
ICrawler crawler = crawlerFactory.GetCrawler(blog, ct, pt, new Progress<DownloadProgress>());
186198
crawler.IsBlogOnlineAsync().Wait(4000);
187199

188200
if (crawlerService.ActiveItems.Any(item => item.Blog.Name.Equals(nextQueueItem.Blog.Name)))
@@ -217,7 +229,7 @@ private async Task StartSiteSpecificDownloader(QueueListItem queueListItem, Canc
217229
blog.Dirty = true;
218230
ProgressThrottler<DownloadProgress> progress = SetupThrottledQueueListProgress(queueListItem);
219231

220-
ICrawler crawler = crawlerFactory.GetCrawler(blog, ct, pt, progress, shellService, crawlerService, managerService);
232+
ICrawler crawler = crawlerFactory.GetCrawler(blog, ct, pt, progress);
221233
await crawler.CrawlAsync();
222234

223235
if (ct.IsCancellationRequested)
@@ -235,10 +247,10 @@ private async Task StartSiteSpecificDownloader(QueueListItem queueListItem, Canc
235247
}
236248
}
237249

238-
private ProgressThrottler<DataModels.DownloadProgress> SetupThrottledQueueListProgress(QueueListItem queueListItem)
250+
private ProgressThrottler<DownloadProgress> SetupThrottledQueueListProgress(QueueListItem queueListItem)
239251
{
240-
var progressHandler = new Progress<DataModels.DownloadProgress>(value => { queueListItem.Progress = value.Progress; });
241-
return new ProgressThrottler<DataModels.DownloadProgress>(progressHandler, shellService.Settings.ProgessUpdateInterval);
252+
var progressHandler = new Progress<DownloadProgress>(value => { queueListItem.Progress = value.Progress; });
253+
return new ProgressThrottler<DownloadProgress>(progressHandler, shellService.Settings.ProgessUpdateInterval);
242254
}
243255
}
244256
}

src/TumblThree/TumblThree.Applications/Controllers/DetailsController.cs

+14-19
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
using System.ComponentModel.Composition;
66
using System.Linq;
77
using System.Reflection;
8+
89
using TumblThree.Applications.Services;
910
using TumblThree.Applications.Views;
1011
using TumblThree.Domain.Models;
12+
using TumblThree.Domain.Models.Blogs;
1113
using TumblThree.Domain.Queue;
1214

1315
namespace TumblThree.Applications.Controllers
@@ -44,6 +46,7 @@ public Lazy<IDetailsViewModel> GetViewModel(IBlog blog)
4446
{
4547
return viewModel;
4648
}
49+
4750
throw new ArgumentException("Website is not supported!", "blogType");
4851
}
4952

@@ -55,14 +58,14 @@ public void SelectBlogFiles(IReadOnlyList<IBlog> blogFiles)
5558

5659
ClearBlogSelection();
5760

58-
if (blogFiles.Count() <= 1)
61+
if (blogFiles.Count <= 1)
5962
{
6063
DetailsViewModel.Count = 1;
6164
DetailsViewModel.BlogFile = blogFiles.FirstOrDefault();
6265
}
6366
else
6467
{
65-
DetailsViewModel.Count = blogFiles.Count();
68+
DetailsViewModel.Count = blogFiles.Count;
6669
DetailsViewModel.BlogFile = CreateFromMultiple(blogFiles.ToArray());
6770
DetailsViewModel.BlogFile.PropertyChanged += ChangeBlogSettings;
6871
}
@@ -72,14 +75,9 @@ private void UpdateViewModelBasedOnSelection(IReadOnlyList<IBlog> blogFiles)
7275
{
7376
if (blogFiles.Count == 0)
7477
return;
75-
if (blogFiles.Select(blog => blog.GetType()).Distinct().Count() < 2)
76-
{
77-
detailsViewModel = GetViewModel(blogFiles.FirstOrDefault());
78-
}
79-
else
80-
{
81-
detailsViewModel = GetViewModel(new Blog());
82-
}
78+
detailsViewModel = GetViewModel(blogFiles.Select(blog => blog.GetType()).Distinct().Count() < 2
79+
? blogFiles.FirstOrDefault()
80+
: new Blog());
8381
shellService.DetailsView = DetailsViewModel.View;
8482
shellService.UpdateDetailsView();
8583
}
@@ -106,12 +104,12 @@ public void Shutdown()
106104

107105
public IBlog CreateFromMultiple(IEnumerable<IBlog> blogFiles)
108106
{
109-
if (!blogFiles.Any())
107+
List<IBlog> sharedBlogFiles = blogFiles.ToList();
108+
if (!sharedBlogFiles.Any())
110109
{
111110
throw new ArgumentException("The collection must have at least one item.", nameof(blogFiles));
112111
}
113112

114-
IBlog[] sharedBlogFiles = blogFiles.ToArray();
115113
foreach (IBlog blog in sharedBlogFiles)
116114
{
117115
blogsToSave.Add(blog);
@@ -195,14 +193,10 @@ private static T SetProperty<T>(IReadOnlyCollection<IBlog> blogs, string propert
195193
{
196194
PropertyInfo property = typeof(IBlog).GetProperty(propertyName);
197195
var value = (T)property.GetValue(blogs.FirstOrDefault());
198-
if (value != null)
199-
{
200-
bool equal = blogs.All(blog => property.GetValue(blog)?.Equals(value) ?? false);
201-
if (equal)
202-
return value;
196+
if (value == null)
203197
return default(T);
204-
}
205-
return default(T);
198+
bool equal = blogs.All(blog => property.GetValue(blog)?.Equals(value) ?? false);
199+
return equal ? value : default(T);
206200
}
207201

208202
private static bool SetCheckBox(IReadOnlyCollection<IBlog> blogs, string propertyName)
@@ -232,6 +226,7 @@ private void SelectedBlogFilesCollectionChanged(object sender, NotifyCollectionC
232226
{
233227
DetailsViewModel.BlogFile.PropertyChanged -= ChangeBlogSettings;
234228
}
229+
235230
SelectBlogFiles(selectionService.SelectedBlogFiles.ToArray());
236231
}
237232
}

0 commit comments

Comments
 (0)