ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/SearchInstaller/Worker.cpp
Revision: 48
Committed: 2003-02-05T14:47:24-08:00 (22 years, 4 months ago) by douglas
File size: 12015 byte(s)
Log Message:
Initial revision

File Contents

# Content
1 // Douglas Thrift's Search Engine Installer
2 //
3 // Douglas Thrift
4 //
5 // Worker.cpp
6
7 #include "Worker.h"
8
9 Worker::Worker()
10 {
11 OSVERSIONINFO os;
12 os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
13 GetVersionEx(&os);
14 nt = os.dwPlatformId == VER_PLATFORM_WIN32_NT ? true : false;
15
16 if (nt)
17 {
18 single = false;
19
20 HKEY hkey;
21 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, NULL, 0, KEY_WRITE, &hkey) !=
22 ERROR_SUCCESS)
23 {
24 single = true;
25 }
26 else
27 {
28 unsigned char* crackers = new unsigned char[2];
29 crackers[0] = '#';
30 crackers[1] = '\0';
31
32 if (RegSetValueEx(hkey, "cheese", 0, REG_SZ, crackers, 2)
33 != ERROR_SUCCESS)
34 {
35 single = true;
36 }
37
38 RegDeleteValue(hkey, "cheese");
39 }
40
41 RegCloseKey(hkey);
42 }
43 else
44 {
45 single = true;
46 }
47
48 templates = true;
49
50 char* programs = new char[MAX_PATH + 1];
51
52 SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, SHGFP_TYPE_CURRENT,
53 programs);
54
55 string search = "\\Douglas Thrift's Search Engine";
56 folder = programs + search;
57 delete [] programs;
58 }
59
60 void Worker::work(HWND bar)
61 {
62 prepare(bar);
63 files(bar);
64 shortcuts(bar);
65 registry(bar);
66
67 SendMessage(GetGrandParent(bar), PSM_PRESSBUTTON, (WPARAM)PSBTN_NEXT, 0);
68 }
69
70 void Worker::prepare(HWND bar)
71 {
72 SendMessage(bar, PBM_SETRANGE, 0, MAKELPARAM(0, 50));
73
74 ifstream fin("search.cgi.win");
75 ofstream fout("search.cgi");
76
77 string perl;
78
79 {
80 HKEY key;
81 const string BinDir = "BinDir";
82
83 if (RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Perl", 0, KEY_READ,
84 &key) == ERROR_SUCCESS)
85 {
86 char* value = new char[16383 + 1];
87 unsigned long valueSize = 16383 + 1;
88 unsigned long type;
89 unsigned char* data = new unsigned char[MAX_PATH + 1];
90 unsigned long dataSize = MAX_PATH + 1;
91
92 for (unsigned long index = 0; RegEnumValue(key, index, value,
93 &valueSize, NULL, &type, data, &dataSize) !=
94 ERROR_NO_MORE_ITEMS; index++)
95 {
96 if (value == BinDir && type == REG_SZ)
97 {
98 char* buffer = new char[dataSize + 1];
99 sprintf(buffer, "%s", data);
100 perl = buffer;
101 delete [] buffer;
102 break;
103 }
104 else
105 {
106 valueSize = 16383 + 1;
107 dataSize = MAX_PATH + 1;
108 }
109 }
110
111 delete [] value;
112 delete [] data;
113 }
114 else if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Perl", 0,
115 KEY_READ, &key) == ERROR_SUCCESS)
116 {
117 char* value = new char[16383 + 1];
118 unsigned long valueSize = 16383 + 1;
119 unsigned long type;
120 unsigned char* data = new unsigned char[MAX_PATH + 1];
121 unsigned long dataSize = MAX_PATH + 1;
122
123 for (unsigned long index = 0; RegEnumValue(key, index, value,
124 &valueSize, NULL, &type, data, &dataSize) !=
125 ERROR_NO_MORE_ITEMS; index++)
126 {
127 if (value == BinDir && type == REG_SZ)
128 {
129 char* buffer = new char[dataSize + 1];
130 sprintf(buffer, "%s", data);
131 perl = buffer;
132 delete [] buffer;
133 break;
134 }
135 else
136 {
137 valueSize = 16383 + 1;
138 dataSize = MAX_PATH + 1;
139 }
140 }
141
142 delete [] value;
143 delete [] data;
144 }
145
146 RegCloseKey(key);
147 SendMessage(bar, PBM_STEPIT, 0, 0);
148 }
149
150 CreateDirectory(folder.c_str(), NULL);
151
152 do
153 {
154 string line;
155 getline(fin, line);
156
157 if (line == "#!C:\\Perl\\bin\\perl.exe")
158 {
159 if (perl != "" && line != "#!" + perl)
160 {
161 line = "#!" + perl;
162 }
163
164 SendMessage(bar, PBM_STEPIT, 0, 0);
165 }
166 else if (line.find("my $root = ") == 0)
167 {
168 char* buffer = new char[MAX_PATH + 1];
169
170 GetShortPathName(folder.c_str(), buffer, MAX_PATH + 1);
171
172 string root = buffer;
173
174 delete [] buffer;
175
176 unsigned begin = 0;
177 do
178 {
179 unsigned slash = root.find('\\', begin);
180
181 if (slash == string::npos) break;
182
183 root.insert(slash, 1, '\\');
184
185 begin = slash + 2;
186 }
187 while (begin < root.length());
188
189 if (line != "my $root = \"" + root + "\";")
190 {
191 line = "my $root = \"" + root + "\";";
192 }
193
194 SendMessage(bar, PBM_STEPIT, 0, 0);
195 }
196
197 fout << line << (fin.good() ? "\n" : "");
198 }
199 while (fin.good());
200
201 fin.close();
202 fout.close();
203 SendMessage(bar, PBM_STEPIT, 0, 0);
204
205 if (nt)
206 {
207 fout.open("SearchPrompt.cmd");
208
209 fout << "@echo off\n"
210 << "set PATH=" << folder << "\\bin;%PATH%\n"
211 << "set PROMPT=[Search$SEngine]$$$S\n"
212 << "Search -help\n";
213
214 fout.close();
215 }
216 else
217 {
218 fout.open("SearchPrompt.bat");
219
220 char* buffer = new char[MAX_PATH + 1];
221
222 GetShortPathName(folder.c_str(), buffer, MAX_PATH + 1);
223
224 fout << "@echo off\n"
225 << "set PATH=" << buffer << "\\bin;%PATH%\n"
226 << "set PROMPT=[Search Engine]$$ $S\n"
227 << "Search -help\n";
228
229 delete [] buffer;
230
231 fout.close();
232 }
233
234 SendMessage(bar, PBM_STEPIT, 0, 0);
235 }
236
237 void Worker::files(HWND bar)
238 {
239 SendMessage(bar, PBM_SETPOS, 0, 0);
240 SendDlgItemMessage(GetParent(bar), IDC_PROGRESS_TEXT, WM_SETTEXT, 0,
241 (LPARAM)(LPCTSTR)"Installing files...");
242
243 vector<string> base;
244 base.push_back("LICENSE");
245 base.push_back("LICENSE.html");
246 base.push_back("README");
247 base.push_back("README.html");
248 base.push_back("osi-certified-90x75.png");
249 base.push_back("SearchUninstaller.exe");
250
251 vector<string> bin;
252 bin.push_back("Search.exe");
253 bin.push_back(nt ? "SearchPrompt.cmd" : "SearchPrompt.bat");
254 bin.push_back("SearchPrompt.ico");
255
256 vector<string> http;
257 http.push_back("search.cgi");
258 http.push_back("dtse_pb.png");
259
260 vector<string> data;
261 if (templates)
262 {
263 data.push_back("header.html");
264 data.push_back("body.html");
265 data.push_back("footer.html");
266 data.push_back("notfound.html");
267 data.push_back("pages.html");
268 }
269
270 SendMessage(bar, PBM_SETRANGE, 0, MAKELPARAM(0, (base.size() + bin.size() +
271 http.size() + data.size()) * 10));
272
273 for (unsigned index = 0; index < base.size(); index++)
274 {
275 CopyFile(base[index].c_str(), (folder + '\\' + base[index]).c_str(),
276 false);
277
278 SendMessage(bar, PBM_STEPIT, 0, 0);
279 }
280
281 CreateDirectory((folder + "\\bin").c_str(), NULL);
282
283 for (index = 0; index < bin.size(); index++)
284 {
285 CopyFile(bin[index].c_str(), (folder + "\\bin\\" + bin[index]).c_str(),
286 false);
287
288 SendMessage(bar, PBM_STEPIT, 0, 0);
289 }
290
291 CreateDirectory((folder + "\\http").c_str(), NULL);
292
293 for (index = 0; index < http.size(); index++)
294 {
295 CopyFile(http[index].c_str(), (folder + "\\http\\" +
296 http[index]).c_str(), false);
297
298 SendMessage(bar, PBM_STEPIT, 0, 0);
299 }
300
301 CreateDirectory((folder + "\\data").c_str(), NULL);
302
303 for (index = 0; index < data.size(); index++)
304 {
305 CopyFile(data[index].c_str(), (folder + "\\data\\" +
306 data[index]).c_str(), false);
307
308 SendMessage(bar, PBM_STEPIT, 0, 0);
309 }
310 }
311
312 void Worker::shortcuts(HWND bar)
313 {
314 SendMessage(bar, PBM_SETPOS, 0, 0);
315 SendDlgItemMessage(GetParent(bar), IDC_PROGRESS_TEXT, WM_SETTEXT, 0,
316 (LPARAM)(LPCTSTR)"Creating shortcuts...");
317
318 SendMessage(bar, PBM_SETRANGE, 0, MAKELPARAM(0, 40));
319
320 char* buffer = new char[MAX_PATH + 1];
321 SHGetFolderPath(GetGrandParent(bar), single ? CSIDL_PROGRAMS :
322 CSIDL_COMMON_PROGRAMS, NULL, SHGFP_TYPE_CURRENT, buffer);
323 string programs = buffer;
324 delete [] buffer;
325
326 CreateDirectory((programs + "\\Douglas Thrift\'s Search Engine").c_str(),
327 NULL);
328
329 IPersistFile* link;
330 IShellLink* prompt;
331
332 string description = string("Command Prompt for configuring Douglas Thrif")
333 +"t\'s Search Engine";
334
335 CoInitialize(NULL);
336 CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
337 IID_IShellLink, (LPVOID*)&prompt);
338
339 prompt->SetPath(getenv("ComSpec"));
340 if (nt)
341 {
342 string arguments = "/k \"" + folder + "\\bin\\SearchPrompt.cmd\"";
343
344 prompt->SetArguments(arguments.c_str());
345 prompt->SetDescription(description.c_str());
346 }
347 else
348 {
349 char* buffer = new char[MAX_PATH + 1];
350
351 GetShortPathName((folder + "\\bin\\SearchPrompt.bat").c_str(), buffer,
352 MAX_PATH + 1);
353
354 string arguments = string("/e:4096 /k ") + buffer;
355
356 delete [] buffer;
357
358 prompt->SetArguments(arguments.c_str());
359 prompt->SetDescription("Search Engine Prompt");
360 }
361 prompt->SetIconLocation((folder + "\\bin\\SearchPrompt.ico").c_str(), 0);
362 prompt->SetWorkingDirectory((folder + "\\data").c_str());
363 prompt->QueryInterface(IID_IPersistFile, (LPVOID*)&link);
364
365 unsigned short* file = new unsigned short[MAX_PATH + 1];
366
367 MultiByteToWideChar(CP_ACP, 0, (programs + "\\Douglas Thrift\'s Search Eng"
368 + "ine\\Search Engine Prompt." + (nt ? "lnk" : "pif")).c_str(), -1,
369 file, MAX_PATH + 1);
370
371 link->Save(file, true);
372 link->Release();
373 prompt->Release();
374 delete [] file;
375
376 SendMessage(bar, PBM_STEPIT, 0, 0);
377
378 IShellLink* license;
379
380 description = "Douglas Thrift\'s Search Engine License";
381
382 CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
383 IID_IShellLink, (LPVOID*)&license);
384
385 license->SetPath((folder + "\\LICENSE.html").c_str());
386 license->SetDescription(description.c_str());
387 license->QueryInterface(IID_IPersistFile, (LPVOID*)&link);
388
389 file = new unsigned short[MAX_PATH + 1];
390
391 MultiByteToWideChar(CP_ACP, 0, (programs + "\\Douglas Thrift\'s Search Eng"
392 + "ine\\License.lnk").c_str(), -1, file, MAX_PATH + 1);
393
394 link->Save(file, true);
395 link->Release();
396 license->Release();
397 delete [] file;
398
399 SendMessage(bar, PBM_STEPIT, 0, 0);
400
401 IShellLink* readme;
402
403 description = "Douglas Thrift\'s Search Engine ReadMe";
404
405 CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
406 IID_IShellLink, (LPVOID*)&readme);
407
408 readme->SetPath((folder + "\\README.html").c_str());
409 readme->SetDescription(description.c_str());
410 readme->QueryInterface(IID_IPersistFile, (LPVOID*)&link);
411
412 file = new unsigned short[MAX_PATH + 1];
413
414 MultiByteToWideChar(CP_ACP, 0, (programs + "\\Douglas Thrift\'s Search Eng"
415 + "ine\\ReadMe.lnk").c_str(), -1, file, MAX_PATH + 1);
416
417 link->Save(file, true);
418 link->Release();
419 readme->Release();
420 delete [] file;
421
422 SendMessage(bar, PBM_STEPIT, 0, 0);
423
424 IShellLink* uninstall;
425
426 description = "Uninstall Douglas Thrift\'s Search Engine";
427
428 CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
429 IID_IShellLink, (LPVOID*)&uninstall);
430
431 uninstall->SetPath((folder + "\\SearchUninstaller.exe").c_str());
432 uninstall->SetIconLocation((folder + "\\SearchUninstaller.exe").c_str(),
433 0);
434 uninstall->SetDescription(description.c_str());
435 uninstall->QueryInterface(IID_IPersistFile, (LPVOID*)&link);
436
437 file = new unsigned short[MAX_PATH + 1];
438
439 MultiByteToWideChar(CP_ACP, 0, (programs + "\\Douglas Thrift\'s Search Eng"
440 + "ine\\Uninstall.lnk").c_str(), -1, file, MAX_PATH + 1);
441
442 link->Save(file, true);
443 link->Release();
444 uninstall->Release();
445 delete [] file;
446
447 SendMessage(bar, PBM_STEPIT, 0, 0);
448 }
449
450 void Worker::registry(HWND bar)
451 {
452 SendMessage(bar, PBM_SETPOS, 0, 0);
453 SendDlgItemMessage(GetParent(bar), IDC_PROGRESS_TEXT, WM_SETTEXT, 0,
454 (LPARAM)(LPCTSTR)"Registering files...");
455
456 SendMessage(bar, PBM_SETRANGE, 0, MAKELPARAM(0, 90));
457
458 HKEY hkey;
459 string subkey = string("Software\\Microsoft\\Windows\\CurrentVersion\\Uni")
460 + "nstall\\Douglas Thrift\'s Search Engine";
461 RegCreateKeyEx(single && nt ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
462 subkey.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
463 &hkey, NULL);
464
465 vector<string> values;
466 vector<string> data;
467
468 values.push_back("Contact");
469 data.push_back("Douglas Thrift");
470
471 values.push_back("DisplayIcon");
472 data.push_back(folder + "\\bin\\Search.exe,0");
473
474 values.push_back("DisplayName");
475 data.push_back("Douglas Thrift\'s Search Engine");
476
477 values.push_back("DisplayVersion");
478 data.push_back("1.1");
479
480 values.push_back("InstallLocation");
481 data.push_back(folder);
482
483 values.push_back("Publisher");
484 data.push_back("DouglasThrift.net");
485
486 values.push_back("Readme");
487 data.push_back(folder + "\\README.html");
488
489 values.push_back("UninstallString");
490 data.push_back(folder + "\\SearchUninstaller.exe");
491
492 values.push_back("URLInfoAbout");
493 data.push_back("http://computers.douglasthrift.net/searchengine/");
494
495 for (unsigned index = 0; index < values.size(); index++)
496 {
497 unsigned char* buffer = new unsigned char[data[index].length() + 1];
498 for (unsigned number = 0; number < data[index].length(); number++)
499 {
500 buffer[number] = data[index][number];
501 }
502 buffer[number] = '\0';
503
504 RegSetValueEx(hkey, values[index].c_str(), 0, REG_SZ, buffer, number +
505 1);
506
507 delete [] buffer;
508
509 SendMessage(bar, PBM_STEPIT, 0, 0);
510 }
511
512 RegCloseKey(hkey);
513 }