jbuehler-add-missing-extern-c-patch-20031207
[openafs.git] / src / WINNT / afssvrcfg / create_partition_dlg.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 extern "C" {
15 #include <afs/param.h>
16 #include <afs/stds.h>
17 }
18
19 #include "afscfg.h"             // Main header for this application
20 #include "resource.h"
21 #include "create_partition_dlg.h"
22 #include "volume_utils.h"
23 #include "partition_utils.h"
24
25
26 /*
27  * DEFINITIONS ________________________________________________________________
28  *
29  */
30 static HWND hDlg = 0;                                                           // This dialog's HWND
31 static HWND hDriveList = 0;
32 static BOOL bAutoSetPartitionName = TRUE;
33 static HLISTITEM hSelectedItem = 0;
34 static BOOL bCreated;                                                           // TRUE if a partition was created
35
36 static const UINT IDD = IDD_CREATE_PARTITION;           // Dialog's resource ID
37
38 static TCHAR szPartitionName[MAX_PARTITION_NAME_LEN];
39 static TCHAR szDrive[4];
40 static TCHAR szSize[32];
41
42 static rwWindowData arwDialog[] = {
43         { IDC_TITLE,            raSizeX | raRepaint,    0,      0 },
44         { IDC_DRIVE_LIST,       raSizeX | raSizeY,      MAKELONG(350, 150), 0 },
45         { IDC_ARGS_FRAME,       raSizeX | raMoveY,      0,      0 },
46         { IDC_NAME_STATIC,      raMoveY | raRepaint,    0,      0 },
47         { IDC_VICEP_STATIC,     raMoveY | raRepaint,    0,      0 },
48         { IDC_PARTITION_NAME,   raMoveY | raRepaint,    0,      0 },
49         { IDC_CREATE,           raMoveX | raMoveY,      0,      0 },
50         { IDC_CLOSE,            raMoveX | raMoveY,      0,      0 },
51         { 9,                    raMoveX | raMoveY,      0,      0 },
52         { idENDLIST,            0,                      0,      0 }
53 };
54
55
56 /*
57  * PROTOTYPES _________________________________________________________________
58  *
59  */
60 static BOOL CALLBACK DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
61 static void OnInitDialog(HWND hwndDlg);
62 static void OnClose();
63 static void OnCreate();
64 static void OnListSelection(LPFLN_ITEMSELECT_PARAMS pItemParms);
65 static void OnPartitionName();
66 static void CheckEnableButtons();
67
68
69 /*
70  * EXPORTED FUNCTIONS _________________________________________________________
71  *
72  */
73 BOOL CreatePartition(HWND hParent)
74 {       
75         ModalDialog(IDD, hParent, (DLGPROC)DlgProc);
76
77         return bCreated;
78 }
79
80
81 /*
82  * STATIC FUNCTIONS ___________________________________________________________
83  *
84  */
85
86 /*
87  * Dialog Proc ________________________________________________________________
88  *
89  */
90 static BOOL CALLBACK DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
91 {
92         if (AfsAppLib_HandleHelp(IDD_CREATE_PARTITION, hwndDlg, uMsg, wParam, lParam))
93                 return TRUE;
94
95         switch (uMsg) {
96                 case WM_INITDIALOG:     OnInitDialog(hwndDlg);
97                                                         break;
98                                 
99                 case WM_COMMAND:        switch (LOWORD(wParam)) {
100                                                                 case IDC_CLOSE:         OnClose();
101                                                                                                         break;
102
103                                                                 case IDC_CREATE:        OnCreate();
104                                                                                                         break;
105
106                                                                 case IDC_PARTITION_NAME:
107                                                                         if (HIWORD(wParam) == EN_CHANGE) {
108                                                                                 OnPartitionName();
109                                                                                 SetFocus((HWND)lParam);
110                                                                         }
111                                                                         break;
112                                                         }
113                                                         break;
114         
115                 case WM_NCACTIVATE:     if (wParam)
116                                                                 UpdateDriveList();
117                                                         break;
118
119                 case WM_NOTIFY: if ((((LPNMHDR)lParam)->code) == FLN_ITEMSELECT)
120                                                         OnListSelection((LPFLN_ITEMSELECT_PARAMS)lParam);
121                                                 break;
122
123                 case WM_SIZE:   if (lParam != 0)
124                                                         ResizeWindow(hwndDlg, arwDialog, rwaFixupGuts);
125                                                 break;
126
127         }
128
129         return FALSE;
130 }
131
132
133 /*
134  * Event Handler Functions ____________________________________________________
135  *
136  */
137 static void OnInitDialog(HWND hwndDlg)
138 {
139         hDlg = hwndDlg;
140
141         ResizeWindow(hDlg, arwDialog, rwaFixupGuts);
142
143         hDriveList = GetDlgItem(hDlg, IDC_DRIVE_LIST);
144
145         bAutoSetPartitionName = TRUE;
146         hSelectedItem = 0;
147
148         SetupDriveList(hDriveList);
149
150         bCreated = FALSE;
151 }
152
153 static void OnClose()
154 {
155         EndDialog(hDlg, IDCANCEL);
156 }
157
158 static void OnCreate()
159 {
160         ASSERT(g_hServer);
161         
162         HLISTITEM hItem = FastList_FindFirstSelected(hDriveList);
163         _ASSERTE(hItem);
164
165         GetWindowText(GetDlgItem(hDlg, IDC_PARTITION_NAME), szPartitionName, sizeof(szPartitionName));
166     if (!Validation_IsValid(szPartitionName, VALID_AFS_PARTITION_NAME))
167         return;
168
169         lstrcpy(szDrive, FastList_GetItemText(hDriveList, hItem, 0));
170         lstrcpy(szSize, FastList_GetItemText(hDriveList, hItem, 2));
171
172         // Constuct the device name
173         char szDevNameA[] = "?:";
174         S2A drive(szDrive);
175     szDevNameA[0] = ((char*)drive)[0];
176
177         // Construct the partition name
178         char szPartNameA[9] = "/vicep";
179         strncat(szPartNameA, (char *)S2A(szPartitionName), 2);
180
181         if (DoesPartitionExist(A2S(szPartNameA))) {
182                 MsgBox(hDlg, IDS_PARTITION_EXISTS, GetAppTitleID(), MB_OK | MB_ICONSTOP);
183                 return;
184         }
185
186         g_LogFile.Write("Adding an AFS partition on device '%s' with name '%s'.\r\n", szDevNameA, szPartNameA);
187
188         afs_status_t nStatus;
189         int nResult = cfg_HostPartitionTableAddEntry(g_hServer, szPartNameA, szDevNameA, &nStatus);
190         if (!nResult) {
191                 ShowError(hDlg, nStatus, IDS_CREATE_PARTITION_ERROR);
192                 return;
193         }
194
195         MsgBox(hDlg, IDS_PARTITION_CREATED, GetAppTitleID(), MB_OK);
196
197         bCreated = TRUE;
198
199         SetWndText(hDlg, IDC_PARTITION_NAME, TEXT(""));
200 }
201
202 static void OnPartitionName()
203 {
204         TCHAR szBuf[MAX_PARTITION_NAME_LEN];
205         GetWindowText(GetDlgItem(hDlg, IDC_PARTITION_NAME), szBuf, MAX_PARTITION_NAME_LEN);
206
207         bAutoSetPartitionName = szBuf[0] == 0;
208         
209         CheckEnableButtons();
210 }
211
212 static void OnListSelection(LPFLN_ITEMSELECT_PARAMS pItemParms)
213 {
214         ASSERT(pItemParms);
215
216         hSelectedItem = 0;
217
218         if (pItemParms->hItem) {
219                 LPARAM lParam = FastList_GetItemParam(hDriveList, pItemParms->hItem);
220                 if (lParam == 0) {
221                         hSelectedItem = pItemParms->hItem;
222
223                         if (bAutoSetPartitionName) {
224                                 TCHAR szName[2];
225                                 LPCTSTR pDrive = FastList_GetItemText(hDriveList, hSelectedItem, 0);
226                                 szName[0] = _totlower(pDrive[0]);
227                                 szName[1] = 0;
228                                 
229                                 TCHAR szFullName[6] = TEXT("vice");
230                                 lstrcat(szFullName, szName);
231
232                                 if (!DoesPartitionExist(szFullName)) {
233                                         SetWndText(hDlg, IDC_PARTITION_NAME, szName);
234
235                                         // Must set this to true because the call to SetWndText will cause
236                                         // a call to OnPartitionName, which would incorrectly think that the
237                                         // Partition Name had been set by the user rather than by us, thus
238                                         // setting bAutoSetPartitionName to false.
239                                         bAutoSetPartitionName = TRUE;
240                                 }
241                         }
242                 }
243         }
244
245         CheckEnableButtons();
246 }
247
248 static void CheckEnableButtons()
249 {
250         TCHAR szBuf[MAX_PARTITION_NAME_LEN];
251
252         GetWindowText(GetDlgItem(hDlg, IDC_PARTITION_NAME), szBuf, MAX_PARTITION_NAME_LEN);
253         
254         ENABLE_STATE es = ES_ENABLE;
255         
256         if ((hSelectedItem == 0) || (szBuf[0] == 0))
257                 es = ES_DISABLE;
258
259         SetEnable(hDlg, IDC_CREATE, es);
260 }
261