We can now tell WLAN interfaces from wired interfaces on Windows.
[olsrd.git] / gui / win32 / Main / MyDialog3.cpp
1 /*\r
2  * Windows GUI for olsr.org\r
3  * Copyright (C) 2004 Thomas Lopatic (thomas@lopatic.de)\r
4  *\r
5  * This file is part of olsr.org.\r
6  *\r
7  * olsr.org is free software; you can redistribute it and/or modify\r
8  * it under the terms of the GNU General Public License as published by\r
9  * the Free Software Foundation; either version 2 of the License, or\r
10  * (at your option) any later version.\r
11  *\r
12  * olsr.org is distributed in the hope that it will be useful,\r
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15  * GNU General Public License for more details.\r
16  *\r
17  * You should have received a copy of the GNU General Public License\r
18  * along with olsr.org; if not, write to the Free Software\r
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
20  *\r
21  */\r
22 \r
23 #include "stdafx.h"\r
24 #include "Frontend.h"\r
25 #include "MyDialog3.h"\r
26 \r
27 #include "MprEntry.h"\r
28 #include "MidEntry.h"\r
29 #include "HnaEntry.h"\r
30 \r
31 #ifdef _DEBUG\r
32 #define new DEBUG_NEW\r
33 #undef THIS_FILE\r
34 static char THIS_FILE[] = __FILE__;\r
35 #endif\r
36 \r
37 MyDialog3::MyDialog3(CWnd* pParent)\r
38         : CDialog(MyDialog3::IDD, pParent)\r
39 {\r
40         LastUpdate = 0;\r
41         Info = NULL;\r
42 \r
43         //{{AFX_DATA_INIT(MyDialog3)\r
44         //}}AFX_DATA_INIT\r
45 }\r
46 \r
47 BOOL MyDialog3::Create(CWnd *Parent)\r
48 {\r
49         return CDialog::Create(MyDialog3::IDD, Parent);\r
50 }\r
51 \r
52 void MyDialog3::DoDataExchange(CDataExchange* pDX)\r
53 {\r
54         CDialog::DoDataExchange(pDX);\r
55         //{{AFX_DATA_MAP(MyDialog3)\r
56         DDX_Control(pDX, IDC_LIST6, m_HnaList);\r
57         DDX_Control(pDX, IDC_LIST5, m_MidList);\r
58         DDX_Control(pDX, IDC_LIST4, m_MprList);\r
59         DDX_Control(pDX, IDC_LIST1, m_NodeList);\r
60         //}}AFX_DATA_MAP\r
61 }\r
62 \r
63 BEGIN_MESSAGE_MAP(MyDialog3, CDialog)\r
64         //{{AFX_MSG_MAP(MyDialog3)\r
65         ON_BN_CLICKED(IDOK, OnOK)\r
66         ON_BN_CLICKED(IDCANCEL, OnCancel)\r
67         ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, OnItemchangedNodeList)\r
68         //}}AFX_MSG_MAP\r
69 END_MESSAGE_MAP()\r
70 \r
71 void MyDialog3::OnOK()\r
72 {\r
73 }\r
74 \r
75 void MyDialog3::OnCancel()\r
76 {\r
77 }\r
78 \r
79 BOOL MyDialog3::OnInitDialog() \r
80 {\r
81         CDialog::OnInitDialog();\r
82         \r
83         m_NodeList.InsertColumn(0, "Address", LVCFMT_LEFT, 110, 0);\r
84         m_NodeList.InsertColumn(1, "Timeout", LVCFMT_LEFT, 110, 1);\r
85         m_NodeList.InsertColumn(2, "MID", LVCFMT_LEFT, 68, 2);\r
86         m_NodeList.InsertColumn(3, "HNA", LVCFMT_LEFT, 67, 3);\r
87 \r
88         m_MprList.InsertColumn(0, "MPR", LVCFMT_LEFT, 169, 0);\r
89 \r
90         m_MidList.InsertColumn(0, "MID", LVCFMT_LEFT, 169, 0);\r
91 \r
92         m_HnaList.InsertColumn(0, "HNA", LVCFMT_LEFT, 169, 0);\r
93 \r
94         return TRUE;\r
95 }\r
96 \r
97 void MyDialog3::ClearNodeInfo(void)\r
98 {\r
99         m_NodeList.DeleteAllItems();\r
100 \r
101         m_MprList.DeleteAllItems();\r
102         m_MidList.DeleteAllItems();\r
103         m_HnaList.DeleteAllItems();\r
104 \r
105         if (Info != NULL)\r
106         {\r
107                 delete[] Info;\r
108                 Info = NULL;\r
109         }\r
110 }\r
111 \r
112 void MyDialog3::UpdateNodeInfo(CList<class NodeEntry, class NodeEntry &> &NodeList)\r
113 {\r
114         SYSTEMTIME SysTime;\r
115         FILETIME FileTime;\r
116         unsigned __int64 Now;\r
117         class NodeEntry Entry;\r
118         POSITION Pos;\r
119         CString AddrStr;\r
120         CString TimeoutStr;\r
121         int Idx, Num;\r
122         POSITION Pos2;\r
123         class MprEntry MprEntry;\r
124         class MidEntry MidEntry;\r
125         class HnaEntry HnaEntry;\r
126         CString CurrNode;\r
127 \r
128         ::GetSystemTime(&SysTime);\r
129         ::SystemTimeToFileTime(&SysTime, &FileTime);\r
130 \r
131         Now = *(unsigned __int64 *)&FileTime;\r
132 \r
133         if (Now < LastUpdate + (__int64)3 * (__int64)10000000)\r
134                 return;\r
135 \r
136         LastUpdate = Now;\r
137 \r
138         int CurrItem = m_NodeList.GetNextItem(-1, LVNI_SELECTED);\r
139 \r
140         if (CurrItem >= 0)\r
141                 CurrNode = m_NodeList.GetItemText(CurrItem, 0);\r
142 \r
143         m_NodeList.DeleteAllItems();\r
144 \r
145         if (Info != NULL)\r
146         {\r
147                 delete[] Info;\r
148                 Info = NULL;\r
149         }\r
150 \r
151         Num = NodeList.GetCount();\r
152 \r
153         if (Num == 0)\r
154                 return;\r
155 \r
156         Info = new class NodeInfo[Num];\r
157 \r
158         Pos = NodeList.GetHeadPosition();\r
159 \r
160         Idx = 0;\r
161 \r
162         while (Pos != NULL)\r
163         {\r
164                 Entry = NodeList.GetNext(Pos);\r
165 \r
166                 Pos2 = Entry.MprList.GetHeadPosition();\r
167 \r
168                 while (Pos2 != NULL)\r
169                 {\r
170                         MprEntry = Entry.MprList.GetNext(Pos2);\r
171 \r
172                         AddrStr.Format("%d.%d.%d.%d",\r
173                                 ((unsigned char *)&MprEntry.Addr)[0], ((unsigned char *)&MprEntry.Addr)[1],\r
174                                 ((unsigned char *)&MprEntry.Addr)[2], ((unsigned char *)&MprEntry.Addr)[3]);\r
175 \r
176                         Info[Idx].MprList.Add(AddrStr);\r
177                 }\r
178 \r
179                 Pos2 = Entry.MidList.GetHeadPosition();\r
180 \r
181                 while (Pos2 != NULL)\r
182                 {\r
183                         MidEntry = Entry.MidList.GetNext(Pos2);\r
184 \r
185                         AddrStr.Format("%d.%d.%d.%d",\r
186                                 ((unsigned char *)&MidEntry.Addr)[0], ((unsigned char *)&MidEntry.Addr)[1],\r
187                                 ((unsigned char *)&MidEntry.Addr)[2], ((unsigned char *)&MidEntry.Addr)[3]);\r
188 \r
189                         Info[Idx].MidList.Add(AddrStr);\r
190                 }\r
191 \r
192                 Pos2 = Entry.HnaList.GetHeadPosition();\r
193 \r
194                 while (Pos2 != NULL)\r
195                 {\r
196                         HnaEntry = Entry.HnaList.GetNext(Pos2);\r
197 \r
198                         unsigned int Mask = 0x80000000;\r
199                         unsigned int Val = ::ntohl(HnaEntry.Mask);\r
200                         int Bits;\r
201 \r
202                         for (Bits = 0; Bits < 32; Bits++)\r
203                         {\r
204                                 if ((Val & Mask) == 0)\r
205                                         break;\r
206 \r
207                                 Mask >>= 1;\r
208                         }\r
209 \r
210                         AddrStr.Format("%d.%d.%d.%d/%d",\r
211                                 ((unsigned char *)&HnaEntry.Addr)[0], ((unsigned char *)&HnaEntry.Addr)[1],\r
212                                 ((unsigned char *)&HnaEntry.Addr)[2], ((unsigned char *)&HnaEntry.Addr)[3],\r
213                                 Bits);\r
214 \r
215                         Info[Idx].HnaList.Add(AddrStr);\r
216                 }\r
217 \r
218                 AddrStr.Format("%d.%d.%d.%d",\r
219                         ((unsigned char *)&Entry.Addr)[0], ((unsigned char *)&Entry.Addr)[1],\r
220                         ((unsigned char *)&Entry.Addr)[2], ((unsigned char *)&Entry.Addr)[3]);\r
221 \r
222                 m_NodeList.InsertItem(Idx, AddrStr);\r
223 \r
224                 if (AddrStr == CurrNode)\r
225                         m_NodeList.SetItemState(Idx, LVIS_SELECTED, LVIS_SELECTED);\r
226 \r
227                 SYSTEMTIME SysTime;\r
228                 FILETIME LocalFileTime;\r
229 \r
230                 ::FileTimeToLocalFileTime((FILETIME *)&Entry.Timeout, &LocalFileTime);\r
231                 ::FileTimeToSystemTime(&LocalFileTime, &SysTime);\r
232 \r
233                 TimeoutStr.Format("%02d:%02d:%02d", SysTime.wHour, SysTime.wMinute,\r
234                         SysTime.wSecond);\r
235 \r
236                 m_NodeList.SetItemText(Idx, 1, TimeoutStr);\r
237 \r
238                 if (Entry.MidList.IsEmpty())\r
239                         m_NodeList.SetItemText(Idx, 2, "no");\r
240 \r
241                 else\r
242                         m_NodeList.SetItemText(Idx, 2, "yes");\r
243 \r
244                 if (Entry.HnaList.IsEmpty())\r
245                         m_NodeList.SetItemText(Idx, 3, "no");\r
246 \r
247                 else\r
248                         m_NodeList.SetItemText(Idx, 3, "yes");\r
249 \r
250                 Idx++;\r
251         }\r
252 }\r
253 \r
254 void MyDialog3::OnItemchangedNodeList(NMHDR* pNMHDR, LRESULT* pResult) \r
255 {\r
256         int Item;\r
257         int Idx;\r
258         int Num;\r
259 \r
260         pNMHDR = pNMHDR;\r
261 \r
262         *pResult = 0;\r
263 \r
264         Item = m_NodeList.GetNextItem(-1, LVNI_SELECTED);\r
265 \r
266         if (Item < 0)\r
267                 return;\r
268 \r
269         m_MprList.DeleteAllItems();\r
270 \r
271         Num = Info[Item].MprList.GetSize();\r
272         \r
273         for (Idx = 0; Idx < Num; Idx++)\r
274                 m_MprList.InsertItem(Idx, Info[Item].MprList.GetAt(Idx));\r
275 \r
276         m_MidList.DeleteAllItems();\r
277 \r
278         Num = Info[Item].MidList.GetSize();\r
279         \r
280         for (Idx = 0; Idx < Num; Idx++)\r
281                 m_MidList.InsertItem(Idx, Info[Item].MidList.GetAt(Idx));\r
282 \r
283         m_HnaList.DeleteAllItems();\r
284 \r
285         Num = Info[Item].HnaList.GetSize();\r
286         \r
287         for (Idx = 0; Idx < Num; Idx++)\r
288                 m_HnaList.InsertItem(Idx, Info[Item].HnaList.GetAt(Idx));\r
289 }\r