Skip to content

Commit 3b4cacf

Browse files
committed
Start work on OS portability
1 parent dc60615 commit 3b4cacf

File tree

12 files changed

+181
-124
lines changed

12 files changed

+181
-124
lines changed

SmartImage.Lib/SearchClient.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ public sealed class SearchClient
2828
{
2929
public SearchClient(SearchConfig config)
3030
{
31-
Config = config;
32-
33-
Results = new List<SearchResult>();
31+
Config = config;
3432

33+
Results = new List<SearchResult>();
3534
FilteredResults = new List<SearchResult>();
3635

3736
Reload();
@@ -104,7 +103,7 @@ public async Task RunSearchAsync()
104103
}
105104

106105
var tasks = new List<Task<SearchResult>>(Engines.Select(e => e.GetResultAsync(Config.Query)));
107-
106+
108107
Pending = tasks.Count;
109108

110109
while (!IsComplete) {

SmartImage.Lib/SearchConfig.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public sealed class SearchConfig
5050
/// </summary>
5151
public bool NotificationImage { get; set; } = false;
5252

53+
public bool OutputOnly { get; set; } = false;
54+
5355
public override string ToString()
5456
{
5557
var sb = new StringBuilder();
@@ -58,7 +60,8 @@ public override string ToString()
5860
sb.Append("Filtering", Filtering);
5961
sb.Append("Notification", Notification);
6062
sb.Append("Notification image", NotificationImage);
61-
63+
sb.Append("Output only", OutputOnly);
64+
6265

6366
return sb.ToString();
6467
}

SmartImage.Lib/Searching/ImageResult.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,7 @@ public DisplayResolutionType DisplayResolution
133133
get
134134
{
135135
if (HasImageDimensions) {
136-
137-
var resolutionType = ImageHelper.GetDisplayResolution(Width.Value, Height.Value);
138-
139-
140-
return resolutionType;
136+
return ImageHelper.GetDisplayResolution(Width.Value, Height.Value);
141137
}
142138

143139
throw new SmartImageException("Resolution unavailable");

SmartImage.Lib/SmartImage.Lib.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@
3434
</ItemGroup>
3535
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='TestDebug|AnyCPU'">
3636
<DefineConstants>DEBUG;TRACE;JETBRAINS_ANNOTATIONS;TEST_DEBUG</DefineConstants>
37-
<NoWarn>CA1416</NoWarn>
37+
<!-- <NoWarn>CA1416</NoWarn> -->
3838
</PropertyGroup>
3939
</Project>

SmartImage/Core/AppConfig.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ public static Dictionary<string, string> ConfigMap
3232
{
3333
var map = new Dictionary<string, string>()
3434
{
35-
{K_ENGINES, Program.Config.SearchEngines.ToString()},
36-
{K_PRIORITY_ENGINES, Program.Config.PriorityEngines.ToString()},
37-
{K_FILTER, Program.Config.Filtering.ToString()},
38-
{K_NOTIFICATION, Program.Config.Notification.ToString()},
39-
{K_NOTIFICATION_IMAGE, Program.Config.NotificationImage.ToString()},
35+
{ K_ENGINES, Program.Config.SearchEngines.ToString() },
36+
{ K_PRIORITY_ENGINES, Program.Config.PriorityEngines.ToString() },
37+
{ K_FILTER, Program.Config.Filtering.ToString() },
38+
{ K_NOTIFICATION, Program.Config.Notification.ToString() },
39+
{ K_NOTIFICATION_IMAGE, Program.Config.NotificationImage.ToString() },
40+
{ K_OUTPUT_ONLY, Program.Config.OutputOnly.ToString() },
41+
4042
};
4143
return map;
4244
}
@@ -58,6 +60,7 @@ public static void ReadConfigFile()
5860
Program.Config.Filtering = Boolean.Parse(map[K_FILTER]);
5961
Program.Config.Notification = Boolean.Parse(map[K_NOTIFICATION]);
6062
Program.Config.NotificationImage = Boolean.Parse(map[K_NOTIFICATION_IMAGE]);
63+
Program.Config.OutputOnly = Boolean.Parse(map[K_OUTPUT_ONLY]);
6164

6265
SaveConfigFile();
6366

@@ -80,11 +83,13 @@ private static void SaveConfigFile()
8083
private const string K_FILTER = "filter";
8184
private const string K_NOTIFICATION = "notification";
8285
private const string K_NOTIFICATION_IMAGE = "notification-image";
86+
private const string K_OUTPUT_ONLY = "output-only";
87+
8388

8489
public static void UpdateConfig()
8590
{
8691
Program.Client.Reload();
8792
SaveConfigFile();
8893
}
8994
}
90-
}
95+
}

SmartImage/Core/AppInfo.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ public static string ExeLocation
105105
[ModuleInitializer]
106106
public static void Setup()
107107
{
108-
if (!OperatingSystem.IsWindows()) {
108+
/*if (!OperatingSystem.IsWindows()) {
109109
throw new NotSupportedException();
110-
}
110+
}*/
111111

112112
if (!IsAppFolderInPath) {
113113
AppIntegration.HandlePath(true);
114114
}
115115

116116
Debug.WriteLine($"Cli utilities: {ImageHelper.Utilities.QuickJoin()}", C_INFO);
117117

118-
var languages = Windows.System.UserProfile.GlobalizationPreferences.Languages;
118+
/*var languages = Windows.System.UserProfile.GlobalizationPreferences.Languages;
119119
120120
bool zh = languages.Any(l => l.Contains("zh"));
121121
@@ -126,10 +126,10 @@ public static void Setup()
126126
/*Console.WriteLine("Non-Romance language detected!");
127127
Console.WriteLine("If English is not the main IME, things may not work properly!");
128128
129-
ConsoleManager.WaitForInput();*/
129+
ConsoleManager.WaitForInput();#1#
130130
131131
Trace.WriteLine($"Languages: {languages.QuickJoin()}");
132-
}
132+
}*/
133133

