Upstream tarball 9882
[amule.git] / src / TransferWnd.cpp
blob4d2127ff26b857be6b005ecac5e2f4f1bc839d75
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 "TransferWnd.h" // Interface declarations
28 #include <common/MenuIDs.h>
30 #include <wx/config.h>
33 // This include must be before amuleDlg.h
34 #ifdef __WXMSW__
35 #include <wx/msw/winundef.h> // Needed for windows compilation
36 #endif
39 #include "amuleDlg.h" // Needed for CamuleDlg
40 #include "PartFile.h" // Needed for PR_LOW
41 #include "DownloadQueue.h" // Needed for CDownloadQueue
42 #include "CatDialog.h" // Needed for CCatDialog
43 #include "DownloadListCtrl.h" // Needed for CDownloadListCtrl
44 #include "ClientListCtrl.h" // Needed for CClientListCtrl
45 #include "amule.h" // Needed for theApp
46 #include "muuli_wdr.h" // Needed for ID_CATEGORIES
47 #include "SearchDlg.h" // Needed for CSearchDlg->UpdateCatChoice()
48 #include "MuleNotebook.h"
49 #include "Preferences.h"
50 #include "Statistics.h" // Needed for theStats
51 #include "SharedFileList.h" // Needed for CSharedFileList
52 #include "GuiEvents.h" // Needed for CoreNotify_*
55 BEGIN_EVENT_TABLE(CTransferWnd, wxPanel)
56 EVT_RIGHT_DOWN(CTransferWnd::OnNMRclickDLtab)
57 EVT_NOTEBOOK_PAGE_CHANGED(ID_CATEGORIES, CTransferWnd::OnCategoryChanged)
59 EVT_SPLITTER_SASH_POS_CHANGING(ID_SPLATTER, CTransferWnd::OnSashPositionChanging)
61 EVT_BUTTON(ID_BTNCLRCOMPL, CTransferWnd::OnBtnClearDownloads)
62 EVT_BUTTON(ID_BTNSWITCHUP, CTransferWnd::SwitchUploadList)
63 EVT_BUTTON(ID_CLIENTTOGGLE, CTransferWnd::OnToggleClientList)
65 EVT_MENU_RANGE(MP_CAT_SET0, MP_CAT_SET0 + 15, CTransferWnd::OnSetDefaultCat)
66 EVT_MENU(MP_CAT_ADD, CTransferWnd::OnAddCategory)
67 EVT_MENU(MP_CAT_EDIT, CTransferWnd::OnEditCategory)
68 EVT_MENU(MP_CAT_REMOVE, CTransferWnd::OnDelCategory)
69 EVT_MENU(MP_PRIOLOW, CTransferWnd::OnSetCatPriority)
70 EVT_MENU(MP_PRIONORMAL, CTransferWnd::OnSetCatPriority)
71 EVT_MENU(MP_PRIOHIGH, CTransferWnd::OnSetCatPriority)
72 EVT_MENU(MP_PRIOAUTO, CTransferWnd::OnSetCatPriority)
73 EVT_MENU(MP_PAUSE, CTransferWnd::OnSetCatStatus)
74 EVT_MENU(MP_STOP, CTransferWnd::OnSetCatStatus)
75 EVT_MENU(MP_CANCEL, CTransferWnd::OnSetCatStatus)
76 EVT_MENU(MP_RESUME, CTransferWnd::OnSetCatStatus)
77 END_EVENT_TABLE()
81 CTransferWnd::CTransferWnd( wxWindow* pParent )
82 : wxPanel( pParent, -1 )
84 wxSizer* content = transferDlg(this, true);
85 content->Show(this, true);
87 downloadlistctrl = CastChild( wxT("downloadList"), CDownloadListCtrl );
88 clientlistctrl = CastChild( ID_CLIENTLIST, CClientListCtrl );
89 m_dlTab = CastChild( ID_CATEGORIES, CMuleNotebook );
91 CMuleNotebook* nb = CastChild( ID_CATEGORIES, CMuleNotebook );
92 // We want to use our own popup
93 nb->SetPopupHandler( this );
95 // Set default category
96 theApp->glob_prefs->GetCategory(0)->title = GetCatTitle(thePrefs::GetAllcatType());
97 theApp->glob_prefs->GetCategory(0)->path = thePrefs::GetIncomingDir();
99 // Show default + userdefined categories
100 for ( uint32 i = 0; i < theApp->glob_prefs->GetCatCount(); i++ ) {
101 m_dlTab->AddPage( new wxPanel(m_dlTab), theApp->glob_prefs->GetCategory(i)->title );
104 m_menu = NULL;
105 m_splitter = 0;
108 wxConfigBase *config = wxConfigBase::Get();
110 // Check if the clientlist is hidden
111 bool show = true;
112 config->Read( wxT("/GUI/TransferWnd/ShowClientList"), &show, true );
114 if ( !show ) {
115 // Disable the client-list
116 wxCommandEvent event;
117 OnToggleClientList( event );
120 // Load the last used splitter position
121 m_splitter = config->Read( wxT("/GUI/TransferWnd/Splitter"), 463l );
125 CTransferWnd::~CTransferWnd()
127 wxConfigBase *config = wxConfigBase::Get();
129 if ( clientlistctrl->GetListView() == vtNone ) {
130 // Save the splitter position
131 config->Write( wxT("/GUI/TransferWnd/Splitter"), m_splitter );
133 // Save the visible status of the list
134 config->Write( wxT("/GUI/TransferWnd/ShowClientList"), false );
135 } else {
136 wxSplitterWindow* splitter = CastChild( wxT("splitterWnd"), wxSplitterWindow );
138 // Save the splitter position
139 config->Write( wxT("/GUI/TransferWnd/Splitter"), splitter->GetSashPosition() );
141 // Save the visible status of the list
142 config->Write( wxT("/GUI/TransferWnd/ShowClientList"), true );
147 void CTransferWnd::AddCategory( Category_Struct* category )
149 // Add the initial page
150 m_dlTab->AddPage( new wxPanel(m_dlTab), category->title );
152 // Update the title
153 UpdateCategory( m_dlTab->GetPageCount() - 1 );
155 theApp->amuledlg->m_searchwnd->UpdateCatChoice();
158 void CTransferWnd::UpdateCategory( int index, bool titleChanged )
160 wxString label = theApp->glob_prefs->GetCategory( index )->title;
162 if ( thePrefs::ShowCatTabInfos() ) {
163 uint16 files = 0;
164 uint16 download = 0;
166 for ( unsigned int i = 0; i < theApp->downloadqueue->GetFileCount(); ++i ) {
167 CPartFile *cur_file = theApp->downloadqueue->GetFileByIndex(i);
169 if ( cur_file && cur_file->CheckShowItemInGivenCat(index) ) {
170 files++;
172 if ( cur_file->GetTransferingSrcCount() ) {
173 download++;
178 label += wxString::Format(wxT(" (%u/%u)"), download, files );
181 m_dlTab->SetPageText( index, label );
184 if ( titleChanged ) {
185 theApp->amuledlg->m_searchwnd->UpdateCatChoice();
191 void CTransferWnd::OnSetCatStatus( wxCommandEvent& event )
193 if ( event.GetId() == MP_CANCEL ) {
194 if ( wxMessageBox(_("Are you sure you wish to cancel and delete all files in this category?"),_("Confirmation Required"), wxYES_NO|wxCENTRE|wxICON_EXCLAMATION, this) == wxNO) {
195 return;
199 CoreNotify_Download_Set_Cat_Status( m_dlTab->GetSelection(), event.GetId() );
203 void CTransferWnd::OnSetCatPriority( wxCommandEvent& event )
205 int priority = 0;
207 switch ( event.GetId() ) {
208 case MP_PRIOLOW: priority = PR_LOW; break;
209 case MP_PRIONORMAL: priority = PR_NORMAL; break;
210 case MP_PRIOHIGH: priority = PR_HIGH; break;
211 case MP_PRIOAUTO: priority = PR_AUTO; break;
212 default:
213 return;
216 CoreNotify_Download_Set_Cat_Prio( m_dlTab->GetSelection(), priority );
220 void CTransferWnd::OnAddCategory(wxCommandEvent& WXUNUSED(event))
222 CCatDialog dialog( this,
223 // Allow browse?
224 #ifdef CLIENT_GUI
225 false
226 #else
227 true
228 #endif
230 if (dialog.ShowModal() == wxOK) {
231 // Add the files on this folder.
232 Category_Struct* newcat =
233 theApp->glob_prefs->GetCategory(
234 theApp->glob_prefs->GetCatCount()-1);
235 theApp->sharedfiles->AddFilesFromDirectory(newcat->path);
236 theApp->sharedfiles->Reload();
241 void CTransferWnd::OnDelCategory(wxCommandEvent& WXUNUSED(event))
243 RemoveCategory(m_dlTab->GetSelection());
247 void CTransferWnd::RemoveCategory(int index)
249 if ( index > 0 ) {
250 theApp->downloadqueue->ResetCatParts(index);
251 theApp->glob_prefs->RemoveCat(index);
252 RemoveCategoryPage(index);
253 if ( theApp->glob_prefs->GetCatCount() == 1 ) {
254 thePrefs::SetAllcatType(0);
256 theApp->glob_prefs->SaveCats();
257 theApp->amuledlg->m_searchwnd->UpdateCatChoice();
262 void CTransferWnd::RemoveCategoryPage(int index)
264 m_dlTab->RemovePage(index);
265 m_dlTab->SetSelection(0);
266 downloadlistctrl->ChangeCategory(0);
270 void CTransferWnd::OnEditCategory( wxCommandEvent& WXUNUSED(event) )
272 Category_Struct* cat = theApp->glob_prefs->GetCategory(m_dlTab->GetSelection());
273 CPath oldpath = cat->path;
274 CCatDialog dialog( this,
275 // Allow browse?
276 #ifdef CLIENT_GUI
277 false
278 #else
279 true
280 #endif
281 , m_dlTab->GetSelection());
283 if (dialog.ShowModal() == wxOK) {
284 if (!oldpath.IsSameDir(cat->path)) {
285 theApp->sharedfiles->AddFilesFromDirectory(cat->path);
286 theApp->sharedfiles->Reload();
292 void CTransferWnd::OnSetDefaultCat( wxCommandEvent& event )
294 thePrefs::SetAllcatType( event.GetId() - MP_CAT_SET0 );
295 theApp->glob_prefs->GetCategory(0)->title = GetCatTitle( thePrefs::GetAllcatType() );
297 UpdateCategory( 0 );
299 downloadlistctrl->ChangeCategory( 0 );
301 theApp->glob_prefs->SaveCats();
303 downloadlistctrl->SortList();
307 void CTransferWnd::ShowQueueCount(uint32 number)
309 wxString str = wxString::Format( wxT("%u (%u %s)"), number, theStats::GetBannedCount(), _("Banned") );
310 wxStaticText* label = CastChild( ID_CLIENTCOUNT, wxStaticText );
312 label->SetLabel( str );
313 label->GetParent()->Layout();
317 void CTransferWnd::OnCategoryChanged(wxNotebookEvent& evt)
319 downloadlistctrl->ChangeCategory(evt.GetSelection());
320 downloadlistctrl->SortList();
324 void CTransferWnd::OnNMRclickDLtab(wxMouseEvent& evt)
326 // Only handle events from the category-notebook
327 if ( evt.GetEventObject() != (wxObject*)m_dlTab )
328 return;
330 if ( m_dlTab->GetSelection() == -1 ) {
331 return;
334 // Avoid opening another menu when it's already open
335 if ( m_menu == NULL ) {
336 m_menu = new wxMenu( _("Category") );
338 if ( m_dlTab->GetSelection() == 0 ) {
339 wxMenu* catmenu = new wxMenu();
341 catmenu->Append( MP_CAT_SET0, _("All") );
342 catmenu->Append( MP_CAT_SET0 + 1, _("All others") );
344 catmenu->AppendSeparator();
346 catmenu->Append( MP_CAT_SET0 + 2, _("Incomplete") );
347 catmenu->Append( MP_CAT_SET0 + 3, _("Completed") );
348 catmenu->Append( MP_CAT_SET0 + 4, _("Waiting") );
349 catmenu->Append( MP_CAT_SET0 + 5, _("Downloading") );
350 catmenu->Append( MP_CAT_SET0 + 6, _("Erroneous") );
351 catmenu->Append( MP_CAT_SET0 + 7, _("Paused") );
352 catmenu->Append( MP_CAT_SET0 + 8, _("Stopped") );
353 catmenu->Append( MP_CAT_SET0 + 15, _("Active") );
355 catmenu->AppendSeparator();
357 catmenu->Append( MP_CAT_SET0 + 9, _("Video") );
358 catmenu->Append( MP_CAT_SET0 + 10, _("Audio") );
359 catmenu->Append( MP_CAT_SET0 + 11, _("Archive") );
360 catmenu->Append( MP_CAT_SET0 + 12, _("CD-Images") );
361 catmenu->Append( MP_CAT_SET0 + 13, _("Pictures") );
362 catmenu->Append( MP_CAT_SET0 + 14, _("Text") );
364 m_menu->Append(0, _("Select view filter"), catmenu);
367 m_menu->Append( MP_CAT_ADD, _("Add category") );
369 if ( m_dlTab->GetSelection() ) {
370 m_menu->Append(MP_CAT_EDIT,_("Edit category"));
371 m_menu->Append(MP_CAT_REMOVE, _("Remove category"));
374 m_menu->AppendSeparator();
376 m_menu->Append( MP_CANCEL, _("Cancel"));
377 m_menu->Append( MP_STOP, _("&Stop"));
378 m_menu->Append( MP_PAUSE, _("&Pause"));
379 m_menu->Append( MP_RESUME, _("&Resume"));
381 PopupMenu(m_menu, evt.GetPosition());
383 delete m_menu;
384 m_menu = NULL;
389 void CTransferWnd::OnBtnClearDownloads( wxCommandEvent& WXUNUSED(evt) )
391 downloadlistctrl->Freeze();
392 downloadlistctrl->ClearCompleted();
393 downloadlistctrl->Thaw();
397 void CTransferWnd::UpdateCatTabTitles()
399 for ( uint8 i = 0; i < m_dlTab->GetPageCount(); i++ ) {
400 UpdateCategory( i, false );
405 void CTransferWnd::Prepare()
407 int header_height = s_clientlistHeader->GetSize().GetHeight();
408 wxSplitterWindow* splitter = CastChild( wxT("splitterWnd"), wxSplitterWindow );
410 if ( clientlistctrl->GetListView() == vtNone ) {
411 int height = splitter->GetWindow1()->GetSize().GetHeight();
412 height += splitter->GetWindow2()->GetSize().GetHeight();
414 splitter->SetSashPosition( height - header_height );
415 } else if ( m_splitter ) {
416 // Some sainity checking
417 if ( m_splitter < 90 ) {
418 m_splitter = 90;
419 } else {
420 int height = splitter->GetWindow1()->GetSize().GetHeight();
421 height += splitter->GetWindow2()->GetSize().GetHeight();
423 if ( height - header_height * 2 < m_splitter ) {
424 m_splitter = height - header_height * 2;
428 splitter->SetSashPosition( m_splitter );
430 m_splitter = 0;
435 void CTransferWnd::SwitchUploadList(wxCommandEvent& WXUNUSED(evt))
437 clientlistctrl->ToggleView();
438 wxStaticText* label = CastChild( wxT("uploadTitle"), wxStaticText );
440 switch ( clientlistctrl->GetListView() ) {
441 case vtNone:
442 return;
444 case vtUploading:
445 label->SetLabel( _("Uploads") );
446 break;
448 case vtQueued:
449 label->SetLabel( _("On Queue") );
450 break;
452 case vtClients:
453 label->SetLabel( _("Clients") );
454 break;
457 label->GetParent()->Layout();
461 void CTransferWnd::OnToggleClientList(wxCommandEvent& WXUNUSED(evt))
463 wxSplitterWindow* splitter = CastChild( wxT("splitterWnd"), wxSplitterWindow );
464 wxBitmapButton* button = CastChild( ID_CLIENTTOGGLE, wxBitmapButton );
466 // Keeps track of what was last selected.
467 static ViewType lastView = vtNone;
470 if ( clientlistctrl->GetListView() == vtNone ) {
471 splitter->SetSashPosition( m_splitter );
473 clientlistctrl->SetListView( lastView );
475 button->SetBitmapLabel( amuleDlgImages( 10 ) );
476 button->SetBitmapFocus( amuleDlgImages( 10 ) );
477 button->SetBitmapSelected( amuleDlgImages( 10 ) );
479 m_splitter = 0;
480 } else {
481 lastView = clientlistctrl->GetListView();
482 clientlistctrl->SetListView( vtNone );
484 int pos = splitter->GetSashPosition();
486 // Add the height of the listctrl to the top-window
487 int height = clientlistctrl->GetSize().GetHeight();
488 height += splitter->GetWindow1()->GetSize().GetHeight();
490 splitter->SetSashPosition( height );
492 m_splitter = pos;
494 button->SetBitmapLabel( amuleDlgImages( 11 ) );
495 button->SetBitmapFocus( amuleDlgImages( 11 ) );
496 button->SetBitmapSelected( amuleDlgImages( 11 ) );
499 FindWindow(ID_BTNSWITCHUP)->Enable( clientlistctrl->GetListView() != vtNone );
503 void CTransferWnd::OnSashPositionChanging(wxSplitterEvent& evt)
505 int header_height = s_clientlistHeader->GetSize().GetHeight();
507 if ( evt.GetSashPosition() < 90 ) {
508 evt.SetSashPosition( 90 );
509 } else {
510 wxSplitterWindow* splitter = wxStaticCast( evt.GetEventObject(), wxSplitterWindow);
512 int height = splitter->GetWindow1()->GetSize().GetHeight();
513 height += splitter->GetWindow2()->GetSize().GetHeight();
515 if ( clientlistctrl->GetListView() == vtNone ) {
516 if ( height - evt.GetSashPosition() < header_height * 2 ) {
517 evt.Veto();
518 } else {
519 wxCommandEvent event;
520 OnToggleClientList( event );
522 } else {
523 if ( height - evt.GetSashPosition() < header_height * 2 ) {
524 evt.SetSashPosition( height - header_height );
526 wxCommandEvent event;
527 OnToggleClientList( event );
532 // File_checked_for_headers