Initial IBM OpenAFS 1.0 tree
[openafs.git] / src / WINNT / client_config / tab_hosts.cpp
1 extern "C" {
2 #include <afs/param.h>
3 #include <afs/stds.h>
4 }
5
6 #include "afs_config.h"
7 #include "tab_hosts.h"
8
9
10 /*
11  * PROTOTYPES _________________________________________________________________
12  *
13  */
14
15 void HostsTab_OnInitDialog (HWND hDlg);
16 BOOL HostsTab_OnApply (HWND hDlg);
17 void HostsTab_OnSelect (HWND hDlg);
18 void HostsTab_OnAdd (HWND hDlg);
19 void HostsTab_OnEdit (HWND hDlg);
20 void HostsTab_OnRemove (HWND hDlg);
21
22 void HostsTab_FillList (HWND hDlg);
23
24 BOOL CALLBACK CellEdit_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
25 void CellEdit_OnInitDialog (HWND hDlg);
26 void CellEdit_OnDestroy (HWND hDlg);
27 void CellEdit_OnApply (HWND hDlg);
28 void CellEdit_OnSelect (HWND hDlg);
29 void CellEdit_OnAdd (HWND hDlg);
30 void CellEdit_OnEdit (HWND hDlg);
31 void CellEdit_OnRemove (HWND hDlg);
32 void CellEdit_Enable (HWND hDlg);
33 void CellEdit_AddServerEntry (HWND hDlg, PCELLDBLINE pLine, int iOrder);
34 int CALLBACK CellEdit_SortFunction (HWND hList, HLISTITEM hItem1, LPARAM lpItem1, HLISTITEM hItem2, LPARAM lpItem2);
35
36 BOOL CALLBACK ServerEdit_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
37 void ServerEdit_OnInitDialog (HWND hDlg);
38 BOOL ServerEdit_OnOK (HWND hDlg);
39
40 BOOL TextToAddr (SOCKADDR_IN *pAddr, LPTSTR pszServer);
41
42
43 /*
44  * ROUTINES ___________________________________________________________________
45  *
46  */
47
48 BOOL CALLBACK HostsTab_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
49 {
50    switch (msg)
51       {
52       case WM_INITDIALOG:
53          if (g.fIsCCenter)
54             Main_OnInitDialog (GetParent(hDlg));
55          HostsTab_OnInitDialog (hDlg);
56          break;
57
58       case WM_COMMAND:
59          switch (LOWORD(wp))
60             {
61             case IDAPPLY:
62                if (!HostsTab_OnApply (hDlg))
63                   SetWindowLong (hDlg, DWL_MSGRESULT, TRUE);
64                break;
65
66             case IDC_ADD:
67                HostsTab_OnAdd (hDlg);
68                break;
69
70             case IDC_EDIT:
71                HostsTab_OnEdit (hDlg);
72                break;
73
74             case IDC_REMOVE:
75                HostsTab_OnRemove (hDlg);
76                break;
77
78             case IDHELP:
79                HostsTab_DlgProc (hDlg, WM_HELP, 0, 0);
80                break;
81             }
82          break;
83
84       case WM_HELP:
85          if (g.fIsCCenter)
86             WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_GENERAL_CCENTER);
87          else
88             WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_CELLS);
89          break;
90
91       case WM_NOTIFY:
92          switch (((LPNMHDR)lp)->code)
93             {
94             case FLN_ITEMSELECT:
95                HostsTab_OnSelect (hDlg);
96                break;
97
98             case FLN_LDBLCLICK:
99                if (IsWindowEnabled (GetDlgItem (hDlg, IDC_EDIT)))
100                   HostsTab_OnEdit (hDlg);
101                break;
102             }
103          break;
104       }
105
106    return FALSE;
107 }
108
109
110 void HostsTab_OnInitDialog (HWND hDlg)
111 {
112    if (!g.Configuration.CellServDB.pFirst)
113       {
114       CSDB_ReadFile (&g.Configuration.CellServDB, NULL);
115
116       // Fill in our list with cell names.
117       //
118       HostsTab_FillList (hDlg);
119       HostsTab_OnSelect (hDlg);
120
121       // If this is the Control Center applet, shove the default cell
122       // name onto the tab too.
123       //
124       if (g.fIsCCenter)
125          {
126          Config_GetCellName (g.Configuration.szCell);
127          SetDlgItemText (hDlg, IDC_CELL, g.Configuration.szCell);
128          }
129       }
130 }
131
132
133 BOOL HostsTab_CommitChanges (BOOL fForce)
134 {
135    HWND hDlg;
136    if ((hDlg = PropSheet_FindTabWindow (g.psh, (DLGPROC)HostsTab_DlgProc)) == NULL)
137       return TRUE;
138    if (fForce)
139       SetWindowLong (hDlg, DWL_MSGRESULT, FALSE); // Make sure we try to apply
140    if (HostsTab_OnApply (hDlg))
141       return TRUE;
142    SetWindowLong (hDlg, DWL_MSGRESULT, TRUE);
143    return FALSE;
144 }
145
146
147 BOOL HostsTab_OnApply (HWND hDlg)
148 {
149    // Don't try to do anything if we've already failed the apply
150    if (GetWindowLong (hDlg, DWL_MSGRESULT))
151       return FALSE;
152
153    if (!CSDB_WriteFile (&g.Configuration.CellServDB))
154       return FALSE;
155
156    // If this is the Control Center applet, we'll have to validate
157    // the cell name too.
158    //
159    if (g.fIsCCenter)
160       {
161       TCHAR szNoCell[ cchRESOURCE ];
162       GetString (szNoCell, IDS_CELL_UNKNOWN);
163
164       TCHAR szCell[ cchRESOURCE ];
165       GetDlgItemText (hDlg, IDC_CELL, szCell, cchRESOURCE);
166
167       if ((!szCell[0]) || (!lstrcmpi (szNoCell, szCell)))
168          {
169          Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_NOCELL_DESC_CC);
170          return FALSE;
171          }
172
173       if (!CSDB_FindCell (&g.Configuration.CellServDB, szCell))
174          {
175          Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADCELL_DESC_CC);
176          return FALSE;
177          }
178
179       if (!Config_SetCellName (szCell))
180          return FALSE;
181       lstrcpy (g.Configuration.szCell, szCell);
182       }
183
184    return TRUE;
185 }
186
187
188 void HostsTab_OnSelect (HWND hDlg)
189 {
190    HWND hList = GetDlgItem (hDlg, IDC_LIST);
191    HLISTITEM hItem = FastList_FindFirstSelected (hList);
192    HLISTITEM hNext = FastList_FindNextSelected (hList, hItem);
193
194    EnableWindow (GetDlgItem (hDlg, IDC_EDIT), !!hItem && !hNext);
195    EnableWindow (GetDlgItem (hDlg, IDC_REMOVE), !!hItem);
196 }
197
198
199 void HostsTab_OnAdd (HWND hDlg)
200 {
201    TCHAR szTitle[ cchRESOURCE ];
202    GetString (szTitle, IDS_CELLADD_TITLE);
203
204    LPPROPSHEET psh = PropSheet_Create (szTitle, FALSE, GetParent(hDlg), 0);
205    psh->sh.dwFlags |= PSH_NOAPPLYNOW;  // Remove the Apply button
206    psh->sh.dwFlags |= PSH_HASHELP;     // Add a Help button instead
207    PropSheet_AddTab (psh, szTitle, IDD_CELL_EDIT, (DLGPROC)CellEdit_DlgProc, 0, TRUE);
208    PropSheet_ShowModal (psh);
209
210    HostsTab_FillList (hDlg);
211    HostsTab_OnSelect (hDlg);
212 }
213
214
215 void HostsTab_OnEdit (HWND hDlg)
216 {
217    HWND hList = GetDlgItem (hDlg, IDC_LIST);
218    HLISTITEM hItem = FastList_FindFirstSelected (hList);
219    if (hItem)
220       {
221       PCELLDBLINE pLine = (PCELLDBLINE)FastList_GetItemParam (hList, hItem);
222       CELLDBLINEINFO Info;
223       CSDB_CrackLine (&Info, pLine->szLine);
224
225       LPTSTR pszTitle = FormatString (IDS_CELLEDIT_TITLE, TEXT("%s"), ((Info.szComment[0]) ? Info.szComment : Info.szCell));
226
227       LPPROPSHEET psh = PropSheet_Create (pszTitle, FALSE, GetParent(hDlg), (LPARAM)pLine);
228       psh->sh.dwFlags |= PSH_NOAPPLYNOW;  // Remove the Apply button
229       psh->sh.dwFlags |= PSH_HASHELP;     // Add a Help button instead
230       PropSheet_AddTab (psh, ((Info.szComment[0]) ? Info.szComment : Info.szCell), IDD_CELL_EDIT, (DLGPROC)CellEdit_DlgProc, (LPARAM)pLine, TRUE);
231       PropSheet_ShowModal (psh);
232
233       FreeString (pszTitle);
234       }
235
236    HostsTab_FillList (hDlg);
237    HostsTab_OnSelect (hDlg);
238 }
239
240
241 void HostsTab_OnRemove (HWND hDlg)
242 {
243    HWND hList = GetDlgItem (hDlg, IDC_LIST);
244    HLISTITEM hItem = FastList_FindFirstSelected (hList);
245    HLISTITEM hNext = FastList_FindNextSelected (hList, hItem);
246
247    if (!hItem)
248       {
249       return;
250       }
251    else if (hNext)
252       {
253       if (Message (MB_ICONEXCLAMATION | MB_OKCANCEL, GetCautionTitle(), IDS_HOSTREM_MANY) != IDOK)
254          return;
255       }
256    else // (!hNext)
257       {
258       PCELLDBLINE pLine = (PCELLDBLINE)FastList_GetItemParam (hList, hItem);
259       CELLDBLINEINFO Info;
260       CSDB_CrackLine (&Info, pLine->szLine);
261
262       if (Message (MB_ICONEXCLAMATION | MB_OKCANCEL, GetCautionTitle(), IDS_HOSTREM_ONE, TEXT("%s"), Info.szCell) != IDOK)
263          return;
264       }
265
266    for ( ; hItem; hItem = FastList_FindNextSelected (hList, hItem))
267       {
268       PCELLDBLINE pLine = (PCELLDBLINE)FastList_GetItemParam (hList, hItem);
269       CSDB_RemoveCell (&g.Configuration.CellServDB, pLine);
270       }
271
272    HostsTab_FillList (hDlg);
273    HostsTab_OnSelect (hDlg);
274 }
275
276
277 void HostsTab_FillList (HWND hDlg)
278 {
279    HWND hList = GetDlgItem (hDlg, IDC_LIST);
280    FastList_Begin (hList);
281    FastList_RemoveAll (hList);
282
283    for (PCELLDBLINE pLine = g.Configuration.CellServDB.pFirst; pLine; pLine = pLine->pNext)
284       {
285       CELLDBLINEINFO Info;
286       if (!CSDB_CrackLine (&Info, pLine->szLine))
287          continue;
288       if (!Info.szCell[0])
289          continue;
290
291       TCHAR szText[ MAX_PATH ];
292       lstrcpy (szText, Info.szCell);
293
294 #if 0 // Add this if you like a more verbose Cell Hosts tab
295       if (Info.szComment)
296          wsprintf (&szText[ lstrlen(szText) ], TEXT(" (%s)"), Info.szComment);
297 #endif
298
299       FASTLISTADDITEM ai;
300       memset (&ai, 0x00, sizeof(ai));
301       ai.iFirstImage = IMAGE_NOIMAGE;
302       ai.iSecondImage = IMAGE_NOIMAGE;
303       ai.pszText = szText;
304       ai.lParam = (LPARAM)pLine;
305       FastList_AddItem (hList, &ai);
306       }
307
308    FastList_End (hList);
309 }
310
311
312 BOOL CALLBACK CellEdit_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
313 {
314    switch (msg)
315       {
316       case WM_INITDIALOG:
317          CellEdit_OnInitDialog (hDlg);
318          break;
319
320       case WM_DESTROY:
321          CellEdit_OnDestroy (hDlg);
322          break;
323
324       case WM_COMMAND:
325          switch (LOWORD(wp))
326             {
327             case IDAPPLY:
328                CellEdit_OnApply (hDlg);
329                break;
330
331             case IDC_CELL:
332                CellEdit_Enable (hDlg);
333                break;
334
335             case IDC_COMMENT:
336                CellEdit_Enable (hDlg);
337                break;
338
339             case IDC_ADD:
340                CellEdit_OnAdd (hDlg);
341                CellEdit_Enable (hDlg);
342                break;
343
344             case IDC_EDIT:
345                CellEdit_OnEdit (hDlg);
346                CellEdit_Enable (hDlg);
347                break;
348
349             case IDC_REMOVE:
350                CellEdit_OnRemove (hDlg);
351                CellEdit_Enable (hDlg);
352                break;
353
354             case IDHELP:
355                CellEdit_DlgProc (hDlg, WM_HELP, 0, 0);
356                break;
357             }
358          break;
359
360       case WM_HELP:
361          if (PropSheet_FindTabParam (hDlg))
362             WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_CELLPROP_EDIT);
363          else
364             WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_CELLPROP_ADD);
365          break;
366
367       case WM_NOTIFY:
368          switch (((LPNMHDR)lp)->code)
369             {
370             case FLN_ITEMSELECT:
371                CellEdit_OnSelect (hDlg);
372                break;
373
374             case FLN_LDBLCLICK:
375                if (IsWindowEnabled (GetDlgItem (hDlg, IDC_EDIT)))
376                   CellEdit_OnEdit (hDlg);
377                CellEdit_Enable (hDlg);
378                break;
379             }
380          break;
381       }
382
383    return FALSE;
384 }
385
386
387 void CellEdit_OnInitDialog (HWND hDlg)
388 {
389    PCELLDBLINE pLine = (PCELLDBLINE)PropSheet_FindTabParam (hDlg);
390    if (pLine)
391       {
392       CELLDBLINEINFO Info;
393       CSDB_CrackLine (&Info, pLine->szLine);
394       SetDlgItemText (hDlg, IDC_CELL, Info.szCell);
395       SetDlgItemText (hDlg, IDC_COMMENT, Info.szComment);
396
397       int iOrder = 0;
398       for (pLine = pLine->pNext; pLine; pLine = pLine->pNext)
399          {
400          CELLDBLINEINFO Info;
401          if (!CSDB_CrackLine (&Info, pLine->szLine))
402             break;
403          if (Info.szCell[0])
404             break;
405
406          CellEdit_AddServerEntry (hDlg, pLine, iOrder++);
407          }
408       }
409
410    // Prepare the columns on the server list
411    //
412    HWND hList = GetDlgItem (hDlg, IDC_LIST);
413
414    FASTLISTCOLUMN Column;
415    Column.dwFlags = FLCF_JUSTIFY_LEFT;
416    Column.cxWidth = 200;
417    GetString (Column.szText, IDS_SVRCOL_COMMENT);
418    FastList_SetColumn (hList, 0, &Column);
419
420    Column.dwFlags = FLCF_JUSTIFY_LEFT;
421    Column.cxWidth = 100;
422    GetString (Column.szText, IDS_SVRCOL_SERVER);
423    FastList_SetColumn (hList, 1, &Column);
424
425    FastList_SetSortFunction (hList, CellEdit_SortFunction);
426
427    // Remove the Context Help [?] thing from the title bar
428    //
429    DWORD dwStyle = GetWindowLong (GetParent (hDlg), GWL_STYLE);
430    dwStyle &= ~DS_CONTEXTHELP;
431    SetWindowLong (GetParent (hDlg), GWL_STYLE, dwStyle);
432
433    dwStyle = GetWindowLong (GetParent (hDlg), GWL_EXSTYLE);
434    dwStyle &= ~WS_EX_CONTEXTHELP;
435    SetWindowLong (GetParent (hDlg), GWL_EXSTYLE, dwStyle);
436
437    // A little cleanup and we're done!
438    //
439    CellEdit_Enable (hDlg);
440    CellEdit_OnSelect (hDlg);
441 }
442
443
444 void CellEdit_OnDestroy (HWND hDlg)
445 {
446    HWND hList = GetDlgItem (hDlg, IDC_LIST);
447    for (HLISTITEM hItem = FastList_FindFirst (hList); hItem; hItem = FastList_FindNext (hList, hItem))
448       {
449       PCELLDBLINE pInfo = (PCELLDBLINE)FastList_GetItemParam (hList, hItem);
450       Delete (pInfo);
451       }
452 }
453
454
455 void CellEdit_OnApply (HWND hDlg)
456 {
457    TCHAR szCell[ cchCELLDBLINE ];
458    GetDlgItemText (hDlg, IDC_CELL, szCell, cchCELLDBLINE);
459
460    TCHAR szComment[ cchCELLDBLINE ];
461    GetDlgItemText (hDlg, IDC_COMMENT, szComment, cchCELLDBLINE);
462
463    TCHAR szLinkedCell[ cchCELLDBLINE ] = TEXT("");
464
465    // Find out if there's already an entry in CellServDB for this cell
466    //
467    PCELLDBLINE pCellLine;
468    if ((pCellLine = CSDB_FindCell (&g.Configuration.CellServDB, szCell)) != NULL)
469       {
470       CELLDBLINEINFO Info;
471       if (CSDB_CrackLine (&Info, pCellLine->szLine))
472          lstrcpy (szLinkedCell, Info.szLinkedCell);
473       }
474
475    // Replace our cell's entry in CellServDB, or add one if necessary.
476    //
477    if ((pCellLine = CSDB_AddCell (&g.Configuration.CellServDB, szCell, szLinkedCell, szComment)) != NULL)
478       {
479       // Remove the old servers from this cell
480       //
481       CSDB_RemoveCellServers (&g.Configuration.CellServDB, pCellLine);
482
483       // Add the servers from our list to the CellServDB
484       //
485       PCELLDBLINE pAppendTo = pCellLine;
486
487       HWND hList = GetDlgItem (hDlg, IDC_LIST);
488       for (HLISTITEM hItem = FastList_FindFirst (hList); hItem; hItem = FastList_FindNext (hList, hItem))
489          {
490          PCELLDBLINE pFromList = (PCELLDBLINE)FastList_GetItemParam (hList, hItem);
491
492          pAppendTo = CSDB_AddLine (&g.Configuration.CellServDB, pAppendTo, pFromList->szLine);
493          }
494       }
495 }
496
497
498 void CellEdit_OnSelect (HWND hDlg)
499 {
500    HWND hList = GetDlgItem (hDlg, IDC_LIST);
501    HLISTITEM hItem = FastList_FindFirstSelected (hList);
502    HLISTITEM hNext = FastList_FindNextSelected (hList, hItem);
503
504    EnableWindow (GetDlgItem (hDlg, IDC_EDIT), !!hItem && !hNext);
505    EnableWindow (GetDlgItem (hDlg, IDC_REMOVE), !!hItem);
506 }
507
508
509 void CellEdit_OnAdd (HWND hDlg)
510 {
511    CELLDBLINE Line;
512    memset (&Line, 0x00, sizeof(CELLDBLINE));
513
514    int iOrder = 0;
515
516    HWND hList = GetDlgItem (hDlg, IDC_LIST);
517    for (HLISTITEM hItem = FastList_FindFirst (hList); hItem; hItem = FastList_FindNext (hList, hItem))
518       {
519       PCELLDBLINE pInfo = (PCELLDBLINE)FastList_GetItemParam (hList, hItem);
520       iOrder = max (iOrder, 1+ (int)(pInfo->pNext));
521       }
522
523    if (ModalDialogParam (IDD_SERVER_EDIT, hDlg, (DLGPROC)ServerEdit_DlgProc, (LPARAM)&Line) == IDOK)
524       {
525       CellEdit_AddServerEntry (hDlg, &Line, iOrder);
526       }
527 }
528
529
530 void CellEdit_OnEdit (HWND hDlg)
531 {
532    HWND hList = GetDlgItem (hDlg, IDC_LIST);
533    HLISTITEM hItem = FastList_FindFirstSelected (hList);
534    PCELLDBLINE pInfo = (PCELLDBLINE)FastList_GetItemParam (hList, hItem);
535
536    CELLDBLINE Line;
537    memcpy (&Line, pInfo, sizeof(CELLDBLINE));
538
539    if (ModalDialogParam (IDD_SERVER_EDIT, hDlg, (DLGPROC)ServerEdit_DlgProc, (LPARAM)&Line) == IDOK)
540       {
541       CELLDBLINEINFO Info;
542       CSDB_CrackLine (&Info, Line.szLine);
543
544       TCHAR szServer[ cchRESOURCE ];
545       lstrcpy (szServer, inet_ntoa (*(struct in_addr *)&Info.ipServer));
546
547       FastList_SetItemText (hList, hItem, 0, Info.szComment);
548       FastList_SetItemText (hList, hItem, 1, szServer);
549
550       lstrcpy (pInfo->szLine, Line.szLine);
551       }
552 }
553
554
555 void CellEdit_OnRemove (HWND hDlg)
556 {
557    HWND hList = GetDlgItem (hDlg, IDC_LIST);
558    FastList_Begin (hList);
559
560    HLISTITEM hItem;
561    while ((hItem = FastList_FindFirstSelected (hList)) != NULL)
562       {
563       PCELLDBLINE pInfo = (PCELLDBLINE)FastList_GetItemParam (hList, hItem);
564       Delete (pInfo);
565       FastList_RemoveItem (hList, hItem);
566       }
567
568    FastList_End (hList);
569 }
570
571
572 void CellEdit_Enable (HWND hDlg)
573 {
574    BOOL fEnable = TRUE;
575
576    TCHAR szText[ cchRESOURCE ];
577    GetDlgItemText (hDlg, IDC_CELL, szText, cchRESOURCE);
578    if (!szText[0])
579       fEnable = FALSE;
580
581    if (!FastList_FindFirst (GetDlgItem (hDlg, IDC_LIST)))
582       fEnable = FALSE;
583
584    EnableWindow (GetDlgItem (GetParent (hDlg), IDOK), fEnable);
585 }
586
587
588 void CellEdit_AddServerEntry (HWND hDlg, PCELLDBLINE pLine, int iOrder)
589 {
590    HWND hList = GetDlgItem (hDlg, IDC_LIST);
591
592    PCELLDBLINE pCopy = New (CELLDBLINE);
593    memcpy (pCopy, pLine, sizeof(CELLDBLINE));
594    pCopy->pPrev = NULL;
595    pCopy->pNext = (PCELLDBLINE)iOrder;
596
597    CELLDBLINEINFO Info;
598    CSDB_CrackLine (&Info, pCopy->szLine);
599
600    TCHAR szServer[ cchRESOURCE ];
601    lstrcpy (szServer, inet_ntoa (*(struct in_addr *)&Info.ipServer));
602
603    FASTLISTADDITEM ai;
604    memset (&ai, 0x00, sizeof(ai));
605    ai.iFirstImage = IMAGE_NOIMAGE;
606    ai.iSecondImage = IMAGE_NOIMAGE;
607    ai.pszText = Info.szComment;
608    ai.lParam = (LPARAM)pCopy;
609    HLISTITEM hItem = FastList_AddItem (hList, &ai);
610
611    FastList_SetItemText (hList, hItem, 1, szServer);
612 }
613
614
615 int CALLBACK CellEdit_SortFunction (HWND hList, HLISTITEM hItem1, LPARAM lpItem1, HLISTITEM hItem2, LPARAM lpItem2)
616 {
617    if (!hItem1 || !hItem2)
618       return 0;
619
620    PCELLDBLINE pLine1 = (PCELLDBLINE)lpItem1;
621    PCELLDBLINE pLine2 = (PCELLDBLINE)lpItem2;
622
623    int iOrder1 = (int)(pLine1->pNext);
624    int iOrder2 = (int)(pLine2->pNext);
625    return iOrder1 - iOrder2;
626 }
627
628
629 BOOL CALLBACK ServerEdit_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
630 {
631    switch (msg)
632       {
633       case WM_INITDIALOG:
634          SetWindowLong (hDlg, DWL_USER, lp);
635          ServerEdit_OnInitDialog (hDlg);
636          break;
637
638       case WM_COMMAND:
639          switch (LOWORD(wp))
640             {
641             case IDOK:
642                if (ServerEdit_OnOK (hDlg))
643                   EndDialog (hDlg, IDOK);
644                break;
645
646             case IDCANCEL:
647                EndDialog (hDlg, IDCANCEL);
648                break;
649
650             case IDHELP:
651                ServerEdit_DlgProc (hDlg, WM_HELP, 0, 0);
652                break;
653
654             case IDC_ADDR_SPECIFIC:
655             case IDC_ADDR_LOOKUP:
656                EnableWindow (GetDlgItem (hDlg, IDC_SERVER), IsDlgButtonChecked (hDlg, IDC_ADDR_SPECIFIC));
657                break;
658             }
659          break;
660
661       case WM_HELP:
662          PCELLDBLINE pLine;
663          pLine = (PCELLDBLINE)GetWindowLong (hDlg, DWL_USER);
664
665          CELLDBLINEINFO Info;
666          if (!CSDB_CrackLine (&Info, pLine->szLine))
667             WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_CELLPROP_SERVER_ADD);
668          else
669             WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_CELLPROP_SERVER_EDIT);
670          break;
671       }
672
673    return FALSE;
674 }
675
676
677 void ServerEdit_OnInitDialog (HWND hDlg)
678 {
679    PCELLDBLINE pLine = (PCELLDBLINE)GetWindowLong (hDlg, DWL_USER);
680
681    TCHAR szTitle[ cchRESOURCE ];
682    CELLDBLINEINFO Info;
683    if (!CSDB_CrackLine (&Info, pLine->szLine))
684       GetString (szTitle, IDS_ADDSERVER_TITLE);
685    else
686       GetString (szTitle, IDS_EDITSERVER_TITLE);
687    SetWindowText (hDlg, szTitle);
688
689    SOCKADDR_IN Addr;
690    memset (&Addr, 0x00, sizeof(SOCKADDR_IN));
691    Addr.sin_family = AF_INET;
692    Addr.sin_addr.s_addr = Info.ipServer;
693    SA_SetAddr (GetDlgItem (hDlg, IDC_SERVER), &Addr);
694
695    CheckDlgButton (hDlg, IDC_ADDR_SPECIFIC, !!Info.ipServer);
696    CheckDlgButton (hDlg, IDC_ADDR_LOOKUP, !Info.ipServer);
697    EnableWindow (GetDlgItem (hDlg, IDC_SERVER), IsDlgButtonChecked (hDlg, IDC_ADDR_SPECIFIC));
698
699    SetDlgItemText (hDlg, IDC_COMMENT, Info.szComment);
700 }
701
702
703 BOOL ServerEdit_OnOK (HWND hDlg)
704 {
705    PCELLDBLINE pLine = (PCELLDBLINE)GetWindowLong (hDlg, DWL_USER);
706
707    TCHAR szComment[ cchCELLDBLINE ];
708    GetDlgItemText (hDlg, IDC_COMMENT, szComment, cchCELLDBLINE);
709
710    SOCKADDR_IN Addr;
711    if (IsDlgButtonChecked (hDlg, IDC_ADDR_SPECIFIC))
712       {
713       SA_GetAddr (GetDlgItem (hDlg, IDC_SERVER), &Addr);
714       lstrcpy (szComment, inet_ntoa (*(struct in_addr *)&Addr.sin_addr.s_addr));
715       }
716    if (!TextToAddr (&Addr, szComment))
717       {
718       Message (MB_ICONHAND, GetErrorTitle(), IDS_BADLOOKUP_DESC, TEXT("%s"), szComment);
719       return FALSE;
720       }
721
722    TCHAR szServer[ cchCELLDBLINE ];
723    lstrcpy (szServer, inet_ntoa (*(struct in_addr *)&Addr.sin_addr.s_addr));
724
725    CSDB_FormatLine (pLine->szLine, szServer, NULL, szComment, FALSE);
726    return TRUE;
727 }
728
729
730 BOOL TextToAddr (SOCKADDR_IN *pAddr, LPTSTR pszServer)
731 {
732    if (!pszServer || !*pszServer)
733       return FALSE;
734
735    try {
736       memset (pAddr, 0x00, sizeof(SOCKADDR_IN));
737       pAddr->sin_family = AF_INET;
738
739       if ((*pszServer >= TEXT('0')) && (*pszServer <= TEXT('9')))
740          {
741          if ((pAddr->sin_addr.s_addr = inet_addr (pszServer)) == 0)
742             return FALSE;
743
744          HOSTENT *pEntry;
745          if ((pEntry = gethostbyaddr ((char*)&pAddr->sin_addr.s_addr, sizeof(int), AF_INET)) == NULL)
746             return FALSE;
747
748          if (pEntry->h_name[0])
749             lstrcpy (pszServer, pEntry->h_name);  // return the server's fqdn
750          }
751       else // Need to lookup the server by its name
752          {
753          HOSTENT *pEntry;
754          if ((pEntry = gethostbyname (pszServer)) == NULL)
755             return FALSE;
756
757          memcpy (&pAddr->sin_addr, (struct in_addr *)pEntry->h_addr, sizeof(struct in_addr));
758          if (!pAddr->sin_addr.s_addr)
759             return FALSE;
760
761          if (pEntry->h_name[0])
762             lstrcpy (pszServer, pEntry->h_name);  // return the server's fqdn
763          }
764
765       return TRUE;
766       }
767    catch(...)
768       {
769       return FALSE;
770       }
771 }
772