1 | #include "gsRegistry.h"
|
---|
2 |
|
---|
3 | gsRegistry::gsRegistry(installManager &manager, configureFile &configFile)
|
---|
4 | : installAgent(manager)
|
---|
5 | {
|
---|
6 | // Create string for volume key
|
---|
7 | volumeKey = "Software\\" + configFile.getString("CompanyName") + "\\" +
|
---|
8 | configFile.getString("CollectionName") + "\\" +
|
---|
9 | configFile.getString("CollectionVolume") + "\\" +
|
---|
10 | configFile.getString("CollectionVersion");
|
---|
11 |
|
---|
12 | // Create string for collection key
|
---|
13 | collectKey = "Software\\" + configFile.getString("CompanyName") + "\\" +
|
---|
14 | configFile.getString("CollectionName") + "\\" +
|
---|
15 | configFile.getString("CollectionVersion");
|
---|
16 | }
|
---|
17 |
|
---|
18 | bool gsRegistry::logAction(string action, HKEY base, string path, string item, string value)
|
---|
19 | {
|
---|
20 | unInstallCommand command(action);
|
---|
21 | command.addParameter(path);
|
---|
22 | if (item != "" || value != "")
|
---|
23 | {
|
---|
24 | if (item == "")
|
---|
25 | {
|
---|
26 | command.addParameter("{Default}");
|
---|
27 | }
|
---|
28 | else
|
---|
29 | {
|
---|
30 | command.addParameter(item);
|
---|
31 | }
|
---|
32 | command.addParameter(value);
|
---|
33 | }
|
---|
34 | return manager->storeCommand(command);
|
---|
35 | }
|
---|
36 |
|
---|
37 | bool gsRegistry::undoAction(string actionName, stringArray ¶ms)
|
---|
38 | { if (actionName == "RegistryCreateKey")
|
---|
39 | { this->destroyKey(HKEY_LOCAL_MACHINE, params[0]);
|
---|
40 | }
|
---|
41 | else if (actionName == "RegistrySetString")
|
---|
42 | { this->destroyItem(HKEY_LOCAL_MACHINE, params[0], params[1]);
|
---|
43 | }
|
---|
44 | else
|
---|
45 | { return false;
|
---|
46 | }
|
---|
47 | return true;
|
---|
48 | }
|
---|
49 |
|
---|
50 | bool gsRegistry::ensureParentKeyExists(HKEY base, string key)
|
---|
51 | { string parent;
|
---|
52 | int foundat;
|
---|
53 |
|
---|
54 | if ((foundat = key.rfind('\\', key.length())) < key.length())
|
---|
55 | { parent = key.substr(0, foundat);
|
---|
56 |
|
---|
57 | return this->ensureKeyExists(base, parent);
|
---|
58 | }
|
---|
59 | return true;
|
---|
60 | }
|
---|
61 |
|
---|
62 | bool gsRegistry::ensureKeysExist()
|
---|
63 | { if (!this->ensureKeyExists(HKEY_LOCAL_MACHINE, this->collectKey))
|
---|
64 | { return false;
|
---|
65 | }
|
---|
66 | return this->ensureKeyExists(HKEY_LOCAL_MACHINE, this->volumeKey);
|
---|
67 | }
|
---|
68 |
|
---|
69 | bool gsRegistry::ensureKeyExists(HKEY base, string key)
|
---|
70 | { if (!registry_keyExists(base, key.c_str()))
|
---|
71 | { if (!this->ensureParentKeyExists(base, key))
|
---|
72 | { return false;
|
---|
73 | }
|
---|
74 | if (!registry_ensureKeyExists(base, key.c_str()))
|
---|
75 | { return false;
|
---|
76 | }
|
---|
77 | this->logAction("RegistryCreateKey", base, key, "", "");
|
---|
78 | }
|
---|
79 | return true;
|
---|
80 | }
|
---|
81 |
|
---|
82 | bool gsRegistry::destroyKey(HKEY base, string key)
|
---|
83 | { return registry_deletekey(base, key.c_str());
|
---|
84 | }
|
---|
85 |
|
---|
86 | bool gsRegistry::storeKeyString(HKEY base, string path, string item, string value)
|
---|
87 | { HKEY local;
|
---|
88 | bool reply;
|
---|
89 |
|
---|
90 | if (!registry_openkey(base, path.c_str(), &local))
|
---|
91 | { return false;
|
---|
92 | }
|
---|
93 | reply = registry_setstring(local, item.c_str(), value.c_str());
|
---|
94 | if (reply)
|
---|
95 | { this->logAction("RegistrySetString", base, path, item, value);
|
---|
96 | }
|
---|
97 | registry_closekey(local);
|
---|
98 | return reply;
|
---|
99 | }
|
---|
100 |
|
---|
101 | bool gsRegistry::destroyItem(HKEY base, string path, string item)
|
---|
102 | { HKEY local;
|
---|
103 |
|
---|
104 | if (!registry_openkey(base, path.c_str(), &local))
|
---|
105 | { return false;
|
---|
106 | }
|
---|
107 | registry_deleteitem(local, item.c_str());
|
---|
108 | registry_closekey(local);
|
---|
109 | return true;
|
---|
110 | }
|
---|
111 |
|
---|
112 | bool gsRegistry::collectionInstalled()
|
---|
113 | { return registry_keyExists(HKEY_LOCAL_MACHINE, this->collectKey.c_str());
|
---|
114 | }
|
---|
115 |
|
---|
116 | bool gsRegistry::volumeInstalled()
|
---|
117 | { return registry_keyExists(HKEY_LOCAL_MACHINE, this->volumeKey.c_str());
|
---|
118 | }
|
---|
119 |
|
---|
120 | string gsRegistry::collectKeyId()
|
---|
121 | { return this->collectKey;
|
---|
122 | }
|
---|
123 |
|
---|
124 | string gsRegistry::volumeKeyId()
|
---|
125 | { return this->volumeKey;
|
---|
126 | }
|
---|
127 |
|
---|
128 | string gsRegistry::exeKeyId(string exename)
|
---|
129 | { string reply = "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\";
|
---|
130 | reply += exename;
|
---|
131 | return reply;
|
---|
132 | }
|
---|
133 |
|
---|
134 | string gsRegistry::uninstallKeyId(string collectName)
|
---|
135 | { return "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + collectName;
|
---|
136 | }
|
---|
137 |
|
---|
138 | FilePath *gsRegistry::collectionPath()
|
---|
139 | { char *path = NULL;
|
---|
140 | FilePath *reply = NULL;
|
---|
141 | HKEY key;
|
---|
142 |
|
---|
143 | if (registry_openkey(HKEY_LOCAL_MACHINE, this->collectKey.c_str(), &key))
|
---|
144 | { if (registry_getstring(key, "library", &path))
|
---|
145 | { // get leaf (\library.exe) and remove it to give a path
|
---|
146 | char *leaf;
|
---|
147 |
|
---|
148 | leaf = strrchr(path, '\\');
|
---|
149 | if (leaf != NULL)
|
---|
150 | { *leaf = '\0';
|
---|
151 | }
|
---|
152 | reply = new FilePath(path);
|
---|
153 | free(path);
|
---|
154 | }
|
---|
155 | registry_closekey(key);
|
---|
156 | }
|
---|
157 | return reply;
|
---|
158 | }
|
---|