Initial IBM OpenAFS 1.0 tree
[openafs.git] / src / WINNT / afsusrmgr / main.cpp
1 extern "C" {
2 #include <afs/param.h>
3 #include <afs/stds.h>
4 }
5
6 #include "TaAfsUsrMgr.h"
7 #include "messages.h"
8 #include "window.h"
9 #include "cmdline.h"
10 #include "usr_col.h"
11 #include "usr_create.h"
12 #include "usr_search.h"
13 #include "grp_col.h"
14 #include "grp_create.h"
15 #include "mch_col.h"
16 #include "mch_create.h"
17 #include "creds.h"
18 #include "action.h"
19
20
21 /*
22  * DEFINITIONS ________________________________________________________________
23  *
24  */
25
26
27 /*
28  * VARIABLES __________________________________________________________________
29  *
30  */
31
32 GLOBALS g;
33 GLOBALS_RESTORED gr;
34
35
36 /*
37  * PROTOTYPES _________________________________________________________________
38  *
39  */
40
41 BOOL InitApplication (HINSTANCE hInst, LPTSTR pszCmdLine, int nCmdShow);
42 void ExitApplication (void);
43
44
45 /*
46  * ROUTINES ___________________________________________________________________
47  *
48  */
49
50 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pszCmdLineA, int nCmdShow)
51 {
52    LPTSTR pszCmdLine = AnsiToString (pszCmdLineA);
53
54    if (InitApplication (hInst, pszCmdLine, nCmdShow))
55       {
56       AfsAppLib_MainPump();
57       }
58    ExitApplication();
59
60    FreeString (pszCmdLine, pszCmdLineA);
61    return g.rc;
62 }
63
64
65 BOOL InitApplication (HINSTANCE hInst, LPTSTR pszCmdLine, int nCmdShow)
66 {
67    TaLocale_LoadCorrespondingModule (hInst);
68
69    memset (&g, 0x00, sizeof(g));
70    g.hInst = hInst;
71    g.hAccel = TaLocale_LoadAccelerators (ACCEL_MAIN);
72
73    HWND hPrevious;
74    TCHAR szTitle[ cchRESOURCE ];
75    GetString (szTitle, IDS_APP_TITLE);
76    if ((hPrevious = FindWindow (TEXT("AFSAccountManagerClass"), szTitle)) != NULL)
77       {
78       SetFocus (hPrevious);
79       SendMessage (hPrevious, WM_SHOW_YOURSELF, 0, 0);
80       return FALSE;
81       }
82
83    AfsAppLib_SetAppName(szTitle);
84    AfsAppLib_SetPumpRoutine(PumpMessage);
85
86    TASKQUEUE_PARAMS tqp;
87    memset (&tqp, 0x00, sizeof(tqp));
88    tqp.nThreadsMax = 10;
89    tqp.fnCreateTaskPacket = CreateTaskPacket;
90    tqp.fnPerformTask = PerformTask;
91    tqp.fnFreeTaskPacket = FreeTaskPacket;
92    AfsAppLib_InitTaskQueue (&tqp);
93
94    Main_ConfigureHelp();
95
96    // Determine how the app is supposed to look--that is, remember what it
97    // looked like last time, and if there was no "last time", pick some
98    // decent defaults.
99    //
100    if (!RestoreSettings (REGSTR_SETTINGS_BASE, REGSTR_SETTINGS_PATH, REGVAL_SETTINGS, &gr, sizeof(gr), wVerGLOBALS_RESTORED))
101       {
102       memset (&gr, 0x00, sizeof(gr));
103       SetRectEmpty (&gr.rMain);
104       gr.cminRefreshRate = 60; // 1 hour default refresh rate
105
106       User_SetDefaultCreateParams (&gr.CreateUser);
107       Group_SetDefaultCreateParams (&gr.CreateGroup);
108       Machine_SetDefaultCreateParams (&gr.CreateMachine);
109       Actions_SetDefaultView (&gr.viewAct);
110       User_SetDefaultView (&gr.viewUsr, &gr.ivUsr);
111       Group_SetDefaultView (&gr.viewGrp, &gr.ivGrp);
112       Machine_SetDefaultView (&gr.viewMch, &gr.ivMch);
113       Users_SetDefaultSearchParams (&gr.SearchUsers);
114       }
115
116    // Create a variation on WC_DIALOG, so we get appropriate icons on
117    // our windows.
118    //
119    WNDCLASS wc;
120    GetClassInfo (THIS_HINST, MAKEINTRESOURCE( WC_DIALOG ), &wc);
121    wc.hInstance = THIS_HINST;
122    wc.hIcon = TaLocale_LoadIcon (IDI_MAIN);
123    wc.lpszClassName = TEXT("AFSAccountManagerClass");
124    wc.style |= CS_GLOBALCLASS;
125    RegisterClass (&wc);
126
127    // Okay, the big step: create the main window.  Note that it doesn't
128    // get shown yet!
129    //
130    CMDLINEOP op = ParseCommandLine (pszCmdLine);
131    if (op == opCLOSEAPP)
132       return FALSE;
133
134    // Okay, the big step: create the main window.
135    // Note that it doesn't get shown yet!
136    //
137    g.hMain = ModelessDialog (IDD_MAIN, NULL, (DLGPROC)Main_DialogProc);
138    if (g.hMain == NULL)
139       return FALSE;
140
141    if (op != opNOCELLDIALOG)
142       {
143       if (OpenCellDialog() != IDOK)
144          return FALSE;
145       }
146
147    return TRUE;
148 }
149
150
151 void ExitApplication (void)
152 {
153    if (g.idClient)
154       {
155       ULONG status;
156       if (g.idCell)
157          asc_CellClose (g.idClient, g.idCell, &status);
158       AfsAppLib_CloseAdminServer();
159       }
160 }
161
162
163 void Quit (int rc)
164 {
165    if (g.hMain && IsWindow(g.hMain))
166       {
167       WINDOWPLACEMENT wpl;
168       wpl.length = sizeof(wpl);
169       if (GetWindowPlacement (g.hMain, &wpl))
170          gr.rMain = wpl.rcNormalPosition;
171       }
172
173    StoreSettings (REGSTR_SETTINGS_BASE, REGSTR_SETTINGS_PATH, REGVAL_SETTINGS, &gr, sizeof(gr), wVerGLOBALS_RESTORED);
174    PostQuitMessage (0);
175 }
176
177
178 void PumpMessage (MSG *lpm)
179 {
180    if (g.hMain && IsWindow (g.hMain))
181       {
182       if (GetActiveWindow())
183          {
184          if (TranslateAccelerator (GetActiveWindow(), g.hAccel, lpm))
185             return;
186          }
187       }
188
189    if (!IsMemoryManagerMessage (lpm))
190       {
191       TranslateMessage (lpm);
192       DispatchMessage (lpm);
193       }
194 }
195
196
197 BOOL cdecl StartThread (DWORD (WINAPI *lpfnStart)(PVOID lp), ...)
198 {
199    va_list   arg;
200    va_start (arg, lpfnStart);
201    PVOID     lp = va_arg (arg, PVOID);
202
203    DWORD dwThreadID;
204    HANDLE hThread;
205
206    if ((hThread = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)lpfnStart, lp, 0, &dwThreadID)) == NULL)
207       return FALSE;
208
209    SetThreadPriority (hThread, THREAD_PRIORITY_BELOW_NORMAL);
210    return TRUE;
211 }
212