COM is really a nightmare... Use managed solution (SharpCompress) instead

This commit is contained in:
Paddy Xu
2017-05-01 12:09:16 +03:00
parent e6b7e14bba
commit dbafec9cb8
20 changed files with 82 additions and 4390 deletions

2
MakeZip.bat Normal file
View File

@@ -0,0 +1,2 @@
bc a -r Debug_%date:~-4,4%%date:~-7,2%%date:~-10,2%.zip Build\Debug\*.exe Build\Debug\*.dll
bc a -r Release_%date:~-4,4%%date:~-7,2%%date:~-10,2%.zip Build\Release\*.exe Build\Release\*.dll

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Windows.Controls; using System.Windows.Controls;
using SevenZip; using SharpCompress.Archives;
namespace QuickLook.Plugin.ArchiveViewer namespace QuickLook.Plugin.ArchiveViewer
{ {
@@ -65,23 +65,25 @@ namespace QuickLook.Plugin.ArchiveViewer
private void LoadItemsFromArchive(string path) private void LoadItemsFromArchive(string path)
{ {
using (var reader = new SevenZipExtractor(path)) using (var stream = File.OpenRead(path))
{ {
_totalZippedSize = (ulong) reader.PackedSize; var archive = ArchiveFactory.Open(stream);
_solid = reader.IsSolid;
_type = reader.Format.ToString(); _totalZippedSize = (ulong) archive.TotalSize;
_solid = archive.IsSolid;
_type = archive.Type.ToString();
var root = new ArchiveFileEntry(Path.GetFileName(path), true); var root = new ArchiveFileEntry(Path.GetFileName(path), true);
_fileEntries.Add("", root); _fileEntries.Add("", root);
foreach (var entry in reader.ArchiveFileData) foreach (var entry in archive.Entries)
ProcessByLevel(entry); ProcessByLevel(entry);
} }
} }
private void ProcessByLevel(ArchiveFileInfo entry) private void ProcessByLevel(IArchiveEntry entry)
{ {
var pf = GetPathFragments(entry.FileName); var pf = GetPathFragments(entry.Key);
// process folders. When entry is a directory, all fragments are folders. // process folders. When entry is a directory, all fragments are folders.
pf.Take(entry.IsDirectory ? pf.Length : pf.Length - 1) pf.Take(entry.IsDirectory ? pf.Length : pf.Length - 1)
@@ -107,11 +109,11 @@ namespace QuickLook.Plugin.ArchiveViewer
ArchiveFileEntry parent; ArchiveFileEntry parent;
_fileEntries.TryGetValue(GetDirectoryName(file), out parent); _fileEntries.TryGetValue(GetDirectoryName(file), out parent);
_fileEntries.Add(file, new ArchiveFileEntry(Path.GetFileName(entry.FileName), false, parent) _fileEntries.Add(file, new ArchiveFileEntry(Path.GetFileName(entry.Key), false, parent)
{ {
Encrypted = entry.Encrypted, Encrypted = entry.IsEncrypted,
Size = entry.Size, Size = (ulong) entry.Size,
ModifiedDate = entry.LastWriteTime ModifiedDate = entry.LastModifiedTime ?? new DateTime()
}); });
} }
} }
@@ -125,7 +127,7 @@ namespace QuickLook.Plugin.ArchiveViewer
private string[] GetPathFragments(string path) private string[] GetPathFragments(string path)
{ {
var frags = path.Split('\\'); var frags = path.Split('\\', '/').Where(f => !string.IsNullOrEmpty(f)).ToArray();
return frags.Select((s, i) => frags.Take(i + 1).Aggregate((a, b) => a + "\\" + b)).ToArray(); return frags.Select((s, i) => frags.Take(i + 1).Aggregate((a, b) => a + "\\" + b)).ToArray();
} }

View File

