Windows: permit aklog to build with krb4 support and roken
[openafs.git] / src / WINNT / afssvrmgr / svrmgr.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 #include <winsock2.h>
11 #include <ws2tcpip.h>
12
13 extern "C" {
14 #include <afs/param.h>
15 #include <afs/stds.h>
16 }
17
18 #include "svrmgr.h"
19 #include "svr_col.h"
20 #include "svc_col.h"
21 #include "agg_col.h"
22 #include "set_col.h"
23 #include "svr_security.h"
24 #include "creds.h"
25 #include "propcache.h"
26 #include "action.h"
27 #include "subset.h"
28 #include "messages.h"
29 #include "cmdline.h"
30
31 extern "C" {
32 #include <afs/afs_AdminErrors.h>
33 } // extern "C"
34
35
36 /*
37  * DEFINITIONS ________________________________________________________________
38  *
39  */
40
41
42 /*
43  * VARIABLES __________________________________________________________________
44  *
45  */
46
47 GLOBALS g;
48 GLOBALS_RESTORED gr;
49
50
51 /*
52  * PROTOTYPES _________________________________________________________________
53  *
54  */
55
56 BOOL InitApplication (HINSTANCE hInst, LPTSTR pszCmdLine, int nCmdShow);
57 void ExitApplication (void);
58
59
60 /*
61  * ROUTINES ___________________________________________________________________
62  *
63  */
64
65 extern "C" int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pszCmdLineA, int nCmdShow)
66 {
67    LPTSTR pszCmdLine = AnsiToString (pszCmdLineA);
68
69    if (InitApplication (hInst, pszCmdLine, nCmdShow))
70       {
71       AfsAppLib_MainPump();
72
73       ExitApplication();
74       }
75
76    FreeString (pszCmdLine, pszCmdLineA);
77    return g.rc;
78 }
79
80
81 BOOL InitApplication (HINSTANCE hInst, LPTSTR pszCmdLine, int nCmdShow)
82 {
83    TaLocale_LoadCorrespondingModule (hInst);
84
85    memset (&g, 0x00, sizeof(g));
86    g.hInst = hInst;
87    g.hAccel = TaLocale_LoadAccelerators (ACCEL_MAIN);
88
89    HWND hPrevious;
90    TCHAR szTitle[ cchRESOURCE ];
91    GetString (szTitle, IDS_APP_TITLE);
92    if ((hPrevious = FindWindow (TEXT("AFSManagerClass"), szTitle)) != NULL)
93       {
94       SetFocus (hPrevious);
95       SendMessage (hPrevious, WM_SHOW_YOURSELF, 0, 0);
96       return FALSE;
97       }
98
99    AfsAppLib_SetAppName(szTitle);
100    AfsAppLib_SetPumpRoutine(PumpMessage);
101
102    TASKQUEUE_PARAMS tqp;
103    memset (&tqp, 0x00, sizeof(tqp));
104    tqp.nThreadsMax = 10;
105    tqp.fnCreateTaskPacket = CreateTaskPacket;
106    tqp.fnPerformTask = PerformTask;
107    tqp.fnFreeTaskPacket = FreeTaskPacket;
108    AfsAppLib_InitTaskQueue (&tqp);
109
110    Main_ConfigureHelp();
111
112    // Determine how the app is supposed to look--that is, remember what it
113    // looked like last time, and if there was no "last time", pick some
114    // decent defaults.
115    //
116    if (!RestoreSettings (REGSTR_SETTINGS_BASE, REGSTR_SETTINGS_PATH, REGVAL_SETTINGS, &gr, sizeof(gr), wVerGLOBALS_RESTORED))
117       {
118       memset (&gr, 0x00, sizeof(gr));
119       SetRectEmpty (&gr.rMain);
120       SetRectEmpty (&gr.rMainPreview);
121       SetRectEmpty (&gr.rServerLast);
122       SetRectEmpty (&gr.rViewLog);
123       SetRectEmpty (&gr.rActions);
124
125       gr.fPreview = TRUE;
126       gr.fVert = TRUE;
127       gr.fActions = FALSE;
128
129       gr.tabLast = tabFILESETS;
130
131       Server_SetDefaultView_Horz (&gr.diHorz.viewSvr);
132       Server_SetDefaultView_Vert (&gr.diVert.viewSvr);
133       Services_SetDefaultView (&gr.viewSvc);
134       Aggregates_SetDefaultView (&gr.viewAgg);
135       Filesets_SetDefaultView (&gr.viewSet);
136       Replicas_SetDefaultView (&gr.viewRep);
137       Action_SetDefaultView (&gr.viewAct);
138       Server_Key_SetDefaultView (&gr.viewKey);
139
140       gr.diHorz.cSplitter = -100;
141       gr.diVert.cSplitter = -89;
142
143       gr.cbQuotaUnits = cb1KB;
144
145       gr.fOpenMonitors = TRUE;
146       gr.fCloseUnmonitors = TRUE;
147       gr.fServerLongNames = FALSE;
148       gr.fDoubleClickOpens = 2;
149       gr.fWarnBadCreds = TRUE;
150
151       gr.ivSvr = ivSTATUS;
152       gr.ivAgg = ivSTATUS;
153       gr.ivSet = ivSTATUS;
154       gr.ivSvc = ivSTATUS;
155       }
156
157    ULONG status;
158    if (!AfsClass_Initialize (&status))
159       {
160       if (status == ADMCLIENTCANTINITAFSLOCATION)
161          ImmediateErrorDialog (status, IDS_ERROR_CANT_INIT_AFSCLASS_INSTALL);
162       else
163          ImmediateErrorDialog (status, IDS_ERROR_CANT_INIT_AFSCLASS_UNKNOWN);
164       return FALSE;
165       }
166
167    AfsClass_RequestLongServerNames (gr.fServerLongNames);
168    AfsClass_SpecifyRefreshDomain (AFSCLASS_WANT_VOLUMES);
169
170    // Create a notification object for the AFSClass library, so that it can
171    // let us know when anything changes.  The notification handler we'll
172    // install will take requests from the rest of the SVRMGR package and
173    // forward notifications around to whichever windows are actually
174    // interested.
175    //
176    CreateNotificationDispatch();
177
178    // Create a few variations on WC_DIALOG, so we get appropriate icons on
179    // our windows.
180    //
181    WNDCLASS wc;
182    GetClassInfo (THIS_HINST, MAKEINTRESOURCE( WC_DIALOG ), &wc);
183    wc.hInstance = THIS_HINST;
184    wc.hIcon = TaLocale_LoadIcon (IDI_MAIN);
185    wc.lpszClassName = TEXT("AFSManagerClass");
186    wc.style |= CS_GLOBALCLASS;
187    RegisterClass (&wc);
188
189    GetClassInfo (THIS_HINST, MAKEINTRESOURCE( WC_DIALOG ), &wc);
190    wc.hInstance = THIS_HINST;
191    wc.hIcon = TaLocale_LoadIcon (IDI_SERVER);
192    wc.lpszClassName = TEXT("ServerWindowClass");
193    wc.style |= CS_GLOBALCLASS;
194    RegisterClass (&wc);
195
196    // Okay, the big step: create the main window (ie, the servers list).
197    // Note that it doesn't get shown yet!
198    //
199    CMDLINEOP op = ParseCommandLine (pszCmdLine);
200    if (op == opCLOSEAPP)
201       return FALSE;
202
203    if (op == opLOOKUPERRORCODE)
204       {
205       Help_FindError();
206       return FALSE;
207       }
208
209    g.hMain = ModelessDialog (IDD_MAIN, NULL, (DLGPROC)Main_DialogProc);
210    if (g.hMain == NULL)
211       return FALSE;
212
213    if (op != opNOCELLDIALOG)
214       {
215       if (OpenCellDialog() != IDOK)
216          return FALSE;
217       }
218
219    return TRUE;
220 }
221
222
223 void ExitApplication (void)
224 {
225 }
226
227
228 void Quit (int rc)
229 {
230    if (g.hMain && IsWindow(g.hMain))
231       {
232       if (gr.fPreview && !gr.fVert)
233          FL_StoreView (GetDlgItem (g.hMain, IDC_SERVERS), &gr.diHorz.viewSvr);
234       else
235          FL_StoreView (GetDlgItem (g.hMain, IDC_SERVERS), &gr.diVert.viewSvr);
236
237       WINDOWPLACEMENT wpl;
238       wpl.length = sizeof(wpl);
239       if (GetWindowPlacement (g.hMain, &wpl))
240          {
241          if (gr.fPreview)
242             gr.rMainPreview = wpl.rcNormalPosition;
243          else
244             gr.rMain = wpl.rcNormalPosition;
245          }
246       }
247
248    StoreSettings (REGSTR_SETTINGS_BASE, REGSTR_SETTINGS_PATH, REGVAL_SETTINGS, &gr, sizeof(gr), wVerGLOBALS_RESTORED);
249
250    if (Subsets_SaveIfDirty (g.sub))
251       {
252       if (Action_fAnyActive())  // just *pretend* to close the app
253          {
254          Action_WindowToTop (FALSE);
255          ShowWindow (g.hMain, SW_HIDE);
256          }
257       else
258          {
259          g.rc = rc;
260          PostQuitMessage (g.rc);
261          }
262       }
263 }
264
265
266 void PumpMessage (MSG *lpm)
267 {
268    if (g.hMain && IsWindow (g.hMain))
269       {
270       if (GetActiveWindow())
271          {
272          if (TranslateAccelerator (GetActiveWindow(), g.hAccel, lpm))
273             return;
274          }
275       }
276
277    if (!IsMemoryManagerMessage (lpm))
278       {
279       TranslateMessage (lpm);
280       DispatchMessage (lpm);
281       }
282 }
283
284
285 BOOL cdecl StartThread (DWORD (WINAPI *lpfnStart)(PVOID lp), ...)
286 {
287    va_list   arg;
288    va_start (arg, lpfnStart);
289    PVOID     lp = va_arg (arg, PVOID);
290
291    DWORD dwThreadID;
292    HANDLE hThread;
293
294    if ((hThread = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)lpfnStart, lp, 0, &dwThreadID)) == NULL)
295       return FALSE;
296
297    SetThreadPriority (hThread, THREAD_PRIORITY_BELOW_NORMAL);
298    return TRUE;
299 }
300