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