#include "gsRegistry.h" gsRegistry::gsRegistry(installManager &manager, configureFile &configFile) : installAgent(manager) { // Create string for volume key volumeKey = "Software\\" + configFile.getString("CompanyName") + "\\" + configFile.getString("CollectionName") + "\\" + configFile.getString("CollectionVolume") + "\\" + configFile.getString("CollectionVersion"); // Create string for collection key collectKey = "Software\\" + configFile.getString("CompanyName") + "\\" + configFile.getString("CollectionName") + "\\" + configFile.getString("CollectionVersion"); } bool gsRegistry::logAction(string action, HKEY base, string path, string item, string value) { unInstallCommand command(action); command.addParameter(path); if (item != "" || value != "") { if (item == "") { command.addParameter("{Default}"); } else { command.addParameter(item); } command.addParameter(value); } return manager->storeCommand(command); } bool gsRegistry::undoAction(string actionName, stringArray ¶ms) { if (actionName == "RegistryCreateKey") { this->destroyKey(HKEY_LOCAL_MACHINE, params[0]); } else if (actionName == "RegistrySetString") { this->destroyItem(HKEY_LOCAL_MACHINE, params[0], params[1]); } else { return false; } return true; } bool gsRegistry::ensureParentKeyExists(HKEY base, string key) { string parent; int foundat; if ((foundat = key.rfind('\\', key.length())) < key.length()) { parent = key.substr(0, foundat); return this->ensureKeyExists(base, parent); } return true; } bool gsRegistry::ensureKeysExist() { if (!this->ensureKeyExists(HKEY_LOCAL_MACHINE, this->collectKey)) { return false; } return this->ensureKeyExists(HKEY_LOCAL_MACHINE, this->volumeKey); } bool gsRegistry::ensureKeyExists(HKEY base, string key) { if (!registry_keyExists(base, key.c_str())) { if (!this->ensureParentKeyExists(base, key)) { return false; } if (!registry_ensureKeyExists(base, key.c_str())) { return false; } this->logAction("RegistryCreateKey", base, key, "", ""); } return true; } bool gsRegistry::destroyKey(HKEY base, string key) { return registry_deletekey(base, key.c_str()); } bool gsRegistry::storeKeyString(HKEY base, string path, string item, string value) { HKEY local; bool reply; if (!registry_openkey(base, path.c_str(), &local)) { return false; } reply = registry_setstring(local, item.c_str(), value.c_str()); if (reply) { this->logAction("RegistrySetString", base, path, item, value); } registry_closekey(local); return reply; } bool gsRegistry::destroyItem(HKEY base, string path, string item) { HKEY local; if (!registry_openkey(base, path.c_str(), &local)) { return false; } registry_deleteitem(local, item.c_str()); registry_closekey(local); return true; } bool gsRegistry::collectionInstalled() { return registry_keyExists(HKEY_LOCAL_MACHINE, this->collectKey.c_str()); } bool gsRegistry::volumeInstalled() { return registry_keyExists(HKEY_LOCAL_MACHINE, this->volumeKey.c_str()); } string gsRegistry::collectKeyId() { return this->collectKey; } string gsRegistry::volumeKeyId() { return this->volumeKey; } string gsRegistry::exeKeyId(string exename) { string reply = "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\"; reply += exename; return reply; } string gsRegistry::uninstallKeyId(string collectName) { return "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + collectName; } FilePath *gsRegistry::collectionPath() { char *path = NULL; FilePath *reply = NULL; HKEY key; if (registry_openkey(HKEY_LOCAL_MACHINE, this->collectKey.c_str(), &key)) { if (registry_getstring(key, "library", &path)) { // get leaf (\library.exe) and remove it to give a path char *leaf; leaf = strrchr(path, '\\'); if (leaf != NULL) { *leaf = '\0'; } reply = new FilePath(path); free(path); } registry_closekey(key); } return reply; }