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