From f1ac2d2f9fd726f9eddaca0b03bfa4367925115f Mon Sep 17 00:00:00 2001 From: ema Date: Tue, 10 Dec 2024 16:42:20 +0800 Subject: [PATCH 1/2] Merge from emako/QuickLook.Plugin.TrialOfficeViewer 1. Convert to .NET type csproj 2. Add hint of `Readonly file is not supported` 3. Update nuget packages --- Plugin.cs | 16 +-- QuickLook.Plugin.OfficeViewer.csproj | 195 +++++++++++---------------- QuickLook.Plugin.OfficeViewer.sln | 4 +- SyncfusionControl.cs | 170 +++++++++++------------ 4 files changed, 170 insertions(+), 215 deletions(-) diff --git a/Plugin.cs b/Plugin.cs index d0bb579..c7282c2 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -1,31 +1,31 @@ // Copyright © 2018 Paddy Xu -// +// // 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 . +using QuickLook.Common.Plugin; using System.IO; using System.Linq; using System.Windows; -using QuickLook.Common.Plugin; namespace QuickLook.Plugin.OfficeViewer { public class Plugin : IViewer { private readonly string[] _formats = - {".doc", ".docm", ".docx", ".rtf", ".xls", ".xlsx", ".xlsm", ".pptx", ".pptm", ".potx", ".potm"}; + [".doc", ".docm", ".docx", ".rtf", ".xls", ".xlsx", ".xlsm", ".pptx", ".pptm", ".potx", ".potm"]; public int Priority => 0; @@ -41,7 +41,7 @@ public bool CanHandle(string path) public void Prepare(string path, ContextObject context) { - context.SetPreferredSizeFit(new Size {Width = 1920, Height = 1440}, 0.9); + context.SetPreferredSizeFit(new Size { Width = 1920, Height = 1440 }, 0.9d); } public void View(string path, ContextObject context) @@ -58,4 +58,4 @@ public void Cleanup() { } } -} \ No newline at end of file +} diff --git a/QuickLook.Plugin.OfficeViewer.csproj b/QuickLook.Plugin.OfficeViewer.csproj index 8660d78..bc8af02 100644 --- a/QuickLook.Plugin.OfficeViewer.csproj +++ b/QuickLook.Plugin.OfficeViewer.csproj @@ -1,118 +1,81 @@  - - - - Debug - AnyCPU - {863ECAAC-18D9-4256-A27D-0F308089FB47} - library - QuickLook.Plugin.OfficeViewer - QuickLook.Plugin.OfficeViewer - v4.6.2 - 512 - {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - 4 - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - true - Debug\ - DEBUG;TRACE - full - x86 - prompt - MinimumRecommendedRules.ruleset - - - Release\ - TRACE - true - pdbonly - x86 - prompt - MinimumRecommendedRules.ruleset - - - false - - - - - - - - QuickLook.Plugin.PDFViewer\QuickLook.Plugin.PDFViewer.dll - False - - - - - 4.0 - - - - - - - - - - - - - - - - - - {85fdd6ba-871d-46c8-bd64-f6bb0cb5ea95} - QuickLook.Common - False - - - - - Always - - - - - 18.2.0.59 - - - 18.2.0.59 - - - 18.2.0.59 - - - 18.2.0.59 - - - 18.2.0.59 - - - 18.2.0.59 - - - 18.2.0.59 - - - + + + + net462 + QuickLook.Plugin.OfficeViewer + QuickLook.Plugin.OfficeViewer + true + latest + {3BC001F4-1B29-4526-923F-E56E9B12980A} + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + Debug + AnyCPU + 512 + 4 + false + false + MinimumRecommendedRules.ruleset + + + + DEBUG;TRACE + true + full + false + ..\QuickLook\Build\Debug\QuickLook.Plugin\QuickLook.Plugin.OfficeViewer + + + + TRACE + false + Portable + true + bin\Release\ + + + + + + + + + + + + QuickLook.Plugin.PDFViewer\QuickLook.Plugin.PDFViewer.dll + False + + + + + + + + + + + Always + + + + + + + + + + + + + + + + + {85FDD6BA-871D-46C8-BD64-F6BB0CB5EA95} + QuickLook.Common + False + + + \ No newline at end of file diff --git a/QuickLook.Plugin.OfficeViewer.sln b/QuickLook.Plugin.OfficeViewer.sln index 53abfa3..d03d0e9 100644 --- a/QuickLook.Plugin.OfficeViewer.sln +++ b/QuickLook.Plugin.OfficeViewer.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28010.2003 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34728.123 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickLook.Plugin.OfficeViewer", "QuickLook.Plugin.OfficeViewer.csproj", "{863ECAAC-18D9-4256-A27D-0F308089FB47}" EndProject diff --git a/SyncfusionControl.cs b/SyncfusionControl.cs index b7186d8..1a0e6d1 100644 --- a/SyncfusionControl.cs +++ b/SyncfusionControl.cs @@ -1,9 +1,4 @@ -using System; -using System.IO; -using System.Windows.Controls; -using System.Windows.Media; -using System.Windows.Threading; -using QuickLook.Plugin.PDFViewer; +using QuickLook.Plugin.PDFViewer; using Syncfusion; using Syncfusion.OfficeChartToImageConverter; using Syncfusion.Presentation; @@ -13,106 +8,103 @@ using Syncfusion.UI.Xaml.Spreadsheet.GraphicCells; using Syncfusion.UI.Xaml.SpreadsheetHelper; using Syncfusion.Windows.Controls.RichTextBoxAdv; +using System; +using System.IO; +using System.Windows.Controls; +using System.Windows.Media; +using System.Windows.Threading; + +namespace QuickLook.Plugin.OfficeViewer; -namespace QuickLook.Plugin.OfficeViewer +internal static class SyncfusionControl { - internal static class SyncfusionControl + public static Control Open(string path) { - public static Control Open(string path) + if (new FileInfo(path).IsReadOnly) { - switch (Path.GetExtension(path)?.ToLower()) - { - case ".doc": - case ".docx": - case ".docm": - case ".rtf": - return OpenWord(path); - case ".xls": - case ".xlsx": - case ".xlsm": - return OpenExcel(path); - case ".pptx": - case ".pptm": - case ".potx": - case ".potm": - return OpenPowerpoint(path); - default: - return new Label {Content = "File not supported."}; - } + return new Label { Content = "Readonly file is not supported." }; } - private static Control OpenWord(string path) + return (Path.GetExtension(path)?.ToLower()) switch { - var editor = new SfRichTextBoxAdv - { - IsReadOnly = true, - Background = Brushes.Transparent, - EnableMiniToolBar = false - }; + ".doc" or ".docx" or ".docm" or ".rtf" => OpenWord(path), + ".xls" or ".xlsx" or ".xlsm" => OpenExcel(path), + ".pptx" or ".pptm" or ".potx" or ".potm" => OpenPowerpoint(path), + _ => new Label { Content = "File not supported." }, + }; + } - editor.LoadAsync(path); + private static Control OpenWord(string path) + { + var editor = new SfRichTextBoxAdv + { + IsReadOnly = true, + Background = Brushes.Transparent, + EnableMiniToolBar = false, + }; - return editor; - } + editor.LoadAsync(path); + + return editor; + } + + private static Control OpenExcel(string path) + { + // Pre-load Syncfusion.Tools.Wpf.dll or it will throw error + var _ = new ToolsWPFAssembly(); - private static Control OpenExcel(string path) + var sheet = new SfSpreadsheet { - // Pre-load Syncfusion.Tools.Wpf.dll or it will throw error - var _ = new ToolsWPFAssembly(); + AllowCellContextMenu = false, + AllowTabItemContextMenu = false, + Background = Brushes.Transparent, + DisplayAlerts = false + }; - var sheet = new SfSpreadsheet - { - AllowCellContextMenu = false, - AllowTabItemContextMenu = false, - Background = Brushes.Transparent, - DisplayAlerts = false - }; + sheet.AddGraphicChartCellRenderer(new GraphicChartCellRenderer()); - sheet.AddGraphicChartCellRenderer(new GraphicChartCellRenderer()); + sheet.Open(path); - sheet.Open(path); + sheet.WorkbookLoaded += (sender, e) => + { + sheet.SuspendFormulaCalculation(); - sheet.WorkbookLoaded += (sender, e) => - { - sheet.SuspendFormulaCalculation(); + sheet.Protect(true, true, string.Empty); + sheet.Workbook.Worksheets.ForEach(s => sheet.ProtectSheet(s, string.Empty)); + sheet.GridCollection.ForEach(kv => kv.Value.ShowHidePopup(false)); + }; - sheet.Protect(true, true, ""); - sheet.Workbook.Worksheets.ForEach(s => sheet.ProtectSheet(s, "")); - sheet.GridCollection.ForEach(kv => kv.Value.ShowHidePopup(false)); - }; + return sheet; + } - return sheet; - } + private static Control OpenPowerpoint(string path) + { + var ppt = Presentation.Open(path); + ppt.ChartToImageConverter = new ChartToImageConverter(); - private static Control OpenPowerpoint(string path) + var settings = new PresentationToPdfConverterSettings { - var ppt = Presentation.Open(path); - ppt.ChartToImageConverter = new ChartToImageConverter(); - - var settings = new PresentationToPdfConverterSettings - { - OptimizeIdenticalImages = true, - ShowHiddenSlides = true - }; - - var pdf = PresentationToPdfConverter.Convert(ppt, settings); - - var viewer = new PdfViewerControl(); - - var tempPdf = new MemoryStream(); - pdf.Save(tempPdf); - pdf.Close(true); - pdf.Dispose(); - ppt.Close(); - ppt.Dispose(); - - viewer.Dispatcher.BeginInvoke(new Action(() => - { - viewer.LoadPdf(tempPdf); - tempPdf.Dispose(); - }), DispatcherPriority.Loaded); - - return viewer; - } + OptimizeIdenticalImages = true, + ShowHiddenSlides = true + }; + + var pdf = PresentationToPdfConverter.Convert(ppt, settings); + + var viewer = new PdfViewerControl(); + + var tempPdf = new MemoryStream(); + pdf.Save(tempPdf); + pdf.Close(true); + pdf.Dispose(); + ppt.Close(); + ppt.Dispose(); + + viewer.Dispatcher.BeginInvoke(new Action(() => + { + viewer.LoadPdf(tempPdf); + tempPdf.Dispose(); + }), DispatcherPriority.Loaded); + + return viewer; } -} \ No newline at end of file +} From c34ef0a2840ab73fc4ae2a02e5b12fb4ebd2d680 Mon Sep 17 00:00:00 2001 From: ema Date: Tue, 10 Dec 2024 17:08:00 +0800 Subject: [PATCH 2/2] Button to remove readonly file attribute --- SyncfusionControl.cs | 55 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/SyncfusionControl.cs b/SyncfusionControl.cs index 1a0e6d1..2f8891b 100644 --- a/SyncfusionControl.cs +++ b/SyncfusionControl.cs @@ -10,7 +10,9 @@ using Syncfusion.Windows.Controls.RichTextBoxAdv; using System; using System.IO; +using System.Windows; using System.Windows.Controls; +using System.Windows.Input; using System.Windows.Media; using System.Windows.Threading; @@ -20,9 +22,42 @@ internal static class SyncfusionControl { public static Control Open(string path) { - if (new FileInfo(path).IsReadOnly) + // The Syncfusion method we are currently using does not support reading read-only file + if ((File.GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { - return new Label { Content = "Readonly file is not supported." }; + return new ContentControl() + { + Content = new StackPanel() + { + Children = + { + new Label { Content = "Read-only file is not supported." }, + new Button + { + Content = "Remove read-only file attribute", + Margin = new Thickness(0, 10, 0, 0), + Command = new RelayCommand(() => + { + try + { + FileAttributes attributes = File.GetAttributes(path); + + if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) + { + // Try to remove read-only file attribute + File.SetAttributes(path, attributes & ~FileAttributes.ReadOnly); + MessageBox.Show("Removed readonly file attribute successfully, please try to open again.", "Success", MessageBoxButton.OK, MessageBoxImage.Information); + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); + } + }), + } + } + } + }; } return (Path.GetExtension(path)?.ToLower()) switch @@ -108,3 +143,19 @@ private static Control OpenPowerpoint(string path) return viewer; } } + +file sealed class RelayCommand(Action execute, Func canExecute = null) : ICommand +{ + private readonly Action _execute = execute ?? throw new ArgumentNullException(nameof(execute)); + private readonly Func _canExecute = canExecute; + + public bool CanExecute(object parameter) => _canExecute == null || _canExecute(); + + public void Execute(object parameter) => _execute(); + + public event EventHandler CanExecuteChanged + { + add => CommandManager.RequerySuggested += value; + remove => CommandManager.RequerySuggested -= value; + } +}