Upstream tarball 10143
[amule.git] / src / SharedFilesWnd.cpp
blobf1cf7b83a8d4963a28c270746bd5b935c7d64778
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-2008 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 <wx/config.h>
27 #include <wx/gauge.h> // Do_not_auto_remove (win32)
29 #include "SharedFilesWnd.h" // Interface declarations
30 #include "SharedFilesCtrl.h"
31 #include "SharedFilePeersListCtrl.h"
32 #include "muuli_wdr.h" // Needed for ID_SHFILELIST
33 #include "KnownFileList.h" // Needed for CKnownFileList
34 #include "KnownFile.h" // Needed for CKnownFile
35 #include "amule.h" // Needed for theApp
38 BEGIN_EVENT_TABLE(CSharedFilesWnd, wxPanel)
39 EVT_LIST_ITEM_SELECTED( ID_SHFILELIST, CSharedFilesWnd::OnItemSelectionChanged )
40 EVT_LIST_ITEM_DESELECTED( ID_SHFILELIST, CSharedFilesWnd::OnItemSelectionChanged )
41 EVT_BUTTON( ID_BTNRELSHARED, CSharedFilesWnd::OnBtnReloadShared )
43 EVT_SPLITTER_SASH_POS_CHANGING(ID_SHARESSPLATTER, CSharedFilesWnd::OnSashPositionChanging)
44 END_EVENT_TABLE()
46 CSharedFilesWnd::CSharedFilesWnd( wxWindow* pParent )
47 : wxPanel(pParent, -1)
49 wxSizer* content = sharedfilesDlg(this, true);
50 content->Show(this, true);
52 m_bar_requests = CastChild( wxT("popbar"), wxGauge );
53 m_bar_accepted = CastChild( wxT("popbarAccept"), wxGauge );
54 m_bar_transfer = CastChild( wxT("popbarTrans"), wxGauge );
55 sharedfilesctrl = CastChild( wxT("sharedFilesCt"), CSharedFilesCtrl );
56 peerslistctrl = CastChild( ID_SHAREDCLIENTLIST, CSharedFilePeersListCtrl );
57 wxASSERT(sharedfilesctrl);
58 wxASSERT(peerslistctrl);
60 m_splitter = 0;
62 wxConfigBase *config = wxConfigBase::Get();
64 // Check if the clientlist is hidden
65 bool show = true;
66 config->Read( wxT("/GUI/SharedWnd/ShowClientList"), &show, true );
68 if ( !show ) {
69 // Disable the client-list
70 wxCommandEvent event;
71 OnToggleClientList( event );
74 // Load the last used splitter position
75 m_splitter = config->Read( wxT("/GUI/SharedWnd/Splitter"), 463l );
79 CSharedFilesWnd::~CSharedFilesWnd()
81 wxConfigBase *config = wxConfigBase::Get();
83 if ( !peerslistctrl->GetShowing() ) {
84 // Save the splitter position
85 config->Write( wxT("/GUI/SharedWnd/Splitter"), m_splitter );
87 // Save the visible status of the list
88 config->Write( wxT("/GUI/SharedWnd/ShowClientList"), false );
89 } else {
90 wxSplitterWindow* splitter = CastChild( wxT("sharedsplitterWnd"), wxSplitterWindow );
92 // Save the splitter position
93 config->Write( wxT("/GUI/SharedWnd/Splitter"), splitter->GetSashPosition() );
95 // Save the visible status of the list
96 config->Write( wxT("/GUI/SharedWnd/ShowClientList"), true );
101 void CSharedFilesWnd::SelectionUpdated()
103 if (!sharedfilesctrl->IsSorting()) {
104 uint64 lTransferred = theApp->knownfiles->transferred;
105 uint32 lAccepted = theApp->knownfiles->accepted;
106 uint32 lRequested = theApp->knownfiles->requested;
107 m_bar_requests->SetRange( lRequested );
108 m_bar_accepted->SetRange( lAccepted );
109 m_bar_transfer->SetRange( lTransferred / 1024 );
111 if ( !sharedfilesctrl->GetSelectedItemCount() ) {
112 // Requests
113 m_bar_requests->SetValue( 0 );
114 CastChild(IDC_SREQUESTED, wxStaticText)->SetLabel( wxT("- /") );
115 CastChild(IDC_SREQUESTED2, wxStaticText)->SetLabel( wxT("- /") );
117 // Accepted requets
118 m_bar_accepted->SetValue( 0 );
119 CastChild(IDC_SACCEPTED, wxStaticText)->SetLabel( wxT("- /") );
120 CastChild(IDC_SACCEPTED2, wxStaticText)->SetLabel( wxT("- /") );
122 // Transferred
123 m_bar_transfer->SetValue( 0 );
124 CastChild(IDC_STRANSFERRED, wxStaticText)->SetLabel( wxT("- /") );
125 CastChild(IDC_STRANSFERRED2, wxStaticText)->SetLabel( wxT("- /") );
126 } else {
127 // Create a total statistic for the selected item(s)
128 uint32 session_requests = 0;
129 uint32 session_accepted = 0;
130 uint64 session_transferred = 0;
131 uint32 all_requests = 0;
132 uint32 all_accepted = 0;
133 uint64 all_transferred = 0;
135 long index = -1;
136 CKnownFileVector fileVector;
137 fileVector.reserve(sharedfilesctrl->GetSelectedItemCount());
139 while ( (index = sharedfilesctrl->GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED )) != -1) {
140 if ( sharedfilesctrl->GetItemState( index, wxLIST_STATE_SELECTED ) ) {
141 CKnownFile* file = (CKnownFile*)sharedfilesctrl->GetItemData( index );
142 wxASSERT(file);
144 session_requests += file->statistic.GetRequests();
145 session_accepted += file->statistic.GetAccepts();
146 session_transferred += file->statistic.GetTransferred();
148 all_requests += file->statistic.GetAllTimeRequests();
149 all_accepted += file->statistic.GetAllTimeAccepts();
150 all_transferred += file->statistic.GetAllTimeTransferred();
152 fileVector.push_back(file);
156 std::sort(fileVector.begin(), fileVector.end());
157 this->peerslistctrl->ShowSources(fileVector);
159 // Requests
160 session_requests = session_requests > lRequested ? lRequested : session_requests;
161 m_bar_requests->SetValue( session_requests );
162 CastChild(IDC_SREQUESTED, wxStaticText)->SetLabel( wxString::Format(wxT("%u /"), session_requests ) );
163 CastChild(IDC_SREQUESTED2, wxStaticText)->SetLabel( wxString::Format(wxT("%u /"), all_requests ) );
165 // Accepted requets
166 session_accepted = session_accepted > lAccepted ? lAccepted : session_accepted;
167 m_bar_accepted->SetValue( session_accepted );
168 CastChild(IDC_SACCEPTED, wxStaticText)->SetLabel( wxString::Format(wxT("%u /"), session_accepted ) );
169 CastChild(IDC_SACCEPTED2, wxStaticText)->SetLabel( wxString::Format(wxT("%u /"), all_accepted ) );
171 // Transferred
172 session_transferred = session_transferred > lTransferred ? lTransferred : session_transferred;
173 m_bar_transfer->SetValue( session_transferred / 1024 );
174 CastChild(IDC_STRANSFERRED, wxStaticText)->SetLabel( CastItoXBytes( session_transferred ) + wxT(" /"));
175 CastChild(IDC_STRANSFERRED2, wxStaticText)->SetLabel( CastItoXBytes( all_transferred ) + wxT(" /"));
178 Layout();
183 void CSharedFilesWnd::OnBtnReloadShared( wxCommandEvent& WXUNUSED(evt) )
185 theApp->sharedfiles->Reload();
186 #ifndef CLIENT_GUI
187 // remote gui will update display when data is back
188 SelectionUpdated();
189 #endif
193 void CSharedFilesWnd::OnItemSelectionChanged(wxListEvent& evt)
195 SelectionUpdated();
197 evt.Skip();
201 void CSharedFilesWnd::RemoveAllSharedFiles() {
202 sharedfilesctrl->DeleteAllItems();
203 sharedfilesctrl->ShowFilesCount();
204 peerslistctrl->DeleteAllItems();
205 SelectionUpdated();
208 void CSharedFilesWnd::OnToggleClientList(wxCommandEvent& WXUNUSED(evt))
210 wxSplitterWindow* splitter = CastChild( wxT("sharedsplitterWnd"), wxSplitterWindow );
211 wxBitmapButton* button = CastChild( ID_SHAREDCLIENTTOGGLE, wxBitmapButton );
213 wxCHECK_RET(splitter, wxT("ERROR: NULL splitter in CSharedFilesWnd::OnToggleClientList"));
214 wxCHECK_RET(button, wxT("ERROR: NULL button in CSharedFilesWnd::OnToggleClientList"));
216 if ( !peerslistctrl->GetShowing() ) {
217 splitter->SetSashPosition( m_splitter );
219 peerslistctrl->SetShowing( true );
221 button->SetBitmapLabel( amuleDlgImages( 10 ) );
222 button->SetBitmapFocus( amuleDlgImages( 10 ) );
223 button->SetBitmapSelected( amuleDlgImages( 10 ) );
225 m_splitter = 0;
226 } else {
227 peerslistctrl->SetShowing( false );
229 int pos = splitter->GetSashPosition();
231 // Add the height of the listctrl to the top-window
232 int height = peerslistctrl->GetSize().GetHeight();
233 height += splitter->GetWindow1()->GetSize().GetHeight();
235 splitter->SetSashPosition( height );
237 m_splitter = pos;
239 button->SetBitmapLabel( amuleDlgImages( 11 ) );
240 button->SetBitmapFocus( amuleDlgImages( 11 ) );
241 button->SetBitmapSelected( amuleDlgImages( 11 ) );
245 void CSharedFilesWnd::OnSashPositionChanging(wxSplitterEvent& evt)
247 if ( evt.GetSashPosition() < 90 ) {
248 evt.SetSashPosition( 90 );
249 } else {
250 wxSplitterWindow* splitter = wxStaticCast( evt.GetEventObject(), wxSplitterWindow);
252 wxCHECK_RET(splitter, wxT("ERROR: NULL splitter in CSharedFilesWnd::OnSashPositionChanging"));
254 int height = splitter->GetSize().GetHeight();
256 int header_height = s_sharedfilespeerHeader->GetSize().GetHeight();
258 if ( !peerslistctrl->GetShowing() ) {
259 if ( height - evt.GetSashPosition() < header_height * 1.5 ) {
260 evt.Veto();
261 } else {
262 wxCommandEvent event;
263 OnToggleClientList( event );
265 } else {
266 if ( height - evt.GetSashPosition() < header_height * 1.5 ) {
267 evt.SetSashPosition( height - header_height );
269 wxCommandEvent event;
270 OnToggleClientList( event );
275 // File_checked_for_headers