134134
//Windows.System.UserProfile.GlobalizationPreferences.Languages
135135
//Thread.CurrentThread.CurrentUICulture

SmartImage/Core/AppIntegration.cs

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Globalization;
77
using System.IO;
88
using System.Linq;
9+
using System.Runtime.Versioning;
910
using System.Threading;
1011
using Kantan.Cli;
1112
using Novus.Utilities;
@@ -19,8 +20,6 @@
1920

2021
namespace SmartImage.Core
2122
{
22-
23-
2423
/// <summary>
2524
/// Program OS integrations
2625
/// </summary>
@@ -39,64 +38,66 @@ public static bool HandleContextMenu(bool option)
3938
/*
4039
* New context menu
4140
*/
41+
if (OperatingSystem.IsWindows()) {
42+
switch (option) {
43+
case true:
4244

43-
switch (option) {
44-
case true:
45+
RegistryKey regMenu = null;
46+
RegistryKey regCmd = null;
4547

46-
RegistryKey regMenu = null;
47-
RegistryKey regCmd = null;
48+
string fullPath = AppInfo.ExeLocation;
4849

49-
string fullPath = AppInfo.ExeLocation;
50+
try {
51+
regMenu = Registry.CurrentUser.CreateSubKey(REG_SHELL);
52+
regMenu?.SetValue(String.Empty, AppInfo.NAME);
53+
regMenu?.SetValue("Icon", $"\"{fullPath}\"");
5054

51-
try {
52-
regMenu = Registry.CurrentUser.CreateSubKey(REG_SHELL);
53-
regMenu?.SetValue(String.Empty, AppInfo.NAME);
54-
regMenu?.SetValue("Icon", $"\"{fullPath}\"");
55+
regCmd = Registry.CurrentUser.CreateSubKey(REG_SHELL_CMD);
56+
regCmd?.SetValue(String.Empty, $"\"{fullPath}\" \"%1\"");
57+
}
58+
catch (Exception ex) {
59+
Trace.WriteLine($"{ex.Message}");
60+
ConsoleManager.WaitForInput();
61+
return false;
62+
}
63+
finally {
64+
regMenu?.Close();
65+
regCmd?.Close();
66+
}
5567

56-
regCmd = Registry.CurrentUser.CreateSubKey(REG_SHELL_CMD);
57-
regCmd?.SetValue(String.Empty, $"\"{fullPath}\" \"%1\"");
58-
}
59-
catch (Exception ex) {
60-
Trace.WriteLine($"{ex.Message}");
61-
ConsoleManager.WaitForInput();
62-
return false;
63-
}
64-
finally {
65-
regMenu?.Close();
66-
regCmd?.Close();
67-
}
68+
break;
69+
case false:
6870

69-
break;
70-
case false:
71+
try {
72+
var reg = Registry.CurrentUser.OpenSubKey(REG_SHELL_CMD);
7173

72-
try {
73-
var reg = Registry.CurrentUser.OpenSubKey(REG_SHELL_CMD);
74+
if (reg != null) {
75+
reg.Close();
76+
Registry.CurrentUser.DeleteSubKey(REG_SHELL_CMD);
77+
}
7478

75-
if (reg != null) {
76-
reg.Close();
77-
Registry.CurrentUser.DeleteSubKey(REG_SHELL_CMD);
78-
}
79+
reg = Registry.CurrentUser.OpenSubKey(REG_SHELL);
7980

80-
reg = Registry.CurrentUser.OpenSubKey(REG_SHELL);
81+
if (reg != null) {
82+
reg.Close();
83+
Registry.CurrentUser.DeleteSubKey(REG_SHELL);
84+
}
85+
}
86+
catch (Exception ex) {
87+
Trace.WriteLine($"{ex.Message}", C_ERROR);
8188

82-
if (reg != null) {
83-
reg.Close();
84-
Registry.CurrentUser.DeleteSubKey(REG_SHELL);
89+
return false;
8590
}
86-
}
87-
catch (Exception ex) {
88-
Trace.WriteLine($"{ex.Message}", C_ERROR);
8991

90-
return false;
91-
}
92+
break;
93+
94+
}
9295

93-
break;
94-
default:
95-
throw new ArgumentOutOfRangeException(nameof(option), option, null);
96-
}
96+
9797

98+
}
9899

99-
return true;
100+
return false;
100101

101102
}
102103

@@ -140,12 +141,16 @@ public static void ResetIntegrations()
140141
// Computer\HKEY_CLASSES_ROOT\*\shell\SmartImage
141142

142143
if (IsContextMenuAdded) {
143-
HandleContextMenu(false);
144+
if (OperatingSystem.IsWindows()) {
145+
HandleContextMenu(false);
146+
147+
}
144148
}
145149

146150
Trace.WriteLine("Reset config");
147151
}
148152

153+
[SupportedOSPlatform("windows")]
149154
[DoesNotReturn]
150155
public static void Uninstall()
151156
{
@@ -186,9 +191,14 @@ public static bool IsContextMenuAdded
186191
{
187192
get
188193
{
189-
var reg = Registry.CurrentUser.OpenSubKey(REG_SHELL_CMD);
190194

191-
return reg != null;
195+
if (OperatingSystem.IsWindows()) {
196+
var reg = Registry.CurrentUser.OpenSubKey(REG_SHELL_CMD);
197+
return reg != null;
198+
199+
}
200+
201+
return false;
192202
}
193203
}
194204
}

0 commit comments

Comments
 (0)