Skip to content

Commit 1e8ba4b

Browse files
committed
Organization
1 parent 81195c3 commit 1e8ba4b

32 files changed

+329
-343
lines changed

SmartImage/Commands.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
#region
2+
13
using System;
24
using Neocmd;
3-
using SmartImage.Engines;
45
using SmartImage.Searching;
56
using SmartImage.Utilities;
67

8+
#endregion
9+
710
namespace SmartImage
811
{
912
public static class Commands
@@ -15,7 +18,7 @@ public static class Commands
1518
Description = "Sets up Imgur API authentication",
1619
Action = args =>
1720
{
18-
var newId = args[1];
21+
string newId = args[1];
1922

2023
CliOutput.WriteInfo("New client ID and secret: {0}", newId);
2124

@@ -30,7 +33,7 @@ public static class Commands
3033
Description = "Sets up SauceNao API authentication",
3134
Action = args =>
3235
{
33-
var newKey = args[1];
36+
string newKey = args[1];
3437

3538
CliOutput.WriteInfo("New API key: {0}", newKey);
3639

@@ -45,7 +48,7 @@ public static class Commands
4548
Description = "Sets the search engines to utilize when searching; delimited by commas",
4649
Action = args =>
4750
{
48-
var newOptions = args[1];
51+
string newOptions = args[1];
4952

5053
CliOutput.WriteInfo("Engines: {0}", newOptions);
5154

@@ -61,7 +64,7 @@ public static class Commands
6164
"open in the browser when search is complete; delimited by commas",
6265
Action = args =>
6366
{
64-
var newOptions = args[1];
67+
string newOptions = args[1];
6568

6669
CliOutput.WriteInfo("Priority engines: {0}", newOptions);
6770

@@ -90,7 +93,7 @@ public static class Commands
9093
Action = args => { Config.AddToPath(); }
9194
};
9295

93-
private static readonly CliCommand Reset = new CliCommand()
96+
private static readonly CliCommand Reset = new CliCommand
9497
{
9598
Parameter = "--reset",
9699
Syntax = "[all]",
@@ -109,22 +112,22 @@ public static class Commands
109112
}
110113
};
111114

112-
private static readonly CliCommand Info = new CliCommand()
115+
private static readonly CliCommand Info = new CliCommand
113116
{
114117
Parameter = "--info",
115118
Syntax = null,
116119
Description = "Information about the program",
117120
Action = args => { Config.Info(); }
118121
};
119122

120-
private static readonly CliCommand Help = new CliCommand()
123+
private static readonly CliCommand Help = new CliCommand
121124
{
122125
Parameter = "--help",
123126
Syntax = null,
124127
Description = "Display available commands",
125128
Action = args =>
126129
{
127-
CliOutput.WriteHelp();
130+
CliOutput.WriteCommands();
128131
CliOutput.WriteInfo("Readme: {0}", Config.Readme);
129132
}
130133
};
@@ -139,7 +142,6 @@ public static void Setup()
139142
{
140143
CliOutput.Commands.AddRange(AllCommands);
141144
CliOutput.Init(Config.NAME);
142-
Config.Check();
143145
}
144146
}
145147
}

SmartImage/Config.cs

Lines changed: 33 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1+
#region
2+
13
using System;
2-
using System.Diagnostics;
3-
using System.Diagnostics.CodeAnalysis;
44
using System.IO;
55
using System.Linq;
6-
using System.Reflection;
7-
using System.Security.Permissions;
8-
using Microsoft.Win32;
96
using Neocmd;
10-
using SmartImage.Engines;
117
using SmartImage.Searching;
128
using SmartImage.Utilities;
139

10+
#endregion
11+
1412
namespace SmartImage
1513
{
1614
public static class Config
@@ -37,7 +35,7 @@ public static class Config
3735

3836
internal static string AppFolder {
3937
get {
40-
var folder = Path.GetDirectoryName(Location);
38+
string? folder = Path.GetDirectoryName(Location);
4139

4240

4341
return folder;
@@ -48,23 +46,23 @@ internal static string AppFolder {
4846
internal static bool IsExeInAppFolder => File.Exists(Path.Combine(AppFolder, NAME_EXE));
4947

5048
/// <summary>
51-
/// Null if executable is not in path.
49+
/// Null if executable is not in path.
5250
/// </summary>
5351
internal static string Location => FindExecutableLocation(NAME_EXE);
5452

5553
internal static bool IsContextMenuAdded {
5654
get {
57-
var cmdStr = string.Format(@"reg query {0}", REG_SHELL_CMD);
58-
var cmd = Cli.Shell(cmdStr, true);
55+
string cmdStr = String.Format(@"reg query {0}", REG_SHELL_CMD);
56+
var cmd = Cli.Shell(cmdStr, true);
5957

60-
var stdOut = Cli.ReadAllLines(cmd.StandardOutput);
58+
string[] stdOut = Cli.ReadAllLines(cmd.StandardOutput);
6159

6260
// todo
6361
if (stdOut.Any(s => s.Contains(NAME))) {
6462
return true;
6563
}
6664

67-
var stdErr = Cli.ReadAllLines(cmd.StandardError);
65+
string[] stdErr = Cli.ReadAllLines(cmd.StandardError);
6866

6967
if (stdErr.Any(s => s.Contains("ERROR"))) {
7068
return false;
@@ -75,13 +73,7 @@ internal static bool IsContextMenuAdded {
7573
}
7674
}
7775

78-
internal static bool IsAppFolderInPath {
79-
get {
80-
string dir = Win32.GetEnvironmentPath()?.Split(';').FirstOrDefault(s => s == AppFolder);
81-
82-
return !String.IsNullOrWhiteSpace(dir);
83-
}
84-
}
76+
internal static bool IsAppFolderInPath => ExplorerSystem.IsFolderInPath(AppFolder);
8577

8678
private static RegistryConfig RegConfig { get; } = new RegistryConfig(REG_SUBKEY);
8779

@@ -101,7 +93,7 @@ internal static AuthInfo ImgurAuth {
10193

10294
return new AuthInfo(id);
10395
}
104-
set { RegConfig.Write(REG_IMGUR_CLIENT_ID, value.Id); }
96+
set => RegConfig.Write(REG_IMGUR_CLIENT_ID, value.Id);
10597
}
10698

10799
internal static AuthInfo SauceNaoAuth {
@@ -121,51 +113,39 @@ private static void RemoveFromContextMenu()
121113
string[] code =
122114
{
123115
"@echo off",
124-
string.Format(@"reg.exe delete {0} /f >nul", REG_SHELL)
116+
String.Format(@"reg.exe delete {0} /f >nul", REG_SHELL)
125117
};
126118

127119
Cli.CreateRunBatchFile("rem_from_menu.bat", code);
128120
}
129121

130122
internal static void AddToPath()
131123
{
132-
var oldValue = Win32.GetEnvironmentPath();
124+
string oldValue = ExplorerSystem.EnvironmentPath;
133125

134-
var appFolder = AppFolder;
126+
string appFolder = AppFolder;
135127

136128
if (IsAppFolderInPath) {
137129
CliOutput.WriteInfo("Executable is already in path: {0}", Location);
138130
return;
139131
}
140132

141133

142-
bool appFolderInPath = oldValue.Split(';').Any(p => p == appFolder);
134+
bool appFolderInPath = oldValue.Split(ExplorerSystem.PATH_DELIM).Any(p => p == appFolder);
143135

144136

145-
var cd = Environment.CurrentDirectory;
146-
var exe = Path.Combine(cd, NAME_EXE);
137+
string cd = Environment.CurrentDirectory;
138+
string exe = Path.Combine(cd, NAME_EXE);
147139

148140

149141
if (appFolderInPath) {
150142
CliOutput.WriteInfo("App folder already in path: {0}", appFolder);
151143
}
152144
else {
153-
var newValue = oldValue + @";" + cd;
154-
Win32.SetEnvironmentPath(newValue);
145+
string newValue = oldValue + ExplorerSystem.PATH_DELIM + cd;
146+
ExplorerSystem.EnvironmentPath = newValue;
155147
CliOutput.WriteInfo("Added {0} to path", cd);
156148
}
157-
158-
159-
var dest = Path.Combine(appFolder, NAME_EXE);
160-
161-
162-
// todo: fix
163-
//Common.TryMove(exe, dest);
164-
// Can't reload environment variables immediately
165-
//Console.WriteLine("Global alloc: {0}", Alloc.Count);
166-
//Environment.Exit(0);
167-
168-
return;
169149
}
170150

171151
internal static void Info()
@@ -196,10 +176,10 @@ internal static void Info()
196176

197177
internal static void AddToContextMenu()
198178
{
199-
var fullPath = Location;
179+
string fullPath = Location;
200180

201181
if (!IsExeInAppFolder) {
202-
var v = CliOutput.ReadConfirm("Could not find exe in system path. Add now?");
182+
bool v = CliOutput.ReadConfirm("Could not find exe in system path. Add now?");
203183

204184
if (v) {
205185
AddToPath();
@@ -216,9 +196,7 @@ internal static void AddToContextMenu()
216196
string[] commandCode =
217197
{
218198
"@echo off",
219-
//String.Format("SET \"SMARTIMAGE={0}\"", fullPath),
220-
//"SET COMMAND=%SMARTIMAGE% \"\"%%1\"\"",
221-
string.Format("reg.exe add {0} /ve /d \"{1} \"\"%%1\"\"\" /f >nul", REG_SHELL_CMD, fullPath)
199+
String.Format("reg.exe add {0} /ve /d \"{1} \"\"%%1\"\"\" /f >nul", REG_SHELL_CMD, fullPath)
222200
};
223201

224202
Cli.CreateRunBatchFile("add_to_menu.bat", commandCode);
@@ -228,24 +206,13 @@ internal static void AddToContextMenu()
228206
string[] iconCode =
229207
{
230208
"@echo off",
231-
//String.Format("SET \"SMARTIMAGE={0}\"", fullPath),
232-
//"SET ICO=%SMARTIMAGE%",
233209
String.Format("reg.exe add {0} /v Icon /d \"{1}\" /f >nul", REG_SHELL, fullPath),
234210
};
235211

236212
Cli.CreateRunBatchFile("add_icon_to_menu.bat", iconCode);
237213
}
238214

239-
internal static void RemoveFromPath()
240-
{
241-
var oldValue = Win32.GetEnvironmentPath();
242-
243-
244-
var newValue = oldValue.Replace(";" + AppFolder, String.Empty);
245-
246-
247-
Win32.SetEnvironmentPath(newValue);
248-
}
215+
internal static void RemoveFromPath() => ExplorerSystem.RemoveFromPath(AppFolder);
249216

250217
internal static void Reset(bool all = false)
251218
{
@@ -267,47 +234,23 @@ internal static void Reset(bool all = false)
267234
Info();
268235
}
269236

270-
internal static void Check()
271-
{
272-
//var files = AppFolder.GetFiles("*.exe").Any(f => f.Name == NAME_EXE);
273-
274-
//CliOutput.WriteInfo("{0}", files);
275-
276-
//var l = new FileInfo(Location);
277-
//bool exeInAppFolder = l.DirectoryName == AppFolder.Name;
278-
}
279-
280-
private static string GetPath(string exe)
281-
{
282-
string dir = Win32.GetEnvironmentPath().Split(';')
283-
.FirstOrDefault(s => File.Exists(Path.Combine(s, exe)));
284-
285-
if (!String.IsNullOrWhiteSpace(dir)) {
286-
return Path.Combine(dir, exe);
287-
}
288-
289-
return null;
290-
}
291-
292-
internal static string FindExecutableLocation(string exe)
237+
private static string FindExecutableLocation(string exe)
293238
{
294-
var path = GetPath(exe);
239+
string path = ExplorerSystem.FindExectableInPath(exe);
295240

296241
if (path == null) {
297-
var cd = Environment.CurrentDirectory;
298-
var cdExe = Path.Combine(cd, exe);
299-
var inCd = File.Exists(cdExe);
242+
string cd = Environment.CurrentDirectory;
243+
string cdExe = Path.Combine(cd, exe);
244+
bool inCd = File.Exists(cdExe);
300245

301246
if (inCd) {
302247
return cdExe;
303248
}
304249

305-
else {
306-
var appFolderExe = Path.Combine(AppFolder, exe);
307-
var inAppFolder = File.Exists(appFolderExe);
308-
if (inAppFolder) {
309-
return appFolderExe;
310-
}
250+
string appFolderExe = Path.Combine(AppFolder, exe);
251+
bool inAppFolder = File.Exists(appFolderExe);
252+
if (inAppFolder) {
253+
return appFolderExe;
311254
}
312255
}
313256

SmartImage/Engines/Bing.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
#region
2+
13
using SmartImage.Model;
24
using SmartImage.Searching;
35

6+
#endregion
7+
48
namespace SmartImage.Engines
59
{
610
public sealed class Bing : QuickSearchEngine
711
{
812
public Bing() : base("https://www.bing.com/images/searchbyimage?cbir=sbi&imgurl=") { }
913
public override SearchEngines Engine => SearchEngines.Bing;
10-
public override string Name => "Bing";
14+
public override string Name => "Bing";
1115
}
1216
}

SmartImage/Engines/GoogleImages.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
#region
2+
13
using SmartImage.Model;
24
using SmartImage.Searching;
35

6+
#endregion
7+
48
namespace SmartImage.Engines
59
{
610
public sealed class GoogleImages : QuickSearchEngine
711
{
8-
public GoogleImages() : base("http://images.google.com/searchbyimage?image_url=") {}
12+
public GoogleImages() : base("http://images.google.com/searchbyimage?image_url=") { }
913

1014
public override string Name => "Google Images";
1115

0 commit comments

Comments
 (0)