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