mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-14 12:19:08 +00:00
Code Cleanup
This commit is contained in:
@@ -1,82 +1,80 @@
|
||||
// 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using QuickLook.Common.ExtensionMethods;
|
||||
using QuickLook.Common.Plugin;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
|
||||
namespace QuickLook.Plugin.PluginInstaller
|
||||
namespace QuickLook.Plugin.PluginInstaller;
|
||||
|
||||
public class Plugin : IViewer
|
||||
{
|
||||
public class Plugin : IViewer
|
||||
public int Priority => int.MaxValue;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
public int Priority => int.MaxValue;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
CleanupOldPlugins(App.UserPluginPath);
|
||||
}
|
||||
|
||||
public bool CanHandle(string path)
|
||||
{
|
||||
return !Directory.Exists(path) && path.ToLower().EndsWith(".qlplugin");
|
||||
}
|
||||
|
||||
public void Prepare(string path, ContextObject context)
|
||||
{
|
||||
context.PreferredSize = new Size { Width = 460, Height = 200 };
|
||||
|
||||
context.Title = "";
|
||||
context.TitlebarOverlap = false;
|
||||
context.TitlebarBlurVisibility = false;
|
||||
context.TitlebarColourVisibility = false;
|
||||
context.CanResize = false;
|
||||
context.FullWindowDragging = true;
|
||||
}
|
||||
|
||||
public void View(string path, ContextObject context)
|
||||
{
|
||||
context.ViewerContent = new PluginInfoPanel(path, context);
|
||||
|
||||
context.IsBusy = false;
|
||||
}
|
||||
|
||||
public void Cleanup()
|
||||
{
|
||||
}
|
||||
|
||||
private static void CleanupOldPlugins(string folder)
|
||||
{
|
||||
if (!Directory.Exists(folder))
|
||||
return;
|
||||
|
||||
Directory.GetFiles(folder, "*.to_be_deleted", SearchOption.AllDirectories).ForEach(file =>
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(file);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
});
|
||||
}
|
||||
CleanupOldPlugins(App.UserPluginPath);
|
||||
}
|
||||
}
|
||||
|
||||
public bool CanHandle(string path)
|
||||
{
|
||||
return !Directory.Exists(path) && path.ToLower().EndsWith(".qlplugin");
|
||||
}
|
||||
|
||||
public void Prepare(string path, ContextObject context)
|
||||
{
|
||||
context.PreferredSize = new Size { Width = 460, Height = 200 };
|
||||
|
||||
context.Title = string.Empty;
|
||||
context.TitlebarOverlap = false;
|
||||
context.TitlebarBlurVisibility = false;
|
||||
context.TitlebarColourVisibility = false;
|
||||
context.CanResize = false;
|
||||
context.FullWindowDragging = true;
|
||||
}
|
||||
|
||||
public void View(string path, ContextObject context)
|
||||
{
|
||||
context.ViewerContent = new PluginInfoPanel(path, context);
|
||||
|
||||
context.IsBusy = false;
|
||||
}
|
||||
|
||||
public void Cleanup()
|
||||
{
|
||||
}
|
||||
|
||||
private static void CleanupOldPlugins(string folder)
|
||||
{
|
||||
if (!Directory.Exists(folder))
|
||||
return;
|
||||
|
||||
Directory.GetFiles(folder, "*.to_be_deleted", SearchOption.AllDirectories).ForEach(file =>
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(file);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -1,10 +1,12 @@
|
||||
<UserControl x:Class="QuickLook.Plugin.PluginInstaller.PluginInfoPanel"
|
||||
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
Width="460"
|
||||
Height="200"
|
||||
FontSize="14"
|
||||
mc:Ignorable="d" Width="460" Height="200">
|
||||
mc:Ignorable="d">
|
||||
<UserControl.Resources>
|
||||
<ResourceDictionary>
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
@@ -29,23 +31,41 @@
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="20" />
|
||||
</Grid.RowDefinitions>
|
||||
<TextBlock x:Name="filename" Grid.Row="1" Grid.Column="1" FontSize="19" Padding="3"
|
||||
TextWrapping="Wrap"
|
||||
LineHeight="25" MaxHeight="60" TextTrimming="CharacterEllipsis" FontWeight="SemiBold">
|
||||
<TextBlock x:Name="filename"
|
||||
Grid.Row="1"
|
||||
Grid.Column="1"
|
||||
MaxHeight="60"
|
||||
Padding="3"
|
||||
FontSize="19"
|
||||
FontWeight="SemiBold"
|
||||
LineHeight="25"
|
||||
TextTrimming="CharacterEllipsis"
|
||||
TextWrapping="Wrap">
|
||||
QuickLook.Plugin.PluginInstaller
|
||||
</TextBlock>
|
||||
<TextBlock x:Name="version" Grid.Row="3" Grid.Column="1" Padding="3"
|
||||
<TextBlock x:Name="version"
|
||||
Grid.Row="3"
|
||||
Grid.Column="1"
|
||||
Padding="3"
|
||||
Foreground="{DynamicResource WindowTextForegroundAlternative}">
|
||||
Version 0
|
||||
</TextBlock>
|
||||
<TextBlock x:Name="description" Grid.Row="5" Grid.Column="1" Padding="3"
|
||||
<TextBlock x:Name="description"
|
||||
Grid.Row="5"
|
||||
Grid.Column="1"
|
||||
Padding="3"
|
||||
Foreground="{DynamicResource WindowTextForegroundAlternative}">
|
||||
I am a potato.
|
||||
</TextBlock>
|
||||
<Button x:Name="btnInstall" Content="Click here to install this plugin." Grid.Column="1" Grid.Row="6"
|
||||
|
||||
FontWeight="SemiBold" Foreground="{DynamicResource WindowTextForegroundAlternative}"
|
||||
Style="{StaticResource CaptionTextButtonStyle}" HorizontalAlignment="Right" Margin="0,0,20,0" />
|
||||
<Button x:Name="btnInstall"
|
||||
Grid.Row="6"
|
||||
Grid.Column="1"
|
||||
Margin="0,0,20,0"
|
||||
HorizontalAlignment="Right"
|
||||
Content="Click here to install this plugin."
|
||||
FontWeight="SemiBold"
|
||||
Foreground="{DynamicResource WindowTextForegroundAlternative}"
|
||||
Style="{StaticResource CaptionTextButtonStyle}" />
|
||||
</Grid>
|
||||
</Grid>
|
||||
</UserControl>
|
@@ -1,20 +1,22 @@
|
||||
// Copyright © 2017 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
using QuickLook.Common.ExtensionMethods;
|
||||
using QuickLook.Common.Plugin;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
@@ -22,143 +24,139 @@ using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Xml;
|
||||
using QuickLook.Common.ExtensionMethods;
|
||||
using QuickLook.Common.Plugin;
|
||||
|
||||
namespace QuickLook.Plugin.PluginInstaller
|
||||
namespace QuickLook.Plugin.PluginInstaller;
|
||||
|
||||
public partial class PluginInfoPanel : UserControl
|
||||
{
|
||||
public partial class PluginInfoPanel : UserControl
|
||||
private readonly ContextObject _context;
|
||||
private readonly string _path;
|
||||
private string _namespace;
|
||||
|
||||
public PluginInfoPanel(string path, ContextObject context)
|
||||
{
|
||||
private readonly ContextObject _context;
|
||||
private readonly string _path;
|
||||
private string _namespace;
|
||||
InitializeComponent();
|
||||
|
||||
public PluginInfoPanel(string path, ContextObject context)
|
||||
// apply global theme
|
||||
Resources.MergedDictionaries[0].Clear();
|
||||
|
||||
_path = path;
|
||||
_context = context;
|
||||
ReadInfo();
|
||||
|
||||
btnInstall.Click += BtnInstall_Click;
|
||||
}
|
||||
|
||||
private void BtnInstall_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
btnInstall.Content = "Installing ...";
|
||||
btnInstall.IsEnabled = false;
|
||||
|
||||
var t = DoInstall();
|
||||
t.ContinueWith(_ =>
|
||||
Dispatcher.BeginInvoke(new Action(() => btnInstall.Content = "Done! Please restart QuickLook.")));
|
||||
t.Start();
|
||||
}
|
||||
|
||||
private Task DoInstall()
|
||||
{
|
||||
var targetFolder = Path.Combine(App.UserPluginPath, _namespace);
|
||||
return new Task(() =>
|
||||
{
|
||||
InitializeComponent();
|
||||
CleanUp();
|
||||
|
||||
// apply global theme
|
||||
Resources.MergedDictionaries[0].Clear();
|
||||
|
||||
_path = path;
|
||||
_context = context;
|
||||
ReadInfo();
|
||||
|
||||
btnInstall.Click += BtnInstall_Click;
|
||||
}
|
||||
|
||||
private void BtnInstall_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
btnInstall.Content = "Installing ...";
|
||||
btnInstall.IsEnabled = false;
|
||||
|
||||
var t = DoInstall();
|
||||
t.ContinueWith(_ =>
|
||||
Dispatcher.BeginInvoke(new Action(() => btnInstall.Content = "Done! Please restart QuickLook.")));
|
||||
t.Start();
|
||||
}
|
||||
|
||||
private Task DoInstall()
|
||||
{
|
||||
var targetFolder = Path.Combine(App.UserPluginPath, _namespace);
|
||||
return new Task(() =>
|
||||
try
|
||||
{
|
||||
ZipFile.ExtractToDirectory(_path, targetFolder);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Dispatcher.BeginInvoke(new Action(() => description.Text = ex.Message));
|
||||
Dispatcher.BeginInvoke(new Action(() => btnInstall.Content = "Installation failed."));
|
||||
CleanUp();
|
||||
|
||||
try
|
||||
{
|
||||
ZipFile.ExtractToDirectory(_path, targetFolder);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Dispatcher.BeginInvoke(new Action(() => description.Text = ex.Message));
|
||||
Dispatcher.BeginInvoke(new Action(() => btnInstall.Content = "Installation failed."));
|
||||
CleanUp();
|
||||
}
|
||||
});
|
||||
|
||||
void CleanUp()
|
||||
{
|
||||
if (!Directory.Exists(targetFolder))
|
||||
{
|
||||
Directory.CreateDirectory(targetFolder);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Directory.GetFiles(targetFolder, "*", SearchOption.AllDirectories)
|
||||
.ForEach(file => File.Move(file,
|
||||
Path.Combine(Path.GetDirectoryName(file), Guid.NewGuid() + ".to_be_deleted")));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Dispatcher.BeginInvoke(new Action(() => description.Text = ex.Message));
|
||||
Dispatcher.BeginInvoke(new Action(() => btnInstall.Content = "Installation failed."));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
private void ReadInfo()
|
||||
void CleanUp()
|
||||
{
|
||||
filename.Text = Path.GetFileNameWithoutExtension(_path);
|
||||
|
||||
var xml = LoadXml(GetFileFromZip(_path, "QuickLook.Plugin.Metadata.config"));
|
||||
|
||||
_namespace = GetString(xml, @"/Metadata/Namespace");
|
||||
|
||||
var okay = _namespace != null && _namespace.StartsWith("QuickLook.Plugin.");
|
||||
|
||||
filename.Text = okay ? _namespace : "Invalid plugin.";
|
||||
version.Text = "Version " + GetString(xml, @"/Metadata/Version", "not defined");
|
||||
description.Text = GetString(xml, @"/Metadata/Description", string.Empty);
|
||||
|
||||
btnInstall.Visibility = okay ? Visibility.Visible : Visibility.Collapsed;
|
||||
}
|
||||
|
||||
private static string GetString(XmlNode xml, string xpath, string def = null)
|
||||
{
|
||||
var n = xml?.SelectSingleNode(xpath);
|
||||
|
||||
return n?.InnerText ?? def;
|
||||
}
|
||||
|
||||
private static XmlDocument LoadXml(Stream data)
|
||||
{
|
||||
var doc = new XmlDocument();
|
||||
try
|
||||
if (!Directory.Exists(targetFolder))
|
||||
{
|
||||
doc.Load(data);
|
||||
return doc;
|
||||
Directory.CreateDirectory(targetFolder);
|
||||
return;
|
||||
}
|
||||
catch (XmlException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static MemoryStream GetFileFromZip(string archive, string entry)
|
||||
{
|
||||
var ms = new MemoryStream();
|
||||
|
||||
try
|
||||
{
|
||||
using (var zip = ZipFile.Open(archive, ZipArchiveMode.Read))
|
||||
{
|
||||
using (var s = zip?.GetEntry(entry)?.Open())
|
||||
{
|
||||
s?.CopyTo(ms);
|
||||
}
|
||||
}
|
||||
Directory.GetFiles(targetFolder, "*", SearchOption.AllDirectories)
|
||||
.ForEach(file => File.Move(file,
|
||||
Path.Combine(Path.GetDirectoryName(file), Guid.NewGuid() + ".to_be_deleted")));
|
||||
}
|
||||
catch (Exception)
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ms;
|
||||
Dispatcher.BeginInvoke(new Action(() => description.Text = ex.Message));
|
||||
Dispatcher.BeginInvoke(new Action(() => btnInstall.Content = "Installation failed."));
|
||||
}
|
||||
|
||||
ms.Position = 0;
|
||||
return ms;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ReadInfo()
|
||||
{
|
||||
filename.Text = Path.GetFileNameWithoutExtension(_path);
|
||||
|
||||
var xml = LoadXml(GetFileFromZip(_path, "QuickLook.Plugin.Metadata.config"));
|
||||
|
||||
_namespace = GetString(xml, @"/Metadata/Namespace");
|
||||
|
||||
var okay = _namespace != null && _namespace.StartsWith("QuickLook.Plugin.");
|
||||
|
||||
filename.Text = okay ? _namespace : "Invalid plugin.";
|
||||
version.Text = "Version " + GetString(xml, @"/Metadata/Version", "not defined");
|
||||
description.Text = GetString(xml, @"/Metadata/Description", string.Empty);
|
||||
|
||||
btnInstall.Visibility = okay ? Visibility.Visible : Visibility.Collapsed;
|
||||
}
|
||||
|
||||
private static string GetString(XmlNode xml, string xpath, string def = null)
|
||||
{
|
||||
var n = xml?.SelectSingleNode(xpath);
|
||||
|
||||
return n?.InnerText ?? def;
|
||||
}
|
||||
|
||||
private static XmlDocument LoadXml(Stream data)
|
||||
{
|
||||
var doc = new XmlDocument();
|
||||
try
|
||||
{
|
||||
doc.Load(data);
|
||||
return doc;
|
||||
}
|
||||
catch (XmlException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static MemoryStream GetFileFromZip(string archive, string entry)
|
||||
{
|
||||
var ms = new MemoryStream();
|
||||
|
||||
try
|
||||
{
|
||||
using (var zip = ZipFile.Open(archive, ZipArchiveMode.Read))
|
||||
{
|
||||
using (var s = zip?.GetEntry(entry)?.Open())
|
||||
{
|
||||
s?.CopyTo(ms);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return ms;
|
||||
}
|
||||
|
||||
ms.Position = 0;
|
||||
return ms;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user