2 // This file is part of the aMule Project.
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 )
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
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.
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
37 #include "kademlia/utils/UInt128.h"
39 #include "ClientList.h"
41 BEGIN_EVENT_TABLE(CServerWnd
,wxPanel
)
42 EVT_BUTTON(ID_ADDTOLIST
,CServerWnd::OnBnClickedAddserver
)
43 EVT_BUTTON(IDC_ED2KDISCONNECT
,CServerWnd::OnBnClickedED2KDisconnect
)
44 EVT_BUTTON(ID_UPDATELIST
,CServerWnd::OnBnClickedUpdateservermetfromurl
)
45 EVT_TEXT_ENTER(IDC_SERVERLISTURL
,CServerWnd::OnBnClickedUpdateservermetfromurl
)
46 EVT_BUTTON(ID_BTN_RESET
, CServerWnd::OnBnClickedResetLog
)
47 EVT_BUTTON(ID_BTN_RESET_SERVER
, CServerWnd::OnBnClickedResetServerLog
)
48 EVT_SPLITTER_SASH_POS_CHANGED(ID_SRV_SPLITTER
,CServerWnd::OnSashPositionChanged
)
52 CServerWnd::CServerWnd(wxWindow
* pParent
/*=NULL*/, int splitter_pos
)
53 : wxPanel(pParent
, -1)
55 wxSizer
* sizer
= serverListDlg(this,TRUE
);
58 // no use now. too early.
60 serverlistctrl
= CastChild( ID_SERVERLIST
, CServerListCtrl
);
62 CastChild( ID_SRV_SPLITTER
, wxSplitterWindow
)->SetSashPosition(splitter_pos
, true);
63 CastChild( ID_SRV_SPLITTER
, wxSplitterWindow
)->SetSashGravity(0.5f
);
64 CastChild( IDC_NODESLISTURL
, wxTextCtrl
)->SetValue(thePrefs::GetKadNodesUrl());
65 CastChild( IDC_SERVERLISTURL
, wxTextCtrl
)->SetValue(thePrefs::GetEd2kServersUrl());
67 // Insert two columns, currently without a header
68 wxListCtrl
* ED2KInfoList
= CastChild( ID_ED2KINFO
, wxListCtrl
);
69 wxASSERT(ED2KInfoList
);
70 ED2KInfoList
->InsertColumn(0, wxEmptyString
);
71 ED2KInfoList
->InsertColumn(1, wxEmptyString
);
73 wxListCtrl
* KadInfoList
= CastChild( ID_KADINFO
, wxListCtrl
);
74 wxASSERT(KadInfoList
);
75 KadInfoList
->InsertColumn(0, wxEmptyString
);
76 KadInfoList
->InsertColumn(1, wxEmptyString
);
78 sizer
->Show(this,TRUE
);
82 CServerWnd::~CServerWnd()
84 thePrefs::SetEd2kServersUrl(CastChild( IDC_SERVERLISTURL
, wxTextCtrl
)->GetValue());
85 thePrefs::SetKadNodesUrl(CastChild( IDC_NODESLISTURL
, wxTextCtrl
)->GetValue());
89 void CServerWnd::UpdateServerMetFromURL(const wxString
& strURL
)
91 thePrefs::SetEd2kServersUrl(strURL
);
92 theApp
->serverlist
->UpdateServerMetFromURL(strURL
);
96 void CServerWnd::OnBnClickedAddserver(wxCommandEvent
& WXUNUSED(evt
))
98 wxString servername
= CastChild( IDC_SERVERNAME
, wxTextCtrl
)->GetValue();
99 wxString serveraddr
= CastChild( IDC_IPADDRESS
, wxTextCtrl
)->GetValue();
100 long port
= StrToULong( CastChild( IDC_SPORT
, wxTextCtrl
)->GetValue() );
102 if ( serveraddr
.IsEmpty() ) {
103 AddLogLineC(_("Server not added: No IP or hostname specified."));
107 if ( port
<= 0 || port
> 65535 ) {
108 AddLogLineC(_("Server not added: Invalid server-port specified."));
112 CServer
* toadd
= new CServer( port
, serveraddr
);
113 toadd
->SetListName( servername
.IsEmpty() ? serveraddr
: servername
);
115 if ( theApp
->AddServer( toadd
, true ) ) {
116 CastChild( IDC_SERVERNAME
, wxTextCtrl
)->Clear();
117 CastChild( IDC_IPADDRESS
, wxTextCtrl
)->Clear();
118 CastChild( IDC_SPORT
, wxTextCtrl
)->Clear();
120 CServer
* update
= theApp
->serverlist
->GetServerByAddress(toadd
->GetAddress(), toadd
->GetPort());
121 // See note on CServerList::AddServer
122 if (update
== NULL
&& toadd
->GetIP() != 0) {
123 update
= theApp
->serverlist
->GetServerByIPTCP(toadd
->GetIP(), toadd
->GetPort());
127 update
->SetListName(toadd
->GetListName());
128 serverlistctrl
->RefreshServer(update
);
133 theApp
->serverlist
->SaveServerMet();
137 void CServerWnd::OnBnClickedUpdateservermetfromurl(wxCommandEvent
& WXUNUSED(evt
))
139 wxString strURL
= CastChild( IDC_SERVERLISTURL
, wxTextCtrl
)->GetValue();
140 UpdateServerMetFromURL(strURL
);
144 void CServerWnd::OnBnClickedResetLog(wxCommandEvent
& WXUNUSED(evt
))
146 theApp
->GetLog(true); // Reset it.
150 void CServerWnd::OnBnClickedResetServerLog(wxCommandEvent
& WXUNUSED(evt
))
152 theApp
->GetServerLog(true); // Reset it
156 void CServerWnd::UpdateED2KInfo()
158 wxListCtrl
* ED2KInfoList
= CastChild( ID_ED2KINFO
, wxListCtrl
);
160 ED2KInfoList
->DeleteAllItems();
161 ED2KInfoList
->InsertItem(0, _("eD2k Status:"));
163 if (theApp
->IsConnectedED2K()) {
164 ED2KInfoList
->SetItem(0, 1, _("Connected"));
168 ED2KInfoList
->InsertItem(1, _("IP:Port"));
169 ED2KInfoList
->SetItem(1, 1, theApp
->serverconnect
->IsLowID() ?
170 wxString(_("LowID")) : Uint32_16toStringIP_Port( theApp
->GetED2KID(), thePrefs::GetPort()));
172 ED2KInfoList
->InsertItem(2, _("ID"));
173 // No need to test the server connect, it's already true
174 ED2KInfoList
->SetItem(2, 1, CFormat(wxT("%u")) % theApp
->GetED2KID());
176 ED2KInfoList
->InsertItem(3, wxEmptyString
);
178 if (theApp
->serverconnect
->IsLowID()) {
179 ED2KInfoList
->SetItem(1, 1, _("Server")); // LowID, unknown ip
180 ED2KInfoList
->SetItem(3, 1, _("LowID"));
182 ED2KInfoList
->SetItem(1, 1, Uint32_16toStringIP_Port(theApp
->GetED2KID(), thePrefs::GetPort()));
183 ED2KInfoList
->SetItem(3, 1, _("HighID"));
188 ED2KInfoList
->SetItem(0, 1, _("Not Connected"));
191 // Fit the width of the columns
192 ED2KInfoList
->SetColumnWidth(0, -1);
193 ED2KInfoList
->SetColumnWidth(1, -1);
196 void CServerWnd::UpdateKadInfo()
198 wxListCtrl
* KadInfoList
= CastChild( ID_KADINFO
, wxListCtrl
);
202 KadInfoList
->DeleteAllItems();
204 KadInfoList
->InsertItem(next_row
, _("Kademlia Status:"));
206 if (theApp
->IsKadRunning()) {
207 KadInfoList
->SetItem(next_row
++, 1, (theApp
->IsKadRunningInLanMode() ? _("Running in LAN mode") : _("Running")));
210 KadInfoList
->InsertItem(next_row
, _("Kademlia client ID:"));
211 KadInfoList
->SetItem(next_row
++, 1, theApp
->GetKadID().ToHexString());
212 KadInfoList
->InsertItem(next_row
, _("Status:"));
213 KadInfoList
->SetItem(next_row
++, 1, theApp
->IsConnectedKad() ? _("Connected"): _("Disconnected"));
214 if (theApp
->IsConnectedKad()) {
215 KadInfoList
->InsertItem(next_row
, _("Connection State:"));
216 KadInfoList
->SetItem(next_row
++, 1, theApp
->IsFirewalledKad() ?
217 wxString(CFormat(_("Firewalled - open TCP port %d in your router or firewall")) % thePrefs::GetPort())
218 : wxString(_("OK")));
219 KadInfoList
->InsertItem(next_row
, _("UDP Connection State:"));
220 bool UDPFirewalled
= theApp
->IsFirewalledKadUDP();
221 KadInfoList
->SetItem(next_row
++, 1, UDPFirewalled
?
222 wxString(CFormat(_("Firewalled - open UDP port %d in your router or firewall")) % thePrefs::GetUDPPort())
223 : wxString(_("OK")));
225 if (theApp
->IsFirewalledKad() || UDPFirewalled
) {
226 KadInfoList
->InsertItem(next_row
, _("Firewalled state: "));
228 switch ( theApp
->GetBuddyStatus() )
231 if (!theApp
->IsFirewalledKad()) {
232 BuddyState
= _("No buddy required - TCP port open");
233 } else if (!UDPFirewalled
) {
234 BuddyState
= _("No buddy required - UDP port open");
236 BuddyState
= _("No buddy");
240 BuddyState
= _("Connecting to buddy");
243 BuddyState
= CFormat(_("Connected to buddy at %s")) % Uint32_16toStringIP_Port(theApp
->GetBuddyIP(), theApp
->GetBuddyPort());
246 KadInfoList
->SetItem(next_row
++, 1, BuddyState
);
249 KadInfoList
->InsertItem(next_row
, _("IP address:"));
250 KadInfoList
->SetItem(next_row
++, 1, Uint32toStringIP(theApp
->GetKadIPAdress()));
253 KadInfoList
->InsertItem(next_row
, _("Indexed sources:"));
254 KadInfoList
->SetItem(next_row
++, 1, CFormat(wxT("%d")) % theApp
->GetKadIndexedSources());
255 KadInfoList
->InsertItem(next_row
, _("Indexed keywords:"));
256 KadInfoList
->SetItem(next_row
++, 1, CFormat(wxT("%d")) % theApp
->GetKadIndexedKeywords());
257 KadInfoList
->InsertItem(next_row
, _("Indexed notes:"));
258 KadInfoList
->SetItem(next_row
++, 1, CFormat(wxT("%d")) % theApp
->GetKadIndexedNotes());
259 KadInfoList
->InsertItem(next_row
, _("Indexed load:"));
260 KadInfoList
->SetItem(next_row
++, 1, CFormat(wxT("%d")) % theApp
->GetKadIndexedLoad());
262 KadInfoList
->InsertItem(next_row
, _("Average Users:"));
263 KadInfoList
->SetItem(next_row
, 1, CastItoIShort(theApp
->GetKadUsers()));
265 KadInfoList
->InsertItem(next_row
, _("Average Files:"));
266 KadInfoList
->SetItem(next_row
, 1, CastItoIShort(theApp
->GetKadFiles()));
270 KadInfoList
->SetItem(next_row
, 1, _("Not running"));
273 // Fit the width of the columns
274 KadInfoList
->SetColumnWidth(0, -1);
275 KadInfoList
->SetColumnWidth(1, -1);
278 void CServerWnd::OnSashPositionChanged(wxSplitterEvent
& WXUNUSED(evt
))
280 if (theApp
->amuledlg
) {
281 theApp
->amuledlg
->m_srv_split_pos
= CastChild( wxT("SrvSplitterWnd"), wxSplitterWindow
)->GetSashPosition();
285 void CServerWnd::OnBnClickedED2KDisconnect(wxCommandEvent
& WXUNUSED(evt
))
287 if (theApp
->serverconnect
->IsConnecting()) {
288 theApp
->serverconnect
->StopConnectionTry();
290 theApp
->serverconnect
->Disconnect();
293 // File_checked_for_headers