2 // This file is part of the aMule Project.
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 )
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
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.
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
35 #include <wx/msw/winundef.h> // Needed for windows compilation
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
)
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
);
106 wxConfigBase
*config
= wxConfigBase::Get();
108 // Check if the clientlist is hidden
110 config
->Read( wxT("/GUI/TransferWnd/ShowClientList"), &show
, true );
113 // Disable the client-list
114 wxCommandEvent event
;
115 OnToggleClientList( event
);
118 // Load the last used splitter position
119 m_splitter
= config
->Read( wxT("/GUI/TransferWnd/Splitter"), 463l );
123 CTransferWnd::~CTransferWnd()
125 wxConfigBase
*config
= wxConfigBase::Get();
127 if ( !clientlistctrl
->GetShowing() ) {
128 // Save the splitter position
129 config
->Write( wxT("/GUI/TransferWnd/Splitter"), m_splitter
);
131 // Save the visible status of the list
132 config
->Write( wxT("/GUI/TransferWnd/ShowClientList"), false );
134 wxSplitterWindow
* splitter
= CastChild( wxT("splitterWnd"), wxSplitterWindow
);
136 // Save the splitter position
137 config
->Write( wxT("/GUI/TransferWnd/Splitter"), splitter
->GetSashPosition() );
139 // Save the visible status of the list
140 config
->Write( wxT("/GUI/TransferWnd/ShowClientList"), true );
145 void CTransferWnd::AddCategory( Category_Struct
* category
)
147 // Add the initial page
148 m_dlTab
->AddPage( new wxPanel(m_dlTab
), category
->title
);
151 UpdateCategory( m_dlTab
->GetPageCount() - 1 );
153 theApp
->amuledlg
->m_searchwnd
->UpdateCatChoice();
156 void CTransferWnd::UpdateCategory( int index
, bool titleChanged
)
158 wxString label
= theApp
->glob_prefs
->GetCategory( index
)->title
;
160 if ( thePrefs::ShowCatTabInfos() ) {
164 for ( unsigned int i
= 0; i
< theApp
->downloadqueue
->GetFileCount(); ++i
) {
165 CPartFile
*cur_file
= theApp
->downloadqueue
->GetFileByIndex(i
);
167 if ( cur_file
&& cur_file
->CheckShowItemInGivenCat(index
) ) {
170 if ( cur_file
->GetTransferingSrcCount() ) {
176 label
+= wxString::Format(wxT(" (%u/%u)"), download
, files
);
179 m_dlTab
->SetPageText( index
, label
);
182 if ( titleChanged
) {
183 theApp
->amuledlg
->m_searchwnd
->UpdateCatChoice();
189 void CTransferWnd::OnSetCatStatus( wxCommandEvent
& event
)
191 if ( event
.GetId() == MP_CANCEL
) {
192 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
) {
197 CoreNotify_Download_Set_Cat_Status( m_dlTab
->GetSelection(), event
.GetId() );
201 void CTransferWnd::OnSetCatPriority( wxCommandEvent
& event
)
205 switch ( event
.GetId() ) {
206 case MP_PRIOLOW
: priority
= PR_LOW
; break;
207 case MP_PRIONORMAL
: priority
= PR_NORMAL
; break;
208 case MP_PRIOHIGH
: priority
= PR_HIGH
; break;
209 case MP_PRIOAUTO
: priority
= PR_AUTO
; break;
214 CoreNotify_Download_Set_Cat_Prio( m_dlTab
->GetSelection(), priority
);
218 void CTransferWnd::OnAddCategory(wxCommandEvent
& WXUNUSED(event
))
220 CCatDialog
dialog( this,
228 if (dialog
.ShowModal() == wxOK
) {
229 // Add the files on this folder.
230 Category_Struct
* newcat
=
231 theApp
->glob_prefs
->GetCategory(
232 theApp
->glob_prefs
->GetCatCount()-1);
233 theApp
->sharedfiles
->AddFilesFromDirectory(newcat
->path
);
234 theApp
->sharedfiles
->Reload();
239 void CTransferWnd::OnDelCategory(wxCommandEvent
& WXUNUSED(event
))
241 RemoveCategory(m_dlTab
->GetSelection());
245 void CTransferWnd::RemoveCategory(int index
)
248 theApp
->downloadqueue
->ResetCatParts(index
);
249 theApp
->glob_prefs
->RemoveCat(index
);
250 RemoveCategoryPage(index
);
251 if ( theApp
->glob_prefs
->GetCatCount() == 1 ) {
252 thePrefs::SetAllcatType(0);
254 theApp
->glob_prefs
->SaveCats();
255 theApp
->amuledlg
->m_searchwnd
->UpdateCatChoice();
260 void CTransferWnd::RemoveCategoryPage(int index
)
262 m_dlTab
->RemovePage(index
);
263 m_dlTab
->SetSelection(0);
264 downloadlistctrl
->ChangeCategory(0);
268 void CTransferWnd::OnEditCategory( wxCommandEvent
& WXUNUSED(event
) )
270 Category_Struct
* cat
= theApp
->glob_prefs
->GetCategory(m_dlTab
->GetSelection());
271 CPath oldpath
= cat
->path
;
272 CCatDialog
dialog( this,
279 , m_dlTab
->GetSelection());
281 if (dialog
.ShowModal() == wxOK
) {
282 if (!oldpath
.IsSameDir(cat
->path
)) {
283 theApp
->sharedfiles
->AddFilesFromDirectory(cat
->path
);
284 theApp
->sharedfiles
->Reload();
290 void CTransferWnd::OnSetDefaultCat( wxCommandEvent
& event
)
292 thePrefs::SetAllcatType( event
.GetId() - MP_CAT_SET0
);
293 theApp
->glob_prefs
->GetCategory(0)->title
= GetCatTitle( thePrefs::GetAllcatType() );
297 downloadlistctrl
->ChangeCategory( 0 );
299 theApp
->glob_prefs
->SaveCats();
301 downloadlistctrl
->SortList();
305 void CTransferWnd::ShowQueueCount(uint32 number
)
308 wxString str
= wxString::Format( wxT("%u (%u %s)"), number
, theStats::GetBannedCount(), _("Banned") );
309 wxStaticText
* label
= CastChild( ID_CLIENTCOUNT
, wxStaticText
);
311 label
->SetLabel( str
);
312 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
)
330 if ( m_dlTab
->GetSelection() == -1 ) {
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());
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
->GetShowing() ) {
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 ) {
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
);
434 void CTransferWnd::OnToggleClientList(wxCommandEvent
& WXUNUSED(evt
))
436 wxSplitterWindow
* splitter
= CastChild( wxT("splitterWnd"), wxSplitterWindow
);
437 wxBitmapButton
* button
= CastChild( ID_CLIENTTOGGLE
, wxBitmapButton
);
439 if ( !clientlistctrl
->GetShowing() ) {
440 splitter
->SetSashPosition( m_splitter
);
442 clientlistctrl
->SetShowing( true );
444 button
->SetBitmapLabel( amuleDlgImages( 10 ) );
445 button
->SetBitmapFocus( amuleDlgImages( 10 ) );
446 button
->SetBitmapSelected( amuleDlgImages( 10 ) );
450 clientlistctrl
->SetShowing( false );
452 int pos
= splitter
->GetSashPosition();
454 // Add the height of the listctrl to the top-window
455 int height
= clientlistctrl
->GetSize().GetHeight();
456 height
+= splitter
->GetWindow1()->GetSize().GetHeight();
458 splitter
->SetSashPosition( height
);
462 button
->SetBitmapLabel( amuleDlgImages( 11 ) );
463 button
->SetBitmapFocus( amuleDlgImages( 11 ) );
464 button
->SetBitmapSelected( amuleDlgImages( 11 ) );
469 void CTransferWnd::OnSashPositionChanging(wxSplitterEvent
& evt
)
471 int header_height
= s_clientlistHeader
->GetSize().GetHeight();
473 if ( evt
.GetSashPosition() < 90 ) {
474 evt
.SetSashPosition( 90 );
476 wxSplitterWindow
* splitter
= wxStaticCast( evt
.GetEventObject(), wxSplitterWindow
);
478 int height
= splitter
->GetWindow1()->GetSize().GetHeight();
479 height
+= splitter
->GetWindow2()->GetSize().GetHeight();
481 if ( !clientlistctrl
->GetShowing() ) {
482 if ( height
- evt
.GetSashPosition() < header_height
* 2 ) {
485 wxCommandEvent event
;
486 OnToggleClientList( event
);
489 if ( height
- evt
.GetSashPosition() < header_height
* 2 ) {
490 evt
.SetSashPosition( height
- header_height
);
492 wxCommandEvent event
;
493 OnToggleClientList( event
);
498 // File_checked_for_headers