Skip to content

Commit 923cef2

Browse files
committed
Context menu work
1 parent 556d210 commit 923cef2

File tree

13 files changed

+221
-138
lines changed

13 files changed

+221
-138
lines changed

SmartImage.Lib 3/Resources.Designer.cs

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SmartImage.Lib 3/Resources.resx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@
193193
<value>/usr/share/applications</value>
194194
</data>
195195
<data name="Linux_Desktop_File" xml:space="preserve">
196-
<value>SmartImage.desktop</value>
196+
<value>smartimage.desktop</value>
197+
</data>
198+
<data name="Linux_Launch_Args" xml:space="preserve">
199+
<value>%u</value>
200+
</data>
201+
<data name="Reg_Launch_Args" xml:space="preserve">
202+
<value>"%1"</value>
197203
</data>
198204
</root>

SmartImage.Lib 3/Utilities/AppUtil.cs

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Date: 2024/05/22 @ 15:05:58
33

44
#nullable disable
5-
global using USI=JetBrains.Annotations.UsedImplicitlyAttribute;
5+
global using USI = JetBrains.Annotations.UsedImplicitlyAttribute;
66
using System.ComponentModel.DataAnnotations;
77
using System.Diagnostics;
88
using System.Reflection;
@@ -34,10 +34,6 @@ public static class AppUtil
3434
[SupportedOSPlatformGuard(OS_WIN)]
3535
internal static readonly bool IsWindows = OperatingSystem.IsWindows();
3636

37-
public static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
38-
39-
public static readonly Version Version = Assembly.GetName().Version;
40-
4137
[CBN]
4238
internal static string GetOSName()
4339
{
@@ -122,21 +118,28 @@ public static bool IsContextMenuAdded
122118
}
123119

124120
/// <returns><c>true</c> if operation succeeded; <c>false</c> otherwise</returns>
125-
public static bool HandleContextMenu(bool option)
121+
public static bool? HandleContextMenu(bool option, [CanBeNull] string args = null)
126122
{
127123
if (IsWindows) {
128-
HandleContextMenuWindows(option);
124+
return HandleContextMenuWindows(option, args);
129125
}
130126
else if (IsLinux) {
131-
HandleContextMenuLinux(option);
127+
return HandleContextMenuLinux(option, args);
132128
}
133129

134-
throw new InvalidOperationException();
130+
return null;
135131
}
136132

137133
[SupportedOSPlatform(OS_LINUX)]
138-
public static bool HandleContextMenuLinux(bool option)
134+
internal static bool HandleContextMenuLinux(bool option, [CanBeNull] string args = null)
139135
{
136+
if (!FileSystem.IsRoot) {
137+
throw new SmartImageException("Root permissions required");
138+
139+
}
140+
141+
args ??= R1.Linux_Launch_Args;
142+
140143
if (option) {
141144
string dsk = $"""
142145
[Desktop Entry]
@@ -145,14 +148,15 @@ [Desktop Entry]
145148
Version=1.0
146149
Name=SmartImage
147150
Terminal=true
148-
Exec={ExeLocation} %u
151+
Exec={ExeLocation} {args}
149152
""";
150153
File.WriteAllText(LinuxDesktopFile, dsk);
151154

152155
}
153156
else {
154-
155-
File.Delete(LinuxDesktopFile);
157+
if (File.Exists(LinuxDesktopFile)) {
158+
File.Delete(LinuxDesktopFile);
159+
}
156160
}
157161

158162
// Console.WriteLine(Path.GetFullPath(s));
@@ -164,13 +168,16 @@ [Desktop Entry]
164168
}
165169

166170
[SupportedOSPlatform(OS_WIN)]
167-
public static bool HandleContextMenuWindows(bool option)
171+
internal static bool HandleContextMenuWindows(bool option,[CanBeNull] string args = null)
168172
{
169173
/*
170174
* New context menu
171175
*/
176+
bool ok = false;
172177
switch (option) {
173178
case true:
179+
180+
args ??= R1.Reg_Launch_Args;
174181

175182
RegistryKey regMenu = null;
176183
RegistryKey regCmd = null;
@@ -184,12 +191,15 @@ public static bool HandleContextMenuWindows(bool option)
184191

185192
regCmd = Registry.CurrentUser.CreateSubKey(R1.Reg_Shell_Cmd);
186193

187-
regCmd?.SetValue(String.Empty,
188-
$"\"{fullPath}\" -i \"%1\" -auto -s");
194+
regCmd?.SetValue(String.Empty, $"\"{fullPath}\" {args}");
195+
// regCmd?.SetValue(String.Empty, $"\"{fullPath}\" \"%1\"");
196+
// regCmd?.SetValue(String.Empty, $"\"{fullPath}\" -i \"%1\" -auto -s");
197+
ok = true;
189198
}
190199
catch (Exception ex) {
191200
Trace.WriteLine($"{ex.Message}");
192-
return false;
201+
// return false;
202+
ok = false;
193203
}
194204
finally {
195205
regMenu?.Close();
@@ -214,18 +224,23 @@ public static bool HandleContextMenuWindows(bool option)
214224
reg.Close();
215225
Registry.CurrentUser.DeleteSubKey(R1.Reg_Shell);
216226
}
227+
228+
// return true;
229+
ok = true;
230+
break;
217231
}
218232
catch (Exception ex) {
219233
Trace.WriteLine($"{ex.Message}");
220-
221-
return false;
234+
ok = false;
235+
// return false;
236+
break;
222237
}
223238

