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