|
| 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 | +} |
0 commit comments