Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
xupefei committed Apr 29, 2017
1 parent f5d9dc3 commit f11c59b
Show file tree
Hide file tree
Showing 30 changed files with 807 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ public ArchiveFileListView()

public void Dispose()
{
GC.SuppressFinalize(this);

IconManager.ClearCache();
}

~ArchiveFileListView()
{
Dispose();
}

public void SetDataContext(object context)
{
treeGrid.DataContext = context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ public ArchiveInfoPanel(string path)

public void Dispose()
{
GC.SuppressFinalize(this);

fileListView.Dispose();
}

~ArchiveInfoPanel()
{
Dispose();
}

private void LoadArchive(string path)
{
LoadItemsFromArchive(path);
Expand Down
18 changes: 10 additions & 8 deletions QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ public bool CanHandle(string path)
if (Directory.Exists(path))
return false;

SevenZipExtractor archive = null;
try
{
using (archive = new SevenZipExtractor(path))
using (var archive = new SevenZipExtractor(path))
{
// dummy access to the data. If it throws exception, return false
if (archive.ArchiveFileData == null)
Expand All @@ -42,10 +41,6 @@ public bool CanHandle(string path)
{
return false;
}
finally
{
archive?.Dispose();
}

return true;
}
Expand All @@ -63,9 +58,16 @@ public void View(string path, ViewContentContainer container)
container.Title = $"{Path.GetFileName(path)}";
}

public void Close()
public void Dispose()
{
GC.SuppressFinalize(this);

_panel?.Dispose();
}

~Plugin()
{
_panel.Dispose();
Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<Reference Include="PresentationFramework" />
<Reference Include="SevenZipSharp">
<HintPath>References\SevenZipSharp.dll</HintPath>
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
2 changes: 1 addition & 1 deletion QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void View(string path, ViewContentContainer container)
container.Title = $"{Path.GetFileName(path)} ({_imageSize.Width} × {_imageSize.Height})";
}

public void Close()
public void Dispose()
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<Reference Include="WpfAnimatedGif, Version=1.4.14.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\WpfAnimatedGif.1.4.14\lib\net\WpfAnimatedGif.dll</HintPath>
<HintPath>..\..\packages\WpfAnimatedGif.1.4.14\lib\net\WpfAnimatedGif.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand Down
181 changes: 181 additions & 0 deletions QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/OfficeInteropWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Excel.Application;
using Task = System.Threading.Tasks.Task;

namespace QuickLook.Plugin.OfficeViewer
{
internal class OfficeInteropWrapper : IDisposable
{
public enum FileTypeEnum
{
Word,
Excel,
PowerPoint
}

private readonly string _path;
private readonly string _tempPdf = Path.GetTempFileName();

private Application _excelApp;
private Microsoft.Office.Interop.PowerPoint.Application _powerpointApp;
private Microsoft.Office.Interop.Word.Application _wordApp;

public OfficeInteropWrapper(string path)
{
_path = path;

switch (Path.GetExtension(path).ToLower())
{
case ".doc":
case ".docx":
FileType = FileTypeEnum.Word;
break;
case ".xls":
case ".xlsx":
FileType = FileTypeEnum.Excel;
break;
case ".ppt":
case ".pptx":
FileType = FileTypeEnum.PowerPoint;
break;
default:
throw new NotSupportedException($"{path} is not supported.");
}

LoadApplication();
}

public FileTypeEnum FileType { get; }

public void Dispose()
{
GC.SuppressFinalize(this);

// communicate with COM in a separate thread
Task.Run(() =>
{
try
{
//_wordApp?.Documents.Close(false);
_wordApp?.Quit(false);
_wordApp = null;

_excelApp?.Workbooks.Close();
_excelApp?.Quit();
_excelApp = null;

_powerpointApp?.Quit();
_powerpointApp = null;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
}
})
.Wait();
}

public string SaveAsPdf()
{
if (_wordApp == null && _excelApp == null && _powerpointApp == null)
throw new Exception("Office application launch failed.");

var succeeded = false;

// communicate with COM in a separate thread
Task.Run(() =>
{
try
{
switch (FileType)
{
case FileTypeEnum.Word:
_wordApp.ActiveDocument.ExportAsFixedFormat(_tempPdf,
WdExportFormat.wdExportFormatPDF);
succeeded = true;
break;
case FileTypeEnum.Excel:
_excelApp.ActiveWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF,
_tempPdf);
succeeded = true;
break;
case FileTypeEnum.PowerPoint:
_powerpointApp.ActivePresentation.ExportAsFixedFormat(_tempPdf,
PpFixedFormatType.ppFixedFormatTypePDF);
succeeded = true;
break;
default:
throw new NotSupportedException($"{_path} is not supported.");
}
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
}
})
.Wait();

if (succeeded)
return FileType == FileTypeEnum.Excel
? _tempPdf + ".pdf"
: _tempPdf; // Excel will add ".pdf" to our filename

Dispose();
return string.Empty;
}

private void LoadApplication()
{
var succeeded = false;

// communicate with COM in a separate thread
Task.Run(() =>
{
try
{
switch (FileType)
{
case FileTypeEnum.Word:
_wordApp = new Microsoft.Office.Interop.Word.Application();
_wordApp.Documents.Add(_path);
succeeded = true;
break;
case FileTypeEnum.Excel:
_excelApp = new Application();
_excelApp.Workbooks.Add(_path);
succeeded = true;
break;
case FileTypeEnum.PowerPoint:
_powerpointApp = new Microsoft.Office.Interop.PowerPoint.Application();
_powerpointApp.Presentations.Open(_path);
succeeded = true;
break;
default:
throw new NotSupportedException($"{_path} is not supported.");
}
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
}
})
.Wait();

if (!succeeded)
Dispose();
}

~OfficeInteropWrapper()
{
Dispose();
}
}
}
93 changes: 93 additions & 0 deletions QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/PluginInterface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.IO;
using System.Windows;
using QuickLook.Plugin.PDFViewer;

namespace QuickLook.Plugin.OfficeViewer
{
public class PluginInterface : IViewer
{
private string _pdfPath = "";
private PdfViewerControl _pdfViewer;

public int Priority => int.MaxValue;

public bool CanHandle(string path)
{
if (Directory.Exists(path))
return false;

switch (Path.GetExtension(path).ToLower())
{
case ".doc":
case ".docx":
case ".xls":
case ".xlsx":
case ".ppt":
case ".pptx":
return true;
}

return false;
}

public void Prepare(string path, ViewContentContainer container)
{
container.SetPreferedSizeFit(new Size {Width = 800, Height = 600}, 0.8);
}

public void View(string path, ViewContentContainer container)
{
using (var officeApp = new OfficeInteropWrapper(path))
{
_pdfPath = officeApp.SaveAsPdf();
}

if (string.IsNullOrEmpty(_pdfPath))
throw new Exception("COM failed.");

_pdfViewer = new PdfViewerControl();

_pdfViewer.Loaded += (sender, e) =>
{
try
{
_pdfViewer.LoadPdf(_pdfPath);
}
catch (Exception ex)
{
throw ex;
}

container.Title = $"{Path.GetFileName(path)} (1 / {_pdfViewer.TotalPages})";
};
_pdfViewer.CurrentPageChanged += (sender, e) => container.Title =
$"{Path.GetFileName(path)} ({_pdfViewer.CurrectPage + 1} / {_pdfViewer.TotalPages})";

container.SetContent(_pdfViewer);
}

public void Dispose()
{
GC.SuppressFinalize(this);

// release the Pdf file first
_pdfViewer?.Dispose();
_pdfViewer = null;

try
{
File.Delete(_pdfPath);
}
catch (Exception)
{
// ignored
}
}

~PluginInterface()
{
Dispose();
}
}
}
Loading

0 comments on commit f11c59b

Please sign in to comment.