Skip to content

Commit 9c66eba

Browse files
committed
Add plugin icon registration and include QLPlugin.ico
1 parent c2e5a9c commit 9c66eba

4 files changed

Lines changed: 131 additions & 2 deletions

File tree

QuickLook/App.xaml.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ protected override void OnStartup(StartupEventArgs e)
235235
MessageBoxPatcher.Initialize();
236236

237237
CheckUpdate();
238+
239+
CheckAndRegisterPluginIcon();
238240
}
239241

240242
protected virtual void OnSessionEnding(object sender, SessionEndingEventArgs e)
@@ -317,7 +319,7 @@ private bool EnsureFirstInstance(string[] args)
317319
// Invalid path, continue to show duplicate message
318320
}
319321
}
320-
322+
321323
// Second instance: duplicate
322324
MessageBox.Show(TranslationHelper.Get("APP_SECOND_TEXT"), TranslationHelper.Get("APP_SECOND"),
323325
MessageBoxButton.OK, MessageBoxImage.Information);
@@ -337,6 +339,11 @@ private void CheckUpdate()
337339
SettingHelper.Set("LastUpdateTicks", DateTime.Now.Ticks);
338340
}
339341

342+
private void CheckAndRegisterPluginIcon()
343+
{
344+
_ = Task.Delay(3000).ContinueWith(_ => PluginIconRegistrationHelper.CheckAndRegisterPluginIcon());
345+
}
346+
340347
private void RunListener(StartupEventArgs e)
341348
{
342349
TrayIconManager.GetInstance();
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright © 2017-2026 QL-Win Contributors
2+
//
3+
// This file is part of QuickLook program.
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
using Microsoft.Win32;
19+
using QuickLook.Common.Helpers;
20+
using System;
21+
using System.IO;
22+
using System.Runtime.InteropServices;
23+
using System.Security.Principal;
24+
25+
namespace QuickLook.Helpers;
26+
27+
internal static class PluginIconRegistrationHelper
28+
{
29+
internal static void CheckAndRegisterPluginIcon()
30+
{
31+
try
32+
{
33+
var isElevated = new WindowsPrincipal(WindowsIdentity.GetCurrent())
34+
.IsInRole(WindowsBuiltInRole.Administrator);
35+
if (!isElevated)
36+
return;
37+
38+
var iconPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "QLPlugin.ico");
39+
if (!File.Exists(iconPath))
40+
return;
41+
42+
bool changed = false;
43+
44+
// Computer\HKEY_CLASSES_ROOT\.qlplugin
45+
using (var key = Registry.ClassesRoot.CreateSubKey(".qlplugin", true))
46+
{
47+
if (key != null)
48+
{
49+
if (key.GetValue(string.Empty) as string != "QuickLook.Plugin")
50+
{
51+
key.SetValue(string.Empty, "QuickLook.Plugin");
52+
changed = true;
53+
}
54+
55+
if (key.GetValue("PerceivedType") as string != "compressed")
56+
{
57+
key.SetValue("PerceivedType", "compressed");
58+
changed = true;
59+
}
60+
}
61+
}
62+
63+
// Computer\HKEY_CLASSES_ROOT\QuickLook.Plugin\DefaultIcon
64+
using (var key = Registry.ClassesRoot.CreateSubKey(@"QuickLook.Plugin\DefaultIcon", true))
65+
{
66+
var iconValue = $"{iconPath},0";
67+
if (key != null && key.GetValue("") as string != iconValue)
68+
{
69+
key.SetValue("", iconValue);
70+
changed = true;
71+
}
72+
}
73+
74+
// Computer\HKEY_CLASSES_ROOT\QuickLook.Plugin
75+
using (var key = Registry.ClassesRoot.CreateSubKey("QuickLook.Plugin", true))
76+
{
77+
const string fileTypeName = "QuickLook Plugin File";
78+
if (key != null && key.GetValue(string.Empty) as string != fileTypeName)
79+
{
80+
key.SetValue(string.Empty, fileTypeName);
81+
changed = true;
82+
}
83+
}
84+
85+
// Computer\HKEY_CLASSES_ROOT\QuickLook.Plugin\shell\open\command
86+
using (var key = Registry.ClassesRoot.CreateSubKey(@"QuickLook.Plugin\shell\open\command", true))
87+
{
88+
var commandValue = $"\"{App.AppFullPath}\" \"%1\"";
89+
if (key != null && key.GetValue(string.Empty) as string != commandValue)
90+
{
91+
key.SetValue(string.Empty, commandValue);
92+
changed = true;
93+
}
94+
}
95+
96+
// Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.qlplugin
97+
{
98+
// It is left to the user to choose whether to restore the default option in the future
99+
}
100+
101+
if (changed)
102+
{
103+
SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); // SHCNE_ASSOCCHANGED
104+
}
105+
}
106+
catch (Exception ex)
107+
{
108+
ProcessHelper.WriteLog(ex.ToString());
109+
}
110+
}
111+
112+
[DllImport("shell32.dll", SetLastError = true)]
113+
private static extern void SHChangeNotify(int wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
114+
}

QuickLook/QuickLook.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@
136136
</None>
137137
</ItemGroup>
138138

139+
<ItemGroup>
140+
<Content Include="..\QLPlugin.ico">
141+
<Link>QLPlugin.ico</Link>
142+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
143+
</Content>
144+
</ItemGroup>
145+
139146
<ItemGroup>
140147
<ProjectReference Include="..\QuickLook.Common\QuickLook.Common.csproj">
141148
<Project>{85FDD6BA-871D-46C8-BD64-F6BB0CB5EA95}</Project>

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,8 @@ Interested in contributing? Check out our development resources:
790790

791791
[@OiCkilL](https://twitter.com/OiCkilL)
792792
[@QubitsDev](https://twitter.com/qubitsdev)
793-
[Donno](https://github.com/Donnnno)
793+
[@Donno](https://github.com/Donnnno)
794+
[@Shomnipotence](https://github.com/Shomnipotence)
794795

795796
*Fluent UI & Icons*
796797

0 commit comments

Comments
 (0)