mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-11 17:59:17 +00:00
.
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace QuickLook.Plugin.ArchiveViewer
|
||||
{
|
||||
public class ArchiveFileEntry : IComparable<ArchiveFileEntry>
|
||||
{
|
||||
private readonly ArchiveFileEntry _parent;
|
||||
private int _cachedDepth = -1;
|
||||
private int _cachedLevel = -1;
|
||||
|
||||
public ArchiveFileEntry(string name, bool isFolder, ArchiveFileEntry parent = null)
|
||||
{
|
||||
Name = name;
|
||||
IsFolder = isFolder;
|
||||
|
||||
_parent = parent;
|
||||
_parent?.Children.Add(this, false);
|
||||
}
|
||||
|
||||
public SortedList<ArchiveFileEntry, bool> Children { get; set; } = new SortedList<ArchiveFileEntry, bool>();
|
||||
|
||||
public string Name { get; set; }
|
||||
public bool Encrypted { get; set; }
|
||||
public bool IsFolder { get; set; }
|
||||
public ulong Size { get; set; }
|
||||
public DateTime ModifiedDate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the maximum depth of all siblings
|
||||
/// </summary>
|
||||
public int Level
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_cachedLevel != -1)
|
||||
return _cachedLevel;
|
||||
|
||||
if (_parent == null)
|
||||
_cachedLevel = GetDepth();
|
||||
else
|
||||
_cachedLevel = _parent.Level - 1;
|
||||
|
||||
return _cachedLevel;
|
||||
}
|
||||
}
|
||||
|
||||
public int CompareTo(ArchiveFileEntry other)
|
||||
{
|
||||
if (IsFolder == other.IsFolder)
|
||||
return string.Compare(Name, other.Name, StringComparison.CurrentCulture);
|
||||
|
||||
if (IsFolder)
|
||||
return -1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of nodes in the longest path to a leaf
|
||||
/// </summary>
|
||||
private int GetDepth()
|
||||
{
|
||||
if (_cachedDepth != -1)
|
||||
return _cachedDepth;
|
||||
|
||||
var max = Children.Keys.Count == 0 ? 0 : Children.Keys.Max(r => r.GetDepth());
|
||||
_cachedDepth = max + 1;
|
||||
return _cachedDepth;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (IsFolder)
|
||||
return $"{Name}";
|
||||
|
||||
var en = Encrypted ? "*" : "";
|
||||
return $"{Name}{en},{IsFolder},{Size},{ModifiedDate}";
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,142 @@
|
||||
<UserControl x:Class="QuickLook.Plugin.ArchiveViewer.ArchiveFileListView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:QuickLook.Plugin.ArchiveViewer"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="300" d:DesignWidth="300">
|
||||
<Grid Grid.IsSharedSizeScope="True"
|
||||
x:Name="treeGrid">
|
||||
<Grid.RowDefinitions>
|
||||
<!-- Header row -->
|
||||
<RowDefinition Height="Auto" />
|
||||
<!-- Row for data -->
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<Grid.Resources>
|
||||
|
||||
<!-- Converts the level in the tree to the width of the spacer column -->
|
||||
<local:LevelToIndentConverter x:Key="LevelToIndentConverter" />
|
||||
<local:BooleanToAsteriskConverter x:Key="BooleanToAsteriskConverter" />
|
||||
<local:SizePrettyPrintConverter x:Key="SizePrettyPrintConverter" />
|
||||
<local:DatePrintConverter x:Key="DatePrintConverter" />
|
||||
<local:FileExtToIconConverter x:Key="FileExtToIconConverter" />
|
||||
<local:LevelToBooleanConverter x:Key="LevelToBooleanConverter" />
|
||||
|
||||
<!-- Template for directory information at all levels -->
|
||||
<HierarchicalDataTemplate ItemsSource="{Binding Children.Keys}"
|
||||
DataType="{x:Type local:ArchiveFileEntry}">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="2" />
|
||||
<RowDefinition />
|
||||
<RowDefinition Height="2" />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid Grid.Row="1" ShowGridLines="False">
|
||||
<!-- All column widths are shared except for column 1 which is sized
|
||||
to compensate for different indentation at each level -->
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition MinWidth="300" SharedSizeGroup="rowHeaderColumn" />
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition MinWidth="100" SharedSizeGroup="column1" />
|
||||
<ColumnDefinition MinWidth="100" SharedSizeGroup="column2" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<StackPanel Grid.Column="0" Orientation="Horizontal">
|
||||
<Image Width="16" Height="16">
|
||||
<Image.Source>
|
||||
<MultiBinding Converter="{StaticResource FileExtToIconConverter}">
|
||||
<Binding Path="Name" />
|
||||
<Binding Path="IsFolder" />
|
||||
</MultiBinding>
|
||||
</Image.Source>
|
||||
</Image>
|
||||
<TextBlock Text="{Binding Name}" />
|
||||
<TextBlock
|
||||
Text="{Binding Encrypted, Converter={StaticResource BooleanToAsteriskConverter}}" />
|
||||
</StackPanel>
|
||||
<Rectangle Grid.Column="1">
|
||||
<Rectangle.Width>
|
||||
<MultiBinding Converter="{StaticResource LevelToIndentConverter}">
|
||||
<Binding Path="Level" />
|
||||
<Binding ElementName="treeViewItemToMeasure"
|
||||
Path="ActualWidth" />
|
||||
</MultiBinding>
|
||||
</Rectangle.Width>
|
||||
</Rectangle>
|
||||
<TextBlock Grid.Column="2">
|
||||
<TextBlock.Text>
|
||||
<MultiBinding Converter="{StaticResource SizePrettyPrintConverter}">
|
||||
<Binding Path="Size" />
|
||||
<Binding Path="IsFolder" />
|
||||
</MultiBinding>
|
||||
</TextBlock.Text>
|
||||
</TextBlock>
|
||||
<TextBlock Grid.Column="3">
|
||||
<TextBlock.Text>
|
||||
<MultiBinding Converter="{StaticResource DatePrintConverter}">
|
||||
<Binding Path="ModifiedDate" />
|
||||
<Binding Path="IsFolder" />
|
||||
</MultiBinding>
|
||||
</TextBlock.Text>
|
||||
</TextBlock>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</HierarchicalDataTemplate>
|
||||
</Grid.Resources>
|
||||
|
||||
<!-- Tree view with one item for the header row -->
|
||||
|
||||
<TreeView BorderThickness="0" Focusable="False" Background="Transparent"
|
||||
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
|
||||
<TreeViewItem Focusable="False">
|
||||
<TreeViewItem.Header>
|
||||
<Grid ShowGridLines="False">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition SharedSizeGroup="rowHeaderColumn" />
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition SharedSizeGroup="column1" />
|
||||
<ColumnDefinition SharedSizeGroup="column2" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Grid.Column="0"
|
||||
Text=" " />
|
||||
<TreeViewItem Grid.Column="1">
|
||||
<TreeViewItem.Header>
|
||||
<TreeViewItem x:Name="treeViewItemToMeasure"
|
||||
Padding="0" />
|
||||
</TreeViewItem.Header>
|
||||
|
||||
<!-- Set the width of Column 1 to the same width as the top level
|
||||
in the data -->
|
||||
<TreeViewItem.Width>
|
||||
<MultiBinding Converter="{StaticResource LevelToIndentConverter}">
|
||||
<Binding Path="Level" />
|
||||
<Binding ElementName="treeViewItemToMeasure"
|
||||
Path="ActualWidth" />
|
||||
</MultiBinding>
|
||||
</TreeViewItem.Width>
|
||||
</TreeViewItem>
|
||||
<TextBlock Grid.Column="2"
|
||||
Text="Original Size" />
|
||||
<TextBlock Grid.Column="3"
|
||||
Text="Modified Date" />
|
||||
</Grid>
|
||||
</TreeViewItem.Header>
|
||||
</TreeViewItem>
|
||||
</TreeView>
|
||||
|
||||
<!-- Tree view that will display hierarchical data rows -->
|
||||
|
||||
<TreeView Grid.Row="1"
|
||||
x:Name="treeView"
|
||||
BorderThickness="0" Background="Transparent"
|
||||
ItemsSource="{Binding}">
|
||||
<!--<TreeView.ItemContainerStyle>
|
||||
<Style TargetType="{x:Type TreeViewItem}">
|
||||
<Setter Property="IsExpanded" Value="{Binding Level, Converter={StaticResource LevelToBooleanConverter}}" />
|
||||
</Style>
|
||||
</TreeView.ItemContainerStyle>-->
|
||||
</TreeView>
|
||||
</Grid>
|
||||
</UserControl>
|
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace QuickLook.Plugin.ArchiveViewer
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for ArchiveFileListView.xaml
|
||||
/// </summary>
|
||||
public partial class ArchiveFileListView : UserControl, IDisposable
|
||||
{
|
||||
public ArchiveFileListView()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
IconManager.ClearCache();
|
||||
}
|
||||
|
||||
public void SetDataContext(object context)
|
||||
{
|
||||
treeGrid.DataContext = context;
|
||||
|
||||
treeView.Loaded += (sender, e) =>
|
||||
{
|
||||
// return when empty
|
||||
if (treeView.Items.Count == 0)
|
||||
return;
|
||||
|
||||
// return when there are more than one root nodes
|
||||
if (treeView.Items.Count > 1)
|
||||
return;
|
||||
|
||||
var root = (TreeViewItem) treeView.ItemContainerGenerator.ContainerFromItem(treeView.Items[0]);
|
||||
if (root == null)
|
||||
return;
|
||||
|
||||
root.IsExpanded = true;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
<UserControl x:Class="QuickLook.Plugin.ArchiveViewer.ArchiveInfoPanel"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:QuickLook.Plugin.ArchiveViewer"
|
||||
mc:Ignorable="d"
|
||||
x:Name="infoPanel"
|
||||
d:DesignHeight="600" d:DesignWidth="800">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition />
|
||||
<RowDefinition Height="30" />
|
||||
</Grid.RowDefinitions>
|
||||
<local:ArchiveFileListView Grid.Row="0" x:Name="fileListView" Focusable="False" />
|
||||
<Grid Grid.Row="1">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="40*" />
|
||||
<ColumnDefinition Width="30*" />
|
||||
<ColumnDefinition Width="30*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Label x:Name="archiveCount" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center">9 folders and 10 files, solid, password-protected</Label>
|
||||
<Label x:Name="archiveSizeC" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center">Compressed size 9999 bytes</Label>
|
||||
<Label x:Name="archiveSizeU" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center">Uncompressed size 99999 bytes</Label>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</UserControl>
|
@@ -0,0 +1,126 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Windows.Controls;
|
||||
using SevenZip;
|
||||
|
||||
namespace QuickLook.Plugin.ArchiveViewer
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for ArchiveInfoPanel.xaml
|
||||
/// </summary>
|
||||
public partial class ArchiveInfoPanel : UserControl, IDisposable
|
||||
{
|
||||
private readonly Dictionary<string, ArchiveFileEntry> _fileEntries = new Dictionary<string, ArchiveFileEntry>();
|
||||
private bool _solid;
|
||||
private ulong _totalZippedSize;
|
||||
private string _type;
|
||||
|
||||
public ArchiveInfoPanel(string path)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
LoadArchive(path);
|
||||
|
||||
fileListView.SetDataContext(_fileEntries[""].Children.Keys);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
fileListView.Dispose();
|
||||
}
|
||||
|
||||
private void LoadArchive(string path)
|
||||
{
|
||||
LoadItemsFromArchive(path);
|
||||
|
||||
var folder = 0;
|
||||
var files = 0;
|
||||
ulong sizeU = 0L;
|
||||
_fileEntries.ForEach(e =>
|
||||
{
|
||||
if (e.Value.IsFolder)
|
||||
folder++;
|
||||
else
|
||||
files++;
|
||||
|
||||
sizeU += e.Value.Size;
|
||||
});
|
||||
|
||||
var s = _solid ? "solid" : "not solid";
|
||||
|
||||
archiveCount.Content =
|
||||
$"{_type} archive, {s}, {folder - 1} folders and {files} files"; // do not count root node
|
||||
archiveSizeC.Content = $"Compressed size {_totalZippedSize.ToPrettySize(2)}";
|
||||
archiveSizeU.Content = $"Uncompressed size {sizeU.ToPrettySize(2)}";
|
||||
}
|
||||
|
||||
private void LoadItemsFromArchive(string path)
|
||||
{
|
||||
using (var reader = new SevenZipExtractor(path))
|
||||
{
|
||||
_totalZippedSize = (ulong) reader.PackedSize;
|
||||
_solid = reader.IsSolid;
|
||||
_type = reader.Format.ToString();
|
||||
|
||||
var root = new ArchiveFileEntry(Path.GetFileName(path), true);
|
||||
_fileEntries.Add("", root);
|
||||
|
||||
foreach (var entry in reader.ArchiveFileData)
|
||||
ProcessByLevel(entry);
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessByLevel(ArchiveFileInfo entry)
|
||||
{
|
||||
var pf = GetPathFragments(entry.FileName);
|
||||
|
||||
// process folders. When entry is a directory, all fragments are folders.
|
||||
pf.Take(entry.IsDirectory ? pf.Length : pf.Length - 1)
|
||||
.ForEach(f =>
|
||||
{
|
||||
// skip if current dir is already added
|
||||
if (_fileEntries.ContainsKey(f))
|
||||
return;
|
||||
|
||||
ArchiveFileEntry parent;
|
||||
_fileEntries.TryGetValue(GetDirectoryName(f), out parent);
|
||||
|
||||
var afe = new ArchiveFileEntry(Path.GetFileName(f), true, parent);
|
||||
|
||||
_fileEntries.Add(f, afe);
|
||||
});
|
||||
|
||||
// add the last path fragments, which is a file
|
||||
if (!entry.IsDirectory)
|
||||
{
|
||||
var file = pf.Last();
|
||||
|
||||
ArchiveFileEntry parent;
|
||||
_fileEntries.TryGetValue(GetDirectoryName(file), out parent);
|
||||
|
||||
_fileEntries.Add(file, new ArchiveFileEntry(Path.GetFileName(entry.FileName), false, parent)
|
||||
{
|
||||
Encrypted = entry.Encrypted,
|
||||
Size = entry.Size,
|
||||
ModifiedDate = entry.LastWriteTime
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private string GetDirectoryName(string path)
|
||||
{
|
||||
var d = Path.GetDirectoryName(path);
|
||||
|
||||
return d ?? "";
|
||||
}
|
||||
|
||||
private string[] GetPathFragments(string path)
|
||||
{
|
||||
var frags = path.Split('\\');
|
||||
|
||||
return frags.Select((s, i) => frags.Take(i + 1).Aggregate((a, b) => a + "\\" + b)).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
103
QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Converters.cs
Normal file
103
QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Converters.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace QuickLook.Plugin.ArchiveViewer
|
||||
{
|
||||
public class LevelToIndentConverter : DependencyObject, IMultiValueConverter
|
||||
{
|
||||
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
var level = (int) values[0];
|
||||
var indent = (double) values[1];
|
||||
return indent * level;
|
||||
}
|
||||
|
||||
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public class LevelToBooleanConverter : DependencyObject, IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
var level = (int) value;
|
||||
|
||||
return level < 2;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public class BooleanToAsteriskConverter : DependencyObject, IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
var b = (bool) value;
|
||||
|
||||
return b ? "*" : "";
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public class SizePrettyPrintConverter : DependencyObject, IMultiValueConverter
|
||||
{
|
||||
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
var size = (ulong) values[0];
|
||||
var isFolder = (bool) values[1];
|
||||
|
||||
return isFolder ? "" : size.ToPrettySize(2);
|
||||
}
|
||||
|
||||
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public class DatePrintConverter : DependencyObject, IMultiValueConverter
|
||||
{
|
||||
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
var date = (DateTime) values[0];
|
||||
var isFolder = (bool) values[1];
|
||||
|
||||
return isFolder ? "" : date.ToString(CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public class FileExtToIconConverter : DependencyObject, IMultiValueConverter
|
||||
{
|
||||
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
var name = (string) values[0];
|
||||
var isFolder = (bool) values[1];
|
||||
|
||||
if (isFolder)
|
||||
return IconManager.FindIconForDir(false);
|
||||
|
||||
return IconManager.FindIconForFilename(name, false);
|
||||
}
|
||||
|
||||
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace QuickLook.Plugin.ArchiveViewer
|
||||
{
|
||||
public static class Extensions
|
||||
{
|
||||
public static void ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
|
||||
{
|
||||
foreach (var item in enumeration)
|
||||
action(item);
|
||||
}
|
||||
|
||||
public static T GetDescendantByType<T>(this Visual element) where T : class
|
||||
{
|
||||
if (element == null)
|
||||
return default(T);
|
||||
if (element.GetType() == typeof(T))
|
||||
return element as T;
|
||||
|
||||
T foundElement = null;
|
||||
(element as FrameworkElement)?.ApplyTemplate();
|
||||
|
||||
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
|
||||
{
|
||||
var visual = VisualTreeHelper.GetChild(element, i) as Visual;
|
||||
foundElement = visual.GetDescendantByType<T>();
|
||||
if (foundElement != null)
|
||||
break;
|
||||
}
|
||||
return foundElement;
|
||||
}
|
||||
|
||||
public static string ToPrettySize(this ulong value, int decimalPlaces = 0)
|
||||
{
|
||||
const long OneKb = 1024;
|
||||
const long OneMb = OneKb * 1024;
|
||||
const long OneGb = OneMb * 1024;
|
||||
const long OneTb = OneGb * 1024;
|
||||
|
||||
var asTb = Math.Round((double) value / OneTb, decimalPlaces);
|
||||
var asGb = Math.Round((double) value / OneGb, decimalPlaces);
|
||||
var asMb = Math.Round((double) value / OneMb, decimalPlaces);
|
||||
var asKb = Math.Round((double) value / OneKb, decimalPlaces);
|
||||
var chosenValue = asTb > 1
|
||||
? $"{asTb} TB"
|
||||
: asGb > 1
|
||||
? $"{asGb} GB"
|
||||
: asMb > 1
|
||||
? $"{asMb} MB"
|
||||
: asKb > 1
|
||||
? $"{asKb} KB"
|
||||
: $"{Math.Round((double) value, decimalPlaces)} bytes";
|
||||
|
||||
return chosenValue;
|
||||
}
|
||||
}
|
||||
}
|
217
QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/IconManager.cs
Normal file
217
QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/IconManager.cs
Normal file
@@ -0,0 +1,217 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace QuickLook.Plugin.ArchiveViewer
|
||||
{
|
||||
/// <summary>
|
||||
/// Internals are mostly from here:
|
||||
/// http://www.codeproject.com/Articles/2532/Obtaining-and-managing-file-and-folder-icons-using
|
||||
/// Caches all results.
|
||||
/// </summary>
|
||||
public static class IconManager
|
||||
{
|
||||
private static ImageSource SmallDirIcon;
|
||||
private static ImageSource LargeDirIcon;
|
||||
private static readonly Dictionary<string, ImageSource> SmallIconCache = new Dictionary<string, ImageSource>();
|
||||
private static readonly Dictionary<string, ImageSource> LargeIconCache = new Dictionary<string, ImageSource>();
|
||||
|
||||
public static void ClearCache()
|
||||
{
|
||||
SmallDirIcon = LargeDirIcon = null;
|
||||
|
||||
SmallIconCache.Clear();
|
||||
LargeIconCache.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the icon of a directory
|
||||
/// </summary>
|
||||
/// <param name="large">16x16 or 32x32 icon</param>
|
||||
/// <returns>an icon</returns>
|
||||
public static ImageSource FindIconForDir(bool large)
|
||||
{
|
||||
var icon = large ? LargeDirIcon : SmallDirIcon;
|
||||
if (icon != null)
|
||||
return icon;
|
||||
icon = IconReader.GetFolderIcon(large ? IconReader.IconSize.Large : IconReader.IconSize.Small,
|
||||
false)
|
||||
.ToImageSource();
|
||||
if (large)
|
||||
LargeDirIcon = icon;
|
||||
else
|
||||
SmallDirIcon = icon;
|
||||
return icon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an icon for a given filename
|
||||
/// </summary>
|
||||
/// <param name="fileName">any filename</param>
|
||||
/// <param name="large">16x16 or 32x32 icon</param>
|
||||
/// <returns>null if path is null, otherwise - an icon</returns>
|
||||
public static ImageSource FindIconForFilename(string fileName, bool large)
|
||||
{
|
||||
var extension = Path.GetExtension(fileName);
|
||||
if (extension == null)
|
||||
return null;
|
||||
var cache = large ? LargeIconCache : SmallIconCache;
|
||||
ImageSource icon;
|
||||
if (cache.TryGetValue(extension, out icon))
|
||||
return icon;
|
||||
icon = IconReader.GetFileIcon(fileName, large ? IconReader.IconSize.Large : IconReader.IconSize.Small,
|
||||
false)
|
||||
.ToImageSource();
|
||||
cache.Add(extension, icon);
|
||||
return icon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// http://stackoverflow.com/a/6580799/1943849
|
||||
/// </summary>
|
||||
private static ImageSource ToImageSource(this Icon icon)
|
||||
{
|
||||
var imageSource = Imaging.CreateBitmapSourceFromHIcon(
|
||||
icon.Handle,
|
||||
Int32Rect.Empty,
|
||||
BitmapSizeOptions.FromEmptyOptions());
|
||||
return imageSource;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides static methods to read system icons for both folders and files.
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// <code>IconReader.GetFileIcon("c:\\general.xls");</code>
|
||||
/// </example>
|
||||
private static class IconReader
|
||||
{
|
||||
/// <summary>
|
||||
/// Options to specify the size of icons to return.
|
||||
/// </summary>
|
||||
public enum IconSize
|
||||
{
|
||||
/// <summary>
|
||||
/// Specify large icon - 32 pixels by 32 pixels.
|
||||
/// </summary>
|
||||
Large = 0,
|
||||
/// <summary>
|
||||
/// Specify small icon - 16 pixels by 16 pixels.
|
||||
/// </summary>
|
||||
Small = 1
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the icon of a folder.
|
||||
/// </summary>
|
||||
/// <param name="size">Large or small</param>
|
||||
/// <param name="linkOverlay">Whether to include the link icon</param>
|
||||
/// <returns>System.Drawing.Icon</returns>
|
||||
public static Icon GetFolderIcon(IconSize size, bool linkOverlay)
|
||||
{
|
||||
var shfi = new Shell32.Shfileinfo();
|
||||
var flags = Shell32.ShgfiIcon | Shell32.ShgfiUsefileattributes;
|
||||
if (linkOverlay) flags += Shell32.ShgfiLinkoverlay;
|
||||
/* Check the size specified for return. */
|
||||
if (IconSize.Small == size)
|
||||
flags += Shell32.ShgfiSmallicon;
|
||||
else
|
||||
flags += Shell32.ShgfiLargeicon;
|
||||
Shell32.SHGetFileInfo("placeholder",
|
||||
Shell32.FileAttributeDirectory,
|
||||
ref shfi,
|
||||
(uint) Marshal.SizeOf(shfi),
|
||||
flags);
|
||||
// Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
|
||||
var icon = (Icon) Icon.FromHandle(shfi.hIcon).Clone();
|
||||
User32.DestroyIcon(shfi.hIcon); // Cleanup
|
||||
return icon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an icon for a given file - indicated by the name parameter.
|
||||
/// </summary>
|
||||
/// <param name="name">Pathname for file.</param>
|
||||
/// <param name="size">Large or small</param>
|
||||
/// <param name="linkOverlay">Whether to include the link icon</param>
|
||||
/// <returns>System.Drawing.Icon</returns>
|
||||
public static Icon GetFileIcon(string name, IconSize size, bool linkOverlay)
|
||||
{
|
||||
var shfi = new Shell32.Shfileinfo();
|
||||
var flags = Shell32.ShgfiIcon | Shell32.ShgfiUsefileattributes;
|
||||
if (linkOverlay) flags += Shell32.ShgfiLinkoverlay;
|
||||
/* Check the size specified for return. */
|
||||
if (IconSize.Small == size)
|
||||
flags += Shell32.ShgfiSmallicon;
|
||||
else
|
||||
flags += Shell32.ShgfiLargeicon;
|
||||
Shell32.SHGetFileInfo(name,
|
||||
Shell32.FileAttributeNormal,
|
||||
ref shfi,
|
||||
(uint) Marshal.SizeOf(shfi),
|
||||
flags);
|
||||
// Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
|
||||
var icon = (Icon) Icon.FromHandle(shfi.hIcon).Clone();
|
||||
User32.DestroyIcon(shfi.hIcon); // Cleanup
|
||||
return icon;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps necessary Shell32.dll structures and functions required to retrieve Icon Handles using SHGetFileInfo. Code
|
||||
/// courtesy of MSDN Cold Rooster Consulting case study.
|
||||
/// </summary>
|
||||
private static class Shell32
|
||||
{
|
||||
private const int MaxPath = 256;
|
||||
public const uint ShgfiIcon = 0x000000100; // get icon
|
||||
public const uint ShgfiLinkoverlay = 0x000008000; // put a link overlay on icon
|
||||
public const uint ShgfiLargeicon = 0x000000000; // get large icon
|
||||
public const uint ShgfiSmallicon = 0x000000001; // get small icon
|
||||
public const uint ShgfiUsefileattributes = 0x000000010; // use passed dwFileAttribute
|
||||
public const uint FileAttributeNormal = 0x00000080;
|
||||
public const uint FileAttributeDirectory = 0x00000010;
|
||||
|
||||
[DllImport("Shell32.dll")]
|
||||
public static extern IntPtr SHGetFileInfo(
|
||||
string pszPath,
|
||||
uint dwFileAttributes,
|
||||
ref Shfileinfo psfi,
|
||||
uint cbFileInfo,
|
||||
uint uFlags
|
||||
);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Shfileinfo
|
||||
{
|
||||
private const int Namesize = 80;
|
||||
public readonly IntPtr hIcon;
|
||||
private readonly int iIcon;
|
||||
private readonly uint dwAttributes;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MaxPath)] private readonly string szDisplayName;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = Namesize)] private readonly string szTypeName;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps necessary functions imported from User32.dll. Code courtesy of MSDN Cold Rooster Consulting example.
|
||||
/// </summary>
|
||||
private static class User32
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides access to function required to delete handle. This method is used internally
|
||||
/// and is not required to be called separately.
|
||||
/// </summary>
|
||||
/// <param name="hIcon">Pointer to icon handle.</param>
|
||||
/// <returns>N/A</returns>
|
||||
[DllImport("User32.dll")]
|
||||
public static extern int DestroyIcon(IntPtr hIcon);
|
||||
}
|
||||
}
|
||||
}
|
71
QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Plugin.cs
Normal file
71
QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Plugin.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using SevenZip;
|
||||
|
||||
namespace QuickLook.Plugin.ArchiveViewer
|
||||
{
|
||||
public class Plugin : IViewer
|
||||
{
|
||||
private ArchiveInfoPanel _panel;
|
||||
|
||||
public int Priority => 0;
|
||||
|
||||
public bool CanHandle(string path)
|
||||
{
|
||||
if (Directory.Exists(path))
|
||||
return false;
|
||||
|
||||
SevenZipExtractor archive = null;
|
||||
try
|
||||
{
|
||||
using (archive = new SevenZipExtractor(path))
|
||||
{
|
||||
// dummy access to the data. If it throws exception, return false
|
||||
if (archive.ArchiveFileData == null)
|
||||
return false;
|
||||
|
||||
// ignore some formats
|
||||
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;
|
||||
}
|
||||
finally
|
||||
{
|
||||
archive?.Dispose();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Prepare(string path, ViewContentContainer container)
|
||||
{
|
||||
container.PreferedSize = new Size {Width = 800, Height = 600};
|
||||
}
|
||||
|
||||
public void View(string path, ViewContentContainer container)
|
||||
{
|
||||
_panel = new ArchiveInfoPanel(path);
|
||||
|
||||
container.SetContent(_panel);
|
||||
container.Title = $"{Path.GetFileName(path)}";
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
_panel.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
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.ArchiveViewer")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("QuickLook.Plugin.ArchiveViewer")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2017")]
|
||||
[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("de2e3bc5-6ab2-4420-a160-48c7a7506c1c")]
|
||||
|
||||
// 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.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
@@ -0,0 +1,84 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{DE2E3BC5-6AB2-4420-A160-48C7A7506C1C}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>QuickLook.Plugin.ArchiveViewer</RootNamespace>
|
||||
<AssemblyName>QuickLook.Plugin.ArchiveViewer</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>..\..\Build\Debug\Plugins\QuickLook.Plugin.ArchiveViewer\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>..\..\Build\Release\Plugins\QuickLook.Plugin.ArchiveViewer\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="PresentationCore" />
|
||||
<Reference Include="PresentationFramework" />
|
||||
<Reference Include="SevenZipSharp">
|
||||
<HintPath>References\SevenZipSharp.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Xaml" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="WindowsBase" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ArchiveInfoPanel.xaml.cs">
|
||||
<DependentUpon>ArchiveInfoPanel.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="ArchiveFileEntry.cs" />
|
||||
<Compile Include="IconManager.cs" />
|
||||
<Compile Include="Extensions.cs" />
|
||||
<Compile Include="Converters.cs" />
|
||||
<Compile Include="Plugin.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ArchiveFileListView.xaml.cs">
|
||||
<DependentUpon>ArchiveFileListView.xaml</DependentUpon>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\QuickLook\QuickLook.csproj">
|
||||
<Project>{8b4a9ce5-67b5-4a94-81cb-3771f688fdeb}</Project>
|
||||
<Name>QuickLook</Name>
|
||||
<Private>False</Private>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Page Include="ArchiveInfoPanel.xaml">
|
||||
<SubType>Designer</SubType>
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</Page>
|
||||
<Page Include="ArchiveFileListView.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
</Page>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user