summaryrefslogtreecommitdiff
path: root/SparkleShare/Windows/UserInterface/ProtocolHandler.cs
blob: b08bd7e0027790374b11b9e48e1d42f215f7a7ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//   SparkleShare, a collaboration and sharing tool.
//   Copyright (C) 2010  Hylke Bons <hi@planetpeanut.uk>
//
//   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 Microsoft.Win32;
using System.IO;

namespace SparkleShare
{

    /// <summary>
    /// Maintain Protocol Handlers created by SparkleShare
    /// </summary>
    static class SparkleProtocolHandler
    {

        /// <summary>
        /// Add or Update protocol handler
        /// </summary>
        /// <param name="handleName">The name of the handler to add</param>
        /// <param name="handleValue">Default value of the protocol handler</param>
        /// <param name="handleCommand">The arguments passed to the Invite Opener</param>
        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);
                    }
                }
            }
        }

        /// <summary>
        /// Remove protocol handler
        /// </summary>
        /// <param name="handleName">The name of the handler to remove</param>
        /// <param name="handleValue">Default value of the protocol handler</param>
        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);
        }
    }
}