Merge OfficeViewer-Native plugin #1662

Repo URL: https://github.com/QL-Win/QuickLook.Plugin.OfficeViewer-Native
This commit is contained in:
ema
2025-06-11 17:58:07 +08:00
parent 4fd8c14b98
commit 5fd2d8a88a
10 changed files with 622 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
// Copyright © 2017-2025 QL-Win Contributors
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System.Runtime.InteropServices;
namespace QuickLook.Plugin.OfficeViewer;
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("b7d14566-0509-4cce-a71f-0a554233bd9b")]
internal interface IInitializeWithFile
{
public void Initialize([MarshalAs(UnmanagedType.LPWStr)] string pszFilePath, uint grfMode);
}

View File

@@ -0,0 +1,44 @@
// Copyright © 2017-2025 QL-Win Contributors
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace QuickLook.Plugin.OfficeViewer;
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("8895b1c6-b41f-4c1c-a562-0d564250836f")]
internal interface IPreviewHandler
{
public void SetWindow(nint hwnd, ref Rectangle rect);
public void SetRect(ref Rectangle rect);
public void DoPreview();
public void Unload();
public void SetFocus();
public void QueryFocus(out nint pHwnd);
[PreserveSig]
public uint TranslateAccelerator(ref Message pmsg);
}

View File

@@ -0,0 +1,115 @@
// Copyright © 2017-2025 QL-Win Contributors
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using QuickLook.Common.Plugin;
using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using UnblockZoneIdentifier;
namespace QuickLook.Plugin.OfficeViewer;
public class Plugin : IViewer
{
private static readonly string[] Extensions =
[
".doc", ".docx", ".docm",
".xls", ".xlsx", ".xlsm", ".xlsb",
".vsd", ".vsdx",
".ppt", ".pptx",
".odt", ".ods", ".odp"
];
private PreviewPanel _panel;
public int Priority => -1;
public void Init()
{
}
public bool CanHandle(string path)
{
if (Directory.Exists(path))
return false;
if (Extensions.Any(path.ToLower().EndsWith))
return PreviewHandlerHost.GetPreviewHandlerGUID(path) != Guid.Empty;
return false;
}
public void Prepare(string path, ContextObject context)
{
context.SetPreferredSizeFit(new Size { Width = 1200, Height = 800 }, 0.8d);
}
public void View(string path, ContextObject context)
{
// MS Office interface does not allow loading of protected view (It's also possible that I haven't found a way)
// Therefore, we need to predict in advance and then let users choose whether to lift the protection
if (ZoneIdentifierManager.IsZoneBlocked(path))
{
context.Title = $"[PROTECTED VIEW] {Path.GetFileName(path)}";
MessageBoxResult result = MessageBox.Show(
"""
Be careful - files from the Internet can contain viruses.
The Office interface prevents loading in Protected View.
Would you like OfficeViewer-Native to unblock the ZoneIdentifier of Internet?
""",
"PROTECTED VIEW",
MessageBoxButton.YesNo,
MessageBoxImage.Question
);
if (result == MessageBoxResult.Yes)
{
_ = ZoneIdentifierManager.UnblockZone(path);
}
else
{
context.ViewerContent = new Label()
{
Content = "The Office interface prevents loading in Protected View.",
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center,
};
context.Title = $"[PROTECTED VIEW] {Path.GetFileName(path)}";
context.IsBusy = false;
return;
}
}
_panel = new PreviewPanel();
context.ViewerContent = _panel;
context.Title = Path.GetFileName(path);
_panel.PreviewFile(path, context);
context.IsBusy = false;
}
public void Cleanup()
{
_panel?.Dispose();
_panel = null;
}
}

View File

