Upstream tarball 20080609
[amule.git] / src / ServerWnd.cpp
blob56421f3f1052cef30bfc10209bc5dbf77d67d02c
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002 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 "kademlia/kademlia/Kademlia.h"
39 #include "ClientList.h"
40 #include "updownclient.h"
42 BEGIN_EVENT_TABLE(CServerWnd,wxPanel)
43 EVT_BUTTON(ID_ADDTOLIST,CServerWnd::OnBnClickedAddserver)
44 EVT_BUTTON(IDC_ED2KDISCONNECT,CServerWnd::OnBnClickedED2KDisconnect)
45 EVT_BUTTON(ID_UPDATELIST,CServerWnd::OnBnClickedUpdateservermetfromurl)
46 EVT_TEXT_ENTER(IDC_SERVERLISTURL,CServerWnd::OnBnClickedUpdateservermetfromurl)
47 EVT_BUTTON(ID_BTN_RESET, CServerWnd::OnBnClickedResetLog)
48 EVT_BUTTON(ID_BTN_RESET_SERVER, CServerWnd::OnBnClickedResetServerLog)
49 EVT_SPLITTER_SASH_POS_CHANGED(ID_SRV_SPLITTER,CServerWnd::OnSashPositionChanged)
50 END_EVENT_TABLE()
53 CServerWnd::CServerWnd(wxWindow* pParent /*=NULL*/, int splitter_pos)
54 : wxPanel(pParent, -1)
56 wxSizer* sizer = serverListDlg(this,TRUE);
58 // init serverlist
59 // no use now. too early.
61 serverlistctrl = CastChild( ID_SERVERLIST, CServerListCtrl );
63 CastChild( ID_SRV_SPLITTER, wxSplitterWindow )->SetSashPosition(splitter_pos, true);
64 CastChild( ID_SRV_SPLITTER, wxSplitterWindow )->SetSashGravity(0.5f);
65 CastChild( IDC_NODESLISTURL, wxTextCtrl )->SetValue(thePrefs::GetKadNodesUrl());
66 CastChild( IDC_SERVERLISTURL, wxTextCtrl )->SetValue(thePrefs::GetEd2kServersUrl());
68 // Insert two columns, currently without a header
69 wxListCtrl* ED2KInfoList = CastChild( ID_ED2KINFO, wxListCtrl );
70 wxASSERT(ED2KInfoList);
71 ED2KInfoList->InsertColumn(0, wxEmptyString);
72 ED2KInfoList->InsertColumn(1, wxEmptyString);
74 wxListCtrl* KadInfoList = CastChild( ID_KADINFO, wxListCtrl );
75 wxASSERT(KadInfoList);
76 KadInfoList->InsertColumn(0, wxEmptyString);
77 KadInfoList->InsertColumn(1, wxEmptyString);
79 sizer->Show(this,TRUE);
83 CServerWnd::~CServerWnd()
85 thePrefs::SetEd2kServersUrl(CastChild( IDC_SERVERLISTURL, wxTextCtrl )->GetValue());
86 thePrefs::SetKadNodesUrl(CastChild( IDC_NODESLISTURL, wxTextCtrl )->GetValue());
90 void CServerWnd::UpdateServerMetFromURL(const wxString& strURL)
92 thePrefs::SetEd2kServersUrl(strURL);
93 theApp->serverlist->UpdateServerMetFromURL(strURL);
97 void CServerWnd::OnBnClickedAddserver(wxCommandEvent& WXUNUSED(evt))
99 wxString servername = CastChild( IDC_SERVERNAME, wxTextCtrl )->GetValue();
100 wxString serveraddr = CastChild( IDC_IPADDRESS, wxTextCtrl )->GetValue();
101 long port = StrToULong( CastChild( IDC_SPORT, wxTextCtrl )->GetValue() );
103 if ( serveraddr.IsEmpty() ) {
104 AddLogLineM( true, _("Server not added: No IP or hostname specified."));
105 return;
108 if ( port <= 0 || port > 65535 ) {
109 AddLogLineM( true, _("Server not added: Invalid server-port specified."));
110 return;
113 CServer* toadd = new CServer( port, serveraddr );
114 toadd->SetListName( servername.IsEmpty() ? serveraddr : servername );
116 if ( theApp->AddServer( toadd, true ) ) {
117 CastChild( IDC_SERVERNAME, wxTextCtrl )->Clear();
118 CastChild( IDC_IPADDRESS, wxTextCtrl )->Clear();
119 CastChild( IDC_SPORT, wxTextCtrl )->Clear();
120 } else {
121 CServer* update = theApp->serverlist->GetServerByAddress(toadd->GetAddress(), toadd->GetPort());
122 // See note on CServerList::AddServer
123 if (update == NULL && toadd->GetIP() != 0) {
124 update = theApp->serverlist->GetServerByIPTCP(toadd->GetIP(), toadd->GetPort());
127 if ( update ) {
128 update->SetListName(toadd->GetListName());
129 serverlistctrl->RefreshServer(update);
131 delete toadd;
134 theApp->serverlist->SaveServerMet();
138 void CServerWnd::OnBnClickedUpdateservermetfromurl(wxCommandEvent& WXUNUSED(evt))
140 wxString strURL = CastChild( IDC_SERVERLISTURL, wxTextCtrl )->GetValue();
141 UpdateServerMetFromURL(strURL);
145 void CServerWnd::OnBnClickedResetLog(wxCommandEvent& WXUNUSED(evt))
147 theApp->GetLog(true); // Reset it.
151 void CServerWnd::OnBnClickedResetServerLog(wxCommandEvent& WXUNUSED(evt))
153 theApp->GetServerLog(true); // Reset it
157 void CServerWnd::UpdateED2KInfo()
159 wxListCtrl* ED2KInfoList = CastChild( ID_ED2KINFO, wxListCtrl );
161 ED2KInfoList->DeleteAllItems();
162 ED2KInfoList->InsertItem(0, _("eD2k Status:"));
164 if (theApp->IsConnectedED2K()) {
165 ED2KInfoList->SetItem(0, 1, _("Connected"));
167 // Connection data
169 ED2KInfoList->InsertItem(1, _("IP:Port"));
170 ED2KInfoList->SetItem(1, 1, theApp->serverconnect->IsLowID() ?
171 wxString(_("LowID")) : Uint32_16toStringIP_Port( theApp->GetED2KID(), thePrefs::GetPort()));
173 ED2KInfoList->InsertItem(2, _("ID"));
174 // No need to test the server connect, it's already true
175 ED2KInfoList->SetItem(2, 1, wxString::Format(wxT("%u"), theApp->GetED2KID()));
177 ED2KInfoList->InsertItem(3, wxEmptyString);
179 if (theApp->serverconnect->IsLowID()) {
180 ED2KInfoList->SetItem(1, 1, _("Server")); // LowID, unknown ip
181 ED2KInfoList->SetItem(3, 1, _("LowID"));
182 } else {
183 ED2KInfoList->SetItem(1, 1, Uint32_16toStringIP_Port(theApp->GetED2KID(), thePrefs::GetPort()));
184 ED2KInfoList->SetItem(3, 1, _("HighID"));
187 } else {
188 // No data
189 ED2KInfoList->SetItem(0, 1, _("Not Connected"));
192 // Fit the width of the columns
193 ED2KInfoList->SetColumnWidth(0, -1);
194 ED2KInfoList->SetColumnWidth(1, -1);
197 void CServerWnd::UpdateKadInfo()
199 wxListCtrl* KadInfoList = CastChild( ID_KADINFO, wxListCtrl );
201 int next_row = 0;
203 KadInfoList->DeleteAllItems();
205 KadInfoList->InsertItem(next_row, _("Kademlia Status:"));
207 if (theApp->IsKadRunning()) {
208 KadInfoList->SetItem(next_row, 1, _("Running"));
210 ++next_row;
212 // Connection data
214 KadInfoList->InsertItem(next_row, _("Status:"));
215 KadInfoList->SetItem(next_row, 1, theApp->IsConnectedKad() ? _("Connected"): _("Disconnected"));
216 ++next_row;
217 if (theApp->IsConnectedKad()) {
218 KadInfoList->InsertItem(next_row, _("Connection State:"));
219 KadInfoList->SetItem(next_row, 1, theApp->IsFirewalledKad() ? _("Firewalled") : _("OK"));
220 ++next_row;
221 #ifndef CLIENT_GUI
222 if (theApp->IsFirewalledKad()) {
223 KadInfoList->InsertItem(next_row, _("Firewalled state: "));
224 KadInfoList->SetItem(next_row, 1, theApp->clientlist->GetBuddy() ? _("Connected to buddy") : _("No buddy"));
225 ++next_row;
226 #ifdef __DEBUG__
227 if (theApp->clientlist->GetBuddy()) {
228 KadInfoList->InsertItem(next_row, wxT("Buddy address: "));
229 KadInfoList->SetItem(next_row, 1, Uint32_16toStringIP_Port(theApp->clientlist->GetBuddy()->GetIP(), theApp->clientlist->GetBuddy()->GetUDPPort()));
230 ++next_row;
232 #endif
234 uint32 KademliaUsers = Kademlia::CKademlia::GetKademliaUsers();
235 uint32 KademliaFiles = Kademlia::CKademlia::GetKademliaFiles();
236 #else
237 uint32 KademliaUsers = theStats::GetKadUsers();
238 uint32 KademliaFiles = theStats::GetKadFiles();
239 //#warning TODO: Buddy state on remote GUI
240 /* Maybe Averages too, but that would be redundant
241 they are already on the status bar */
242 #endif
243 KadInfoList->InsertItem(next_row, _("Average Users:"));
244 KadInfoList->SetItem(next_row, 1, CastItoIShort(KademliaUsers));
245 ++next_row;
246 KadInfoList->InsertItem(next_row, _("Average Files:"));
247 KadInfoList->SetItem(next_row, 1, CastItoIShort(KademliaFiles));
251 } else {
252 // No data
253 KadInfoList->SetItem(next_row, 1, _("Not running"));
256 // Fit the width of the columns
257 KadInfoList->SetColumnWidth(0, -1);
258 KadInfoList->SetColumnWidth(1, -1);
261 void CServerWnd::OnSashPositionChanged(wxSplitterEvent& WXUNUSED(evt))
263 if (theApp->amuledlg) {
264 theApp->amuledlg->m_srv_split_pos = CastChild( wxT("SrvSplitterWnd"), wxSplitterWindow )->GetSashPosition();
268 void CServerWnd::OnBnClickedED2KDisconnect(wxCommandEvent& WXUNUSED(evt))
270 if (theApp->serverconnect->IsConnecting()) {
271 theApp->serverconnect->StopConnectionTry();
272 } else {
273 theApp->serverconnect->Disconnect();
276 // File_checked_for_headers