[WIP] Associate .wpost files with OLW (#592)

* Add file associations

* Updated assemblyinfo

* Update package manifest
This commit is contained in:
Scott Lovegrove 2019-01-19 10:15:25 +00:00 committed by Jon Galloway
parent 6e2d8dc89a
commit 82b0be177a
3 changed files with 110 additions and 2 deletions

View File

@ -1,5 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:uap2="http://schemas.microsoft.com/appx/manifest/uap/windows10/2" xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10">
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:uap2="http://schemas.microsoft.com/appx/manifest/uap/windows10/2"
xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
IgnorableNamespaces="uap uap2 uap3 mp rescap desktop">
<Identity Name="D5BA6BCD.OpenLiveWriter" ProcessorArchitecture="x86" Publisher="CN=59FB1262-434E-4693-9A1E-CD275B151706" Version="0.6.1.0" />
<Properties>
<DisplayName>Open Live Writer</DisplayName>
@ -24,7 +30,18 @@
</uap:ShowNameOnTiles>
</uap:DefaultTile>
</uap:VisualElements>
<Extensions />
<Extensions>
<uap3:Extension Category="windows.fileTypeAssociation">
<uap3:FileTypeAssociation Name="openLiveWriter" Parameters="&quot;%1&quot;">
<uap:SupportedFileTypes>
<uap:FileType>.wpost</uap:FileType>
</uap:SupportedFileTypes>
<uap2:SupportedVerbs>
<uap3:Verb Id="Edit" Parameters="&quot;%1&quot;">Edit</uap3:Verb>
</uap2:SupportedVerbs>
</uap3:FileTypeAssociation>
</uap3:Extension>
</Extensions>
</Application>
</Applications>
</Package>

View File

@ -6,3 +6,4 @@ using System.Reflection;
[assembly: AssemblyCompany("Open Live Writer")]
[assembly: AssemblyCopyright("Copyright (c) .NET Foundation. All rights reserved.")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyMetadata("SquirrelAwareVersion", "1")]

View File

@ -5,6 +5,7 @@ using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Windows.Forms;
using Microsoft.Win32;
@ -16,6 +17,8 @@ using OpenLiveWriter.Interop.Windows;
using OpenLiveWriter.Localization;
using OpenLiveWriter.PostEditor;
using OpenLiveWriter.PostEditor.JumpList;
using OpenLiveWriter.PostEditor.Updates;
using Squirrel;
namespace OpenLiveWriter
{
@ -137,6 +140,9 @@ namespace OpenLiveWriter
InitializeApplicationEnvironment();
string downloadUrl = UpdateSettings.CheckForBetaUpdates ? UpdateSettings.BetaUpdateDownloadUrl : UpdateSettings.UpdateDownloadUrl;
RegisterSquirrelEventHandlers(downloadUrl);
try
{
// TODO:OLW
@ -190,6 +196,90 @@ namespace OpenLiveWriter
}
}
/// <summary>
/// Registers functions to handle Squirrel's events.
/// </summary>
/// <param name="downloadUrl">The Url to use for downloading payloads.</param>
private static void RegisterSquirrelEventHandlers(string downloadUrl)
{
try
{
using (var mgr = new Squirrel.UpdateManager(downloadUrl))
{
SquirrelAwareApp.HandleEvents(
onInitialInstall: v => InitialInstall(mgr),
onFirstRun: () => FirstRun(mgr),
onAppUpdate: v => OnAppUpdate(mgr),
onAppUninstall: v => OnAppUninstall(mgr));
}
}
catch (Exception)
{
// Ignore, chances are this is being run locally on a dev's machine
}
}
/// <summary>
/// Removes registry keys under HKCU/SOFTWARE/OpenLiveWriter and deletes the AppData and Roaming profiles.
/// </summary>
/// <param name="mgr">An instance of Squirrel.UpdateManager to be used as helper class.</param>
private static async void OnAppUninstall(IUpdateManager mgr)
{
await mgr.FullUninstall();
string OLWRegKey = @"SOFTWARE\OpenLiveWriter";
Registry.CurrentUser.DeleteSubKeyTree(OLWRegKey);
mgr.RemoveShortcutForThisExe();
mgr.RemoveUninstallerRegistryEntry();
Directory.Delete(ApplicationEnvironment.LocalApplicationDataDirectory, true);
Directory.Delete(ApplicationEnvironment.ApplicationDataDirectory, true);
}
private static async void OnAppUpdate(IUpdateManager mgr)
{
await mgr.UpdateApp();
}
private static void FirstRun(IUpdateManager mgr)
{
mgr.CreateShortcutForThisExe();
}
private static async void InitialInstall(Squirrel.UpdateManager mgr)
{
mgr.CreateShortcutForThisExe();
await mgr.CreateUninstallerRegistryEntry();
await mgr.FullInstall();
SetAssociation(".wpost", "OPEN_LIVE_WRITER", Application.ExecutablePath, "Open Live Writer post");
}
public static void SetAssociation(string extension, string keyName, string openWith, string fileDescription)
{
var baseKey = Registry.ClassesRoot.CreateSubKey(extension);
baseKey?.SetValue("", keyName);
var openMethod = Registry.ClassesRoot.CreateSubKey(keyName);
openMethod?.SetValue("", fileDescription);
openMethod?.CreateSubKey("DefaultIcon")?.SetValue("", "\"" + openWith + "\",0");
var shell = openMethod?.CreateSubKey("Shell");
shell?.CreateSubKey("edit")?.CreateSubKey("command")?.SetValue("", "\"" + openWith + "\"" + " \"%1\"");
shell?.CreateSubKey("open")?.CreateSubKey("command")?.SetValue("", "\"" + openWith + "\"" + " \"%1\"");
baseKey?.Close();
openMethod?.Close();
shell?.Close();
// Delete the key instead of trying to change it
var currentUser = Registry.CurrentUser.OpenSubKey($"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\{extension}", true);
currentUser?.DeleteSubKey("UserChoice", false);
currentUser?.Close();
// Tell explorer the file association has been changed
SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
}
[DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
private static void InitializeApplicationEnvironment()
{
// initialize application environment