@@ -0,0 +1,169 @@
// Copyright © 2017-2025 QL-Win Contributors
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;
namespace QuickLook.Plugin.OfficeViewer;
/// <summary>
/// A Windows Forms host for Preview Handlers.
/// </summary>
public class PreviewHandlerHost : Control
{
/// <summary>
/// The GUID for the IShellItem interface.
/// </summary>
internal const string GuidIshellitem = "43826d1e-e718-42ee-bc55-a1e261c37bfe";
private IPreviewHandler _mCurrentPreviewHandler;
/// <summary>
/// Initialialises a new instance of the PreviewHandlerHost class.
/// </summary>
public PreviewHandlerHost()
{
Size = new Size(320, 240);
}
/// <summary>
/// Gets the GUID of the current preview handler.
/// </summary>
[Browsable(false)]
[ReadOnly(true)]
public Guid CurrentPreviewHandler { get; private set; } = Guid.Empty;
/// <summary>
/// Releases the unmanaged resources used by the PreviewHandlerHost and optionally releases the managed resources.
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
UnloadPreviewHandler();
if (_mCurrentPreviewHandler != null)
{
Marshal.FinalReleaseComObject(_mCurrentPreviewHandler);
_mCurrentPreviewHandler = null;
GC.Collect();
}
base.Dispose(disposing);
}
/// <summary>
/// Returns the GUID of the preview handler associated with the specified file.
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public static Guid GetPreviewHandlerGUID(string filename)
{
// open the registry key corresponding to the file extension
var ext = Registry.ClassesRoot.OpenSubKey(Path.GetExtension(filename));
if (ext != null)
{
// open the key that indicates the GUID of the preview handler type
var test = ext.OpenSubKey("shellex\\{8895b1c6-b41f-4c1c-a562-0d564250836f}");
if (test != null) return new Guid(Convert.ToString(test.GetValue(null)));
// sometimes preview handlers are declared on key for the class
var className = Convert.ToString(ext.GetValue(null));
if (className != null)
{
test = Registry.ClassesRoot.OpenSubKey(
className + "\\shellex\\{8895b1c6-b41f-4c1c-a562-0d564250836f}");
if (test != null) return new Guid(Convert.ToString(test.GetValue(null)));
}
}
return Guid.Empty;
}
/// <summary>
/// Resizes the hosted preview handler when this PreviewHandlerHost is resized.
/// </summary>
/// <param name="e"></param>
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
var r = ClientRectangle;
_mCurrentPreviewHandler?.SetRect(ref r);
}
/// <summary>
/// Opens the specified file using the appropriate preview handler and displays the result in this PreviewHandlerHost.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public bool Open(string path)
{
UnloadPreviewHandler();
if (string.IsNullOrEmpty(path))
return false;
// try to get GUID for the preview handler
var guid = GetPreviewHandlerGUID(path);
if (guid == Guid.Empty)
return false;
CurrentPreviewHandler = guid;
var o = Activator.CreateInstance(Type.GetTypeFromCLSID(CurrentPreviewHandler, true));
var fileInit = o as IInitializeWithFile;
if (fileInit == null)
return false;
fileInit.Initialize(path, 0);
_mCurrentPreviewHandler = o as IPreviewHandler;
if (_mCurrentPreviewHandler == null)
return false;
if (IsDisposed)
return false;
// bind the preview handler to the control's bounds and preview the content
var r = ClientRectangle;
_mCurrentPreviewHandler.SetWindow(Handle, ref r);
_mCurrentPreviewHandler.DoPreview();
return true;
}
/// <summary>
/// Unloads the preview handler hosted in this PreviewHandlerHost and closes the file stream.
/// </summary>
public void UnloadPreviewHandler()
{
try
{
_mCurrentPreviewHandler?.Unload();
}
catch (Exception)
{
// ignored
}
}
}

View File

@@ -0,0 +1,59 @@
// Copyright © 2017-2025 QL-Win Contributors
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using QuickLook.Common.Plugin;
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms.Integration;
namespace QuickLook.Plugin.OfficeViewer;
public class PreviewPanel : WindowsFormsHost, IDisposable
{
private PreviewHandlerHost _control;
public new void Dispose()
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
Child = null;
_control?.Dispose();
_control = null;
base.Dispose();
}));
}
public void PreviewFile(string file, ContextObject context)
{
_control = new PreviewHandlerHost();
Child = _control;
_control.Open(file);
//SetForegroundWindow(new WindowInteropHelper(context.ViewerWindow).Handle);
//SetActiveWindow(presenter.Handle);
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetActiveWindow(IntPtr hWnd);
}

View File

@@ -0,0 +1,13 @@
<UserControl x:Class="QuickLook.Plugin.IPreviewHandlers.PreviewPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="panel"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<Grid>
<WindowsFormsHost x:Name="presenter" />
</Grid>
</UserControl>

View File

