1 | //----------------------------------------------------------- |
---|
2 | // Copyright Christian Arnault LAL-Orsay CNRS |
---|
3 | // arnault@lal.in2p3.fr |
---|
4 | // See the complete license in cmt_license.txt "http://www.cecill.info". |
---|
5 | //----------------------------------------------------------- |
---|
6 | |
---|
7 | #include <stdio.h> |
---|
8 | |
---|
9 | //--------------------------------------------------------------------- |
---|
10 | #include <windows.h> // required for all Windows applications |
---|
11 | #include "install_resource.h" |
---|
12 | #include <stdio.h> |
---|
13 | #include <stdlib.h> |
---|
14 | #include <stdarg.h> |
---|
15 | #include <io.h> |
---|
16 | #include <sys/types.h> |
---|
17 | #include <sys/stat.h> |
---|
18 | |
---|
19 | #include "install_utils.h" |
---|
20 | #include "cmt_system.h" |
---|
21 | //--------------------------------------------------------------------- |
---|
22 | |
---|
23 | //------------------------------------------------------ |
---|
24 | static cmt_string dummy; |
---|
25 | //------------------------------------------------------ |
---|
26 | |
---|
27 | //--------------------------------------------------------------------- |
---|
28 | // |
---|
29 | // Class definitions |
---|
30 | // |
---|
31 | //--------------------------------------------------------------------- |
---|
32 | |
---|
33 | //--------------------------------------------------------------------- |
---|
34 | class Install; |
---|
35 | //--------------------------------------------------------------------- |
---|
36 | |
---|
37 | //--------------------------------------------------------------------- |
---|
38 | class Install |
---|
39 | { |
---|
40 | public: |
---|
41 | static Install& instance (); |
---|
42 | BOOL initialize (HINSTANCE instance, |
---|
43 | HINSTANCE previous_instance, |
---|
44 | int show_mode); |
---|
45 | BOOL run (); |
---|
46 | |
---|
47 | const char* m_application_name; |
---|
48 | const char* m_title; |
---|
49 | |
---|
50 | HINSTANCE m_instance; |
---|
51 | HWND m_window; |
---|
52 | |
---|
53 | HWND m_dialog; |
---|
54 | |
---|
55 | private: |
---|
56 | |
---|
57 | static LRESULT CALLBACK process_messages (HWND window, |
---|
58 | UINT message_type, |
---|
59 | WPARAM parameter1, |
---|
60 | LPARAM parameter2); |
---|
61 | static LRESULT CALLBACK handle_dialog (HWND dialog, |
---|
62 | UINT message_type, |
---|
63 | WPARAM parameter1, |
---|
64 | LPARAM parameter2); |
---|
65 | |
---|
66 | Install (); |
---|
67 | void init (HWND window); |
---|
68 | void install (); |
---|
69 | void uninstall (); |
---|
70 | |
---|
71 | HANDLE* m_objects; |
---|
72 | int m_object_count; |
---|
73 | |
---|
74 | WEdit m_root; |
---|
75 | WEdit m_version; |
---|
76 | WEdit m_config; |
---|
77 | WEdit m_site; |
---|
78 | WList m_path; |
---|
79 | }; |
---|
80 | //--------------------------------------------------------------------- |
---|
81 | |
---|
82 | |
---|
83 | |
---|
84 | //--------------------------------------------------------------------- |
---|
85 | int APIENTRY WinMain (HINSTANCE instance, |
---|
86 | HINSTANCE previous_instance, |
---|
87 | LPSTR command_line, |
---|
88 | int show_mode) |
---|
89 | //--------------------------------------------------------------------- |
---|
90 | { |
---|
91 | Install& install = Install::instance (); |
---|
92 | |
---|
93 | install.initialize (instance, previous_instance, show_mode); |
---|
94 | |
---|
95 | return install.run (); |
---|
96 | |
---|
97 | UNREFERENCED_PARAMETER (command_line); |
---|
98 | return (0); |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | //--------------------------------------------------------------------- |
---|
104 | // |
---|
105 | // Class implementations |
---|
106 | // |
---|
107 | //--------------------------------------------------------------------- |
---|
108 | |
---|
109 | //--------------------------------------------------------------------- |
---|
110 | Install& Install::instance () |
---|
111 | { |
---|
112 | static Install the_instance; |
---|
113 | return (the_instance); |
---|
114 | } |
---|
115 | |
---|
116 | BOOL Install::initialize (HINSTANCE instance, |
---|
117 | HINSTANCE previous_instance, |
---|
118 | int show_mode) |
---|
119 | { |
---|
120 | WNDCLASS window_class; |
---|
121 | |
---|
122 | // Fill in window class structure with parameters that describe the |
---|
123 | // main window. |
---|
124 | |
---|
125 | if (!previous_instance) |
---|
126 | { |
---|
127 | window_class.style = CS_OWNDC; |
---|
128 | window_class.lpfnWndProc = (WNDPROC) process_messages; |
---|
129 | window_class.cbClsExtra = 0; |
---|
130 | window_class.cbWndExtra = 0; |
---|
131 | window_class.hInstance = instance; |
---|
132 | window_class.hIcon = LoadIcon (instance, m_application_name); |
---|
133 | window_class.hCursor = LoadCursor (NULL, IDC_ARROW); |
---|
134 | window_class.hbrBackground = (HBRUSH) (COLOR_MENU+1); |
---|
135 | window_class.lpszMenuName = "IDR_MENU"; |
---|
136 | window_class.lpszClassName = m_application_name; |
---|
137 | |
---|
138 | if (!RegisterClass (&window_class)) return (FALSE); |
---|
139 | } |
---|
140 | |
---|
141 | m_instance = instance; |
---|
142 | |
---|
143 | m_window = CreateWindow (m_application_name, |
---|
144 | m_title, |
---|
145 | WS_OVERLAPPEDWINDOW, |
---|
146 | CW_USEDEFAULT, 0, 440, 590, |
---|
147 | NULL, |
---|
148 | NULL, |
---|
149 | m_instance, |
---|
150 | NULL); |
---|
151 | |
---|
152 | // If window could not be created, return "failure" |
---|
153 | |
---|
154 | if (!m_window) |
---|
155 | { |
---|
156 | return (FALSE); |
---|
157 | } |
---|
158 | |
---|
159 | ShowWindow (m_window, show_mode); |
---|
160 | UpdateWindow (m_window); |
---|
161 | |
---|
162 | return (TRUE); |
---|
163 | } |
---|
164 | |
---|
165 | BOOL Install::run () |
---|
166 | { |
---|
167 | MSG message; |
---|
168 | DWORD result; |
---|
169 | HANDLE objects[1]; |
---|
170 | int count = 0; |
---|
171 | |
---|
172 | while (TRUE) |
---|
173 | { |
---|
174 | result = MsgWaitForMultipleObjects (count, objects, FALSE, |
---|
175 | INFINITE, QS_ALLINPUT); |
---|
176 | if (result == (WAIT_OBJECT_0 + count)) |
---|
177 | { |
---|
178 | while (PeekMessage (&message, NULL, 0, 0, PM_REMOVE)) |
---|
179 | { |
---|
180 | if ((m_dialog == NULL) || |
---|
181 | !IsDialogMessage (m_dialog, &message)) |
---|
182 | { |
---|
183 | if (!TranslateAccelerator (message.hwnd, NULL, &message)) |
---|
184 | { |
---|
185 | if (message.message == WM_QUIT) return (0); |
---|
186 | |
---|
187 | TranslateMessage (&message); |
---|
188 | DispatchMessage (&message); |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | } |
---|
193 | else |
---|
194 | { |
---|
195 | // For future async objects |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | return (message.wParam); |
---|
200 | } |
---|
201 | |
---|
202 | Install::Install () : m_application_name ("install"), m_title ("install") |
---|
203 | { |
---|
204 | m_dialog = 0; |
---|
205 | m_objects = 0; |
---|
206 | m_object_count = 0; |
---|
207 | } |
---|
208 | |
---|
209 | void Install::init (HWND window) |
---|
210 | { |
---|
211 | m_window = window; |
---|
212 | |
---|
213 | m_dialog = CreateDialog (m_instance, |
---|
214 | MAKEINTRESOURCE(IDD_DIALOG1), |
---|
215 | m_window, |
---|
216 | (DLGPROC) handle_dialog); |
---|
217 | |
---|
218 | m_root.init (m_dialog, IDC_ROOT); |
---|
219 | m_version.init (m_dialog, IDC_VERSION); |
---|
220 | m_config.init (m_dialog, IDC_CONFIG); |
---|
221 | m_site.init (m_dialog, IDC_SITE); |
---|
222 | m_path.init (m_dialog, IDC_PATH); |
---|
223 | |
---|
224 | cmt_string root = CmtSystem::pwd (); |
---|
225 | cmt_string v; |
---|
226 | cmt_string p; |
---|
227 | |
---|
228 | for (;;) |
---|
229 | { |
---|
230 | CmtSystem::basename (root, v); |
---|
231 | CmtSystem::dirname (root, root); |
---|
232 | CmtSystem::basename (root, p); |
---|
233 | if ((p == "Cmt") || (p == "CMT") || (p == "cmt")) break; |
---|
234 | } |
---|
235 | |
---|
236 | CmtSystem::dirname (root, root); |
---|
237 | |
---|
238 | m_root.set (root); |
---|
239 | m_version.set (v); |
---|
240 | m_config.set ("VisualC"); |
---|
241 | m_site.set (CmtSystem::get_cmt_site ()); |
---|
242 | |
---|
243 | CmtSystem::cmt_string_vector paths; |
---|
244 | CmtSystem::cmt_string_vector filtered_paths; |
---|
245 | CmtSystem::cmt_string_vector path_sources; |
---|
246 | |
---|
247 | CmtSystem::get_cmt_paths (paths, path_sources); |
---|
248 | |
---|
249 | for (int i = 0; i < paths.size (); i++) |
---|
250 | { |
---|
251 | const cmt_string& s = paths[i]; |
---|
252 | if (s == root) continue; |
---|
253 | cmt_string& f = filtered_paths.add (); |
---|
254 | f = s; |
---|
255 | } |
---|
256 | |
---|
257 | m_path.set (filtered_paths); |
---|
258 | } |
---|
259 | |
---|
260 | void Install::install () |
---|
261 | { |
---|
262 | cmt_string root = m_root.get (); |
---|
263 | cmt_string version = m_version.get (); |
---|
264 | cmt_string config = m_config.get (); |
---|
265 | cmt_string site = m_site.get (); |
---|
266 | CmtSystem::cmt_string_vector paths; |
---|
267 | m_path.get (paths); |
---|
268 | |
---|
269 | LONG status; |
---|
270 | HKEY key = 0; |
---|
271 | HKEY subkey = 0; |
---|
272 | DWORD disposition; |
---|
273 | |
---|
274 | status = RegCreateKeyEx (HKEY_LOCAL_MACHINE, "Software\\CMT", 0, "", |
---|
275 | REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &disposition); |
---|
276 | if (status == ERROR_SUCCESS) |
---|
277 | { |
---|
278 | DWORD length; |
---|
279 | |
---|
280 | length = root.size (); |
---|
281 | status = RegSetValueEx (key, "root", 0, REG_SZ, (LPBYTE) root.c_str (), length); |
---|
282 | |
---|
283 | length = version.size (); |
---|
284 | status = RegSetValueEx (key, "version", 0, REG_SZ, (LPBYTE) version.c_str (), length); |
---|
285 | |
---|
286 | length = config.size (); |
---|
287 | status = RegSetValueEx (key, "config", 0, REG_SZ, (LPBYTE) config.c_str (), length); |
---|
288 | |
---|
289 | length = site.size (); |
---|
290 | status = RegSetValueEx (key, "site", 0, REG_SZ, (LPBYTE) site.c_str (), length); |
---|
291 | |
---|
292 | status = RegDeleteKey (key, "path"); |
---|
293 | |
---|
294 | status = RegCreateKeyEx (key, "path", 0, "", |
---|
295 | REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &subkey, &disposition); |
---|
296 | if (status == ERROR_SUCCESS) |
---|
297 | { |
---|
298 | DWORD index = 0; |
---|
299 | char name[256]; |
---|
300 | char temp[256]; |
---|
301 | |
---|
302 | for (;;) |
---|
303 | { |
---|
304 | DWORD name_length = sizeof (name) - 1; |
---|
305 | DWORD length = sizeof (temp) - 1; |
---|
306 | DWORD type; |
---|
307 | status = RegEnumValue (subkey, index, |
---|
308 | name, &name_length, 0, &type, |
---|
309 | (LPBYTE) temp, &length); |
---|
310 | if ((status == ERROR_SUCCESS) || |
---|
311 | (status == 234)) |
---|
312 | { |
---|
313 | RegDeleteValue (subkey, name); |
---|
314 | } |
---|
315 | |
---|
316 | if (status == 259) |
---|
317 | { |
---|
318 | break; |
---|
319 | } |
---|
320 | |
---|
321 | index++; |
---|
322 | } |
---|
323 | |
---|
324 | for (int i = 0; i < paths.size (); i++) |
---|
325 | { |
---|
326 | const cmt_string& s = paths[i]; |
---|
327 | |
---|
328 | sprintf (temp, "#%d", i); |
---|
329 | |
---|
330 | length = s.size (); |
---|
331 | status = RegSetValueEx (subkey, temp, 0, REG_SZ, (LPBYTE) s.c_str (), length); |
---|
332 | } |
---|
333 | |
---|
334 | status = RegCloseKey (subkey); |
---|
335 | } |
---|
336 | |
---|
337 | status = RegCloseKey (key); |
---|
338 | } |
---|
339 | |
---|
340 | cmt_string mgr = root; |
---|
341 | mgr += "\\CMT\\"; |
---|
342 | mgr += version; |
---|
343 | mgr += "\\mgr"; |
---|
344 | |
---|
345 | CmtSystem::cd (mgr); |
---|
346 | CmtSystem::execute ("INSTALL"); |
---|
347 | } |
---|
348 | |
---|
349 | void Install::uninstall () |
---|
350 | { |
---|
351 | LONG status; |
---|
352 | HKEY key = 0; |
---|
353 | HKEY subkey = 0; |
---|
354 | |
---|
355 | status = RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Software\\CMT", 0, KEY_READ, &key); |
---|
356 | if (status == ERROR_SUCCESS) |
---|
357 | { |
---|
358 | status = RegDeleteKey (key, "root"); |
---|
359 | status = RegDeleteKey (key, "version"); |
---|
360 | status = RegDeleteKey (key, "config"); |
---|
361 | status = RegDeleteKey (key, "site"); |
---|
362 | status = RegDeleteKey (key, "path"); |
---|
363 | status = RegCloseKey (key); |
---|
364 | } |
---|
365 | |
---|
366 | status = RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ, &key); |
---|
367 | if (status == ERROR_SUCCESS) |
---|
368 | { |
---|
369 | status = RegDeleteKey (key, "CMT"); |
---|
370 | status = RegCloseKey (key); |
---|
371 | } |
---|
372 | } |
---|
373 | |
---|
374 | LRESULT CALLBACK Install::process_messages (HWND window, |
---|
375 | UINT message_type, |
---|
376 | WPARAM parameter1, |
---|
377 | LPARAM parameter2) |
---|
378 | { |
---|
379 | Install& install = Install::instance (); |
---|
380 | |
---|
381 | int menu_item; |
---|
382 | static HPEN blue_pen; |
---|
383 | static HPEN red_pen; |
---|
384 | |
---|
385 | SetLastError (0); |
---|
386 | |
---|
387 | switch (message_type) |
---|
388 | { |
---|
389 | case WM_CREATE: |
---|
390 | { |
---|
391 | HDC context; |
---|
392 | LOGFONT log_font; |
---|
393 | HFONT font; |
---|
394 | |
---|
395 | context = GetDC (window); |
---|
396 | |
---|
397 | memset (&log_font, 0, sizeof(LOGFONT)); |
---|
398 | log_font.lfHeight = -72; |
---|
399 | strcpy ((LPSTR) &(log_font.lfFaceName), "arial"); |
---|
400 | |
---|
401 | font = CreateFontIndirect (&log_font); |
---|
402 | SelectObject (context, font); |
---|
403 | |
---|
404 | blue_pen = CreatePen (PS_SOLID, 1, RGB(0,0,255)); |
---|
405 | red_pen = CreatePen (PS_SOLID, 1, RGB(255,0,0)); |
---|
406 | } |
---|
407 | |
---|
408 | install.init (window); |
---|
409 | |
---|
410 | break; |
---|
411 | |
---|
412 | case WM_COMMAND: |
---|
413 | menu_item = LOWORD (parameter1); |
---|
414 | |
---|
415 | switch (menu_item) |
---|
416 | { |
---|
417 | case ID_FILE_QUIT: |
---|
418 | DestroyWindow (window); |
---|
419 | break; |
---|
420 | default: |
---|
421 | return (DefWindowProc (window, message_type, |
---|
422 | parameter1, parameter2)); |
---|
423 | } |
---|
424 | break; |
---|
425 | case WM_DESTROY: |
---|
426 | PostQuitMessage (0); |
---|
427 | break; |
---|
428 | default: |
---|
429 | return (DefWindowProc (window, message_type, parameter1, parameter2)); |
---|
430 | } |
---|
431 | return (0); |
---|
432 | } |
---|
433 | |
---|
434 | LRESULT CALLBACK Install::handle_dialog (HWND dialog, |
---|
435 | UINT message_type, |
---|
436 | WPARAM parameter1, |
---|
437 | LPARAM parameter2) |
---|
438 | { |
---|
439 | Install& install = Install::instance (); |
---|
440 | |
---|
441 | int menu_item; |
---|
442 | |
---|
443 | switch (message_type) |
---|
444 | { |
---|
445 | case WM_CREATE: |
---|
446 | break; |
---|
447 | case WM_SETFONT: |
---|
448 | break; |
---|
449 | case WM_INITDIALOG: |
---|
450 | { |
---|
451 | int x = 0; |
---|
452 | int y = 0; |
---|
453 | int width; |
---|
454 | int height; |
---|
455 | RECT rectangle; |
---|
456 | |
---|
457 | GetWindowRect (dialog, &rectangle); |
---|
458 | |
---|
459 | width = rectangle.right - rectangle.left; |
---|
460 | height = rectangle.bottom - rectangle.top; |
---|
461 | |
---|
462 | MoveWindow (install.m_window, x, y, width, height, TRUE); |
---|
463 | } |
---|
464 | |
---|
465 | break; |
---|
466 | case WM_COMMAND: |
---|
467 | menu_item = LOWORD (parameter1); |
---|
468 | |
---|
469 | switch (menu_item) |
---|
470 | { |
---|
471 | case IDC_INSTALL: |
---|
472 | install.install (); |
---|
473 | break; |
---|
474 | case IDC_UNINSTALL: |
---|
475 | install.uninstall (); |
---|
476 | break; |
---|
477 | case IDC_EDITPATH: |
---|
478 | { |
---|
479 | ListEditor editor (install.m_path); |
---|
480 | editor.run (install.m_instance, install.m_dialog); |
---|
481 | } |
---|
482 | break; |
---|
483 | case IDC_EXIT: |
---|
484 | DestroyWindow (install.m_window); |
---|
485 | break; |
---|
486 | } |
---|
487 | |
---|
488 | break; |
---|
489 | case WM_ACTIVATE: |
---|
490 | menu_item = LOWORD (parameter1); |
---|
491 | break; |
---|
492 | default: |
---|
493 | return (FALSE); |
---|
494 | } |
---|
495 | return (TRUE); |
---|
496 | } |
---|
497 | //--------------------------------------------------------------------- |
---|
498 | |
---|