Upstream tarball 20080414
[amule.git] / src / DownloadListCtrl.cpp
blob95b57c0ded4788f486ae9b603447093f1c4c937c
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 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
26 #include <protocol/ed2k/ClientSoftware.h>
27 #include <common/MenuIDs.h>
29 #include <common/Format.h> // Needed for CFormat
30 #include "amule.h" // Needed for theApp
31 #include "amuleDlg.h" // Needed for CamuleDlg
32 #include "BarShader.h" // Needed for CBarShader
33 #include "ClientDetailDialog.h" // Needed for CClientDetailDialog
34 #include "ChatWnd.h" // Needed for CChatWnd
35 #include "CommentDialogLst.h" // Needed for CCommentDialogLst
36 #include "DownloadListCtrl.h" // Interface declarations
37 #include "DataToText.h" // Needed for PriorityToStr
38 #include "FileDetailDialog.h" // Needed for CFileDetailDialog
39 #include "GuiEvents.h" // Needed for CoreNotify_*
40 #ifdef ENABLE_IP2COUNTRY
41 #include "IP2Country.h" // Needed for IP2Country
42 #endif
43 #include "Logger.h"
44 #include "muuli_wdr.h" // Needed for ID_DLOADLIST
45 #include "PartFile.h" // Needed for CPartFile
46 #include "Preferences.h"
47 #include "SharedFileList.h" // Needed for CSharedFileList
48 #include "TerminationProcess.h" // Needed for CTerminationProcess
49 #include "updownclient.h" // Needed for CUpDownClient
52 class CPartFile;
55 struct CtrlItem_Struct
57 CtrlItem_Struct()
58 : dwUpdated(0),
59 status(NULL),
60 m_owner(NULL),
61 m_fileValue(NULL),
62 m_sourceValue(NULL),
63 m_type(FILE_TYPE)
64 { }
66 ~CtrlItem_Struct() {
67 delete status;
70 DownloadItemType GetType() const {
71 return m_type;
74 CPartFile* GetOwner() const {
75 return m_owner;
78 CPartFile* GetFile() const {
79 return m_fileValue;
82 CUpDownClient* GetSource() const {
83 return m_sourceValue;
86 void SetContents(CPartFile* file) {
87 m_owner = NULL;
88 m_fileValue = file;
89 m_sourceValue = NULL;
90 m_owner = NULL;
91 m_type = FILE_TYPE;
94 void SetContents(CPartFile* owner, CUpDownClient* source, DownloadItemType type) {
95 wxCHECK_RET(type != FILE_TYPE, wxT("Invalid type, not a source"));
97 m_owner = owner;
98 m_fileValue = NULL;
99 m_sourceValue = source;
100 m_type = type;
104 uint32 dwUpdated;
105 wxBitmap* status;
107 private:
108 CPartFile* m_owner;
109 CPartFile* m_fileValue;
110 CUpDownClient* m_sourceValue;
111 DownloadItemType m_type;
116 #define m_ImageList theApp->amuledlg->m_imagelist
119 BEGIN_EVENT_TABLE(CDownloadListCtrl, CMuleListCtrl)
120 EVT_LIST_ITEM_ACTIVATED(ID_DLOADLIST, CDownloadListCtrl::OnItemActivated)
121 EVT_LIST_ITEM_RIGHT_CLICK(ID_DLOADLIST, CDownloadListCtrl::OnMouseRightClick)
122 EVT_LIST_ITEM_MIDDLE_CLICK(ID_DLOADLIST, CDownloadListCtrl::OnMouseMiddleClick)
124 EVT_CHAR( CDownloadListCtrl::OnKeyPressed )
126 EVT_MENU( MP_CANCEL, CDownloadListCtrl::OnCancelFile )
128 EVT_MENU( MP_PAUSE, CDownloadListCtrl::OnSetStatus )
129 EVT_MENU( MP_STOP, CDownloadListCtrl::OnSetStatus )
130 EVT_MENU( MP_RESUME, CDownloadListCtrl::OnSetStatus )
132 EVT_MENU( MP_PRIOLOW, CDownloadListCtrl::OnSetPriority )
133 EVT_MENU( MP_PRIONORMAL, CDownloadListCtrl::OnSetPriority )
134 EVT_MENU( MP_PRIOHIGH, CDownloadListCtrl::OnSetPriority )
135 EVT_MENU( MP_PRIOAUTO, CDownloadListCtrl::OnSetPriority )
137 EVT_MENU( MP_SWAP_A4AF_TO_THIS, CDownloadListCtrl::OnSwapSources )
138 EVT_MENU( MP_SWAP_A4AF_TO_THIS_AUTO, CDownloadListCtrl::OnSwapSources )
139 EVT_MENU( MP_SWAP_A4AF_TO_ANY_OTHER, CDownloadListCtrl::OnSwapSources )
141 EVT_MENU_RANGE( MP_ASSIGNCAT, MP_ASSIGNCAT + 99, CDownloadListCtrl::OnSetCategory )
143 EVT_MENU( MP_CLEARCOMPLETED, CDownloadListCtrl::OnClearCompleted )
145 EVT_MENU( MP_GETMAGNETLINK, CDownloadListCtrl::OnGetLink )
146 EVT_MENU( MP_GETED2KLINK, CDownloadListCtrl::OnGetLink )
148 EVT_MENU( MP_METINFO, CDownloadListCtrl::OnViewFileInfo )
149 EVT_MENU( MP_VIEW, CDownloadListCtrl::OnPreviewFile )
150 EVT_MENU( MP_VIEWFILECOMMENTS, CDownloadListCtrl::OnViewFileComments )
152 EVT_MENU( MP_WS, CDownloadListCtrl::OnGetFeedback )
153 EVT_MENU( MP_RAZORSTATS, CDownloadListCtrl::OnGetRazorStats )
155 EVT_MENU( MP_CHANGE2FILE, CDownloadListCtrl::OnSwapSource )
156 EVT_MENU( MP_SHOWLIST, CDownloadListCtrl::OnViewFiles )
157 EVT_MENU( MP_ADDFRIEND, CDownloadListCtrl::OnAddFriend )
158 EVT_MENU( MP_SENDMESSAGE, CDownloadListCtrl::OnSendMessage )
159 EVT_MENU( MP_DETAIL, CDownloadListCtrl::OnViewClientInfo )
160 END_EVENT_TABLE()
164 //! This listtype is used when gathering the selected items.
165 typedef std::list<CtrlItem_Struct*> ItemList;
169 CDownloadListCtrl::CDownloadListCtrl(
170 wxWindow *parent, wxWindowID winid, const wxPoint& pos, const wxSize& size,
171 long style, const wxValidator& validator, const wxString& name )
173 CMuleListCtrl( parent, winid, pos, size, style | wxLC_OWNERDRAW, validator, name )
175 // Setting the sorter function.
176 SetSortFunc( SortProc );
178 // Set the table-name (for loading and saving preferences).
179 SetTableName( wxT("Download") );
181 m_menu = NULL;
183 wxColour colour = BLEND( SYSCOLOR( wxSYS_COLOUR_HIGHLIGHT ), 125 );
184 m_hilightBrush = new wxBrush( colour, wxSOLID );
186 colour = BLEND( SYSCOLOR( wxSYS_COLOUR_BTNSHADOW), 125 );
187 m_hilightUnfocusBrush = new wxBrush( colour, wxSOLID );
189 InsertColumn( 0, _("File Name"), wxLIST_FORMAT_LEFT, 260 );
190 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT, 60 );
191 InsertColumn( 2, _("Transferred"), wxLIST_FORMAT_LEFT, 65 );
192 InsertColumn( 3, _("Completed"), wxLIST_FORMAT_LEFT, 65 );
193 InsertColumn( 4, _("Speed"), wxLIST_FORMAT_LEFT, 65 );
194 InsertColumn( 5, _("Progress"), wxLIST_FORMAT_LEFT, 170 );
195 InsertColumn( 6, _("Sources"), wxLIST_FORMAT_LEFT, 50 );
196 InsertColumn( 7, _("Priority"), wxLIST_FORMAT_LEFT, 55 );
197 InsertColumn( 8, _("Status"), wxLIST_FORMAT_LEFT, 70 );
198 InsertColumn( 9, _("Time Remaining"), wxLIST_FORMAT_LEFT, 110 );
199 InsertColumn( 10, _("Last Seen Complete"), wxLIST_FORMAT_LEFT, 220 );
200 InsertColumn( 11, _("Last Reception"), wxLIST_FORMAT_LEFT, 220 );
202 m_category = 0;
203 m_completedFiles = 0;
204 m_filecount = 0;
205 LoadSettings();
209 CDownloadListCtrl::~CDownloadListCtrl()
211 while ( !m_ListItems.empty() ) {
212 delete m_ListItems.begin()->second;
213 m_ListItems.erase( m_ListItems.begin() );
215 delete m_hilightBrush;
216 delete m_hilightUnfocusBrush;
220 void CDownloadListCtrl::AddFile( CPartFile* file )
222 wxASSERT( file );
224 // Avoid duplicate entries of files
225 if ( m_ListItems.find( file ) == m_ListItems.end() ) {
226 CtrlItem_Struct* newitem = new CtrlItem_Struct;
227 newitem->SetContents(file);
229 m_ListItems.insert( ListItemsPair( file, newitem ) );
231 // Check if the new file is visible in the current category
232 if ( file->CheckShowItemInGivenCat( m_category ) ) {
233 ShowFile( file, true );
239 void CDownloadListCtrl::AddSource(CPartFile* owner, CUpDownClient* source, DownloadItemType type)
241 wxCHECK_RET(owner, wxT("NULL owner in CDownloadListCtrl::AddSource"));
242 wxCHECK_RET(source, wxT("NULL source in CDownloadListCtrl::AddSource"));
243 wxCHECK_RET(type != FILE_TYPE, wxT("Invalid type, not a source"));
245 // Update the other instances of this source
246 bool bFound = false;
247 ListIteratorPair rangeIt = m_ListItems.equal_range(source);
248 for ( ListItems::iterator it = rangeIt.first; it != rangeIt.second; ++it ) {
249 CtrlItem_Struct* cur_item = it->second;
251 // Check if this source has been already added to this file => to be sure
252 if ( cur_item->GetOwner() == owner ) {
253 // Update this instance with its new setting
254 cur_item->SetContents(owner, source, type);
255 cur_item->dwUpdated = 0;
256 bFound = true;
257 } else if ( type == AVAILABLE_SOURCE ) {
258 // The state 'Available' is exclusive
259 cur_item->SetContents(cur_item->GetOwner(), source, A4AF_SOURCE);
260 cur_item->dwUpdated = 0;
264 if ( bFound ) {
265 return;
268 if ( owner->ShowSources() ) {
269 CtrlItem_Struct* newitem = new CtrlItem_Struct;
270 newitem->SetContents(owner, source, type);
272 m_ListItems.insert( ListItemsPair(source, newitem) );
274 // Find the owner-object
275 ListItems::iterator it = m_ListItems.find( owner );
277 if ( it != m_ListItems.end() ) {
278 long item = FindItem( -1, reinterpret_cast<wxUIntPtr>(it->second) );
280 if ( item > -1 ) {
281 item = InsertItem( item + 1, wxEmptyString );
283 SetItemPtrData( item, reinterpret_cast<wxUIntPtr>(newitem) );
285 // background.. this should be in a function
286 wxListItem listitem;
287 listitem.m_itemId = item;
289 listitem.SetBackgroundColour( GetBackgroundColour() );
291 SetItem( listitem );
298 void CDownloadListCtrl::RemoveSource( const CUpDownClient* source, const CPartFile* owner )
300 wxASSERT( source );
302 // Retrieve all entries matching the source
303 ListIteratorPair rangeIt = m_ListItems.equal_range(source);
305 for ( ListItems::iterator it = rangeIt.first; it != rangeIt.second; ) {
306 ListItems::iterator tmp = it++;
308 CtrlItem_Struct* item = tmp->second;
309 if ( owner == NULL || owner == item->GetOwner() ) {
310 // Remove it from the m_ListItems
311 m_ListItems.erase( tmp );
313 long index = FindItem( -1, reinterpret_cast<wxUIntPtr>(item) );
315 if ( index > -1 ) {
316 DeleteItem( index );
319 delete item;
325 void CDownloadListCtrl::RemoveFile( CPartFile* file )
327 wxASSERT( file );
329 // Ensure that any assosiated sources and list-entries are removed
330 ShowFile( file, false );
332 // Find the assosiated list-item
333 ListItems::iterator it = m_ListItems.find( file );
335 if ( it != m_ListItems.end() ) {
336 delete it->second;
338 m_ListItems.erase( it );
343 void CDownloadListCtrl::UpdateItem(const void* toupdate)
345 // Retrieve all entries matching the source
346 ListIteratorPair rangeIt = m_ListItems.equal_range( toupdate );
348 // Visible lines, default to all because not all platforms
349 // support the GetVisibleLines function
350 long first = 0, last = GetItemCount();
352 #ifndef __WXMSW__
353 // Get visible lines if we need them
354 if ( rangeIt.first != rangeIt.second ) {
355 GetVisibleLines( &first, &last );
357 #endif
359 for ( ListItems::iterator it = rangeIt.first; it != rangeIt.second; ++it ) {
360 CtrlItem_Struct* item = it->second;
362 long index = FindItem( -1, reinterpret_cast<wxUIntPtr>(item) );
364 // Determine if the file should be shown in the current category
365 if ( item->GetType() == FILE_TYPE ) {
366 CPartFile* file = item->GetFile();
368 bool show = file->CheckShowItemInGivenCat( m_category );
370 if ( index > -1 ) {
371 if ( show ) {
372 item->dwUpdated = 0;
374 // Only update visible lines
375 if ( index >= first && index <= last) {
376 RefreshItem( index );
378 } else {
379 // Item should no longer be shown in
380 // the current category
381 ShowFile( file, false );
383 } else if ( show ) {
384 // Item has been hidden but new status means
385 // that it should it should be shown in the
386 // current category
387 ShowFile( file, true );
390 if (file->GetStatus() == PS_COMPLETE) {
391 m_completedFiles = true;
393 CastByID(ID_BTNCLRCOMPL, GetParent(), wxButton)->Enable(true);
395 } else {
396 item->dwUpdated = 0;
398 // Only update visible lines
399 if ( index >= first && index <= last) {
400 RefreshItem( index );
407 void CDownloadListCtrl::ShowFile( CPartFile* file, bool show )
409 wxASSERT( file );
411 ListItems::iterator it = m_ListItems.find( file );
413 if ( it != m_ListItems.end() ) {
414 CtrlItem_Struct* item = it->second;
416 if ( show ) {
417 // Check if the file is already being displayed
418 long index = FindItem( -1, reinterpret_cast<wxUIntPtr>(item) );
419 if ( index == -1 ) {
420 long newitem = InsertItem( GetItemCount(), wxEmptyString );
422 SetItemPtrData( newitem, reinterpret_cast<wxUIntPtr>(item) );
424 wxListItem myitem;
425 myitem.m_itemId = newitem;
426 myitem.SetBackgroundColour( GetBackgroundColour() );
428 SetItem(myitem);
430 RefreshItem( newitem );
432 ShowFilesCount( 1 );
434 } else {
435 // Ensure sources are hidden
436 ShowSources( file, false );
438 // Try to find the file and remove it
439 long index = FindItem( -1, reinterpret_cast<wxUIntPtr>(item) );
440 if ( index > -1 ) {
441 DeleteItem( index );
442 ShowFilesCount( -1 );
449 void CDownloadListCtrl::ShowSources( CPartFile* file, bool show )
451 // Check if the current state is the same as the new state
452 if ( file->ShowSources() == show ) {
453 return;
456 Freeze();
458 file->SetShowSources( show );
460 if ( show ) {
461 const CPartFile::SourceSet& normSources = file->GetSourceList();
462 const CPartFile::SourceSet& a4afSources = file->GetA4AFList();
464 // Adding normal sources
465 CPartFile::SourceSet::const_iterator it;
466 for ( it = normSources.begin(); it != normSources.end(); ++it ) {
467 switch ((*it)->GetDownloadState()) {
468 case DS_DOWNLOADING:
469 case DS_ONQUEUE:
470 AddSource( file, *it, AVAILABLE_SOURCE );
471 default:
472 // Any other state
473 AddSource( file, *it, UNAVAILABLE_SOURCE );
478 // Adding A4AF sources
479 for ( it = a4afSources.begin(); it != a4afSources.end(); ++it ) {
480 AddSource( file, *it, A4AF_SOURCE );
482 } else {
483 for ( int i = GetItemCount() - 1; i >= 0; --i ) {
484 CtrlItem_Struct* item = (CtrlItem_Struct*)GetItemData(i);
486 if ( item->GetType() != FILE_TYPE && item->GetOwner() == file ) {
487 // Remove from the grand list, this call doesn't remove the source
488 // from the listctrl, because ShowSources is now false. This also
489 // deletes the item.
490 RemoveSource(item->GetSource(), file);
495 Thaw();
499 void CDownloadListCtrl::ChangeCategory( int newCategory )
501 Freeze();
503 // remove all displayed files with a different cat and show the correct ones
504 for (ListItems::const_iterator it = m_ListItems.begin(); it != m_ListItems.end(); it++) {
505 const CtrlItem_Struct *cur_item = it->second;
507 if ( cur_item->GetType() == FILE_TYPE ) {
508 CPartFile* file = cur_item->GetFile();
510 bool curVisibility = file->CheckShowItemInGivenCat( m_category );
511 bool newVisibility = file->CheckShowItemInGivenCat( newCategory );
513 // Check if the visibility of the file has changed. However, if the
514 // current category is the default (0) category, then we can't use
515 // curVisiblity to see if the visibility has changed but instead
516 // have to let ShowFile() check if the file is or isn't on the list.
517 if ( curVisibility != newVisibility || !newCategory ) {
518 ShowFile( file, newVisibility );
523 Thaw();
525 m_category = newCategory;
529 uint8 CDownloadListCtrl::GetCategory() const
531 return m_category;
538 const int itFILES = 1;
539 const int itSOURCES = 2;
542 * Helper-function: This function is used to gather selected items.
544 * @param list A pointer to the list to gather items from.
545 * @param types The desired types OR'd together.
546 * @return A list containing the selected items of the choosen types.
548 ItemList GetSelectedItems( CDownloadListCtrl* list, int types )
550 ItemList results;
552 long index = list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
554 while ( index > -1 ) {
555 CtrlItem_Struct* item = (CtrlItem_Struct*)list->GetItemData( index );
557 bool add = false;
558 add |= ( item->GetType() == FILE_TYPE ) && ( types & itFILES );
559 add |= ( item->GetType() != FILE_TYPE ) && ( types & itSOURCES );
561 if ( add ) {
562 results.push_back( item );
565 index = list->GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
568 return results;
572 void CDownloadListCtrl::OnCancelFile(wxCommandEvent& WXUNUSED(event))
574 ItemList files = ::GetSelectedItems(this, itFILES);
575 if (files.size()) {
576 wxString question;
577 if (files.size() == 1) {
578 question = _("Are you sure that you wish to delete the selected file?");
579 } else {
580 question = _("Are you sure that you wish to delete the selected files?");
582 if (wxMessageBox( question, _("Cancel"), wxICON_QUESTION | wxYES_NO, this) == wxYES) {
583 for (ItemList::iterator it = files.begin(); it != files.end(); ++it) {
584 CPartFile* file = (*it)->GetFile();
585 if (file) {
586 switch (file->GetStatus()) {
587 case PS_WAITINGFORHASH:
588 case PS_HASHING:
589 case PS_COMPLETING:
590 case PS_COMPLETE:
591 break;
592 default:
593 CoreNotify_PartFile_Delete(file);
602 void CDownloadListCtrl::OnSetPriority( wxCommandEvent& event )
604 int priority = 0;
605 switch ( event.GetId() ) {
606 case MP_PRIOLOW: priority = PR_LOW; break;
607 case MP_PRIONORMAL: priority = PR_NORMAL; break;
608 case MP_PRIOHIGH: priority = PR_HIGH; break;
609 case MP_PRIOAUTO: priority = PR_AUTO; break;
610 default:
611 wxASSERT( false );
614 ItemList files = ::GetSelectedItems( this, itFILES );
616 for ( ItemList::iterator it = files.begin(); it != files.end(); ++it ) {
617 CPartFile* file = (*it)->GetFile();
619 if ( priority == PR_AUTO ) {
620 CoreNotify_PartFile_PrioAuto( file, true );
621 } else {
622 CoreNotify_PartFile_PrioAuto( file, false );
624 CoreNotify_PartFile_PrioSet( file, priority, true );
630 void CDownloadListCtrl::OnSwapSources( wxCommandEvent& event )
632 ItemList files = ::GetSelectedItems( this, itFILES );
634 for ( ItemList::iterator it = files.begin(); it != files.end(); ++it ) {
635 CPartFile* file = (*it)->GetFile();
637 switch ( event.GetId() ) {
638 case MP_SWAP_A4AF_TO_THIS:
639 CoreNotify_PartFile_Swap_A4AF( file );
640 break;
642 case MP_SWAP_A4AF_TO_THIS_AUTO:
643 CoreNotify_PartFile_Swap_A4AF_Auto( file );
644 break;
646 case MP_SWAP_A4AF_TO_ANY_OTHER:
647 CoreNotify_PartFile_Swap_A4AF_Others( file );
648 break;
654 void CDownloadListCtrl::OnSetCategory( wxCommandEvent& event )
656 ItemList files = ::GetSelectedItems( this, itFILES );
658 for ( ItemList::iterator it = files.begin(); it != files.end(); ++it ) {
659 CPartFile* file = (*it)->GetFile();
661 CoreNotify_PartFile_SetCat( file, event.GetId() - MP_ASSIGNCAT );
664 ChangeCategory( m_category );
668 void CDownloadListCtrl::OnSetStatus( wxCommandEvent& event )
670 ItemList files = ::GetSelectedItems( this, itFILES );
672 for ( ItemList::iterator it = files.begin(); it != files.end(); ++it ) {
673 CPartFile* file = (*it)->GetFile();
675 switch ( event.GetId() ) {
676 case MP_PAUSE:
677 CoreNotify_PartFile_Pause( file );
678 break;
680 case MP_RESUME:
681 CoreNotify_PartFile_Resume( file );
682 break;
684 case MP_STOP:
685 ShowSources(file, false);
686 CoreNotify_PartFile_Stop( file );
687 break;
693 void CDownloadListCtrl::OnClearCompleted( wxCommandEvent& WXUNUSED(event) )
695 ClearCompleted();
699 void CDownloadListCtrl::OnGetLink(wxCommandEvent& event)
701 ItemList files = ::GetSelectedItems( this, itFILES );
703 wxString URIs;
705 for ( ItemList::iterator it = files.begin(); it != files.end(); ++it ) {
706 CPartFile* file = (*it)->GetFile();
708 if ( event.GetId() == MP_GETED2KLINK ) {
709 URIs += theApp->CreateED2kLink( file ) + wxT("\n");
710 } else {
711 URIs += theApp->CreateMagnetLink( file ) + wxT("\n");
715 if ( !URIs.IsEmpty() ) {
716 theApp->CopyTextToClipboard( URIs.BeforeLast(wxT('\n')) );
721 void CDownloadListCtrl::OnGetFeedback(wxCommandEvent& WXUNUSED(event))
723 wxString feed;
724 ItemList files = ::GetSelectedItems(this, itFILES);
725 for (ItemList::iterator it = files.begin(); it != files.end(); ++it) {
726 CPartFile* file = (*it)->GetFile();
727 feed += file->GetFeedback();
729 if (!feed.IsEmpty()) {
730 theApp->CopyTextToClipboard(feed);
735 void CDownloadListCtrl::OnGetRazorStats( wxCommandEvent& WXUNUSED(event) )
737 ItemList files = ::GetSelectedItems( this, itFILES );
739 if ( files.size() == 1 ) {
740 CPartFile* file = files.front()->GetFile();
742 theApp->amuledlg->LaunchUrl(
743 wxT("http://stats.razorback2.com/ed2khistory?ed2k=") +
744 file->GetFileHash().Encode());
749 void CDownloadListCtrl::OnViewFileInfo( wxCommandEvent& WXUNUSED(event) )
751 ItemList files = ::GetSelectedItems( this, itFILES );
753 if ( files.size() == 1 ) {
754 CPartFile* file = files.front()->GetFile();
756 CFileDetailDialog dialog( this, file );
757 dialog.ShowModal();
762 void CDownloadListCtrl::OnViewFileComments( wxCommandEvent& WXUNUSED(event) )
764 ItemList files = ::GetSelectedItems( this, itFILES );
766 if ( files.size() == 1 ) {
767 CPartFile* file = files.front()->GetFile();
769 CCommentDialogLst dialog( this, file );
770 dialog.ShowModal();
775 void CDownloadListCtrl::OnPreviewFile( wxCommandEvent& WXUNUSED(event) )
777 ItemList files = ::GetSelectedItems( this, itFILES );
779 if ( files.size() == 1 ) {
780 PreviewFile(files.front()->GetFile());
784 void CDownloadListCtrl::OnSwapSource( wxCommandEvent& WXUNUSED(event) )
786 ItemList sources = ::GetSelectedItems( this, itSOURCES );
788 for ( ItemList::iterator it = sources.begin(); it != sources.end(); ++it ) {
789 CPartFile* file = (*it)->GetOwner();
790 CUpDownClient* source = (*it)->GetSource();
792 source->SwapToAnotherFile( true, false, false, file );
797 void CDownloadListCtrl::OnViewFiles( wxCommandEvent& WXUNUSED(event) )
799 ItemList sources = ::GetSelectedItems( this, itSOURCES );
801 if ( sources.size() == 1 ) {
802 CUpDownClient* source = sources.front()->GetSource();
804 source->RequestSharedFileList();
809 void CDownloadListCtrl::OnAddFriend( wxCommandEvent& WXUNUSED(event) )
811 ItemList sources = ::GetSelectedItems( this, itSOURCES );
813 for ( ItemList::iterator it = sources.begin(); it != sources.end(); ++it ) {
814 CUpDownClient* client = (*it)->GetSource();
815 if (client->IsFriend()) {
816 theApp->amuledlg->m_chatwnd->RemoveFriend(client->GetUserHash(), client->GetIP(), client->GetUserPort());
817 } else {
818 theApp->amuledlg->m_chatwnd->AddFriend( client );
824 void CDownloadListCtrl::OnSendMessage( wxCommandEvent& WXUNUSED(event) )
826 ItemList sources = ::GetSelectedItems( this, itSOURCES );
828 if ( sources.size() == 1 ) {
829 CUpDownClient* source = (sources.front())->GetSource();
831 // These values are cached, since calling wxGetTextFromUser will
832 // start an event-loop, in which the client may be deleted.
833 wxString userName = source->GetUserName();
834 uint64 userID = GUI_ID(source->GetIP(), source->GetUserPort());
836 wxString message = ::wxGetTextFromUser(_("Send message to user"),
837 _("Message to send:"));
838 if ( !message.IsEmpty() ) {
839 theApp->amuledlg->m_chatwnd->SendMessage(message, userName, userID);
845 void CDownloadListCtrl::OnViewClientInfo( wxCommandEvent& WXUNUSED(event) )
847 ItemList sources = ::GetSelectedItems( this, itSOURCES );
849 if ( sources.size() == 1 ) {
850 CUpDownClient* source = (sources.front())->GetSource();
852 CClientDetailDialog dialog( this, source );
853 dialog.ShowModal();
858 void CDownloadListCtrl::OnItemActivated( wxListEvent& evt )
860 CtrlItem_Struct* content = (CtrlItem_Struct*)GetItemData( evt.GetIndex() );
862 if ( content->GetType() == FILE_TYPE ) {
863 CPartFile* file = content->GetFile();
865 if ((!file->IsPartFile() || file->GetStatus() == PS_COMPLETE) && file->PreviewAvailable()) {
866 PreviewFile( file );
867 } else {
868 ShowSources( file, !file->ShowSources() );
875 void CDownloadListCtrl::OnMouseRightClick(wxListEvent& evt)
877 long index = CheckSelection(evt);
878 if (index < 0) {
879 return;
882 delete m_menu;
883 m_menu = NULL;
885 CtrlItem_Struct* item = (CtrlItem_Struct*)GetItemData( index );
886 if (item->GetType() == FILE_TYPE) {
887 m_menu = new wxMenu( _("Downloads") );
889 wxMenu* priomenu = new wxMenu();
890 priomenu->AppendCheckItem(MP_PRIOLOW, _("Low"));
891 priomenu->AppendCheckItem(MP_PRIONORMAL, _("Normal"));
892 priomenu->AppendCheckItem(MP_PRIOHIGH, _("High"));
893 priomenu->AppendCheckItem(MP_PRIOAUTO, _("Auto"));
895 m_menu->Append(MP_MENU_PRIO, _("Priority"), priomenu);
896 m_menu->Append(MP_CANCEL, _("Cancel"));
897 m_menu->Append(MP_STOP, _("&Stop"));
898 m_menu->Append(MP_PAUSE, _("&Pause"));
899 m_menu->Append(MP_RESUME, _("&Resume"));
900 m_menu->Append(MP_CLEARCOMPLETED, _("C&lear completed"));
901 //-----------------------------------------------------
902 m_menu->AppendSeparator();
903 //-----------------------------------------------------
904 wxMenu* extendedmenu = new wxMenu();
905 extendedmenu->Append(MP_SWAP_A4AF_TO_THIS,
906 _("Swap every A4AF to this file now"));
907 extendedmenu->AppendCheckItem(MP_SWAP_A4AF_TO_THIS_AUTO,
908 _("Swap every A4AF to this file (Auto)"));
909 //-----------------------------------------------------
910 extendedmenu->AppendSeparator();
911 //-----------------------------------------------------
912 extendedmenu->Append(MP_SWAP_A4AF_TO_ANY_OTHER,
913 _("Swap every A4AF to any other file now"));
914 //-----------------------------------------------------
915 m_menu->Append(MP_MENU_EXTD,
916 _("Extended Options"), extendedmenu);
917 //-----------------------------------------------------
918 m_menu->AppendSeparator();
919 //-----------------------------------------------------
920 /* Commented out till RB2 is back
921 m_menu->Append( MP_RAZORSTATS,
922 _("Get Razorback 2's stats for this file"));
923 //-----------------------------------------------------
924 m_menu->AppendSeparator();
925 //-----------------------------------------------------
927 m_menu->Append(MP_VIEW, _("Preview"));
928 m_menu->Append(MP_METINFO, _("Show file &details"));
929 m_menu->Append(MP_VIEWFILECOMMENTS,
930 _("Show all comments"));
931 //-----------------------------------------------------
932 m_menu->AppendSeparator();
933 //-----------------------------------------------------
934 m_menu->Append(MP_GETMAGNETLINK,
935 _("Copy magnet URI to clipboard"));
936 m_menu->Append(MP_GETED2KLINK,
937 _("Copy ED2k &link to clipboard"));
938 m_menu->Append(MP_WS,
939 _("Copy feedback to clipboard"));
940 //-----------------------------------------------------
941 m_menu->AppendSeparator();
942 //-----------------------------------------------------
943 // Add dinamic entries
944 wxMenu *cats = new wxMenu(_("Category"));
945 if (theApp->glob_prefs->GetCatCount() > 1) {
946 for (uint32 i = 0; i < theApp->glob_prefs->GetCatCount(); i++) {
947 if ( i == 0 ) {
948 cats->Append( MP_ASSIGNCAT, _("unassign") );
949 } else {
950 cats->Append( MP_ASSIGNCAT + i,
951 theApp->glob_prefs->GetCategory(i)->title );
955 m_menu->Append(MP_MENU_CATS, _("Assign to category"), cats);
956 m_menu->Enable(MP_MENU_CATS, (theApp->glob_prefs->GetCatCount() > 1) );
958 CPartFile* file = item->GetFile();
959 // then set state
960 const bool canStop =
961 (file->GetStatus() != PS_ERROR) &&
962 (file->GetStatus() != PS_COMPLETE) &&
963 (file->IsStopped() != true);
964 const bool canPause =
965 (file->GetStatus() != PS_PAUSED) && canStop;
966 const bool fileResumable =
967 (file->GetStatus() == PS_PAUSED) ||
968 (file->GetStatus() == PS_ERROR) ||
969 (file->GetStatus() == PS_INSUFFICIENT);
971 wxMenu* menu = m_menu;
972 menu->Enable( MP_CANCEL, ( file->GetStatus() != PS_COMPLETE ) );
973 menu->Enable( MP_PAUSE, canPause );
974 menu->Enable( MP_STOP, canStop );
975 menu->Enable( MP_RESUME, fileResumable );
976 menu->Enable( MP_CLEARCOMPLETED, m_completedFiles );
978 wxString view;
979 if (file->IsPartFile() && (file->GetStatus() != PS_COMPLETE)) {
980 view << CFormat(wxT("%s [%s]")) % _("Preview")
981 % file->GetPartMetFileName().RemoveExt();
982 } else if ( file->GetStatus() == PS_COMPLETE ) {
983 view << _("&Open the file");
985 menu->SetLabel(MP_VIEW, view);
986 menu->Enable(MP_VIEW, file->PreviewAvailable() );
988 menu->Check( MP_SWAP_A4AF_TO_THIS_AUTO, file->IsA4AFAuto() );
990 int priority = file->IsAutoDownPriority() ?
991 PR_AUTO : file->GetDownPriority();
993 priomenu->Check( MP_PRIOHIGH, priority == PR_HIGH );
994 priomenu->Check( MP_PRIONORMAL, priority == PR_NORMAL );
995 priomenu->Check( MP_PRIOLOW, priority == PR_LOW );
996 priomenu->Check( MP_PRIOAUTO, priority == PR_AUTO );
998 menu->Enable( MP_MENU_EXTD, canPause );
1000 PopupMenu(m_menu, evt.GetPoint());
1002 } else {
1003 CUpDownClient* client = item->GetSource();
1005 m_menu = new wxMenu(wxT("Clients"));
1006 m_menu->Append(MP_DETAIL, _("Show &Details"));
1007 m_menu->Append(MP_ADDFRIEND, client->IsFriend() ? _("Remove from friends") : _("Add to Friends"));
1008 m_menu->Append(MP_SHOWLIST, _("View Files"));
1009 m_menu->Append(MP_SENDMESSAGE, _("Send message"));
1010 m_menu->Append(MP_CHANGE2FILE, _("Swap to this file"));
1012 // Only enable the Swap option for A4AF sources
1013 m_menu->Enable(MP_CHANGE2FILE, (item->GetType() == A4AF_SOURCE));
1014 // We need a valid IP if we are to message the client
1015 m_menu->Enable(MP_SENDMESSAGE, (client->GetIP() != 0));
1017 m_menu->Enable(MP_SHOWLIST, !client->HasDisabledSharedFiles());
1019 PopupMenu(m_menu, evt.GetPoint());
1023 delete m_menu;
1024 m_menu = NULL;
1029 void CDownloadListCtrl::OnMouseMiddleClick(wxListEvent& evt)
1031 // Check if clicked item is selected. If not, unselect all and select it.
1032 long index = CheckSelection(evt);
1033 if ( index < 0 ) {
1034 return;
1037 CtrlItem_Struct* item = (CtrlItem_Struct*)GetItemData( index );
1039 if ( item->GetType() == FILE_TYPE ) {
1040 CFileDetailDialog(this, item->GetFile()).ShowModal();
1041 } else {
1042 CClientDetailDialog(this, item->GetSource()).ShowModal();
1047 void CDownloadListCtrl::OnKeyPressed( wxKeyEvent& event )
1049 // Check if delete was pressed
1050 switch (event.GetKeyCode()) {
1051 case WXK_NUMPAD_DELETE:
1052 case WXK_DELETE: {
1053 wxCommandEvent evt;
1054 OnCancelFile( evt );
1055 break;
1057 case WXK_F2: {
1058 ItemList files = ::GetSelectedItems( this, itFILES );
1059 if (files.size() == 1) {
1060 CPartFile* file = files.front()->GetFile();
1062 // Currently renaming of completed files causes problem with kad
1063 if (file->IsPartFile()) {
1064 wxString strNewName = ::wxGetTextFromUser(
1065 _("Enter new name for this file:"),
1066 _("File rename"), file->GetFileName().GetPrintable());
1068 CPath newName = CPath(strNewName);
1069 if (newName.IsOk() && (newName != file->GetFileName())) {
1070 theApp->sharedfiles->RenameFile(file, newName);
1074 break;
1076 default:
1077 event.Skip();
1082 void CDownloadListCtrl::OnDrawItem(
1083 int item, wxDC* dc, const wxRect& rect, const wxRect& rectHL, bool highlighted)
1085 // Don't do any drawing if there's nobody to see it.
1086 if ( !theApp->amuledlg->IsDialogVisible( CamuleDlg::DT_TRANSFER_WND ) ) {
1087 return;
1090 CtrlItem_Struct* content = (CtrlItem_Struct *)GetItemData(item);
1092 // Define text-color and background
1093 if ((content->GetType() == FILE_TYPE) && (highlighted)) {
1094 if (GetFocus()) {
1095 dc->SetBackground(*m_hilightBrush);
1096 dc->SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
1097 } else {
1098 dc->SetBackground(*m_hilightUnfocusBrush);
1099 dc->SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
1101 } else {
1102 dc->SetBackground(*(wxTheBrushList->FindOrCreateBrush(
1103 wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX), wxSOLID)));
1104 dc->SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
1108 // Define the border of the drawn area
1109 if ( highlighted ) {
1110 wxColour old;
1111 if ( ( content->GetType() == FILE_TYPE ) && !GetFocus() ) {
1112 old = m_hilightUnfocusBrush->GetColour();
1113 } else {
1114 old = m_hilightBrush->GetColour();
1117 wxColor newcol(
1118 ((int)old.Red() * 65) / 100,
1119 ((int)old.Green() * 65) / 100,
1120 ((int)old.Blue() * 65) / 100);
1122 dc->SetPen( wxPen(newcol, 1, wxSOLID) );
1123 } else {
1124 dc->SetPen(*wxTRANSPARENT_PEN);
1128 dc->SetBrush( dc->GetBackground() );
1129 dc->DrawRectangle( rectHL.x, rectHL.y, rectHL.width, rectHL.height );
1131 dc->SetPen(*wxTRANSPARENT_PEN);
1133 if ( content->GetType() == FILE_TYPE && ( !highlighted || !GetFocus() ) ) {
1134 // If we have category, override textforeground with what category tells us.
1135 CPartFile *file = content->GetFile();
1136 if ( file->GetCategory() ) {
1137 dc->SetTextForeground(
1138 WxColourFromCr(theApp->glob_prefs->GetCatColor(file->GetCategory())) );
1142 // Various constant values we use
1143 const int iTextOffset = ( rect.GetHeight() - dc->GetCharHeight() ) / 2;
1144 const int iOffset = 4;
1146 // The starting end ending position of the tree
1147 bool tree_show = false;
1148 int tree_start = 0;
1149 int tree_end = 0;
1151 wxRect cur_rec( iOffset, rect.y, 0, rect.height );
1152 for (int i = 0; i < GetColumnCount(); i++) {
1153 wxListItem listitem;
1154 GetColumn(i, listitem);
1156 if (listitem.GetWidth() > 2*iOffset) {
1157 cur_rec.width = listitem.GetWidth() - 2*iOffset;
1159 // Make a copy of the current rectangle so we can apply specific tweaks
1160 wxRect target_rec = cur_rec;
1161 if ( i == 5 ) {
1162 tree_show = ( listitem.GetWidth() > 0 );
1164 tree_start = cur_rec.x - iOffset;
1165 tree_end = cur_rec.x + iOffset;
1167 // Double the offset to make room for the cirle-marker
1168 target_rec.x += iOffset;
1169 target_rec.width -= iOffset;
1170 } else {
1171 // will ensure that text is about in the middle ;)
1172 target_rec.y += iTextOffset;
1175 // Draw the item
1176 if ( content->GetType() == FILE_TYPE ) {
1177 DrawFileItem(dc, i, target_rec, content);
1178 } else {
1179 DrawSourceItem(dc, i, target_rec, content);
1183 // Increment to the next column
1184 cur_rec.x += listitem.GetWidth();
1187 // Draw tree last so it draws over selected and focus (looks better)
1188 if ( tree_show ) {
1189 // Gather some information
1190 const bool notLast = item + 1 != GetItemCount();
1191 const bool notFirst = item != 0;
1192 const bool hasNext = notLast &&
1193 ((CtrlItem_Struct*)GetItemData(item + 1))->GetType() != FILE_TYPE;
1194 const bool isOpenRoot = content->GetType() == FILE_TYPE &&
1195 (content->GetFile())->ShowSources() &&
1196 ((content->GetFile())->GetStatus() != PS_COMPLETE);
1197 const bool isChild = content->GetType() != FILE_TYPE;
1199 // Might as well calculate these now
1200 const int treeCenter = tree_start + 3;
1201 const int middle = cur_rec.y + ( cur_rec.height + 1 ) / 2;
1203 // Set up a new pen for drawing the tree
1204 dc->SetPen( *(wxThePenList->FindOrCreatePen(dc->GetTextForeground(), 1, wxSOLID)) );
1206 if (isChild) {
1207 // Draw the line to the status bar
1208 dc->DrawLine(tree_end, middle, tree_start + 3, middle);
1210 // Draw the line to the child node
1211 if (hasNext) {
1212 dc->DrawLine(treeCenter, middle, treeCenter, cur_rec.y + cur_rec.height + 1);
1215 // Draw the line back up to parent node
1216 if (notFirst) {
1217 dc->DrawLine(treeCenter, middle, treeCenter, cur_rec.y - 1);
1219 } else if ( isOpenRoot ) {
1220 // Draw empty circle
1221 dc->SetBrush(*wxTRANSPARENT_BRUSH);
1223 dc->DrawCircle( treeCenter, middle, 3 );
1225 // Draw the line to the child node if there are any children
1226 if (hasNext) {
1227 dc->DrawLine(treeCenter, middle + 3, treeCenter, cur_rec.y + cur_rec.height + 1);
1235 void CDownloadListCtrl::DrawFileItem( wxDC* dc, int nColumn, const wxRect& rect, CtrlItem_Struct* item ) const
1237 wxDCClipper clipper( *dc, rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight() );
1239 const CPartFile* file = item->GetFile();
1241 // Used to contain the contenst of cells that dont need any fancy drawing, just text.
1242 wxString text;
1244 switch (nColumn) {
1245 // Filename
1246 case 0: {
1247 // show no. of partfile in filename column
1248 wxString filename;
1249 if (thePrefs::ShowPartFileNumber()) {
1250 if (file->IsPartFile() && !(file->GetStatus() == PS_COMPLETE)) {
1251 filename = CFormat(wxT("[%s] ")) % file->GetPartMetFileName().RemoveAllExt();
1254 filename += file->GetFileName().GetPrintable();
1256 if (file->HasRating() || file->HasComment()) {
1257 int image = Client_CommentOnly_Smiley;
1258 if (file->HasRating()) {
1259 image = Client_InvalidRating_Smiley + file->UserRating() - 1;
1262 wxASSERT(image >= Client_InvalidRating_Smiley);
1263 wxASSERT(image <= Client_CommentOnly_Smiley);
1265 int imgWidth = 16;
1267 // it's already centered by OnDrawItem() ...
1268 m_ImageList.Draw(image, *dc, rect.GetX(), rect.GetY() - 1,
1269 wxIMAGELIST_DRAW_TRANSPARENT);
1270 dc->DrawText(filename, rect.GetX() + imgWidth + 4, rect.GetY());
1271 } else {
1272 dc->DrawText(filename, rect.GetX(), rect.GetY());
1275 break;
1277 // Filesize
1278 case 1:
1279 text = CastItoXBytes( file->GetFileSize() );
1280 break;
1282 // Transferred
1283 case 2:
1284 text = CastItoXBytes( file->GetTransferred() );
1285 break;
1287 // Completed
1288 case 3:
1289 text = CastItoXBytes( file->GetCompletedSize() );
1290 break;
1292 // Speed
1293 case 4: // speed
1294 if ( file->GetTransferingSrcCount() ) {
1295 text = wxString::Format( wxT("%.1f "), file->GetKBpsDown() ) +
1296 _("kB/s");
1298 break;
1300 case 5: // progress
1302 if (thePrefs::ShowProgBar())
1304 int iWidth = rect.GetWidth() - 2;
1305 int iHeight = rect.GetHeight() - 2;
1307 // DO NOT DRAW IT ALL THE TIME
1308 uint32 dwTicks = GetTickCount();
1310 wxMemoryDC cdcStatus;
1312 if ( item->dwUpdated < dwTicks || !item->status || iWidth != item->status->GetWidth() ) {
1313 if ( item->status == NULL) {
1314 item->status = new wxBitmap(iWidth, iHeight);
1315 } else if ( item->status->GetWidth() != iWidth ) {
1316 // Only recreate if the size has changed
1317 item->status->Create(iWidth, iHeight);
1320 cdcStatus.SelectObject( *item->status );
1322 if ( thePrefs::UseFlatBar() ) {
1323 DrawFileStatusBar( file, &cdcStatus,
1324 wxRect(0, 0, iWidth, iHeight), true);
1325 } else {
1326 DrawFileStatusBar( file, &cdcStatus,
1327 wxRect(1, 1, iWidth - 2, iHeight - 2), false);
1329 // Draw black border
1330 cdcStatus.SetPen( *wxBLACK_PEN );
1331 cdcStatus.SetBrush( *wxTRANSPARENT_BRUSH );
1332 cdcStatus.DrawRectangle( 0, 0, iWidth, iHeight );
1335 item->dwUpdated = dwTicks + 5000; // Plus five seconds
1336 } else {
1337 cdcStatus.SelectObject( *item->status );
1340 dc->Blit( rect.GetX(), rect.GetY() + 1, iWidth, iHeight, &cdcStatus, 0, 0);
1343 if (thePrefs::ShowPercent()) {
1344 // Percentage of completing
1345 // We strip anything below the first decimal point,
1346 // to avoid Format doing roundings
1347 float percent = floor( file->GetPercentCompleted() * 10.0f ) / 10.0f;
1349 wxString buffer = wxString::Format( wxT("%.1f%%"), percent );
1350 int middlex = (2*rect.GetX() + rect.GetWidth()) >> 1;
1351 int middley = (2*rect.GetY() + rect.GetHeight()) >> 1;
1353 wxCoord textwidth, textheight;
1355 dc->GetTextExtent(buffer, &textwidth, &textheight);
1356 wxColour AktColor = dc->GetTextForeground();
1357 if (thePrefs::ShowProgBar()) {
1358 dc->SetTextForeground(*wxWHITE);
1359 } else {
1360 dc->SetTextForeground(*wxBLACK);
1362 dc->DrawText(buffer, middlex - (textwidth >> 1), middley - (textheight >> 1));
1363 dc->SetTextForeground(AktColor);
1366 break;
1368 // Sources
1369 case 6: {
1370 uint16 sc = file->GetSourceCount();
1371 uint16 ncsc = file->GetNotCurrentSourcesCount();
1372 if ( ncsc ) {
1373 text = wxString::Format( wxT("%i/%i" ), sc - ncsc, sc );
1374 } else {
1375 text = wxString::Format( wxT("%i"), sc );
1378 if ( file->GetSrcA4AFCount() ) {
1379 text += wxString::Format( wxT("+%i"), file->GetSrcA4AFCount() );
1382 if ( file->GetTransferingSrcCount() ) {
1383 text += wxString::Format( wxT(" (%i)"), file->GetTransferingSrcCount() );
1386 break;
1389 // Priority
1390 case 7:
1391 text = PriorityToStr( file->GetDownPriority(), file->IsAutoDownPriority() );
1392 break;
1394 // File-status
1395 case 8:
1396 text = file->getPartfileStatus();
1397 break;
1399 // Remaining
1400 case 9: {
1401 if ((file->GetStatus() != PS_COMPLETING) && file->IsPartFile()) {
1402 uint64 remainSize = file->GetFileSize() - file->GetCompletedSize();
1403 sint32 remainTime = file->getTimeRemaining();
1405 if (remainTime >= 0) {
1406 text = CastSecondsToHM(remainTime);
1407 } else {
1408 text = _("Unknown");
1411 text += wxT(" (") + CastItoXBytes(remainSize) + wxT(")");
1413 break;
1416 // Last seen completed
1417 case 10: {
1418 if ( file->lastseencomplete ) {
1419 text = wxDateTime( file->lastseencomplete ).Format( _("%y/%m/%d %H:%M:%S") );
1420 } else {
1421 text = _("Unknown");
1423 break;
1426 // Laste received
1427 case 11: {
1428 const time_t lastReceived = file->GetLastChangeDatetime();
1429 if (lastReceived) {
1430 text = wxDateTime(lastReceived).Format( _("%y/%m/%d %H:%M:%S") );
1431 } else {
1432 text = _("Unknown");
1435 } // switch
1437 if ( !text.IsEmpty() ) {
1438 dc->DrawText( text, rect.GetX(), rect.GetY() );
1443 void CDownloadListCtrl::DrawSourceItem(
1444 wxDC* dc, int nColumn, const wxRect& rect, CtrlItem_Struct* item ) const
1446 wxDCClipper clipper( *dc, rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight() );
1447 wxString buffer;
1449 const CUpDownClient* client = item->GetSource();
1451 switch (nColumn) {
1452 // Client name + various icons
1453 case 0: {
1454 wxRect cur_rec = rect;
1455 // +3 is added by OnDrawItem()... so take it off
1456 // Kry - eMule says +1, so I'm trusting it
1457 wxPoint point( cur_rec.GetX(), cur_rec.GetY()+1 );
1459 if (item->GetType() != A4AF_SOURCE) {
1460 uint8 image = 0;
1462 switch (client->GetDownloadState()) {
1463 case DS_CONNECTING:
1464 case DS_CONNECTED:
1465 case DS_WAITCALLBACK:
1466 case DS_TOOMANYCONNS:
1467 image = Client_Red_Smiley;
1468 break;
1469 case DS_ONQUEUE:
1470 if (client->IsRemoteQueueFull()) {
1471 image = Client_Grey_Smiley;
1472 } else {
1473 image = Client_Yellow_Smiley;
1475 break;
1476 case DS_DOWNLOADING:
1477 case DS_REQHASHSET:
1478 image = Client_Green_Smiley;
1479 break;
1480 case DS_NONEEDEDPARTS:
1481 case DS_LOWTOLOWIP:
1482 image = Client_Grey_Smiley;
1483 break;
1484 default: // DS_NONE i.e.
1485 image = Client_White_Smiley;
1488 m_ImageList.Draw(image, *dc, point.x, point.y,
1489 wxIMAGELIST_DRAW_TRANSPARENT);
1490 } else {
1491 m_ImageList.Draw(Client_Grey_Smiley, *dc, point.x, point.y,
1492 wxIMAGELIST_DRAW_TRANSPARENT);
1495 cur_rec.x += 20;
1496 wxPoint point2( cur_rec.GetX(), cur_rec.GetY() + 1 );
1498 uint8 clientImage;
1500 if ( client->IsFriend() ) {
1501 clientImage = Client_Friend_Smiley;
1502 } else {
1503 switch ( client->GetClientSoft() ) {
1504 case SO_AMULE:
1505 clientImage = Client_aMule_Smiley;
1506 break;
1507 case SO_MLDONKEY:
1508 case SO_NEW_MLDONKEY:
1509 case SO_NEW2_MLDONKEY:
1510 clientImage = Client_mlDonkey_Smiley;
1511 break;
1512 case SO_EDONKEY:
1513 case SO_EDONKEYHYBRID:
1514 clientImage = Client_eDonkeyHybrid_Smiley;
1515 break;
1516 case SO_EMULE:
1517 clientImage = Client_eMule_Smiley;
1518 break;
1519 case SO_LPHANT:
1520 clientImage = Client_lphant_Smiley;
1521 break;
1522 case SO_SHAREAZA:
1523 case SO_NEW_SHAREAZA:
1524 case SO_NEW2_SHAREAZA:
1525 clientImage = Client_Shareaza_Smiley;
1526 break;
1527 case SO_LXMULE:
1528 clientImage = Client_xMule_Smiley;
1529 break;
1530 default:
1531 // cDonkey, Compatible, Unknown
1532 // No icon for those yet.
1533 // Using the eMule one + '?'
1534 clientImage = Client_Unknown;
1535 break;
1539 m_ImageList.Draw(clientImage, *dc, point2.x, point.y,
1540 wxIMAGELIST_DRAW_TRANSPARENT);
1542 if (client->GetScoreRatio() > 1) {
1543 // Has credits, draw the gold star
1544 m_ImageList.Draw(Client_CreditsYellow_Smiley, *dc, point2.x, point.y,
1545 wxIMAGELIST_DRAW_TRANSPARENT );
1546 } else if ( client->ExtProtocolAvailable() ) {
1547 // Ext protocol -> Draw the '+'
1548 m_ImageList.Draw(Client_ExtendedProtocol_Smiley, *dc, point2.x, point.y,
1549 wxIMAGELIST_DRAW_TRANSPARENT);
1552 if (client->IsIdentified()) {
1553 // the 'v'
1554 m_ImageList.Draw(Client_SecIdent_Smiley, *dc, point2.x, point.y,
1555 wxIMAGELIST_DRAW_TRANSPARENT);
1556 } else if (client->IsBadGuy()) {
1557 // the 'X'
1558 m_ImageList.Draw(Client_BadGuy_Smiley, *dc, point2.x, point.y,
1559 wxIMAGELIST_DRAW_TRANSPARENT);
1562 if (client->HasObfuscatedConnectionBeenEstablished()) {
1563 // the "¿" except it's a key
1564 m_ImageList.Draw(Client_Encryption_Smiley, *dc, point2.x, point.y,
1565 wxIMAGELIST_DRAW_TRANSPARENT);
1568 wxString userName;
1569 #ifdef ENABLE_IP2COUNTRY
1570 // Draw the flag
1571 const CountryData& countrydata = theApp->amuledlg->m_IP2Country->GetCountryData(client->GetFullIP());
1572 dc->DrawBitmap(countrydata.Flag,
1573 rect.x + 40, rect.y + 5,
1574 wxIMAGELIST_DRAW_TRANSPARENT);
1576 userName << countrydata.Name;
1578 userName << wxT(" - ");
1579 #endif // ENABLE_IP2COUNTRY
1580 if (client->GetUserName().IsEmpty()) {
1581 userName << wxT("?");
1582 } else {
1583 userName << client->GetUserName();
1585 dc->DrawText(userName, rect.GetX() + 60, rect.GetY());
1587 break;
1589 case 3: // completed
1590 if (item->GetType() != A4AF_SOURCE && client->GetTransferredDown()) {
1591 buffer = CastItoXBytes(client->GetTransferredDown());
1592 dc->DrawText(buffer, rect.GetX(), rect.GetY());
1594 break;
1596 case 4: // speed
1597 if (item->GetType() != A4AF_SOURCE && client->GetKBpsDown() > 0.001) {
1598 buffer = wxString::Format(wxT("%.1f "),
1599 client->GetKBpsDown()) + _("kB/s");
1600 dc->DrawText(buffer, rect.GetX(), rect.GetY());
1602 break;
1604 case 5: // file info
1605 if ( thePrefs::ShowProgBar() ) {
1606 int iWidth = rect.GetWidth() - 2;
1607 int iHeight = rect.GetHeight() - 2;
1609 if ( item->GetType() != A4AF_SOURCE ) {
1610 uint32 dwTicks = GetTickCount();
1612 wxMemoryDC cdcStatus;
1614 if ( item->dwUpdated < dwTicks || !item->status ||
1615 iWidth != item->status->GetWidth() ) {
1617 if (item->status == NULL) {
1618 item->status = new wxBitmap(iWidth, iHeight);
1619 } else if ( item->status->GetWidth() != iWidth ) {
1620 // Only recreate if size has changed
1621 item->status->Create(iWidth, iHeight);
1624 cdcStatus.SelectObject(*(item->status));
1626 if ( thePrefs::UseFlatBar() ) {
1627 DrawSourceStatusBar( client, &cdcStatus,
1628 wxRect(0, 0, iWidth, iHeight), true);
1629 } else {
1630 DrawSourceStatusBar( client, &cdcStatus,
1631 wxRect(1, 1, iWidth - 2, iHeight - 2), false);
1633 // Draw black border
1634 cdcStatus.SetPen( *wxBLACK_PEN );
1635 cdcStatus.SetBrush( *wxTRANSPARENT_BRUSH );
1636 cdcStatus.DrawRectangle( 0, 0, iWidth, iHeight );
1639 // Plus ten seconds
1640 item->dwUpdated = dwTicks + 10000;
1641 } else {
1642 cdcStatus.SelectObject(*(item->status));
1645 dc->Blit(rect.GetX(), rect.GetY() + 1, iWidth, iHeight, &cdcStatus, 0, 0);
1646 } else {
1647 buffer = _("A4AF");
1649 int midx = (2*rect.GetX() + rect.GetWidth()) >> 1;
1650 int midy = (2*rect.GetY() + rect.GetHeight()) >> 1;
1652 wxCoord txtwidth, txtheight;
1654 dc->GetTextExtent(buffer, &txtwidth, &txtheight);
1656 dc->SetTextForeground(*wxBLACK);
1657 dc->DrawText(buffer, midx - (txtwidth >> 1), midy - (txtheight >> 1));
1659 // Draw black border
1660 dc->SetPen( *wxBLACK_PEN );
1661 dc->SetBrush( *wxTRANSPARENT_BRUSH );
1662 dc->DrawRectangle( rect.GetX(), rect.GetY() + 1, iWidth, iHeight );
1665 break;
1667 case 6: {
1668 // Version
1669 dc->DrawText(client->GetClientVerString(), rect.GetX(), rect.GetY());
1670 break;
1673 case 7: // prio
1674 // We only show priority for sources actually queued for that file
1675 if ( item->GetType() != A4AF_SOURCE &&
1676 client->GetDownloadState() == DS_ONQUEUE ) {
1677 if (client->IsRemoteQueueFull()) {
1678 buffer = _("Queue Full");
1679 dc->DrawText(buffer, rect.GetX(), rect.GetY());
1680 } else {
1681 if (client->GetRemoteQueueRank()) {
1682 sint16 qrDiff = client->GetRemoteQueueRank() -
1683 client->GetOldRemoteQueueRank();
1684 if(qrDiff == client->GetRemoteQueueRank() ) {
1685 qrDiff = 0;
1687 wxColour savedColour = dc->GetTextForeground();
1688 if( qrDiff < 0 ) {
1689 dc->SetTextForeground(*wxBLUE);
1691 if( qrDiff > 0 ) {
1692 dc->SetTextForeground(*wxRED);
1694 //if( qrDiff == 0 ) {
1695 // dc->SetTextForeground(*wxLIGHT_GREY);
1697 buffer = wxString::Format(_("QR: %u (%i)"),
1698 client->GetRemoteQueueRank(), qrDiff);
1699 dc->DrawText(buffer, rect.GetX(), rect.GetY());
1700 dc->SetTextForeground(savedColour);
1704 break;
1706 case 8: // status
1707 if (item->GetType() != A4AF_SOURCE) {
1708 buffer = DownloadStateToStr( client->GetDownloadState(),
1709 client->IsRemoteQueueFull() );
1710 } else {
1711 buffer = _("Asked for another file");
1712 if ( client->GetRequestFile() &&
1713 client->GetRequestFile()->GetFileName().IsOk()) {
1714 buffer += CFormat(wxT(" (%s)"))
1715 % client->GetRequestFile()->GetFileName();
1718 dc->DrawText(buffer, rect.GetX(), rect.GetY());
1719 break;
1720 // Source comes from?
1721 case 9: {
1722 buffer = wxGetTranslation(OriginToText(client->GetSourceFrom()));
1723 dc->DrawText(buffer, rect.GetX(), rect.GetY());
1724 break;
1730 wxString CDownloadListCtrl::GetTTSText(unsigned item) const
1732 CtrlItem_Struct* content = (CtrlItem_Struct*)GetItemData(item);
1734 if (content->GetType() == FILE_TYPE) {
1735 CPartFile* file = content->GetFile();
1737 return file->GetFileName().GetPrintable();
1740 return wxEmptyString;
1744 int CDownloadListCtrl::SortProc(wxUIntPtr param1, wxUIntPtr param2, long sortData)
1746 CtrlItem_Struct* item1 = (CtrlItem_Struct*)param1;
1747 CtrlItem_Struct* item2 = (CtrlItem_Struct*)param2;
1749 int sortMod = (sortData & CMuleListCtrl::SORT_DES) ? -1 : 1;
1750 sortData &= CMuleListCtrl::COLUMN_MASK;
1751 int comp = 0;
1753 if ( item1->GetType() == FILE_TYPE ) {
1754 if ( item2->GetType() == FILE_TYPE ) {
1755 // Both are files, so we just compare them
1756 comp = Compare( item1->GetFile(), item2->GetFile(), sortData);
1757 } else {
1758 // A file and a source, checking if they belong to each other
1759 if ( item1->GetFile() == item2->GetOwner() ) {
1760 // A file should always be above its sources
1761 // Returning directly to avoid the modifier
1762 return -1;
1763 } else {
1764 // Source belongs to anther file, so we compare the files instead
1765 comp = Compare( item1->GetFile(), item2->GetOwner(), sortData);
1768 } else {
1769 if ( item2->GetType() == FILE_TYPE ) {
1770 // A source and a file, checking if they belong to each other
1771 if ( item1->GetOwner() == item2->GetFile() ) {
1772 // A source should always be below its file
1773 // Returning directly to avoid the modifier
1774 return 1;
1775 } else {
1776 // Source belongs to anther file, so we compare the files instead
1777 comp = Compare( item1->GetOwner(), item2->GetFile(), sortData);
1779 } else {
1780 // Two sources, some different possibilites
1781 if ( item1->GetOwner() == item2->GetOwner() ) {
1782 // Avilable sources first, if we have both an
1783 // available and an unavailable
1784 comp = ( item2->GetType() - item1->GetType() );
1786 if (comp) {
1787 // A4AF and non-A4AF. The order is fixed regardless of sort-order.
1788 return comp;
1789 } else {
1790 comp = Compare(item1->GetSource(), item2->GetSource(), sortData);
1792 } else {
1793 // Belongs to different files, so we compare the files
1794 comp = Compare( item1->GetOwner(), item2->GetOwner(), sortData);
1799 // We modify the result so that it matches with ascending or decending
1800 return sortMod * comp;
1804 int CDownloadListCtrl::Compare( const CPartFile* file1, const CPartFile* file2, long lParamSort)
1806 int result = 0;
1808 switch (lParamSort) {
1809 // Sort by filename
1810 case 0:
1811 result = CmpAny(
1812 file1->GetFileName(),
1813 file2->GetFileName() );
1814 break;
1816 // Sort by size
1817 case 1:
1818 result = CmpAny(
1819 file1->GetFileSize(),
1820 file2->GetFileSize() );
1821 break;
1823 // Sort by transferred
1824 case 2:
1825 result = CmpAny(
1826 file1->GetTransferred(),
1827 file2->GetTransferred() );
1828 break;
1830 // Sort by completed
1831 case 3:
1832 result = CmpAny(
1833 file1->GetCompletedSize(),
1834 file2->GetCompletedSize() );
1835 break;
1837 // Sort by speed
1838 case 4:
1839 result = CmpAny(
1840 file1->GetKBpsDown() * 1024,
1841 file2->GetKBpsDown() * 1024 );
1842 break;
1844 // Sort by percentage completed
1845 case 5:
1846 result = CmpAny(
1847 file1->GetPercentCompleted(),
1848 file2->GetPercentCompleted() );
1849 break;
1851 // Sort by number of sources
1852 case 6:
1853 result = CmpAny(
1854 file1->GetSourceCount(),
1855 file2->GetSourceCount() );
1856 break;
1858 // Sort by priority
1859 case 7:
1860 result = CmpAny(
1861 file1->GetDownPriority(),
1862 file2->GetDownPriority() );
1863 break;
1865 // Sort by status
1866 case 8:
1867 result = CmpAny(
1868 file1->getPartfileStatusRang(),
1869 file2->getPartfileStatusRang() );
1870 break;
1872 // Sort by remaining time
1873 case 9:
1874 if (file1->getTimeRemaining() == -1) {
1875 if (file2->getTimeRemaining() == -1) {
1876 result = 0;
1877 } else {
1878 result = -1;
1880 } else {
1881 if (file2->getTimeRemaining() == -1) {
1882 result = 1;
1883 } else {
1884 result = CmpAny(
1885 file1->getTimeRemaining(),
1886 file2->getTimeRemaining() );
1889 break;
1891 // Sort by last seen complete
1892 case 10:
1893 result = CmpAny(
1894 file1->lastseencomplete,
1895 file2->lastseencomplete );
1896 break;
1898 // Sort by last reception
1899 case 11:
1900 result = CmpAny(
1901 file1->GetLastChangeDatetime(),
1902 file2->GetLastChangeDatetime() );
1903 break;
1906 return result;
1910 int CDownloadListCtrl::Compare(
1911 const CUpDownClient* client1, const CUpDownClient* client2, long lParamSort)
1913 switch (lParamSort) {
1914 // Sort by name
1915 case 0:
1916 return CmpAny( client1->GetUserName(), client2->GetUserName() );
1918 // Sort by status (size field)
1919 case 1:
1920 return CmpAny( client1->GetDownloadState(), client2->GetDownloadState() );
1922 // Sort by transferred in the following fields
1923 case 2:
1924 case 3:
1925 return CmpAny( client1->GetTransferredDown(), client2->GetTransferredDown() );
1927 // Sort by speed
1928 case 4:
1929 return CmpAny( client1->GetKBpsDown(), client2->GetKBpsDown() );
1931 // Sort by parts offered (Progress field)
1932 case 5:
1933 return CmpAny(
1934 client1->GetAvailablePartCount(),
1935 client2->GetAvailablePartCount() );
1937 // Sort by client version
1938 case 6: {
1939 if (client1->GetClientSoft() != client2->GetClientSoft()) {
1940 return client1->GetSoftStr().Cmp(client2->GetSoftStr());
1943 if (client1->GetVersion() != client2->GetVersion()) {
1944 return CmpAny(client1->GetVersion(), client2->GetVersion());
1947 return client1->GetClientModString().Cmp(client2->GetClientModString());
1950 // Sort by Queue-Rank
1951 case 7: {
1952 // This will sort by download state: Downloading, OnQueue, Connecting ...
1953 // However, Asked For Another will always be placed last, due to
1954 // sorting in SortProc
1955 if ( client1->GetDownloadState() != client2->GetDownloadState() ) {
1956 return client1->GetDownloadState() - client2->GetDownloadState();
1959 // Placing items on queue before items on full queues
1960 if ( client1->IsRemoteQueueFull() ) {
1961 if ( client2->IsRemoteQueueFull() ) {
1962 return 0;
1963 } else {
1964 return 1;
1966 } else if ( client2->IsRemoteQueueFull() ) {
1967 return -1;
1968 } else {
1969 if ( client1->GetRemoteQueueRank() ) {
1970 if ( client2->GetRemoteQueueRank() ) {
1971 return CmpAny(
1972 client1->GetRemoteQueueRank(),
1973 client2->GetRemoteQueueRank() );
1974 } else {
1975 return -1;
1977 } else {
1978 if ( client2->GetRemoteQueueRank() ) {
1979 return 1;
1980 } else {
1981 return 0;
1987 // Sort by state
1988 case 8: {
1989 if (client1->GetDownloadState() == client2->GetDownloadState()) {
1990 return CmpAny(
1991 client1->IsRemoteQueueFull(),
1992 client2->IsRemoteQueueFull() );
1993 } else {
1994 return CmpAny(
1995 client1->GetDownloadState(),
1996 client2->GetDownloadState() );
2000 // Source of source ;)
2001 case 9:
2002 return CmpAny(client1->GetSourceFrom(), client2->GetSourceFrom());
2004 default:
2005 return 0;
2010 void CDownloadListCtrl::ClearCompleted()
2012 m_completedFiles = false;
2013 CastByID(ID_BTNCLRCOMPL, GetParent(), wxButton)->Enable(false);
2015 // Search for completed files
2016 for ( ListItems::iterator it = m_ListItems.begin(); it != m_ListItems.end(); ) {
2017 CtrlItem_Struct* item = it->second; ++it;
2019 if ( item->GetType() == FILE_TYPE ) {
2020 CPartFile* file = item->GetFile();
2022 if ( file->IsPartFile() == false ) {
2023 RemoveFile(file);
2030 void CDownloadListCtrl::ShowFilesCount( int diff )
2032 m_filecount += diff;
2034 wxString str = wxString::Format( _("Downloads (%i)"), m_filecount );
2035 wxStaticText* label = CastByName( wxT("downloadsLabel"), GetParent(), wxStaticText );
2037 label->SetLabel( str );
2038 label->GetParent()->Layout();
2044 bool CDownloadListCtrl::ShowItemInCurrentCat(
2045 const CPartFile* file, int newsel ) const
2047 return
2048 ((newsel == 0 && !thePrefs::ShowAllNotCats()) ||
2049 (newsel == 0 && thePrefs::ShowAllNotCats() && file->GetCategory() == 0)) ||
2050 (newsel > 0 && newsel == file->GetCategory());
2055 void CDownloadListCtrl::DrawFileStatusBar(
2056 const CPartFile* file, wxDC* dc, const wxRect& rect, bool bFlat ) const
2058 static CBarShader s_ChunkBar(16);
2060 COLORREF crHave = ( bFlat ? RGB( 0, 0, 0 ) : RGB( 104, 104, 104 ) );
2061 COLORREF crPending = ( bFlat ? RGB( 255, 255, 100 ) : RGB( 255, 208, 0 ) );
2062 COLORREF crProgress = ( bFlat ? RGB( 0, 150, 0 ) : RGB( 0, 224, 0 ) );
2063 COLORREF crMissing = RGB(255, 0, 0);
2065 s_ChunkBar.SetHeight(rect.height);
2066 s_ChunkBar.SetWidth(rect.width);
2067 s_ChunkBar.SetFileSize( file->GetFileSize() );
2068 s_ChunkBar.Set3dDepth( thePrefs::Get3DDepth() );
2071 if ( file->GetStatus() == PS_COMPLETE || file->GetStatus() == PS_COMPLETING ) {
2072 s_ChunkBar.Fill( crProgress );
2073 s_ChunkBar.Draw(dc, rect.x, rect.y, bFlat);
2074 return;
2078 // Part availability ( of missing parts )
2079 const CPartFile::CGapPtrList& gaplist = file->GetGapList();
2080 CPartFile::CGapPtrList::const_iterator it = gaplist.begin();
2081 uint64 lastGapEnd = 0;
2082 for (; it != gaplist.end(); ++it) {
2083 Gap_Struct* gap = *it;
2085 // Start position
2086 uint32 start = ( gap->start / PARTSIZE );
2087 // fill the Have-Part (between this gap and the last)
2088 if (gap->start) {
2089 s_ChunkBar.FillRange(lastGapEnd + 1, gap->start - 1, crHave);
2091 lastGapEnd = gap->end;
2092 // End position
2093 uint32 end = ( gap->end / PARTSIZE ) + 1;
2095 // Avoid going past the filesize. Dunno if this can happen, but the old code did check.
2096 if ( end > file->GetPartCount() ) {
2097 end = file->GetPartCount();
2100 // Place each gap, one PART at a time
2101 for ( uint64 i = start; i < end; ++i ) {
2102 COLORREF color;
2103 if ( i < file->m_SrcpartFrequency.size() && file->m_SrcpartFrequency[i]) {
2104 int blue = 210 - ( 22 * ( file->m_SrcpartFrequency[i] - 1 ) );
2105 color = RGB( 0, ( blue < 0 ? 0 : blue ), 255 );
2106 } else {
2107 color = crMissing;
2110 if ( file->IsStopped() ) {
2111 color = DarkenColour( color, 2 );
2114 uint64 gap_begin = ( i == start ? gap->start : PARTSIZE * i );
2115 uint64 gap_end = ( i == end - 1 ? gap->end : PARTSIZE * ( i + 1 ) - 1 );
2117 s_ChunkBar.FillRange( gap_begin, gap_end, color);
2120 // fill the last Have-Part (between this gap and the last)
2121 s_ChunkBar.FillRange(lastGapEnd + 1, file->GetFileSize() - 1, crHave);
2124 // Pending parts
2125 const CPartFile::CReqBlockPtrList& requestedblocks_list = file->GetRequestedBlockList();
2126 CPartFile::CReqBlockPtrList::const_iterator it2 = requestedblocks_list.begin();
2127 // adjacing pending parts must be joined to avoid bright lines between them
2128 uint64 lastStartOffset = 0;
2129 uint64 lastEndOffset = 0;
2130 COLORREF color = file->IsStopped() ? DarkenColour( crPending, 2 ) : crPending;
2131 for (; it2 != requestedblocks_list.end(); ++it2) {
2133 if ((*it2)->StartOffset > lastEndOffset + 1) {
2134 // not adjacing, draw last block
2135 s_ChunkBar.FillRange(lastStartOffset, lastEndOffset, color);
2136 lastStartOffset = (*it2)->StartOffset;
2137 lastEndOffset = (*it2)->EndOffset;
2138 } else {
2139 // adjacing, grow block
2140 lastEndOffset = (*it2)->EndOffset;
2143 s_ChunkBar.FillRange(lastStartOffset, lastEndOffset, color);
2146 // Draw the progress-bar
2147 s_ChunkBar.Draw( dc, rect.x, rect.y, bFlat );
2150 // Green progressbar width
2151 int width = (int)(( (float)rect.width / (float)file->GetFileSize() ) *
2152 file->GetCompletedSize() );
2154 if ( bFlat ) {
2155 dc->SetBrush( wxBrush( crProgress, wxSOLID ) );
2157 dc->DrawRectangle( rect.x, rect.y, width, 3 );
2158 } else {
2159 // Draw the two black lines for 3d-effect
2160 dc->SetPen( wxPen( wxColour( 0, 0, 0 ), 1, wxSOLID ) );
2161 dc->DrawLine( rect.x, rect.y + 0, rect.x + width, rect.y + 0 );
2162 dc->DrawLine( rect.x, rect.y + 2, rect.x + width, rect.y + 2 );
2164 // Draw the green line
2165 dc->SetPen( wxPen( crProgress, 1, wxSOLID ) );
2166 dc->DrawLine( rect.x, rect.y + 1, rect.x + width, rect.y + 1 );
2171 void CDownloadListCtrl::DrawSourceStatusBar(
2172 const CUpDownClient* source, wxDC* dc, const wxRect& rect, bool bFlat) const
2174 static CBarShader s_StatusBar(16);
2176 uint32 crBoth = ( bFlat ? RGB( 0, 150, 0 ) : RGB( 0, 192, 0 ) );
2177 uint32 crNeither = ( bFlat ? RGB( 224, 224, 224 ) : RGB( 240, 240, 240 ) );
2178 uint32 crClientOnly = ( bFlat ? RGB( 0, 0, 0 ) : RGB( 104, 104, 104 ) );
2179 uint32 crPending = ( bFlat ? RGB( 255, 208, 0 ) : RGB( 255, 208, 0 ) );
2180 uint32 crNextPending = ( bFlat ? RGB( 255, 255, 100 ) : RGB( 255, 255, 100 ) );
2182 CPartFile* reqfile = source->GetRequestFile();
2184 s_StatusBar.SetFileSize( reqfile->GetFileSize() );
2185 s_StatusBar.SetHeight(rect.height);
2186 s_StatusBar.SetWidth(rect.width);
2187 s_StatusBar.Set3dDepth( thePrefs::Get3DDepth() );
2189 // Barry - was only showing one part from client, even when reserved bits from 2 parts
2190 wxString gettingParts = source->ShowDownloadingParts();
2192 const BitVector& partStatus = source->GetPartStatus();
2194 uint64 uEnd = 0;
2195 for ( uint64 i = 0; i < partStatus.size(); i++ ) {
2196 uint64 uStart = PARTSIZE * i;
2197 uEnd = wxMin(reqfile->GetFileSize(), uStart + PARTSIZE) - 1;
2199 uint32 color = 0;
2200 if (!partStatus[i]) {
2201 color = crNeither;
2202 } else if ( reqfile->IsComplete(uStart, uEnd)) {
2203 color = crBoth;
2204 } else if ( source->GetDownloadState() == DS_DOWNLOADING &&
2205 source->GetLastBlockOffset() <= uEnd &&
2206 source->GetLastBlockOffset() >= uStart) {
2207 color = crPending;
2208 } else if (gettingParts.GetChar((uint16)i) == 'Y') {
2209 color = crNextPending;
2210 } else {
2211 color = crClientOnly;
2214 if ( source->GetRequestFile()->IsStopped() ) {
2215 color = DarkenColour( color, 2 );
2218 s_StatusBar.FillRange(uStart, uEnd, color);
2220 // fill the rest (if partStatus is empty)
2221 s_StatusBar.FillRange(uEnd + 1, reqfile->GetFileSize() - 1, crNeither);
2223 s_StatusBar.Draw(dc, rect.x, rect.y, bFlat);
2226 #ifdef __WXMSW__
2227 # define QUOTE wxT("\"")
2228 #else
2229 # define QUOTE wxT("\'")
2230 #endif
2232 void CDownloadListCtrl::PreviewFile(CPartFile* file)
2234 wxString command;
2235 // If no player set in preferences, use mplayer.
2236 // And please, do a warning also :P
2237 if (thePrefs::GetVideoPlayer().IsEmpty()) {
2238 wxMessageBox(_(
2239 "Please set your preferred video player on preferences.\nMeanwhile, aMule will attempt to use mplayer and you will get this warning on every preview"),
2240 _("File preview"), wxOK, this);
2241 // Since newer versions for some reason mplayer does not automatically
2242 // select video output device and needs a parameter, go figure...
2243 command = wxT("xterm -T \"aMule Preview\" -iconic -e mplayer ") QUOTE wxT("$file") QUOTE;
2244 } else {
2245 command = thePrefs::GetVideoPlayer();
2248 // Check if we are (pre)viewing a completed file or not
2249 if (file->GetStatus() != PS_COMPLETE) {
2250 // Remove the .met and see if out video player specifiation uses the magic string
2251 wxString fileWithoutMet = thePrefs::GetTempDir().JoinPaths(
2252 file->GetPartMetFileName().RemoveExt()).GetRaw();
2253 if (!command.Replace(wxT("$file"), fileWithoutMet)) {
2254 // No magic string, so we just append the filename to the player command
2255 // Need to use quotes in case filename contains spaces
2256 command << wxT(" ") << QUOTE << fileWithoutMet << QUOTE;
2258 } else {
2259 // This is a complete file
2260 // FIXME: This is probably not going to work if the filenames are mangled ...
2261 wxString rawFileName = file->GetFullName().GetRaw();
2262 if (!command.Replace(wxT("$file"), rawFileName)) {
2263 // No magic string, so we just append the filename to the player command
2264 // Need to use quotes in case filename contains spaces
2265 command << wxT(" ") << QUOTE << rawFileName << QUOTE;
2269 // We can't use wxShell here, it blocks the app
2270 CTerminationProcess *p = new CTerminationProcess(command);
2271 int ret = wxExecute(command, wxEXEC_ASYNC, p);
2272 bool ok = ret > 0;
2273 if (!ok) {
2274 delete p;
2275 AddLogLineM( true,
2276 CFormat( _("ERROR: Failed to execute external media-player! Command: `%s'") ) %
2277 command );
2280 // File_checked_for_headers