done installer

This commit is contained in:
Paddy Xu
2018-09-01 21:29:15 +03:00
parent 4f496f5ab8
commit 5cd9018ce4
6 changed files with 129 additions and 16 deletions

View File

@@ -16,6 +16,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
using System; using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -63,6 +64,8 @@ namespace QuickLook.Common.Helpers
public static void WriteLog(string msg) public static void WriteLog(string msg)
{ {
Debug.WriteLine(msg);
var logFilePath = Path.Combine(SettingHelper.LocalDataPath, @"QuickLook.Exception.log"); var logFilePath = Path.Combine(SettingHelper.LocalDataPath, @"QuickLook.Exception.log");
using (var writer = new StreamWriter(new FileStream(logFilePath, FileMode.OpenOrCreate, using (var writer = new StreamWriter(new FileStream(logFilePath, FileMode.OpenOrCreate,

View File

@@ -33,12 +33,12 @@ namespace QuickLook.Plugin.PluginInstaller
public bool CanHandle(string path) public bool CanHandle(string path)
{ {
return !Directory.Exists(path) && path.ToLower().EndsWith(".qlviewer"); return !Directory.Exists(path) && path.ToLower().EndsWith(".qlplugin");
} }
public void Prepare(string path, ContextObject context) public void Prepare(string path, ContextObject context)
{ {
context.PreferredSize = new Size { Width = 400, Height = 172 }; context.PreferredSize = new Size { Width = 460, Height = 200 };
context.Title = ""; context.Title = "";
context.TitlebarOverlap = false; context.TitlebarOverlap = false;
@@ -50,7 +50,7 @@ namespace QuickLook.Plugin.PluginInstaller
public void View(string path, ContextObject context) public void View(string path, ContextObject context)
{ {
context.ViewerContent = new PluginInfoPanel(path); context.ViewerContent = new PluginInfoPanel(path, context);
context.IsBusy = false; context.IsBusy = false;
} }

View File

@@ -4,7 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
FontSize="14" FontSize="14"
mc:Ignorable="d" Width="400" Height="172" UseLayoutRounding="True"> mc:Ignorable="d" Width="460" Height="200">
<UserControl.Resources> <UserControl.Resources>
<ResourceDictionary> <ResourceDictionary>
<ResourceDictionary.MergedDictionaries> <ResourceDictionary.MergedDictionaries>

View File

@@ -16,19 +16,24 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
using System; using System;
using System.Globalization;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Xml; 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 readonly string _path;
private string _namespace;
public PluginInfoPanel(string path) public PluginInfoPanel(string path, ContextObject context)
{ {
InitializeComponent(); InitializeComponent();
@@ -36,19 +41,87 @@ namespace QuickLook.Plugin.PluginInstaller
Resources.MergedDictionaries[0].Clear(); Resources.MergedDictionaries[0].Clear();
_path = path; _path = path;
_context = context;
ReadInfo(); 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(() =>
{
CleanUp();
try
{
ZipFile.ExtractToDirectory(_path, targetFolder);
}
catch (Exception ex)
{
Dispatcher.BeginInvoke(new Action(() => description.Text = ex.Message));
CleanUp();
}
});
void CleanUp()
{
if (!Directory.Exists(targetFolder))
{
Directory.CreateDirectory(targetFolder);
return;
}
try
{
Directory.GetFiles(targetFolder, "*", SearchOption.AllDirectories)
.ForEach(file => File.Move(file, new Guid() + ".to_be_deleted"));
}
catch (Exception ex)
{
Dispatcher.BeginInvoke(new Action(() => description.Text = ex.Message));
}
}
}
private void ReadInfo() private void ReadInfo()
{ {
filename.Text = Path.GetFileNameWithoutExtension(_path); filename.Text = Path.GetFileNameWithoutExtension(_path);
var xml = LoadXml(GetFileFromZip(_path, "Metadata.config")); 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.";
description.Text = GetString(xml, @"/Metadata/Description", string.Empty);
btnInstall.Visibility = okay ? Visibility.Visible : Visibility.Collapsed;
} }
private XmlDocument LoadXml(Stream data) private static string GetString(XmlNode xml, string xpath, string def = null)
{ {
var n = xml?.SelectSingleNode(xpath);
return n?.InnerText;
}
private static XmlDocument LoadXml(Stream data)
{
var doc = new XmlDocument(); var doc = new XmlDocument();
try try
{ {
@@ -61,18 +134,26 @@ namespace QuickLook.Plugin.PluginInstaller
} }
} }
private Stream GetFileFromZip(string archive, string entry) private static MemoryStream GetFileFromZip(string archive, string entry)
{ {
var ms = new MemoryStream(); var ms = new MemoryStream();
using (var zip = ZipFile.Open(archive, ZipArchiveMode.Read)) try
{ {
using (var s = zip.GetEntry(entry)?.Open()) using (var zip = ZipFile.Open(archive, ZipArchiveMode.Read))
{ {
s?.CopyTo(ms); using (var s = zip?.GetEntry(entry)?.Open())
{
s?.CopyTo(ms);
}
} }
} }
catch (Exception)
{
return ms;
}
ms.Position = 0;
return ms; return ms;
} }
} }

View File

@@ -34,6 +34,7 @@ namespace QuickLook
/// </summary> /// </summary>
public partial class App : Application public partial class App : Application
{ {
public static readonly string UserPluginPath = Path.Combine(SettingHelper.LocalDataPath, "QuickLook.Plugin\\");
public static readonly string AppFullPath = Assembly.GetExecutingAssembly().Location; public static readonly string AppFullPath = Assembly.GetExecutingAssembly().Location;
public static readonly string AppPath = Path.GetDirectoryName(AppFullPath); public static readonly string AppPath = Path.GetDirectoryName(AppFullPath);
public static readonly bool Is64Bit = Environment.Is64BitProcess; public static readonly bool Is64Bit = Environment.Is64BitProcess;

View File

@@ -22,6 +22,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using QuickLook.Common.ExtensionMethods; using QuickLook.Common.ExtensionMethods;
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin; using QuickLook.Common.Plugin;
namespace QuickLook namespace QuickLook
@@ -32,7 +33,10 @@ namespace QuickLook
private PluginManager() private PluginManager()
{ {
LoadPlugins(); CleanupOldPlugins(App.UserPluginPath);
LoadPlugins(App.UserPluginPath);
LoadPlugins(Path.Combine(App.AppPath, "QuickLook.Plugin\\"));
InitLoadedPlugins();
} }
internal IViewer DefaultPlugin { get; } = new Plugin.InfoPanel.Plugin(); internal IViewer DefaultPlugin { get; } = new Plugin.InfoPanel.Plugin();
@@ -74,9 +78,12 @@ namespace QuickLook
return (matched ?? DefaultPlugin).GetType().CreateInstance<IViewer>(); return (matched ?? DefaultPlugin).GetType().CreateInstance<IViewer>();
} }
private void LoadPlugins() private void LoadPlugins(string folder)
{ {
Directory.GetFiles(Path.Combine(App.AppPath, "QuickLook.Plugin\\"), "QuickLook.Plugin.*.dll", if (!Directory.Exists(folder))
return;
Directory.GetFiles(folder, "QuickLook.Plugin.*.dll",
SearchOption.AllDirectories) SearchOption.AllDirectories)
.ToList() .ToList()
.ForEach( .ForEach(
@@ -90,7 +97,10 @@ namespace QuickLook
}); });
LoadedPlugins = LoadedPlugins.OrderByDescending(i => i.Priority).ToList(); LoadedPlugins = LoadedPlugins.OrderByDescending(i => i.Priority).ToList();
}
private void InitLoadedPlugins()
{
LoadedPlugins.ForEach(i => LoadedPlugins.ForEach(i =>
{ {
try try
@@ -99,7 +109,25 @@ namespace QuickLook
} }
catch (Exception e) catch (Exception e)
{ {
Debug.WriteLine(e); ProcessHelper.WriteLog(e.ToString());
}
});
}
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
} }
}); });
} }