Ejemplo n.º 1
0
    /// <summary>
    /// Collects data about file associations indicated by registered application capabilities.
    /// </summary>
    /// <param name="capsKey">A registry key containing capability information for a registered application.</param>
    /// <param name="capabilities">The capability list to add the collected data to.</param>
    /// <exception cref="IOException">There was an error accessing the registry.</exception>
    /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception>
    private static void CollectFileAssocsEx(RegistryKey capsKey, CapabilityList capabilities)
    {
        #region Sanity checks
        if (capsKey == null)
        {
            throw new ArgumentNullException(nameof(capsKey));
        }
        if (capabilities == null)
        {
            throw new ArgumentNullException(nameof(capabilities));
        }
        #endregion

        using var fileAssocKey = capsKey.OpenSubKey(DesktopIntegration.Windows.AppRegistration.RegSubKeyFileAssocs);
        if (fileAssocKey == null)
        {
            return;
        }

        foreach (string extension in fileAssocKey.GetValueNames())
        {
            string?progID = fileAssocKey.GetValue(extension)?.ToString();
            if (!string.IsNullOrEmpty(progID))
            {
                AddExtensionToFileType(extension, progID, capabilities);
            }
        }
    }
Ejemplo n.º 2
0
        private static CapabilityList GetCapabilityList(CommandMapper commandMapper, SnapshotDiff diff)
        {
            var capabilities = new CapabilityList {
                OS = OS.Windows
            };
            string appName = null, appDescription = null;

            diff.CollectFileTypes(commandMapper, capabilities);
            diff.CollectContextMenus(commandMapper, capabilities);
            diff.CollectAutoPlays(commandMapper, capabilities);
            diff.CollectDefaultPrograms(commandMapper, capabilities, ref appName);

            var appRegistration = diff.GetAppRegistration(commandMapper, capabilities, ref appName, ref appDescription);

            if (appRegistration != null)
            {
                capabilities.Entries.Add(appRegistration);
            }
            else
            { // Only collect URL protocols if there wasn't already an application registration that covered them
                diff.CollectProtocolAssocs(commandMapper, capabilities);
            }

            return(capabilities);
        }
        public void TestUpdateApp()
        {
            var capabilityList = new CapabilityList
            {
                OS      = Architecture.CurrentSystem.OS,
                Entries = { new FileType {
                                ID = "my_ext1"
                            }, new FileType{
                                ID = "my_ext2"
                            } }
            };
            var feed = new Feed {
                Name = "Test 1", CapabilityLists = { capabilityList }
            };
            var accessPoints = new AccessPoint[] { new MockAccessPoint {
                                                       ID = "id1", Capability = "my_ext1"
                                                   } };

            var appEntry = _integrationManager.AddApp(new FeedTarget(FeedTest.Test1Uri, feed));

            _integrationManager.AddAccessPoints(appEntry, feed, accessPoints);
            _integrationManager.AppList.Entries[0].AccessPoints !.Entries
            .Should().Equal(accessPoints, because: "All access points should be applied.");

            // Modify feed
            feed.Name = "Test 2";
            capabilityList.Entries.RemoveLast();

            _integrationManager.UpdateApp(appEntry, feed);
            appEntry.Name.Should().Be("Test 2");
            _integrationManager.AppList.Entries[0].AccessPoints !.Entries
            .Should().Equal(new[] { accessPoints[0] }, because: "Only the first access point should be left.");
        }
