Forwardport from 2.2.3: Fix autosort happening when it shouldn't
[amule.git] / src / TransferWnd.cpp
blobde6331bb27c00aa080383649b8ef4d9399600e03
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 "SourceListCtrl.h" // Needed for CSourceListCtrl
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_DOWNLOADSSPLATTER, CTransferWnd::OnSashPositionChanging)
61 EVT_BUTTON(ID_BTNCLRCOMPL, CTransferWnd::OnBtnClearDownloads)
62 EVT_BUTTON(ID_CLIENTTOGGLE, CTransferWnd::OnToggleClientList)
64 EVT_MENU_RANGE(MP_CAT_SET0, MP_CAT_SET0 + 15, CTransferWnd::OnSetDefaultCat)
65 EVT_MENU(MP_CAT_ADD, CTransferWnd::OnAddCategory)
66 EVT_MENU(MP_CAT_EDIT, CTransferWnd::OnEditCategory)
67 EVT_MENU(MP_CAT_REMOVE, CTransferWnd::OnDelCategory)
68 EVT_MENU(MP_PRIOLOW, CTransferWnd::OnSetCatPriority)
69 EVT_MENU(MP_PRIONORMAL, CTransferWnd::OnSetCatPriority)
70 EVT_MENU(MP_PRIOHIGH, CTransferWnd::OnSetCatPriority)
71 EVT_MENU(MP_PRIOAUTO, CTransferWnd::OnSetCatPriority)
72 EVT_MENU(MP_PAUSE, CTransferWnd::OnSetCatStatus)
73 EVT_MENU(MP_STOP, CTransferWnd::OnSetCatStatus)
74 EVT_MENU(MP_CANCEL, CTransferWnd::OnSetCatStatus)
75 EVT_MENU(MP_RESUME, CTransferWnd::OnSetCatStatus)
76 END_EVENT_TABLE()
80 CTransferWnd::CTransferWnd( wxWindow* pParent )
81 : wxPanel( pParent, -1 )
83 wxSizer* content = transferDlg(this, true);
84 content->Show(this, true);
86 downloadlistctrl = CastChild( wxT("downloadList"), CDownloadListCtrl );
87 clientlistctrl = CastChild( ID_CLIENTLIST, CSourceListCtrl );
88 m_dlTab = CastChild( ID_CATEGORIES, CMuleNotebook );
90 CMuleNotebook* nb = CastChild( ID_CATEGORIES, CMuleNotebook );
91 // We want to use our own popup
92 nb->SetPopupHandler( this );
94 // Set default category
95 theApp->glob_prefs->GetCategory(0)->title = GetCatTitle(thePrefs::GetAllcatType());
96 theApp->glob_prefs->GetCategory(0)->path = thePrefs::GetIncomingDir();
98 // Show default + userdefined categories
99 for ( uint32 i = 0; i < theApp->glob_prefs->GetCatCount(); i++ ) {
100 m_dlTab->AddPage( new wxPanel(m_dlTab), theApp->glob_prefs->GetCategory(i)->title );
103 m_menu = NULL;
104 m_splitter = 0;
106 wxConfigBase *config = wxConfigBase::Get();
108 // Check if the clientlist is hidden
109 bool show = true;
110 config->Read( wxT("/GUI/TransferWnd/ShowClientList"), &show, true );
111 clientlistctrl->SetShowing(show);
112 // Load the last used splitter position
113 m_splitter = config->Read( wxT("/GUI/TransferWnd/Splitter"), 463l );
117 CTransferWnd::~CTransferWnd()
119 wxConfigBase *config = wxConfigBase::Get();
121 if ( !clientlistctrl->GetShowing() ) {
122 // Save the splitter position
123 config->Write( wxT("/GUI/TransferWnd/Splitter"), m_splitter );
125 // Save the visible status of the list
126 config->Write( wxT("/GUI/TransferWnd/ShowClientList"), false );
127 } else {
128 wxSplitterWindow* splitter = CastChild( wxT("splitterWnd"), wxSplitterWindow );
130 // Save the splitter position
131 config->Write( wxT("/GUI/TransferWnd/Splitter"), splitter->GetSashPosition() );
133 // Save the visible status of the list
134 config->Write( wxT("/GUI/TransferWnd/ShowClientList"), true );
139 void CTransferWnd::AddCategory( Category_Struct* category )
141 // Add the initial page
142 m_dlTab->AddPage( new wxPanel(m_dlTab), category->title );
144 // Update the title
145 UpdateCategory( m_dlTab->GetPageCount() - 1 );
147 theApp->amuledlg->m_searchwnd->UpdateCatChoice();
150 void CTransferWnd::UpdateCategory( int index, bool titleChanged )
152 wxString label = theApp->glob_prefs->GetCategory( index )->title;
154 if ( thePrefs::ShowCatTabInfos() ) {
155 uint16 files = 0;
156 uint16 download = 0;
158 #ifdef CLIENT_GUI
159 for (CDownQueueRem::const_iterator it = theApp->downloadqueue->begin(); it != theApp->downloadqueue->end(); ++it) {
160 CPartFile *cur_file = it->second;
161 #else
162 for ( unsigned int i = 0; i < theApp->downloadqueue->GetFileCount(); ++i ) {
163 CPartFile *cur_file = theApp->downloadqueue->GetFileByIndex(i);
164 #endif
165 if ( cur_file && cur_file->CheckShowItemInGivenCat(index) ) {
166 files++;
168 if ( cur_file->GetTransferingSrcCount() ) {
169 download++;
174 label += wxString::Format(wxT(" (%u/%u)"), download, files );
177 m_dlTab->SetPageText( index, label );
180 if ( titleChanged ) {
181 theApp->amuledlg->m_searchwnd->UpdateCatChoice();
187 void CTransferWnd::OnSetCatStatus( wxCommandEvent& event )
189 if ( event.GetId() == MP_CANCEL ) {
190 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) {
191 return;
195 CoreNotify_Download_Set_Cat_Status( m_dlTab->GetSelection(), event.GetId() );
199 void CTransferWnd::OnSetCatPriority( wxCommandEvent& event )
201 int priority = 0;
203 switch ( event.GetId() ) {
204 case MP_PRIOLOW: priority = PR_LOW; break;
205 case MP_PRIONORMAL: priority = PR_NORMAL; break;
206 case MP_PRIOHIGH: priority = PR_HIGH; break;
207 case MP_PRIOAUTO: priority = PR_AUTO; break;
208 default:
209 return;
212 CoreNotify_Download_Set_Cat_Prio( m_dlTab->GetSelection(), priority );
216 void CTransferWnd::OnAddCategory(wxCommandEvent& WXUNUSED(event))
218 CCatDialog dialog( this,
219 // Allow browse?
220 #ifdef CLIENT_GUI
221 false
222 #else
223 true
224 #endif
226 if (dialog.ShowModal() == wxOK) {
227 // Add the files on this folder.
228 Category_Struct* newcat =
229 theApp->glob_prefs->GetCategory(
230 theApp->glob_prefs->GetCatCount()-1);
231 theApp->sharedfiles->AddFilesFromDirectory(newcat->path);
232 theApp->sharedfiles->Reload();
237 void CTransferWnd::OnDelCategory(wxCommandEvent& WXUNUSED(event))
239 RemoveCategory(m_dlTab->GetSelection());
243 void CTransferWnd::RemoveCategory(int index)
245 if ( index > 0 ) {
246 theApp->downloadqueue->ResetCatParts(index);
247 theApp->glob_prefs->RemoveCat(index);
248 RemoveCategoryPage(index);
249 if ( theApp->glob_prefs->GetCatCount() == 1 ) {
250 thePrefs::SetAllcatType(0);
252 theApp->glob_prefs->SaveCats();
253 theApp->amuledlg->m_searchwnd->UpdateCatChoice();
258 void CTransferWnd::RemoveCategoryPage(int index)
260 m_dlTab->RemovePage(index);
261 m_dlTab->SetSelection(0);
262 downloadlistctrl->ChangeCategory(0);
266 void CTransferWnd::OnEditCategory( wxCommandEvent& WXUNUSED(event) )
268 Category_Struct* cat = theApp->glob_prefs->GetCategory(m_dlTab->GetSelection());
269 CPath oldpath = cat->path;
270 CCatDialog dialog( this,
271 // Allow browse?
272 #ifdef CLIENT_GUI
273 false
274 #else
275 true
276 #endif
277 , m_dlTab->GetSelection());
279 if (dialog.ShowModal() == wxOK) {
280 if (!oldpath.IsSameDir(cat->path)) {
281 theApp->sharedfiles->AddFilesFromDirectory(cat->path);
282 theApp->sharedfiles->Reload();
288 void CTransferWnd::OnSetDefaultCat( wxCommandEvent& event )
290 thePrefs::SetAllcatType( event.GetId() - MP_CAT_SET0 );
291 theApp->glob_prefs->GetCategory(0)->title = GetCatTitle( thePrefs::GetAllcatType() );
293 UpdateCategory( 0 );
295 downloadlistctrl->ChangeCategory( 0 );
297 theApp->glob_prefs->SaveCats();
299 downloadlistctrl->SortList();
303 void CTransferWnd::ShowQueueCount(uint32 /*number*/)
305 #if 0
306 wxString str = wxString::Format( wxT("%u (%u %s)"), number, theStats::GetBannedCount(), _("Banned") );
307 wxStaticText* label = CastChild( ID_CLIENTCOUNT, wxStaticText );
309 label->SetLabel( str );
310 label->GetParent()->Layout();
311 #endif
315 void CTransferWnd::OnCategoryChanged(wxNotebookEvent& evt)
317 downloadlistctrl->ChangeCategory(evt.GetSelection());
318 downloadlistctrl->SortList();
322 void CTransferWnd::OnNMRclickDLtab(wxMouseEvent& evt)
324 // Only handle events from the category-notebook
325 if ( evt.GetEventObject() != (wxObject*)m_dlTab )
326 return;
328 if ( m_dlTab->GetSelection() == -1 ) {
329 return;
332 // Avoid opening another menu when it's already open
333 if ( m_menu == NULL ) {
334 m_menu = new wxMenu( _("Category") );
336 if ( m_dlTab->GetSelection() == 0 ) {
337 wxMenu* catmenu = new wxMenu();
339 catmenu->Append( MP_CAT_SET0, _("All") );
340 catmenu->Append( MP_CAT_SET0 + 1, _("All others") );
342 catmenu->AppendSeparator();
344 catmenu->Append( MP_CAT_SET0 + 2, _("Incomplete") );
345 catmenu->Append( MP_CAT_SET0 + 3, _("Completed") );
346 catmenu->Append( MP_CAT_SET0 + 4, _("Waiting") );
347 catmenu->Append( MP_CAT_SET0 + 5, _("Downloading") );
348 catmenu->Append( MP_CAT_SET0 + 6, _("Erroneous") );
349 catmenu->Append( MP_CAT_SET0 + 7, _("Paused") );
350 catmenu->Append( MP_CAT_SET0 + 8, _("Stopped") );
351 catmenu->Append( MP_CAT_SET0 + 15, _("Active") );
353 catmenu->AppendSeparator();
355 catmenu->Append( MP_CAT_SET0 + 9, _("Video") );
356 catmenu->Append( MP_CAT_SET0 + 10, _("Audio") );
357 catmenu->Append( MP_CAT_SET0 + 11, _("Archive") );
358 catmenu->Append( MP_CAT_SET0 + 12, _("CD-Images") );
359 catmenu->Append( MP_CAT_SET0 + 13, _("Pictures") );
360 catmenu->Append( MP_CAT_SET0 + 14, _("Text") );
362 m_menu->Append(0, _("Select view filter"), catmenu);
365 m_menu->Append( MP_CAT_ADD, _("Add category") );
367 if ( m_dlTab->GetSelection() ) {
368 m_menu->Append(MP_CAT_EDIT,_("Edit category"));
369 m_menu->Append(MP_CAT_REMOVE, _("Remove category"));
372 m_menu->AppendSeparator();
374 m_menu->Append( MP_CANCEL, _("Cancel"));
375 m_menu->Append( MP_STOP, _("&Stop"));
376 m_menu->Append( MP_PAUSE, _("&Pause"));
377 m_menu->Append( MP_RESUME, _("&Resume"));
379 PopupMenu(m_menu, evt.GetPosition());
381 delete m_menu;
382 m_menu = NULL;
387 void CTransferWnd::OnBtnClearDownloads( wxCommandEvent& WXUNUSED(evt) )
389 downloadlistctrl->Freeze();
390 downloadlistctrl->ClearCompleted();
391 downloadlistctrl->Thaw();
395 void CTransferWnd::UpdateCatTabTitles()
397 for ( uint8 i = 0; i < m_dlTab->GetPageCount(); i++ ) {
398 UpdateCategory( i, false );
403 void CTransferWnd::Prepare()
405 wxSplitterWindow* splitter = CastChild( wxT("splitterWnd"), wxSplitterWindow );
406 int height = splitter->GetSize().GetHeight();
407 int header_height = s_clientlistHeader->GetSize().GetHeight();
409 if ( m_splitter ) {
410 // Some sanity checking
411 if ( m_splitter < 90 ) {
412 m_splitter = 90;
413 } else if ( m_splitter > height - header_height * 2 ) {
414 m_splitter = height - header_height * 2;
416 splitter->SetSashPosition( m_splitter );
417 m_splitter = 0;
420 if ( !clientlistctrl->GetShowing() ) {
421 // use a toggle event to close it (calculate size, change button)
422 clientlistctrl->SetShowing( true ); // so it will be toggled to false
423 wxCommandEvent evt1;
424 OnToggleClientList( evt1 );
428 void CTransferWnd::OnToggleClientList(wxCommandEvent& WXUNUSED(evt))
430 wxSplitterWindow* splitter = CastChild( wxT("splitterWnd"), wxSplitterWindow );
431 wxBitmapButton* button = CastChild( ID_CLIENTTOGGLE, wxBitmapButton );
433 if ( !clientlistctrl->GetShowing() ) {
434 splitter->SetSashPosition( m_splitter );
435 m_splitter = 0;
437 clientlistctrl->SetShowing( true );
439 button->SetBitmapLabel( amuleDlgImages( 10 ) );
440 button->SetBitmapFocus( amuleDlgImages( 10 ) );
441 button->SetBitmapSelected( amuleDlgImages( 10 ) );
442 button->SetBitmapHover( amuleDlgImages( 10 ) );
443 } else {
444 clientlistctrl->SetShowing( false );
446 m_splitter = splitter->GetSashPosition();
448 // Add the height of the listctrl to the top-window
449 int height = clientlistctrl->GetSize().GetHeight()
450 + splitter->GetWindow1()->GetSize().GetHeight();
452 splitter->SetSashPosition( height );
454 button->SetBitmapLabel( amuleDlgImages( 11 ) );
455 button->SetBitmapFocus( amuleDlgImages( 11 ) );
456 button->SetBitmapSelected( amuleDlgImages( 11 ) );
457 button->SetBitmapHover( amuleDlgImages( 11 ) );
462 void CTransferWnd::OnSashPositionChanging(wxSplitterEvent& evt)
464 if ( evt.GetSashPosition() < 90 ) {
465 evt.SetSashPosition( 90 );
466 } else {
467 wxSplitterWindow* splitter = wxStaticCast( evt.GetEventObject(), wxSplitterWindow);
468 wxCHECK_RET(splitter, wxT("ERROR: NULL splitter in CTransferWnd::OnSashPositionChanging"));
470 int height = splitter->GetSize().GetHeight();
471 int header_height = s_clientlistHeader->GetSize().GetHeight();
472 int mousey = wxGetMousePosition().y - splitter->GetScreenRect().GetTop();
474 if ( !clientlistctrl->GetShowing() ) {
475 // lower window hidden
476 if ( height - mousey < header_height * 2 ) {
477 // no moving down if already hidden
478 evt.Veto();
479 } else {
480 // show it
481 m_splitter = mousey; // prevent jumping if it was minimized and is then shown by dragging the sash
482 wxCommandEvent event;
483 OnToggleClientList( event );
485 } else {
486 // lower window showing
487 if ( height - mousey < header_height * 2 ) {
488 // hide it
489 wxCommandEvent event;
490 OnToggleClientList( event );
491 } else {
492 // normal resize
493 // If several events queue up, setting the sash to the current mouse position
494 // will speed up things and make sash moving more smoothly.
495 evt.SetSashPosition( mousey );
500 // File_checked_for_headers