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