@@ -9,6 +9,9 @@ namespace QuickLook.Plugin.ArchiveViewer
{ {
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{ {
if (values[0] == DependencyProperty.UnsetValue)
values[0] = 1;
var level = (int) values[0]; var level = (int) values[0];
var indent = (double) values[1]; var indent = (double) values[1];
return indent * level; return indent * level;

View File

@@ -1,7 +1,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Windows; using System.Windows;
using SevenZip; using SharpCompress.Archives;
namespace QuickLook.Plugin.ArchiveViewer namespace QuickLook.Plugin.ArchiveViewer
{ {
@@ -16,41 +16,26 @@ namespace QuickLook.Plugin.ArchiveViewer
if (Directory.Exists(path)) if (Directory.Exists(path))
return false; return false;
try using (var stream = File.OpenRead(path))
{ {
using (var archive = new SevenZipExtractor(path)) try
{ {
// dummy access to the data. If it throws exception, return false ArchiveFactory.Open(stream);
if (archive.ArchiveFileData == null) }
return false; catch (Exception)
{
// ignore some formats return false;
switch (archive.Format)
{
case InArchiveFormat.Chm:
case InArchiveFormat.Flv:
case InArchiveFormat.Elf:
case InArchiveFormat.Msi:
case InArchiveFormat.PE:
case InArchiveFormat.Swf:
return false;
}
} }
} }
catch (Exception)
{
return false;
}
return true; return true;
} }
public void BoundViewSize(string path, ViewerObject context) public void BoundViewSize(string path, ContextObject context)
{ {
context.PreferredSize = new Size {Width = 800, Height = 600}; context.PreferredSize = new Size {Width = 800, Height = 600};
} }
public void View(string path, ViewerObject context) public void View(string path, ContextObject context)
{ {
_panel = new ArchiveInfoPanel(path); _panel = new ArchiveInfoPanel(path);

View File

@@ -34,9 +34,8 @@
<ItemGroup> <ItemGroup>
<Reference Include="PresentationCore" /> <Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" /> <Reference Include="PresentationFramework" />
<Reference Include="SevenZipSharp"> <Reference Include="SharpCompress, Version=0.15.2.0, Culture=neutral, PublicKeyToken=afb0a02973931d96, processorArchitecture=MSIL">
<HintPath>References\SevenZipSharp.dll</HintPath> <HintPath>..\..\packages\SharpCompress.0.15.2\lib\net45\SharpCompress.dll</HintPath>
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
@@ -81,5 +80,8 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
</Page> </Page>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="SharpCompress" version="0.15.2" targetFramework="net452" />
</packages>

View File

@@ -33,14 +33,14 @@ namespace QuickLook.Plugin.ImageViewer
} }
} }
public void BoundViewSize(string path, ViewerObject context) public void BoundViewSize(string path, ContextObject context)
{ {
_imageSize = ImageFileHelper.GetImageSize(path); _imageSize = ImageFileHelper.GetImageSize(path);
context.SetPreferredSizeFit(_imageSize, 0.8); context.SetPreferredSizeFit(_imageSize, 0.8);
} }
public void View(string path, ViewerObject context) public void View(string path, ContextObject context)
{ {
_ip = new ImagePanel(path); _ip = new ImagePanel(path);

View File

@@ -31,12 +31,12 @@ namespace QuickLook.Plugin.OfficeViewer
return false; return false;
} }
public void BoundViewSize(string path, ViewerObject context) public void BoundViewSize(string path, ContextObject context)
{ {
context.SetPreferredSizeFit(new Size {Width = 800, Height = 600}, 0.8); context.SetPreferredSizeFit(new Size {Width = 800, Height = 600}, 0.8);
} }
public void View(string path, ViewerObject context) public void View(string path, ContextObject context)
{ {
using (var officeApp = new OfficeInteropWrapper(path)) using (var officeApp = new OfficeInteropWrapper(path))
{ {

View File

@@ -24,7 +24,7 @@ namespace QuickLook.Plugin.PDFViewer
} }
} }
public void BoundViewSize(string path, ViewerObject context) public void BoundViewSize(string path, ContextObject context)
{ {
_pdfControl = new PdfViewerControl(); _pdfControl = new PdfViewerControl();
@@ -33,7 +33,7 @@ namespace QuickLook.Plugin.PDFViewer
context.SetPreferredSizeFit(desiredSize, 0.8); context.SetPreferredSizeFit(desiredSize, 0.8);
} }
public void View(string path, ViewerObject context) public void View(string path, ContextObject context)
{ {
context.ViewerContent = _pdfControl; context.ViewerContent = _pdfControl;

View File

@@ -40,12 +40,12 @@ namespace QuickLook.Plugin.TextViewer
} }
} }
public void BoundViewSize(string path, ViewerObject context) public void BoundViewSize(string path, ContextObject context)
{ {
context.PreferredSize = new Size {Width = 800, Height = 600}; context.PreferredSize = new Size {Width = 800, Height = 600};
} }
public void View(string path, ViewerObject context) public void View(string path, ContextObject context)
{ {
_tvp = new TextViewerPanel(path); _tvp = new TextViewerPanel(path);

View File

@@ -1,4 +1,5 @@
using System.IO; using System;
using System.IO;
using System.Reflection; using System.Reflection;
using System.Windows; using System.Windows;
@@ -11,6 +12,14 @@ namespace QuickLook
{ {
public static string AppPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); public static string AppPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
protected override void OnStartup(StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException +=
(sender, args) => MessageBox.Show(((Exception) args.ExceptionObject).Message);
base.OnStartup(e);
}
private void Application_Startup(object sender, StartupEventArgs e) private void Application_Startup(object sender, StartupEventArgs e)
{ {
PluginManager.GetInstance(); PluginManager.GetInstance();

View File

@@ -23,7 +23,7 @@
<DockPanel.Style> <DockPanel.Style>
<Style TargetType="{x:Type DockPanel}"> <Style TargetType="{x:Type DockPanel}">
<Style.Triggers> <Style.Triggers>
<DataTrigger Binding="{Binding ViewerObject.IsBusy, ElementName=mainWindow}" Value="False"> <DataTrigger Binding="{Binding ContextObject.IsBusy, ElementName=mainWindow}" Value="False">
<DataTrigger.EnterActions> <DataTrigger.EnterActions>
<BeginStoryboard> <BeginStoryboard>
<Storyboard> <Storyboard>
@@ -43,7 +43,7 @@
<fa:ImageAwesome DockPanel.Dock="Right" x:Name="buttonCloseWindow" Icon="TimesCircle" <fa:ImageAwesome DockPanel.Dock="Right" x:Name="buttonCloseWindow" Icon="TimesCircle"
Height="15" Margin="10,0" Foreground="Gray" Height="15" Margin="10,0" Foreground="Gray"
Cursor="Hand" /> Cursor="Hand" />
<Label x:Name="titlebarTitleArea" Content="{Binding ViewerObject.Title, ElementName=mainWindow}" <Label x:Name="titlebarTitleArea" Content="{Binding ContextObject.Title, ElementName=mainWindow}"
FontSize="14" HorizontalContentAlignment="Center" FontSize="14" HorizontalContentAlignment="Center"
VerticalContentAlignment="Center" /> VerticalContentAlignment="Center" />
</DockPanel> </DockPanel>
@@ -55,7 +55,7 @@
<Grid.Style> <Grid.Style>
<Style TargetType="{x:Type Grid}"> <Style TargetType="{x:Type Grid}">
<Style.Triggers> <Style.Triggers>
<DataTrigger Binding="{Binding ViewerObject.IsBusy, ElementName=mainWindow}" Value="False"> <DataTrigger Binding="{Binding ContextObject.IsBusy, ElementName=mainWindow}" Value="False">
<DataTrigger.EnterActions> <DataTrigger.EnterActions>
<BeginStoryboard> <BeginStoryboard>
<Storyboard> <Storyboard>

View File

@@ -16,7 +16,7 @@ namespace QuickLook
internal MainWindow() internal MainWindow()
{ {
// this object should be initialized before loading UI components, because many of which are binding to it. // this object should be initialized before loading UI components, because many of which are binding to it.
ViewerObject = new ViewerObject(); ContextObject = new ContextObject();
InitializeComponent(); InitializeComponent();
@@ -34,13 +34,13 @@ namespace QuickLook
titlebarTitleArea.MouseLeftButtonDown += DragMoveCurrentWindow; titlebarTitleArea.MouseLeftButtonDown += DragMoveCurrentWindow;
} }
public ViewerObject ViewerObject { get; } public ContextObject ContextObject { get; }
public void Dispose() public void Dispose()
{ {
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
ViewerObject?.Dispose(); ContextObject?.Dispose();
} }
private void DragMoveCurrentWindow(object sender, MouseButtonEventArgs e) private void DragMoveCurrentWindow(object sender, MouseButtonEventArgs e)
@@ -63,12 +63,12 @@ namespace QuickLook
private new void Show() private new void Show()
{ {
Height = ViewerObject.PreferredSize.Height + titlebar.Height + windowBorder.BorderThickness.Top + Height = ContextObject.PreferredSize.Height + titlebar.Height + windowBorder.BorderThickness.Top +
windowBorder.BorderThickness.Bottom; windowBorder.BorderThickness.Bottom;
Width = ViewerObject.PreferredSize.Width + windowBorder.BorderThickness.Left + Width = ContextObject.PreferredSize.Width + windowBorder.BorderThickness.Left +
windowBorder.BorderThickness.Right; windowBorder.BorderThickness.Right;
ResizeMode = ViewerObject.CanResize ? ResizeMode.CanResizeWithGrip : ResizeMode.NoResize; ResizeMode = ContextObject.CanResize ? ResizeMode.CanResizeWithGrip : ResizeMode.NoResize;
base.Show(); base.Show();
@@ -77,15 +77,15 @@ namespace QuickLook
internal void BeginShow(IViewer matchedPlugin, string path) internal void BeginShow(IViewer matchedPlugin, string path)
{ {
ViewerObject.CurrentContentContainer = viewContentContainer; ContextObject.CurrentContentContainer = viewContentContainer;
ViewerObject.ViewerPlugin = matchedPlugin; ContextObject.ViewerPlugin = matchedPlugin;
// get window size before showing it // get window size before showing it
matchedPlugin.BoundViewSize(path, ViewerObject); matchedPlugin.BoundViewSize(path, ContextObject);
Show(); Show();
matchedPlugin.View(path, ViewerObject); matchedPlugin.View(path, ContextObject);
} }
~MainWindow() ~MainWindow()

View File

@@ -9,7 +9,7 @@ namespace QuickLook.Plugin
/// <summary> /// <summary>
/// A runtime object which allows interaction between this plugin and QuickLook. /// A runtime object which allows interaction between this plugin and QuickLook.
/// </summary> /// </summary>
public class ViewerObject : INotifyPropertyChanged, IDisposable public class ContextObject : INotifyPropertyChanged, IDisposable
{ {
private bool _isBusy = true; private bool _isBusy = true;
@@ -111,7 +111,7 @@ namespace QuickLook.Plugin
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
} }
~ViewerObject() ~ContextObject()
{ {
Dispose(); Dispose();
} }

View File

@@ -22,7 +22,7 @@
/// </summary> /// </summary>
/// <param name="path">The full path of the target file.</param> /// <param name="path">The full path of the target file.</param>
/// <param name="context">A runtime object which allows interaction between this plugin and QuickLook.</param> /// <param name="context">A runtime object which allows interaction between this plugin and QuickLook.</param>
void BoundViewSize(string path, ViewerObject context); void BoundViewSize(string path, ContextObject context);
/// <summary> /// <summary>
/// Start the loading process. During the process a busy indicator will be shown. Finish by setting context.IsBusy to /// Start the loading process. During the process a busy indicator will be shown. Finish by setting context.IsBusy to
@@ -30,7 +30,7 @@
/// </summary> /// </summary>
/// <param name="path">The full path of the target file.</param> /// <param name="path">The full path of the target file.</param>
/// <param name="context">A runtime object which allows interaction between this plugin and QuickLook.</param> /// <param name="context">A runtime object which allows interaction between this plugin and QuickLook.</param>
void View(string path, ViewerObject context); void View(string path, ContextObject context);
/// <summary> /// <summary>
/// Release any unmanaged resource here. /// Release any unmanaged resource here.

View File

@@ -14,7 +14,7 @@ namespace QuickLook.Plugin.InfoPanel
return true; return true;
} }
public void BoundViewSize(string path, ViewerObject context) public void BoundViewSize(string path, ContextObject context)
{ {
_ip = new InfoPanel(); _ip = new InfoPanel();
@@ -22,7 +22,7 @@ namespace QuickLook.Plugin.InfoPanel
context.PreferredSize = new Size {Width = _ip.Width, Height = _ip.Height}; context.PreferredSize = new Size {Width = _ip.Width, Height = _ip.Height};
} }
public void View(string path, ViewerObject context) public void View(string path, ContextObject context)
{ {
_ip.DisplayInfo(path); _ip.DisplayInfo(path);

View File

@@ -13,6 +13,7 @@
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<IsWebBootstrapper>false</IsWebBootstrapper>
<PublishUrl>publish\</PublishUrl> <PublishUrl>publish\</PublishUrl>
<Install>true</Install> <Install>true</Install>
<InstallFrom>Disk</InstallFrom> <InstallFrom>Disk</InstallFrom>
@@ -25,7 +26,6 @@
<MapFileExtensions>true</MapFileExtensions> <MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision> <ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion> <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust> <UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled> <BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup> </PropertyGroup>
@@ -92,7 +92,7 @@
<Compile Include="ViewContentContainer.xaml.cs"> <Compile Include="ViewContentContainer.xaml.cs">
<DependentUpon>ViewContentContainer.xaml</DependentUpon> <DependentUpon>ViewContentContainer.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Plugin\ViewerObject.cs" /> <Compile Include="Plugin\ContextObject.cs" />
<Compile Include="Helpers\WindowHelper.cs" /> <Compile Include="Helpers\WindowHelper.cs" />
<Compile Include="ViewWindowManager.cs" /> <Compile Include="ViewWindowManager.cs" />
<Page Include="Plugin\InfoPanel\InfoPanel.xaml"> <Page Include="Plugin\InfoPanel\InfoPanel.xaml">

View File

@@ -52,7 +52,10 @@ namespace QuickLook
Debug.WriteLine(e.StackTrace); Debug.WriteLine(e.StackTrace);
if (matchedPlugin.GetType() != PluginManager.GetInstance().DefaultPlugin) if (matchedPlugin.GetType() != PluginManager.GetInstance().DefaultPlugin)
{
matchedPlugin.Dispose();
_viewWindow.BeginShow(PluginManager.GetInstance().DefaultPlugin.CreateInstance<IViewer>(), path); _viewWindow.BeginShow(PluginManager.GetInstance().DefaultPlugin.CreateInstance<IViewer>(), path);
}
} }
} }