win32: use #ifdef _WIN32
[olsrd.git] / gui / win32 / Main / MyDialog3.cpp
1 /*
2  * The olsr.org Optimized Link-State Routing daemon (olsrd)
3  * Copyright (c) 2004, Thomas Lopatic (thomas@lopatic.de)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright 
11  *   notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright 
13  *   notice, this list of conditions and the following disclaimer in 
14  *   the documentation and/or other materials provided with the 
15  *   distribution.
16  * * Neither the name of olsr.org, olsrd nor the names of its 
17  *   contributors may be used to endorse or promote products derived 
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Visit http://www.olsr.org for more information.
34  *
35  * If you find this software useful feel free to make a donation
36  * to the project. For more information see the website or contact
37  * the copyright holders.
38  *
39  */
40
41 #ifdef _WIN32
42
43 #include "stdafx.h"
44 #include "Frontend.h"
45 #include "MyDialog3.h"
46
47 #include "MprEntry.h"
48 #include "MidEntry.h"
49 #include "HnaEntry.h"
50
51 #ifdef _DEBUG
52 #define new DEBUG_NEW
53 #undef THIS_FILE
54 static char THIS_FILE[] = __FILE__;
55 #endif /* _DEBUG */
56
57 MyDialog3::MyDialog3(CWnd* pParent)
58         : CDialog(MyDialog3::IDD, pParent)
59 {
60         LastUpdate = 0;
61         Info = NULL;
62
63         //{{AFX_DATA_INIT(MyDialog3)
64         //}}AFX_DATA_INIT
65 }
66
67 BOOL MyDialog3::Create(CWnd *Parent)
68 {
69         return CDialog::Create(MyDialog3::IDD, Parent);
70 }
71
72 void MyDialog3::DoDataExchange(CDataExchange* pDX)
73 {
74         CDialog::DoDataExchange(pDX);
75         //{{AFX_DATA_MAP(MyDialog3)
76         DDX_Control(pDX, IDC_LIST6, m_HnaList);
77         DDX_Control(pDX, IDC_LIST5, m_MidList);
78         DDX_Control(pDX, IDC_LIST4, m_MprList);
79         DDX_Control(pDX, IDC_LIST1, m_NodeList);
80         //}}AFX_DATA_MAP
81 }
82
83 BEGIN_MESSAGE_MAP(MyDialog3, CDialog)
84         //{{AFX_MSG_MAP(MyDialog3)
85         ON_BN_CLICKED(IDOK, OnOK)
86         ON_BN_CLICKED(IDCANCEL, OnCancel)
87         ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, OnItemchangedNodeList)
88         //}}AFX_MSG_MAP
89 END_MESSAGE_MAP()
90
91 void MyDialog3::OnOK()
92 {
93 }
94
95 void MyDialog3::OnCancel()
96 {
97 }
98
99 BOOL MyDialog3::OnInitDialog() 
100 {
101         CDialog::OnInitDialog();
102         
103         m_NodeList.InsertColumn(0, "Address", LVCFMT_LEFT, 110, 0);
104         m_NodeList.InsertColumn(1, "Timeout", LVCFMT_LEFT, 110, 1);
105         m_NodeList.InsertColumn(2, "MID", LVCFMT_LEFT, 68, 2);
106         m_NodeList.InsertColumn(3, "HNA", LVCFMT_LEFT, 67, 3);
107
108         m_MprList.InsertColumn(0, "MPR", LVCFMT_LEFT, 169, 0);
109
110         m_MidList.InsertColumn(0, "MID", LVCFMT_LEFT, 169, 0);
111
112         m_HnaList.InsertColumn(0, "HNA", LVCFMT_LEFT, 169, 0);
113
114         return TRUE;
115 }
116
117 void MyDialog3::ClearNodeInfo(void)
118 {
119         m_NodeList.DeleteAllItems();
120
121         m_MprList.DeleteAllItems();
122         m_MidList.DeleteAllItems();
123         m_HnaList.DeleteAllItems();
124
125         if (Info != NULL)
126         {
127                 delete[] Info;
128                 Info = NULL;
129         }
130 }
131
132 void MyDialog3::UpdateNodeInfo(CList<class NodeEntry, class NodeEntry &> &NodeList)
133 {
134         SYSTEMTIME SysTime;
135         FILETIME FileTime;
136         unsigned __int64 Now;
137         class NodeEntry Entry;
138         POSITION Pos;
139         CString AddrStr;
140         CString TimeoutStr;
141         int Idx, Num;
142         POSITION Pos2;
143         class MprEntry MprEntry;
144         class MidEntry MidEntry;
145         class HnaEntry HnaEntry;
146         CString CurrNode;
147
148         ::GetSystemTime(&SysTime);
149         ::SystemTimeToFileTime(&SysTime, &FileTime);
150
151         Now = *(unsigned __int64 *)&FileTime;
152
153         if (Now < LastUpdate + (__int64)3 * (__int64)10000000)
154                 return;
155
156         LastUpdate = Now;
157
158         int CurrItem = m_NodeList.GetNextItem(-1, LVNI_SELECTED);
159
160         if (CurrItem >= 0)
161                 CurrNode = m_NodeList.GetItemText(CurrItem, 0);
162
163         m_NodeList.DeleteAllItems();
164
165         if (Info != NULL)
166         {
167                 delete[] Info;
168                 Info = NULL;
169         }
170
171         Num = NodeList.GetCount();
172
173         if (Num == 0)
174                 return;
175
176         Info = new class NodeInfo[Num];
177
178         Pos = NodeList.GetHeadPosition();
179
180         Idx = 0;
181
182         while (Pos != NULL)
183         {
184                 Entry = NodeList.GetNext(Pos);
185
186                 Pos2 = Entry.MprList.GetHeadPosition();
187
188                 while (Pos2 != NULL)
189                 {
190                         MprEntry = Entry.MprList.GetNext(Pos2);
191
192                         AddrStr.Format("%d.%d.%d.%d",
193                                 ((unsigned char *)&MprEntry.Addr)[0], ((unsigned char *)&MprEntry.Addr)[1],
194                                 ((unsigned char *)&MprEntry.Addr)[2], ((unsigned char *)&MprEntry.Addr)[3]);
195
196                         Info[Idx].MprList.Add(AddrStr);
197                 }
198
199                 Pos2 = Entry.MidList.GetHeadPosition();
200
201                 while (Pos2 != NULL)
202                 {
203                         MidEntry = Entry.MidList.GetNext(Pos2);
204
205                         AddrStr.Format("%d.%d.%d.%d",
206                                 ((unsigned char *)&MidEntry.Addr)[0], ((unsigned char *)&MidEntry.Addr)[1],
207                                 ((unsigned char *)&MidEntry.Addr)[2], ((unsigned char *)&MidEntry.Addr)[3]);
208
209                         Info[Idx].MidList.Add(AddrStr);
210                 }
211
212                 Pos2 = Entry.HnaList.GetHeadPosition();
213
214                 while (Pos2 != NULL)
215                 {
216                         HnaEntry = Entry.HnaList.GetNext(Pos2);
217
218                         unsigned int Mask = 0x80000000;
219                         unsigned int Val = ::ntohl(HnaEntry.Mask);
220                         int Bits;
221
222                         for (Bits = 0; Bits < 32; Bits++)
223                         {
224                                 if ((Val & Mask) == 0)
225                                         break;
226
227                                 Mask >>= 1;
228                         }
229
230                         AddrStr.Format("%d.%d.%d.%d/%d",
231                                 ((unsigned char *)&HnaEntry.Addr)[0], ((unsigned char *)&HnaEntry.Addr)[1],
232                                 ((unsigned char *)&HnaEntry.Addr)[2], ((unsigned char *)&HnaEntry.Addr)[3],
233                                 Bits);
234
235                         Info[Idx].HnaList.Add(AddrStr);
236                 }
237
238                 AddrStr.Format("%d.%d.%d.%d",
239                         ((unsigned char *)&Entry.Addr)[0], ((unsigned char *)&Entry.Addr)[1],
240                         ((unsigned char *)&Entry.Addr)[2], ((unsigned char *)&Entry.Addr)[3]);
241
242                 m_NodeList.InsertItem(Idx, AddrStr);
243
244                 if (AddrStr == CurrNode)
245                         m_NodeList.SetItemState(Idx, LVIS_SELECTED, LVIS_SELECTED);
246
247                 SYSTEMTIME SysTime;
248                 FILETIME LocalFileTime;
249
250                 ::FileTimeToLocalFileTime((FILETIME *)&Entry.Timeout, &LocalFileTime);
251                 ::FileTimeToSystemTime(&LocalFileTime, &SysTime);
252
253                 TimeoutStr.Format("%02d:%02d:%02d", SysTime.wHour, SysTime.wMinute,
254                         SysTime.wSecond);
255
256                 m_NodeList.SetItemText(Idx, 1, TimeoutStr);
257
258                 if (Entry.MidList.IsEmpty())
259                         m_NodeList.SetItemText(Idx, 2, "no");
260
261                 else
262                         m_NodeList.SetItemText(Idx, 2, "yes");
263
264                 if (Entry.HnaList.IsEmpty())
265                         m_NodeList.SetItemText(Idx, 3, "no");
266
267                 else
268                         m_NodeList.SetItemText(Idx, 3, "yes");
269
270                 Idx++;
271         }
272 }
273
274 void MyDialog3::OnItemchangedNodeList(NMHDR* pNMHDR, LRESULT* pResult) 
275 {
276         int Item;
277         int Idx;
278         int Num;
279
280         pNMHDR = pNMHDR;
281
282         *pResult = 0;
283
284         Item = m_NodeList.GetNextItem(-1, LVNI_SELECTED);
285
286         if (Item < 0)
287                 return;
288
289         m_MprList.DeleteAllItems();
290
291         Num = Info[Item].MprList.GetSize();
292         
293         for (Idx = 0; Idx < Num; Idx++)
294                 m_MprList.InsertItem(Idx, Info[Item].MprList.GetAt(Idx));
295
296         m_MidList.DeleteAllItems();
297
298         Num = Info[Item].MidList.GetSize();
299         
300         for (Idx = 0; Idx < Num; Idx++)
301                 m_MidList.InsertItem(Idx, Info[Item].MidList.GetAt(Idx));
302
303         m_HnaList.DeleteAllItems();
304
305         Num = Info[Item].HnaList.GetSize();
306         
307         for (Idx = 0; Idx < Num; Idx++)
308                 m_HnaList.InsertItem(Idx, Info[Item].HnaList.GetAt(Idx));
309 }
310
311 #endif /* _WIN32 */