@@ -0,0 +1,50 @@
// Copyright © 2017-2025 QL-Win Contributors
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("QuickLook.Plugin.OfficeViewer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("pooi.moe")]
[assembly: AssemblyProduct("QuickLook.Plugin.OfficeViewer")]
[assembly: AssemblyCopyright("Copyright © 2017-2025 QL-Win Contributors")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("9dd27ab3-c99b-4103-8bc8-b220ec399635")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

View File

@@ -0,0 +1,80 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net462</TargetFramework>
<RootNamespace>QuickLook.Plugin.OfficeViewer</RootNamespace>
<AssemblyName>QuickLook.Plugin.OfficeViewer</AssemblyName>
<FileAlignment>512</FileAlignment>
<SignAssembly>false</SignAssembly>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
<LangVersion>latest</LangVersion>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<ProjectGuid>{9DD27AB3-C99B-4103-8BC8-B220EC399635}</ProjectGuid>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\Build\Debug\QuickLook.Plugin\QuickLook.Plugin.OfficeViewer\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\Build\Release\QuickLook.Plugin\QuickLook.Plugin.OfficeViewer\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<OutputPath>..\..\Build\Debug\QuickLook.Plugin\QuickLook.Plugin.OfficeViewer\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>..\..\Build\Release\QuickLook.Plugin\QuickLook.Plugin.OfficeViewer\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\QuickLook.Common\QuickLook.Common.csproj">
<Project>{85FDD6BA-871D-46C8-BD64-F6BB0CB5EA95}</Project>
<Name>QuickLook.Common</Name>
<Private>False</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="UnblockZoneIdentifier " Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\GitVersion.cs">
<Link>Properties\GitVersion.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="Translations.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<Translations>
<en>
<RecycleBinEmptyText>Recycle Bin is empty</RecycleBinEmptyText>
<RecycleBinButton>Empty Recycle Bin</RecycleBinButton>
<ConfirmDeleteText>Are you sure you want to permanently delete these {0} item(s)?</ConfirmDeleteText>
<RecycleBinSizeText>Total size {0}, {1} items</RecycleBinSizeText>
</en>
<zh-CN>
<RecycleBinEmptyText>回收站是空的</RecycleBinEmptyText>
<RecycleBinButton>清空回收站</RecycleBinButton>
<ConfirmDeleteText>您确定要永久删除这 {0} 个项目吗?</ConfirmDeleteText>
<RecycleBinSizeText>总大小 {0},共 {1} 个项目</RecycleBinSizeText>
</zh-CN>
<zh-TW>
<RecycleBinEmptyText>回收站是空的</RecycleBinEmptyText>
<RecycleBinButton>清空回收站</RecycleBinButton>
<ConfirmDeleteText>您確定要永久刪除這 {0} 個項目嗎?</ConfirmDeleteText>
<RecycleBinSizeText>總大小 {0},共 {1} 個項目</RecycleBinSizeText>
</zh-TW>
<ja>
<RecycleBinEmptyText>ごみ箱は空です</RecycleBinEmptyText>
<RecycleBinButton>ごみ箱を空にする</RecycleBinButton>
<ConfirmDeleteText>これらの {0} 項目を完全に削除してもよろしいですか?</ConfirmDeleteText>
<RecycleBinSizeText>合計サイズ {0}、{1} 項目</RecycleBinSizeText>
</ja>
<ko>
<RecycleBinEmptyText>휴지통이 비어 있습니다</RecycleBinEmptyText>
<RecycleBinButton>휴지통 비우기</RecycleBinButton>
<ConfirmDeleteText>{0}개의 항목을 영구적으로 삭제하시겠습니까?</ConfirmDeleteText>
<RecycleBinSizeText>총 크기 {0}, 총 {1}개 항목</RecycleBinSizeText>
</ko>
<fr>
<RecycleBinEmptyText>La corbeille est vide</RecycleBinEmptyText>
<RecycleBinButton>Vider la corbeille</RecycleBinButton>
<ConfirmDeleteText>Êtes-vous sûr de vouloir supprimer définitivement ces {0} élément(s) ?</ConfirmDeleteText>
<RecycleBinSizeText>Taille totale {0}, {1} éléments</RecycleBinSizeText>
</fr>
<de>
<RecycleBinEmptyText>Der Papierkorb ist leer</RecycleBinEmptyText>
<RecycleBinButton>Papierkorb leeren</RecycleBinButton>
<ConfirmDeleteText>Möchten Sie diese {0} Elemente wirklich dauerhaft löschen?</ConfirmDeleteText>
<RecycleBinSizeText>Gesamtgröße {0}, {1} Elemente</RecycleBinSizeText>
</de>
<es>
<RecycleBinEmptyText>La papelera está vacía</RecycleBinEmptyText>
<RecycleBinButton>Vaciar papelera</RecycleBinButton>
<ConfirmDeleteText>¿Está seguro de que desea eliminar permanentemente estos {0} elemento(s)?</ConfirmDeleteText>
<RecycleBinSizeText>Tamaño total {0}, {1} elementos</RecycleBinSizeText>
</es>
</Translations>