Fix "Clear completed" button enabled only on category change, not on startup
[amule.git] / src / DownloadListCtrl.cpp
blobc1b54ca9ea5afe7e64cfd751fb7b6adcfc48af9c
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 "DownloadListCtrl.h" // Interface declarations
28 #include <protocol/ed2k/ClientSoftware.h>
29 #include <common/MenuIDs.h>
31 #include <common/Format.h> // Needed for CFormat
32 #include "amule.h" // Needed for theApp
33 #include "amuleDlg.h" // Needed for CamuleDlg
34 #include "BarShader.h" // Needed for CBarShader
35 #include "CommentDialogLst.h" // Needed for CCommentDialogLst
36 #include "DataToText.h" // Needed for PriorityToStr
37 #include "DownloadQueue.h"
38 #include "FileDetailDialog.h" // Needed for CFileDetailDialog
39 #include "GuiEvents.h" // Needed for CoreNotify_*
40 #include "Logger.h"
41 #include "muuli_wdr.h" // Needed for ID_DLOADLIST
42 #include "PartFile.h" // Needed for CPartFile
43 #include "Preferences.h"
44 #include "SharedFileList.h" // Needed for CSharedFileList
45 #include "TerminationProcess.h" // Needed for CTerminationProcess
46 #include "updownclient.h" // Needed for CUpDownClient
47 #include "TransferWnd.h"
48 #include "SourceListCtrl.h"
50 class CPartFile;
53 struct FileCtrlItem_Struct
55 FileCtrlItem_Struct()
56 : dwUpdated(0),
57 status(NULL),
58 m_fileValue(NULL)
59 { }
61 ~FileCtrlItem_Struct() {
62 delete status;
65 CPartFile* GetFile() const {
66 return m_fileValue;
69 void SetContents(CPartFile* file) {
70 m_fileValue = file;
73 uint32 dwUpdated;
74 wxBitmap* status;
76 private:
77 CPartFile* m_fileValue;
80 #define m_ImageList theApp->amuledlg->m_imagelist
82 enum ColumnEnum {
83 ColumnPart = 0,
84 ColumnFileName,
85 ColumnSize,
86 ColumnTransferred,
87 ColumnCompleted,
88 ColumnSpeed,
89 ColumnProgress,
90 ColumnSources,
91 ColumnPriority,
92 ColumnStatus,
93 ColumnTimeRemaining,
94 ColumnLastSeenComplete,
95 ColumnLastReception,
96 ColumnNumberOfColumns
99 BEGIN_EVENT_TABLE(CDownloadListCtrl, CMuleListCtrl)
100 EVT_LIST_ITEM_ACTIVATED(ID_DLOADLIST, CDownloadListCtrl::OnItemActivated)
101 EVT_LIST_ITEM_RIGHT_CLICK(ID_DLOADLIST, CDownloadListCtrl::OnMouseRightClick)
102 EVT_LIST_ITEM_MIDDLE_CLICK(ID_DLOADLIST, CDownloadListCtrl::OnMouseMiddleClick)
103 EVT_LIST_ITEM_SELECTED(ID_DLOADLIST, CDownloadListCtrl::OnItemSelectionChanged)
104 EVT_LIST_ITEM_DESELECTED(ID_DLOADLIST, CDownloadListCtrl::OnItemSelectionChanged)
106 EVT_CHAR( CDownloadListCtrl::OnKeyPressed )
108 EVT_MENU( MP_CANCEL, CDownloadListCtrl::OnCancelFile )
110 EVT_MENU( MP_PAUSE, CDownloadListCtrl::OnSetStatus )
111 EVT_MENU( MP_STOP, CDownloadListCtrl::OnSetStatus )
112 EVT_MENU( MP_RESUME, CDownloadListCtrl::OnSetStatus )
114 EVT_MENU( MP_PRIOLOW, CDownloadListCtrl::OnSetPriority )
115 EVT_MENU( MP_PRIONORMAL, CDownloadListCtrl::OnSetPriority )
116 EVT_MENU( MP_PRIOHIGH, CDownloadListCtrl::OnSetPriority )
117 EVT_MENU( MP_PRIOAUTO, CDownloadListCtrl::OnSetPriority )
119 EVT_MENU( MP_SWAP_A4AF_TO_THIS, CDownloadListCtrl::OnSwapSources )
120 EVT_MENU( MP_SWAP_A4AF_TO_THIS_AUTO, CDownloadListCtrl::OnSwapSources )
121 EVT_MENU( MP_SWAP_A4AF_TO_ANY_OTHER, CDownloadListCtrl::OnSwapSources )
123 EVT_MENU_RANGE( MP_ASSIGNCAT, MP_ASSIGNCAT + 99, CDownloadListCtrl::OnSetCategory )
125 EVT_MENU( MP_CLEARCOMPLETED, CDownloadListCtrl::OnClearCompleted )
127 EVT_MENU( MP_GETMAGNETLINK, CDownloadListCtrl::OnGetLink )
128 EVT_MENU( MP_GETED2KLINK, CDownloadListCtrl::OnGetLink )
130 EVT_MENU( MP_METINFO, CDownloadListCtrl::OnViewFileInfo )
131 EVT_MENU( MP_VIEW, CDownloadListCtrl::OnPreviewFile )
132 EVT_MENU( MP_VIEWFILECOMMENTS, CDownloadListCtrl::OnViewFileComments )
134 EVT_MENU( MP_WS, CDownloadListCtrl::OnGetFeedback )
136 END_EVENT_TABLE()
138 //! This listtype is used when gathering the selected items.
139 typedef std::list<FileCtrlItem_Struct*> ItemList;
141 CDownloadListCtrl::CDownloadListCtrl(
142 wxWindow *parent, wxWindowID winid, const wxPoint& pos, const wxSize& size,
143 long style, const wxValidator& validator, const wxString& name )
145 CMuleListCtrl( parent, winid, pos, size, style | wxLC_OWNERDRAW, validator, name )
147 // Setting the sorter function.
148 SetSortFunc( SortProc );
150 // Set the table-name (for loading and saving preferences).
151 SetTableName( wxT("Download") );
153 m_menu = NULL;
155 m_hilightBrush = CMuleColour(wxSYS_COLOUR_HIGHLIGHT).Blend(125).GetBrush();
157 m_hilightUnfocusBrush = CMuleColour(wxSYS_COLOUR_BTNSHADOW).Blend(125).GetBrush();
159 InsertColumn( ColumnPart, _("Part"), wxLIST_FORMAT_LEFT, 30, wxT("a") );
160 InsertColumn( ColumnFileName, _("File Name"), wxLIST_FORMAT_LEFT, 260, wxT("N") );
161 InsertColumn( ColumnSize, _("Size"), wxLIST_FORMAT_LEFT, 60, wxT("Z") );
162 InsertColumn( ColumnTransferred, _("Transferred"), wxLIST_FORMAT_LEFT, 65, wxT("T") );
163 InsertColumn( ColumnCompleted, _("Completed"), wxLIST_FORMAT_LEFT, 65, wxT("C") );
164 InsertColumn( ColumnSpeed, _("Speed"), wxLIST_FORMAT_LEFT, 65, wxT("S") );
165 InsertColumn( ColumnProgress, _("Progress"), wxLIST_FORMAT_LEFT, 170, wxT("P") );
166 InsertColumn( ColumnSources, _("Sources"), wxLIST_FORMAT_LEFT, 50, wxT("u") );
167 InsertColumn( ColumnPriority, _("Priority"), wxLIST_FORMAT_LEFT, 55, wxT("p") );
168 InsertColumn( ColumnStatus, _("Status"), wxLIST_FORMAT_LEFT, 70, wxT("s") );
169 InsertColumn( ColumnTimeRemaining, _("Time Remaining"), wxLIST_FORMAT_LEFT, 110, wxT("r") );
170 InsertColumn( ColumnLastSeenComplete, _("Last Seen Complete"), wxLIST_FORMAT_LEFT, 220, wxT("c") );
171 InsertColumn( ColumnLastReception, _("Last Reception"), wxLIST_FORMAT_LEFT, 220, wxT("R") );
173 m_category = 0;
174 m_filecount = 0;
175 LoadSettings();
177 //m_ready = true;
180 // This is the order the columns had before extendable list-control settings save/load code was introduced.
181 // Don't touch when inserting new columns!
182 wxString CDownloadListCtrl::GetOldColumnOrder() const
184 return wxT("N,Z,T,C,S,P,u,p,s,r,c,R");
187 CDownloadListCtrl::~CDownloadListCtrl()
189 while ( !m_ListItems.empty() ) {
190 delete m_ListItems.begin()->second;
191 m_ListItems.erase( m_ListItems.begin() );
195 void CDownloadListCtrl::AddFile( CPartFile* file )
197 wxASSERT( file );
199 // Avoid duplicate entries of files
200 if ( m_ListItems.find( file ) == m_ListItems.end() ) {
201 FileCtrlItem_Struct* newitem = new FileCtrlItem_Struct;
202 newitem->SetContents(file);
204 m_ListItems.insert( ListItemsPair( file, newitem ) );
206 // Check if the new file is visible in the current category
207 if ( file->CheckShowItemInGivenCat( m_category ) ) {
208 ShowFile( file, true );
209 if (file->IsCompleted()) {
210 CastByID(ID_BTNCLRCOMPL, GetParent(), wxButton)->Enable(true);
212 SortList();
217 void CDownloadListCtrl::RemoveFile( CPartFile* file )
219 wxASSERT( file );
221 // Ensure that any list-entries are removed
222 ShowFile( file, false );
224 // Find the assosiated list-item
225 ListItems::iterator it = m_ListItems.find( file );
227 if ( it != m_ListItems.end() ) {
228 delete it->second;
230 m_ListItems.erase( it );
235 void CDownloadListCtrl::UpdateItem(const void* toupdate)
237 // Retrieve all entries matching the source
238 ListIteratorPair rangeIt = m_ListItems.equal_range( toupdate );
240 // Visible lines, default to all because not all platforms
241 // support the GetVisibleLines function
242 long first = 0, last = GetItemCount();
244 #ifndef __WXMSW__
245 // Get visible lines if we need them
246 if ( rangeIt.first != rangeIt.second ) {
247 GetVisibleLines( &first, &last );
249 #endif
251 for ( ListItems::iterator it = rangeIt.first; it != rangeIt.second; ++it ) {
252 FileCtrlItem_Struct* item = it->second;
254 long index = FindItem( -1, reinterpret_cast<wxUIntPtr>(item) );
256 // Determine if the file should be shown in the current category
258 CPartFile* file = item->GetFile();
260 bool show = file->CheckShowItemInGivenCat( m_category );
262 if ( index > -1 ) {
263 if ( show ) {
264 item->dwUpdated = 0;
266 // Only update visible lines
267 if ( index >= first && index <= last) {
268 RefreshItem( index );
270 } else {
271 // Item should no longer be shown in
272 // the current category
273 ShowFile( file, false );
275 } else if ( show ) {
276 // Item has been hidden but new status means
277 // that it should it should be shown in the
278 // current category
279 ShowFile( file, true );
282 if (file->IsCompleted() && show) {
283 CastByID(ID_BTNCLRCOMPL, GetParent(), wxButton)->Enable(true);
289 void CDownloadListCtrl::ShowFile( CPartFile* file, bool show )
291 wxASSERT( file );
293 ListItems::iterator it = m_ListItems.find( file );
295 if ( it != m_ListItems.end() ) {
296 FileCtrlItem_Struct* item = it->second;
298 if ( show ) {
299 // Check if the file is already being displayed
300 long index = FindItem( -1, reinterpret_cast<wxUIntPtr>(item) );
301 if ( index == -1 ) {
302 long newitem = InsertItem( GetItemCount(), wxEmptyString );
304 SetItemPtrData( newitem, reinterpret_cast<wxUIntPtr>(item) );
306 wxListItem myitem;
307 myitem.m_itemId = newitem;
308 myitem.SetBackgroundColour( GetBackgroundColour() );
310 SetItem(myitem);
312 RefreshItem( newitem );
314 ShowFilesCount( 1 );
316 } else {
317 // Try to find the file and remove it
318 long index = FindItem( -1, reinterpret_cast<wxUIntPtr>(item) );
319 if ( index > -1 ) {
320 DeleteItem( index );
321 ShowFilesCount( -1 );
327 void CDownloadListCtrl::ChangeCategory( int newCategory )
329 Freeze();
331 bool hasCompletedDownloads = false;
333 // remove all displayed files with a different cat and show the correct ones
334 for (ListItems::const_iterator it = m_ListItems.begin(); it != m_ListItems.end(); it++) {
336 CPartFile* file = it->second->GetFile();
338 bool curVisibility = file->CheckShowItemInGivenCat( m_category );
339 bool newVisibility = file->CheckShowItemInGivenCat( newCategory );
341 if (newVisibility && file->IsCompleted()) {
342 hasCompletedDownloads = true;
345 // Check if the visibility of the file has changed. However, if the
346 // current category is the default (0) category, then we can't use
347 // curVisiblity to see if the visibility has changed but instead
348 // have to let ShowFile() check if the file is or isn't on the list.
349 if ( curVisibility != newVisibility || !newCategory ) {
350 ShowFile( file, newVisibility );
354 CastByID(ID_BTNCLRCOMPL, GetParent(), wxButton)->Enable(hasCompletedDownloads);
356 Thaw();
358 m_category = newCategory;
362 uint8 CDownloadListCtrl::GetCategory() const
364 return m_category;
369 * Helper-function: This function is used to gather selected items.
371 * @param list A pointer to the list to gather items from.
372 * @return A list containing the selected items.
374 ItemList GetSelectedItems( CDownloadListCtrl* list)
376 ItemList results;
378 long index = list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
380 while ( index > -1 ) {
381 FileCtrlItem_Struct* item = (FileCtrlItem_Struct*)list->GetItemData( index );
382 results.push_back( item );
384 index = list->GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
387 return results;
391 void CDownloadListCtrl::OnCancelFile(wxCommandEvent& WXUNUSED(event))
393 ItemList files = ::GetSelectedItems(this);
394 for (ItemList::iterator it = files.begin(); it != files.end(); ) {
395 ItemList::iterator it1 = it++;
396 CPartFile* file = (*it1)->GetFile();
397 if (file) {
398 switch (file->GetStatus()) {
399 case PS_WAITINGFORHASH:
400 case PS_HASHING:
401 case PS_COMPLETING:
402 case PS_COMPLETE:
403 files.erase(it1);
404 break;
408 if (files.size()) {
409 wxString question;
410 if (files.size() == 1) {
411 question = _("Are you sure that you wish to delete the selected file?");
412 } else {
413 question = _("Are you sure that you wish to delete the selected files?");
415 if (wxMessageBox( question, _("Cancel"), wxICON_QUESTION | wxYES_NO, this) == wxYES) {
416 for (ItemList::iterator it = files.begin(); it != files.end(); ++it) {
417 CPartFile* file = (*it)->GetFile();
418 if (file) {
419 CoreNotify_PartFile_Delete(file);
427 void CDownloadListCtrl::OnSetPriority( wxCommandEvent& event )
429 int priority = 0;
430 switch ( event.GetId() ) {
431 case MP_PRIOLOW: priority = PR_LOW; break;
432 case MP_PRIONORMAL: priority = PR_NORMAL; break;
433 case MP_PRIOHIGH: priority = PR_HIGH; break;
434 case MP_PRIOAUTO: priority = PR_AUTO; break;
435 default:
436 wxASSERT( false );
439 ItemList files = ::GetSelectedItems( this );
441 for ( ItemList::iterator it = files.begin(); it != files.end(); ++it ) {
442 CPartFile* file = (*it)->GetFile();
444 if ( priority == PR_AUTO ) {
445 CoreNotify_PartFile_PrioAuto( file, true );
446 } else {
447 CoreNotify_PartFile_PrioAuto( file, false );
449 CoreNotify_PartFile_PrioSet( file, priority, true );
455 void CDownloadListCtrl::OnSwapSources( wxCommandEvent& event )
457 ItemList files = ::GetSelectedItems( this );
459 for ( ItemList::iterator it = files.begin(); it != files.end(); ++it ) {
460 CPartFile* file = (*it)->GetFile();
462 switch ( event.GetId() ) {
463 case MP_SWAP_A4AF_TO_THIS:
464 CoreNotify_PartFile_Swap_A4AF( file );
465 break;
467 case MP_SWAP_A4AF_TO_THIS_AUTO:
468 CoreNotify_PartFile_Swap_A4AF_Auto( file );
469 break;
471 case MP_SWAP_A4AF_TO_ANY_OTHER:
472 CoreNotify_PartFile_Swap_A4AF_Others( file );
473 break;
479 void CDownloadListCtrl::OnSetCategory( wxCommandEvent& event )
481 ItemList files = ::GetSelectedItems( this );
483 for ( ItemList::iterator it = files.begin(); it != files.end(); ++it ) {
484 CoreNotify_PartFile_SetCat( (*it)->GetFile(), event.GetId() - MP_ASSIGNCAT );
485 ShowFile((*it)->GetFile(), false);
487 wxListEvent ev;
488 OnItemSelectionChanged(ev); // clear clients that may have been shown
490 ChangeCategory( m_category ); // This only updates the visibility of the clear completed button
494 void CDownloadListCtrl::OnSetStatus( wxCommandEvent& event )
496 ItemList files = ::GetSelectedItems( this );
498 for ( ItemList::iterator it = files.begin(); it != files.end(); ++it ) {
499 CPartFile* file = (*it)->GetFile();
501 switch ( event.GetId() ) {
502 case MP_PAUSE:
503 CoreNotify_PartFile_Pause( file );
504 break;
506 case MP_RESUME:
507 CoreNotify_PartFile_Resume( file );
508 break;
510 case MP_STOP:
511 CoreNotify_PartFile_Stop( file );
512 break;
518 void CDownloadListCtrl::OnClearCompleted( wxCommandEvent& WXUNUSED(event) )
520 ClearCompleted();
524 void CDownloadListCtrl::OnGetLink(wxCommandEvent& event)
526 ItemList files = ::GetSelectedItems( this );
528 wxString URIs;
530 for ( ItemList::iterator it = files.begin(); it != files.end(); ++it ) {
531 CPartFile* file = (*it)->GetFile();
533 if ( event.GetId() == MP_GETED2KLINK ) {
534 URIs += theApp->CreateED2kLink( file ) + wxT("\n");
535 } else {
536 URIs += theApp->CreateMagnetLink( file ) + wxT("\n");
540 if ( !URIs.IsEmpty() ) {
541 theApp->CopyTextToClipboard( URIs.BeforeLast(wxT('\n')) );
546 void CDownloadListCtrl::OnGetFeedback(wxCommandEvent& WXUNUSED(event))
548 wxString feed;
549 ItemList files = ::GetSelectedItems( this );
551 for (ItemList::iterator it = files.begin(); it != files.end(); ++it) {
552 if (feed.IsEmpty()) {
553 feed = CFormat(_("Feedback from: %s (%s)\n\n")) % thePrefs::GetUserNick() % theApp->GetFullMuleVersion();
554 } else {
555 feed += wxT("\n");
557 feed += (*it)->GetFile()->GetFeedback();
560 if (!feed.IsEmpty()) {
561 theApp->CopyTextToClipboard(feed);
565 void CDownloadListCtrl::OnViewFileInfo( wxCommandEvent& WXUNUSED(event) )
567 ItemList files = ::GetSelectedItems( this );
569 if ( files.size() == 1 ) {
570 CFileDetailDialog dialog( this, files.front()->GetFile() );
571 dialog.ShowModal();
576 void CDownloadListCtrl::OnViewFileComments( wxCommandEvent& WXUNUSED(event) )
578 ItemList files = ::GetSelectedItems( this );
580 if ( files.size() == 1 ) {
581 CCommentDialogLst dialog( this, files.front()->GetFile() );
582 dialog.ShowModal();
586 void CDownloadListCtrl::OnPreviewFile( wxCommandEvent& WXUNUSED(event) )
588 ItemList files = ::GetSelectedItems( this );
590 if ( files.size() == 1 ) {
591 PreviewFile(files.front()->GetFile());
595 void CDownloadListCtrl::OnItemActivated( wxListEvent& evt )
597 CPartFile* file = ((FileCtrlItem_Struct*)GetItemData( evt.GetIndex()))->GetFile();
599 if ((!file->IsPartFile() || file->IsCompleted()) && file->PreviewAvailable()) {
600 PreviewFile( file );
604 void CDownloadListCtrl::OnItemSelectionChanged( wxListEvent& )
606 if (!IsSorting()) {
607 CKnownFileVector filesVector;
608 filesVector.reserve(GetSelectedItemCount());
610 long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
612 while ( index > -1 ) {
613 CPartFile* file = ((FileCtrlItem_Struct*)GetItemData( index ))->GetFile();
614 if (file->IsPartFile()) {
615 filesVector.push_back(file);
617 index = GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
620 std::sort(filesVector.begin(), filesVector.end());
621 theApp->amuledlg->m_transferwnd->clientlistctrl->ShowSources(filesVector);
625 void CDownloadListCtrl::OnMouseRightClick(wxListEvent& evt)
627 long index = CheckSelection(evt);
628 if (index < 0) {
629 return;
632 delete m_menu;
633 m_menu = NULL;
635 FileCtrlItem_Struct* item = (FileCtrlItem_Struct*)GetItemData( index );
636 m_menu = new wxMenu( _("Downloads") );
638 wxMenu* priomenu = new wxMenu();
639 priomenu->AppendCheckItem(MP_PRIOLOW, _("Low"));
640 priomenu->AppendCheckItem(MP_PRIONORMAL, _("Normal"));
641 priomenu->AppendCheckItem(MP_PRIOHIGH, _("High"));
642 priomenu->AppendCheckItem(MP_PRIOAUTO, _("Auto"));
644 m_menu->Append(MP_MENU_PRIO, _("Priority"), priomenu);
645 m_menu->Append(MP_CANCEL, _("Cancel"));
646 m_menu->Append(MP_STOP, _("&Stop"));
647 m_menu->Append(MP_PAUSE, _("&Pause"));
648 m_menu->Append(MP_RESUME, _("&Resume"));
649 m_menu->Append(MP_CLEARCOMPLETED, _("C&lear completed"));
650 //-----------------------------------------------------
651 m_menu->AppendSeparator();
652 //-----------------------------------------------------
653 wxMenu* extendedmenu = new wxMenu();
654 extendedmenu->Append(MP_SWAP_A4AF_TO_THIS,
655 _("Swap every A4AF to this file now"));
656 extendedmenu->AppendCheckItem(MP_SWAP_A4AF_TO_THIS_AUTO,
657 _("Swap every A4AF to this file (Auto)"));
658 //-----------------------------------------------------
659 extendedmenu->AppendSeparator();
660 //-----------------------------------------------------
661 extendedmenu->Append(MP_SWAP_A4AF_TO_ANY_OTHER,
662 _("Swap every A4AF to any other file now"));
663 //-----------------------------------------------------
664 m_menu->Append(MP_MENU_EXTD,
665 _("Extended Options"), extendedmenu);
666 //-----------------------------------------------------
667 m_menu->AppendSeparator();
668 //-----------------------------------------------------
670 m_menu->Append(MP_VIEW, _("Preview"));
671 m_menu->Append(MP_METINFO, _("Show file &details"));
672 m_menu->Append(MP_VIEWFILECOMMENTS, _("Show all comments"));
673 //-----------------------------------------------------
674 m_menu->AppendSeparator();
675 //-----------------------------------------------------
676 m_menu->Append(MP_GETMAGNETLINK,
677 _("Copy magnet URI to clipboard"));
678 m_menu->Append(MP_GETED2KLINK,
679 _("Copy eD2k &link to clipboard"));
680 m_menu->Append(MP_WS,
681 _("Copy feedback to clipboard"));
682 //-----------------------------------------------------
683 m_menu->AppendSeparator();
684 //-----------------------------------------------------
685 // Add dynamic entries
686 wxMenu *cats = new wxMenu(_("Category"));
687 if (theApp->glob_prefs->GetCatCount() > 1) {
688 for (uint32 i = 0; i < theApp->glob_prefs->GetCatCount(); i++) {
689 if ( i == 0 ) {
690 cats->Append( MP_ASSIGNCAT, _("unassign") );
691 } else {
692 cats->Append( MP_ASSIGNCAT + i,
693 theApp->glob_prefs->GetCategory(i)->title );
697 m_menu->Append(MP_MENU_CATS, _("Assign to category"), cats);
698 m_menu->Enable(MP_MENU_CATS, (theApp->glob_prefs->GetCatCount() > 1) );
700 CPartFile* file = item->GetFile();
701 // then set state
702 bool canStop;
703 bool canPause;
704 bool canCancel;
705 bool fileResumable;
706 if (file->GetStatus(true) != PS_ALLOCATING) {
707 const uint8_t fileStatus = file->GetStatus();
708 canStop =
709 (fileStatus != PS_ERROR) &&
710 (fileStatus != PS_COMPLETE) &&
711 (file->IsStopped() != true);
712 canPause = (file->GetStatus() != PS_PAUSED) && canStop;
713 fileResumable =
714 (fileStatus == PS_PAUSED) ||
715 (fileStatus == PS_ERROR) ||
716 (fileStatus == PS_INSUFFICIENT);
717 canCancel = fileStatus != PS_COMPLETE;
718 } else {
719 canStop = canPause = canCancel = fileResumable = false;
722 m_menu->Enable( MP_CANCEL, canCancel );
723 m_menu->Enable( MP_PAUSE, canPause );
724 m_menu->Enable( MP_STOP, canStop );
725 m_menu->Enable( MP_RESUME, fileResumable );
726 m_menu->Enable( MP_CLEARCOMPLETED, CastByID(ID_BTNCLRCOMPL, GetParent(), wxButton)->IsEnabled() );
728 wxString view;
729 if (file->IsPartFile() && !file->IsCompleted()) {
730 view = CFormat(wxT("%s [%s]")) % _("Preview")
731 % file->GetPartMetFileName().RemoveExt();
732 } else if ( file->IsCompleted() ) {
733 view = _("&Open the file");
735 m_menu->SetLabel(MP_VIEW, view);
736 m_menu->Enable(MP_VIEW, file->PreviewAvailable());
738 FileRatingList ratingList;
739 item->GetFile()->GetRatingAndComments(ratingList);
740 m_menu->Enable(MP_VIEWFILECOMMENTS, !ratingList.empty());
742 m_menu->Check( MP_SWAP_A4AF_TO_THIS_AUTO, file->IsA4AFAuto() );
744 int priority = file->IsAutoDownPriority() ? PR_AUTO : file->GetDownPriority();
746 priomenu->Check( MP_PRIOHIGH, priority == PR_HIGH );
747 priomenu->Check( MP_PRIONORMAL, priority == PR_NORMAL );
748 priomenu->Check( MP_PRIOLOW, priority == PR_LOW );
749 priomenu->Check( MP_PRIOAUTO, priority == PR_AUTO );
751 m_menu->Enable( MP_MENU_EXTD, canPause );
753 bool autosort = thePrefs::AutoSortDownload(false);
754 PopupMenu(m_menu, evt.GetPoint());
755 thePrefs::AutoSortDownload(autosort);
757 delete m_menu;
758 m_menu = NULL;
762 void CDownloadListCtrl::OnMouseMiddleClick(wxListEvent& evt)
764 // Check if clicked item is selected. If not, unselect all and select it.
765 long index = CheckSelection(evt);
766 if ( index < 0 ) {
767 return;
770 bool autosort = thePrefs::AutoSortDownload(false);
771 CFileDetailDialog(this, ((FileCtrlItem_Struct*)GetItemData( index ))->GetFile()).ShowModal();
772 thePrefs::AutoSortDownload(autosort);
776 void CDownloadListCtrl::OnKeyPressed( wxKeyEvent& event )
778 // Check if delete was pressed
779 switch (event.GetKeyCode()) {
780 case WXK_NUMPAD_DELETE:
781 case WXK_DELETE: {
782 wxCommandEvent evt;
783 OnCancelFile( evt );
784 break;
786 case WXK_F2: {
787 ItemList files = ::GetSelectedItems( this );
788 if (files.size() == 1) {
789 CPartFile* file = files.front()->GetFile();
791 // Currently renaming of completed files causes problem with kad
792 if (file->IsPartFile()) {
793 wxString strNewName = ::wxGetTextFromUser(
794 _("Enter new name for this file:"),
795 _("File rename"), file->GetFileName().GetPrintable());
797 CPath newName = CPath(strNewName);
798 if (newName.IsOk() && (newName != file->GetFileName())) {
799 theApp->sharedfiles->RenameFile(file, newName);
803 break;
805 default:
806 event.Skip();
811 void CDownloadListCtrl::OnDrawItem(
812 int item, wxDC* dc, const wxRect& rect, const wxRect& rectHL, bool highlighted)
814 // Don't do any drawing if there's nobody to see it.
815 if ( !theApp->amuledlg->IsDialogVisible( CamuleDlg::DT_TRANSFER_WND ) ) {
816 return;
819 FileCtrlItem_Struct* content = (FileCtrlItem_Struct *)GetItemData(item);
821 // Define text-color and background
822 // and the border of the drawn area
823 if (highlighted) {
824 CMuleColour colour;
825 if (GetFocus()) {
826 dc->SetBackground(m_hilightBrush);
827 colour = m_hilightBrush.GetColour();
828 } else {
829 dc->SetBackground(m_hilightUnfocusBrush);
830 colour = m_hilightUnfocusBrush.GetColour();
832 dc->SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
833 dc->SetPen( colour.Blend(65).GetPen() );
834 } else {
835 dc->SetBackground(*(wxTheBrushList->FindOrCreateBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX), wxSOLID)));
836 dc->SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
837 dc->SetPen(*wxTRANSPARENT_PEN);
839 dc->SetBrush( dc->GetBackground() );
841 dc->DrawRectangle( rectHL.x, rectHL.y, rectHL.width, rectHL.height );
843 dc->SetPen(*wxTRANSPARENT_PEN);
845 if (!highlighted || !GetFocus() ) {
846 // If we have category, override textforeground with what category tells us.
847 CPartFile *file = content->GetFile();
848 if ( file->GetCategory() ) {
849 dc->SetTextForeground(CMuleColour(theApp->glob_prefs->GetCatColor(file->GetCategory())) );
853 // Various constant values we use
854 const int iTextOffset = ( rect.GetHeight() - dc->GetCharHeight() ) / 2;
855 const int iOffset = 4;
857 wxRect cur_rec( iOffset, rect.y, 0, rect.height );
858 for (int i = 0; i < GetColumnCount(); i++) {
859 wxListItem listitem;
860 GetColumn(i, listitem);
862 if (listitem.GetWidth() > 2*iOffset) {
863 cur_rec.width = listitem.GetWidth() - 2*iOffset;
865 // Make a copy of the current rectangle so we can apply specific tweaks
866 wxRect target_rec = cur_rec;
867 if ( i == ColumnProgress ) {
868 // Double the offset to make room for the cirle-marker
869 target_rec.x += iOffset;
870 target_rec.width -= iOffset;
871 } else {
872 // will ensure that text is about in the middle ;)
873 target_rec.y += iTextOffset;
876 // Draw the item
877 DrawFileItem(dc, i, target_rec, content);
880 // Increment to the next column
881 cur_rec.x += listitem.GetWidth();
886 void CDownloadListCtrl::DrawFileItem( wxDC* dc, int nColumn, const wxRect& rect, FileCtrlItem_Struct* item ) const
888 wxDCClipper clipper( *dc, rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight() );
890 const CPartFile* file = item->GetFile();
892 // Used to contain the contenst of cells that dont need any fancy drawing, just text.
893 wxString text;
895 switch (nColumn) {
896 // Part Number
897 case ColumnPart: {
898 if (file->IsPartFile() && !file->IsCompleted()) {
899 text = file->GetPartMetFileName().RemoveAllExt().GetPrintable();
901 break;
903 // Filename
904 case ColumnFileName: {
905 wxString filename = file->GetFileName().GetPrintable();
907 if (file->HasRating() || file->HasComment()) {
908 int image = Client_CommentOnly_Smiley;
909 if (file->HasRating()) {
910 image = Client_InvalidRating_Smiley + file->UserRating() - 1;
913 wxASSERT(image >= Client_InvalidRating_Smiley);
914 wxASSERT(image <= Client_CommentOnly_Smiley);
916 int imgWidth = 16;
918 // it's already centered by OnDrawItem() ...
919 m_ImageList.Draw(image, *dc, rect.GetX(), rect.GetY() - 1,
920 wxIMAGELIST_DRAW_TRANSPARENT);
921 dc->DrawText(filename, rect.GetX() + imgWidth + 4, rect.GetY());
922 } else {
923 dc->DrawText(filename, rect.GetX(), rect.GetY());
925 break;
928 // Filesize
929 case ColumnSize:
930 text = CastItoXBytes( file->GetFileSize() );
931 break;
933 // Transferred
934 case ColumnTransferred:
935 text = CastItoXBytes( file->GetTransferred() );
936 break;
938 // Completed
939 case ColumnCompleted:
940 text = CastItoXBytes( file->GetCompletedSize() );
941 break;
943 // Speed
944 case ColumnSpeed:
945 if ( file->GetTransferingSrcCount() ) {
946 text = CFormat(_("%.1f kB/s")) % file->GetKBpsDown();
948 break;
950 // Progress
951 case ColumnProgress:{
952 if (thePrefs::ShowProgBar()) {
953 int iWidth = rect.GetWidth() - 2;
954 int iHeight = rect.GetHeight() - 2;
956 // DO NOT DRAW IT ALL THE TIME
957 uint32 dwTicks = GetTickCount();
959 wxMemoryDC cdcStatus;
961 if ( item->dwUpdated < dwTicks || !item->status || iWidth != item->status->GetWidth() ) {
962 if ( item->status == NULL) {
963 item->status = new wxBitmap(iWidth, iHeight);
964 } else if ( item->status->GetWidth() != iWidth ) {
965 // Only recreate if the size has changed
966 item->status->Create(iWidth, iHeight);
969 cdcStatus.SelectObject( *item->status );
971 if ( thePrefs::UseFlatBar() ) {
972 DrawFileStatusBar( file, &cdcStatus,
973 wxRect(0, 0, iWidth, iHeight), true);
974 } else {
975 DrawFileStatusBar( file, &cdcStatus,
976 wxRect(1, 1, iWidth - 2, iHeight - 2), false);
978 // Draw black border
979 cdcStatus.SetPen( *wxBLACK_PEN );
980 cdcStatus.SetBrush( *wxTRANSPARENT_BRUSH );
981 cdcStatus.DrawRectangle( 0, 0, iWidth, iHeight );
984 item->dwUpdated = dwTicks + 5000; // Plus five seconds
985 } else {
986 cdcStatus.SelectObject( *item->status );
989 dc->Blit( rect.GetX(), rect.GetY() + 1, iWidth, iHeight, &cdcStatus, 0, 0);
991 if (thePrefs::ShowPercent()) {
992 // Percentage of completing
993 // We strip anything below the first decimal point,
994 // to avoid Format doing roundings
995 float percent = floor( file->GetPercentCompleted() * 10.0f ) / 10.0f;
997 wxString buffer = CFormat(wxT("%.1f%%")) % percent;
998 int middlex = (2*rect.GetX() + rect.GetWidth()) >> 1;
999 int middley = (2*rect.GetY() + rect.GetHeight()) >> 1;
1001 wxCoord textwidth, textheight;
1003 dc->GetTextExtent(buffer, &textwidth, &textheight);
1004 wxColour AktColor = dc->GetTextForeground();
1005 if (thePrefs::ShowProgBar()) {
1006 dc->SetTextForeground(*wxWHITE);
1007 } else {
1008 dc->SetTextForeground(*wxBLACK);
1010 dc->DrawText(buffer, middlex - (textwidth >> 1), middley - (textheight >> 1));
1011 dc->SetTextForeground(AktColor);
1015 break;
1018 // Sources
1019 case ColumnSources: {
1020 uint16 sc = file->GetSourceCount();
1021 uint16 ncsc = file->GetNotCurrentSourcesCount();
1022 if ( ncsc ) {
1023 text = CFormat(wxT("%i/%i")) % (sc - ncsc) % sc;
1024 } else {
1025 text = CFormat(wxT("%i")) % sc;
1028 if ( file->GetSrcA4AFCount() ) {
1029 text += CFormat(wxT("+%i")) % file->GetSrcA4AFCount();
1032 if ( file->GetTransferingSrcCount() ) {
1033 text += CFormat(wxT(" (%i)")) % file->GetTransferingSrcCount();
1036 break;
1039 // Priority
1040 case ColumnPriority:
1041 text = PriorityToStr( file->GetDownPriority(), file->IsAutoDownPriority() );
1042 break;
1044 // File-status
1045 case ColumnStatus:
1046 text = file->getPartfileStatus();
1047 break;
1049 // Remaining
1050 case ColumnTimeRemaining: {
1051 if ((file->GetStatus() != PS_COMPLETING) && file->IsPartFile()) {
1052 uint64 remainSize = file->GetFileSize() - file->GetCompletedSize();
1053 sint32 remainTime = file->getTimeRemaining();
1055 if (remainTime >= 0) {
1056 text = CastSecondsToHM(remainTime);
1057 } else {
1058 text = _("Unknown");
1061 text += wxT(" (") + CastItoXBytes(remainSize) + wxT(")");
1063 break;
1066 // Last seen completed
1067 case ColumnLastSeenComplete: {
1068 if ( file->lastseencomplete ) {
1069 text = wxDateTime( file->lastseencomplete ).Format( _("%y/%m/%d %H:%M:%S") );
1070 } else {
1071 text = _("Unknown");
1073 break;
1076 // Last received
1077 case ColumnLastReception: {
1078 const time_t lastReceived = file->GetLastChangeDatetime();
1079 if (lastReceived) {
1080 text = wxDateTime(lastReceived).Format( _("%y/%m/%d %H:%M:%S") );
1081 } else {
1082 text = _("Unknown");
1087 if ( !text.IsEmpty() ) {
1088 dc->DrawText( text, rect.GetX(), rect.GetY() );
1092 wxString CDownloadListCtrl::GetTTSText(unsigned item) const
1094 return ((FileCtrlItem_Struct*)GetItemData(item))->GetFile()->GetFileName().GetPrintable();
1098 int CDownloadListCtrl::SortProc(wxUIntPtr param1, wxUIntPtr param2, long sortData)
1100 FileCtrlItem_Struct* item1 = (FileCtrlItem_Struct*)param1;
1101 FileCtrlItem_Struct* item2 = (FileCtrlItem_Struct*)param2;
1103 int sortMod = (sortData & CMuleListCtrl::SORT_DES) ? -1 : 1;
1104 sortData &= CMuleListCtrl::COLUMN_MASK;
1106 // We modify the result so that it matches with ascending or decending
1107 return sortMod * Compare( item1->GetFile(), item2->GetFile(), sortData);
1111 int CDownloadListCtrl::Compare( const CPartFile* file1, const CPartFile* file2, long lParamSort)
1113 int result = 0;
1115 switch (lParamSort) {
1116 // Sort by part number
1117 case ColumnPart:
1118 result = CmpAny(
1119 file1->GetPartMetFileName().RemoveAllExt(),
1120 file2->GetPartMetFileName().RemoveAllExt() );
1121 break;
1123 // Sort by filename
1124 case ColumnFileName:
1125 result = CmpAny(
1126 file1->GetFileName(),
1127 file2->GetFileName() );
1128 break;
1130 // Sort by size
1131 case ColumnSize:
1132 result = CmpAny(
1133 file1->GetFileSize(),
1134 file2->GetFileSize() );
1135 break;
1137 // Sort by transferred
1138 case ColumnTransferred:
1139 result = CmpAny(
1140 file1->GetTransferred(),
1141 file2->GetTransferred() );
1142 break;
1144 // Sort by completed
1145 case ColumnCompleted:
1146 result = CmpAny(
1147 file1->GetCompletedSize(),
1148 file2->GetCompletedSize() );
1149 break;
1151 // Sort by speed
1152 case ColumnSpeed:
1153 result = CmpAny(
1154 file1->GetKBpsDown() * 1024,
1155 file2->GetKBpsDown() * 1024 );
1156 break;
1158 // Sort by percentage completed
1159 case ColumnProgress:
1160 result = CmpAny(
1161 file1->GetPercentCompleted(),
1162 file2->GetPercentCompleted() );
1163 break;
1165 // Sort by number of sources
1166 case ColumnSources:
1167 result = CmpAny(
1168 file1->GetSourceCount(),
1169 file2->GetSourceCount() );
1170 break;
1172 // Sort by priority
1173 case ColumnPriority:
1174 result = CmpAny(
1175 file1->GetDownPriority(),
1176 file2->GetDownPriority() );
1177 break;
1179 // Sort by status
1180 case ColumnStatus:
1181 result = CmpAny(
1182 file1->getPartfileStatusRang(),
1183 file2->getPartfileStatusRang() );
1184 break;
1186 // Sort by remaining time
1187 case ColumnTimeRemaining:
1188 if (file1->getTimeRemaining() == -1) {
1189 if (file2->getTimeRemaining() == -1) {
1190 result = 0;
1191 } else {
1192 result = -1;
1194 } else {
1195 if (file2->getTimeRemaining() == -1) {
1196 result = 1;
1197 } else {
1198 result = CmpAny(
1199 file1->getTimeRemaining(),
1200 file2->getTimeRemaining() );
1203 break;
1205 // Sort by last seen complete
1206 case ColumnLastSeenComplete:
1207 result = CmpAny(
1208 file1->lastseencomplete,
1209 file2->lastseencomplete );
1210 break;
1212 // Sort by last reception
1213 case ColumnLastReception:
1214 result = CmpAny(
1215 file1->GetLastChangeDatetime(),
1216 file2->GetLastChangeDatetime() );
1217 break;
1220 return result;
1223 void CDownloadListCtrl::ClearCompleted()
1225 CastByID(ID_BTNCLRCOMPL, GetParent(), wxButton)->Enable(false);
1227 // Search for completed files
1228 ListOfUInts32 toClear;
1229 for ( ListItems::iterator it = m_ListItems.begin(); it != m_ListItems.end(); ) {
1230 FileCtrlItem_Struct* item = it->second; ++it;
1232 CPartFile* file = item->GetFile();
1234 if (file->IsCompleted() && file->CheckShowItemInGivenCat(m_category)) {
1235 toClear.push_back(file->ECID());
1238 if (!toClear.empty()) {
1239 theApp->downloadqueue->ClearCompleted(toClear);
1244 void CDownloadListCtrl::ShowFilesCount( int diff )
1246 m_filecount += diff;
1248 wxStaticText* label = CastByName( wxT("downloadsLabel"), GetParent(), wxStaticText );
1250 label->SetLabel(CFormat(_("Downloads (%i)")) % m_filecount);
1251 label->GetParent()->Layout();
1255 static const CMuleColour crHave(104, 104, 104);
1256 static const CMuleColour crFlatHave(0, 0, 0);
1258 static const CMuleColour crPending(255, 208, 0);
1259 static const CMuleColour crFlatPending(255, 255, 100);
1261 static const CMuleColour crProgress(0, 224, 0);
1262 static const CMuleColour crFlatProgress(0, 150, 0);
1264 static const CMuleColour crMissing(255, 0, 0);
1266 void CDownloadListCtrl::DrawFileStatusBar(
1267 const CPartFile* file, wxDC* dc, const wxRect& rect, bool bFlat ) const
1269 static CBarShader s_ChunkBar(16);
1271 s_ChunkBar.SetHeight(rect.height);
1272 s_ChunkBar.SetWidth(rect.width);
1273 s_ChunkBar.SetFileSize( file->GetFileSize() );
1274 s_ChunkBar.Set3dDepth( thePrefs::Get3DDepth() );
1276 if ( file->IsCompleted() || file->GetStatus() == PS_COMPLETING ) {
1277 s_ChunkBar.Fill( bFlat ? crFlatProgress : crProgress );
1278 s_ChunkBar.Draw(dc, rect.x, rect.y, bFlat);
1279 return;
1282 // Part availability ( of missing parts )
1283 const CGapList& gaplist = file->GetGapList();
1284 CGapList::const_iterator it = gaplist.begin();
1285 uint64 lastGapEnd = 0;
1286 CMuleColour colour;
1288 for (; it != gaplist.end(); ++it) {
1290 // Start position
1291 uint32 start = ( it.start() / PARTSIZE );
1292 // fill the Have-Part (between this gap and the last)
1293 if (it.start()) {
1294 s_ChunkBar.FillRange(lastGapEnd + 1, it.start() - 1, bFlat ? crFlatHave : crHave);
1296 lastGapEnd = it.end();
1297 // End position
1298 uint32 end = ( it.end() / PARTSIZE ) + 1;
1300 // Avoid going past the filesize. Dunno if this can happen, but the old code did check.
1301 if ( end > file->GetPartCount() ) {
1302 end = file->GetPartCount();
1305 // Place each gap, one PART at a time
1306 for ( uint64 i = start; i < end; ++i ) {
1307 if ( i < file->m_SrcpartFrequency.size() && file->m_SrcpartFrequency[i]) {
1308 int blue = 210 - ( 22 * ( file->m_SrcpartFrequency[i] - 1 ) );
1309 colour.Set(0, ( blue < 0 ? 0 : blue ), 255 );
1310 } else {
1311 colour = crMissing;
1314 if ( file->IsStopped() ) {
1315 colour.Blend(50);
1318 uint64 gap_begin = ( i == start ? it.start() : PARTSIZE * i );
1319 uint64 gap_end = ( i == end - 1 ? it.end() : PARTSIZE * ( i + 1 ) - 1 );
1321 s_ChunkBar.FillRange( gap_begin, gap_end, colour);
1325 // fill the last Have-Part (between this gap and the last)
1326 s_ChunkBar.FillRange(lastGapEnd + 1, file->GetFileSize() - 1, bFlat ? crFlatHave : crHave);
1328 // Pending parts
1329 const CPartFile::CReqBlockPtrList& requestedblocks_list = file->GetRequestedBlockList();
1330 CPartFile::CReqBlockPtrList::const_iterator it2 = requestedblocks_list.begin();
1331 // adjacing pending parts must be joined to avoid bright lines between them
1332 uint64 lastStartOffset = 0;
1333 uint64 lastEndOffset = 0;
1335 colour = bFlat ? crFlatPending : crPending;
1337 if ( file->IsStopped() ) {
1338 colour.Blend(50);
1341 for (; it2 != requestedblocks_list.end(); ++it2) {
1343 if ((*it2)->StartOffset > lastEndOffset + 1) {
1344 // not adjacing, draw last block
1345 s_ChunkBar.FillRange(lastStartOffset, lastEndOffset, colour);
1346 lastStartOffset = (*it2)->StartOffset;
1347 lastEndOffset = (*it2)->EndOffset;
1348 } else {
1349 // adjacing, grow block
1350 lastEndOffset = (*it2)->EndOffset;
1354 s_ChunkBar.FillRange(lastStartOffset, lastEndOffset, colour);
1357 // Draw the progress-bar
1358 s_ChunkBar.Draw( dc, rect.x, rect.y, bFlat );
1361 // Green progressbar width
1362 int width = (int)(( (float)rect.width / (float)file->GetFileSize() ) *
1363 file->GetCompletedSize() );
1365 if ( bFlat ) {
1366 dc->SetBrush( crFlatProgress.GetBrush() );
1368 dc->DrawRectangle( rect.x, rect.y, width, 3 );
1369 } else {
1370 // Draw the two black lines for 3d-effect
1371 dc->SetPen( *wxBLACK_PEN );
1372 dc->DrawLine( rect.x, rect.y + 0, rect.x + width, rect.y + 0 );
1373 dc->DrawLine( rect.x, rect.y + 2, rect.x + width, rect.y + 2 );
1375 // Draw the green line
1376 dc->SetPen( *(wxThePenList->FindOrCreatePen( crProgress , 1, wxSOLID ) ));
1377 dc->DrawLine( rect.x, rect.y + 1, rect.x + width, rect.y + 1 );
1381 #ifdef __WXMSW__
1382 # define QUOTE wxT("\"")
1383 #else
1384 # define QUOTE wxT("\'")
1385 #endif
1387 void CDownloadListCtrl::PreviewFile(CPartFile* file)
1389 wxString command;
1390 // If no player set in preferences, use mplayer.
1391 // And please, do a warning also :P
1392 if (thePrefs::GetVideoPlayer().IsEmpty()) {
1393 wxMessageBox(_(
1394 "To prevent this warning to show up in every preview,\nset your preferred video player in preferences (default is mplayer)."),
1395 _("File preview"), wxOK, this);
1396 // Since newer versions for some reason mplayer does not automatically
1397 // select video output device and needs a parameter, go figure...
1398 command = wxT("xterm -T \"aMule Preview\" -iconic -e mplayer ") QUOTE wxT("$file") QUOTE;
1399 } else {
1400 command = thePrefs::GetVideoPlayer();
1403 wxString partFile; // File name with full path
1404 wxString partName; // File name only, without path
1406 // Check if we are (pre)viewing a completed file or not
1407 if (!file->IsCompleted()) {
1408 // Remove the .met and see if out video player specifiation uses the magic string
1409 partName = file->GetPartMetFileName().RemoveExt().GetRaw();
1410 partFile = thePrefs::GetTempDir().JoinPaths(file->GetPartMetFileName().RemoveExt()).GetRaw();
1411 } else {
1412 // This is a complete file
1413 // FIXME: This is probably not going to work if the filenames are mangled ...
1414 partName = file->GetFileName().GetRaw();
1415 partFile = file->GetFullName().GetRaw();
1418 // Compatibility with old behaviour
1419 if (!command.Replace(wxT("$file"), wxT("%PARTFILE"))) {
1420 if ((command.Find(wxT("%PARTFILE")) == wxNOT_FOUND) && (command.Find(wxT("%PARTNAME")) == wxNOT_FOUND)) {
1421 // No magic string, so we just append the filename to the player command
1422 // Need to use quotes in case filename contains spaces
1423 command << wxT(" ") << QUOTE << wxT("%PARTFILE") << QUOTE;
1427 #ifndef __WXMSW__
1428 // We have to escape quote characters in the file name, otherwise arbitrary
1429 // options could be passed to the player.
1430 partFile.Replace(QUOTE, wxT("\\") QUOTE);
1431 partName.Replace(QUOTE, wxT("\\") QUOTE);
1432 #endif
1434 command.Replace(wxT("%PARTFILE"), partFile);
1435 command.Replace(wxT("%PARTNAME"), partName);
1437 // We can't use wxShell here, it blocks the app
1438 CTerminationProcess *p = new CTerminationProcess(command);
1439 int ret = wxExecute(command, wxEXEC_ASYNC, p);
1440 bool ok = ret > 0;
1441 if (!ok) {
1442 delete p;
1443 AddLogLineC(CFormat( _("ERROR: Failed to execute external media-player! Command: `%s'") ) %
1444 command );
1447 // File_checked_for_headers