Ejemplo n.º 4
0
    /// <summary>
    /// Adds an extension to an existing <see cref="FileType"/>.
    /// </summary>
    /// <param name="extension">The file extension including the leading dot (e.g. ".png").</param>
    /// <param name="progID">The ID of the <see cref="FileType"/> to add the extension to.</param>
    /// <param name="capabilities">The list of capabilities to find existing <see cref="FileType"/>s in.</param>
    private static void AddExtensionToFileType(string extension, string progID, CapabilityList capabilities)
    {
        #region Sanity checks
        if (string.IsNullOrEmpty(progID))
        {
            throw new ArgumentNullException(nameof(progID));
        }
        if (string.IsNullOrEmpty(extension))
        {
            throw new ArgumentNullException(nameof(extension));
        }
        if (capabilities == null)
        {
            throw new ArgumentNullException(nameof(capabilities));
        }
        #endregion

        // Find the matching existing file type
        var fileType = capabilities.Entries.OfType <FileType>().FirstOrDefault(type => type.ID == progID);

        if (fileType != null)
        {
            // Check if the file type already has the extension and add it if not
            if (!fileType.Extensions.Any(element => StringUtils.EqualsIgnoreCase(element.Value, extension)))
            {
                fileType.Extensions.Add(new FileTypeExtension {
                    Value = extension.ToLower()
                });
            }
        }
    }
