Upstream tarball 10114
[amule.git] / src / amule-remote-gui.cpp
blob7c816e39e0b5a84904df05c99952166cb09c459a
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2005-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 //
6 // Any parts of this program derived from the xMule, lMule or eMule project,
7 // or contributed by third-party developers are copyrighted by their
8 // respective authors.
9 //
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <wx/ipc.h>
27 #include <wx/cmdline.h> // Needed for wxCmdLineParser
28 #include <wx/config.h> // Do_not_auto_remove (win32)
29 #include <wx/fileconf.h> // Needed for wxFileConfig
32 #include <common/Format.h>
33 #include <common/StringFunctions.h>
34 #include <common/MD5Sum.h>
37 #include <include/common/EventIDs.h>
40 #include "amule.h" // Interface declarations.
41 #include "amuleDlg.h" // Needed for CamuleDlg
42 #include "ClientCredits.h"
43 #include "ClientListCtrl.h"
44 #include "DataToText.h" // Needed for GetSoftName()
45 #include "DownloadListCtrl.h" // Needed for CDownloadListCtrl
46 #include "GuiEvents.h"
47 #ifdef ENABLE_IP2COUNTRY
48 #include "IP2Country.h" // Needed for IP2Country
49 #endif
50 #include "InternalEvents.h" // Needed for wxEVT_CORE_FINISHED_HTTP_DOWNLOAD
51 #include "Logger.h"
52 #include "muuli_wdr.h" // Needed for IDs
53 #include "PartFile.h" // Needed for CPartFile
54 #include "SearchDlg.h" // Needed for CSearchDlg
55 #include "Server.h" // Needed for GetListName
56 #include "ServerWnd.h" // Needed for CServerWnd
57 #include "SharedFilesCtrl.h" // Needed for CSharedFilesCtrl
58 #include "SharedFilesWnd.h" // Needed for CSharedFilesWnd
59 #include "TransferWnd.h" // Needed for CTransferWnd
60 #include "updownclient.h"
61 #include "ServerListCtrl.h" // Needed for CServerListCtrl
62 #include "ScopedPtr.h"
65 CEConnectDlg::CEConnectDlg()
67 wxDialog(theApp->amuledlg, -1, _("Connect to remote amule"), wxDefaultPosition)
69 CoreConnect(this, true);
71 wxString pref_host, pref_port;
72 wxConfig::Get()->Read(wxT("/EC/Host"), &pref_host, wxT("localhost"));
73 wxConfig::Get()->Read(wxT("/EC/Port"), &pref_port, wxT("4712"));
74 wxConfig::Get()->Read(wxT("/EC/Password"), &pwd_hash);
76 CastChild(ID_REMOTE_HOST, wxTextCtrl)->SetValue(pref_host);
77 CastChild(ID_REMOTE_PORT, wxTextCtrl)->SetValue(pref_port);
78 CastChild(ID_EC_PASSWD, wxTextCtrl)->SetValue(pwd_hash);
80 CentreOnParent();
84 wxString CEConnectDlg::PassHash()
86 return pwd_hash;
90 BEGIN_EVENT_TABLE(CEConnectDlg, wxDialog)
91 EVT_BUTTON(wxID_OK, CEConnectDlg::OnOK)
92 END_EVENT_TABLE()
95 void CEConnectDlg::OnOK(wxCommandEvent& evt)
97 wxString s_port = CastChild(ID_REMOTE_PORT, wxTextCtrl)->GetValue();
98 port = StrToLong(s_port);
100 host = CastChild(ID_REMOTE_HOST, wxTextCtrl)->GetValue();
101 passwd = CastChild(ID_EC_PASSWD, wxTextCtrl)->GetValue();
103 if (passwd != pwd_hash) {
104 pwd_hash = MD5Sum(passwd).GetHash();
106 m_save_user_pass = CastChild(ID_EC_SAVE, wxCheckBox)->IsChecked();
107 evt.Skip();
111 DEFINE_LOCAL_EVENT_TYPE(wxEVT_EC_INIT_DONE)
114 BEGIN_EVENT_TABLE(CamuleRemoteGuiApp, wxApp)
115 // Core timer
116 EVT_TIMER(ID_CORE_TIMER_EVENT, CamuleRemoteGuiApp::OnPollTimer)
118 EVT_CUSTOM(wxEVT_EC_CONNECTION, -1, CamuleRemoteGuiApp::OnECConnection)
119 EVT_CUSTOM(wxEVT_EC_INIT_DONE, -1, CamuleRemoteGuiApp::OnECInitDone)
121 EVT_MULE_NOTIFY(CamuleRemoteGuiApp::OnNotifyEvent)
123 #ifdef ENABLE_IP2COUNTRY
124 // HTTPDownload finished
125 EVT_MULE_INTERNAL(wxEVT_CORE_FINISHED_HTTP_DOWNLOAD, -1, CamuleRemoteGuiApp::OnFinishedHTTPDownload)
126 #endif
127 END_EVENT_TABLE()
130 IMPLEMENT_APP(CamuleRemoteGuiApp)
133 int CamuleRemoteGuiApp::OnExit()
135 StopTickTimer();
137 return wxApp::OnExit();
141 void CamuleRemoteGuiApp::OnPollTimer(wxTimerEvent&)
143 static int request_step = 0;
145 if (m_connect->RequestFifoFull()) {
146 return;
149 switch (request_step) {
150 case 0:
151 serverconnect->ReQuery();
152 serverlist->UpdateUserFileStatus(serverconnect->GetCurrentServer());
153 request_step++;
154 break;
155 case 1: {
156 CECPacket stats_req(EC_OP_STAT_REQ);
157 m_connect->SendRequest(&m_stats_updater, &stats_req);
158 amuledlg->ShowTransferRate();
159 request_step++;
160 break;
162 case 2:
163 if (amuledlg->m_sharedfileswnd->IsShown()) {
164 // update both downloads and shared files
165 knownfiles->DoRequery(EC_OP_GET_UPDATE, EC_TAG_KNOWNFILE);
166 } else if (amuledlg->m_serverwnd->IsShown()) {
167 //serverlist->FullReload(EC_OP_GET_SERVER_LIST);
168 } else if (amuledlg->m_transferwnd->IsShown()) {
169 // update both downloads and shared files
170 knownfiles->DoRequery(EC_OP_GET_UPDATE, EC_TAG_KNOWNFILE);
171 switch(amuledlg->m_transferwnd->clientlistctrl->GetListView()) {
172 case vtUploading:
173 uploadqueue->ReQueryUp();
174 break;
175 case vtQueued:
176 uploadqueue->ReQueryWait();
177 break;
178 case vtClients:
179 break;
180 case vtNone:
181 break;
183 amuledlg->m_transferwnd->ShowQueueCount(theStats::GetWaitingUserCount());
184 } else if (amuledlg->m_searchwnd->IsShown()) {
185 if (searchlist->m_curr_search != -1) {
186 searchlist->DoRequery(EC_OP_SEARCH_RESULTS, EC_TAG_SEARCHFILE);
189 // Back to the roots
190 request_step = 0;
191 break;
192 default:
193 AddLogLineCS(wxT("WTF?")); // should not happen. :-)
194 request_step = 0;
197 // Check for new links once per second.
198 static uint32 lastED2KLinkCheck = 0;
199 if (GetTickCount() - lastED2KLinkCheck >= 1000) {
200 AddLinksFromFile();
201 lastED2KLinkCheck = GetTickCount();
206 void CamuleRemoteGuiApp::OnFinishedHTTPDownload(CMuleInternalEvent& event)
208 #ifdef ENABLE_IP2COUNTRY
209 if (event.GetInt() == HTTP_GeoIP) {
210 amuledlg->IP2CountryDownloadFinished(event.GetExtraLong());
211 // If we updated, the dialog is already up. Redraw it to show the flags.
212 amuledlg->Refresh();
214 #endif
218 void CamuleRemoteGuiApp::ShutDown(wxCloseEvent &WXUNUSED(evt))
220 // Stop the Core Timer
221 delete poll_timer;
222 poll_timer = NULL;
224 // Destroy the EC socket
225 m_connect->Destroy();
226 m_connect = NULL;
229 if (amuledlg) {
230 amuledlg->DlgShutDown();
231 amuledlg->Destroy();
232 amuledlg = NULL;
237 bool CamuleRemoteGuiApp::OnInit()
239 StartTickTimer();
240 amuledlg = NULL;
242 // Get theApp
243 theApp = &wxGetApp();
245 // Handle uncaught exceptions
246 InstallMuleExceptionHandler();
248 // Parse cmdline arguments.
249 if (!InitCommon(AMULE_APP_BASE::argc, AMULE_APP_BASE::argv)) {
250 return false;
253 // Create the polling timer
254 poll_timer = new wxTimer(this,ID_CORE_TIMER_EVENT);
255 if (!poll_timer) {
256 AddLogLineCS(_("Fatal Error: Failed to create Poll Timer"));
257 OnExit();
260 m_connect = new CRemoteConnect(this);
262 glob_prefs = new CPreferencesRem(m_connect);
263 long enableZLIB;
264 wxConfig::Get()->Read(wxT("/EC/ZLIB"), &enableZLIB, 1);
265 m_connect->SetCapabilities(enableZLIB != 0, true, false); // ZLIB, UTF8 numbers, notification
267 InitCustomLanguages();
268 InitLocale(m_locale, StrLang2wx(thePrefs::GetLanguageID()));
270 if (ShowConnectionDialog()) {
271 AddLogLineNS(_("Going to event loop..."));
272 return true;
275 return false;
279 bool CamuleRemoteGuiApp::CryptoAvailable() const
281 return thePrefs::IsSecureIdentEnabled(); // good enough
285 bool CamuleRemoteGuiApp::ShowConnectionDialog()
287 dialog = new CEConnectDlg;
289 if (m_skipConnectionDialog) {
290 wxCommandEvent evt;
291 dialog->OnOK(evt);
292 } else if (dialog->ShowModal() != wxID_OK) {
293 dialog->Destroy();
295 return false;
297 AddLogLineNS(_("Connecting..."));
298 if (!m_connect->ConnectToCore(dialog->Host(), dialog->Port(),
299 dialog->Login(), dialog->PassHash(),
300 wxT("amule-remote"), wxT("0x0001"))) {
301 wxMessageBox(_("Connection failed "),_("ERROR"),wxOK);
303 return false;
306 return true;
310 void CamuleRemoteGuiApp::OnECConnection(wxEvent& event) {
311 wxECSocketEvent& evt = *((wxECSocketEvent*)&event);
312 AddLogLineNS(_("Remote GUI EC event handler"));
313 wxString reply = evt.GetServerReply();
314 AddLogLineM(true, reply);
315 if (evt.GetResult() == true) {
316 // Connected - go to next init step
317 glob_prefs->LoadRemote();
318 } else {
319 AddLogLineNS(_("Going down"));
320 if (dialog) { // connect failed
321 wxMessageBox(
322 (CFormat(_("Connection Failed. Unable to connect to %s:%d\n")) % dialog->Host() % dialog->Port()) + reply,
323 _("ERROR"), wxOK);
324 } else { // server disconnected (probably terminated) later
325 wxMessageBox(_("Connection closed - aMule has terminated probably."), _("ERROR"), wxOK);
327 ExitMainLoop();
332 void CamuleRemoteGuiApp::OnECInitDone(wxEvent& )
334 Startup();
338 void CamuleRemoteGuiApp::OnNotifyEvent(CMuleGUIEvent& evt)
340 evt.Notify();
344 void CamuleRemoteGuiApp::Startup() {
346 if (dialog->SaveUserPass()) {
347 wxConfig::Get()->Write(wxT("/EC/Host"), dialog->Host());
348 wxConfig::Get()->Write(wxT("/EC/Port"), dialog->Port());
349 wxConfig::Get()->Write(wxT("/EC/Password"), dialog->PassHash());
351 dialog->Destroy();
352 dialog = NULL;
354 m_ConnState = 0;
355 m_clientID = 0;
357 serverconnect = new CServerConnectRem(m_connect);
358 m_statistics = new CStatistics(*m_connect);
360 clientlist = new CClientListRem(m_connect);
361 searchlist = new CSearchListRem(m_connect);
362 serverlist = new CServerListRem(m_connect);
364 sharedfiles = new CSharedFilesRem(m_connect);
365 knownfiles = new CKnownFilesRem(m_connect);
367 // bugfix - do this before creating the uploadqueue
368 downloadqueue = new CDownQueueRem(m_connect);
369 uploadqueue = new CUpQueueRem(m_connect);
370 ipfilter = new CIPFilterRem(m_connect);
372 // Create main dialog
373 InitGui(m_geometryEnabled, m_geometryString);
375 // Forward wxLog events to CLogger
376 wxLog::SetActiveTarget(new CLoggerTarget);
377 serverlist->FullReload(EC_OP_GET_SERVER_LIST);
378 knownfiles->DoRequery(EC_OP_GET_UPDATE, EC_TAG_KNOWNFILE);
380 // Start the Poll Timer
381 poll_timer->Start(1000);
382 amuledlg->StartGuiTimer();
384 // Now activate GeoIP, so that the download dialog doesn't get destroyed immediately
385 #ifdef ENABLE_IP2COUNTRY
386 if (thePrefs::IsGeoIPEnabled()) {
387 amuledlg->m_IP2Country->Enable();
389 #endif
393 int CamuleRemoteGuiApp::ShowAlert(wxString msg, wxString title, int flags)
395 return CamuleGuiBase::ShowAlert(msg, title, flags);
399 void CamuleRemoteGuiApp::AddRemoteLogLine(const wxString& line)
401 amuledlg->AddLogLine(line);
404 int CamuleRemoteGuiApp::InitGui(bool geometry_enabled, wxString &geom_string)
406 CamuleGuiBase::InitGui(geometry_enabled, geom_string);
407 SetTopWindow(amuledlg);
408 AddLogLineN(_("Ready")); // The first log line after the window is up triggers output of all the ones before
409 return 0;
413 bool CamuleRemoteGuiApp::CopyTextToClipboard(wxString strText)
415 return CamuleGuiBase::CopyTextToClipboard(strText);
419 uint32 CamuleRemoteGuiApp::GetPublicIP()
421 return 0;
425 wxString CamuleRemoteGuiApp::GetLog(bool)
427 return wxEmptyString;
431 wxString CamuleRemoteGuiApp::GetServerLog(bool)
433 return wxEmptyString;
437 bool CamuleRemoteGuiApp::AddServer(CServer *, bool)
439 // #warning TODO: Add remote command
440 return true;
444 bool CamuleRemoteGuiApp::IsFirewalled() const
446 if (IsConnectedED2K() && !serverconnect->IsLowID()) {
447 return false;
450 return IsFirewalledKad();
454 bool CamuleRemoteGuiApp::IsConnectedED2K() const {
455 return serverconnect && serverconnect->IsConnected();
459 void CamuleRemoteGuiApp::StartKad() {
460 m_connect->StartKad();
464 void CamuleRemoteGuiApp::StopKad() {
465 m_connect->StopKad();
469 void CamuleRemoteGuiApp::BootstrapKad(uint32 ip, uint16 port)
471 CECPacket req(EC_OP_KAD_BOOTSTRAP_FROM_IP);
472 req.AddTag(CECTag(EC_TAG_BOOTSTRAP_IP, ip));
473 req.AddTag(CECTag(EC_TAG_BOOTSTRAP_PORT, port));
475 m_connect->SendPacket(&req);
479 void CamuleRemoteGuiApp::UpdateNotesDat(const wxString& url)
481 CECPacket req(EC_OP_KAD_UPDATE_FROM_URL);
482 req.AddTag(CECTag(EC_TAG_KADEMLIA_UPDATE_URL, url));
484 m_connect->SendPacket(&req);
488 void CamuleRemoteGuiApp::DisconnectED2K() {
489 if (IsConnectedED2K()) {
490 m_connect->DisconnectED2K();
495 uint32 CamuleRemoteGuiApp::GetED2KID() const
497 return serverconnect ? serverconnect->GetClientID() : 0;
501 uint32 CamuleRemoteGuiApp::GetID() const
503 return m_clientID;
507 void CamuleRemoteGuiApp::ShowUserCount() {
508 wxString buffer;
510 static const wxString s_singlenetstatusformat = _("Users: %s | Files: %s");
511 static const wxString s_bothnetstatusformat = _("Users: E: %s K: %s | Files: E: %s K: %s");
513 if (thePrefs::GetNetworkED2K() && thePrefs::GetNetworkKademlia()) {
514 buffer = CFormat(s_bothnetstatusformat) % CastItoIShort(theStats::GetED2KUsers()) % CastItoIShort(theStats::GetKadUsers()) % CastItoIShort(theStats::GetED2KFiles()) % CastItoIShort(theStats::GetKadFiles());
515 } else if (thePrefs::GetNetworkED2K()) {
516 buffer = CFormat(s_singlenetstatusformat) % CastItoIShort(theStats::GetED2KUsers()) % CastItoIShort(theStats::GetED2KFiles());
517 } else if (thePrefs::GetNetworkKademlia()) {
518 buffer = CFormat(s_singlenetstatusformat) % CastItoIShort(theStats::GetKadUsers()) % CastItoIShort(theStats::GetKadFiles());
519 } else {
520 buffer = _("No networks selected");
523 Notify_ShowUserCount(buffer);
528 * Preferences: holds both local and remote settings.
530 * First, everything is loaded from local config file. Later, settings
531 * that are relevant on remote side only are loaded thru EC
533 CPreferencesRem::CPreferencesRem(CRemoteConnect *conn)
535 m_conn = conn;
538 // Settings queried from remote side
540 m_exchange_send_selected_prefs =
541 EC_PREFS_GENERAL |
542 EC_PREFS_CONNECTIONS |
543 EC_PREFS_MESSAGEFILTER |
544 EC_PREFS_ONLINESIG |
545 EC_PREFS_SERVERS |
546 EC_PREFS_FILES |
547 EC_PREFS_SRCDROP |
548 EC_PREFS_DIRECTORIES |
549 EC_PREFS_SECURITY |
550 EC_PREFS_CORETWEAKS |
551 EC_PREFS_REMOTECONTROLS |
552 EC_PREFS_KADEMLIA;
553 m_exchange_recv_selected_prefs =
554 m_exchange_send_selected_prefs |
555 EC_PREFS_CATEGORIES;
559 void CPreferencesRem::HandlePacket(const CECPacket *packet)
561 ((CEC_Prefs_Packet *)packet)->Apply();
563 const CECTag *cat_tags = packet->GetTagByName(EC_TAG_PREFS_CATEGORIES);
564 if (cat_tags) {
565 for (CECTag::const_iterator it = cat_tags->begin(); it != cat_tags->end(); it++) {
566 const CECTag &cat_tag = *it;
567 Category_Struct *cat = new Category_Struct;
568 cat->title = cat_tag.GetTagByName(EC_TAG_CATEGORY_TITLE)->GetStringData();
569 cat->path = CPath(cat_tag.GetTagByName(EC_TAG_CATEGORY_PATH)->GetStringData());
570 cat->comment = cat_tag.GetTagByName(EC_TAG_CATEGORY_COMMENT)->GetStringData();
571 cat->color = cat_tag.GetTagByName(EC_TAG_CATEGORY_COLOR)->GetInt();
572 cat->prio = cat_tag.GetTagByName(EC_TAG_CATEGORY_PRIO)->GetInt();
573 theApp->glob_prefs->AddCat(cat);
575 } else {
576 Category_Struct *cat = new Category_Struct;
577 cat->title = _("All");
578 cat->color = 0;
579 cat->prio = PR_NORMAL;
580 theApp->glob_prefs->AddCat(cat);
582 wxECInitDoneEvent event;
583 theApp->AddPendingEvent(event);
588 bool CPreferencesRem::LoadRemote()
591 // override local settings with remote
592 CECPacket req(EC_OP_GET_PREFERENCES, EC_DETAIL_UPDATE);
594 // bring categories too
595 req.AddTag(CECTag(EC_TAG_SELECT_PREFS, m_exchange_recv_selected_prefs));
597 m_conn->SendRequest(this, &req);
599 return true;
603 void CPreferencesRem::SendToRemote()
605 CEC_Prefs_Packet pref_packet(m_exchange_send_selected_prefs, EC_DETAIL_UPDATE, EC_DETAIL_FULL);
606 m_conn->SendPacket(&pref_packet);
610 class CCatHandler : public CECPacketHandlerBase {
611 virtual void HandlePacket(const CECPacket *packet);
615 void CCatHandler::HandlePacket(const CECPacket *packet)
617 if (packet->GetOpCode() == EC_OP_FAILED) {
618 const CECTag * catTag = packet->GetTagByName(EC_TAG_CATEGORY);
619 const CECTag * pathTag = packet->GetTagByName(EC_TAG_CATEGORY_PATH);
620 if (catTag && pathTag && catTag->GetInt() < theApp->glob_prefs->GetCatCount()) {
621 int cat = catTag->GetInt();
622 Category_Struct* cs = theApp->glob_prefs->GetCategory(cat);
623 wxMessageBox(CFormat(_("Can't create directory '%s' for category '%s', keeping directory '%s'."))
624 % cs->path.GetPrintable() % cs->title % pathTag->GetStringData(),
625 _("ERROR"), wxOK);
626 cs->path = CPath(pathTag->GetStringData());
627 theApp->amuledlg->m_transferwnd->UpdateCategory(cat);
628 theApp->amuledlg->m_transferwnd->downloadlistctrl->Refresh();
631 delete this;
635 bool CPreferencesRem::CreateCategory(
636 Category_Struct *& category,
637 const wxString& name,
638 const CPath& path,
639 const wxString& comment,
640 uint32 color,
641 uint8 prio)
643 CECPacket req(EC_OP_CREATE_CATEGORY);
644 CEC_Category_Tag tag(0xffffffff, name, path.GetRaw(), comment, color, prio);
645 req.AddTag(tag);
646 m_conn->SendRequest(new CCatHandler, &req);
648 category = new Category_Struct();
649 category->path = path;
650 category->title = name;
651 category->comment = comment;
652 category->color = color;
653 category->prio = prio;
655 AddCat(category);
657 return true;
661 bool CPreferencesRem::UpdateCategory(
662 uint8 cat,
663 const wxString& name,
664 const CPath& path,
665 const wxString& comment,
666 uint32 color,
667 uint8 prio)
669 CECPacket req(EC_OP_UPDATE_CATEGORY);
670 CEC_Category_Tag tag(cat, name, path.GetRaw(), comment, color, prio);
671 req.AddTag(tag);
672 m_conn->SendRequest(new CCatHandler, &req);
674 Category_Struct *category = m_CatList[cat];
675 category->path = path;
676 category->title = name;
677 category->comment = comment;
678 category->color = color;
679 category->prio = prio;
681 return true;
685 void CPreferencesRem::RemoveCat(uint8 cat)
687 CECPacket req(EC_OP_DELETE_CATEGORY);
688 CEC_Category_Tag tag(cat, EC_DETAIL_CMD);
689 req.AddTag(tag);
690 m_conn->SendPacket(&req);
691 CPreferences::RemoveCat(cat);
696 // Container implementation
698 CServerConnectRem::CServerConnectRem(CRemoteConnect *conn)
700 m_CurrServer = 0;
701 m_Conn = conn;
705 void CServerConnectRem::ConnectToAnyServer()
707 CECPacket req(EC_OP_SERVER_CONNECT);
708 m_Conn->SendPacket(&req);
712 void CServerConnectRem::StopConnectionTry()
714 // lfroen: isn't Disconnect the same ?
718 void CServerConnectRem::Disconnect()
720 CECPacket req(EC_OP_SERVER_DISCONNECT);
721 m_Conn->SendPacket(&req);
725 void CServerConnectRem::ConnectToServer(CServer *server)
727 m_Conn->ConnectED2K(server->GetIP(), server->GetPort());
731 bool CServerConnectRem::ReQuery()
733 CECPacket stat_req(EC_OP_GET_CONNSTATE);
734 m_Conn->SendRequest(this, &stat_req);
736 return true;
740 void CServerConnectRem::HandlePacket(const CECPacket *packet)
742 CEC_ConnState_Tag *tag =
743 (CEC_ConnState_Tag *)packet->GetTagByName(EC_TAG_CONNSTATE);
744 if (!tag) {
745 return;
748 theApp->m_ConnState = 0;
749 CServer *server;
750 m_ID = tag->GetEd2kId();
751 theApp->m_clientID = tag->GetClientId();
753 if (tag->IsConnectedED2K()) {
754 CECTag *srvtag = tag->GetTagByName(EC_TAG_SERVER);
755 if (!srvtag) {
756 return;
758 server = theApp->serverlist->GetByID(srvtag->GetIPv4Data().IP());
759 if (m_CurrServer && (server != m_CurrServer)) {
760 theApp->amuledlg->m_serverwnd->serverlistctrl->
761 HighlightServer(m_CurrServer, false);
763 theApp->amuledlg->m_serverwnd->serverlistctrl->
764 HighlightServer(server, true);
765 m_CurrServer = server;
766 theApp->m_ConnState |= CONNECTED_ED2K;
767 } else {
768 if ( m_CurrServer ) {
769 theApp->amuledlg->m_serverwnd->serverlistctrl->
770 HighlightServer(m_CurrServer, false);
771 m_CurrServer = 0;
775 if (tag->IsConnectedKademlia()) {
776 if (tag->IsKadFirewalled()) {
777 theApp->m_ConnState |= CONNECTED_KAD_FIREWALLED;
778 } else {
779 theApp->m_ConnState |= CONNECTED_KAD_OK;
781 } else {
782 if (tag->IsKadRunning()) {
783 theApp->m_ConnState |= CONNECTED_KAD_NOT;
787 theApp->amuledlg->ShowConnectionState();
792 * Server list: host list of ed2k servers.
794 CServerListRem::CServerListRem(CRemoteConnect *conn)
796 CRemoteContainer<CServer, uint32, CEC_Server_Tag>(conn, false)
801 void CServerListRem::HandlePacket(const CECPacket *packet)
803 CRemoteContainer<CServer, uint32, CEC_Server_Tag>::HandlePacket(packet);
804 ReloadControl();
808 void CServerListRem::UpdateServerMetFromURL(wxString url)
810 CECPacket req(EC_OP_SERVER_UPDATE_FROM_URL);
811 req.AddTag(CECTag(EC_TAG_SERVERS_UPDATE_URL, url));
813 m_conn->SendPacket(&req);
817 void CServerListRem::SaveServerMet()
819 // lfroen: stub, nothing to do
823 void CServerListRem::FilterServers()
825 // FIXME: add code
826 //wxFAIL;
830 void CServerListRem::RemoveServer(CServer* server)
832 m_conn->RemoveServer(server->GetIP(),server->GetPort());
836 void CServerListRem::UpdateUserFileStatus(CServer *server)
838 if (server) {
839 m_TotalUser = server->GetUsers();
840 m_TotalFile = server->GetFiles();
845 CServer *CServerListRem::GetServerByAddress(const wxString& WXUNUSED(address), uint16 WXUNUSED(port)) const
847 // It's ok to return 0 for context where this code is used in remote gui
848 return 0;
851 CServer *CServerListRem::GetServerByIPTCP(uint32 WXUNUSED(nIP), uint16 WXUNUSED(nPort)) const
853 // It's ok to return 0 for context where this code is used in remote gui
854 return 0;
857 CServer *CServerListRem::CreateItem(CEC_Server_Tag *tag)
859 return new CServer(tag);
863 void CServerListRem::DeleteItem(CServer *in_srv)
865 CScopedPtr<CServer> srv(in_srv);
866 theApp->amuledlg->m_serverwnd->serverlistctrl->RemoveServer(srv.get());
870 uint32 CServerListRem::GetItemID(CServer *server)
872 return server->GetIP();
876 void CServerListRem::ProcessItemUpdate(CEC_Server_Tag *, CServer *)
878 // server list is always realoaded from scratch
879 wxFAIL;
883 void CServerListRem::ReloadControl()
885 for(iterator it = begin(); it != end(); it++) {
886 CServer *srv = *it;
887 theApp->amuledlg->m_serverwnd->serverlistctrl->RefreshServer(srv);
892 CIPFilterRem::CIPFilterRem(CRemoteConnect* conn)
894 m_conn = conn;
898 void CIPFilterRem::Reload()
900 CECPacket req(EC_OP_IPFILTER_RELOAD);
901 m_conn->SendPacket(&req);
905 void CIPFilterRem::Update(wxString url)
907 CECPacket req(EC_OP_IPFILTER_UPDATE);
908 req.AddTag(CECTag(EC_TAG_STRING, url));
910 m_conn->SendPacket(&req);
915 * Shared files list
917 CSharedFilesRem::CSharedFilesRem(CRemoteConnect *conn)
919 m_conn = conn;
923 void CSharedFilesRem::Reload(bool, bool)
925 CECPacket req(EC_OP_SHAREDFILES_RELOAD);
927 m_conn->SendPacket(&req);
931 void CSharedFilesRem::AddFilesFromDirectory(const CPath& path)
933 CECPacket req(EC_OP_SHAREDFILES_ADD_DIRECTORY);
935 req.AddTag(CECTag(EC_TAG_PREFS_DIRECTORIES, path.GetRaw()));
937 m_conn->SendPacket(&req);
941 bool CSharedFilesRem::RenameFile(CKnownFile* file, const CPath& newName)
943 // We use the printable name, as the filename originated from user input,
944 // and the filesystem name might not be valid on the remote host.
945 const wxString strNewName = newName.GetPrintable();
947 CECPacket request(EC_OP_RENAME_FILE);
948 request.AddTag(CECTag(EC_TAG_KNOWNFILE, file->GetFileHash()));
949 request.AddTag(CECTag(EC_TAG_PARTFILE_NAME, strNewName));
951 m_conn->SendPacket(&request);
953 return true;
957 void CKnownFilesRem::DeleteItem(CKnownFile * file)
959 uint32 id = file->ECID();
960 if (theApp->sharedfiles->count(id)) {
961 theApp->amuledlg->m_sharedfileswnd->sharedfilesctrl->RemoveFile(file);
962 theApp->sharedfiles->erase(id);
964 if (theApp->downloadqueue->count(id)) {
965 theApp->amuledlg->m_transferwnd->downloadlistctrl->RemoveFile((CPartFile *) file);
966 theApp->downloadqueue->erase(id);
968 delete file;
972 uint32 CKnownFilesRem::GetItemID(CKnownFile *file)
974 return file->ECID();
978 void CKnownFilesRem::ProcessItemUpdate(CEC_SharedFile_Tag *tag, CKnownFile *file)
980 CECTag *parttag = tag->GetTagByName(EC_TAG_PARTFILE_PART_STATUS);
981 if (parttag) {
982 const uint8 *data = file->m_partStatus.Decode(
983 (uint8 *)parttag->GetTagData(),
984 parttag->GetTagDataLen());
985 for(int i = 0; i < file->GetPartCount(); ++i) {
986 file->m_AvailPartFrequency[i] = data[i];
989 tag->GetRequests(&file->statistic.requested);
990 tag->GetAllRequests(&file->statistic.alltimerequested);
991 tag->GetAccepts(&file->statistic.accepted);
992 tag->GetAllAccepts(&file->statistic.alltimeaccepted);
993 tag->GetXferred(&file->statistic.transferred);
994 tag->GetAllXferred(&file->statistic.alltimetransferred);
995 tag->UpPrio(&file->m_iUpPriorityEC);
996 if (file->m_iUpPriorityEC >= 10) {
997 file->m_iUpPriority = file->m_iUpPriorityEC - 10;
998 file->m_bAutoUpPriority = true;
999 } else {
1000 file->m_iUpPriority = file->m_iUpPriorityEC;
1001 file->m_bAutoUpPriority = false;
1003 tag->GetCompleteSourcesLow(&file->m_nCompleteSourcesCountLo);
1004 tag->GetCompleteSourcesHigh(&file->m_nCompleteSourcesCountHi);
1005 tag->GetCompleteSources(&file->m_nCompleteSourcesCount);
1007 tag->GetOnQueue(&file->m_queuedCount);
1009 requested += file->statistic.requested;
1010 transferred += file->statistic.transferred;
1011 accepted += file->statistic.transferred;
1013 theApp->amuledlg->m_sharedfileswnd->sharedfilesctrl->UpdateItem(file);
1015 if (file->IsPartFile()) {
1016 ProcessItemUpdatePartfile((CEC_PartFile_Tag *) tag, (CPartFile *) file);
1020 void CSharedFilesRem::SetFilePrio(CKnownFile *file, uint8 prio)
1022 CECPacket req(EC_OP_SHARED_SET_PRIO);
1024 CECTag hashtag(EC_TAG_PARTFILE, file->GetFileHash());
1025 hashtag.AddTag(CECTag(EC_TAG_PARTFILE_PRIO, prio));
1027 req.AddTag(hashtag);
1029 m_conn->SendPacket(&req);
1032 void CKnownFilesRem::ProcessUpdate(const CECPacket *reply, CECPacket *, int)
1034 requested = 0;
1035 transferred = 0;
1036 accepted = 0;
1038 std::set<uint32> core_files;
1039 for (CECPacket::const_iterator it = reply->begin(); it != reply->end(); it++) {
1040 CEC_SharedFile_Tag *tag = (CEC_SharedFile_Tag *) & *it;
1041 uint32 id = tag->ID();
1042 core_files.insert(id);
1043 if ( m_items_hash.count(id) ) {
1044 // Item already known: update it
1045 ProcessItemUpdate(tag, m_items_hash[id]);
1046 } else {
1047 CKnownFile * newFile;
1048 if (tag->GetTagName() == EC_TAG_PARTFILE) {
1049 CPartFile *file = new CPartFile((CEC_PartFile_Tag *) tag);
1050 ProcessItemUpdate(tag, file);
1051 // GUI doesn't allow clear finished files well at the moment,
1052 // so don't show finished files for now until GUI gets unlocked.
1053 // Files completing while GUI is open will still show.
1054 if (file->IsPartFile()) {
1055 (*theApp->downloadqueue)[id] = file;
1056 theApp->amuledlg->m_transferwnd->downloadlistctrl->AddFile(file);
1058 newFile = file;
1059 } else {
1060 newFile = new CKnownFile(tag);
1061 ProcessItemUpdate(tag, newFile);
1062 (*theApp->sharedfiles)[id] = newFile;
1063 theApp->amuledlg->m_sharedfileswnd->sharedfilesctrl->ShowFile(newFile);
1065 AddItem(newFile);
1068 // remove items no longer present
1069 for(iterator it = begin(); it != end();) {
1070 iterator it2 = it++;
1071 if (!core_files.count(GetItemID(*it2))) {
1072 RemoveItem(it2); // This calls DeleteItem, where it is removed from lists and views.
1077 CKnownFilesRem::CKnownFilesRem(CRemoteConnect * conn) : CRemoteContainer<CKnownFile, uint32, CEC_SharedFile_Tag>(conn, true)
1079 requested = 0;
1080 transferred = 0;
1081 accepted = 0;
1086 * List of uploading and waiting clients.
1088 CUpDownClientListRem::CUpDownClientListRem(CRemoteConnect *conn, int viewtype)
1090 CRemoteContainer<CUpDownClient, uint32, CEC_UpDownClient_Tag>(conn, true)
1092 m_viewtype = viewtype;
1096 CUpDownClient::CUpDownClient(CEC_UpDownClient_Tag *tag) : CECID(tag->ID())
1098 m_bRemoteQueueFull = false;
1099 m_nUserIDHybrid = tag->UserID();
1100 m_Username = tag->ClientName();
1101 m_clientSoft = tag->ClientSoftware();
1102 m_clientVersionString = GetSoftName(m_clientSoft);
1103 m_clientSoftString = m_clientVersionString;
1104 m_bEmuleProtocol = false;
1106 // The functions to retrieve m_clientVerString information are
1107 // currently in BaseClient.cpp, which is not linked in remote-gui app.
1108 // So, in the meantime, we use a tag sent from core.
1109 m_clientVerString = tag->SoftVerStr();
1110 m_strModVersion = wxEmptyString;
1111 wxString clientModString;
1112 if (!clientModString.IsEmpty()) {
1113 m_clientVerString += wxT(" - ") + clientModString;
1115 m_fullClientVerString = m_clientSoftString + wxT(" ") + m_clientVerString;
1117 // User hash
1118 m_UserHash = tag->UserHash();
1120 // User IP:Port
1121 m_nConnectIP = m_dwUserIP = tag->UserIP();
1122 m_nUserPort = tag->UserPort();
1123 m_FullUserIP = m_nConnectIP;
1125 // Server IP:Port
1126 m_dwServerIP = tag->ServerIP();
1127 m_nServerPort = tag->ServerPort();
1128 m_ServerName = tag->ServerName();
1130 m_Friend = 0;
1131 if (tag->HaveFile()) {
1132 m_uploadingfile = theApp->knownfiles->GetByID(tag->FileID());
1133 } else {
1134 m_uploadingfile = NULL;
1137 m_nCurSessionUp = 0;
1139 credits = new CClientCredits(new CreditStruct());
1142 uint16 CUpQueueRem::GetWaitingPosition(const CUpDownClient *client) const
1144 return client->GetWaitingPosition();
1147 /* Warning: do common base */
1150 bool CUpDownClient::IsIdentified() const
1152 return m_identState == IS_IDENTIFIED;
1156 bool CUpDownClient::IsBadGuy() const
1158 return m_identState == IS_IDBADGUY;
1162 bool CUpDownClient::SUIFailed() const
1164 return m_identState == IS_IDFAILED;
1168 bool CUpDownClient::SUINeeded() const
1170 return m_identState == IS_IDNEEDED;
1174 bool CUpDownClient::SUINotSupported() const
1176 return m_identState == IS_NOTAVAILABLE;
1180 uint64 CUpDownClient::GetDownloadedTotal() const
1182 return credits->GetDownloadedTotal();
1186 uint64 CUpDownClient::GetUploadedTotal() const
1188 return credits->GetUploadedTotal();
1192 double CUpDownClient::GetScoreRatio() const
1194 return credits->GetScoreRatio(GetIP(), theApp->CryptoAvailable());
1197 /* End Warning */
1200 CUpDownClient::~CUpDownClient()
1202 delete credits;
1206 CUpDownClient *CUpDownClientListRem::CreateItem(CEC_UpDownClient_Tag *tag)
1208 CUpDownClient *client = new CUpDownClient(tag);
1209 ProcessItemUpdate(tag, client);
1211 theApp->amuledlg->m_transferwnd->clientlistctrl->InsertClient(client, (ViewType)m_viewtype);
1213 return client;
1217 void CUpDownClientListRem::DeleteItem(CUpDownClient *client)
1219 theApp->amuledlg->m_transferwnd->clientlistctrl->
1220 RemoveClient(client, (ViewType)m_viewtype);
1221 delete client;
1225 uint32 CUpDownClientListRem::GetItemID(CUpDownClient *client)
1227 return client->ECID();
1231 void CUpDownClientListRem::ProcessItemUpdate(
1232 CEC_UpDownClient_Tag *tag,
1233 CUpDownClient *client)
1235 tag->UserID(&client->m_nUserIDHybrid);
1236 tag->GetCurrentIdentState(&client->m_identState);
1237 tag->HasObfuscatedConnection(&client->m_hasbeenobfuscatinglately);
1238 tag->HasExtendedProtocol(&client->m_bEmuleProtocol);
1240 tag->WaitingPosition(&client->m_waitingPosition);
1241 tag->RemoteQueueRank(&client->m_nRemoteQueueRank);
1242 client->m_bRemoteQueueFull = client->m_nRemoteQueueRank == 0xffff;
1243 tag->AskedCount(&client->m_cAsked);
1245 tag->ClientDownloadState(&client->m_nDownloadState);
1246 tag->ClientUploadState(&client->m_nUploadState);
1248 tag->SpeedUp(&client->m_nUpDatarate);
1249 if ( client->m_nDownloadState == DS_DOWNLOADING ) {
1250 tag->SpeedDown(&client->kBpsDown);
1251 } else {
1252 client->kBpsDown = 0;
1255 tag->WaitTime(&client->m_WaitTime);
1256 tag->XferTime(&client->m_UpStartTimeDelay);
1257 tag->LastReqTime(&client->m_dwLastUpRequest);
1258 tag->QueueTime(&client->m_WaitStartTime);
1260 CreditStruct *credit_struct =
1261 (CreditStruct *)client->credits->GetDataStruct();
1262 tag->XferUp(&credit_struct->uploaded);
1263 tag->XferUpSession(&client->m_nTransferredUp);
1265 tag->XferDown(&credit_struct->downloaded);
1266 tag->XferDownSession(&client->m_nTransferredDown);
1268 tag->Score(&client->m_score);
1269 tag->Rating(&client->m_rating);
1271 theApp->amuledlg->m_transferwnd->clientlistctrl->UpdateClient(client,
1272 theApp->amuledlg->m_transferwnd->clientlistctrl->GetListView()); // Fixme, Listview shouldn't be required as parameter
1276 CUpQueueRem::CUpQueueRem(CRemoteConnect *conn)
1278 m_up_list(conn, vtUploading), m_wait_list(conn, vtQueued)
1284 * Download queue container: hold PartFiles with progress status
1289 bool CDownQueueRem::AddLink(const wxString &link, uint8 cat)
1291 CECPacket req(EC_OP_ADD_LINK);
1292 CECTag link_tag(EC_TAG_STRING, link);
1293 link_tag.AddTag(CECTag(EC_TAG_PARTFILE_CAT, cat));
1294 req.AddTag(link_tag);
1296 m_conn->SendPacket(&req);
1297 return true;
1301 void CDownQueueRem::ResetCatParts(int cat)
1303 // Called when category is deleted. Command will be performed on the remote side,
1304 // but files still should be updated here right away, or drawing errors (colour not available)
1305 // will happen.
1306 for (iterator it = begin(); it != end(); it++) {
1307 CPartFile* file = it->second;
1309 if ( file->GetCategory() == cat ) {
1310 // Reset the category
1311 file->SetCategory( 0 );
1312 } else if ( file->GetCategory() > cat ) {
1313 // Set to the new position of the original category
1314 file->SetCategory( file->GetCategory() - 1 );
1321 void CKnownFilesRem::ProcessItemUpdatePartfile(CEC_PartFile_Tag *tag, CPartFile *file)
1324 // update status
1326 tag->Speed(&file->m_kbpsDown);
1327 file->kBpsDown = file->m_kbpsDown / 1024.0;
1329 tag->SizeXfer(&file->transferred);
1330 tag->SizeDone(&file->completedsize);
1331 tag->SourceXferCount(&file->transferingsrc);
1332 tag->SourceNotCurrCount(&file->m_notCurrentSources);
1333 tag->SourceCount(&file->m_source_count);
1334 tag->SourceCountA4AF(&file->m_a4af_source_count);
1335 tag->FileStatus(&file->status);
1336 tag->Stopped(&file->m_stopped);
1338 tag->LastSeenComplete(&file->lastseencomplete);
1339 tag->LastDateChanged(&file->m_lastDateChanged);
1340 tag->DownloadActiveTime(&file->m_nDlActiveTime);
1341 tag->AvailablePartCount(&file->m_availablePartsCount);
1342 tag->Shared(&file->m_isShared);
1344 tag->GetLostDueToCorruption(&file->m_iLostDueToCorruption);
1345 tag->GetGainDueToCompression(&file->m_iGainDueToCompression);
1346 tag->TotalPacketsSavedDueToICH(&file->m_iTotalPacketsSavedDueToICH);
1348 tag->FileCat(&file->m_category);
1350 tag->DownPrio(&file->m_iDownPriorityEC);
1351 if ( file->m_iDownPriorityEC >= 10 ) {
1352 file->m_iDownPriority = file->m_iDownPriorityEC - 10;
1353 file->m_bAutoDownPriority = true;
1354 } else {
1355 file->m_iDownPriority = file->m_iDownPriorityEC;
1356 file->m_bAutoDownPriority = false;
1359 file->percentcompleted = (100.0*file->GetCompletedSize()) / file->GetFileSize();
1362 // Copy part/gap status
1364 CECTag *gaptag = tag->GetTagByName(EC_TAG_PARTFILE_GAP_STATUS);
1365 CECTag *parttag = tag->GetTagByName(EC_TAG_PARTFILE_PART_STATUS);
1366 CECTag *reqtag = tag->GetTagByName(EC_TAG_PARTFILE_REQ_STATUS);
1367 if (gaptag || parttag || reqtag) {
1368 PartFileEncoderData &encoder = file->m_PartFileEncoderData;
1370 if (gaptag) {
1371 ArrayOfUInts64 gaps;
1372 encoder.DecodeGaps(gaptag, gaps);
1373 int gap_size = gaps.size() / 2;
1374 // clear gaplist
1375 file->m_gaplist.Init(file->GetFileSize(), false);
1377 // and refill it
1378 for (int j = 0; j < gap_size; j++) {
1379 file->m_gaplist.AddGap(gaps[2*j], gaps[2*j+1]);
1382 if (parttag) {
1383 encoder.DecodeParts(parttag, file->m_SrcpartFrequency);
1384 // sanity check
1385 wxASSERT (file->m_SrcpartFrequency.size() == file->GetPartCount());
1387 if (reqtag) {
1388 ArrayOfUInts64 reqs;
1389 encoder.DecodeReqs(reqtag, reqs);
1390 int req_size = reqs.size() / 2;
1391 // clear reqlist
1392 DeleteContents(file->m_requestedblocks_list);
1394 // and refill it
1395 for (int j = 0; j < req_size; j++) {
1396 Requested_Block_Struct* block = new Requested_Block_Struct;
1397 block->StartOffset = reqs[2*j];
1398 block->EndOffset = reqs[2*j+1];
1399 file->m_requestedblocks_list.push_back(block);
1404 // Get source names and counts
1405 CECTag *srcnametag = tag->GetTagByName(EC_TAG_PARTFILE_SOURCE_NAMES);
1406 if (srcnametag) {
1407 SourcenameItemMap &map = file->GetSourcenameItemMap();
1408 for (CECTag::const_iterator it = srcnametag->begin(); it != srcnametag->end(); it++) {
1409 uint32 key = it->GetInt();
1410 int count = it->GetTagByNameSafe(EC_TAG_PARTFILE_SOURCE_NAMES_COUNTS)->GetInt();
1411 if (count == 0) {
1412 map.erase(key);
1413 } else {
1414 SourcenameItem &item = map[key];
1415 item.count = count;
1416 const CECTag *nametag = it->GetTagByName(EC_TAG_PARTFILE_SOURCE_NAMES);
1417 if (nametag) {
1418 item.name = nametag->GetStringData();
1424 // Get comments
1425 CECTag *commenttag = tag->GetTagByName(EC_TAG_PARTFILE_COMMENTS);
1426 if (commenttag) {
1427 file->ClearFileRatingList();
1428 for (CECTag::const_iterator it = commenttag->begin(); it != commenttag->end(); ) {
1429 wxString u = (it++)->GetStringData();
1430 wxString f = (it++)->GetStringData();
1431 int r = (it++)->GetInt();
1432 wxString c = (it++)->GetStringData();
1433 file->AddFileRatingList(u, f, r, c);
1435 file->UpdateFileRatingCommentAvail();
1438 theApp->amuledlg->m_transferwnd->downloadlistctrl->UpdateItem(file);
1440 // If file is shared check if it is already listed in shared files.
1441 // If not, add it and show it.
1442 if (file->IsShared() && !theApp->sharedfiles->count(file->ECID())) {
1443 (*theApp->sharedfiles)[file->ECID()] = file;
1444 theApp->amuledlg->m_sharedfileswnd->sharedfilesctrl->ShowFile(file);
1449 void CDownQueueRem::SendFileCommand(CPartFile *file, ec_tagname_t cmd)
1451 CECPacket req(cmd);
1452 req.AddTag(CECTag(EC_TAG_PARTFILE, file->GetFileHash()));
1454 m_conn->SendPacket(&req);
1458 void CDownQueueRem::Prio(CPartFile *file, uint8 prio)
1460 CECPacket req(EC_OP_PARTFILE_PRIO_SET);
1462 CECTag hashtag(EC_TAG_PARTFILE, file->GetFileHash());
1463 hashtag.AddTag(CECTag(EC_TAG_PARTFILE_PRIO, prio));
1464 req.AddTag(hashtag);
1466 m_conn->SendPacket(&req);
1470 void CDownQueueRem::AutoPrio(CPartFile *file, bool flag)
1472 CECPacket req(EC_OP_PARTFILE_PRIO_SET);
1474 CECTag hashtag(EC_TAG_PARTFILE, file->GetFileHash());
1476 hashtag.AddTag(CECTag(EC_TAG_PARTFILE_PRIO,
1477 (uint8)(flag ? PR_AUTO : file->GetDownPriority())));
1478 req.AddTag(hashtag);
1480 m_conn->SendPacket(&req);
1484 void CDownQueueRem::Category(CPartFile *file, uint8 cat)
1486 CECPacket req(EC_OP_PARTFILE_SET_CAT);
1487 file->SetCategory(cat);
1489 CECTag hashtag(EC_TAG_PARTFILE, file->GetFileHash());
1490 hashtag.AddTag(CECTag(EC_TAG_PARTFILE_CAT, cat));
1491 req.AddTag(hashtag);
1493 m_conn->SendPacket(&req);
1497 void CDownQueueRem::AddSearchToDownload(CSearchFile* file, uint8 category)
1499 CECPacket req(EC_OP_DOWNLOAD_SEARCH_RESULT);
1500 CECTag hashtag(EC_TAG_PARTFILE, file->GetFileHash());
1501 hashtag.AddTag(CECTag(EC_TAG_PARTFILE_CAT, category));
1502 req.AddTag(hashtag);
1504 m_conn->SendPacket(&req);
1508 // This is an ugly hack and to be deleted when GUI is unlocked!
1509 // (replacing iterate-by-index with iteration)
1510 // It will only work for linear iteration starting with 0 (as used in CTransferWnd::UpdateCategory)
1511 CPartFile* CDownQueueRem::GetFileByIndex(unsigned int idx)
1513 static CDownQueueRem::iterator it;
1514 if (idx == 0) {
1515 it = begin();
1517 if (it == end()) {
1518 return NULL;
1520 return (it++)->second;
1524 CClientListRem::CClientListRem(CRemoteConnect *conn)
1526 m_conn = conn;
1530 void CClientListRem::FilterQueues()
1532 // FIXME: add code
1533 //wxFAIL;
1537 CSearchListRem::CSearchListRem(CRemoteConnect *conn) : CRemoteContainer<CSearchFile, uint32, CEC_SearchFile_Tag>(conn, true)
1539 m_curr_search = -1;
1543 wxString CSearchListRem::StartNewSearch(
1544 uint32* nSearchID, SearchType search_type,
1545 const CSearchList::CSearchParams& params)
1547 CECPacket search_req(EC_OP_SEARCH_START);
1548 EC_SEARCH_TYPE ec_search_type = EC_SEARCH_LOCAL;
1549 switch(search_type) {
1550 case LocalSearch: ec_search_type = EC_SEARCH_LOCAL; break;
1551 case GlobalSearch: ec_search_type = EC_SEARCH_GLOBAL; break;
1552 case KadSearch: ec_search_type = EC_SEARCH_KAD; break;
1554 search_req.AddTag(
1555 CEC_Search_Tag(params.searchString, ec_search_type,
1556 params.typeText, params.extension, params.availability,
1557 params.minSize, params.maxSize));
1559 m_conn->SendPacket(&search_req);
1560 m_curr_search = *(nSearchID); // No kad remote search yet.
1562 Flush();
1564 return wxEmptyString; // EC reply will have the error mesg is needed.
1568 void CSearchListRem::StopGlobalSearch()
1570 if (m_curr_search != -1) {
1571 CECPacket search_req(EC_OP_SEARCH_STOP);
1572 m_conn->SendPacket(&search_req);
1577 void CSearchListRem::StopKadSearch()
1579 // FIXME implementation needed
1583 void CSearchListRem::HandlePacket(const CECPacket *packet)
1585 if ( packet->GetOpCode() == EC_OP_SEARCH_PROGRESS ) {
1586 CoreNotify_Search_Update_Progress(packet->GetFirstTagSafe()->GetInt());
1587 } else {
1588 CRemoteContainer<CSearchFile, uint32, CEC_SearchFile_Tag>::HandlePacket(packet);
1593 CSearchFile::CSearchFile(CEC_SearchFile_Tag *tag)
1595 CECID(tag->ID()),
1596 m_parent(NULL),
1597 m_showChildren(false),
1598 m_sourceCount(0),
1599 m_completeSourceCount(0),
1600 m_kademlia(false),
1601 m_downloadStatus(NEW),
1602 m_clientID(0),
1603 m_clientPort(0),
1604 m_kadPublishInfo(0)
1606 SetFileName(CPath(tag->FileName()));
1607 m_abyFileHash = tag->FileHash();
1608 SetFileSize(tag->SizeFull());
1610 m_searchID = theApp->searchlist->m_curr_search;
1615 // dtor is virtual - must be implemented
1616 CSearchFile::~CSearchFile()
1621 CSearchFile *CSearchListRem::CreateItem(CEC_SearchFile_Tag *tag)
1623 CSearchFile *file = new CSearchFile(tag);
1624 ProcessItemUpdate(tag, file);
1626 theApp->amuledlg->m_searchwnd->AddResult(file);
1628 return file;
1632 void CSearchListRem::DeleteItem(CSearchFile *file)
1634 delete file;
1638 uint32 CSearchListRem::GetItemID(CSearchFile *file)
1640 return file->ECID();
1644 void CSearchListRem::ProcessItemUpdate(CEC_SearchFile_Tag *tag, CSearchFile *file)
1646 uint32 sourceCount = file->m_sourceCount;
1647 uint32 completeSourceCount = file->m_completeSourceCount;
1648 CSearchFile::DownloadStatus status = file->m_downloadStatus;
1649 tag->SourceCount(&file->m_sourceCount);
1650 tag->CompleteSourceCount(&file->m_completeSourceCount);
1651 tag->DownloadStatus((uint32 *) &file->m_downloadStatus);
1653 if (file->m_sourceCount != sourceCount
1654 || file->m_completeSourceCount != completeSourceCount
1655 || file->m_downloadStatus != status) {
1656 if (theApp->amuledlg && theApp->amuledlg->m_searchwnd) {
1657 theApp->amuledlg->m_searchwnd->UpdateResult(file);
1663 bool CSearchListRem::Phase1Done(const CECPacket *WXUNUSED(reply))
1665 CECPacket progress_req(EC_OP_SEARCH_PROGRESS);
1666 m_conn->SendRequest(this, &progress_req);
1668 return true;
1672 void CSearchListRem::RemoveResults(long nSearchID)
1674 ResultMap::iterator it = m_results.find(nSearchID);
1675 if (it != m_results.end()) {
1676 CSearchResultList& list = it->second;
1677 for (unsigned int i = 0; i < list.size(); ++i) {
1678 delete list[i];
1680 m_results.erase(it);
1685 const CSearchResultList& CSearchListRem::GetSearchResults(long nSearchID)
1687 ResultMap::const_iterator it = m_results.find(nSearchID);
1688 if (it != m_results.end()) {
1689 return it->second;
1692 // TODO: Should we assert in this case?
1693 static CSearchResultList list;
1694 return list;
1698 void CStatsUpdaterRem::HandlePacket(const CECPacket *packet)
1700 theStats::UpdateStats(packet);
1701 theApp->ShowUserCount(); // maybe there should be a check if a usercount changed ?
1705 bool CUpDownClient::IsBanned() const
1707 // FIXME: add code
1708 return false;
1713 // Those functions have different implementation in remote gui
1715 void CUpDownClient::Ban()
1717 // FIXME: add code
1718 wxFAIL;
1722 void CUpDownClient::UnBan()
1724 // FIXME: add code
1725 wxFAIL;
1729 void CUpDownClient::RequestSharedFileList()
1731 // FIXME: add code
1732 wxFAIL;
1736 void CKnownFile::SetFileComment(const wxString &)
1738 // FIXME: add code
1739 wxMessageBox(_("Comments and ratings are not supported on remote gui yet"), _("Information"), wxOK | wxICON_INFORMATION);
1743 void CKnownFile::SetFileRating(unsigned char)
1745 // FIXME: add code
1749 // I don't think it will be implemented - too match data transfer. But who knows ?
1750 wxString CUpDownClient::ShowDownloadingParts() const
1752 return wxEmptyString;
1756 bool CUpDownClient::SwapToAnotherFile(
1757 bool WXUNUSED(bIgnoreNoNeeded),
1758 bool WXUNUSED(ignoreSuspensions),
1759 bool WXUNUSED(bRemoveCompletely),
1760 CPartFile* WXUNUSED(toFile))
1762 // FIXME: add code
1763 wxFAIL;
1764 return false;
1769 // Those functions are virtual. So even they don't get called they must
1770 // be defined so linker will be happy
1772 CPacket* CKnownFile::CreateSrcInfoPacket(const CUpDownClient *, uint8 /*byRequestedVersion*/, uint16 /*nRequestedOptions*/)
1774 wxFAIL;
1775 return 0;
1779 bool CKnownFile::LoadFromFile(const class CFileDataIO*)
1781 wxFAIL;
1782 return false;
1786 void CKnownFile::UpdatePartsInfo()
1788 wxFAIL;
1792 CPacket* CPartFile::CreateSrcInfoPacket(CUpDownClient const *, uint8 /*byRequestedVersion*/, uint16 /*nRequestedOptions*/)
1794 wxFAIL;
1795 return 0;
1799 void CPartFile::UpdatePartsInfo()
1801 wxFAIL;
1805 void CPartFile::UpdateFileRatingCommentAvail()
1807 bool prevComment = m_hasComment;
1808 int prevRating = m_iUserRating;
1810 m_hasComment = false;
1811 m_iUserRating = 0;
1812 int ratingCount = 0;
1814 FileRatingList::iterator it = m_FileRatingList.begin();
1815 for (; it != m_FileRatingList.end(); ++it) {
1816 SFileRating& cur_rat = *it;
1818 if (!cur_rat.Comment.IsEmpty()) {
1819 m_hasComment = true;
1822 uint8 rating = cur_rat.Rating;
1823 if (rating) {
1824 wxASSERT(rating <= 5);
1826 ratingCount++;
1827 m_iUserRating += rating;
1831 if (ratingCount) {
1832 m_iUserRating /= ratingCount;
1833 wxASSERT(m_iUserRating > 0 && m_iUserRating <= 5);
1836 if ((prevComment != m_hasComment) || (prevRating != m_iUserRating)) {
1837 UpdateDisplayedInfo();
1841 bool CPartFile::SavePartFile(bool)
1843 wxFAIL;
1844 return false;
1847 CamuleRemoteGuiApp *theApp;
1850 // since gui is not linked with amule.cpp - define events here
1852 DEFINE_LOCAL_EVENT_TYPE(wxEVT_CORE_FINISHED_HTTP_DOWNLOAD)
1853 DEFINE_LOCAL_EVENT_TYPE(wxEVT_CORE_SOURCE_DNS_DONE)
1854 DEFINE_LOCAL_EVENT_TYPE(wxEVT_CORE_UDP_DNS_DONE)
1855 DEFINE_LOCAL_EVENT_TYPE(wxEVT_CORE_SERVER_DNS_DONE)
1856 // File_checked_for_headers