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.
10//
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.
15//
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
24//
25
26
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"
37
38#include "ClientList.h"
39
40BEGIN_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)
48END_EVENT_TABLE()
49
50
51CServerWnd::CServerWnd(wxWindow* pParent /*=NULL*/, int splitter_pos)
52: wxPanel(pParent, -1)
53{
54	wxSizer* sizer = serverListDlg(this,TRUE);
55
56	// init serverlist
57	// no use now. too early.
58
59	serverlistctrl = CastChild( ID_SERVERLIST, CServerListCtrl );
60
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());
65
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);
71
72	wxListCtrl* KadInfoList = CastChild( ID_KADINFO, wxListCtrl );
73	wxASSERT(KadInfoList);
74	KadInfoList->InsertColumn(0, wxEmptyString);
75	KadInfoList->InsertColumn(1, wxEmptyString);
76
77	sizer->Show(this,TRUE);
78}
79
80
81CServerWnd::~CServerWnd()
82{
83	thePrefs::SetEd2kServersUrl(CastChild( IDC_SERVERLISTURL, wxTextCtrl )->GetValue());
84	thePrefs::SetKadNodesUrl(CastChild( IDC_NODESLISTURL, wxTextCtrl )->GetValue());
85}
86
87
88void CServerWnd::UpdateServerMetFromURL(const wxString& strURL)
89{
90	thePrefs::SetEd2kServersUrl(strURL);
91	theApp->serverlist->UpdateServerMetFromURL(strURL);
92}
93
94
95void CServerWnd::OnBnClickedAddserver(wxCommandEvent& WXUNUSED(evt))
96{
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() );
100
101	if ( serveraddr.IsEmpty() ) {
102		AddLogLineC(_("Server not added: No IP or hostname specified."));
103		return;
104	}
105
106	if ( port <= 0 || port > 65535 ) {
107		AddLogLineC(_("Server not added: Invalid server-port specified."));
108		return;
109	}
110
111	CServer* toadd = new CServer( port, serveraddr );
112	toadd->SetListName( servername.IsEmpty() ? serveraddr : servername );
113
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());
123		}
124
125		if ( update ) {
126			update->SetListName(toadd->GetListName());
127			serverlistctrl->RefreshServer(update);
128		}
129		delete toadd;
130	}
131
132	theApp->serverlist->SaveServerMet();
133}
134
135
136void CServerWnd::OnBnClickedUpdateservermetfromurl(wxCommandEvent& WXUNUSED(evt))
137{
138	wxString strURL = CastChild( IDC_SERVERLISTURL, wxTextCtrl )->GetValue();
139	UpdateServerMetFromURL(strURL);
140}
141
142
143void CServerWnd::OnBnClickedResetLog(wxCommandEvent& WXUNUSED(evt))
144{
145	theApp->GetLog(true); // Reset it.
146}
147
148
149void CServerWnd::OnBnClickedResetServerLog(wxCommandEvent& WXUNUSED(evt))
150{
151	theApp->GetServerLog(true); // Reset it
152}
153
154
155void CServerWnd::UpdateED2KInfo()
156{
157	wxListCtrl* ED2KInfoList = CastChild( ID_ED2KINFO, wxListCtrl );
158
159	ED2KInfoList->DeleteAllItems();
160	ED2KInfoList->InsertItem(0, _("eD2k Status:"));
161
162	if (theApp->IsConnectedED2K()) {
163		ED2KInfoList->SetItem(0, 1, _("Connected"));
164
165		// Connection data
166
167		ED2KInfoList->InsertItem(1, _("IP:Port"));
168		ED2KInfoList->SetItem(1, 1, theApp->serverconnect->IsLowID() ?
169			 wxString(_("LowID")) : Uint32_16toStringIP_Port( theApp->GetED2KID(), thePrefs::GetPort()));
170
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());
174
175		ED2KInfoList->InsertItem(3, wxEmptyString);
176
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"));
183		}
184
185	} else {
186		// No data
187		ED2KInfoList->SetItem(0, 1, _("Not Connected"));
188	}
189
190	// Fit the width of the columns
191	ED2KInfoList->SetColumnWidth(0, -1);
192	ED2KInfoList->SetColumnWidth(1, -1);
193}
194
195void CServerWnd::UpdateKadInfo()
196{
197	wxListCtrl* KadInfoList = CastChild( ID_KADINFO, wxListCtrl );
198
199	int next_row = 0;
200
201	KadInfoList->DeleteAllItems();
202
203	KadInfoList->InsertItem(next_row, _("Kademlia Status:"));
204
205	if (theApp->IsKadRunning()) {
206		KadInfoList->SetItem(next_row++, 1, (theApp->IsKadRunningInLanMode() ? _("Running in LAN mode") : _("Running")));
207
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")));
221
222			if (theApp->IsFirewalledKad() || UDPFirewalled) {
223				KadInfoList->InsertItem(next_row, _("Firewalled state: "));
224				wxString BuddyState;
225				switch ( theApp->GetBuddyStatus() )
226				{
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");
234						}
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;
242				}
243				KadInfoList->SetItem(next_row++, 1, BuddyState);
244			}
245
246			KadInfoList->InsertItem(next_row, _("IP address:"));
247			KadInfoList->SetItem(next_row++, 1, Uint32toStringIP(theApp->GetKadIPAdress()));
248
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());
258
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()));
264		}
265	} else {
266		// No data
267		KadInfoList->SetItem(next_row, 1, _("Not running"));
268	}
269
270	// Fit the width of the columns
271	KadInfoList->SetColumnWidth(0, -1);
272	KadInfoList->SetColumnWidth(1, -1);
273}
274
275void CServerWnd::OnSashPositionChanged(wxSplitterEvent& WXUNUSED(evt))
276{
277	if (theApp->amuledlg) {
278		theApp->amuledlg->m_srv_split_pos = CastChild( wxT("SrvSplitterWnd"), wxSplitterWindow )->GetSashPosition();
279	}
280}
281
282void CServerWnd::OnBnClickedED2KDisconnect(wxCommandEvent& WXUNUSED(evt))
283{
284	if (theApp->serverconnect->IsConnecting()) {
285		theApp->serverconnect->StopConnectionTry();
286	} else {
287		theApp->serverconnect->Disconnect();
288	}
289}
290// File_checked_for_headers
291