224239
break;
225240

226241
}
227242

228-
return false;
243+
return ok;
229244

230245
}
231246

@@ -274,6 +289,7 @@ public static async Task<GHRelease> GetLatestReleaseAsync()
274289

275290
return r.OrderByDescending(x => x.published_at).First();
276291
}
292+
277293
}
278294

279295
[USI(ImplicitUseTargetFlags.WithMembers)]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Author: Deci | Project: SmartImage.Rdx | Name: IntegrationCommand.cs
2+
// Date: 2024/05/22 @ 16:05:51
3+
4+
using SmartImage.Lib.Utilities;
5+
using Spectre.Console.Cli;
6+
7+
namespace SmartImage.Rdx;
8+
9+
public class IntegrationCommand : Command<IntegrationCommandSettings>
10+
{
11+
12+
public override int Execute(CommandContext context, IntegrationCommandSettings settings)
13+
{
14+
try {
15+
// AConsole.WriteLine($"{AppUtil.IsContextMenuAdded}");
16+
17+
if (settings.ContextMenu.HasValue) {
18+
var rv = AppUtil.HandleContextMenu(settings.ContextMenu.Value);
19+
AConsole.WriteLine($"Context menu change: {rv}");
20+
}
21+
22+
AConsole.WriteLine($"Context menu enabled: {AppUtil.IsContextMenuAdded}");
23+
}
24+
catch (Exception e) {
25+
AConsole.WriteException(e);
26+
}
27+
28+
return SearchCommand.EC_OK;
29+
}
30+
31+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Author: Deci | Project: SmartImage.Rdx | Name: IntegrationCommandSettings.cs
2+
// Date: 2024/05/22 @ 16:05:47
3+
4+
using Spectre.Console.Cli;
5+
6+
namespace SmartImage.Rdx;
7+
8+
public class IntegrationCommandSettings : CommandSettings
9+
{
10+
11+
[CommandOption("--ctx-menu")]
12+
public bool? ContextMenu { get; internal set; }
13+
14+
}

SmartImage.Rdx/Program.cs

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using SixLabors.ImageSharp;
1010
using SixLabors.ImageSharp.Formats;
1111
using SmartImage.Lib;
12-
using SmartImage.Lib.Utilities;
1312
using Spectre.Console;
1413
using Spectre.Console.Cli;
1514
using SmartImage.Rdx.Shell;
@@ -84,7 +83,8 @@ public static async Task<int> Main(string[] args)
8483
c.PropagateExceptions();
8584
var helpProvider = new CustomHelpProvider(c.Settings);
8685
c.SetHelpProvider(helpProvider);
87-
c.AddCommand<IntegrationCommand>("integrate");
86+
c.AddCommand<IntegrationCommand>("integrate")
87+
.WithDescription("Configure system integration such as context menu");
8888
});
8989

9090
try {
@@ -102,38 +102,4 @@ public static async Task<int> Main(string[] args)
102102
}
103103
}
104104

105-
}
106-
107-
public class IntegrationCommandSettings : CommandSettings
108-
{
109-
110-
[CommandOption("--ctx-menu")]
111-
public bool? ContextMenu { get; internal set; }
112-
113-
}
114-
115-
public class IntegrationCommand : Command<IntegrationCommandSettings>
116-
{
117-
118-
public void HandleContextMenu(bool option)
119-
{
120-
121-
}
122-
public override int Execute(CommandContext context, IntegrationCommandSettings settings)
123-
{
124-
try {
125-
AConsole.WriteLine($"{AppUtil.IsContextMenuAdded}");
126-
127-
if (settings.ContextMenu.HasValue) {
128-
AppUtil.HandleContextMenu(settings.ContextMenu.Value);
129-
130-
}
131-
}
132-
catch (Exception e) {
133-
AConsole.WriteException(e);
134-
}
135-
136-
return SearchCommand.EC_OK;
137-
}
138-
139105
}

0 commit comments

Comments
 (0)