source: CMT/v1r12p20020606/src/install.cxx @ 1

Last change on this file since 1 was 1, checked in by arnault, 19 years ago

Import all tags

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