Fix a compiler warning
[amule.git] / src / DownloadListCtrl.cpp
blobedaf96ec5f14c46ac94f61b45a86667265d062c4
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 "FileDetailDialog.h" // Needed for CFileDetailDialog
38 #include "GuiEvents.h" // Needed for CoreNotify_*
39 #include "Logger.h"
40 #include "muuli_wdr.h" // Needed for ID_DLOADLIST
41 #include "PartFile.h" // Needed for CPartFile
42 #include "Preferences.h"
43 #include "SharedFileList.h" // Needed for CSharedFileList
44 #include "TerminationProcess.h" // Needed for CTerminationProcess
45 #include "updownclient.h" // Needed for CUpDownClient
46 #include "TransferWnd.h"
47 #include "SourceListCtrl.h"
49 class CPartFile;
52 struct FileCtrlItem_Struct
54 FileCtrlItem_Struct()
55 : dwUpdated(0),
56 status(NULL),
57 m_fileValue(NULL)
58 { }
60 ~FileCtrlItem_Struct() {
61 delete status;
64 CPartFile* GetFile() const {
65 return m_fileValue;
68 void SetContents(CPartFile* file) {
69 m_fileValue = file;
72 uint32 dwUpdated;
73 wxBitmap* status;
75 private:
76 CPartFile* m_fileValue;
79 #define m_ImageList theApp->amuledlg->m_imagelist
81 enum ColumnEnum {
82 ColumnPart = 0,
83 ColumnFileName,
84 ColumnSize,
85 ColumnTransferred,
86 ColumnCompleted,
87 ColumnSpeed,
88 ColumnProgress,
89 ColumnSources,
90 ColumnPriority,
91 ColumnStatus,
92 ColumnTimeRemaining,
93 ColumnLastSeenComplete,
94 ColumnLastReception,
95 ColumnNumberOfColumns
98 BEGIN_EVENT_TABLE(CDownloadListCtrl, CMuleListCtrl)
99 EVT_LIST_ITEM_ACTIVATED(ID_DLOADLIST, CDownloadListCtrl::OnItemActivated)
100 EVT_LIST_ITEM_RIGHT_CLICK(ID_DLOADLIST, CDownloadListCtrl::OnMouseRightClick)
101 EVT_LIST_ITEM_MIDDLE_CLICK(ID_DLOADLIST, CDownloadListCtrl::OnMouseMiddleClick)
102 EVT_LIST_ITEM_SELECTED(ID_DLOADLIST, CDownloadListCtrl::OnItemSelectionChanged)
103 EVT_LIST_ITEM_DESELECTED(ID_DLOADLIST, CDownloadListCtrl::OnItemSelectionChanged)
105 EVT_CHAR( CDownloadListCtrl::OnKeyPressed )
107 EVT_MENU( MP_CANCEL, CDownloadListCtrl::OnCancelFile )
109 EVT_MENU( MP_PAUSE, CDownloadListCtrl::OnSetStatus )
110 EVT_MENU( MP_STOP, CDownloadListCtrl::OnSetStatus )
111 EVT_MENU( MP_RESUME, CDownloadListCtrl::OnSetStatus )
113 EVT_MENU( MP_PRIOLOW, CDownloadListCtrl::OnSetPriority )
114 EVT_MENU( MP_PRIONORMAL, CDownloadListCtrl::OnSetPriority )
115 EVT_MENU( MP_PRIOHIGH, CDownloadListCtrl::OnSetPriority )
116 EVT_MENU( MP_PRIOAUTO, CDownloadListCtrl::OnSetPriority )
118 EVT_MENU( MP_SWAP_A4AF_TO_THIS, CDownloadListCtrl::OnSwapSources )
119 EVT_MENU( MP_SWAP_A4AF_TO_THIS_AUTO, CDownloadListCtrl::OnSwapSources )
120 EVT_MENU( MP_SWAP_A4AF_TO_ANY_OTHER, CDownloadListCtrl::OnSwapSources )
122 EVT_MENU_RANGE( MP_ASSIGNCAT, MP_ASSIGNCAT + 99, CDownloadListCtrl::OnSetCategory )
124 EVT_MENU( MP_CLEARCOMPLETED, CDownloadListCtrl::OnClearCompleted )
126 EVT_MENU( MP_GETMAGNETLINK, CDownloadListCtrl::OnGetLink )
127 EVT_MENU( MP_GETED2KLINK, CDownloadListCtrl::OnGetLink )
129 EVT_MENU( MP_METINFO, CDownloadListCtrl::OnViewFileInfo )
130 EVT_MENU( MP_VIEW, CDownloadListCtrl::OnPreviewFile )
131 EVT_MENU( MP_VIEWFILECOMMENTS, CDownloadListCtrl::OnViewFileComments )
133 EVT_MENU( MP_WS, CDownloadListCtrl::OnGetFeedback )
135 END_EVENT_TABLE()
137 //! This listtype is used when gathering the selected items.
138 typedef std::list<FileCtrlItem_Struct*> ItemList;
140 CDownloadListCtrl::CDownloadListCtrl(
141 wxWindow *parent, wxWindowID winid, const wxPoint& pos, const wxSize& size,
142 long style, const wxValidator& validator, const wxString& name )
144 CMuleListCtrl( parent, winid, pos, size, style | wxLC_OWNERDRAW, validator, name )
146 // Setting the sorter function.
147 SetSortFunc( SortProc );
149 // Set the table-name (for loading and saving preferences).
150 SetTableName( wxT("Download") );
152 m_menu = NULL;
154 m_hilightBrush = CMuleColour(wxSYS_COLOUR_HIGHLIGHT).Blend(125).GetBrush();
156 m_hilightUnfocusBrush = CMuleColour(wxSYS_COLOUR_BTNSHADOW).Blend(125).GetBrush();
158 InsertColumn( ColumnPart, _("Part"), wxLIST_FORMAT_LEFT, 30, wxT("a") );
159 InsertColumn( ColumnFileName, _("File Name"), wxLIST_FORMAT_LEFT, 260, wxT("N") );
160 InsertColumn( ColumnSize, _("Size"), wxLIST_FORMAT_LEFT, 60, wxT("Z") );
161 InsertColumn( ColumnTransferred, _("Transferred"), wxLIST_FORMAT_LEFT, 65, wxT("T") );
162 InsertColumn( ColumnCompleted, _("Completed"), wxLIST_FORMAT_LEFT, 65, wxT("C") );
163 InsertColumn( ColumnSpeed, _("Speed"), wxLIST_FORMAT_LEFT, 65, wxT("S") );
164 InsertColumn( ColumnProgress, _("Progress"), wxLIST_FORMAT_LEFT, 170, wxT("P") );
165 InsertColumn( ColumnSources, _("Sources"), wxLIST_FORMAT_LEFT, 50, wxT("u") );
166 InsertColumn( ColumnPriority, _("Priority"), wxLIST_FORMAT_LEFT, 55, wxT("p") );
167 InsertColumn( ColumnStatus, _("Status"), wxLIST_FORMAT_LEFT, 70, wxT("s") );
168 InsertColumn( ColumnTimeRemaining, _("Time Remaining"), wxLIST_FORMAT_LEFT, 110, wxT("r") );
169 InsertColumn( ColumnLastSeenComplete, _("Last Seen Complete"), wxLIST_FORMAT_LEFT, 220, wxT("c") );
170 InsertColumn( ColumnLastReception, _("Last Reception"), wxLIST_FORMAT_LEFT, 220, wxT("R") );
172 m_category = 0;
173 m_completedFiles = 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 SortList();
214 void CDownloadListCtrl::RemoveFile( CPartFile* file )
216 wxASSERT( file );
218 // Ensure that any list-entries are removed
219 ShowFile( file, false );
221 // Find the assosiated list-item
222 ListItems::iterator it = m_ListItems.find( file );
224 if ( it != m_ListItems.end() ) {
225 delete it->second;
227 m_ListItems.erase( it );
232 void CDownloadListCtrl::UpdateItem(const void* toupdate)
234 // Retrieve all entries matching the source
235 ListIteratorPair rangeIt = m_ListItems.equal_range( toupdate );
237 // Visible lines, default to all because not all platforms
238 // support the GetVisibleLines function
239 long first = 0, last = GetItemCount();
241 #ifndef __WXMSW__
242 // Get visible lines if we need them
243 if ( rangeIt.first != rangeIt.second ) {
244 GetVisibleLines( &first, &last );
246 #endif
248 for ( ListItems::iterator it = rangeIt.first; it != rangeIt.second; ++it ) {
249 FileCtrlItem_Struct* item = it->second;
251 long index = FindItem( -1, reinterpret_cast<wxUIntPtr>(item) );
253 // Determine if the file should be shown in the current category
255 CPartFile* file = item->GetFile();
257 bool show = file->CheckShowItemInGivenCat( m_category );
259 if ( index > -1 ) {
260 if ( show ) {
261 item->dwUpdated = 0;
263 // Only update visible lines
264 if ( index >= first && index <= last) {
265 RefreshItem( index );
267 } else {
268 // Item should no longer be shown in
269 // the current category
270 ShowFile( file, false );
272 } else if ( show ) {
273 // Item has been hidden but new status means
274 // that it should it should be shown in the
275 // current category
276 ShowFile( file, true );
279 if (file->GetStatus() == PS_COMPLETE) {
280 m_completedFiles = true;
282 CastByID(ID_BTNCLRCOMPL, GetParent(), wxButton)->Enable(true);
288 void CDownloadListCtrl::ShowFile( CPartFile* file, bool show )
290 wxASSERT( file );
292 ListItems::iterator it = m_ListItems.find( file );
294 if ( it != m_ListItems.end() ) {
295 FileCtrlItem_Struct* item = it->second;
297 if ( show ) {
298 // Check if the file is already being displayed
299 long index = FindItem( -1, reinterpret_cast<wxUIntPtr>(item) );
300 if ( index == -1 ) {
301 long newitem = InsertItem( GetItemCount(), wxEmptyString );
303 SetItemPtrData( newitem, reinterpret_cast<wxUIntPtr>(item) );
305 wxListItem myitem;
306 myitem.m_itemId = newitem;
307 myitem.SetBackgroundColour( GetBackgroundColour() );
309 SetItem(myitem);
311 RefreshItem( newitem );
313 ShowFilesCount( 1 );
315 } else {
316 // Try to find the file and remove it
317 long index = FindItem( -1, reinterpret_cast<wxUIntPtr>(item) );
318 if ( index > -1 ) {
319 DeleteItem( index );
320 ShowFilesCount( -1 );
326 void CDownloadListCtrl::ChangeCategory( int newCategory )
328 Freeze();
330 // remove all displayed files with a different cat and show the correct ones
331 for (ListItems::const_iterator it = m_ListItems.begin(); it != m_ListItems.end(); it++) {
333 CPartFile* file = it->second->GetFile();
335 bool curVisibility = file->CheckShowItemInGivenCat( m_category );
336 bool newVisibility = file->CheckShowItemInGivenCat( newCategory );
338 // Check if the visibility of the file has changed. However, if the
339 // current category is the default (0) category, then we can't use
340 // curVisiblity to see if the visibility has changed but instead
341 // have to let ShowFile() check if the file is or isn't on the list.
342 if ( curVisibility != newVisibility || !newCategory ) {
343 ShowFile( file, newVisibility );
347 Thaw();
349 m_category = newCategory;
353 uint8 CDownloadListCtrl::GetCategory() const
355 return m_category;
360 * Helper-function: This function is used to gather selected items.
362 * @param list A pointer to the list to gather items from.
363 * @return A list containing the selected items.
365 ItemList GetSelectedItems( CDownloadListCtrl* list)
367 ItemList results;
369 long index = list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
371 while ( index > -1 ) {
372 FileCtrlItem_Struct* item = (FileCtrlItem_Struct*)list->GetItemData( index );
373 results.push_back( item );
375 index = list->GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
378 return results;
382 void CDownloadListCtrl::OnCancelFile(wxCommandEvent& WXUNUSED(event))
384 ItemList files = ::GetSelectedItems(this);
385 if (files.size()) {
386 wxString question;
387 if (files.size() == 1) {
388 question = _("Are you sure that you wish to delete the selected file?");
389 } else {
390 question = _("Are you sure that you wish to delete the selected files?");
392 if (wxMessageBox( question, _("Cancel"), wxICON_QUESTION | wxYES_NO, this) == wxYES) {
393 for (ItemList::iterator it = files.begin(); it != files.end(); ++it) {
394 CPartFile* file = (*it)->GetFile();
395 if (file) {
396 switch (file->GetStatus()) {
397 case PS_WAITINGFORHASH:
398 case PS_HASHING:
399 case PS_COMPLETING:
400 case PS_COMPLETE:
401 break;
402 default:
403 CoreNotify_PartFile_Delete(file);
412 void CDownloadListCtrl::OnSetPriority( wxCommandEvent& event )
414 int priority = 0;
415 switch ( event.GetId() ) {
416 case MP_PRIOLOW: priority = PR_LOW; break;
417 case MP_PRIONORMAL: priority = PR_NORMAL; break;
418 case MP_PRIOHIGH: priority = PR_HIGH; break;
419 case MP_PRIOAUTO: priority = PR_AUTO; break;
420 default:
421 wxASSERT( false );
424 ItemList files = ::GetSelectedItems( this );
426 for ( ItemList::iterator it = files.begin(); it != files.end(); ++it ) {
427 CPartFile* file = (*it)->GetFile();
429 if ( priority == PR_AUTO ) {
430 CoreNotify_PartFile_PrioAuto( file, true );
431 } else {
432 CoreNotify_PartFile_PrioAuto( file, false );
434 CoreNotify_PartFile_PrioSet( file, priority, true );
440 void CDownloadListCtrl::OnSwapSources( wxCommandEvent& event )
442 ItemList files = ::GetSelectedItems( this );
444 for ( ItemList::iterator it = files.begin(); it != files.end(); ++it ) {
445 CPartFile* file = (*it)->GetFile();
447 switch ( event.GetId() ) {
448 case MP_SWAP_A4AF_TO_THIS:
449 CoreNotify_PartFile_Swap_A4AF( file );
450 break;
452 case MP_SWAP_A4AF_TO_THIS_AUTO:
453 CoreNotify_PartFile_Swap_A4AF_Auto( file );
454 break;
456 case MP_SWAP_A4AF_TO_ANY_OTHER:
457 CoreNotify_PartFile_Swap_A4AF_Others( file );
458 break;
464 void CDownloadListCtrl::OnSetCategory( wxCommandEvent& event )
466 ItemList files = ::GetSelectedItems( this );
468 for ( ItemList::iterator it = files.begin(); it != files.end(); ++it ) {
469 CoreNotify_PartFile_SetCat( (*it)->GetFile(), event.GetId() - MP_ASSIGNCAT );
472 ChangeCategory( m_category );
476 void CDownloadListCtrl::OnSetStatus( wxCommandEvent& event )
478 ItemList files = ::GetSelectedItems( this );
480 for ( ItemList::iterator it = files.begin(); it != files.end(); ++it ) {
481 CPartFile* file = (*it)->GetFile();
483 switch ( event.GetId() ) {
484 case MP_PAUSE:
485 CoreNotify_PartFile_Pause( file );
486 break;
488 case MP_RESUME:
489 CoreNotify_PartFile_Resume( file );
490 break;
492 case MP_STOP:
493 CoreNotify_PartFile_Stop( file );
494 break;
500 void CDownloadListCtrl::OnClearCompleted( wxCommandEvent& WXUNUSED(event) )
502 ClearCompleted();
506 void CDownloadListCtrl::OnGetLink(wxCommandEvent& event)
508 ItemList files = ::GetSelectedItems( this );
510 wxString URIs;
512 for ( ItemList::iterator it = files.begin(); it != files.end(); ++it ) {
513 CPartFile* file = (*it)->GetFile();
515 if ( event.GetId() == MP_GETED2KLINK ) {
516 URIs += theApp->CreateED2kLink( file ) + wxT("\n");
517 } else {
518 URIs += theApp->CreateMagnetLink( file ) + wxT("\n");
522 if ( !URIs.IsEmpty() ) {
523 theApp->CopyTextToClipboard( URIs.BeforeLast(wxT('\n')) );
528 void CDownloadListCtrl::OnGetFeedback(wxCommandEvent& WXUNUSED(event))
530 wxString feed;
531 ItemList files = ::GetSelectedItems( this );
533 for (ItemList::iterator it = files.begin(); it != files.end(); ++it) {
534 if (feed.IsEmpty()) {
535 feed = CFormat(_("Feedback from: %s (%s)\n\n")) % thePrefs::GetUserNick() % theApp->GetFullMuleVersion();
536 } else {
537 feed += wxT("\n");
539 feed += (*it)->GetFile()->GetFeedback();
542 if (!feed.IsEmpty()) {
543 theApp->CopyTextToClipboard(feed);
547 void CDownloadListCtrl::OnViewFileInfo( wxCommandEvent& WXUNUSED(event) )
549 ItemList files = ::GetSelectedItems( this );
551 if ( files.size() == 1 ) {
552 CFileDetailDialog dialog( this, files.front()->GetFile() );
553 dialog.ShowModal();
558 void CDownloadListCtrl::OnViewFileComments( wxCommandEvent& WXUNUSED(event) )
560 ItemList files = ::GetSelectedItems( this );
562 if ( files.size() == 1 ) {
563 CCommentDialogLst dialog( this, files.front()->GetFile() );
564 dialog.ShowModal();
568 void CDownloadListCtrl::OnPreviewFile( wxCommandEvent& WXUNUSED(event) )
570 ItemList files = ::GetSelectedItems( this );
572 if ( files.size() == 1 ) {
573 PreviewFile(files.front()->GetFile());
577 void CDownloadListCtrl::OnItemActivated( wxListEvent& evt )
579 CPartFile* file = ((FileCtrlItem_Struct*)GetItemData( evt.GetIndex()))->GetFile();
581 if ((!file->IsPartFile() || file->GetStatus() == PS_COMPLETE) && file->PreviewAvailable()) {
582 PreviewFile( file );
586 void CDownloadListCtrl::OnItemSelectionChanged( wxListEvent& )
588 if (!IsSorting()) {
589 CKnownFileVector filesVector;
590 filesVector.reserve(GetSelectedItemCount());
592 long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
594 while ( index > -1 ) {
595 CPartFile* file = ((FileCtrlItem_Struct*)GetItemData( index ))->GetFile();
596 if (file->IsPartFile()) {
597 filesVector.push_back(file);
599 index = GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
602 std::sort(filesVector.begin(), filesVector.end());
603 theApp->amuledlg->m_transferwnd->clientlistctrl->ShowSources(filesVector);
607 void CDownloadListCtrl::OnMouseRightClick(wxListEvent& evt)
609 long index = CheckSelection(evt);
610 if (index < 0) {
611 return;
614 delete m_menu;
615 m_menu = NULL;
617 FileCtrlItem_Struct* item = (FileCtrlItem_Struct*)GetItemData( index );
618 m_menu = new wxMenu( _("Downloads") );
620 wxMenu* priomenu = new wxMenu();
621 priomenu->AppendCheckItem(MP_PRIOLOW, _("Low"));
622 priomenu->AppendCheckItem(MP_PRIONORMAL, _("Normal"));
623 priomenu->AppendCheckItem(MP_PRIOHIGH, _("High"));
624 priomenu->AppendCheckItem(MP_PRIOAUTO, _("Auto"));
626 m_menu->Append(MP_MENU_PRIO, _("Priority"), priomenu);
627 m_menu->Append(MP_CANCEL, _("Cancel"));
628 m_menu->Append(MP_STOP, _("&Stop"));
629 m_menu->Append(MP_PAUSE, _("&Pause"));
630 m_menu->Append(MP_RESUME, _("&Resume"));
631 m_menu->Append(MP_CLEARCOMPLETED, _("C&lear completed"));
632 //-----------------------------------------------------
633 m_menu->AppendSeparator();
634 //-----------------------------------------------------
635 wxMenu* extendedmenu = new wxMenu();
636 extendedmenu->Append(MP_SWAP_A4AF_TO_THIS,
637 _("Swap every A4AF to this file now"));
638 extendedmenu->AppendCheckItem(MP_SWAP_A4AF_TO_THIS_AUTO,
639 _("Swap every A4AF to this file (Auto)"));
640 //-----------------------------------------------------
641 extendedmenu->AppendSeparator();
642 //-----------------------------------------------------
643 extendedmenu->Append(MP_SWAP_A4AF_TO_ANY_OTHER,
644 _("Swap every A4AF to any other file now"));
645 //-----------------------------------------------------
646 m_menu->Append(MP_MENU_EXTD,
647 _("Extended Options"), extendedmenu);
648 //-----------------------------------------------------
649 m_menu->AppendSeparator();
650 //-----------------------------------------------------
652 m_menu->Append(MP_VIEW, _("Preview"));
653 m_menu->Append(MP_METINFO, _("Show file &details"));
654 m_menu->Append(MP_VIEWFILECOMMENTS, _("Show all comments"));
655 //-----------------------------------------------------
656 m_menu->AppendSeparator();
657 //-----------------------------------------------------
658 m_menu->Append(MP_GETMAGNETLINK,
659 _("Copy magnet URI to clipboard"));
660 m_menu->Append(MP_GETED2KLINK,
661 _("Copy eD2k &link to clipboard"));
662 m_menu->Append(MP_WS,
663 _("Copy feedback to clipboard"));
664 //-----------------------------------------------------
665 m_menu->AppendSeparator();
666 //-----------------------------------------------------
667 // Add dynamic entries
668 wxMenu *cats = new wxMenu(_("Category"));
669 if (theApp->glob_prefs->GetCatCount() > 1) {
670 for (uint32 i = 0; i < theApp->glob_prefs->GetCatCount(); i++) {
671 if ( i == 0 ) {
672 cats->Append( MP_ASSIGNCAT, _("unassign") );
673 } else {
674 cats->Append( MP_ASSIGNCAT + i,
675 theApp->glob_prefs->GetCategory(i)->title );
679 m_menu->Append(MP_MENU_CATS, _("Assign to category"), cats);
680 m_menu->Enable(MP_MENU_CATS, (theApp->glob_prefs->GetCatCount() > 1) );
682 CPartFile* file = item->GetFile();
683 // then set state
684 bool canStop;
685 bool canPause;
686 bool canCancel;
687 bool fileResumable;
688 if (file->GetStatus(true) != PS_ALLOCATING) {
689 const uint8_t fileStatus = file->GetStatus();
690 canStop =
691 (fileStatus != PS_ERROR) &&
692 (fileStatus != PS_COMPLETE) &&
693 (file->IsStopped() != true);
694 canPause = (file->GetStatus() != PS_PAUSED) && canStop;
695 fileResumable =
696 (fileStatus == PS_PAUSED) ||
697 (fileStatus == PS_ERROR) ||
698 (fileStatus == PS_INSUFFICIENT);
699 canCancel = fileStatus != PS_COMPLETE;
700 } else {
701 canStop = canPause = canCancel = fileResumable = false;
704 m_menu->Enable( MP_CANCEL, canCancel );
705 m_menu->Enable( MP_PAUSE, canPause );
706 m_menu->Enable( MP_STOP, canStop );
707 m_menu->Enable( MP_RESUME, fileResumable );
708 m_menu->Enable( MP_CLEARCOMPLETED, m_completedFiles );
710 wxString view;
711 if (file->IsPartFile() && (file->GetStatus() != PS_COMPLETE)) {
712 view = CFormat(wxT("%s [%s]")) % _("Preview")
713 % file->GetPartMetFileName().RemoveExt();
714 } else if ( file->GetStatus() == PS_COMPLETE ) {
715 view = _("&Open the file");
717 m_menu->SetLabel(MP_VIEW, view);
718 m_menu->Enable(MP_VIEW, file->PreviewAvailable());
720 FileRatingList ratingList;
721 item->GetFile()->GetRatingAndComments(ratingList);
722 m_menu->Enable(MP_VIEWFILECOMMENTS, !ratingList.empty());
724 m_menu->Check( MP_SWAP_A4AF_TO_THIS_AUTO, file->IsA4AFAuto() );
726 int priority = file->IsAutoDownPriority() ? PR_AUTO : file->GetDownPriority();
728 priomenu->Check( MP_PRIOHIGH, priority == PR_HIGH );
729 priomenu->Check( MP_PRIONORMAL, priority == PR_NORMAL );
730 priomenu->Check( MP_PRIOLOW, priority == PR_LOW );
731 priomenu->Check( MP_PRIOAUTO, priority == PR_AUTO );
733 m_menu->Enable( MP_MENU_EXTD, canPause );
735 PopupMenu(m_menu, evt.GetPoint());
736 delete m_menu;
737 m_menu = NULL;
741 void CDownloadListCtrl::OnMouseMiddleClick(wxListEvent& evt)
743 // Check if clicked item is selected. If not, unselect all and select it.
744 long index = CheckSelection(evt);
745 if ( index < 0 ) {
746 return;
749 CFileDetailDialog(this, ((FileCtrlItem_Struct*)GetItemData( index ))->GetFile()).ShowModal();
753 void CDownloadListCtrl::OnKeyPressed( wxKeyEvent& event )
755 // Check if delete was pressed
756 switch (event.GetKeyCode()) {
757 case WXK_NUMPAD_DELETE:
758 case WXK_DELETE: {
759 wxCommandEvent evt;
760 OnCancelFile( evt );
761 break;
763 case WXK_F2: {
764 ItemList files = ::GetSelectedItems( this );
765 if (files.size() == 1) {
766 CPartFile* file = files.front()->GetFile();
768 // Currently renaming of completed files causes problem with kad
769 if (file->IsPartFile()) {
770 wxString strNewName = ::wxGetTextFromUser(
771 _("Enter new name for this file:"),
772 _("File rename"), file->GetFileName().GetPrintable());
774 CPath newName = CPath(strNewName);
775 if (newName.IsOk() && (newName != file->GetFileName())) {
776 theApp->sharedfiles->RenameFile(file, newName);
780 break;
782 default:
783 event.Skip();
788 void CDownloadListCtrl::OnDrawItem(
789 int item, wxDC* dc, const wxRect& rect, const wxRect& rectHL, bool highlighted)
791 // Don't do any drawing if there's nobody to see it.
792 if ( !theApp->amuledlg->IsDialogVisible( CamuleDlg::DT_TRANSFER_WND ) ) {
793 return;
796 FileCtrlItem_Struct* content = (FileCtrlItem_Struct *)GetItemData(item);
798 // Define text-color and background
799 // and the border of the drawn area
800 if (highlighted) {
801 CMuleColour colour;
802 if (GetFocus()) {
803 dc->SetBackground(m_hilightBrush);
804 colour = m_hilightBrush.GetColour();
805 } else {
806 dc->SetBackground(m_hilightUnfocusBrush);
807 colour = m_hilightUnfocusBrush.GetColour();
809 dc->SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
810 dc->SetPen( colour.Blend(65).GetPen() );
811 } else {
812 dc->SetBackground(*(wxTheBrushList->FindOrCreateBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX), wxSOLID)));
813 dc->SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
814 dc->SetPen(*wxTRANSPARENT_PEN);
816 dc->SetBrush( dc->GetBackground() );
818 dc->DrawRectangle( rectHL.x, rectHL.y, rectHL.width, rectHL.height );
820 dc->SetPen(*wxTRANSPARENT_PEN);
822 if (!highlighted || !GetFocus() ) {
823 // If we have category, override textforeground with what category tells us.
824 CPartFile *file = content->GetFile();
825 if ( file->GetCategory() ) {
826 dc->SetTextForeground(CMuleColour(theApp->glob_prefs->GetCatColor(file->GetCategory())) );
830 // Various constant values we use
831 const int iTextOffset = ( rect.GetHeight() - dc->GetCharHeight() ) / 2;
832 const int iOffset = 4;
834 wxRect cur_rec( iOffset, rect.y, 0, rect.height );
835 for (int i = 0; i < GetColumnCount(); i++) {
836 wxListItem listitem;
837 GetColumn(i, listitem);
839 if (listitem.GetWidth() > 2*iOffset) {
840 cur_rec.width = listitem.GetWidth() - 2*iOffset;
842 // Make a copy of the current rectangle so we can apply specific tweaks
843 wxRect target_rec = cur_rec;
844 if ( i == ColumnProgress ) {
845 // Double the offset to make room for the cirle-marker
846 target_rec.x += iOffset;
847 target_rec.width -= iOffset;
848 } else {
849 // will ensure that text is about in the middle ;)
850 target_rec.y += iTextOffset;
853 // Draw the item
854 DrawFileItem(dc, i, target_rec, content);
857 // Increment to the next column
858 cur_rec.x += listitem.GetWidth();
863 void CDownloadListCtrl::DrawFileItem( wxDC* dc, int nColumn, const wxRect& rect, FileCtrlItem_Struct* item ) const
865 wxDCClipper clipper( *dc, rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight() );
867 const CPartFile* file = item->GetFile();
869 // Used to contain the contenst of cells that dont need any fancy drawing, just text.
870 wxString text;
872 switch (nColumn) {
873 // Part Number
874 case ColumnPart: {
875 if (file->IsPartFile() && !(file->GetStatus() == PS_COMPLETE)) {
876 text = file->GetPartMetFileName().RemoveAllExt().GetPrintable();
878 break;
880 // Filename
881 case ColumnFileName: {
882 wxString filename = file->GetFileName().GetPrintable();
884 if (file->HasRating() || file->HasComment()) {
885 int image = Client_CommentOnly_Smiley;
886 if (file->HasRating()) {
887 image = Client_InvalidRating_Smiley + file->UserRating() - 1;
890 wxASSERT(image >= Client_InvalidRating_Smiley);
891 wxASSERT(image <= Client_CommentOnly_Smiley);
893 int imgWidth = 16;
895 // it's already centered by OnDrawItem() ...
896 m_ImageList.Draw(image, *dc, rect.GetX(), rect.GetY() - 1,
897 wxIMAGELIST_DRAW_TRANSPARENT);
898 dc->DrawText(filename, rect.GetX() + imgWidth + 4, rect.GetY());
899 } else {
900 dc->DrawText(filename, rect.GetX(), rect.GetY());
902 break;
905 // Filesize
906 case ColumnSize:
907 text = CastItoXBytes( file->GetFileSize() );
908 break;
910 // Transferred
911 case ColumnTransferred:
912 text = CastItoXBytes( file->GetTransferred() );
913 break;
915 // Completed
916 case ColumnCompleted:
917 text = CastItoXBytes( file->GetCompletedSize() );
918 break;
920 // Speed
921 case ColumnSpeed:
922 if ( file->GetTransferingSrcCount() ) {
923 text = wxString::Format( wxT("%.1f "), file->GetKBpsDown() ) +
924 _("kB/s");
926 break;
928 // Progress
929 case ColumnProgress:{
930 if (thePrefs::ShowProgBar()) {
931 int iWidth = rect.GetWidth() - 2;
932 int iHeight = rect.GetHeight() - 2;
934 // DO NOT DRAW IT ALL THE TIME
935 uint32 dwTicks = GetTickCount();
937 wxMemoryDC cdcStatus;
939 if ( item->dwUpdated < dwTicks || !item->status || iWidth != item->status->GetWidth() ) {
940 if ( item->status == NULL) {
941 item->status = new wxBitmap(iWidth, iHeight);
942 } else if ( item->status->GetWidth() != iWidth ) {
943 // Only recreate if the size has changed
944 item->status->Create(iWidth, iHeight);
947 cdcStatus.SelectObject( *item->status );
949 if ( thePrefs::UseFlatBar() ) {
950 DrawFileStatusBar( file, &cdcStatus,
951 wxRect(0, 0, iWidth, iHeight), true);
952 } else {
953 DrawFileStatusBar( file, &cdcStatus,
954 wxRect(1, 1, iWidth - 2, iHeight - 2), false);
956 // Draw black border
957 cdcStatus.SetPen( *wxBLACK_PEN );
958 cdcStatus.SetBrush( *wxTRANSPARENT_BRUSH );
959 cdcStatus.DrawRectangle( 0, 0, iWidth, iHeight );
962 item->dwUpdated = dwTicks + 5000; // Plus five seconds
963 } else {
964 cdcStatus.SelectObject( *item->status );
967 dc->Blit( rect.GetX(), rect.GetY() + 1, iWidth, iHeight, &cdcStatus, 0, 0);
969 if (thePrefs::ShowPercent()) {
970 // Percentage of completing
971 // We strip anything below the first decimal point,
972 // to avoid Format doing roundings
973 float percent = floor( file->GetPercentCompleted() * 10.0f ) / 10.0f;
975 wxString buffer = wxString::Format( wxT("%.1f%%"), percent );
976 int middlex = (2*rect.GetX() + rect.GetWidth()) >> 1;
977 int middley = (2*rect.GetY() + rect.GetHeight()) >> 1;
979 wxCoord textwidth, textheight;
981 dc->GetTextExtent(buffer, &textwidth, &textheight);
982 wxColour AktColor = dc->GetTextForeground();
983 if (thePrefs::ShowProgBar()) {
984 dc->SetTextForeground(*wxWHITE);
985 } else {
986 dc->SetTextForeground(*wxBLACK);
988 dc->DrawText(buffer, middlex - (textwidth >> 1), middley - (textheight >> 1));
989 dc->SetTextForeground(AktColor);
993 break;
996 // Sources
997 case ColumnSources: {
998 uint16 sc = file->GetSourceCount();
999 uint16 ncsc = file->GetNotCurrentSourcesCount();
1000 if ( ncsc ) {
1001 text = wxString::Format( wxT("%i/%i" ), sc - ncsc, sc );
1002 } else {
1003 text = wxString::Format( wxT("%i"), sc );
1006 if ( file->GetSrcA4AFCount() ) {
1007 text += wxString::Format( wxT("+%i"), file->GetSrcA4AFCount() );
1010 if ( file->GetTransferingSrcCount() ) {
1011 text += wxString::Format( wxT(" (%i)"), file->GetTransferingSrcCount() );
1014 break;
1017 // Priority
1018 case ColumnPriority:
1019 text = PriorityToStr( file->GetDownPriority(), file->IsAutoDownPriority() );
1020 break;
1022 // File-status
1023 case ColumnStatus:
1024 text = file->getPartfileStatus();
1025 break;
1027 // Remaining
1028 case ColumnTimeRemaining: {
1029 if ((file->GetStatus() != PS_COMPLETING) && file->IsPartFile()) {
1030 uint64 remainSize = file->GetFileSize() - file->GetCompletedSize();
1031 sint32 remainTime = file->getTimeRemaining();
1033 if (remainTime >= 0) {
1034 text = CastSecondsToHM(remainTime);
1035 } else {
1036 text = _("Unknown");
1039 text += wxT(" (") + CastItoXBytes(remainSize) + wxT(")");
1041 break;
1044 // Last seen completed
1045 case ColumnLastSeenComplete: {
1046 if ( file->lastseencomplete ) {
1047 text = wxDateTime( file->lastseencomplete ).Format( _("%y/%m/%d %H:%M:%S") );
1048 } else {
1049 text = _("Unknown");
1051 break;
1054 // Last received
1055 case ColumnLastReception: {
1056 const time_t lastReceived = file->GetLastChangeDatetime();
1057 if (lastReceived) {
1058 text = wxDateTime(lastReceived).Format( _("%y/%m/%d %H:%M:%S") );
1059 } else {
1060 text = _("Unknown");
1065 if ( !text.IsEmpty() ) {
1066 dc->DrawText( text, rect.GetX(), rect.GetY() );
1070 wxString CDownloadListCtrl::GetTTSText(unsigned item) const
1072 return ((FileCtrlItem_Struct*)GetItemData(item))->GetFile()->GetFileName().GetPrintable();
1076 int CDownloadListCtrl::SortProc(wxUIntPtr param1, wxUIntPtr param2, long sortData)
1078 FileCtrlItem_Struct* item1 = (FileCtrlItem_Struct*)param1;
1079 FileCtrlItem_Struct* item2 = (FileCtrlItem_Struct*)param2;
1081 int sortMod = (sortData & CMuleListCtrl::SORT_DES) ? -1 : 1;
1082 sortData &= CMuleListCtrl::COLUMN_MASK;
1084 // We modify the result so that it matches with ascending or decending
1085 return sortMod * Compare( item1->GetFile(), item2->GetFile(), sortData);
1089 int CDownloadListCtrl::Compare( const CPartFile* file1, const CPartFile* file2, long lParamSort)
1091 int result = 0;
1093 switch (lParamSort) {
1094 // Sort by part number
1095 case ColumnPart:
1096 result = CmpAny(
1097 file1->GetPartMetFileName().RemoveAllExt(),
1098 file2->GetPartMetFileName().RemoveAllExt() );
1099 break;
1101 // Sort by filename
1102 case ColumnFileName:
1103 result = CmpAny(
1104 file1->GetFileName(),
1105 file2->GetFileName() );
1106 break;
1108 // Sort by size
1109 case ColumnSize:
1110 result = CmpAny(
1111 file1->GetFileSize(),
1112 file2->GetFileSize() );
1113 break;
1115 // Sort by transferred
1116 case ColumnTransferred:
1117 result = CmpAny(
1118 file1->GetTransferred(),
1119 file2->GetTransferred() );
1120 break;
1122 // Sort by completed
1123 case ColumnCompleted:
1124 result = CmpAny(
1125 file1->GetCompletedSize(),
1126 file2->GetCompletedSize() );
1127 break;
1129 // Sort by speed
1130 case ColumnSpeed:
1131 result = CmpAny(
1132 file1->GetKBpsDown() * 1024,
1133 file2->GetKBpsDown() * 1024 );
1134 break;
1136 // Sort by percentage completed
1137 case ColumnProgress:
1138 result = CmpAny(
1139 file1->GetPercentCompleted(),
1140 file2->GetPercentCompleted() );
1141 break;
1143 // Sort by number of sources
1144 case ColumnSources:
1145 result = CmpAny(
1146 file1->GetSourceCount(),
1147 file2->GetSourceCount() );
1148 break;
1150 // Sort by priority
1151 case ColumnPriority:
1152 result = CmpAny(
1153 file1->GetDownPriority(),
1154 file2->GetDownPriority() );
1155 break;
1157 // Sort by status
1158 case ColumnStatus:
1159 result = CmpAny(
1160 file1->getPartfileStatusRang(),
1161 file2->getPartfileStatusRang() );
1162 break;
1164 // Sort by remaining time
1165 case ColumnTimeRemaining:
1166 if (file1->getTimeRemaining() == -1) {
1167 if (file2->getTimeRemaining() == -1) {
1168 result = 0;
1169 } else {
1170 result = -1;
1172 } else {
1173 if (file2->getTimeRemaining() == -1) {
1174 result = 1;
1175 } else {
1176 result = CmpAny(
1177 file1->getTimeRemaining(),
1178 file2->getTimeRemaining() );
1181 break;
1183 // Sort by last seen complete
1184 case ColumnLastSeenComplete:
1185 result = CmpAny(
1186 file1->lastseencomplete,
1187 file2->lastseencomplete );
1188 break;
1190 // Sort by last reception
1191 case ColumnLastReception:
1192 result = CmpAny(
1193 file1->GetLastChangeDatetime(),
1194 file2->GetLastChangeDatetime() );
1195 break;
1198 return result;
1201 void CDownloadListCtrl::ClearCompleted()
1203 m_completedFiles = false;
1204 CastByID(ID_BTNCLRCOMPL, GetParent(), wxButton)->Enable(false);
1206 // Search for completed files
1207 for ( ListItems::iterator it = m_ListItems.begin(); it != m_ListItems.end(); ) {
1208 FileCtrlItem_Struct* item = it->second; ++it;
1210 CPartFile* file = item->GetFile();
1212 if ( file->IsPartFile() == false ) {
1213 RemoveFile(file);
1219 void CDownloadListCtrl::ShowFilesCount( int diff )
1221 m_filecount += diff;
1223 wxStaticText* label = CastByName( wxT("downloadsLabel"), GetParent(), wxStaticText );
1225 label->SetLabel( wxString::Format( _("Downloads (%i)"), m_filecount ) );
1226 label->GetParent()->Layout();
1229 bool CDownloadListCtrl::ShowItemInCurrentCat(
1230 const CPartFile* file, int newsel ) const
1232 return
1233 ((newsel == 0 && !thePrefs::ShowAllNotCats()) ||
1234 (newsel == 0 && thePrefs::ShowAllNotCats() && file->GetCategory() == 0)) ||
1235 (newsel > 0 && newsel == file->GetCategory());
1238 static const CMuleColour crHave(104, 104, 104);
1239 static const CMuleColour crFlatHave(0, 0, 0);
1241 static const CMuleColour crPending(255, 208, 0);
1242 static const CMuleColour crFlatPending(255, 255, 100);
1244 static const CMuleColour crProgress(0, 224, 0);
1245 static const CMuleColour crFlatProgress(0, 150, 0);
1247 static const CMuleColour crMissing(255, 0, 0);
1249 void CDownloadListCtrl::DrawFileStatusBar(
1250 const CPartFile* file, wxDC* dc, const wxRect& rect, bool bFlat ) const
1252 static CBarShader s_ChunkBar(16);
1254 s_ChunkBar.SetHeight(rect.height);
1255 s_ChunkBar.SetWidth(rect.width);
1256 s_ChunkBar.SetFileSize( file->GetFileSize() );
1257 s_ChunkBar.Set3dDepth( thePrefs::Get3DDepth() );
1259 if ( file->GetStatus() == PS_COMPLETE || file->GetStatus() == PS_COMPLETING ) {
1260 s_ChunkBar.Fill( bFlat ? crFlatProgress : crProgress );
1261 s_ChunkBar.Draw(dc, rect.x, rect.y, bFlat);
1262 return;
1265 // Part availability ( of missing parts )
1266 const CGapList& gaplist = file->GetGapList();
1267 CGapList::const_iterator it = gaplist.begin();
1268 uint64 lastGapEnd = 0;
1269 CMuleColour colour;
1271 for (; it != gaplist.end(); ++it) {
1273 // Start position
1274 uint32 start = ( it.start() / PARTSIZE );
1275 // fill the Have-Part (between this gap and the last)
1276 if (it.start()) {
1277 s_ChunkBar.FillRange(lastGapEnd + 1, it.start() - 1, bFlat ? crFlatHave : crHave);
1279 lastGapEnd = it.end();
1280 // End position
1281 uint32 end = ( it.end() / PARTSIZE ) + 1;
1283 // Avoid going past the filesize. Dunno if this can happen, but the old code did check.
1284 if ( end > file->GetPartCount() ) {
1285 end = file->GetPartCount();
1288 // Place each gap, one PART at a time
1289 for ( uint64 i = start; i < end; ++i ) {
1290 if ( i < file->m_SrcpartFrequency.size() && file->m_SrcpartFrequency[i]) {
1291 int blue = 210 - ( 22 * ( file->m_SrcpartFrequency[i] - 1 ) );
1292 colour.Set(0, ( blue < 0 ? 0 : blue ), 255 );
1293 } else {
1294 colour = crMissing;
1297 if ( file->IsStopped() ) {
1298 colour.Blend(50);
1301 uint64 gap_begin = ( i == start ? it.start() : PARTSIZE * i );
1302 uint64 gap_end = ( i == end - 1 ? it.end() : PARTSIZE * ( i + 1 ) - 1 );
1304 s_ChunkBar.FillRange( gap_begin, gap_end, colour);
1308 // fill the last Have-Part (between this gap and the last)
1309 s_ChunkBar.FillRange(lastGapEnd + 1, file->GetFileSize() - 1, bFlat ? crFlatHave : crHave);
1311 // Pending parts
1312 const CPartFile::CReqBlockPtrList& requestedblocks_list = file->GetRequestedBlockList();
1313 CPartFile::CReqBlockPtrList::const_iterator it2 = requestedblocks_list.begin();
1314 // adjacing pending parts must be joined to avoid bright lines between them
1315 uint64 lastStartOffset = 0;
1316 uint64 lastEndOffset = 0;
1318 colour = bFlat ? crFlatPending : crPending;
1320 if ( file->IsStopped() ) {
1321 colour.Blend(50);
1324 for (; it2 != requestedblocks_list.end(); ++it2) {
1326 if ((*it2)->StartOffset > lastEndOffset + 1) {
1327 // not adjacing, draw last block
1328 s_ChunkBar.FillRange(lastStartOffset, lastEndOffset, colour);
1329 lastStartOffset = (*it2)->StartOffset;
1330 lastEndOffset = (*it2)->EndOffset;
1331 } else {
1332 // adjacing, grow block
1333 lastEndOffset = (*it2)->EndOffset;
1337 s_ChunkBar.FillRange(lastStartOffset, lastEndOffset, colour);
1340 // Draw the progress-bar
1341 s_ChunkBar.Draw( dc, rect.x, rect.y, bFlat );
1344 // Green progressbar width
1345 int width = (int)(( (float)rect.width / (float)file->GetFileSize() ) *
1346 file->GetCompletedSize() );
1348 if ( bFlat ) {
1349 dc->SetBrush( crFlatProgress.GetBrush() );
1351 dc->DrawRectangle( rect.x, rect.y, width, 3 );
1352 } else {
1353 // Draw the two black lines for 3d-effect
1354 dc->SetPen( *wxBLACK_PEN );
1355 dc->DrawLine( rect.x, rect.y + 0, rect.x + width, rect.y + 0 );
1356 dc->DrawLine( rect.x, rect.y + 2, rect.x + width, rect.y + 2 );
1358 // Draw the green line
1359 dc->SetPen( *(wxThePenList->FindOrCreatePen( crProgress , 1, wxSOLID ) ));
1360 dc->DrawLine( rect.x, rect.y + 1, rect.x + width, rect.y + 1 );
1364 #ifdef __WXMSW__
1365 # define QUOTE wxT("\"")
1366 #else
1367 # define QUOTE wxT("\'")
1368 #endif
1370 void CDownloadListCtrl::PreviewFile(CPartFile* file)
1372 wxString command;
1373 // If no player set in preferences, use mplayer.
1374 // And please, do a warning also :P
1375 if (thePrefs::GetVideoPlayer().IsEmpty()) {
1376 wxMessageBox(_(
1377 "To prevent this warning to show up in every preview,\nset your preferred video player in preferences (default is mplayer)."),
1378 _("File preview"), wxOK, this);
1379 // Since newer versions for some reason mplayer does not automatically
1380 // select video output device and needs a parameter, go figure...
1381 command = wxT("xterm -T \"aMule Preview\" -iconic -e mplayer ") QUOTE wxT("$file") QUOTE;
1382 } else {
1383 command = thePrefs::GetVideoPlayer();
1386 wxString partFile; // File name with full path
1387 wxString partName; // File name only, without path
1389 // Check if we are (pre)viewing a completed file or not
1390 if (file->GetStatus() != PS_COMPLETE) {
1391 // Remove the .met and see if out video player specifiation uses the magic string
1392 partName = file->GetPartMetFileName().RemoveExt().GetRaw();
1393 partFile = thePrefs::GetTempDir().JoinPaths(file->GetPartMetFileName().RemoveExt()).GetRaw();
1394 } else {
1395 // This is a complete file
1396 // FIXME: This is probably not going to work if the filenames are mangled ...
1397 partName = file->GetFileName().GetRaw();
1398 partFile = file->GetFullName().GetRaw();
1401 // Compatibility with old behaviour
1402 if (!command.Replace(wxT("$file"), wxT("%PARTFILE"))) {
1403 if ((command.Find(wxT("%PARTFILE")) == wxNOT_FOUND) && (command.Find(wxT("%PARTNAME")) == wxNOT_FOUND)) {
1404 // No magic string, so we just append the filename to the player command
1405 // Need to use quotes in case filename contains spaces
1406 command << wxT(" ") << QUOTE << wxT("%PARTFILE") << QUOTE;
1410 #ifndef __WXMSW__
1411 // We have to escape quote characters in the file name, otherwise arbitrary
1412 // options could be passed to the player.
1413 partFile.Replace(QUOTE, wxT("\\") QUOTE);
1414 partName.Replace(QUOTE, wxT("\\") QUOTE);
1415 #endif
1417 command.Replace(wxT("%PARTFILE"), partFile);
1418 command.Replace(wxT("%PARTNAME"), partName);
1420 // We can't use wxShell here, it blocks the app
1421 CTerminationProcess *p = new CTerminationProcess(command);
1422 int ret = wxExecute(command, wxEXEC_ASYNC, p);
1423 bool ok = ret > 0;
1424 if (!ok) {
1425 delete p;
1426 AddLogLineM( true,
1427 CFormat( _("ERROR: Failed to execute external media-player! Command: `%s'") ) %
1428 command );
1431 // File_checked_for_headers