Ejemplo n.º 5
0
    /// <summary>
    /// Collects data about context menu entries.
    /// </summary>
    /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param>
    /// <param name="capabilities">The capability list to add the collected data to.</param>
    /// <exception cref="IOException">There was an error accessing the registry.</exception>
    /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception>
    public void CollectContextMenus(CommandMapper commandMapper, CapabilityList capabilities)
    {
        #region Sanity checks
        if (capabilities == null)
        {
            throw new ArgumentNullException(nameof(capabilities));
        }
        if (commandMapper == null)
        {
            throw new ArgumentNullException(nameof(commandMapper));
        }
        #endregion

        using (var progIDKey = Registry.ClassesRoot.OpenSubKey(DesktopIntegration.Windows.ContextMenu.RegKeyClassesFiles))
        {
            if (progIDKey == null)
            {
                throw new IOException("Registry key not found");
            }
            foreach (string entry in ContextMenuFiles)
            {
                capabilities.Entries.Add(new ContextMenu
                {
                    ID     = "files-" + entry,
                    Target = ContextMenuTarget.Files,
                    Verbs  = { GetVerb(progIDKey, commandMapper, entry) ?? throw new IOException($"Verb '{entry}' not found.") }
                });
    /// <summary>
    /// Collects data about default programs.
    /// </summary>
    /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param>
    /// <param name="capabilities">The capability list to add the collected data to.</param>
    /// <param name="appName">Is set to the name of the application as displayed to the user; unchanged if the name was not found.</param>
    /// <exception cref="IOException">There was an error accessing the registry.</exception>
    /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception>
    public void CollectDefaultPrograms(CommandMapper commandMapper, CapabilityList capabilities, ref string? appName)
    {
        #region Sanity checks
        if (capabilities == null) throw new ArgumentNullException(nameof(capabilities));
        if (commandMapper == null) throw new ArgumentNullException(nameof(commandMapper));
        #endregion

        // Ambiguity warnings
        if (ServiceAssocs.Count > 1)
            Log.Warn(Resources.MultipleDefaultProgramsDetected);

        foreach ((string service, string client) in ServiceAssocs)
        {
            using var clientKey = Registry.LocalMachine.OpenSubKey(DesktopIntegration.Windows.DefaultProgram.RegKeyMachineClients + @"\" + service + @"\" + client);
            if (clientKey == null) continue;

            if (string.IsNullOrEmpty(appName))
                appName = (clientKey.GetValue("") ?? clientKey.GetValue(DesktopIntegration.Windows.DefaultProgram.RegValueLocalizedName))?.ToString();

            capabilities.Entries.Add(new DefaultProgram
            {
                ID = client,
                Service = service,
                Verbs = {GetVerbs(clientKey, commandMapper)},
                InstallCommands = GetInstallCommands(clientKey, commandMapper.InstallationDir)
            });
        }
    }
Ejemplo n.º 7
0
        /// <summary>
        /// Collects data about well-known URL protocol handlers.
        /// </summary>
        /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param>
        /// <param name="capabilities">The capability list to add the collected data to.</param>
        /// <exception cref="IOException">There was an error accessing the registry.</exception>
        /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception>
        public void CollectProtocolAssocs([NotNull] CommandMapper commandMapper, [NotNull] CapabilityList capabilities)
        {
            #region Sanity checks
            if (capabilities == null)
            {
                throw new ArgumentNullException(nameof(capabilities));
            }
            if (commandMapper == null)
            {
                throw new ArgumentNullException(nameof(commandMapper));
            }
            #endregion

            foreach (string protocol in ProtocolAssocs.Select(protocolAssoc => protocolAssoc.Key))
            {
                using (var protocolKey = Registry.ClassesRoot.OpenSubKey(protocol))
                {
                    if (protocolKey == null)
                    {
                        throw new IOException(protocol + " not found");
                    }
                    capabilities.Entries.Add(new UrlProtocol
                    {
                        ID           = protocol,
                        Descriptions = { RegistryUtils.GetString(@"HKEY_CLASSES_ROOT\" + protocol, valueName: null, defaultValue: protocol) },
                        Verbs        = { GetVerb(protocolKey, commandMapper, "open") }
                    });
                }
            }
        }
Ejemplo n.º 8
0
    /// <summary>
    /// Retrieves data about registered applications.
    /// </summary>
    /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param>
    /// <param name="capabilities">The capability list to add the collected data to.</param>
    /// <param name="appName">Is set to the name of the application as displayed to the user; unchanged if the name was not found.</param>
    /// <param name="appDescription">Is set to a user-friendly description of the application; unchanged if the name was not found.</param>
    /// <exception cref="IOException">There was an error accessing the registry.</exception>
    /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception>
    public AppRegistration?GetAppRegistration(CommandMapper commandMapper, CapabilityList capabilities, ref string?appName, ref string?appDescription)
    {
        #region Sanity checks
        if (capabilities == null)
        {
            throw new ArgumentNullException(nameof(capabilities));
        }
        if (commandMapper == null)
        {
            throw new ArgumentNullException(nameof(commandMapper));
        }
        #endregion

        // Ambiguity warnings
        if (RegisteredApplications.Count == 0)
        {
            return(null);
        }
        if (RegisteredApplications.Count > 1)
        {
            Log.Warn(Resources.MultipleRegisteredAppsDetected);
        }

        // Get registry path pointer
        string appRegName          = RegisteredApplications[0];
        string?capabilitiesRegPath = RegistryUtils.GetString(@"HKEY_LOCAL_MACHINE\" + DesktopIntegration.Windows.AppRegistration.RegKeyMachineRegisteredApplications, appRegName);
        if (string.IsNullOrEmpty(capabilitiesRegPath))
        {
            return(null);
        }

        try
        {
            using var capsKey = RegistryUtils.OpenHklmKey(capabilitiesRegPath, out _);
            if (string.IsNullOrEmpty(appName))
            {
                appName = capsKey.GetValue(DesktopIntegration.Windows.AppRegistration.RegValueAppName)?.ToString();
            }
            if (string.IsNullOrEmpty(appDescription))
            {
                appDescription = capsKey.GetValue(DesktopIntegration.Windows.AppRegistration.RegValueAppDescription)?.ToString();
            }

            CollectProtocolAssocsEx(capsKey, commandMapper, capabilities);
            CollectFileAssocsEx(capsKey, capabilities);
            // Note: Contenders for StartMenu entries are detected elsewhere
        }
        catch (IOException ex)
        {
            Log.Warn(ex);
        }

        return(new()
        {
            ID = appRegName,
            CapabilityRegPath = capabilitiesRegPath
        });
    }
Ejemplo n.º 9
0
        /// <summary>
        /// Collects data about default programs.
        /// </summary>
        /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param>
        /// <param name="capabilities">The capability list to add the collected data to.</param>
        /// <param name="appName">Is set to the name of the application as displayed to the user; unchanged if the name was not found.</param>
        /// <exception cref="IOException">There was an error accessing the registry.</exception>
        /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception>
        public void CollectDefaultPrograms([NotNull] CommandMapper commandMapper, [NotNull] CapabilityList capabilities, ref string appName)
        {
            #region Sanity checks
            if (capabilities == null)
            {
                throw new ArgumentNullException(nameof(capabilities));
            }
            if (commandMapper == null)
            {
                throw new ArgumentNullException(nameof(commandMapper));
            }
            #endregion

            // Ambiguity warnings
            if (ServiceAssocs.Length > 1)
            {
                Log.Warn(Resources.MultipleDefaultProgramsDetected);
            }

            foreach (var serviceAssoc in ServiceAssocs)
            {
                string service = serviceAssoc.Key;
                string client  = serviceAssoc.Value;

                using (var clientKey = Registry.LocalMachine.OpenSubKey(DesktopIntegration.Windows.DefaultProgram.RegKeyMachineClients + @"\" + service + @"\" + client))
                {
                    if (clientKey == null)
                    {
                        continue;
                    }

                    if (string.IsNullOrEmpty(appName))
                    {
                        appName = clientKey.GetValue("", "").ToString();
                    }
                    if (string.IsNullOrEmpty(appName))
                    {
                        appName = clientKey.GetValue(DesktopIntegration.Windows.DefaultProgram.RegValueLocalizedName, "").ToString();
                    }

                    var defaultProgram = new DefaultProgram
                    {
                        ID      = client,
                        Service = service
                    };
                    defaultProgram.Verbs.AddRange(GetVerbs(clientKey, commandMapper));
                    defaultProgram.InstallCommands = GetInstallCommands(clientKey, commandMapper.InstallationDir);
                    capabilities.Entries.Add(defaultProgram);
                }
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Display all capabilities
        /// </summary>
        private void UpdateDataView()
        {
            string sSql = string.Empty;

            webTab.Visible = false;
            string filter = txtFilter.Text;

            if (filter != string.Empty)
            {
                string cleanFilter = filter.Replace("'", "''").ToLower();
                cleanFilter = cleanFilter.Replace("[", "[[]");
                cleanFilter = cleanFilter.Replace("_", "[_]");
                cleanFilter = cleanFilter.Replace("%", "[%]");

                sSql += " LOWER(Name) like '%" + cleanFilter + "%' ";
                sSql += " OR LOWER(Description) like '%" + cleanFilter + "%' ";
            }

            using (CapabilityList capabilities = Capability.GetAll(sSql))
            {
                if (capabilities != null)
                {
                    if (capabilities.Count > 0)
                    {
                        dg.DataSource = capabilities;
                        Utils.InitGridSort(ref dg);
                        dg.DataBind();

                        dg.Visible          = true;
                        lbNoresults.Visible = false;
                    }
                    else
                    {
                        if (txtFilter.Text.Length > 0)
                        {
                            lbNoresults.Text = "No record match your search (" + txtFilter.Text + ")";
                        }

                        dg.Visible          = false;
                        lbNoresults.Visible = true;
                    }
                }
                else
                {
                    lbError.CssClass = "hc_error";
                    lbError.Text     = "Error: a system error occurred";
                    lbError.Visible  = true;
                }
            }
        }
Ejemplo n.º 11
0
    [Fact] // Ensures that the class is correctly serialized and deserialized.
    public void TestSaveLoad()
    {
        CapabilityList capabilityList1 = CreateTestCapabilityList(), capabilityList2;

        using (var tempFile = new TemporaryFile("0install-test-capabilities"))
        {
            // Write and read file
            capabilityList1.SaveXml(tempFile);
            capabilityList2 = XmlStorage.LoadXml <CapabilityList>(tempFile);
        }

        // Ensure data stayed the same
        capabilityList2.Should().Be(capabilityList1, because: "Serialized objects should be equal.");
        capabilityList2.GetHashCode().Should().Be(capabilityList1.GetHashCode(), because: "Serialized objects' hashes should be equal.");
        capabilityList2.Should().NotBeSameAs(capabilityList1, because: "Serialized objects should not return the same reference.");
    }
Ejemplo n.º 12
0
        /// <summary>
        /// Collects data about file types and also URL protocol handlers.
        /// </summary>
        /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param>
        /// <param name="capabilities">The capability list to add the collected data to.</param>
        /// <exception cref="IOException">There was an error accessing the registry.</exception>
        /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception>
        public void CollectFileTypes(CommandMapper commandMapper, CapabilityList capabilities)
        {
            #region Sanity checks
            if (capabilities == null)
            {
                throw new ArgumentNullException(nameof(capabilities));
            }
            if (commandMapper == null)
            {
                throw new ArgumentNullException(nameof(commandMapper));
            }
            #endregion

            capabilities.Entries.AddRange((
                                              from progID in ProgIDs
                                              where !string.IsNullOrEmpty(progID)
                                              select GetFileType(progID, commandMapper)).WhereNotNull());
        }
Ejemplo n.º 13
0
        private void writeStats()
        {
            int            i        = carryi;
            CapabilityList capXML   = new CapabilityList();
            SkillList      skillXML = new SkillList();

            capList.Clear();
            skillList.Clear();
            capList      = capXML.createList(pokeList[i].number);
            skillList    = skillXML.createList(pokeList[i].number);
            rtbInfo.Text = ("Capabilities:" + Environment.NewLine);
            for (var e = 0; e < capList.Count; e++)
            {
                rtbInfo.Text += "-" + capList[e].cap + Environment.NewLine;
            }
            rtbInfo.Text += Environment.NewLine + "Skills:" + Environment.NewLine;
            for (var e = 0; e < skillList.Count; e++)
            {
                rtbInfo.Text += "-" + skillList[e].name + " " + skillList[e].die + "d6+" + skillList[e].bonus + Environment.NewLine;
            }
        }
    /// <summary>
    /// Collects data about AutoPlay handlers.
    /// </summary>
    /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param>
    /// <param name="capabilities">The capability list to add the collected data to.</param>
    /// <exception cref="IOException">There was an error accessing the registry.</exception>
    /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception>
    public void CollectAutoPlays(CommandMapper commandMapper, CapabilityList capabilities)
    {
        #region Sanity checks
        if (capabilities == null)
        {
            throw new ArgumentNullException(nameof(capabilities));
        }
        if (commandMapper == null)
        {
            throw new ArgumentNullException(nameof(commandMapper));
        }
        #endregion

        capabilities.Entries.AddRange(AutoPlayHandlersUser
                                      .Select(handler => GetAutoPlay(handler, Registry.CurrentUser, AutoPlayAssocsUser, commandMapper))
                                      .WhereNotNull());

        capabilities.Entries.AddRange(AutoPlayHandlersMachine
                                      .Select(handler => GetAutoPlay(handler, Registry.LocalMachine, AutoPlayAssocsMachine, commandMapper))
                                      .WhereNotNull());
    }
Ejemplo n.º 15
0
        public static bool IsCapkeyEnable(string capkey)
        {
            CapabilityList capability_result = new CapabilityList();
            HciErrorCode   capabilityList    = HciCloudSys.HciGetCapabilityList((string)null, ref capability_result);

            if ((uint)capabilityList > 0U)
            {
                Console.WriteLine("hci_get_capability_list failed return " + (object)capabilityList);
                return(false);
            }
            else
            {
                bool flag = false;
                foreach (CapabilityItem capabilityItem in (IEnumerable <CapabilityItem>)capability_result.CapabilityItemList)
                {
                    if (capkey == capabilityItem.CapKey)
                    {
                        flag = true;
                        break;
                    }
                }
                return(flag);
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Collects data about URL protocol handlers indicated by registered application capabilities.
        /// </summary>
        /// <param name="capsKey">A registry key containing capability information for a registered application.</param>
        /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param>
        /// <param name="capabilities">The capability list to add the collected data to.</param>
        /// <exception cref="IOException">There was an error accessing the registry.</exception>
        /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception>
        private static void CollectProtocolAssocsEx([NotNull] RegistryKey capsKey, [NotNull] CommandMapper commandMapper, [NotNull] CapabilityList capabilities)
        {
            #region Sanity checks
            if (capsKey == null)
            {
                throw new ArgumentNullException("capsKey");
            }
            if (commandMapper == null)
            {
                throw new ArgumentNullException("commandMapper");
            }
            if (capabilities == null)
            {
                throw new ArgumentNullException("capabilities");
            }
            #endregion

            using (var urlAssocKey = capsKey.OpenSubKey(DesktopIntegration.Windows.AppRegistration.RegSubKeyUrlAssocs))
            {
                if (urlAssocKey == null)
                {
                    return;
                }

                // TODO: Fold multiple prefixes pointing to one protocol together
                foreach (string protocol in urlAssocKey.GetValueNames())
                {
                    string progID = urlAssocKey.GetValue(protocol, "").ToString();
                    using (var progIDKey = Registry.ClassesRoot.OpenSubKey(progID))
                    {
                        if (progIDKey == null)
                        {
                            continue;
                        }

                        var capability = new UrlProtocol
                        {
                            ID            = progID,
                            Descriptions  = { progIDKey.GetValue("", "").ToString() },
                            KnownPrefixes = { new KnownProtocolPrefix {
                                                  Value = protocol
                                              } }
                        };

                        capability.Verbs.AddRange(GetVerbs(progIDKey, commandMapper));
                        capabilities.Entries.Add(capability);
                    }
                }
            }
        }
Ejemplo n.º 17
0
        private static CapabilityList GetCapabilityList(CommandMapper commandMapper, SnapshotDiff diff)
        {
            var capabilities = new CapabilityList {OS = OS.Windows};
            string appName = null, appDescription = null;

            diff.CollectFileTypes(commandMapper, capabilities);
            diff.CollectContextMenus(commandMapper, capabilities);
            diff.CollectAutoPlays(commandMapper, capabilities);
            diff.CollectDefaultPrograms(commandMapper, capabilities, ref appName);

            var appRegistration = diff.GetAppRegistration(commandMapper, capabilities, ref appName, ref appDescription);
            if (appRegistration != null) capabilities.Entries.Add(appRegistration);
            else
            { // Only collect URL protocols if there wasn't already an application registration that covered them
                diff.CollectProtocolAssocs(commandMapper, capabilities);
            }

            return capabilities;
        }
        public void TestUpdateApp()
        {
            var capabilitiyList = new CapabilityList
            {
                OS = Architecture.CurrentSystem.OS,
                Entries = {new FileType {ID = "my_ext1"}, new FileType {ID = "my_ext2"}}
            };
            var feed = new Feed {Name = "Test 1", CapabilityLists = {capabilitiyList}};
            var accessPoints = new AccessPoint[] {new MockAccessPoint {ID = "id1", Capability = "my_ext1"}};

            var appEntry = _integrationManager.AddApp(new FeedTarget(FeedTest.Test1Uri, feed));
            _integrationManager.AddAccessPoints(appEntry, feed, accessPoints);
            CollectionAssert.AreEquivalent(accessPoints, _integrationManager.AppList.Entries[0].AccessPoints.Entries, "All access points should be applied.");

            // Modify feed
            feed.Name = "Test 2";
            capabilitiyList.Entries.RemoveLast();

            _integrationManager.UpdateApp(appEntry, feed);
            Assert.AreEqual("Test 2", appEntry.Name);
            CollectionAssert.AreEquivalent(new[] {accessPoints[0]}, _integrationManager.AppList.Entries[0].AccessPoints.Entries, "Only the first access point should be left.");
        }
        /// <summary>
        /// Collects data about context menu entries.
        /// </summary>
        /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param>
        /// <param name="capabilities">The capability list to add the collected data to.</param>
        /// <exception cref="IOException">There was an error accessing the registry.</exception>
        /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception>
        public void CollectContextMenus([NotNull] CommandMapper commandMapper, [NotNull] CapabilityList capabilities)
        {
            #region Sanity checks
            if (capabilities == null)
            {
                throw new ArgumentNullException("capabilities");
            }
            if (commandMapper == null)
            {
                throw new ArgumentNullException("commandMapper");
            }
            #endregion

            using (var progIDKey = Registry.ClassesRoot.OpenSubKey(DesktopIntegration.Windows.ContextMenu.RegKeyClassesFiles))
            {
                if (progIDKey == null)
                {
                    throw new IOException("Registry key not found");
                }
                foreach (string entry in ContextMenuFiles)
                {
                    capabilities.Entries.Add(new ContextMenu
                    {
                        ID     = "files-" + entry,
                        Target = ContextMenuTarget.Files,
                        Verb   = GetVerb(progIDKey, commandMapper, entry)
                    });
                }
            }

            using (var progIDKey = Registry.ClassesRoot.OpenSubKey(DesktopIntegration.Windows.ContextMenu.RegKeyClassesExecutableFiles[0]))
            {
                if (progIDKey == null)
                {
                    throw new IOException("Registry key not found");
                }
                foreach (string entry in ContextMenuExecutableFiles)
                {
                    capabilities.Entries.Add(new ContextMenu
                    {
                        ID     = "executable-files-" + entry,
                        Target = ContextMenuTarget.ExecutableFiles,
                        Verb   = GetVerb(progIDKey, commandMapper, entry)
                    });
                }
            }

            using (var progIDKey = Registry.ClassesRoot.OpenSubKey(DesktopIntegration.Windows.ContextMenu.RegKeyClassesDirectories))
            {
                if (progIDKey == null)
                {
                    throw new IOException("Registry key not found");
                }
                foreach (string entry in ContextMenuDirectories)
                {
                    capabilities.Entries.Add(new ContextMenu
                    {
                        ID     = "directories-" + entry,
                        Target = ContextMenuTarget.Directories,
                        Verb   = GetVerb(progIDKey, commandMapper, entry)
                    });
                }
            }

            using (var progIDKey = Registry.ClassesRoot.OpenSubKey(DesktopIntegration.Windows.ContextMenu.RegKeyClassesAll))
            {
                if (progIDKey == null)
                {
                    throw new IOException("Registry key not found");
                }
                foreach (string entry in ContextMenuAll)
                {
                    capabilities.Entries.Add(new ContextMenu
                    {
                        ID     = "all-" + entry,
                        Target = ContextMenuTarget.Directories,
                        Verb   = GetVerb(progIDKey, commandMapper, entry)
                    });
                }
            }
        }
Ejemplo n.º 20
0
    /// <summary>
    /// Collects data about URL protocol handlers indicated by registered application capabilities.
    /// </summary>
    /// <param name="capsKey">A registry key containing capability information for a registered application.</param>
    /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param>
    /// <param name="capabilities">The capability list to add the collected data to.</param>
    /// <exception cref="IOException">There was an error accessing the registry.</exception>
    /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception>
    private static void CollectProtocolAssocsEx(RegistryKey capsKey, CommandMapper commandMapper, CapabilityList capabilities)
    {
        #region Sanity checks
        if (capsKey == null)
        {
            throw new ArgumentNullException(nameof(capsKey));
        }
        if (commandMapper == null)
        {
            throw new ArgumentNullException(nameof(commandMapper));
        }
        if (capabilities == null)
        {
            throw new ArgumentNullException(nameof(capabilities));
        }
        #endregion

        using var urlAssocKey = capsKey.OpenSubKey(DesktopIntegration.Windows.AppRegistration.RegSubKeyUrlAssocs);
        if (urlAssocKey == null)
        {
            return;
        }

        foreach (string protocol in urlAssocKey.GetValueNames())
        {
            string?progID = urlAssocKey.GetValue(protocol)?.ToString();
            if (string.IsNullOrEmpty(progID))
            {
                continue;
            }
            using var progIDKey = Registry.ClassesRoot.OpenSubKey(progID);
            if (progIDKey == null)
            {
                continue;
            }

            var prefix = new KnownProtocolPrefix {
                Value = protocol
            };
            var existing = capabilities.GetCapability <UrlProtocol>(progID);
            if (existing == null)
            {
                var capability = new UrlProtocol
                {
                    ID            = progID,
                    Descriptions  = { progIDKey.GetValue("", defaultValue: "")?.ToString() ?? "" },
                    KnownPrefixes = { prefix }
                };
                capability.Verbs.AddRange(GetVerbs(progIDKey, commandMapper));
                capabilities.Entries.Add(capability);
            }
            else
            {
                existing.KnownPrefixes.Add(prefix);
            }
        }
    }
Ejemplo n.º 21
0
        /// <summary>
        /// Retrieves data about registered applications.
        /// </summary>
        /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param>
        /// <param name="capabilities">The capability list to add the collected data to.</param>
        /// <param name="appName">Is set to the name of the application as displayed to the user; unchanged if the name was not found.</param>
        /// <param name="appDescription">Is set to a user-friendly description of the application; unchanged if the name was not found.</param>
        /// <exception cref="IOException">There was an error accessing the registry.</exception>
        /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception>
        public AppRegistration GetAppRegistration([NotNull] CommandMapper commandMapper, [NotNull] CapabilityList capabilities, ref string appName, ref string appDescription)
        {
            #region Sanity checks
            if (capabilities == null)
            {
                throw new ArgumentNullException("capabilities");
            }
            if (commandMapper == null)
            {
                throw new ArgumentNullException("commandMapper");
            }
            #endregion

            // Ambiguity warnings
            if (RegisteredApplications.Length == 0)
            {
                return(null);
            }
            if (RegisteredApplications.Length > 1)
            {
                Log.Warn(Resources.MultipleRegisteredAppsDetected);
            }

            // Get registry path pointer
            string appRegName          = RegisteredApplications[0];
            var    capabilitiesRegPath = RegistryUtils.GetString(@"HKEY_LOCAL_MACHINE\" + DesktopIntegration.Windows.AppRegistration.RegKeyMachineRegisteredApplications, appRegName);
            if (string.IsNullOrEmpty(capabilitiesRegPath))
            {
                return(null);
            }

            bool x64;
            using (var capsKey = RegistryUtils.OpenHklmKey(capabilitiesRegPath, out x64))
            {
                if (string.IsNullOrEmpty(appName))
                {
                    appName = capsKey.GetValue(DesktopIntegration.Windows.AppRegistration.RegValueAppName, "").ToString();
                }
                if (string.IsNullOrEmpty(appDescription))
                {
                    appDescription = capsKey.GetValue(DesktopIntegration.Windows.AppRegistration.RegValueAppDescription, "").ToString();
                }

                CollectProtocolAssocsEx(capsKey, commandMapper, capabilities);
                CollectFileAssocsEx(capsKey, capabilities);
                // Note: Contenders for StartMenu entries are detected elsewhere

                return(new AppRegistration
                {
                    ID = appRegName,
                    CapabilityRegPath = capabilitiesRegPath,
                    X64 = x64
                });
            }
        }