From 3d4a2bea11e91590070fea070bfc963730089953 Mon Sep 17 00:00:00 2001 From: Jonathan Haines Date: Mon, 8 May 2017 20:04:27 +1000 Subject: [PATCH] windows: add custom protocol handling --- SparkleShare.sln | 15 ++++ .../Windows/SparkleShare.Windows.csproj | 10 +-- .../SparkleShareInviteOpener.csproj | 20 ++++- .../Windows/UserInterface/ProtocolHandler.cs | 74 +++++++++++++++++++ 4 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 SparkleShare/Windows/UserInterface/ProtocolHandler.cs diff --git a/SparkleShare.sln b/SparkleShare.sln index 5c8e45db..c97fdb11 100644 --- a/SparkleShare.sln +++ b/SparkleShare.sln @@ -22,6 +22,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionIt README.md = README.md EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SparkleShareInviteOpener", "SparkleShare\Windows\SparkleShareInviteOpener\SparkleShareInviteOpener.csproj", "{1DB5492D-B897-4A5E-8DD7-175EC65F52F2}" +EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution SparkleShare\Common\SparkleShare.projitems*{728483aa-e34b-4441-bf2c-c8bc2901e4e0}*SharedItemsImports = 4 @@ -100,6 +102,19 @@ Global {5714D3CA-88A6-4330-A29D-4CA90D1D193C}.ReleaseDist|Any CPU.Build.0 = Release|Any CPU {5714D3CA-88A6-4330-A29D-4CA90D1D193C}.ReleaseMac|Any CPU.ActiveCfg = ReleaseMac|Any CPU {5714D3CA-88A6-4330-A29D-4CA90D1D193C}.ReleaseWindows|Any CPU.ActiveCfg = ReleaseMac|Any CPU + {1DB5492D-B897-4A5E-8DD7-175EC65F52F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1DB5492D-B897-4A5E-8DD7-175EC65F52F2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1DB5492D-B897-4A5E-8DD7-175EC65F52F2}.DebugMac|Any CPU.ActiveCfg = Debug|Any CPU + {1DB5492D-B897-4A5E-8DD7-175EC65F52F2}.DebugWindows|Any CPU.ActiveCfg = DebugWindows|Any CPU + {1DB5492D-B897-4A5E-8DD7-175EC65F52F2}.DebugWindows|Any CPU.Build.0 = DebugWindows|Any CPU + {1DB5492D-B897-4A5E-8DD7-175EC65F52F2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1DB5492D-B897-4A5E-8DD7-175EC65F52F2}.Release|Any CPU.Build.0 = Release|Any CPU + {1DB5492D-B897-4A5E-8DD7-175EC65F52F2}.ReleaseDist|Any CPU.ActiveCfg = Release|Any CPU + {1DB5492D-B897-4A5E-8DD7-175EC65F52F2}.ReleaseDist|Any CPU.Build.0 = Release|Any CPU + {1DB5492D-B897-4A5E-8DD7-175EC65F52F2}.ReleaseMac|Any CPU.ActiveCfg = Release|Any CPU + {1DB5492D-B897-4A5E-8DD7-175EC65F52F2}.ReleaseMac|Any CPU.Build.0 = Release|Any CPU + {1DB5492D-B897-4A5E-8DD7-175EC65F52F2}.ReleaseWindows|Any CPU.ActiveCfg = ReleaseWindows|Any CPU + {1DB5492D-B897-4A5E-8DD7-175EC65F52F2}.ReleaseWindows|Any CPU.Build.0 = ReleaseWindows|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SparkleShare/Windows/SparkleShare.Windows.csproj b/SparkleShare/Windows/SparkleShare.Windows.csproj index 94bf0174..a3c32c1d 100644 --- a/SparkleShare/Windows/SparkleShare.Windows.csproj +++ b/SparkleShare/Windows/SparkleShare.Windows.csproj @@ -144,15 +144,6 @@ - - - - - - - - - @@ -161,6 +152,7 @@ Note.xaml + diff --git a/SparkleShare/Windows/SparkleShareInviteOpener/SparkleShareInviteOpener.csproj b/SparkleShare/Windows/SparkleShareInviteOpener/SparkleShareInviteOpener.csproj index c7c97a3f..bbccc792 100644 --- a/SparkleShare/Windows/SparkleShareInviteOpener/SparkleShareInviteOpener.csproj +++ b/SparkleShare/Windows/SparkleShareInviteOpener/SparkleShareInviteOpener.csproj @@ -1,4 +1,4 @@ - + Release @@ -29,6 +29,24 @@ full true + + true + ..\..\..\bin\ + TRACE;DEBUG + full + AnyCPU + prompt + MinimumRecommendedRules.ruleset + + + ..\..\..\bin\ + TRACE + true + pdbonly + AnyCPU + prompt + MinimumRecommendedRules.ruleset + diff --git a/SparkleShare/Windows/UserInterface/ProtocolHandler.cs b/SparkleShare/Windows/UserInterface/ProtocolHandler.cs new file mode 100644 index 00000000..79c6c275 --- /dev/null +++ b/SparkleShare/Windows/UserInterface/ProtocolHandler.cs @@ -0,0 +1,74 @@ +// SparkleShare, a collaboration and sharing tool. +// Copyright (C) 2010 Hylke Bons +// +// 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 . + + +using Microsoft.Win32; +using System.IO; + +namespace SparkleShare +{ + + /// + /// Maintain Protocol Handlers created by SparkleShare + /// + static class SparkleProtocolHandler + { + + /// + /// Add or Update protocol handler + /// + /// The name of the handler to add + /// Default value of the protocol handler + /// The arguments passed to the Invite Opener + internal static void AddProtocolHandler(string handleName, string handleValue, string handleCommand) + { + var inviteOpener = Path.Combine(Directory.GetCurrentDirectory(), "SparkleShareInviteOpener"); + + // test the handleName for third party protocols like GitHub + // if one exist and their default value doesn't match our custom Protocol, do not update + using (RegistryKey testKey = Registry.ClassesRoot.OpenSubKey(handleName)) + { + if (testKey == null || handleValue.Equals(testKey.GetValue(""))) + { + using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software").CreateSubKey("Classes").CreateSubKey(handleName)) + { + key.SetValue("", handleValue); + key.SetValue("URL Protocol", ""); + key.CreateSubKey("DefaultIcon").SetValue("", inviteOpener); + key.CreateSubKey("shell") + .CreateSubKey("open") + .CreateSubKey("command") + .SetValue("", inviteOpener + " " + handleCommand); + } + } + } + } + + /// + /// Remove protocol handler + /// + /// The name of the handler to remove + /// Default value of the protocol handler + internal static void RemoveProtocolHandler(string handleName, string handleValue) + { + var key = Registry.CurrentUser.OpenSubKey(handleName); + + // if the the default value doesn't match our custom Protocol, do not remove + if (key != null && handleValue.Equals(key.GetValue(""))) + Registry.CurrentUser.DeleteSubKeyTree(handleName); + } + } +}