Skip to content
This repository has been archived by the owner on Apr 3, 2022. It is now read-only.

Commit

Permalink
Use selective imports for registry
Browse files Browse the repository at this point in the history
  • Loading branch information
spikespaz committed Jun 27, 2018
1 parent 82aac0f commit 83bb60d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 80 deletions.
2 changes: 1 addition & 1 deletion deflect.d
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module deflect;

import std.windows.registry;
import std.windows.registry : Registry, Key, REGSAM;
import std.process : spawnShell;
import std.string : replace, indexOf;
import std.array : split;
Expand Down
160 changes: 81 additions & 79 deletions setup.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import std.array : split;
import std.net.curl : get, CurlException;
import std.conv : parse, ConvException;
import std.string : toLower, strip, splitLines, indexOf, stripLeft;
import std.windows.registry;
import std.windows.registry : Key, Registry, REGSAM, RegistryException;

// Online resource to the repository of the project containing a list of search engine choices.
immutable string enginesURL = "https://raw.githubusercontent.com/spikespaz/search-deflector/master/engines.txt";
Expand Down Expand Up @@ -53,96 +53,98 @@ void registerHandler(const string filePath,
const string engineName, const string engineURL,
const string browserName, const string browserPath) {
// dfmt on
// Declare all of the Key variables I will need.
Key deflectorKey;
Key uriClassKey;
Key iconKey;
Key shellCommandKey;
Key softwareKey;
Key capabilityKey;
Key urlAssociationsKey;

// Try to open each one, if it doesn't exist, make it.
try {
deflectorKey = Registry.currentUser.getKey("SOFTWARE\\Clients\\SearchDeflector", REGSAM.KEY_WRITE);
}
catch (RegistryException) {
deflectorKey = Registry.currentUser.createKey("SOFTWARE\\Clients\\SearchDeflector", REGSAM.KEY_WRITE);
}

try {
uriClassKey = Registry.classesRoot.getKey("SearchDeflector", REGSAM.KEY_WRITE);
}
catch (RegistryException) {
uriClassKey = Registry.classesRoot.createKey("SearchDeflector", REGSAM.KEY_WRITE);
}

try {
iconKey = uriClassKey.getKey("DefaultIcon", REGSAM.KEY_WRITE);
}
catch (RegistryException) {
iconKey = uriClassKey.createKey("DefaultIcon", REGSAM.KEY_WRITE);
}
// Declare all of the Key variables I will need.
Key deflectorKey;
Key uriClassKey;
Key iconKey;
Key shellCommandKey;
Key softwareKey;
Key capabilityKey;
Key urlAssociationsKey;

// Try to open each one, if it doesn't exist, make it.
try {
deflectorKey = Registry.currentUser.getKey("SOFTWARE\\Clients\\SearchDeflector", REGSAM.KEY_WRITE);
}
catch (RegistryException) {
deflectorKey = Registry.currentUser.createKey("SOFTWARE\\Clients\\SearchDeflector", REGSAM.KEY_WRITE);
}

try {
shellCommandKey = uriClassKey.getKey("shell\\open\\command", REGSAM.KEY_WRITE);
}
catch (RegistryException) {
shellCommandKey = uriClassKey.createKey("shell\\open\\command", REGSAM.KEY_WRITE);
}
try {
uriClassKey = Registry.classesRoot.getKey("SearchDeflector", REGSAM.KEY_WRITE);
}
catch (RegistryException) {
uriClassKey = Registry.classesRoot.createKey("SearchDeflector", REGSAM.KEY_WRITE);
}

try {
softwareKey = Registry.localMachine.getKey("SOFTWARE\\Clients\\SearchDeflector", REGSAM.KEY_WRITE);
}
catch (RegistryException) {
softwareKey = Registry.localMachine.createKey("SOFTWARE\\Clients\\SearchDeflector", REGSAM.KEY_WRITE);
}
try {
iconKey = uriClassKey.getKey("DefaultIcon", REGSAM.KEY_WRITE);
}
catch (RegistryException) {
iconKey = uriClassKey.createKey("DefaultIcon", REGSAM.KEY_WRITE);
}

try {
capabilityKey = softwareKey.getKey("Capabilities", REGSAM.KEY_WRITE);
}
catch (RegistryException) {
capabilityKey = softwareKey.createKey("Capabilities", REGSAM.KEY_WRITE);
}
try {
shellCommandKey = uriClassKey.getKey("shell\\open\\command", REGSAM.KEY_WRITE);
}
catch (RegistryException) {
shellCommandKey = uriClassKey.createKey("shell\\open\\command", REGSAM.KEY_WRITE);
}

try {
urlAssociationsKey = capabilityKey.getKey("UrlAssociations", REGSAM.KEY_WRITE);
}
catch (RegistryException) {
urlAssociationsKey = capabilityKey.createKey("UrlAssociations", REGSAM.KEY_WRITE);
}
try {
softwareKey = Registry.localMachine.getKey("SOFTWARE\\Clients\\SearchDeflector", REGSAM.KEY_WRITE);
}
catch (RegistryException) {
softwareKey = Registry.localMachine.createKey("SOFTWARE\\Clients\\SearchDeflector", REGSAM.KEY_WRITE);
}

Key registeredAppsKey = Registry.localMachine.getKey("SOFTWARE\\RegisteredApplications", REGSAM.KEY_WRITE);
try {
capabilityKey = softwareKey.getKey("Capabilities", REGSAM.KEY_WRITE);
}
catch (RegistryException) {
capabilityKey = softwareKey.createKey("Capabilities", REGSAM.KEY_WRITE);
}

// Write necessary changes.
deflectorKey.setValue("EngineName", engineName);
deflectorKey.setValue("EngineURL", engineURL);
deflectorKey.setValue("BrowserName", browserName);
deflectorKey.setValue("BrowserPath", browserPath);
try {
urlAssociationsKey = capabilityKey.getKey("UrlAssociations", REGSAM.KEY_WRITE);
}
catch (RegistryException) {
urlAssociationsKey = capabilityKey.createKey("UrlAssociations", REGSAM.KEY_WRITE);
}

uriClassKey.setValue("", "Search Deflector");
uriClassKey.setValue("URL Protocol", "");
Key registeredAppsKey = Registry.localMachine.getKey("SOFTWARE\\RegisteredApplications", REGSAM.KEY_WRITE);

iconKey.setValue("", filePath ~ ",0");
// Write necessary changes.
deflectorKey.setValue("EngineName", engineName);
deflectorKey.setValue("EngineURL", engineURL);
deflectorKey.setValue("BrowserName", browserName);
deflectorKey.setValue("BrowserPath", browserPath);

shellCommandKey.setValue("", '"' ~ filePath ~ "\" \"%1\"");
uriClassKey.setValue("", "Search Deflector");
uriClassKey.setValue("URL Protocol", "");

capabilityKey.setValue("ApplicationName", "Search Deflector");
capabilityKey.setValue("ApplicationDescription",
"Force web links for MS Edge to be opened with your preferred browser and search engine.");
iconKey.setValue("", filePath ~ ",0");

urlAssociationsKey.setValue("microsoft-edge", "SearchDeflector");
registeredAppsKey.setValue("SearchDeflector", "SOFTWARE\\Clients\\SearchDeflector\\Capabilities");
shellCommandKey.setValue("", '"' ~ filePath ~ "\" \"%1\"");

// Flush all of the keys and write changes.
deflectorKey.flush();
uriClassKey.flush();
iconKey.flush();
shellCommandKey.flush();
softwareKey.flush();
capabilityKey.flush();
urlAssociationsKey.flush();
registeredAppsKey.flush();
capabilityKey.setValue("ApplicationName", "Search Deflector");
// dfmt off
capabilityKey.setValue("ApplicationDescription",
"Force web links for MS Edge to be opened with your preferred browser and search engine.");
// dfmt on

urlAssociationsKey.setValue("microsoft-edge", "SearchDeflector");
registeredAppsKey.setValue("SearchDeflector", "SOFTWARE\\Clients\\SearchDeflector\\Capabilities");

// Flush all of the keys and write changes.
deflectorKey.flush();
uriClassKey.flush();
iconKey.flush();
shellCommandKey.flush();
softwareKey.flush();
capabilityKey.flush();
urlAssociationsKey.flush();
registeredAppsKey.flush();
}

// Fetch a list of available browsers from the Windows registry along with their paths.
Expand Down

0 comments on commit 83bb60d

Please sign in to comment.