5ccfcdbaab11849641f9d9977539157acc6cff7c
[openafs.git] / src / WINNT / client_config / dlg_binding.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 #include <stdio.h>
15 #include "afs_config.h"
16 #include <lanahelper.h>
17
18 /*
19  * DEFINITIONS ________________________________________________________________
20  *
21  */
22
23
24 // Our dialog data
25 static BOOL fFirstTime = TRUE;
26 int nLanAdapter;
27 LANAINFO* lanainfo = NULL;
28
29 int GetAdapterNumber(TCHAR*);
30
31 /*
32  * PROTOTYPES _________________________________________________________________
33  *
34  */
35
36 void Binding_OnInitDialog (HWND hDlg);
37 void Binding_OnOK(HWND hDlg);
38 void Binding_OnCancel(HWND hDlg);
39 BOOL Binding_OnApply();
40
41 BOOL isGateway = FALSE;
42 /*
43  * ROUTINES ___________________________________________________________________
44  *
45  */
46
47 BOOL CALLBACK Binding_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
48 {
49    switch (msg)
50       {
51       case WM_INITDIALOG:
52          Binding_OnInitDialog (hDlg);
53          break;
54
55       case WM_CTLCOLORSTATIC:
56          if ((HWND)lp == GetDlgItem (hDlg, IDC_CHUNK_SIZE))
57             {
58             if (IsWindowEnabled ((HWND)lp))
59                {
60                static HBRUSH hbrStatic = CreateSolidBrush (GetSysColor (COLOR_WINDOW));
61                SetTextColor ((HDC)wp, GetSysColor (COLOR_WINDOWTEXT));
62                SetBkColor ((HDC)wp, GetSysColor (COLOR_WINDOW));
63                return (BOOL)hbrStatic;
64                }
65             }
66          break;
67
68       case WM_COMMAND:
69          switch (LOWORD(wp))
70          {
71             case IDHELP:
72                Binding_DlgProc (hDlg, WM_HELP, 0, 0);
73                break;
74
75             case IDOK:
76                 Binding_OnOK(hDlg);
77                 break;
78                 
79             case IDCANCEL:
80                 Binding_OnCancel(hDlg);
81                 break;
82                               case IDC_DEFAULTNIC:
83                 if (HIWORD(wp) == BN_CLICKED)
84                 {
85                   TCHAR name[MAX_PATH];
86                   memset(name, 0, sizeof(name));
87                                           if (IsDlgButtonChecked(hDlg,IDC_DEFAULTNIC))
88                                                   nLanAdapter=-1;
89                                           else
90                   {
91                     HWND hwndCombo = GetDlgItem(hDlg, IDC_NICSELECTION);
92                     if (SendMessage(hwndCombo, CB_GETCURSEL, 0, 0) == CB_ERR)
93                       SendMessage(hwndCombo, CB_SETCURSEL, (WPARAM)0, (LPARAM)0);
94
95                     TCHAR selected[MAX_PATH];
96                     memset(selected, 0, sizeof(selected));
97                     SendDlgItemMessage(hDlg, IDC_NICSELECTION, 
98                           WM_GETTEXT, sizeof(selected), 
99                           (LPARAM) selected); 
100
101                     if (_tcslen(selected) <= 0)
102                   
103                       nLanAdapter = -1;
104                     else
105                       nLanAdapter = GetAdapterNumber(selected);
106                   }
107                                     
108                   lana_GetAfsNameString(nLanAdapter, isGateway, name);
109                   SetDlgItemText (hDlg, IDC_BINDING_MESSAGE, name);
110                   EnableWindow(GetDlgItem(hDlg,IDC_NICSELECTION),(nLanAdapter!=-1));
111                   break;
112                 }
113             case IDC_NICSELECTION:
114               if (HIWORD(wp) == CBN_SELCHANGE)
115               {
116                 TCHAR name[MAX_PATH];
117                 TCHAR selected[MAX_PATH];
118                 memset(name, 0, sizeof(name));
119                 memset(selected, 0, sizeof(selected));
120                 HWND hwndCombo = GetDlgItem(hDlg, IDC_NICSELECTION);
121                 int i = SendMessage(hwndCombo, CB_GETCURSEL, 0, 0);
122                 if (i != CB_ERR)
123                   SendMessage(hwndCombo, CB_GETLBTEXT, (WPARAM)i, 
124                         (LPARAM) selected); 
125
126                 if (_tcslen(selected) <= 0)
127                   nLanAdapter = -1;
128                 else
129                   nLanAdapter = GetAdapterNumber(selected);
130         
131                                   
132                 lana_GetAfsNameString(nLanAdapter, isGateway, name);
133                 SetDlgItemText (hDlg, IDC_BINDING_MESSAGE, name);
134                 break;
135               }
136
137          }
138          break;
139
140       case WM_HELP:
141          WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_ADVANCED_MISC);
142          break;
143       }
144
145    return FALSE;
146 }
147
148
149 void Binding_OnInitDialog (HWND hDlg)
150 {
151     TCHAR name[MAX_PATH];
152     memset(name, 0, sizeof(name));
153       
154     if (fFirstTime) {
155         Config_GetLanAdapter(&g.Configuration.nLanAdapter);
156         nLanAdapter = g.Configuration.nLanAdapter;
157         isGateway = g.Configuration.fBeGateway;
158         fFirstTime = FALSE;
159     }
160
161     lanainfo = lana_FindLanaByName(NULL);
162
163     // TODO: Show more useful error message.
164     if (!lanainfo) {
165         MessageBox(hDlg, "Unable to obtain LANA list", "LANA ERROR", MB_ICONERROR);
166     } 
167     else
168     { 
169         HWND hwndCombo = GetDlgItem(hDlg, IDC_NICSELECTION);
170         int index = 0;
171         TCHAR tmp[MAX_PATH];
172         while (_tcslen(lanainfo[index].lana_name) > 0)
173         {
174             _stprintf(tmp, "%s (lana number = %d)", lanainfo[index].lana_name, 
175                        lanainfo[index].lana_number);
176             SendMessage(hwndCombo, CB_ADDSTRING, 
177                          0, (LPARAM) tmp);
178             if (nLanAdapter == lanainfo[index].lana_number)
179                 SendMessage(hwndCombo, CB_SELECTSTRING, (WPARAM)-1, 
180                              (LPARAM)tmp);
181             index++;
182         }
183     }
184
185     lana_GetAfsNameString(nLanAdapter, isGateway, name);
186     SetDlgItemText (hDlg, IDC_BINDING_MESSAGE, name);
187
188     CheckDlgButton (hDlg, IDC_DEFAULTNIC, (nLanAdapter==-1));
189
190     EnableWindow(GetDlgItem(hDlg,IDC_NICSELECTION),(nLanAdapter!=-1));
191 }
192
193 void Binding_OnOK (HWND hDlg)
194 {
195   if (IsDlgButtonChecked(hDlg,IDC_DEFAULTNIC))
196     nLanAdapter = -1;
197   else
198   {
199     TCHAR selected[MAX_PATH];
200     memset(selected, 0, sizeof(selected));
201     SendDlgItemMessage(hDlg, IDC_NICSELECTION, 
202                         WM_GETTEXT, sizeof(selected), 
203                         (LPARAM) selected); 
204
205     if (_tcslen(selected) <= 0)
206     {
207       MessageBox(hDlg, "Please select the NIC to bind to", "Error", MB_ICONERROR);
208       return;
209     }
210     
211     nLanAdapter = GetAdapterNumber(selected);
212
213   }
214  
215    EndDialog(hDlg, IDOK);
216 }
217
218
219 BOOL Binding_OnApply()
220 {
221    if (fFirstTime)
222       return TRUE;
223
224   if (nLanAdapter != g.Configuration.nLanAdapter) {
225       if (!Config_SetLanAdapter (nLanAdapter))
226          return FALSE;
227       g.Configuration.nLanAdapter = nLanAdapter;
228    }
229
230   return TRUE;
231
232    
233 }
234
235 void Binding_OnCancel(HWND hDlg)
236 {
237    fFirstTime = TRUE;
238
239    if (lanainfo)
240    {
241      delete lanainfo;
242      lanainfo = NULL;
243    }
244
245    EndDialog(hDlg, IDCANCEL);
246 }
247
248
249 int GetAdapterNumber(TCHAR* n)
250 {
251   int index = 0;
252   while (_tcslen(lanainfo[index].lana_name) > 0)
253    {
254        if (_tcsncmp(lanainfo[index].lana_name, n, _tcslen(lanainfo[index].lana_name)) == 0)
255        {
256          return lanainfo[index].lana_number;
257        }
258        index++;
259     }
260
261   return -1;
262
263 }