Fix restored columns getting an almost-zero width
[amule.git] / src / ServerWnd.cpp
blob1dc83d74fee34e2934514d4a198cb792558607b8
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-2008 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"
39 #include "updownclient.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)
49 END_EVENT_TABLE()
52 CServerWnd::CServerWnd(wxWindow* pParent /*=NULL*/, int splitter_pos)
53 : wxPanel(pParent, -1)
55 wxSizer* sizer = serverListDlg(this,TRUE);
57 // init serverlist
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."));
104 return;
107 if ( port <= 0 || port > 65535 ) {
108 AddLogLineC(_("Server not added: Invalid server-port specified."));
109 return;
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();
119 } else {
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());
126 if ( update ) {
127 update->SetListName(toadd->GetListName());
128 serverlistctrl->RefreshServer(update);
130 delete toadd;
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"));
166 // Connection data
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"));
181 } else {
182 ED2KInfoList->SetItem(1, 1, Uint32_16toStringIP_Port(theApp->GetED2KID(), thePrefs::GetPort()));
183 ED2KInfoList->SetItem(3, 1, _("HighID"));
186 } else {
187 // No data
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 );
200 int next_row = 0;
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")));
209 // Connection data
210 KadInfoList->InsertItem(next_row, _("Status:"));
211 KadInfoList->SetItem(next_row++, 1, theApp->IsConnectedKad() ? _("Connected"): _("Disconnected"));
212 if (theApp->IsConnectedKad()) {
213 KadInfoList->InsertItem(next_row, _("Connection State:"));
214 KadInfoList->SetItem(next_row++, 1, theApp->IsFirewalledKad() ?
215 wxString(CFormat(_("Firewalled - open TCP port %d in your router or firewall")) % thePrefs::GetPort())
216 : wxString(_("OK")));
217 KadInfoList->InsertItem(next_row, _("UDP Connection State:"));
218 bool UDPFirewalled = theApp->IsFirewalledKadUDP();
219 KadInfoList->SetItem(next_row++, 1, UDPFirewalled ?
220 wxString(CFormat(_("Firewalled - open UDP port %d in your router or firewall")) % thePrefs::GetUDPPort())
221 : wxString(_("OK")));
223 if (theApp->IsFirewalledKad() || UDPFirewalled) {
224 KadInfoList->InsertItem(next_row, _("Firewalled state: "));
225 wxString BuddyState;
226 switch ( theApp->GetBuddyStatus() )
228 case Disconnected:
229 if (!theApp->IsFirewalledKad()) {
230 BuddyState = _("No buddy required - TCP port open");
231 } else if (!UDPFirewalled) {
232 BuddyState = _("No buddy required - UDP port open");
233 } else {
234 BuddyState = _("No buddy");
236 break;
237 case Connecting:
238 BuddyState = _("Connecting to buddy");
239 break;
240 case Connected:
241 BuddyState = CFormat(_("Connected to buddy at %s")) % Uint32_16toStringIP_Port(theApp->GetBuddyIP(), theApp->GetBuddyPort());
242 break;
244 KadInfoList->SetItem(next_row++, 1, BuddyState);
247 KadInfoList->InsertItem(next_row, _("IP address:"));
248 KadInfoList->SetItem(next_row++, 1, Uint32toStringIP(theApp->GetKadIPAdress()));
250 // Index info
251 KadInfoList->InsertItem(next_row, _("Indexed sources:"));
252 KadInfoList->SetItem(next_row++, 1, CFormat(wxT("%d")) % theApp->GetKadIndexedSources());
253 KadInfoList->InsertItem(next_row, _("Indexed keywords:"));
254 KadInfoList->SetItem(next_row++, 1, CFormat(wxT("%d")) % theApp->GetKadIndexedKeywords());
255 KadInfoList->InsertItem(next_row, _("Indexed notes:"));
256 KadInfoList->SetItem(next_row++, 1, CFormat(wxT("%d")) % theApp->GetKadIndexedNotes());
257 KadInfoList->InsertItem(next_row, _("Indexed load:"));
258 KadInfoList->SetItem(next_row++, 1, CFormat(wxT("%d")) % theApp->GetKadIndexedLoad());
260 KadInfoList->InsertItem(next_row, _("Average Users:"));
261 KadInfoList->SetItem(next_row, 1, CastItoIShort(theApp->GetKadUsers()));
262 ++next_row;
263 KadInfoList->InsertItem(next_row, _("Average Files:"));
264 KadInfoList->SetItem(next_row, 1, CastItoIShort(theApp->GetKadFiles()));
266 } else {
267 // No data
268 KadInfoList->SetItem(next_row, 1, _("Not running"));
271 // Fit the width of the columns
272 KadInfoList->SetColumnWidth(0, -1);
273 KadInfoList->SetColumnWidth(1, -1);
276 void CServerWnd::OnSashPositionChanged(wxSplitterEvent& WXUNUSED(evt))
278 if (theApp->amuledlg) {
279 theApp->amuledlg->m_srv_split_pos = CastChild( wxT("SrvSplitterWnd"), wxSplitterWindow )->GetSashPosition();
283 void CServerWnd::OnBnClickedED2KDisconnect(wxCommandEvent& WXUNUSED(evt))
285 if (theApp->serverconnect->IsConnecting()) {
286 theApp->serverconnect->StopConnectionTry();
287 } else {
288 theApp->serverconnect->Disconnect();
291 // File_checked_for_headers