rx: Remove RX_CALL_BUSY
[openafs.git] / src / WINNT / afssvrcfg / partition_page.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 /*
11  * INCLUDES _________________________________________________________________
12  *
13  */
14 #include <winsock2.h>
15 #include <ws2tcpip.h>
16
17 extern "C" {
18 #include <afsconfig.h>
19 #include <afs/param.h>
20 #include <afs/stds.h>
21 #include <roken.h>
22 }
23
24 #include "afscfg.h"
25 #include <stdlib.h>
26 #include "resource.h"
27 #include "volume_utils.h"
28
29
30 /*
31  * DEFINITIONS _________________________________________________________________
32  *
33  */
34 static HWND hDlg = 0;
35 static HWND hDriveList = 0;
36 static BOOL bCantCreate = FALSE;
37 static HLISTITEM hSelectedItem = 0;
38
39 //      The idea for this var is that if the user clicks on a drive and the vice name
40 //      hasn't been set by the user, then we will set the vice name to the drive letter
41 //      selected.  However, if the user sets the vice name, then clicking on a drive
42 //      letter doesn't change the vice name.  This var tells us whether it is ok to set
43 //      the vice name or not.
44 static BOOL bAutoSetPartitionName = TRUE;
45
46
47 /*
48  * PROTOTYPES _________________________________________________________________
49  *
50  */
51 static void OnInitDialog(HWND hwndDlg);
52 static void EnableDriveListCtrls(BOOL bEnable = TRUE);
53 static BOOL SavePartitionInfo(BOOL bValidate);
54 static void ShowPartitionInfo();
55 static void CheckEnableButtons();
56 static void OnListSelection(LPFLN_ITEMSELECT_PARAMS pItemParms);
57 static void CantMakePartition(UINT nMsgID);
58 static void MustMakePartition();
59 static void OnPartitionName();
60
61
62 /*
63  * EXPORTED FUNCTIONS _________________________________________________________________
64  *
65  */
66
67 /*
68  * Dialog Proc _________________________________________________________________
69  *
70  */
71 /*
72  * WndProc for our subclass of the wizard's dialog.  We do this to detect
73  * the WM_ACTIVATEAPP message, which we use as a trigger to refresh the
74  * partition info that we display.
75  */
76 static BOOL CALLBACK WizardDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
77 {
78     if ((g_pWiz->GetState() == sidSTEP_EIGHT) && (uMsg == WM_ACTIVATEAPP) && wParam) {
79         UpdateDriveList();
80         ShowPartitionInfo();
81     }
82
83     return CallWindowProc((WNDPROC)Subclass_FindNextHook(hwndDlg, WizardDlgProc), hwndDlg, uMsg, wParam, lParam);
84 }
85
86 BOOL CALLBACK PartitionPageDlgProc(HWND hwndDlg, UINT msg, WPARAM wp, LPARAM lp)
87 {
88     if (WizStep_Common_DlgProc (hwndDlg, msg, wp, lp))
89         return FALSE;
90
91     switch (msg) {
92     case WM_INITDIALOG:
93         OnInitDialog(hwndDlg);
94         CheckEnableButtons();
95         break;
96
97     case WM_DESTROY_SHEET:
98         Subclass_RemoveHook(g_pWiz->GetWindow(), WizardDlgProc);
99         break;
100
101     case WM_COMMAND:
102         switch (LOWORD(wp)) {
103         case IDNEXT:
104             if (SavePartitionInfo(TRUE))
105                 g_pWiz->SetState(sidSTEP_NINE);
106             break;
107
108         case IDBACK:
109             if (SavePartitionInfo(FALSE))
110                 g_pWiz->SetState(sidSTEP_SEVEN);
111             break;
112
113         case IDC_CREATE_PARTITION:
114             g_CfgData.configPartition = CS_CONFIGURE;
115             CheckEnableButtons();
116             EnableDriveListCtrls();
117             break;
118
119         case IDC_DONT_CREATE_PARTITION:
120             g_CfgData.configPartition = CS_DONT_CONFIGURE;
121             CheckEnableButtons();
122             EnableDriveListCtrls(FALSE);
123             break;
124
125         case IDC_PARTITION_NAME:
126             if (HIWORD(wp) == EN_CHANGE) {
127                 OnPartitionName();
128                 SetFocus((HWND)lp);
129             }
130             break;
131         }
132         break;
133
134     case WM_NOTIFY:
135         switch (((LPNMHDR)lp)->code) {
136         case FLN_ITEMSELECT:    OnListSelection((LPFLN_ITEMSELECT_PARAMS)lp);
137         }
138         break;
139
140     }
141
142     return FALSE;
143 }
144
145
146
147 /*
148  * STATIC FUNCTIONS _________________________________________________________________
149  *
150  */
151
152 /*
153  * Event Handler Functions _________________________________________________________________
154  *
155  */
156 static void OnInitDialog(HWND hwndDlg)
157 {
158     HOURGLASS hg;
159
160     hDlg = hwndDlg;
161
162     hDriveList = GetDlgItem(hDlg, IDC_DRIVE_LIST);
163
164     g_pWiz->SetButtonText(IDNEXT, IDS_NEXT);
165     g_pWiz->SetDefaultControl(IDNEXT);
166
167     if (g_CfgData.configPartition == CS_ALREADY_CONFIGURED) {
168         CantMakePartition(IDS_PARTITION_ALREADY_CREATED);
169         return;
170     }
171
172     // Should this step be disabled?  Yes, if this machine is
173     // not configured as a file server.
174     if (!ConfiguredOrConfiguring(g_CfgData.configFS)) {
175         CantMakePartition(IDS_NOT_A_FS_SERVER);
176         EnableStep(g_CfgData.configPartition, FALSE);
177         return;
178     }
179
180     // Do this in case it was disabled the last time
181     EnableStep(g_CfgData.configPartition);
182
183     switch (g_CfgData.configPartition) {
184     case CS_DONT_CONFIGURE:
185         SetCheck(hDlg, IDC_DONT_CREATE_PARTITION);
186         EnableDriveListCtrls(FALSE);
187         break;
188
189     case CS_CONFIGURE:
190     default:
191         SetCheck(hDlg, IDC_CREATE_PARTITION);
192         EnableDriveListCtrls();
193         break;
194     }
195
196     Subclass_AddHook(g_pWiz->GetWindow(), WizardDlgProc);
197
198     SetupDriveList(hDriveList);
199     UpdateDriveList();
200     ShowPartitionInfo();
201
202     if (g_CfgData.bFirstServer)
203         MustMakePartition();
204 }
205
206 static void OnPartitionName()
207 {
208     TCHAR szBuf[MAX_PARTITION_NAME_LEN];
209     GetWindowText(GetDlgItem(hDlg, IDC_PARTITION_NAME), szBuf, MAX_PARTITION_NAME_LEN);
210
211     bAutoSetPartitionName = szBuf[0] == 0;
212
213     CheckEnableButtons();
214 }
215
216 static void OnListSelection(LPFLN_ITEMSELECT_PARAMS pItemParms)
217 {
218     ASSERT(pItemParms);
219
220     hSelectedItem = 0;
221
222     if (pItemParms->hItem) {
223         LPARAM lParam = FastList_GetItemParam(hDriveList, pItemParms->hItem);
224         if (lParam == 0) {
225             hSelectedItem = pItemParms->hItem;
226
227             if (bAutoSetPartitionName) {
228                 LPCTSTR pDrive = FastList_GetItemText(hDriveList, hSelectedItem, 0);
229                 g_CfgData.szPartitionName[0] = _totlower(pDrive[0]);
230                 g_CfgData.szPartitionName[1] = 0;
231                 SetWndText(hDlg, IDC_PARTITION_NAME, g_CfgData.szPartitionName);
232
233                 // Must set this to true because the call to SetWndText will cause
234                 // a call to OnPartitionName, which would incorrectly think that the
235                 // Partition Name had been set by the user rather than by us, thus
236                 // setting bAutoSetPartitionName to false.
237                 bAutoSetPartitionName = TRUE;
238             }
239         }
240     }
241
242     CheckEnableButtons();
243 }
244
245
246 /*
247  * Utility Functions _________________________________________________________________
248  *
249  */
250 static void CantMakePartition(UINT nMsgID)
251 {
252     TCHAR szMsg[cchRESOURCE];
253
254     GetString(szMsg, nMsgID);
255
256     ShowWnd(hDlg, IDC_CREATE_PARTITION, FALSE);
257     ShowWnd(hDlg, IDC_DONT_CREATE_PARTITION, FALSE);
258     ShowWnd(hDlg, IDC_ASK_CREATE_PARTITION, FALSE);
259     ShowWnd(hDlg, IDC_SELECT_DRIVE, FALSE);
260     ShowWnd(hDlg, IDC_DRIVE_LIST, FALSE);
261     ShowWnd(hDlg, IDC_NAME_LABEL, FALSE);
262     ShowWnd(hDlg, IDC_PARTITION_NAME, FALSE);
263
264     ShowWnd(hDlg, IDC_PARTITION_COVER);
265     HWND hMsg = GetDlgItem(hDlg, IDC_PARTITION_MSG);
266     ShowWindow(hMsg, SW_SHOW);
267     SetWindowText(hMsg, szMsg);
268
269     bCantCreate = TRUE;
270 }
271
272 static void MustMakePartition()
273 {
274     TCHAR szMsg[cchRESOURCE];
275
276     GetString(szMsg, IDS_MUST_MAKE_PARTITION);
277
278     ShowWnd(hDlg, IDC_CREATE_PARTITION, FALSE);
279     ShowWnd(hDlg, IDC_DONT_CREATE_PARTITION, FALSE);
280
281     SetWndText(hDlg, IDC_ASK_CREATE_PARTITION, szMsg);
282 }
283
284 static void EnableDriveListCtrls(BOOL bEnable)
285 {
286     EnableWnd(hDlg, IDC_SELECT_DRIVE, bEnable);
287     EnableWnd(hDlg, IDC_DRIVE_LIST, bEnable);
288     EnableWnd(hDlg, IDC_NAME_LABEL, bEnable);
289     EnableWnd(hDlg, IDC_PARTITION_NAME, bEnable);
290 }
291
292 static BOOL SavePartitionInfo(BOOL bValidate)
293 {
294     if (bCantCreate)
295         return TRUE;
296
297     if (GetButtonState(hDlg, IDC_CREATE_PARTITION) != BST_CHECKED) {
298         g_CfgData.szPartitionName[0] = 0;
299         bAutoSetPartitionName = TRUE;
300     } else {
301         GetWindowText(GetDlgItem(hDlg, IDC_PARTITION_NAME), g_CfgData.szPartitionName, MAX_PARTITION_NAME_LEN);
302         if (bValidate && !Validation_IsValid(g_CfgData.szPartitionName, VALID_AFS_PARTITION_NAME))
303             return FALSE;
304     }
305
306     if (hSelectedItem == 0)
307         g_CfgData.chDeviceName = 0;
308     else {
309         LPCTSTR pDrive = FastList_GetItemText(hDriveList, hSelectedItem, 0);
310         g_CfgData.chDeviceName = pDrive[0];
311     }
312
313     return TRUE;
314 }
315
316 static void ShowPartitionInfo()
317 {
318     // The SetWndText call below will mess up our bAutoSetPartitionName variable.
319     // It will trigger the change event on the partition name field causing our
320     // OnPartitionName function to get called, and making it look to us like the
321     // user set the partition name.  Therefore, we will save the current value,
322     // make the call, then restore the value.
323     BOOL bAutoSet = bAutoSetPartitionName;
324     SetWndText(hDlg, IDC_PARTITION_NAME, g_CfgData.szPartitionName);
325     bAutoSetPartitionName = bAutoSet;
326
327     if (g_CfgData.chDeviceName != 0) {
328         HLISTITEM hItem = NULL;
329         while ((hItem = FastList_FindNext(hDriveList, hItem)) != NULL) {
330             LPCTSTR pDrive = FastList_GetItemText(hDriveList, hItem, 0);
331             if (pDrive[0] == g_CfgData.chDeviceName) {
332                 FastList_SelectItem(hDriveList, hItem, TRUE);
333                 hSelectedItem = hItem;
334                 break;
335             }
336         }
337     }
338 }
339
340 static void CheckEnableButtons()
341 {
342     if (IsButtonChecked(hDlg, IDC_CREATE_PARTITION)) {
343         TCHAR szBuf[MAX_PARTITION_NAME_LEN];
344
345         GetWindowText(GetDlgItem(hDlg, IDC_PARTITION_NAME), szBuf, MAX_PARTITION_NAME_LEN);
346         if ((hSelectedItem == 0) || (szBuf[0] == 0)) {
347             g_pWiz->EnableButtons(BACK_BUTTON);
348             g_pWiz->SetDefaultControl(IDBACK);
349             return;
350         }
351     }
352
353     g_pWiz->EnableButtons(BACK_BUTTON | NEXT_BUTTON);
354 }
355