| 1 | // |
|---|
| 2 | // This file is part of the imule Project. |
|---|
| 3 | // |
|---|
| 4 | // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org ) |
|---|
| 5 | // Copyright (c) 2004-2011 Angel Vidal (Kry) ( kry@amule.org / http://www.amule.org ) |
|---|
| 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 | #include "KadDlg.h" |
|---|
| 27 | #include "muuli_wdr.h" |
|---|
| 28 | #include "kadNodesListView.h" |
|---|
| 29 | #include "common/Format.h" |
|---|
| 30 | #include "OScopeCtrl.h" |
|---|
| 31 | #include "OtherFunctions.h" |
|---|
| 32 | #include "HTTPDownload.h" |
|---|
| 33 | #include "Logger.h" |
|---|
| 34 | #include "amule.h" |
|---|
| 35 | #include "Preferences.h" |
|---|
| 36 | #include "StatisticsDlg.h" |
|---|
| 37 | #include "ColorFrameCtrl.h" |
|---|
| 38 | #include "MuleTextCtrl.h" // Needed for CMuleTextCtrl |
|---|
| 39 | |
|---|
| 40 | #include "MuleColour.h" |
|---|
| 41 | #include "Statistics.h" |
|---|
| 42 | #include "kademlia/kademlia/Kademlia.h" |
|---|
| 43 | #include "kademlia/routing/RoutingZone.h" |
|---|
| 44 | |
|---|
| 45 | #ifndef CLIENT_GUI |
|---|
| 46 | #include "kademlia/kademlia/Kademlia.h" |
|---|
| 47 | #endif |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | BEGIN_EVENT_TABLE(CKadDlg, wxPanel) |
|---|
| 51 | EVT_TEXT(ID_NODE_DEST, CKadDlg::OnFieldsChange) |
|---|
| 52 | |
|---|
| 53 | EVT_TEXT_ENTER(IDC_NODESLISTURL ,CKadDlg::OnBnClickedUpdateNodeList) |
|---|
| 54 | |
|---|
| 55 | EVT_BUTTON(ID_MYDESTTOCLIPBOARD, CKadDlg::OnBnClickedCopyMyDestToClipboard) |
|---|
| 56 | EVT_BUTTON(ID_NODECONNECT, CKadDlg::OnBnClickedBootstrapClient) |
|---|
| 57 | EVT_BUTTON(ID_KNOWNNODECONNECT, CKadDlg::OnBnClickedBootstrapKnown) |
|---|
| 58 | EVT_BUTTON(ID_UPDATEKADLIST, CKadDlg::OnBnClickedUpdateNodeList) |
|---|
| 59 | EVT_BUTTON(ID_NODEEXPORT, CKadDlg::OnBnClickedNodeExport) |
|---|
| 60 | END_EVENT_TABLE(); |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | CKadDlg::CKadDlg(wxWindow* pParent) |
|---|
| 65 | : wxPanel(pParent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, wxT("kadwnd") ) |
|---|
| 66 | { |
|---|
| 67 | m_kad_scope = NULL; |
|---|
| 68 | m_nodes_list = NULL; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | COScopeCtrl* CKadDlg::GetKadscope() |
|---|
| 76 | { |
|---|
| 77 | return CastChild(wxT("kadScope"), COScopeCtrl); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | void CKadDlg::Init() |
|---|
| 82 | { |
|---|
| 83 | m_nodes_box = ( wxStaticBoxSizer * ) kadDlgNodesGraphFrame ; |
|---|
| 84 | m_nodes_list = GetListctrl(); |
|---|
| 85 | m_kad_scope = GetKadscope(); |
|---|
| 86 | m_kad_scope->SetRanges(0.0, thePrefs::GetStatsMax()); |
|---|
| 87 | m_kad_scope->SetYUnits(wxT("Nodes")); |
|---|
| 88 | |
|---|
| 89 | #ifndef __WXMSW__ |
|---|
| 90 | // |
|---|
| 91 | // Get label with line breaks out of muuli.wdr, because generated code fails |
|---|
| 92 | // to compile in Windows. |
|---|
| 93 | // |
|---|
| 94 | // In Windows, setting a button label with a newline fails (the newline is ignored). |
|---|
| 95 | // Creating a button with such a label works however. :-/ |
|---|
| 96 | // So leave the label from the muuli (without line breaks) here, |
|---|
| 97 | // so it can still be fixed in the translation. |
|---|
| 98 | // |
|---|
| 99 | wxButton* bootstrap = CastChild(ID_KNOWNNODECONNECT, wxButton); |
|---|
| 100 | bootstrap->SetLabel(_("Bootstrap from \nknown clients")); |
|---|
| 101 | #endif |
|---|
| 102 | SetUpdatePeriod(thePrefs::GetTrafficOMeterInterval()); |
|---|
| 103 | SetGraphColors(); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | void CKadDlg::InitSplit() |
|---|
| 107 | { |
|---|
| 108 | if (GetSize().GetWidth()>2) { |
|---|
| 109 | CastChild(ID_SPLITTER, wxSplitterWindow)->SetSashPosition(GetSize().GetWidth()*2/3); |
|---|
| 110 | CastChild(ID_SPLITTER, wxSplitterWindow)->SetSashGravity(0.6667); |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | void CKadDlg::SetUpdatePeriod(int step) |
|---|
| 115 | { |
|---|
| 116 | // this gets called after the value in Preferences/Statistics/Update delay has been changed |
|---|
| 117 | if (step == 0) { |
|---|
| 118 | m_kad_scope->Stop(); |
|---|
| 119 | } else { |
|---|
| 120 | m_kad_scope->Reset(step); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | void CKadDlg::SetGraphColors() |
|---|
| 126 | { |
|---|
| 127 | static const char aTrend[] = { 2, 1, 0 }; |
|---|
| 128 | static const int aRes[] = { IDC_C0, IDC_C0_3, IDC_C0_2 }; |
|---|
| 129 | |
|---|
| 130 | m_kad_scope->SetBackgroundColor(CStatisticsDlg::getColors(0)); |
|---|
| 131 | m_kad_scope->SetGridColor(CStatisticsDlg::getColors(1)); |
|---|
| 132 | |
|---|
| 133 | for (size_t i = 0; i < 3; ++i) { |
|---|
| 134 | m_kad_scope->SetPlotColor(CStatisticsDlg::getColors(12 + i), aTrend[i]); |
|---|
| 135 | |
|---|
| 136 | CColorFrameCtrl* ctrl = CastChild(aRes[i], CColorFrameCtrl); |
|---|
| 137 | ctrl->SetBackgroundBrushColour(CMuleColour(CStatisticsDlg::getColors(12 + i))); |
|---|
| 138 | ctrl->SetFrameBrushColour(*wxBLACK); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | void CKadDlg::UpdateGraph(const GraphUpdateInfo& update) |
|---|
| 144 | { |
|---|
| 145 | std::vector<float *> v(3); |
|---|
| 146 | v[0] = const_cast<float *>(&update.kadnodes[0]); |
|---|
| 147 | v[1] = const_cast<float *>(&update.kadnodes[1]); |
|---|
| 148 | v[2] = const_cast<float *>(&update.kadnodes[2]); |
|---|
| 149 | const std::vector<float *> &apfKad(v); |
|---|
| 150 | unsigned nodeCount = static_cast<unsigned>(update.kadnodes[2]); |
|---|
| 151 | |
|---|
| 152 | if (!IsShownOnScreen()) { |
|---|
| 153 | m_kad_scope->DelayPoints(); |
|---|
| 154 | } else { |
|---|
| 155 | // Check the current node-count to see if we should increase the graph height |
|---|
| 156 | if (m_kad_scope->GetUpperLimit() < update.kadnodes[2]) { |
|---|
| 157 | // Grow the limit by 50 sized increments. |
|---|
| 158 | m_kad_scope->SetRanges(0.0, ((nodeCount + 49) / 50) * 50); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | m_kad_scope->AppendPoints(update.timestamp, apfKad); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | wxStaticText* label = CastChild( wxT("nodesListLabel"), wxStaticText ); |
|---|
| 165 | wxCHECK_RET(label, wxT("Failed to find kad-nodes label")); |
|---|
| 166 | |
|---|
| 167 | label->SetLabel(CFormat(_("Nodes (%u)")) % nodeCount); |
|---|
| 168 | label->GetParent()->Layout(); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | // Enables or disables the node connect button depending on the conents of the text fields |
|---|
| 173 | void CKadDlg::OnFieldsChange(wxCommandEvent& WXUNUSED(evt)) |
|---|
| 174 | { |
|---|
| 175 | // Enable the node connect button if all fields contain text |
|---|
| 176 | wxString txtaddr = this->GetNodeDestCtrl()->GetValue() ; |
|---|
| 177 | CI2PAddress dest = CI2PAddress::fromString(txtaddr); |
|---|
| 178 | FindWindowById(ID_NODECONNECT)->Enable( dest.isValid() ); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | void CKadDlg::OnBnClickedCopyMyDestToClipboard(wxCommandEvent& WXUNUSED(evt)) |
|---|
| 182 | { |
|---|
| 183 | #ifndef CLIENT_GUI |
|---|
| 184 | if (FindWindowById(ID_MYDESTTOCLIPBOARD)->IsEnabled()) |
|---|
| 185 | #endif |
|---|
| 186 | { |
|---|
| 187 | theApp->CopyTextToClipboard(theApp->GetUdpDest().toString()); |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | void CKadDlg::OnBnClickedBootstrapClient(wxCommandEvent& WXUNUSED(evt)) |
|---|
| 193 | { |
|---|
| 194 | if (FindWindowById(ID_NODECONNECT)->IsEnabled()) { |
|---|
| 195 | |
|---|
| 196 | wxString txtaddr = ((wxTextCtrl*)FindWindowById( ID_NODE_DEST ))->GetValue() ; |
|---|
| 197 | CI2PAddress dest = CI2PAddress::fromString(txtaddr); |
|---|
| 198 | |
|---|
| 199 | AddDebugLogLineN(logKadPrefs, CFormat(wxT("bootstrap on %s ")) % dest.humanReadable()); |
|---|
| 200 | |
|---|
| 201 | if (dest.isValid()) { |
|---|
| 202 | theApp->BootstrapKad(dest); |
|---|
| 203 | } else { |
|---|
| 204 | wxMessageBox(_("Invalid ip to bootstrap"), _("Warning"), wxOK | wxICON_EXCLAMATION, this); |
|---|
| 205 | } |
|---|
| 206 | } else { |
|---|
| 207 | wxMessageBox(_("Please fill all fields required"), _("Message"), wxOK | wxICON_INFORMATION, this); |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | void CKadDlg::OnBnClickedNodeExport(wxCommandEvent& WXUNUSED(evt)) |
|---|
| 212 | { |
|---|
| 213 | if ( not theApp->IsKadRunning() ) { |
|---|
| 214 | wxMessageDialog md(NULL, |
|---|
| 215 | _("Kademlia is not running.\n No known node !"), |
|---|
| 216 | _("Export nodes destinations")); |
|---|
| 217 | md.ShowModal(); |
|---|
| 218 | return ; |
|---|
| 219 | } |
|---|
| 220 | wxString name = wxFileSelector(_("Select file to export to")); |
|---|
| 221 | if (!name.empty()) { |
|---|
| 222 | theApp->exportKadContactsOnFile(name); |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | void CKadDlg::OnBnClickedBootstrapKnown(wxCommandEvent& WXUNUSED(evt)) |
|---|
| 227 | { |
|---|
| 228 | theApp->StartKad(); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | void CKadDlg::OnBnClickedDisconnectKad(wxCommandEvent& WXUNUSED(evt)) |
|---|
| 233 | { |
|---|
| 234 | theApp->StopKad(); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | void CKadDlg::OnBnClickedUpdateNodeList(wxCommandEvent& WXUNUSED(evt)) |
|---|
| 239 | { |
|---|
| 240 | if ( wxMessageBox( wxString(_("Are you sure you want to download a new nodes.dat file?\n")) + |
|---|
| 241 | _("Doing so will remove your current nodes and restart Kademlia connection.") |
|---|
| 242 | , _("Continue?"), wxICON_EXCLAMATION | wxYES_NO, this) == wxYES ) { |
|---|
| 243 | wxString strURL = this->GetNodesListUrlCtrl()->GetValue(); |
|---|
| 244 | |
|---|
| 245 | thePrefs::SetKadNodesUrl(strURL); |
|---|
| 246 | theApp->UpdateNotesDat(strURL); |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | // File_checked_for_headers |
|---|