Fixed some build warnings in release mode
[amule.git] / src / ServerWnd.cpp
blob8d4afc6b0d6babeedcfe46abec331518f5684819
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "muuli_wdr.h" // Needed for ID_ADDTOLIST
28 #include "ServerWnd.h" // Interface declarations.
29 #include "Server.h" // Needed for CServer
30 #include "ServerList.h" // Needed for CServerList
31 #include "ServerListCtrl.h" // Needed for CServerListCtrl
32 #include "Preferences.h" // Needed for CPreferences
33 #include "ServerConnect.h"
34 #include "amuleDlg.h" // Needed for CamuleDlg
35 #include "amule.h" // Needed for theApp
36 #include "Logger.h"
38 #include "ClientList.h"
40 BEGIN_EVENT_TABLE(CServerWnd,wxPanel)
41 EVT_BUTTON(ID_ADDTOLIST,CServerWnd::OnBnClickedAddserver)
42 EVT_BUTTON(IDC_ED2KDISCONNECT,CServerWnd::OnBnClickedED2KDisconnect)
43 EVT_BUTTON(ID_UPDATELIST,CServerWnd::OnBnClickedUpdateservermetfromurl)
44 EVT_TEXT_ENTER(IDC_SERVERLISTURL,CServerWnd::OnBnClickedUpdateservermetfromurl)
45 EVT_BUTTON(ID_BTN_RESET, CServerWnd::OnBnClickedResetLog)
46 EVT_BUTTON(ID_BTN_RESET_SERVER, CServerWnd::OnBnClickedResetServerLog)
47 EVT_SPLITTER_SASH_POS_CHANGED(ID_SRV_SPLITTER,CServerWnd::OnSashPositionChanged)
48 END_EVENT_TABLE()
51 CServerWnd::CServerWnd(wxWindow* pParent /*=NULL*/, int splitter_pos)
52 : wxPanel(pParent, -1)
54 wxSizer* sizer = serverListDlg(this,TRUE);
56 // init serverlist
57 // no use now. too early.
59 serverlistctrl = CastChild( ID_SERVERLIST, CServerListCtrl );
61 CastChild( ID_SRV_SPLITTER, wxSplitterWindow )->SetSashPosition(splitter_pos, true);
62 CastChild( ID_SRV_SPLITTER, wxSplitterWindow )->SetSashGravity(0.5f);
63 CastChild( IDC_NODESLISTURL, wxTextCtrl )->SetValue(thePrefs::GetKadNodesUrl());
64 CastChild( IDC_SERVERLISTURL, wxTextCtrl )->SetValue(thePrefs::GetEd2kServersUrl());
66 // Insert two columns, currently without a header
67 wxListCtrl* ED2KInfoList = CastChild( ID_ED2KINFO, wxListCtrl );
68 wxASSERT(ED2KInfoList);
69 ED2KInfoList->InsertColumn(0, wxEmptyString);
70 ED2KInfoList->InsertColumn(1, wxEmptyString);
72 wxListCtrl* KadInfoList = CastChild( ID_KADINFO, wxListCtrl );
73 wxASSERT(KadInfoList);
74 KadInfoList->InsertColumn(0, wxEmptyString);
75 KadInfoList->InsertColumn(1, wxEmptyString);
77 sizer->Show(this,TRUE);
81 CServerWnd::~CServerWnd()
83 thePrefs::SetEd2kServersUrl(CastChild( IDC_SERVERLISTURL, wxTextCtrl )->GetValue());
84 thePrefs::SetKadNodesUrl(CastChild( IDC_NODESLISTURL, wxTextCtrl )->GetValue());
88 void CServerWnd::UpdateServerMetFromURL(const wxString& strURL)
90 thePrefs::SetEd2kServersUrl(strURL);
91 theApp->serverlist->UpdateServerMetFromURL(strURL);
95 void CServerWnd::OnBnClickedAddserver(wxCommandEvent& WXUNUSED(evt))
97 wxString servername = CastChild( IDC_SERVERNAME, wxTextCtrl )->GetValue();
98 wxString serveraddr = CastChild( IDC_IPADDRESS, wxTextCtrl )->GetValue();
99 long port = StrToULong( CastChild( IDC_SPORT, wxTextCtrl )->GetValue() );
101 if ( serveraddr.IsEmpty() ) {
102 AddLogLineC(_("Server not added: No IP or hostname specified."));
103 return;
106 if ( port <= 0 || port > 65535 ) {
107 AddLogLineC(_("Server not added: Invalid server-port specified."));
108 return;
111 CServer* toadd = new CServer( port, serveraddr );
112 toadd->SetListName( servername.IsEmpty() ? serveraddr : servername );
114 if ( theApp->AddServer( toadd, true ) ) {
115 CastChild( IDC_SERVERNAME, wxTextCtrl )->Clear();
116 CastChild( IDC_IPADDRESS, wxTextCtrl )->Clear();
117 CastChild( IDC_SPORT, wxTextCtrl )->Clear();
118 } else {
119 CServer* update = theApp->serverlist->GetServerByAddress(toadd->GetAddress(), toadd->GetPort());
120 // See note on CServerList::AddServer
121 if (update == NULL && toadd->GetIP() != 0) {
122 update = theApp->serverlist->GetServerByIPTCP(toadd->GetIP(), toadd->GetPort());
125 if ( update ) {
126 update->SetListName(toadd->GetListName());
127 serverlistctrl->RefreshServer(update);
129 delete toadd;
132 theApp->serverlist->SaveServerMet();
136 void CServerWnd::OnBnClickedUpdateservermetfromurl(wxCommandEvent& WXUNUSED(evt))
138 wxString strURL = CastChild( IDC_SERVERLISTURL, wxTextCtrl )->GetValue();
139 UpdateServerMetFromURL(strURL);
143 void CServerWnd::OnBnClickedResetLog(wxCommandEvent& WXUNUSED(evt))
145 theApp->GetLog(true); // Reset it.
149 void CServerWnd::OnBnClickedResetServerLog(wxCommandEvent& WXUNUSED(evt))
151 theApp->GetServerLog(true); // Reset it
155 void CServerWnd::UpdateED2KInfo()
157 wxListCtrl* ED2KInfoList = CastChild( ID_ED2KINFO, wxListCtrl );
159 ED2KInfoList->DeleteAllItems();
160 ED2KInfoList->InsertItem(0, _("eD2k Status:"));
162 if (theApp->IsConnectedED2K()) {
163 ED2KInfoList->SetItem(0, 1, _("Connected"));
165 // Connection data
167 ED2KInfoList->InsertItem(1, _("IP:Port"));
168 ED2KInfoList->SetItem(1, 1, theApp->serverconnect->IsLowID() ?
169 wxString(_("LowID")) : Uint32_16toStringIP_Port( theApp->GetED2KID(), thePrefs::GetPort()));
171 ED2KInfoList->InsertItem(2, _("ID"));
172 // No need to test the server connect, it's already true
173 ED2KInfoList->SetItem(2, 1, CFormat(wxT("%u")) % theApp->GetED2KID());
175 ED2KInfoList->InsertItem(3, wxEmptyString);
177 if (theApp->serverconnect->IsLowID()) {
178 ED2KInfoList->SetItem(1, 1, _("Server")); // LowID, unknown ip
179 ED2KInfoList->SetItem(3, 1, _("LowID"));
180 } else {
181 ED2KInfoList->SetItem(1, 1, Uint32_16toStringIP_Port(theApp->GetED2KID(), thePrefs::GetPort()));
182 ED2KInfoList->SetItem(3, 1, _("HighID"));
185 } else {
186 // No data
187 ED2KInfoList->SetItem(0, 1, _("Not Connected"));
190 // Fit the width of the columns
191 ED2KInfoList->SetColumnWidth(0, -1);
192 ED2KInfoList->SetColumnWidth(1, -1);
195 void CServerWnd::UpdateKadInfo()
197 wxListCtrl* KadInfoList = CastChild( ID_KADINFO, wxListCtrl );
199 int next_row = 0;
201 KadInfoList->DeleteAllItems();
203 KadInfoList->InsertItem(next_row, _("Kademlia Status:"));
205 if (theApp->IsKadRunning()) {
206 KadInfoList->SetItem(next_row++, 1, (theApp->IsKadRunningInLanMode() ? _("Running in LAN mode") : _("Running")));
208 // Connection data
209 KadInfoList->InsertItem(next_row, _("Status:"));
210 KadInfoList->SetItem(next_row++, 1, theApp->IsConnectedKad() ? _("Connected"): _("Disconnected"));
211 if (theApp->IsConnectedKad()) {
212 KadInfoList->InsertItem(next_row, _("Connection State:"));
213 KadInfoList->SetItem(next_row++, 1, theApp->IsFirewalledKad() ?
214 wxString(CFormat(_("Firewalled - open TCP port %d in your router or firewall")) % thePrefs::GetPort())
215 : wxString(_("OK")));
216 KadInfoList->InsertItem(next_row, _("UDP Connection State:"));
217 bool UDPFirewalled = theApp->IsFirewalledKadUDP();
218 KadInfoList->SetItem(next_row++, 1, UDPFirewalled ?
219 wxString(CFormat(_("Firewalled - open UDP port %d in your router or firewall")) % thePrefs::GetUDPPort())
220 : wxString(_("OK")));
222 if (theApp->IsFirewalledKad() || UDPFirewalled) {
223 KadInfoList->InsertItem(next_row, _("Firewalled state: "));
224 wxString BuddyState;
225 switch ( theApp->GetBuddyStatus() )
227 case Disconnected:
228 if (!theApp->IsFirewalledKad()) {
229 BuddyState = _("No buddy required - TCP port open");
230 } else if (!UDPFirewalled) {
231 BuddyState = _("No buddy required - UDP port open");
232 } else {
233 BuddyState = _("No buddy");
235 break;
236 case Connecting:
237 BuddyState = _("Connecting to buddy");
238 break;
239 case Connected:
240 BuddyState = CFormat(_("Connected to buddy at %s")) % Uint32_16toStringIP_Port(theApp->GetBuddyIP(), theApp->GetBuddyPort());
241 break;
243 KadInfoList->SetItem(next_row++, 1, BuddyState);
246 KadInfoList->InsertItem(next_row, _("IP address:"));
247 KadInfoList->SetItem(next_row++, 1, Uint32toStringIP(theApp->GetKadIPAdress()));
249 // Index info
250 KadInfoList->InsertItem(next_row, _("Indexed sources:"));
251 KadInfoList->SetItem(next_row++, 1, CFormat(wxT("%d")) % theApp->GetKadIndexedSources());
252 KadInfoList->InsertItem(next_row, _("Indexed keywords:"));
253 KadInfoList->SetItem(next_row++, 1, CFormat(wxT("%d")) % theApp->GetKadIndexedKeywords());
254 KadInfoList->InsertItem(next_row, _("Indexed notes:"));
255 KadInfoList->SetItem(next_row++, 1, CFormat(wxT("%d")) % theApp->GetKadIndexedNotes());
256 KadInfoList->InsertItem(next_row, _("Indexed load:"));
257 KadInfoList->SetItem(next_row++, 1, CFormat(wxT("%d")) % theApp->GetKadIndexedLoad());
259 KadInfoList->InsertItem(next_row, _("Average Users:"));
260 KadInfoList->SetItem(next_row, 1, CastItoIShort(theApp->GetKadUsers()));
261 ++next_row;
262 KadInfoList->InsertItem(next_row, _("Average Files:"));
263 KadInfoList->SetItem(next_row, 1, CastItoIShort(theApp->GetKadFiles()));
265 } else {
266 // No data
267 KadInfoList->SetItem(next_row, 1, _("Not running"));
270 // Fit the width of the columns
271 KadInfoList->SetColumnWidth(0, -1);
272 KadInfoList->SetColumnWidth(1, -1);
275 void CServerWnd::OnSashPositionChanged(wxSplitterEvent& WXUNUSED(evt))
277 if (theApp->amuledlg) {
278 theApp->amuledlg->m_srv_split_pos = CastChild( wxT("SrvSplitterWnd"), wxSplitterWindow )->GetSashPosition();
282 void CServerWnd::OnBnClickedED2KDisconnect(wxCommandEvent& WXUNUSED(evt))
284 if (theApp->serverconnect->IsConnecting()) {
285 theApp->serverconnect->StopConnectionTry();
286 } else {
287 theApp->serverconnect->Disconnect();
290 // File_checked_for_headers