windows-updates-including-dont-lose-tokens-20011110
[openafs.git] / src / WINNT / client_creds / main.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 extern "C" {
11 #include <afs/param.h>
12 #include <afs/stds.h>
13 }
14
15 #include "afscreds.h"
16 #include "..\afsreg\afsreg.h" // So we can see if the server's installed
17 #include "drivemap.h"
18
19
20 /*
21  * DEFINITIONS ________________________________________________________________
22  *
23  */
24
25 const TCHAR cszCLASSNAME[] = TEXT("AfsCreds");
26
27
28 /*
29  * VARIABLES __________________________________________________________________
30  *
31  */
32
33 GLOBALS g;
34
35
36 /*
37  * PROTOTYPES _________________________________________________________________
38  *
39  */
40
41 BOOL InitApp (LPSTR pszCmdLineA);
42 void ExitApp (void);
43 void Quit (void);
44 void PumpMessage (MSG *pmsg);
45 BOOL IsServerInstalled (void);
46
47
48 /*
49  * ROUTINES ___________________________________________________________________
50  *
51  */
52
53 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR pCmdLine, int nCmdShow)
54 {
55    Shortcut_Init();
56    TaLocale_LoadCorrespondingModule (hInst);
57
58    if (InitApp (pCmdLine))
59       {
60       MSG msg;
61       while (GetMessage (&msg, NULL, 0, 0) > 0)
62          {
63          PumpMessage (&msg);
64          }
65
66       ExitApp();
67       }
68
69    Shortcut_Exit();
70    return 0;
71 }
72
73
74 BOOL InitApp (LPSTR pszCmdLineA)
75 {
76    BOOL fShow = FALSE;
77    BOOL fQuiet = FALSE;
78    BOOL fExit = FALSE;
79    BOOL fInstall = FALSE;
80    BOOL fUninstall = FALSE;
81
82    // Parse the command-line
83    //
84    while (pszCmdLineA && *pszCmdLineA)
85       {
86       if ((*pszCmdLineA != '-') && (*pszCmdLineA != '/'))
87          break;
88
89       switch (*(++pszCmdLineA))
90          {
91          case 's':
92          case 'S':
93             fShow = TRUE;
94             break;
95
96          case 'q':
97          case 'Q':
98             fQuiet = TRUE;
99             break;
100
101          case 'e':
102          case 'E':
103             fExit = TRUE;
104             break;
105
106          case 'i':
107          case 'I':
108             fInstall = TRUE;
109             break;
110
111          case 'u':
112          case 'U':
113             fUninstall = TRUE;
114             break;
115                  case ':':
116                          MapShareName(pszCmdLineA);
117                          break;
118          case 'x':
119          case 'X':
120                          DoMapShare();
121                          return 0;
122          }
123
124       while (*pszCmdLineA && (*pszCmdLineA != ' '))
125          ++pszCmdLineA;
126           if (*pszCmdLineA==' ') ++pszCmdLineA;
127       }
128
129    if (fInstall)
130       Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup = TRUE);
131    else if (fUninstall)
132       Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup = FALSE);
133
134    if (fInstall)
135       {
136       HKEY hk;
137       if (RegCreateKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
138          {
139          DWORD dwSize = sizeof(g.fStartup);
140          DWORD dwType = REG_DWORD;
141          RegSetValueEx (hk, TEXT("ShowTrayIcon"), NULL, dwType, (PBYTE)&g.fStartup, dwSize);
142          RegCloseKey (hk);
143          }
144       }
145
146    // Only show up if there's not another version of this app around already.
147    //
148    for (HWND hSearch = GetWindow (GetDesktopWindow(), GW_CHILD);
149         hSearch && IsWindow(hSearch);
150         hSearch = GetWindow (hSearch, GW_HWNDNEXT))
151       {
152       TCHAR szClassName[ cchRESOURCE ];
153       if (GetClassName (hSearch, szClassName, cchRESOURCE))
154          {
155          if (!lstrcmpi (szClassName, cszCLASSNAME))
156             {
157             if (fShow)
158                PostMessage (hSearch, WM_COMMAND, M_ACTIVATE, 0);
159             else if (fExit)
160                PostMessage (hSearch, WM_COMMAND, M_TERMINATE_NOW, 0);
161             else if (fUninstall)
162                PostMessage (hSearch, WM_COMMAND, M_TERMINATE_NOW, 0);
163             return FALSE;
164             }
165          }
166       }
167
168    if (fExit || fUninstall || fInstall)
169       return FALSE;
170
171    // Initialize our global variables and window classes
172    //
173    memset (&g, 0x00, sizeof(g));
174    g.fStartup = TRUE;
175
176    HKEY hk;
177    if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
178       {
179       DWORD dwSize = sizeof(g.fStartup);
180       DWORD dwType = REG_DWORD;
181       RegQueryValueEx (hk, TEXT("ShowTrayIcon"), NULL, &dwType, (PBYTE)&g.fStartup, &dwSize);
182       RegCloseKey (hk);
183       }
184
185    Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);
186
187    // Is this Windows NT?
188    //
189    OSVERSIONINFO Version;
190    memset (&Version, 0x00, sizeof(Version));
191    Version.dwOSVersionInfoSize = sizeof(Version);
192    if (GetVersionEx (&Version))
193       g.fIsWinNT = (Version.dwPlatformId == VER_PLATFORM_WIN32_NT) ? TRUE : FALSE;
194
195    if (!g.fIsWinNT)
196       lstrcpy (g.szHelpFile, TEXT("afs-light.hlp"));
197    else
198       lstrcpy (g.szHelpFile, TEXT("afs-nt.hlp"));
199
200    // Initialize winsock etc
201    //
202    WSADATA Data;
203    WSAStartup (0x0101, &Data);
204
205    InitCommonControls();
206    RegisterCheckListClass();
207
208    // Create a main window. All further initialization will be done during
209    // processing of WM_INITDIALOG.
210    //
211    WNDCLASS wc;
212    if (!GetClassInfo (NULL, WC_DIALOG, &wc))   // Get dialog class info
213       return FALSE;
214    wc.hInstance = THIS_HINST;
215    wc.hIcon = TaLocale_LoadIcon (IDI_MAIN);
216    wc.lpszClassName = cszCLASSNAME;
217    wc.style |= CS_GLOBALCLASS;
218    RegisterClass(&wc);
219
220    g.hMain = ModelessDialog (IDD_MAIN, NULL, (DLGPROC)Main_DlgProc);
221    if (g.hMain == NULL)
222       return FALSE;
223
224    // If the service isn't started yet, show our startup wizard.
225    //
226    if (!IsServiceRunning() && !fQuiet)
227       {
228       if (!g.fIsWinNT)
229          Message (MB_ICONHAND, IDS_UNCONFIG_TITLE_95, IDS_UNCONFIG_DESC_95);
230       else if (IsServiceConfigured())
231          ShowStartupWizard();
232       else if (!IsServerInstalled())
233          Message (MB_ICONHAND, IDS_UNCONFIG_TITLE, IDS_UNCONFIG_DESC);
234       }
235    if (IsServiceRunning() && fShow)
236       {
237       Main_Show (TRUE);
238       }
239
240    return TRUE;
241 }
242
243
244 void ExitApp (void)
245 {
246    g.hMain = NULL;
247 }
248
249
250 void PumpMessage (MSG *pmsg)
251 {
252    if (!IsMemoryManagerMessage (pmsg))
253       {
254       if (!IsDialogMessage (g.hMain, pmsg))
255          {
256          TranslateMessage (pmsg);
257          DispatchMessage (pmsg);
258          }
259       }
260 }
261
262
263 void Quit (void)
264 {
265    if (IsWindow (g.hMain))
266       {
267       ChangeTrayIcon (NIM_DELETE);
268       DestroyWindow (g.hMain);
269       }
270    PostQuitMessage (0);
271 }
272
273
274 BOOL IsServerInstalled (void)
275 {
276    BOOL fInstalled = FALSE;
277
278    TCHAR szKey[] = AFSREG_SVR_SVC_KEY;
279    LPCTSTR pch = lstrchr (szKey, TEXT('\\'));
280
281    HKEY hk;
282    if (RegOpenKey (HKEY_LOCAL_MACHINE, &pch[1], &hk) == 0)
283       {
284       fInstalled = TRUE;
285       RegCloseKey (hk);
286       }
287
288    return fInstalled;
289 }
290