Upstream tarball 20080603
[amule.git] / src / DownloadListCtrl.cpp
blob24da6d4ef062cab57a5c1e2c7c02ddbddf77272a
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);
726 for (ItemList::iterator it = files.begin(); it != files.end(); ++it) {
727 if (feed.IsEmpty()) {
728 feed = CFormat(_("Feedback from: %s (%s)\n\n")) % thePrefs::GetUserNick() % GetFullMuleVersion();
729 } else {
730 feed += wxT("\n");
732 feed += (*it)->GetFile()->GetFeedback();
735 if (!feed.IsEmpty()) {
736 theApp->CopyTextToClipboard(feed);
741 void CDownloadListCtrl::OnGetRazorStats( wxCommandEvent& WXUNUSED(event) )
743 ItemList files = ::GetSelectedItems( this, itFILES );
745 if ( files.size() == 1 ) {
746 CPartFile* file = files.front()->GetFile();
748 theApp->amuledlg->LaunchUrl(
749 wxT("http://stats.razorback2.com/ed2khistory?ed2k=") +
750 file->GetFileHash().Encode());
755 void CDownloadListCtrl::OnViewFileInfo( wxCommandEvent& WXUNUSED(event) )
757 ItemList files = ::GetSelectedItems( this, itFILES );
759 if ( files.size() == 1 ) {
760 CPartFile* file = files.front()->GetFile();
762 CFileDetailDialog dialog( this, file );
763 dialog.ShowModal();
768 void CDownloadListCtrl::OnViewFileComments( wxCommandEvent& WXUNUSED(event) )
770 ItemList files = ::GetSelectedItems( this, itFILES );
772 if ( files.size() == 1 ) {
773 CPartFile* file = files.front()->GetFile();
775 CCommentDialogLst dialog( this, file );
776 dialog.ShowModal();
781 void CDownloadListCtrl::OnPreviewFile( wxCommandEvent& WXUNUSED(event) )
783 ItemList files = ::GetSelectedItems( this, itFILES );
785 if ( files.size() == 1 ) {
786 PreviewFile(files.front()->GetFile());
790 void CDownloadListCtrl::OnSwapSource( wxCommandEvent& WXUNUSED(event) )
792 ItemList sources = ::GetSelectedItems( this, itSOURCES );
794 for ( ItemList::iterator it = sources.begin(); it != sources.end(); ++it ) {
795 CPartFile* file = (*it)->GetOwner();
796 CUpDownClient* source = (*it)->GetSource();
798 source->SwapToAnotherFile( true, false, false, file );
803 void CDownloadListCtrl::OnViewFiles( wxCommandEvent& WXUNUSED(event) )
805 ItemList sources = ::GetSelectedItems( this, itSOURCES );
807 if ( sources.size() == 1 ) {
808 CUpDownClient* source = sources.front()->GetSource();
810 source->RequestSharedFileList();
815 void CDownloadListCtrl::OnAddFriend( wxCommandEvent& WXUNUSED(event) )
817 ItemList sources = ::GetSelectedItems( this, itSOURCES );
819 for ( ItemList::iterator it = sources.begin(); it != sources.end(); ++it ) {
820 CUpDownClient* client = (*it)->GetSource();
821 if (client->IsFriend()) {
822 theApp->amuledlg->m_chatwnd->RemoveFriend(client->GetUserHash(), client->GetIP(), client->GetUserPort());
823 } else {
824 theApp->amuledlg->m_chatwnd->AddFriend( client );
830 void CDownloadListCtrl::OnSendMessage( wxCommandEvent& WXUNUSED(event) )
832 ItemList sources = ::GetSelectedItems( this, itSOURCES );
834 if ( sources.size() == 1 ) {
835 CUpDownClient* source = (sources.front())->GetSource();
837 // These values are cached, since calling wxGetTextFromUser will
838 // start an event-loop, in which the client may be deleted.
839 wxString userName = source->GetUserName();
840 uint64 userID = GUI_ID(source->GetIP(), source->GetUserPort());
842 wxString message = ::wxGetTextFromUser(_("Send message to user"),
843 _("Message to send:"));
844 if ( !message.IsEmpty() ) {
845 theApp->amuledlg->m_chatwnd->SendMessage(message, userName, userID);
851 void CDownloadListCtrl::OnViewClientInfo( wxCommandEvent& WXUNUSED(event) )
853 ItemList sources = ::GetSelectedItems( this, itSOURCES );
855 if ( sources.size() == 1 ) {
856 CUpDownClient* source = (sources.front())->GetSource();
858 CClientDetailDialog dialog( this, source );
859 dialog.ShowModal();
864 void CDownloadListCtrl::OnItemActivated( wxListEvent& evt )
866 CtrlItem_Struct* content = (CtrlItem_Struct*)GetItemData( evt.GetIndex() );
868 if ( content->GetType() == FILE_TYPE ) {
869 CPartFile* file = content->GetFile();
871 if ((!file->IsPartFile() || file->GetStatus() == PS_COMPLETE) && file->PreviewAvailable()) {
872 PreviewFile( file );
873 } else {
874 ShowSources( file, !file->ShowSources() );
881 void CDownloadListCtrl::OnMouseRightClick(wxListEvent& evt)
883 long index = CheckSelection(evt);
884 if (index < 0) {
885 return;
888 delete m_menu;
889 m_menu = NULL;
891 CtrlItem_Struct* item = (CtrlItem_Struct*)GetItemData( index );
892 if (item->GetType() == FILE_TYPE) {
893 m_menu = new wxMenu( _("Downloads") );
895 wxMenu* priomenu = new wxMenu();
896 priomenu->AppendCheckItem(MP_PRIOLOW, _("Low"));
897 priomenu->AppendCheckItem(MP_PRIONORMAL, _("Normal"));
898 priomenu->AppendCheckItem(MP_PRIOHIGH, _("High"));
899 priomenu->AppendCheckItem(MP_PRIOAUTO, _("Auto"));
901 m_menu->Append(MP_MENU_PRIO, _("Priority"), priomenu);
902 m_menu->Append(MP_CANCEL, _("Cancel"));
903 m_menu->Append(MP_STOP, _("&Stop"));
904 m_menu->Append(MP_PAUSE, _("&Pause"));
905 m_menu->Append(MP_RESUME, _("&Resume"));
906 m_menu->Append(MP_CLEARCOMPLETED, _("C&lear completed"));
907 //-----------------------------------------------------
908 m_menu->AppendSeparator();
909 //-----------------------------------------------------
910 wxMenu* extendedmenu = new wxMenu();
911 extendedmenu->Append(MP_SWAP_A4AF_TO_THIS,
912 _("Swap every A4AF to this file now"));
913 extendedmenu->AppendCheckItem(MP_SWAP_A4AF_TO_THIS_AUTO,
914 _("Swap every A4AF to this file (Auto)"));
915 //-----------------------------------------------------
916 extendedmenu->AppendSeparator();
917 //-----------------------------------------------------
918 extendedmenu->Append(MP_SWAP_A4AF_TO_ANY_OTHER,
919 _("Swap every A4AF to any other file now"));
920 //-----------------------------------------------------
921 m_menu->Append(MP_MENU_EXTD,
922 _("Extended Options"), extendedmenu);
923 //-----------------------------------------------------
924 m_menu->AppendSeparator();
925 //-----------------------------------------------------
926 /* Commented out till RB2 is back
927 m_menu->Append( MP_RAZORSTATS,
928 _("Get Razorback 2's stats for this file"));
929 //-----------------------------------------------------
930 m_menu->AppendSeparator();
931 //-----------------------------------------------------
933 m_menu->Append(MP_VIEW, _("Preview"));
934 m_menu->Append(MP_METINFO, _("Show file &details"));
935 m_menu->Append(MP_VIEWFILECOMMENTS,
936 _("Show all comments"));
937 //-----------------------------------------------------
938 m_menu->AppendSeparator();
939 //-----------------------------------------------------
940 m_menu->Append(MP_GETMAGNETLINK,
941 _("Copy magnet URI to clipboard"));
942 m_menu->Append(MP_GETED2KLINK,
943 _("Copy ED2k &link to clipboard"));
944 m_menu->Append(MP_WS,
945 _("Copy feedback to clipboard"));
946 //-----------------------------------------------------
947 m_menu->AppendSeparator();
948 //-----------------------------------------------------
949 // Add dinamic entries
950 wxMenu *cats = new wxMenu(_("Category"));
951 if (theApp->glob_prefs->GetCatCount() > 1) {
952 for (uint32 i = 0; i < theApp->glob_prefs->GetCatCount(); i++) {
953 if ( i == 0 ) {
954 cats->Append( MP_ASSIGNCAT, _("unassign") );
955 } else {
956 cats->Append( MP_ASSIGNCAT + i,
957 theApp->glob_prefs->GetCategory(i)->title );
961 m_menu->Append(MP_MENU_CATS, _("Assign to category"), cats);
962 m_menu->Enable(MP_MENU_CATS, (theApp->glob_prefs->GetCatCount() > 1) );
964 CPartFile* file = item->GetFile();
965 // then set state
966 bool canStop;
967 bool canPause;
968 bool canCancel;
969 bool fileResumable;
970 if (file->GetStatus(true) != PS_ALLOCATING) {
971 const uint8_t fileStatus = file->GetStatus();
972 canStop =
973 (fileStatus != PS_ERROR) &&
974 (fileStatus != PS_COMPLETE) &&
975 (file->IsStopped() != true);
976 canPause = (file->GetStatus() != PS_PAUSED) && canStop;
977 fileResumable =
978 (fileStatus == PS_PAUSED) ||
979 (fileStatus == PS_ERROR) ||
980 (fileStatus == PS_INSUFFICIENT);
981 canCancel = fileStatus != PS_COMPLETE;
982 } else {
983 canStop = canPause = canCancel = fileResumable = false;
986 wxMenu* menu = m_menu;
987 menu->Enable( MP_CANCEL, canCancel );
988 menu->Enable( MP_PAUSE, canPause );
989 menu->Enable( MP_STOP, canStop );
990 menu->Enable( MP_RESUME, fileResumable );
991 menu->Enable( MP_CLEARCOMPLETED, m_completedFiles );
993 wxString view;
994 if (file->IsPartFile() && (file->GetStatus() != PS_COMPLETE)) {
995 view << CFormat(wxT("%s [%s]")) % _("Preview")
996 % file->GetPartMetFileName().RemoveExt();
997 } else if ( file->GetStatus() == PS_COMPLETE ) {
998 view << _("&Open the file");
1000 menu->SetLabel(MP_VIEW, view);
1001 menu->Enable(MP_VIEW, file->PreviewAvailable() );
1003 menu->Check( MP_SWAP_A4AF_TO_THIS_AUTO, file->IsA4AFAuto() );
1005 int priority = file->IsAutoDownPriority() ?
1006 PR_AUTO : file->GetDownPriority();
1008 priomenu->Check( MP_PRIOHIGH, priority == PR_HIGH );
1009 priomenu->Check( MP_PRIONORMAL, priority == PR_NORMAL );
1010 priomenu->Check( MP_PRIOLOW, priority == PR_LOW );
1011 priomenu->Check( MP_PRIOAUTO, priority == PR_AUTO );
1013 menu->Enable( MP_MENU_EXTD, canPause );
1015 PopupMenu(m_menu, evt.GetPoint());
1017 } else {
1018 CUpDownClient* client = item->GetSource();
1020 m_menu = new wxMenu(wxT("Clients"));
1021 m_menu->Append(MP_DETAIL, _("Show &Details"));
1022 m_menu->Append(MP_ADDFRIEND, client->IsFriend() ? _("Remove from friends") : _("Add to Friends"));
1023 m_menu->Append(MP_SHOWLIST, _("View Files"));
1024 m_menu->Append(MP_SENDMESSAGE, _("Send message"));
1025 m_menu->Append(MP_CHANGE2FILE, _("Swap to this file"));
1027 // Only enable the Swap option for A4AF sources
1028 m_menu->Enable(MP_CHANGE2FILE, (item->GetType() == A4AF_SOURCE));
1029 // We need a valid IP if we are to message the client
1030 m_menu->Enable(MP_SENDMESSAGE, (client->GetIP() != 0));
1032 m_menu->Enable(MP_SHOWLIST, !client->HasDisabledSharedFiles());
1034 PopupMenu(m_menu, evt.GetPoint());
1038 delete m_menu;
1039 m_menu = NULL;
1044 void CDownloadListCtrl::OnMouseMiddleClick(wxListEvent& evt)
1046 // Check if clicked item is selected. If not, unselect all and select it.
1047 long index = CheckSelection(evt);
1048 if ( index < 0 ) {
1049 return;
1052 CtrlItem_Struct* item = (CtrlItem_Struct*)GetItemData( index );
1054 if ( item->GetType() == FILE_TYPE ) {
1055 CFileDetailDialog(this, item->GetFile()).ShowModal();
1056 } else {
1057 CClientDetailDialog(this, item->GetSource()).ShowModal();
1062 void CDownloadListCtrl::OnKeyPressed( wxKeyEvent& event )
1064 // Check if delete was pressed
1065 switch (event.GetKeyCode()) {
1066 case WXK_NUMPAD_DELETE:
1067 case WXK_DELETE: {
1068 wxCommandEvent evt;
1069 OnCancelFile( evt );
1070 break;
1072 case WXK_F2: {
1073 ItemList files = ::GetSelectedItems( this, itFILES );
1074 if (files.size() == 1) {
1075 CPartFile* file = files.front()->GetFile();
1077 // Currently renaming of completed files causes problem with kad
1078 if (file->IsPartFile()) {
1079 wxString strNewName = ::wxGetTextFromUser(
1080 _("Enter new name for this file:"),
1081 _("File rename"), file->GetFileName().GetPrintable());
1083 CPath newName = CPath(strNewName);
1084 if (newName.IsOk() && (newName != file->GetFileName())) {
1085 theApp->sharedfiles->RenameFile(file, newName);
1089 break;
1091 default:
1092 event.Skip();
1097 void CDownloadListCtrl::OnDrawItem(
1098 int item, wxDC* dc, const wxRect& rect, const wxRect& rectHL, bool highlighted)
1100 // Don't do any drawing if there's nobody to see it.
1101 if ( !theApp->amuledlg->IsDialogVisible( CamuleDlg::DT_TRANSFER_WND ) ) {
1102 return;
1105 CtrlItem_Struct* content = (CtrlItem_Struct *)GetItemData(item);
1107 // Define text-color and background
1108 if ((content->GetType() == FILE_TYPE) && (highlighted)) {
1109 if (GetFocus()) {
1110 dc->SetBackground(*m_hilightBrush);
1111 dc->SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
1112 } else {
1113 dc->SetBackground(*m_hilightUnfocusBrush);
1114 dc->SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
1116 } else {
1117 dc->SetBackground(*(wxTheBrushList->FindOrCreateBrush(
1118 wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX), wxSOLID)));
1119 dc->SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
1123 // Define the border of the drawn area
1124 if ( highlighted ) {
1125 wxColour old;
1126 if ( ( content->GetType() == FILE_TYPE ) && !GetFocus() ) {
1127 old = m_hilightUnfocusBrush->GetColour();
1128 } else {
1129 old = m_hilightBrush->GetColour();
1132 wxColor newcol(
1133 ((int)old.Red() * 65) / 100,
1134 ((int)old.Green() * 65) / 100,
1135 ((int)old.Blue() * 65) / 100);
1137 dc->SetPen( wxPen(newcol, 1, wxSOLID) );
1138 } else {
1139 dc->SetPen(*wxTRANSPARENT_PEN);
1143 dc->SetBrush( dc->GetBackground() );
1144 dc->DrawRectangle( rectHL.x, rectHL.y, rectHL.width, rectHL.height );
1146 dc->SetPen(*wxTRANSPARENT_PEN);
1148 if ( content->GetType() == FILE_TYPE && ( !highlighted || !GetFocus() ) ) {
1149 // If we have category, override textforeground with what category tells us.
1150 CPartFile *file = content->GetFile();
1151 if ( file->GetCategory() ) {
1152 dc->SetTextForeground(
1153 WxColourFromCr(theApp->glob_prefs->GetCatColor(file->GetCategory())) );
1157 // Various constant values we use
1158 const int iTextOffset = ( rect.GetHeight() - dc->GetCharHeight() ) / 2;
1159 const int iOffset = 4;
1161 // The starting end ending position of the tree
1162 bool tree_show = false;
1163 int tree_start = 0;
1164 int tree_end = 0;
1166 wxRect cur_rec( iOffset, rect.y, 0, rect.height );
1167 for (int i = 0; i < GetColumnCount(); i++) {
1168 wxListItem listitem;
1169 GetColumn(i, listitem);
1171 if (listitem.GetWidth() > 2*iOffset) {
1172 cur_rec.width = listitem.GetWidth() - 2*iOffset;
1174 // Make a copy of the current rectangle so we can apply specific tweaks
1175 wxRect target_rec = cur_rec;
1176 if ( i == 5 ) {
1177 tree_show = ( listitem.GetWidth() > 0 );
1179 tree_start = cur_rec.x - iOffset;
1180 tree_end = cur_rec.x + iOffset;
1182 // Double the offset to make room for the cirle-marker
1183 target_rec.x += iOffset;
1184 target_rec.width -= iOffset;
1185 } else {
1186 // will ensure that text is about in the middle ;)
1187 target_rec.y += iTextOffset;
1190 // Draw the item
1191 if ( content->GetType() == FILE_TYPE ) {
1192 DrawFileItem(dc, i, target_rec, content);
1193 } else {
1194 DrawSourceItem(dc, i, target_rec, content);
1198 // Increment to the next column
1199 cur_rec.x += listitem.GetWidth();
1202 // Draw tree last so it draws over selected and focus (looks better)
1203 if ( tree_show ) {
1204 // Gather some information
1205 const bool notLast = item + 1 != GetItemCount();
1206 const bool notFirst = item != 0;
1207 const bool hasNext = notLast &&
1208 ((CtrlItem_Struct*)GetItemData(item + 1))->GetType() != FILE_TYPE;
1209 const bool isOpenRoot = content->GetType() == FILE_TYPE &&
1210 (content->GetFile())->ShowSources() &&
1211 ((content->GetFile())->GetStatus() != PS_COMPLETE);
1212 const bool isChild = content->GetType() != FILE_TYPE;
1214 // Might as well calculate these now
1215 const int treeCenter = tree_start + 3;
1216 const int middle = cur_rec.y + ( cur_rec.height + 1 ) / 2;
1218 // Set up a new pen for drawing the tree
1219 dc->SetPen( *(wxThePenList->FindOrCreatePen(dc->GetTextForeground(), 1, wxSOLID)) );
1221 if (isChild) {
1222 // Draw the line to the status bar
1223 dc->DrawLine(tree_end, middle, tree_start + 3, middle);
1225 // Draw the line to the child node
1226 if (hasNext) {
1227 dc->DrawLine(treeCenter, middle, treeCenter, cur_rec.y + cur_rec.height + 1);
1230 // Draw the line back up to parent node
1231 if (notFirst) {
1232 dc->DrawLine(treeCenter, middle, treeCenter, cur_rec.y - 1);
1234 } else if ( isOpenRoot ) {
1235 // Draw empty circle
1236 dc->SetBrush(*wxTRANSPARENT_BRUSH);
1238 dc->DrawCircle( treeCenter, middle, 3 );
1240 // Draw the line to the child node if there are any children
1241 if (hasNext) {
1242 dc->DrawLine(treeCenter, middle + 3, treeCenter, cur_rec.y + cur_rec.height + 1);
1250 void CDownloadListCtrl::DrawFileItem( wxDC* dc, int nColumn, const wxRect& rect, CtrlItem_Struct* item ) const
1252 wxDCClipper clipper( *dc, rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight() );
1254 const CPartFile* file = item->GetFile();
1256 // Used to contain the contenst of cells that dont need any fancy drawing, just text.
1257 wxString text;
1259 switch (nColumn) {
1260 // Filename
1261 case 0: {
1262 // show no. of partfile in filename column
1263 wxString filename;
1264 if (thePrefs::ShowPartFileNumber()) {
1265 if (file->IsPartFile() && !(file->GetStatus() == PS_COMPLETE)) {
1266 filename = CFormat(wxT("[%s] ")) % file->GetPartMetFileName().RemoveAllExt();
1269 filename += file->GetFileName().GetPrintable();
1271 if (file->HasRating() || file->HasComment()) {
1272 int image = Client_CommentOnly_Smiley;
1273 if (file->HasRating()) {
1274 image = Client_InvalidRating_Smiley + file->UserRating() - 1;
1277 wxASSERT(image >= Client_InvalidRating_Smiley);
1278 wxASSERT(image <= Client_CommentOnly_Smiley);
1280 int imgWidth = 16;
1282 // it's already centered by OnDrawItem() ...
1283 m_ImageList.Draw(image, *dc, rect.GetX(), rect.GetY() - 1,
1284 wxIMAGELIST_DRAW_TRANSPARENT);
1285 dc->DrawText(filename, rect.GetX() + imgWidth + 4, rect.GetY());
1286 } else {
1287 dc->DrawText(filename, rect.GetX(), rect.GetY());
1290 break;
1292 // Filesize
1293 case 1:
1294 text = CastItoXBytes( file->GetFileSize() );
1295 break;
1297 // Transferred
1298 case 2:
1299 text = CastItoXBytes( file->GetTransferred() );
1300 break;
1302 // Completed
1303 case 3:
1304 text = CastItoXBytes( file->GetCompletedSize() );
1305 break;
1307 // Speed
1308 case 4: // speed
1309 if ( file->GetTransferingSrcCount() ) {
1310 text = wxString::Format( wxT("%.1f "), file->GetKBpsDown() ) +
1311 _("kB/s");
1313 break;
1315 case 5: // progress
1317 if (thePrefs::ShowProgBar())
1319 int iWidth = rect.GetWidth() - 2;
1320 int iHeight = rect.GetHeight() - 2;
1322 // DO NOT DRAW IT ALL THE TIME
1323 uint32 dwTicks = GetTickCount();
1325 wxMemoryDC cdcStatus;
1327 if ( item->dwUpdated < dwTicks || !item->status || iWidth != item->status->GetWidth() ) {
1328 if ( item->status == NULL) {
1329 item->status = new wxBitmap(iWidth, iHeight);
1330 } else if ( item->status->GetWidth() != iWidth ) {
1331 // Only recreate if the size has changed
1332 item->status->Create(iWidth, iHeight);
1335 cdcStatus.SelectObject( *item->status );
1337 if ( thePrefs::UseFlatBar() ) {
1338 DrawFileStatusBar( file, &cdcStatus,
1339 wxRect(0, 0, iWidth, iHeight), true);
1340 } else {
1341 DrawFileStatusBar( file, &cdcStatus,
1342 wxRect(1, 1, iWidth - 2, iHeight - 2), false);
1344 // Draw black border
1345 cdcStatus.SetPen( *wxBLACK_PEN );
1346 cdcStatus.SetBrush( *wxTRANSPARENT_BRUSH );
1347 cdcStatus.DrawRectangle( 0, 0, iWidth, iHeight );
1350 item->dwUpdated = dwTicks + 5000; // Plus five seconds
1351 } else {
1352 cdcStatus.SelectObject( *item->status );
1355 dc->Blit( rect.GetX(), rect.GetY() + 1, iWidth, iHeight, &cdcStatus, 0, 0);
1358 if (thePrefs::ShowPercent()) {
1359 // Percentage of completing
1360 // We strip anything below the first decimal point,
1361 // to avoid Format doing roundings
1362 float percent = floor( file->GetPercentCompleted() * 10.0f ) / 10.0f;
1364 wxString buffer = wxString::Format( wxT("%.1f%%"), percent );
1365 int middlex = (2*rect.GetX() + rect.GetWidth()) >> 1;
1366 int middley = (2*rect.GetY() + rect.GetHeight()) >> 1;
1368 wxCoord textwidth, textheight;
1370 dc->GetTextExtent(buffer, &textwidth, &textheight);
1371 wxColour AktColor = dc->GetTextForeground();
1372 if (thePrefs::ShowProgBar()) {
1373 dc->SetTextForeground(*wxWHITE);
1374 } else {
1375 dc->SetTextForeground(*wxBLACK);
1377 dc->DrawText(buffer, middlex - (textwidth >> 1), middley - (textheight >> 1));
1378 dc->SetTextForeground(AktColor);
1381 break;
1383 // Sources
1384 case 6: {
1385 uint16 sc = file->GetSourceCount();
1386 uint16 ncsc = file->GetNotCurrentSourcesCount();
1387 if ( ncsc ) {
1388 text = wxString::Format( wxT("%i/%i" ), sc - ncsc, sc );
1389 } else {
1390 text = wxString::Format( wxT("%i"), sc );
1393 if ( file->GetSrcA4AFCount() ) {
1394 text += wxString::Format( wxT("+%i"), file->GetSrcA4AFCount() );
1397 if ( file->GetTransferingSrcCount() ) {
1398 text += wxString::Format( wxT(" (%i)"), file->GetTransferingSrcCount() );
1401 break;
1404 // Priority
1405 case 7:
1406 text = PriorityToStr( file->GetDownPriority(), file->IsAutoDownPriority() );
1407 break;
1409 // File-status
1410 case 8:
1411 text = file->getPartfileStatus();
1412 break;
1414 // Remaining
1415 case 9: {
1416 if ((file->GetStatus() != PS_COMPLETING) && file->IsPartFile()) {
1417 uint64 remainSize = file->GetFileSize() - file->GetCompletedSize();
1418 sint32 remainTime = file->getTimeRemaining();
1420 if (remainTime >= 0) {
1421 text = CastSecondsToHM(remainTime);
1422 } else {
1423 text = _("Unknown");
1426 text += wxT(" (") + CastItoXBytes(remainSize) + wxT(")");
1428 break;
1431 // Last seen completed
1432 case 10: {
1433 if ( file->lastseencomplete ) {
1434 text = wxDateTime( file->lastseencomplete ).Format( _("%y/%m/%d %H:%M:%S") );
1435 } else {
1436 text = _("Unknown");
1438 break;
1441 // Laste received
1442 case 11: {
1443 const time_t lastReceived = file->GetLastChangeDatetime();
1444 if (lastReceived) {
1445 text = wxDateTime(lastReceived).Format( _("%y/%m/%d %H:%M:%S") );
1446 } else {
1447 text = _("Unknown");
1450 } // switch
1452 if ( !text.IsEmpty() ) {
1453 dc->DrawText( text, rect.GetX(), rect.GetY() );
1458 void CDownloadListCtrl::DrawSourceItem(
1459 wxDC* dc, int nColumn, const wxRect& rect, CtrlItem_Struct* item ) const
1461 wxDCClipper clipper( *dc, rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight() );
1462 wxString buffer;
1464 const CUpDownClient* client = item->GetSource();
1466 switch (nColumn) {
1467 // Client name + various icons
1468 case 0: {
1469 wxRect cur_rec = rect;
1470 // +3 is added by OnDrawItem()... so take it off
1471 // Kry - eMule says +1, so I'm trusting it
1472 wxPoint point( cur_rec.GetX(), cur_rec.GetY()+1 );
1474 if (item->GetType() != A4AF_SOURCE) {
1475 uint8 image = 0;
1477 switch (client->GetDownloadState()) {
1478 case DS_CONNECTING:
1479 case DS_CONNECTED:
1480 case DS_WAITCALLBACK:
1481 case DS_TOOMANYCONNS:
1482 image = Client_Red_Smiley;
1483 break;
1484 case DS_ONQUEUE:
1485 if (client->IsRemoteQueueFull()) {
1486 image = Client_Grey_Smiley;
1487 } else {
1488 image = Client_Yellow_Smiley;
1490 break;
1491 case DS_DOWNLOADING:
1492 case DS_REQHASHSET:
1493 image = Client_Green_Smiley;
1494 break;
1495 case DS_NONEEDEDPARTS:
1496 case DS_LOWTOLOWIP:
1497 image = Client_Grey_Smiley;
1498 break;
1499 default: // DS_NONE i.e.
1500 image = Client_White_Smiley;
1503 m_ImageList.Draw(image, *dc, point.x, point.y,
1504 wxIMAGELIST_DRAW_TRANSPARENT);
1505 } else {
1506 m_ImageList.Draw(Client_Grey_Smiley, *dc, point.x, point.y,
1507 wxIMAGELIST_DRAW_TRANSPARENT);
1510 cur_rec.x += 20;
1511 wxPoint point2( cur_rec.GetX(), cur_rec.GetY() + 1 );
1513 uint8 clientImage;
1515 if ( client->IsFriend() ) {
1516 clientImage = Client_Friend_Smiley;
1517 } else {
1518 switch ( client->GetClientSoft() ) {
1519 case SO_AMULE:
1520 clientImage = Client_aMule_Smiley;
1521 break;
1522 case SO_MLDONKEY:
1523 case SO_NEW_MLDONKEY:
1524 case SO_NEW2_MLDONKEY:
1525 clientImage = Client_mlDonkey_Smiley;
1526 break;
1527 case SO_EDONKEY:
1528 case SO_EDONKEYHYBRID:
1529 clientImage = Client_eDonkeyHybrid_Smiley;
1530 break;
1531 case SO_EMULE:
1532 clientImage = Client_eMule_Smiley;
1533 break;
1534 case SO_LPHANT:
1535 clientImage = Client_lphant_Smiley;
1536 break;
1537 case SO_SHAREAZA:
1538 case SO_NEW_SHAREAZA:
1539 case SO_NEW2_SHAREAZA:
1540 clientImage = Client_Shareaza_Smiley;
1541 break;
1542 case SO_LXMULE:
1543 clientImage = Client_xMule_Smiley;
1544 break;
1545 default:
1546 // cDonkey, Compatible, Unknown
1547 // No icon for those yet.
1548 // Using the eMule one + '?'
1549 clientImage = Client_Unknown;
1550 break;
1554 m_ImageList.Draw(clientImage, *dc, point2.x, point.y,
1555 wxIMAGELIST_DRAW_TRANSPARENT);
1557 if (client->GetScoreRatio() > 1) {
1558 // Has credits, draw the gold star
1559 m_ImageList.Draw(Client_CreditsYellow_Smiley, *dc, point2.x, point.y,
1560 wxIMAGELIST_DRAW_TRANSPARENT );
1561 } else if ( client->ExtProtocolAvailable() ) {
1562 // Ext protocol -> Draw the '+'
1563 m_ImageList.Draw(Client_ExtendedProtocol_Smiley, *dc, point2.x, point.y,
1564 wxIMAGELIST_DRAW_TRANSPARENT);
1567 if (client->IsIdentified()) {
1568 // the 'v'
1569 m_ImageList.Draw(Client_SecIdent_Smiley, *dc, point2.x, point.y,
1570 wxIMAGELIST_DRAW_TRANSPARENT);
1571 } else if (client->IsBadGuy()) {
1572 // the 'X'
1573 m_ImageList.Draw(Client_BadGuy_Smiley, *dc, point2.x, point.y,
1574 wxIMAGELIST_DRAW_TRANSPARENT);
1577 if (client->HasObfuscatedConnectionBeenEstablished()) {
1578 // the "¿" except it's a key
1579 m_ImageList.Draw(Client_Encryption_Smiley, *dc, point2.x, point.y,
1580 wxIMAGELIST_DRAW_TRANSPARENT);
1583 wxString userName;
1584 #ifdef ENABLE_IP2COUNTRY
1585 // Draw the flag
1586 const CountryData& countrydata = theApp->amuledlg->m_IP2Country->GetCountryData(client->GetFullIP());
1587 dc->DrawBitmap(countrydata.Flag,
1588 rect.x + 40, rect.y + 5,
1589 wxIMAGELIST_DRAW_TRANSPARENT);
1591 userName << countrydata.Name;
1593 userName << wxT(" - ");
1594 #endif // ENABLE_IP2COUNTRY
1595 if (client->GetUserName().IsEmpty()) {
1596 userName << wxT("?");
1597 } else {
1598 userName << client->GetUserName();
1600 dc->DrawText(userName, rect.GetX() + 60, rect.GetY());
1602 break;
1604 case 3: // completed
1605 if (item->GetType() != A4AF_SOURCE && client->GetTransferredDown()) {
1606 buffer = CastItoXBytes(client->GetTransferredDown());
1607 dc->DrawText(buffer, rect.GetX(), rect.GetY());
1609 break;
1611 case 4: // speed
1612 if (item->GetType() != A4AF_SOURCE && client->GetKBpsDown() > 0.001) {
1613 buffer = wxString::Format(wxT("%.1f "),
1614 client->GetKBpsDown()) + _("kB/s");
1615 dc->DrawText(buffer, rect.GetX(), rect.GetY());
1617 break;
1619 case 5: // file info
1620 if ( thePrefs::ShowProgBar() ) {
1621 int iWidth = rect.GetWidth() - 2;
1622 int iHeight = rect.GetHeight() - 2;
1624 if ( item->GetType() != A4AF_SOURCE ) {
1625 uint32 dwTicks = GetTickCount();
1627 wxMemoryDC cdcStatus;
1629 if ( item->dwUpdated < dwTicks || !item->status ||
1630 iWidth != item->status->GetWidth() ) {
1632 if (item->status == NULL) {
1633 item->status = new wxBitmap(iWidth, iHeight);
1634 } else if ( item->status->GetWidth() != iWidth ) {
1635 // Only recreate if size has changed
1636 item->status->Create(iWidth, iHeight);
1639 cdcStatus.SelectObject(*(item->status));
1641 if ( thePrefs::UseFlatBar() ) {
1642 DrawSourceStatusBar( client, &cdcStatus,
1643 wxRect(0, 0, iWidth, iHeight), true);
1644 } else {
1645 DrawSourceStatusBar( client, &cdcStatus,
1646 wxRect(1, 1, iWidth - 2, iHeight - 2), false);
1648 // Draw black border
1649 cdcStatus.SetPen( *wxBLACK_PEN );
1650 cdcStatus.SetBrush( *wxTRANSPARENT_BRUSH );
1651 cdcStatus.DrawRectangle( 0, 0, iWidth, iHeight );
1654 // Plus ten seconds
1655 item->dwUpdated = dwTicks + 10000;
1656 } else {
1657 cdcStatus.SelectObject(*(item->status));
1660 dc->Blit(rect.GetX(), rect.GetY() + 1, iWidth, iHeight, &cdcStatus, 0, 0);
1661 } else {
1662 buffer = _("A4AF");
1664 int midx = (2*rect.GetX() + rect.GetWidth()) >> 1;
1665 int midy = (2*rect.GetY() + rect.GetHeight()) >> 1;
1667 wxCoord txtwidth, txtheight;
1669 dc->GetTextExtent(buffer, &txtwidth, &txtheight);
1671 dc->SetTextForeground(*wxBLACK);
1672 dc->DrawText(buffer, midx - (txtwidth >> 1), midy - (txtheight >> 1));
1674 // Draw black border
1675 dc->SetPen( *wxBLACK_PEN );
1676 dc->SetBrush( *wxTRANSPARENT_BRUSH );
1677 dc->DrawRectangle( rect.GetX(), rect.GetY() + 1, iWidth, iHeight );
1680 break;
1682 case 6: {
1683 // Version
1684 dc->DrawText(client->GetClientVerString(), rect.GetX(), rect.GetY());
1685 break;
1688 case 7: // prio
1689 // We only show priority for sources actually queued for that file
1690 if ( item->GetType() != A4AF_SOURCE &&
1691 client->GetDownloadState() == DS_ONQUEUE ) {
1692 if (client->IsRemoteQueueFull()) {
1693 buffer = _("Queue Full");
1694 dc->DrawText(buffer, rect.GetX(), rect.GetY());
1695 } else {
1696 if (client->GetRemoteQueueRank()) {
1697 sint16 qrDiff = client->GetRemoteQueueRank() -
1698 client->GetOldRemoteQueueRank();
1699 if(qrDiff == client->GetRemoteQueueRank() ) {
1700 qrDiff = 0;
1702 wxColour savedColour = dc->GetTextForeground();
1703 if( qrDiff < 0 ) {
1704 dc->SetTextForeground(*wxBLUE);
1706 if( qrDiff > 0 ) {
1707 dc->SetTextForeground(*wxRED);
1709 //if( qrDiff == 0 ) {
1710 // dc->SetTextForeground(*wxLIGHT_GREY);
1712 buffer = wxString::Format(_("QR: %u (%i)"),
1713 client->GetRemoteQueueRank(), qrDiff);
1714 dc->DrawText(buffer, rect.GetX(), rect.GetY());
1715 dc->SetTextForeground(savedColour);
1719 break;
1721 case 8: // status
1722 if (item->GetType() != A4AF_SOURCE) {
1723 buffer = DownloadStateToStr( client->GetDownloadState(),
1724 client->IsRemoteQueueFull() );
1725 } else {
1726 buffer = _("Asked for another file");
1727 if ( client->GetRequestFile() &&
1728 client->GetRequestFile()->GetFileName().IsOk()) {
1729 buffer += CFormat(wxT(" (%s)"))
1730 % client->GetRequestFile()->GetFileName();
1733 dc->DrawText(buffer, rect.GetX(), rect.GetY());
1734 break;
1735 // Source comes from?
1736 case 9: {
1737 buffer = wxGetTranslation(OriginToText(client->GetSourceFrom()));
1738 dc->DrawText(buffer, rect.GetX(), rect.GetY());
1739 break;
1745 wxString CDownloadListCtrl::GetTTSText(unsigned item) const
1747 CtrlItem_Struct* content = (CtrlItem_Struct*)GetItemData(item);
1749 if (content->GetType() == FILE_TYPE) {
1750 CPartFile* file = content->GetFile();
1752 return file->GetFileName().GetPrintable();
1755 return wxEmptyString;
1759 int CDownloadListCtrl::SortProc(wxUIntPtr param1, wxUIntPtr param2, long sortData)
1761 CtrlItem_Struct* item1 = (CtrlItem_Struct*)param1;
1762 CtrlItem_Struct* item2 = (CtrlItem_Struct*)param2;
1764 int sortMod = (sortData & CMuleListCtrl::SORT_DES) ? -1 : 1;
1765 sortData &= CMuleListCtrl::COLUMN_MASK;
1766 int comp = 0;
1768 if ( item1->GetType() == FILE_TYPE ) {
1769 if ( item2->GetType() == FILE_TYPE ) {
1770 // Both are files, so we just compare them
1771 comp = Compare( item1->GetFile(), item2->GetFile(), sortData);
1772 } else {
1773 // A file and a source, checking if they belong to each other
1774 if ( item1->GetFile() == item2->GetOwner() ) {
1775 // A file should always be above its sources
1776 // Returning directly to avoid the modifier
1777 return -1;
1778 } else {
1779 // Source belongs to anther file, so we compare the files instead
1780 comp = Compare( item1->GetFile(), item2->GetOwner(), sortData);
1783 } else {
1784 if ( item2->GetType() == FILE_TYPE ) {
1785 // A source and a file, checking if they belong to each other
1786 if ( item1->GetOwner() == item2->GetFile() ) {
1787 // A source should always be below its file
1788 // Returning directly to avoid the modifier
1789 return 1;
1790 } else {
1791 // Source belongs to anther file, so we compare the files instead
1792 comp = Compare( item1->GetOwner(), item2->GetFile(), sortData);
1794 } else {
1795 // Two sources, some different possibilites
1796 if ( item1->GetOwner() == item2->GetOwner() ) {
1797 // Avilable sources first, if we have both an
1798 // available and an unavailable
1799 comp = ( item2->GetType() - item1->GetType() );
1801 if (comp) {
1802 // A4AF and non-A4AF. The order is fixed regardless of sort-order.
1803 return comp;
1804 } else {
1805 comp = Compare(item1->GetSource(), item2->GetSource(), sortData);
1807 } else {
1808 // Belongs to different files, so we compare the files
1809 comp = Compare( item1->GetOwner(), item2->GetOwner(), sortData);
1814 // We modify the result so that it matches with ascending or decending
1815 return sortMod * comp;
1819 int CDownloadListCtrl::Compare( const CPartFile* file1, const CPartFile* file2, long lParamSort)
1821 int result = 0;
1823 switch (lParamSort) {
1824 // Sort by filename
1825 case 0:
1826 result = CmpAny(
1827 file1->GetFileName(),
1828 file2->GetFileName() );
1829 break;
1831 // Sort by size
1832 case 1:
1833 result = CmpAny(
1834 file1->GetFileSize(),
1835 file2->GetFileSize() );
1836 break;
1838 // Sort by transferred
1839 case 2:
1840 result = CmpAny(
1841 file1->GetTransferred(),
1842 file2->GetTransferred() );
1843 break;
1845 // Sort by completed
1846 case 3:
1847 result = CmpAny(
1848 file1->GetCompletedSize(),
1849 file2->GetCompletedSize() );
1850 break;
1852 // Sort by speed
1853 case 4:
1854 result = CmpAny(
1855 file1->GetKBpsDown() * 1024,
1856 file2->GetKBpsDown() * 1024 );
1857 break;
1859 // Sort by percentage completed
1860 case 5:
1861 result = CmpAny(
1862 file1->GetPercentCompleted(),
1863 file2->GetPercentCompleted() );
1864 break;
1866 // Sort by number of sources
1867 case 6:
1868 result = CmpAny(
1869 file1->GetSourceCount(),
1870 file2->GetSourceCount() );
1871 break;
1873 // Sort by priority
1874 case 7:
1875 result = CmpAny(
1876 file1->GetDownPriority(),
1877 file2->GetDownPriority() );
1878 break;
1880 // Sort by status
1881 case 8:
1882 result = CmpAny(
1883 file1->getPartfileStatusRang(),
1884 file2->getPartfileStatusRang() );
1885 break;
1887 // Sort by remaining time
1888 case 9:
1889 if (file1->getTimeRemaining() == -1) {
1890 if (file2->getTimeRemaining() == -1) {
1891 result = 0;
1892 } else {
1893 result = -1;
1895 } else {
1896 if (file2->getTimeRemaining() == -1) {
1897 result = 1;
1898 } else {
1899 result = CmpAny(
1900 file1->getTimeRemaining(),
1901 file2->getTimeRemaining() );
1904 break;
1906 // Sort by last seen complete
1907 case 10:
1908 result = CmpAny(
1909 file1->lastseencomplete,
1910 file2->lastseencomplete );
1911 break;
1913 // Sort by last reception
1914 case 11:
1915 result = CmpAny(
1916 file1->GetLastChangeDatetime(),
1917 file2->GetLastChangeDatetime() );
1918 break;
1921 return result;
1925 int CDownloadListCtrl::Compare(
1926 const CUpDownClient* client1, const CUpDownClient* client2, long lParamSort)
1928 switch (lParamSort) {
1929 // Sort by name
1930 case 0:
1931 return CmpAny( client1->GetUserName(), client2->GetUserName() );
1933 // Sort by status (size field)
1934 case 1:
1935 return CmpAny( client1->GetDownloadState(), client2->GetDownloadState() );
1937 // Sort by transferred in the following fields
1938 case 2:
1939 case 3:
1940 return CmpAny( client1->GetTransferredDown(), client2->GetTransferredDown() );
1942 // Sort by speed
1943 case 4:
1944 return CmpAny( client1->GetKBpsDown(), client2->GetKBpsDown() );
1946 // Sort by parts offered (Progress field)
1947 case 5:
1948 return CmpAny(
1949 client1->GetAvailablePartCount(),
1950 client2->GetAvailablePartCount() );
1952 // Sort by client version
1953 case 6: {
1954 if (client1->GetClientSoft() != client2->GetClientSoft()) {
1955 return client1->GetSoftStr().Cmp(client2->GetSoftStr());
1958 if (client1->GetVersion() != client2->GetVersion()) {
1959 return CmpAny(client1->GetVersion(), client2->GetVersion());
1962 return client1->GetClientModString().Cmp(client2->GetClientModString());
1965 // Sort by Queue-Rank
1966 case 7: {
1967 // This will sort by download state: Downloading, OnQueue, Connecting ...
1968 // However, Asked For Another will always be placed last, due to
1969 // sorting in SortProc
1970 if ( client1->GetDownloadState() != client2->GetDownloadState() ) {
1971 return client1->GetDownloadState() - client2->GetDownloadState();
1974 // Placing items on queue before items on full queues
1975 if ( client1->IsRemoteQueueFull() ) {
1976 if ( client2->IsRemoteQueueFull() ) {
1977 return 0;
1978 } else {
1979 return 1;
1981 } else if ( client2->IsRemoteQueueFull() ) {
1982 return -1;
1983 } else {
1984 if ( client1->GetRemoteQueueRank() ) {
1985 if ( client2->GetRemoteQueueRank() ) {
1986 return CmpAny(
1987 client1->GetRemoteQueueRank(),
1988 client2->GetRemoteQueueRank() );
1989 } else {
1990 return -1;
1992 } else {
1993 if ( client2->GetRemoteQueueRank() ) {
1994 return 1;
1995 } else {
1996 return 0;
2002 // Sort by state
2003 case 8: {
2004 if (client1->GetDownloadState() == client2->GetDownloadState()) {
2005 return CmpAny(
2006 client1->IsRemoteQueueFull(),
2007 client2->IsRemoteQueueFull() );
2008 } else {
2009 return CmpAny(
2010 client1->GetDownloadState(),
2011 client2->GetDownloadState() );
2015 // Source of source ;)
2016 case 9:
2017 return CmpAny(client1->GetSourceFrom(), client2->GetSourceFrom());
2019 default:
2020 return 0;
2025 void CDownloadListCtrl::ClearCompleted()
2027 m_completedFiles = false;
2028 CastByID(ID_BTNCLRCOMPL, GetParent(), wxButton)->Enable(false);
2030 // Search for completed files
2031 for ( ListItems::iterator it = m_ListItems.begin(); it != m_ListItems.end(); ) {
2032 CtrlItem_Struct* item = it->second; ++it;
2034 if ( item->GetType() == FILE_TYPE ) {
2035 CPartFile* file = item->GetFile();
2037 if ( file->IsPartFile() == false ) {
2038 RemoveFile(file);
2045 void CDownloadListCtrl::ShowFilesCount( int diff )
2047 m_filecount += diff;
2049 wxString str = wxString::Format( _("Downloads (%i)"), m_filecount );
2050 wxStaticText* label = CastByName( wxT("downloadsLabel"), GetParent(), wxStaticText );
2052 label->SetLabel( str );
2053 label->GetParent()->Layout();
2059 bool CDownloadListCtrl::ShowItemInCurrentCat(
2060 const CPartFile* file, int newsel ) const
2062 return
2063 ((newsel == 0 && !thePrefs::ShowAllNotCats()) ||
2064 (newsel == 0 && thePrefs::ShowAllNotCats() && file->GetCategory() == 0)) ||
2065 (newsel > 0 && newsel == file->GetCategory());
2070 void CDownloadListCtrl::DrawFileStatusBar(
2071 const CPartFile* file, wxDC* dc, const wxRect& rect, bool bFlat ) const
2073 static CBarShader s_ChunkBar(16);
2075 COLORREF crHave = ( bFlat ? RGB( 0, 0, 0 ) : RGB( 104, 104, 104 ) );
2076 COLORREF crPending = ( bFlat ? RGB( 255, 255, 100 ) : RGB( 255, 208, 0 ) );
2077 COLORREF crProgress = ( bFlat ? RGB( 0, 150, 0 ) : RGB( 0, 224, 0 ) );
2078 COLORREF crMissing = RGB(255, 0, 0);
2080 s_ChunkBar.SetHeight(rect.height);
2081 s_ChunkBar.SetWidth(rect.width);
2082 s_ChunkBar.SetFileSize( file->GetFileSize() );
2083 s_ChunkBar.Set3dDepth( thePrefs::Get3DDepth() );
2086 if ( file->GetStatus() == PS_COMPLETE || file->GetStatus() == PS_COMPLETING ) {
2087 s_ChunkBar.Fill( crProgress );
2088 s_ChunkBar.Draw(dc, rect.x, rect.y, bFlat);
2089 return;
2093 // Part availability ( of missing parts )
2094 const CPartFile::CGapPtrList& gaplist = file->GetGapList();
2095 CPartFile::CGapPtrList::const_iterator it = gaplist.begin();
2096 uint64 lastGapEnd = 0;
2097 for (; it != gaplist.end(); ++it) {
2098 Gap_Struct* gap = *it;
2100 // Start position
2101 uint32 start = ( gap->start / PARTSIZE );
2102 // fill the Have-Part (between this gap and the last)
2103 if (gap->start) {
2104 s_ChunkBar.FillRange(lastGapEnd + 1, gap->start - 1, crHave);
2106 lastGapEnd = gap->end;
2107 // End position
2108 uint32 end = ( gap->end / PARTSIZE ) + 1;
2110 // Avoid going past the filesize. Dunno if this can happen, but the old code did check.
2111 if ( end > file->GetPartCount() ) {
2112 end = file->GetPartCount();
2115 // Place each gap, one PART at a time
2116 for ( uint64 i = start; i < end; ++i ) {
2117 COLORREF color;
2118 if ( i < file->m_SrcpartFrequency.size() && file->m_SrcpartFrequency[i]) {
2119 int blue = 210 - ( 22 * ( file->m_SrcpartFrequency[i] - 1 ) );
2120 color = RGB( 0, ( blue < 0 ? 0 : blue ), 255 );
2121 } else {
2122 color = crMissing;
2125 if ( file->IsStopped() ) {
2126 color = DarkenColour( color, 2 );
2129 uint64 gap_begin = ( i == start ? gap->start : PARTSIZE * i );
2130 uint64 gap_end = ( i == end - 1 ? gap->end : PARTSIZE * ( i + 1 ) - 1 );
2132 s_ChunkBar.FillRange( gap_begin, gap_end, color);
2135 // fill the last Have-Part (between this gap and the last)
2136 s_ChunkBar.FillRange(lastGapEnd + 1, file->GetFileSize() - 1, crHave);
2139 // Pending parts
2140 const CPartFile::CReqBlockPtrList& requestedblocks_list = file->GetRequestedBlockList();
2141 CPartFile::CReqBlockPtrList::const_iterator it2 = requestedblocks_list.begin();
2142 // adjacing pending parts must be joined to avoid bright lines between them
2143 uint64 lastStartOffset = 0;
2144 uint64 lastEndOffset = 0;
2145 COLORREF color = file->IsStopped() ? DarkenColour( crPending, 2 ) : crPending;
2146 for (; it2 != requestedblocks_list.end(); ++it2) {
2148 if ((*it2)->StartOffset > lastEndOffset + 1) {
2149 // not adjacing, draw last block
2150 s_ChunkBar.FillRange(lastStartOffset, lastEndOffset, color);
2151 lastStartOffset = (*it2)->StartOffset;
2152 lastEndOffset = (*it2)->EndOffset;
2153 } else {
2154 // adjacing, grow block
2155 lastEndOffset = (*it2)->EndOffset;
2158 s_ChunkBar.FillRange(lastStartOffset, lastEndOffset, color);
2161 // Draw the progress-bar
2162 s_ChunkBar.Draw( dc, rect.x, rect.y, bFlat );
2165 // Green progressbar width
2166 int width = (int)(( (float)rect.width / (float)file->GetFileSize() ) *
2167 file->GetCompletedSize() );
2169 if ( bFlat ) {
2170 dc->SetBrush( wxBrush( crProgress, wxSOLID ) );
2172 dc->DrawRectangle( rect.x, rect.y, width, 3 );
2173 } else {
2174 // Draw the two black lines for 3d-effect
2175 dc->SetPen( wxPen( wxColour( 0, 0, 0 ), 1, wxSOLID ) );
2176 dc->DrawLine( rect.x, rect.y + 0, rect.x + width, rect.y + 0 );
2177 dc->DrawLine( rect.x, rect.y + 2, rect.x + width, rect.y + 2 );
2179 // Draw the green line
2180 dc->SetPen( wxPen( crProgress, 1, wxSOLID ) );
2181 dc->DrawLine( rect.x, rect.y + 1, rect.x + width, rect.y + 1 );
2186 void CDownloadListCtrl::DrawSourceStatusBar(
2187 const CUpDownClient* source, wxDC* dc, const wxRect& rect, bool bFlat) const
2189 static CBarShader s_StatusBar(16);
2191 uint32 crBoth = ( bFlat ? RGB( 0, 150, 0 ) : RGB( 0, 192, 0 ) );
2192 uint32 crNeither = ( bFlat ? RGB( 224, 224, 224 ) : RGB( 240, 240, 240 ) );
2193 uint32 crClientOnly = ( bFlat ? RGB( 0, 0, 0 ) : RGB( 104, 104, 104 ) );
2194 uint32 crPending = ( bFlat ? RGB( 255, 208, 0 ) : RGB( 255, 208, 0 ) );
2195 uint32 crNextPending = ( bFlat ? RGB( 255, 255, 100 ) : RGB( 255, 255, 100 ) );
2197 CPartFile* reqfile = source->GetRequestFile();
2199 s_StatusBar.SetFileSize( reqfile->GetFileSize() );
2200 s_StatusBar.SetHeight(rect.height);
2201 s_StatusBar.SetWidth(rect.width);
2202 s_StatusBar.Set3dDepth( thePrefs::Get3DDepth() );
2204 // Barry - was only showing one part from client, even when reserved bits from 2 parts
2205 wxString gettingParts = source->ShowDownloadingParts();
2207 const BitVector& partStatus = source->GetPartStatus();
2209 uint64 uEnd = 0;
2210 for ( uint64 i = 0; i < partStatus.size(); i++ ) {
2211 uint64 uStart = PARTSIZE * i;
2212 uEnd = wxMin(reqfile->GetFileSize(), uStart + PARTSIZE) - 1;
2214 uint32 color = 0;
2215 if (!partStatus[i]) {
2216 color = crNeither;
2217 } else if ( reqfile->IsComplete(uStart, uEnd)) {
2218 color = crBoth;
2219 } else if ( source->GetDownloadState() == DS_DOWNLOADING &&
2220 source->GetLastBlockOffset() <= uEnd &&
2221 source->GetLastBlockOffset() >= uStart) {
2222 color = crPending;
2223 } else if (gettingParts.GetChar((uint16)i) == 'Y') {
2224 color = crNextPending;
2225 } else {
2226 color = crClientOnly;
2229 if ( source->GetRequestFile()->IsStopped() ) {
2230 color = DarkenColour( color, 2 );
2233 s_StatusBar.FillRange(uStart, uEnd, color);
2235 // fill the rest (if partStatus is empty)
2236 s_StatusBar.FillRange(uEnd + 1, reqfile->GetFileSize() - 1, crNeither);
2238 s_StatusBar.Draw(dc, rect.x, rect.y, bFlat);
2241 #ifdef __WXMSW__
2242 # define QUOTE wxT("\"")
2243 #else
2244 # define QUOTE wxT("\'")
2245 #endif
2247 void CDownloadListCtrl::PreviewFile(CPartFile* file)
2249 wxString command;
2250 // If no player set in preferences, use mplayer.
2251 // And please, do a warning also :P
2252 if (thePrefs::GetVideoPlayer().IsEmpty()) {
2253 wxMessageBox(_(
2254 "Please set your preferred video player on preferences.\nMeanwhile, aMule will attempt to use mplayer and you will get this warning on every preview"),
2255 _("File preview"), wxOK, this);
2256 // Since newer versions for some reason mplayer does not automatically
2257 // select video output device and needs a parameter, go figure...
2258 command = wxT("xterm -T \"aMule Preview\" -iconic -e mplayer ") QUOTE wxT("$file") QUOTE;
2259 } else {
2260 command = thePrefs::GetVideoPlayer();
2263 // Check if we are (pre)viewing a completed file or not
2264 if (file->GetStatus() != PS_COMPLETE) {
2265 // Remove the .met and see if out video player specifiation uses the magic string
2266 wxString fileWithoutMet = thePrefs::GetTempDir().JoinPaths(
2267 file->GetPartMetFileName().RemoveExt()).GetRaw();
2268 if (!command.Replace(wxT("$file"), fileWithoutMet)) {
2269 // No magic string, so we just append the filename to the player command
2270 // Need to use quotes in case filename contains spaces
2271 command << wxT(" ") << QUOTE << fileWithoutMet << QUOTE;
2273 } else {
2274 // This is a complete file
2275 // FIXME: This is probably not going to work if the filenames are mangled ...
2276 wxString rawFileName = file->GetFullName().GetRaw();
2277 if (!command.Replace(wxT("$file"), rawFileName)) {
2278 // No magic string, so we just append the filename to the player command
2279 // Need to use quotes in case filename contains spaces
2280 command << wxT(" ") << QUOTE << rawFileName << QUOTE;
2284 // We can't use wxShell here, it blocks the app
2285 CTerminationProcess *p = new CTerminationProcess(command);
2286 int ret = wxExecute(command, wxEXEC_ASYNC, p);
2287 bool ok = ret > 0;
2288 if (!ok) {
2289 delete p;
2290 AddLogLineM( true,
2291 CFormat( _("ERROR: Failed to execute external media-player! Command: `%s'") ) %
2292 command );
2295 // File_checked_for_headers