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