/// <summary>
        /// Registers a URL protocol in the current system.
        /// </summary>
        /// <param name="target">The application being integrated.</param>
        /// <param name="urlProtocol">The URL protocol to register.</param>
        /// <param name="machineWide">Register the URL protocol machine-wide instead of just for the current user.</param>
        /// <param name="iconStore">Stores icon files downloaded from the web as local files.</param>
        /// <param name="accessPoint">Indicates that the handler shall become the default handler for the protocol.</param>
        /// <exception cref="OperationCanceledException">The user canceled the task.</exception>
        /// <exception cref="IOException">A problem occurred while writing to the filesystem or registry.</exception>
        /// <exception cref="WebException">A problem occurred while downloading additional data (such as icons).</exception>
        /// <exception cref="UnauthorizedAccessException">Write access to the filesystem or registry is not permitted.</exception>
        /// <exception cref="InvalidDataException">The data in <paramref name="urlProtocol"/> is invalid.</exception>
        public static void Register(FeedTarget target, Model.Capabilities.UrlProtocol urlProtocol, IIconStore iconStore, bool machineWide, bool accessPoint = false)
        {
            #region Sanity checks
            if (urlProtocol == null)
            {
                throw new ArgumentNullException(nameof(urlProtocol));
            }
            if (iconStore == null)
            {
                throw new ArgumentNullException(nameof(iconStore));
            }
            #endregion

            if (string.IsNullOrEmpty(urlProtocol.ID))
            {
                throw new InvalidDataException("Missing ID");
            }

            using var classesKey = RegistryClasses.OpenHive(machineWide);

            if (urlProtocol.KnownPrefixes.Count == 0)
            {
                if (accessPoint)
                { // Can only be registered invasively by registering protocol ProgID (will replace existing and become default)
                    using var progIDKey = classesKey.CreateSubKeyChecked(urlProtocol.ID);
                    progIDKey.SetValue("", urlProtocol.Descriptions.GetBestLanguage(CultureInfo.CurrentUICulture) ?? urlProtocol.ID);
                    RegistryClasses.Register(progIDKey, target, urlProtocol, iconStore, machineWide);
                    progIDKey.SetValue(ProtocolIndicator, "");
                }
            }
            else
            { // Can be registered non-invasively by registering custom ProgID (without becoming default)
                using (var progIDKey = classesKey.CreateSubKeyChecked(RegistryClasses.Prefix + urlProtocol.ID))
                {
                    progIDKey.SetValue("", urlProtocol.Descriptions.GetBestLanguage(CultureInfo.CurrentUICulture) ?? urlProtocol.ID);
                    progIDKey.SetValue(accessPoint ? RegistryClasses.PurposeFlagAccessPoint : RegistryClasses.PurposeFlagCapability, "");
                    RegistryClasses.Register(progIDKey, target, urlProtocol, iconStore, machineWide);
                    progIDKey.SetValue(ProtocolIndicator, "");
                }

                if (accessPoint)
                {
                    foreach (var prefix in urlProtocol.KnownPrefixes)
                    {
                        if (WindowsUtils.IsWindowsVista && !machineWide)
                        {
                            using var userChoiceKey = Registry.CurrentUser.CreateSubKeyChecked($@"{RegKeyUserVistaUrlAssoc}\{prefix.Value}\UserChoice");
                            userChoiceKey.SetValue("ProgID", RegistryClasses.Prefix + urlProtocol.ID);
                        }
                        else
                        {
                            // Setting default invasively by registering protocol ProgID
                            using var progIDKey = classesKey.CreateSubKeyChecked(prefix.Value);
                            RegistryClasses.Register(progIDKey, target, urlProtocol, iconStore, machineWide);
                            progIDKey.SetValue(ProtocolIndicator, "");
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Registers an application as a candidate for a default program for some service in the current system. This can only be applied machine-wide, not per user.
        /// </summary>
        /// <param name="target">The application being integrated.</param>
        /// <param name="defaultProgram">The default program information to be registered.</param>
        /// <param name="iconStore">Stores icon files downloaded from the web as local files.</param>
        /// <param name="accessPoint">Indicates that the program should be set as the current default for the service it provides.</param>
        /// <exception cref="OperationCanceledException">The user canceled the task.</exception>
        /// <exception cref="IOException">A problem occurred while writing to the filesystem or registry.</exception>
        /// <exception cref="WebException">A problem occurred while downloading additional data (such as icons).</exception>
        /// <exception cref="UnauthorizedAccessException">Write access to the filesystem or registry is not permitted.</exception>
        /// <exception cref="InvalidDataException">The data in <paramref name="defaultProgram"/> is invalid.</exception>
        public static void Register(FeedTarget target, Model.Capabilities.DefaultProgram defaultProgram, IIconStore iconStore, bool accessPoint = false)
        {
            #region Sanity checks
            if (defaultProgram == null)
            {
                throw new ArgumentNullException(nameof(defaultProgram));
            }
            if (iconStore == null)
            {
                throw new ArgumentNullException(nameof(iconStore));
            }
            #endregion

            if (string.IsNullOrEmpty(defaultProgram.ID))
            {
                throw new InvalidDataException("Missing ID");
            }
            if (string.IsNullOrEmpty(defaultProgram.Service))
            {
                throw new InvalidDataException("Missing Service");
            }

            using var serviceKey = Registry.LocalMachine.CreateSubKeyChecked($@"{RegKeyMachineClients}\{defaultProgram.Service}");
            using (var appKey = serviceKey.CreateSubKeyChecked(defaultProgram.ID))
            {
                appKey.SetValue("", target.Feed.Name);
                appKey.SetValue(accessPoint ? RegistryClasses.PurposeFlagAccessPoint : RegistryClasses.PurposeFlagCapability, "");
                RegistryClasses.Register(appKey, target, defaultProgram, iconStore, machineWide: true);

                // Set callbacks for Windows SPAD
                using (var installInfoKey = appKey.CreateSubKeyChecked(RegSubKeyInstallInfo))
                {
                    string exePath = Path.Combine(Locations.InstallBase, "0install-win.exe");
                    installInfoKey.SetValue(RegValueReinstallCommand, new[] { exePath, "integrate", "--machine", "--batch", "--add", "defaults", target.Uri.ToStringRfc() }.JoinEscapeArguments());
                    installInfoKey.SetValue(RegValueShowIconsCommand, new[] { exePath, "integrate", "--machine", "--batch", "--add", MenuEntry.CategoryName, "--add", DesktopIcon.CategoryName, target.Uri.ToStringRfc() }.JoinEscapeArguments());
                    installInfoKey.SetValue(RegValueHideIconsCommand, new[] { exePath, "integrate", "--machine", "--batch", "--remove", MenuEntry.CategoryName, "--remove", DesktopIcon.CategoryName, target.Uri.ToStringRfc() }.JoinEscapeArguments());
                    installInfoKey.SetValue(RegValueIconsVisible, 0, RegistryValueKind.DWord);
                }

                if (defaultProgram.Service == Model.Capabilities.DefaultProgram.ServiceMail)
                {
                    var mailToProtocol = new Model.Capabilities.UrlProtocol {
                        Verbs = { new Verb {
                                      Name = Verb.NameOpen
                                  } }
                    };
                    using var mailToKey = appKey.CreateSubKeyChecked(@"Protocols\mailto");
                    RegistryClasses.Register(mailToKey, target, mailToProtocol, iconStore, machineWide: true);
                }
            }

            if (accessPoint)
            {
                serviceKey.SetValue("", defaultProgram.ID);
            }
        }
        /// <summary>
        /// Unregisters a URL protocol in the current system.
        /// </summary>
        /// <param name="urlProtocol">The URL protocol to remove.</param>
        /// <param name="machineWide">Unregister the URL protocol machine-wide instead of just for the current user.</param>
        /// <param name="accessPoint">Indicates that the handler was the default handler for the protocol.</param>
        /// <exception cref="IOException">A problem occurred while writing to the filesystem or registry.</exception>
        /// <exception cref="UnauthorizedAccessException">Write access to the filesystem or registry is not permitted.</exception>
        /// <exception cref="InvalidDataException">The data in <paramref name="urlProtocol"/> is invalid.</exception>
        public static void Unregister(Model.Capabilities.UrlProtocol urlProtocol, bool machineWide, bool accessPoint = false)
        {
            #region Sanity checks
            if (urlProtocol == null)
            {
                throw new ArgumentNullException(nameof(urlProtocol));
            }
            #endregion

            if (string.IsNullOrEmpty(urlProtocol.ID))
            {
                throw new InvalidDataException("Missing ID");
            }

            using var classesKey = RegistryClasses.OpenHive(machineWide);

            if (urlProtocol.KnownPrefixes.Count == 0)
            {
                if (accessPoint) // Was registered invasively by registering protocol ProgID
                {
                    classesKey.DeleteSubKeyTree(urlProtocol.ID, throwOnMissingSubKey: false);
                }
            }
            else
            { // Was registered non-invasively by registering custom ProgID
                if (accessPoint)
                {
                    foreach (var prefix in urlProtocol.KnownPrefixes)
                    {
                        // TODO: Restore previous default
                    }
                }

                // Remove appropriate purpose flag and check if there are others
                bool otherFlags;
                using (var progIDKey = classesKey.OpenSubKey(RegistryClasses.Prefix + urlProtocol.ID, writable: true))
                {
                    if (progIDKey == null)
                    {
                        otherFlags = false;
                    }
                    else
                    {
                        progIDKey.DeleteValue(accessPoint ? RegistryClasses.PurposeFlagAccessPoint : RegistryClasses.PurposeFlagCapability, throwOnMissingValue: false);
                        otherFlags = progIDKey.GetValueNames().Any(name => name.StartsWith(RegistryClasses.PurposeFlagPrefix));
                    }
                }

                // Delete ProgID if there are no other references
                if (!otherFlags)
                {
                    classesKey.DeleteSubKeyTree(RegistryClasses.Prefix + urlProtocol.ID, throwOnMissingSubKey: false);
                }
            }
            #endregion
        }
Example #4
0
    /// <summary>
    /// Unregisters a URL protocol in the current system.
    /// </summary>
    /// <param name="urlProtocol">The URL protocol to remove.</param>
    /// <param name="machineWide">Unregister the URL protocol machine-wide instead of just for the current user.</param>
    /// <param name="accessPoint">Indicates that the handler was the default handler for the protocol.</param>
    /// <exception cref="IOException">A problem occurred while writing to the filesystem.</exception>
    /// <exception cref="UnauthorizedAccessException">Write access to the filesystem is not permitted.</exception>
    public static void Unregister(Model.Capabilities.UrlProtocol urlProtocol, bool machineWide, bool accessPoint = false)
    {
        #region Sanity checks
        if (urlProtocol == null)
        {
            throw new ArgumentNullException(nameof(urlProtocol));
        }
        #endregion

        // TODO: Implement
    }
Example #5
0
    /// <summary>
    /// Registers a URL protocol in the current system.
    /// </summary>
    /// <param name="target">The application being integrated.</param>
    /// <param name="urlProtocol">The URL protocol to register.</param>
    /// <param name="machineWide">Register the URL protocol machine-wide instead of just for the current user.</param>
    /// <param name="iconStore">Stores icon files downloaded from the web as local files.</param>
    /// <param name="accessPoint">Indicates that the handler shall become the default handler for the protocol.</param>
    /// <exception cref="OperationCanceledException">The user canceled the task.</exception>
    /// <exception cref="IOException">A problem occurred while writing to the filesystem.</exception>
    /// <exception cref="WebException">A problem occurred while downloading additional data (such as icons).</exception>
    /// <exception cref="UnauthorizedAccessException">Write access to the filesystem is not permitted.</exception>
    public static void Register(FeedTarget target, Model.Capabilities.UrlProtocol urlProtocol, IIconStore iconStore, bool machineWide, bool accessPoint = false)
    {
        #region Sanity checks
        if (urlProtocol == null)
        {
            throw new ArgumentNullException(nameof(urlProtocol));
        }
        if (iconStore == null)
        {
            throw new ArgumentNullException(nameof(iconStore));
        }
        #endregion

        // TODO: Implement
    }
Example #6
0
        /// <summary>
        /// Unregisters a URL protocol in the current system.
        /// </summary>
        /// <param name="urlProtocol">The URL protocol to remove.</param>
        /// <param name="machineWide">Unregister the URL protocol machine-wide instead of just for the current user.</param>
        /// <param name="accessPoint">Indicates that the handler was the default handler for the protocol.</param>
        /// <exception cref="IOException">A problem occurred while writing to the filesystem.</exception>
        /// <exception cref="UnauthorizedAccessException">Write access to the filesystem is not permitted.</exception>
        /// <exception cref="InvalidDataException">The data in <paramref name="urlProtocol"/> is invalid.</exception>
        public static void Unregister(Model.Capabilities.UrlProtocol urlProtocol, bool machineWide, bool accessPoint = false)
        {
            #region Sanity checks
            if (urlProtocol == null)
            {
                throw new ArgumentNullException(nameof(urlProtocol));
            }
            #endregion

            if (string.IsNullOrEmpty(urlProtocol.ID))
            {
                throw new InvalidDataException("Missing ID");
            }

            // TODO: Implement
        }