Upstream tarball 9882
[amule.git] / src / SharedFilesCtrl.cpp
blob0b8e1708ae32172d162109475fd55a5884009247
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 "SharedFilesCtrl.h" // Interface declarations
28 #include <common/MenuIDs.h>
30 #include "muuli_wdr.h" // Needed for ID_SHFILELIST
31 #include "SharedFilesWnd.h" // Needed for CSharedFilesWnd
32 #include "amuleDlg.h" // Needed for CamuleDlg
33 #include "CommentDialog.h" // Needed for CCommentDialog
34 #include "PartFile.h" // Needed for CPartFile
35 #include "SharedFileList.h" // Needed for CKnownFileMap
36 #include "amule.h" // Needed for theApp
37 #include "ServerConnect.h" // Needed for CServerConnect
38 #include "Preferences.h" // Needed for thePrefs
39 #include "BarShader.h" // Needed for CBarShader
40 #include "DataToText.h" // Needed for PriorityToStr
41 #include "GuiEvents.h" // Needed for CoreNotify_*
42 #include "MuleCollection.h" // Needed for CMuleCollection
43 #include "DownloadQueue.h" // Needed for CDownloadQueue
44 #include "TransferWnd.h" // Needed for CTransferWnd
47 BEGIN_EVENT_TABLE(CSharedFilesCtrl,CMuleListCtrl)
48 EVT_LIST_ITEM_RIGHT_CLICK(-1, CSharedFilesCtrl::OnRightClick)
50 EVT_MENU( MP_PRIOVERYLOW, CSharedFilesCtrl::OnSetPriority )
51 EVT_MENU( MP_PRIOLOW, CSharedFilesCtrl::OnSetPriority )
52 EVT_MENU( MP_PRIONORMAL, CSharedFilesCtrl::OnSetPriority )
53 EVT_MENU( MP_PRIOHIGH, CSharedFilesCtrl::OnSetPriority )
54 EVT_MENU( MP_PRIOVERYHIGH, CSharedFilesCtrl::OnSetPriority )
55 EVT_MENU( MP_POWERSHARE, CSharedFilesCtrl::OnSetPriority )
56 EVT_MENU( MP_PRIOAUTO, CSharedFilesCtrl::OnSetPriorityAuto )
58 EVT_MENU( MP_CMT, CSharedFilesCtrl::OnEditComment )
59 EVT_MENU( MP_RAZORSTATS, CSharedFilesCtrl::OnGetRazorStats )
60 EVT_MENU( MP_ADDCOLLECTION, CSharedFilesCtrl::OnAddCollection )
61 EVT_MENU( MP_GETMAGNETLINK, CSharedFilesCtrl::OnCreateURI )
62 EVT_MENU( MP_GETED2KLINK, CSharedFilesCtrl::OnCreateURI )
63 EVT_MENU( MP_GETSOURCEED2KLINK, CSharedFilesCtrl::OnCreateURI )
64 EVT_MENU( MP_GETCRYPTSOURCEDED2KLINK, CSharedFilesCtrl::OnCreateURI )
65 EVT_MENU( MP_GETHOSTNAMESOURCEED2KLINK, CSharedFilesCtrl::OnCreateURI )
66 EVT_MENU( MP_GETHOSTNAMECRYPTSOURCEED2KLINK, CSharedFilesCtrl::OnCreateURI )
67 EVT_MENU( MP_GETAICHED2KLINK, CSharedFilesCtrl::OnCreateURI )
68 EVT_MENU( MP_RENAME, CSharedFilesCtrl::OnRename )
69 EVT_MENU( MP_WS, CSharedFilesCtrl::OnGetFeedback )
72 EVT_CHAR( CSharedFilesCtrl::OnKeyPressed )
73 END_EVENT_TABLE()
75 enum SharedFilesListColumns {
76 ID_SHARED_COL_NAME = 0,
77 ID_SHARED_COL_SIZE,
78 ID_SHARED_COL_TYPE,
79 ID_SHARED_COL_PRIO,
80 ID_SHARED_COL_ID,
81 ID_SHARED_COL_REQ,
82 ID_SHARED_COL_AREQ,
83 ID_SHARED_COL_TRA,
84 ID_SHARED_COL_RTIO,
85 ID_SHARED_COL_PART,
86 ID_SHARED_COL_CMPL,
87 ID_SHARED_COL_PATH
91 CSharedFilesCtrl::CSharedFilesCtrl(wxWindow* parent, int id, const wxPoint& pos, wxSize size, int flags)
92 : CMuleListCtrl(parent, id, pos, size, flags | wxLC_OWNERDRAW )
94 // Setting the sorter function.
95 SetSortFunc( SortProc );
97 // Set the table-name (for loading and saving preferences).
98 SetTableName( wxT("Shared") );
100 m_menu=NULL;
102 InsertColumn(ID_SHARED_COL_NAME, _("File Name"), wxLIST_FORMAT_LEFT, 250, wxT("N") );
103 InsertColumn(ID_SHARED_COL_SIZE, _("Size"), wxLIST_FORMAT_LEFT, 100, wxT("Z") );
104 InsertColumn(ID_SHARED_COL_TYPE, _("Type"), wxLIST_FORMAT_LEFT, 50, wxT("Y") );
105 InsertColumn(ID_SHARED_COL_PRIO, _("Priority"), wxLIST_FORMAT_LEFT, 70, wxT("p") );
106 InsertColumn(ID_SHARED_COL_ID, _("FileID"), wxLIST_FORMAT_LEFT, 220, wxT("I") );
107 InsertColumn(ID_SHARED_COL_REQ, _("Requests"), wxLIST_FORMAT_LEFT, 100, wxT("Q") );
108 InsertColumn(ID_SHARED_COL_AREQ, _("Accepted Requests"), wxLIST_FORMAT_LEFT, 100, wxT("A") );
109 InsertColumn(ID_SHARED_COL_TRA, _("Transferred Data"), wxLIST_FORMAT_LEFT, 120, wxT("T") );
110 InsertColumn(ID_SHARED_COL_RTIO, _("Share Ratio"), wxLIST_FORMAT_LEFT, 100, wxT("R") );
111 InsertColumn(ID_SHARED_COL_PART, _("Obtained Parts"), wxLIST_FORMAT_LEFT, 120, wxT("P") );
112 InsertColumn(ID_SHARED_COL_CMPL, _("Complete Sources"), wxLIST_FORMAT_LEFT, 120, wxT("C") );
113 InsertColumn(ID_SHARED_COL_PATH, _("Directory Path"), wxLIST_FORMAT_LEFT, 220, wxT("D") );
115 LoadSettings();
119 wxString CSharedFilesCtrl::GetOldColumnOrder() const
121 return wxT("N,Z,Y,p,I,Q,A,T,R,P,C,D");
125 CSharedFilesCtrl::~CSharedFilesCtrl()
130 void CSharedFilesCtrl::OnRightClick(wxListEvent& event)
132 long item_hit = CheckSelection(event);
134 if ( (m_menu == NULL) && (item_hit != -1)) {
135 m_menu = new wxMenu(_("Shared Files"));
136 wxMenu* prioMenu = new wxMenu();
137 prioMenu->AppendCheckItem(MP_PRIOVERYLOW, _("Very low"));
138 prioMenu->AppendCheckItem(MP_PRIOLOW, _("Low"));
139 prioMenu->AppendCheckItem(MP_PRIONORMAL, _("Normal"));
140 prioMenu->AppendCheckItem(MP_PRIOHIGH, _("High"));
141 prioMenu->AppendCheckItem(MP_PRIOVERYHIGH, _("Very High"));
142 prioMenu->AppendCheckItem(MP_POWERSHARE, _("Release"));
143 prioMenu->AppendCheckItem(MP_PRIOAUTO, _("Auto"));
145 m_menu->Append(0,_("Priority"),prioMenu);
146 m_menu->AppendSeparator();
148 CKnownFile* file = (CKnownFile*)GetItemData(item_hit);
149 if (file->GetFileComment().IsEmpty() && !file->GetFileRating()) {
150 m_menu->Append(MP_CMT, _("Add Comment/Rating"));
151 } else {
152 m_menu->Append(MP_CMT, _("Edit Comment/Rating"));
155 m_menu->AppendSeparator();
156 m_menu->Append(MP_RENAME, _("Rename"));
157 m_menu->AppendSeparator();
158 /* Commented out while it's gone
159 m_menu->Append( MP_RAZORSTATS, _("Get Razorback 2's stats for this file"));
160 m_menu->AppendSeparator();
162 if (file->GetFileName().GetExt() == wxT("emulecollection")) {
163 m_menu->Append( MP_ADDCOLLECTION, _("Add files in collection to transfer list"));
164 m_menu->AppendSeparator();
166 m_menu->Append(MP_GETMAGNETLINK,_("Copy magnet &URI to clipboard"));
167 m_menu->Append(MP_GETED2KLINK,_("Copy eD2k &link to clipboard"));
168 m_menu->Append(MP_GETSOURCEED2KLINK,_("Copy eD2k link to clipboard (&Source)"));
169 m_menu->Append(MP_GETCRYPTSOURCEDED2KLINK,_("Copy eD2k link to clipboard (Source) (&With Crypt options)"));
170 m_menu->Append(MP_GETHOSTNAMESOURCEED2KLINK,_("Copy eD2k link to clipboard (&Hostname)"));
171 m_menu->Append(MP_GETHOSTNAMECRYPTSOURCEED2KLINK,_("Copy eD2k link to clipboard (Hostname) (With &Crypt options)"));
172 m_menu->Append(MP_GETAICHED2KLINK,_("Copy eD2k link to clipboard (&AICH info)"));
173 m_menu->Append(MP_WS,_("Copy feedback to clipboard"));
175 m_menu->Enable(MP_GETAICHED2KLINK, file->HasProperAICHHashSet());
176 m_menu->Enable(MP_GETHOSTNAMESOURCEED2KLINK, !thePrefs::GetYourHostname().IsEmpty());
177 m_menu->Enable(MP_GETHOSTNAMECRYPTSOURCEED2KLINK, !thePrefs::GetYourHostname().IsEmpty());
178 m_menu->Enable(MP_RENAME, file->IsPartFile());
180 int priority = file->IsAutoUpPriority() ? PR_AUTO : file->GetUpPriority();
182 prioMenu->Check(MP_PRIOVERYLOW, priority == PR_VERYLOW);
183 prioMenu->Check(MP_PRIOLOW, priority == PR_LOW);
184 prioMenu->Check(MP_PRIONORMAL, priority == PR_NORMAL);
185 prioMenu->Check(MP_PRIOHIGH, priority == PR_HIGH);
186 prioMenu->Check(MP_PRIOVERYHIGH,priority == PR_VERYHIGH);
187 prioMenu->Check(MP_POWERSHARE, priority == PR_POWERSHARE);
188 prioMenu->Check(MP_PRIOAUTO, priority == PR_AUTO);
190 PopupMenu( m_menu, event.GetPoint() );
192 delete m_menu;
194 m_menu = NULL;
199 void CSharedFilesCtrl::OnGetFeedback(wxCommandEvent& WXUNUSED(event))
201 wxString feed;
202 long index = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
203 while (index != -1) {
204 if (feed.IsEmpty()) {
205 feed = CFormat(_("Feedback from: %s (%s)\n\n")) % thePrefs::GetUserNick() % GetFullMuleVersion();
206 } else {
207 feed += wxT("\n");
209 feed += ((CKnownFile*)GetItemData(index))->GetFeedback();
210 index = GetNextItem(index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
213 if (!feed.IsEmpty()) {
214 theApp->CopyTextToClipboard(feed);
219 #ifndef CLIENT_GUI
220 void CSharedFilesCtrl::ShowFileList()
222 Freeze();
223 DeleteAllItems();
225 std::vector<CKnownFile*> files;
226 theApp->sharedfiles->CopyFileList(files);
227 for (unsigned i = 0; i < files.size(); ++i) {
228 DoShowFile(files[i], true);
231 SortList();
232 ShowFilesCount();
234 Thaw();
236 #endif
239 void CSharedFilesCtrl::RemoveFile(CKnownFile *toRemove)
241 long index = FindItem( -1, reinterpret_cast<wxUIntPtr>(toRemove) );
243 if ( index != -1 ) {
244 DeleteItem( index );
246 ShowFilesCount();
251 void CSharedFilesCtrl::ShowFile(CKnownFile* file)
253 DoShowFile(file, false);
257 void CSharedFilesCtrl::DoShowFile(CKnownFile* file, bool batch)
259 wxUIntPtr ptr = reinterpret_cast<wxUIntPtr>(file);
260 if ((!batch) && (FindItem(-1, ptr) > -1)) {
261 return;
264 const long insertPos = (batch ? GetItemCount() : GetInsertPos(ptr));
266 long newitem = InsertItem(insertPos, wxEmptyString);
267 SetItemPtrData( newitem, ptr );
269 if (!batch) {
270 ShowFilesCount();
274 void CSharedFilesCtrl::OnSetPriority( wxCommandEvent& event )
276 int priority = 0;
278 switch ( event.GetId() ) {
279 case MP_PRIOVERYLOW: priority = PR_VERYLOW; break;
280 case MP_PRIOLOW: priority = PR_LOW; break;
281 case MP_PRIONORMAL: priority = PR_NORMAL; break;
282 case MP_PRIOHIGH: priority = PR_HIGH; break;
283 case MP_PRIOVERYHIGH: priority = PR_VERYHIGH; break;
284 case MP_POWERSHARE: priority = PR_POWERSHARE; break;
287 long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
289 while( index != -1 ) {
290 CKnownFile* file = (CKnownFile*)GetItemData( index );
291 CoreNotify_KnownFile_Up_Prio_Set( file, priority );
293 RefreshItem( index );
295 index = GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
300 void CSharedFilesCtrl::OnSetPriorityAuto( wxCommandEvent& WXUNUSED(event) )
302 long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
304 while( index != -1 ) {
305 CKnownFile* file = (CKnownFile*)GetItemData( index );
306 CoreNotify_KnownFile_Up_Prio_Auto(file);
308 RefreshItem( index );
310 index = GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
315 void CSharedFilesCtrl::OnCreateURI( wxCommandEvent& event )
317 wxString URIs;
319 if ( event.GetId() == MP_GETSOURCEED2KLINK || event.GetId() == MP_GETCRYPTSOURCEDED2KLINK) {
320 if ( !theApp->IsConnectedED2K() || theApp->serverconnect->IsLowID() ) {
321 wxMessageBox(_("You need a HighID to create a valid sourcelink"), _("WARNING"), wxOK | wxICON_ERROR, this);
323 return;
327 long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
329 while( index != -1 ) {
330 CKnownFile* file = (CKnownFile*)GetItemData( index );
332 switch ( event.GetId() ) {
333 case MP_GETMAGNETLINK: URIs += theApp->CreateMagnetLink( file ) + wxT("\n"); break;
334 case MP_GETED2KLINK: URIs += theApp->CreateED2kLink( file ) + wxT("\n"); break;
335 case MP_GETSOURCEED2KLINK: URIs += theApp->CreateED2kLink( file , true) + wxT("\n"); break;
336 case MP_GETCRYPTSOURCEDED2KLINK: URIs += theApp->CreateED2kLink( file , true, false, true) + wxT("\n"); break;
337 case MP_GETHOSTNAMESOURCEED2KLINK: URIs += theApp->CreateED2kLink( file , true, true) + wxT("\n"); break;
338 case MP_GETHOSTNAMECRYPTSOURCEED2KLINK: URIs += theApp->CreateED2kLink( file, true, true, true ) + wxT("\n"); break;
339 case MP_GETAICHED2KLINK: URIs += theApp->CreateED2kAICHLink( file ) + wxT("\n"); break;
342 index = GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
345 if ( !URIs.IsEmpty() ) {
346 theApp->CopyTextToClipboard( URIs.RemoveLast() );
351 void CSharedFilesCtrl::OnEditComment( wxCommandEvent& WXUNUSED(event) )
353 long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
355 if ( index != -1 ) {
356 CKnownFile* file = (CKnownFile*)GetItemData( index );
358 CCommentDialog dialog( this, file );
360 dialog.ShowModal();
365 int CSharedFilesCtrl::SortProc(wxUIntPtr item1, wxUIntPtr item2, long sortData)
367 CKnownFile* file1 = (CKnownFile*)item1;
368 CKnownFile* file2 = (CKnownFile*)item2;
370 int mod = (sortData & CMuleListCtrl::SORT_DES) ? -1 : 1;
371 bool altSorting = (sortData & CMuleListCtrl::SORT_ALT) > 0;
373 switch (sortData & CMuleListCtrl::COLUMN_MASK) {
374 // Sort by filename.
375 case ID_SHARED_COL_NAME:
376 return mod * CmpAny(file1->GetFileName(), file2->GetFileName());
378 // Sort by filesize.
379 case ID_SHARED_COL_SIZE:
380 return mod * CmpAny( file1->GetFileSize(), file2->GetFileSize() );
382 // Sort by filetype.
383 case ID_SHARED_COL_TYPE:
384 return mod * GetFiletypeByName(file1->GetFileName()).CmpNoCase(GetFiletypeByName( file2->GetFileName()) );
386 // Sort by priority.
387 case ID_SHARED_COL_PRIO: {
388 int8 prioA = file1->GetUpPriority();
389 int8 prioB = file2->GetUpPriority();
391 // Work-around for PR_VERYLOW which has value 4. See KnownFile.h for that stupidity ...
392 return mod * CmpAny( ( prioB != PR_VERYLOW ? prioB : -1 ), ( prioA != PR_VERYLOW ? prioA : -1 ) );
395 // Sort by fileID.
396 case ID_SHARED_COL_ID:
397 return mod * file1->GetFileHash().Encode().Cmp( file2->GetFileHash().Encode() );
399 // Sort by Requests this session.
400 case ID_SHARED_COL_REQ:
401 if (altSorting) {
402 return mod * CmpAny( file1->statistic.GetAllTimeRequests(), file2->statistic.GetAllTimeRequests() );
403 } else {
404 return mod * CmpAny( file1->statistic.GetRequests(), file2->statistic.GetRequests() );
407 // Sort by accepted requests. Ascending.
408 case ID_SHARED_COL_AREQ:
409 if (altSorting) {
410 return mod * CmpAny( file1->statistic.GetAllTimeAccepts(), file2->statistic.GetAllTimeAccepts() );
411 } else {
412 return mod * CmpAny( file1->statistic.GetAccepts(), file2->statistic.GetAccepts() );
415 // Sort by transferred. Ascending.
416 case ID_SHARED_COL_TRA:
417 if (altSorting) {
418 return mod * CmpAny( file1->statistic.GetAllTimeTransferred(), file2->statistic.GetAllTimeTransferred() );
419 } else {
420 return mod * CmpAny( file1->statistic.GetTransferred(), file2->statistic.GetTransferred() );
423 // Sort by Share Ratio. Ascending.
424 case ID_SHARED_COL_RTIO:
425 return mod * CmpAny( (double)file1->statistic.GetAllTimeTransferred() / file1->GetFileSize(),
426 (double)file2->statistic.GetAllTimeTransferred() / file2->GetFileSize() );
428 // Complete sources asc
429 case ID_SHARED_COL_CMPL:
430 return mod * CmpAny( file1->m_nCompleteSourcesCount, file2->m_nCompleteSourcesCount );
432 // Folders ascending
433 case ID_SHARED_COL_PATH: {
434 if ( file1->IsPartFile() && file2->IsPartFile() )
435 return mod * 0;
436 if ( file1->IsPartFile() )
437 return mod * -1;
438 if ( file2->IsPartFile() )
439 return mod * 1;
441 return mod * CmpAny(file1->GetFilePath(), file2->GetFilePath());
444 default:
445 return 0;
450 void CSharedFilesCtrl::UpdateItem(CKnownFile* toupdate)
452 long result = FindItem( -1, reinterpret_cast<wxUIntPtr>(toupdate) );
454 if ( result > -1 ) {
455 RefreshItem(result);
457 if ( GetItemState( result, wxLIST_STATE_SELECTED ) ) {
458 theApp->amuledlg->m_sharedfileswnd->SelectionUpdated();
464 void CSharedFilesCtrl::ShowFilesCount()
466 wxString str = wxString::Format(_("Shared Files (%i)"), GetItemCount());
467 wxStaticText* label = CastByName( wxT("sharedFilesLabel"), GetParent(), wxStaticText );
469 label->SetLabel( str );
470 label->GetParent()->Layout();
474 void CSharedFilesCtrl::OnDrawItem( int item, wxDC* dc, const wxRect& rect, const wxRect& rectHL, bool highlighted )
476 CKnownFile *file = (CKnownFile*)GetItemData(item);
477 wxASSERT( file );
479 if ( highlighted ) {
480 CMuleColour newcol(GetFocus() ? wxSYS_COLOUR_HIGHLIGHT : wxSYS_COLOUR_BTNSHADOW);
481 dc->SetBackground(newcol.Blend(125).GetBrush());
482 dc->SetTextForeground( CMuleColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
483 // The second blending goes over the first one.
484 dc->SetPen(newcol.Blend(65).GetPen());
485 } else {
486 dc->SetBackground( CMuleColour(wxSYS_COLOUR_LISTBOX).GetBrush() );
487 dc->SetTextForeground(CMuleColour(wxSYS_COLOUR_WINDOWTEXT));
488 dc->SetPen(*wxTRANSPARENT_PEN);
491 dc->SetBrush(dc->GetBackground());
492 dc->DrawRectangle(rectHL);
493 dc->SetPen(*wxTRANSPARENT_PEN);
495 // Offset based on the height of the fonts
496 const int textVOffset = ( rect.GetHeight() - dc->GetCharHeight() ) / 2;
497 // Empty space to each side of a column
498 const int SPARE_PIXELS_HORZ = 4;
500 // The leftmost position of the current column
501 int columnLeft = 0;
503 for ( int i = 0; i < GetColumnCount(); ++i ) {
504 const int columnWidth = GetColumnWidth(i);
506 if (columnWidth > 2*SPARE_PIXELS_HORZ) {
507 wxRect columnRect(
508 columnLeft + SPARE_PIXELS_HORZ, rect.y,
509 columnWidth - 2 * SPARE_PIXELS_HORZ, rect.height);
511 wxDCClipper clipper(*dc, columnRect);
513 wxString textBuffer;
514 switch ( i ) {
515 case ID_SHARED_COL_NAME:
516 textBuffer = file->GetFileName().GetPrintable();
518 if (file->GetFileRating() || file->GetFileComment().Length()) {
519 int image = Client_CommentOnly_Smiley;
520 if (file->GetFileRating()) {
521 image = Client_InvalidRating_Smiley + file->GetFileRating() - 1;
524 wxASSERT(image >= Client_InvalidRating_Smiley);
525 wxASSERT(image <= Client_CommentOnly_Smiley);
527 int imgWidth = 16;
529 theApp->amuledlg->m_imagelist.Draw(image, *dc, columnRect.x,
530 columnRect.y + 1, wxIMAGELIST_DRAW_TRANSPARENT);
532 // Move the text to the right
533 columnRect.x += (imgWidth + 4);
536 break;
538 case ID_SHARED_COL_SIZE:
539 textBuffer = CastItoXBytes(file->GetFileSize());
540 break;
542 case ID_SHARED_COL_TYPE:
543 textBuffer = GetFiletypeByName(file->GetFileName());
544 break;
546 case ID_SHARED_COL_PRIO:
547 textBuffer = PriorityToStr(file->GetUpPriority(), file->IsAutoUpPriority());
548 break;
550 case ID_SHARED_COL_ID:
551 textBuffer = file->GetFileHash().Encode();
552 break;
554 case ID_SHARED_COL_REQ:
555 textBuffer = wxString::Format(wxT("%u (%u)"),
556 file->statistic.GetRequests(),
557 file->statistic.GetAllTimeRequests());
558 break;
560 case ID_SHARED_COL_AREQ:
561 textBuffer = wxString::Format(wxT("%u (%u)"),
562 file->statistic.GetAccepts(),
563 file->statistic.GetAllTimeAccepts());
564 break;
566 case ID_SHARED_COL_TRA:
567 textBuffer = CastItoXBytes(file->statistic.GetTransferred())
568 + wxT(" (") + CastItoXBytes(file->statistic.GetAllTimeTransferred()) + wxT(")");
569 break;
571 case ID_SHARED_COL_RTIO:
572 textBuffer = wxString::Format(wxT("%.2f"),
573 (double)file->statistic.GetAllTimeTransferred() / file->GetFileSize() );
574 break;
576 case ID_SHARED_COL_PART:
577 if ( file->GetPartCount() ) {
578 wxRect barRect(columnRect.x, columnRect. y + 1,
579 columnRect.width, columnRect.height - 2);
581 DrawAvailabilityBar(file, dc, barRect);
583 break;
585 case ID_SHARED_COL_CMPL:
586 if ( file->m_nCompleteSourcesCountLo == 0 ) {
587 if ( file->m_nCompleteSourcesCountHi ) {
588 textBuffer = wxString::Format(wxT("< %u"), file->m_nCompleteSourcesCountHi );
589 } else {
590 textBuffer = wxT("0");
592 } else if (file->m_nCompleteSourcesCountLo == file->m_nCompleteSourcesCountHi) {
593 textBuffer = wxString::Format(wxT("%u"), file->m_nCompleteSourcesCountLo);
594 } else {
595 textBuffer = wxString::Format(wxT("%u - %u"),
596 file->m_nCompleteSourcesCountLo,
597 file->m_nCompleteSourcesCountHi);
600 break;
602 case ID_SHARED_COL_PATH:
603 if ( file->IsPartFile() ) {
604 textBuffer = _("[PartFile]");
605 } else {
606 textBuffer = file->GetFilePath().GetPrintable();
610 if (!textBuffer.IsEmpty()) {
611 dc->DrawText(textBuffer, columnRect.x, columnRect.y + textVOffset);
615 // Move to the next column
616 columnLeft += columnWidth;
621 wxString CSharedFilesCtrl::GetTTSText(unsigned item) const
623 return reinterpret_cast<CKnownFile*>(GetItemData(item))->GetFileName().GetPrintable();
627 bool CSharedFilesCtrl::AltSortAllowed(unsigned column) const
629 switch ( column ) {
630 case ID_SHARED_COL_REQ:
631 case ID_SHARED_COL_AREQ:
632 case ID_SHARED_COL_TRA:
633 return true;
635 default:
636 return false;
641 void CSharedFilesCtrl::DrawAvailabilityBar(CKnownFile* file, wxDC* dc, const wxRect& rect ) const
643 // Reference to the availability list
644 const ArrayOfUInts16& list = file->IsPartFile() ?
645 ((CPartFile*)file)->m_SrcpartFrequency :
646 file->m_AvailPartFrequency;
647 wxPen old_pen = dc->GetPen();
648 wxBrush old_brush = dc->GetBrush();
649 bool bFlat = thePrefs::UseFlatBar();
651 wxRect barRect = rect;
652 if (!bFlat) { // round bar has a black border, the bar itself is 1 pixel less on each border
653 barRect.x ++;
654 barRect.y ++;
655 barRect.height -= 2;
656 barRect.width -= 2;
658 static CBarShader s_ChunkBar;
659 s_ChunkBar.SetFileSize( file->GetFileSize() );
660 s_ChunkBar.SetHeight( barRect.GetHeight() );
661 s_ChunkBar.SetWidth( barRect.GetWidth() );
662 s_ChunkBar.Set3dDepth( CPreferences::Get3DDepth() );
663 uint64 end = 0;
664 for ( unsigned int i = 0; i < list.size(); ++i ) {
665 uint64 start = PARTSIZE * static_cast<uint64>(i);
666 end = PARTSIZE * static_cast<uint64>(i + 1);
667 s_ChunkBar.FillRange(start, end, CMuleColour(list[i] ? 0 : 255, list[i] ? ((210-(22*( list[i] - 1 ) ) < 0) ? 0 : (210-(22*( list[i] - 1 ) ))) : 0, list[i] ? 255 : 0));
669 s_ChunkBar.FillRange(end + 1, file->GetFileSize() - 1, CMuleColour(255, 0, 0));
670 s_ChunkBar.Draw(dc, barRect.x, barRect.y, bFlat);
672 if (!bFlat) {
673 // Draw black border
674 dc->SetPen( *wxBLACK_PEN );
675 dc->SetBrush( *wxTRANSPARENT_BRUSH );
676 dc->DrawRectangle(rect);
679 dc->SetPen( old_pen );
680 dc->SetBrush( old_brush );
683 void CSharedFilesCtrl::OnGetRazorStats( wxCommandEvent& WXUNUSED(event) )
685 int item = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
686 if ( item != -1 ) {
687 CKnownFile* file = (CKnownFile*)GetItemData( item );
689 theApp->amuledlg->LaunchUrl(wxT("http://stats.razorback2.com/ed2khistory?ed2k=") + file->GetFileHash().Encode());
694 void CSharedFilesCtrl::OnRename( wxCommandEvent& WXUNUSED(event) )
696 int item = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
697 if ( item != -1 ) {
698 CKnownFile* file = (CKnownFile*)GetItemData(item);
700 // Currently renaming of completed files causes problem with kad
701 if (file->IsPartFile()) {
702 wxString strNewName = ::wxGetTextFromUser(
703 _("Enter new name for this file:"),
704 _("File rename"), file->GetFileName().GetPrintable());
706 CPath newName = CPath(strNewName);
707 if (newName.IsOk() && (newName != file->GetFileName())) {
708 theApp->sharedfiles->RenameFile(file, newName);
715 void CSharedFilesCtrl::OnKeyPressed( wxKeyEvent& event )
717 if (event.GetKeyCode() == WXK_F2) {
718 wxCommandEvent evt;
719 OnRename(evt);
721 return;
723 event.Skip();
727 void CSharedFilesCtrl::OnAddCollection( wxCommandEvent& WXUNUSED(evt) )
729 int item = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
730 if (item != -1) {
731 CKnownFile *file = (CKnownFile*)GetItemData(item);
732 wxString CollectionFile = file->GetFilePath().JoinPaths(file->GetFileName()).GetRaw();
733 CMuleCollection my_collection;
734 if (my_collection.Open( (std::string)CollectionFile.mb_str() )) {
735 //#warning This is probably not working on Unicode
736 for (size_t e = 0; e < my_collection.GetFileCount(); ++e) {
737 theApp->downloadqueue->AddLink(
738 wxString(my_collection.GetEd2kLink(e).c_str(), wxConvUTF8));
745 // File_checked_for_headers