Upstream tarball 20080414
[amule.git] / src / SharedFilesCtrl.cpp
blob5b02a182f549d1a59187b2dd4878f149ee23ecf2
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);
103 InsertColumn(ID_SHARED_COL_SIZE, _("Size"), wxLIST_FORMAT_LEFT, 100);
104 InsertColumn(ID_SHARED_COL_TYPE, _("Type"), wxLIST_FORMAT_LEFT, 50);
105 InsertColumn(ID_SHARED_COL_PRIO, _("Priority"), wxLIST_FORMAT_LEFT, 70);
106 InsertColumn(ID_SHARED_COL_ID, _("FileID"), wxLIST_FORMAT_LEFT, 220);
107 InsertColumn(ID_SHARED_COL_REQ, _("Requests"), wxLIST_FORMAT_LEFT, 100);
108 InsertColumn(ID_SHARED_COL_AREQ, _("Accepted Requests"), wxLIST_FORMAT_LEFT, 100);
109 InsertColumn(ID_SHARED_COL_TRA, _("Transferred Data"), wxLIST_FORMAT_LEFT, 120);
110 InsertColumn(ID_SHARED_COL_RTIO, _("Share Ratio"), wxLIST_FORMAT_LEFT, 100);
111 InsertColumn(ID_SHARED_COL_PART, _("Obtained Parts"), wxLIST_FORMAT_LEFT, 120);
112 InsertColumn(ID_SHARED_COL_CMPL, _("Complete Sources"), wxLIST_FORMAT_LEFT, 120);
113 InsertColumn(ID_SHARED_COL_PATH, _("Directory Path"), wxLIST_FORMAT_LEFT, 220);
115 LoadSettings();
119 CSharedFilesCtrl::~CSharedFilesCtrl()
125 void CSharedFilesCtrl::OnRightClick(wxListEvent& event)
127 long item_hit = CheckSelection(event);
129 if ( (m_menu == NULL) && (item_hit != -1)) {
130 m_menu = new wxMenu(_("Shared Files"));
131 wxMenu* prioMenu = new wxMenu();
132 prioMenu->Append(MP_PRIOVERYLOW, _("Very low"));
133 prioMenu->Append(MP_PRIOLOW, _("Low"));
134 prioMenu->Append(MP_PRIONORMAL, _("Normal"));
135 prioMenu->Append(MP_PRIOHIGH, _("High"));
136 prioMenu->Append(MP_PRIOVERYHIGH, _("Very High"));
137 prioMenu->Append(MP_POWERSHARE, _("Release"));
138 prioMenu->Append(MP_PRIOAUTO, _("Auto"));
140 m_menu->Append(0,_("Priority"),prioMenu);
141 m_menu->AppendSeparator();
143 CKnownFile* file = (CKnownFile*)GetItemData(item_hit);
144 if (file->GetFileComment().IsEmpty() && !file->GetFileRating()) {
145 m_menu->Append(MP_CMT, _("Add Comment/Rating"));
146 } else {
147 m_menu->Append(MP_CMT, _("Edit Comment/Rating"));
150 m_menu->AppendSeparator();
151 m_menu->Append(MP_RENAME, _("Rename"));
152 m_menu->AppendSeparator();
153 /* Commented out while it's gone
154 m_menu->Append( MP_RAZORSTATS, _("Get Razorback 2's stats for this file"));
155 m_menu->AppendSeparator();
157 if (file->GetFileName().GetExt() == wxT("emulecollection")) {
158 m_menu->Append( MP_ADDCOLLECTION, _("Add files in collection to transfer list"));
159 m_menu->AppendSeparator();
161 m_menu->Append(MP_GETMAGNETLINK,_("Copy magnet &URI to clipboard"));
162 m_menu->Append(MP_GETED2KLINK,_("Copy ED2k &link to clipboard"));
163 m_menu->Append(MP_GETSOURCEED2KLINK,_("Copy ED2k link to clipboard (&Source)"));
164 m_menu->Append(MP_GETCRYPTSOURCEDED2KLINK,_("Copy ED2k link to clipboard (Source) (&With Crypt options)"));
165 m_menu->Append(MP_GETHOSTNAMESOURCEED2KLINK,_("Copy ED2k link to clipboard (&Hostname)"));
166 m_menu->Append(MP_GETHOSTNAMECRYPTSOURCEED2KLINK,_("Copy ED2k link to clipboard (Hostname) (With &Crypt options)"));
167 m_menu->Append(MP_GETAICHED2KLINK,_("Copy ED2k link to clipboard (&AICH info)"));
168 m_menu->Append(MP_WS,_("Copy feedback to clipboard"));
170 m_menu->Enable(MP_GETAICHED2KLINK, file->HasProperAICHHashSet());
171 m_menu->Enable(MP_GETHOSTNAMESOURCEED2KLINK, !thePrefs::GetYourHostname().IsEmpty());
172 m_menu->Enable(MP_GETHOSTNAMECRYPTSOURCEED2KLINK, !thePrefs::GetYourHostname().IsEmpty());
173 m_menu->Enable(MP_RENAME, file->IsPartFile());
174 m_menu->Enable(MP_WS, file->IsPartFile());
176 PopupMenu( m_menu, event.GetPoint() );
178 delete m_menu;
180 m_menu = NULL;
185 void CSharedFilesCtrl::OnGetFeedback(wxCommandEvent& WXUNUSED(event))
187 wxString feed;
188 long index = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
189 while (index != -1) {
190 CKnownFile* file = (CKnownFile*)GetItemData(index);
192 if (file->IsPartFile()) {
193 feed += dynamic_cast<CPartFile*>(file)->GetFeedback();
196 index = GetNextItem(index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
198 if (!feed.IsEmpty()) {
199 theApp->CopyTextToClipboard(feed);
204 #ifndef CLIENT_GUI
205 void CSharedFilesCtrl::ShowFileList()
207 Freeze();
208 DeleteAllItems();
210 std::vector<CKnownFile*> files;
211 theApp->sharedfiles->CopyFileList(files);
212 for (unsigned i = 0; i < files.size(); ++i) {
213 DoShowFile(files[i], true);
216 SortList();
217 ShowFilesCount();
219 Thaw();
221 #endif
224 void CSharedFilesCtrl::RemoveFile(CKnownFile *toRemove)
226 long index = FindItem( -1, reinterpret_cast<wxUIntPtr>(toRemove) );
228 if ( index != -1 ) {
229 DeleteItem( index );
231 ShowFilesCount();
236 void CSharedFilesCtrl::ShowFile(CKnownFile* file)
238 DoShowFile(file, false);
242 void CSharedFilesCtrl::DoShowFile(CKnownFile* file, bool batch)
244 wxUIntPtr ptr = reinterpret_cast<wxUIntPtr>(file);
245 if ((!batch) && (FindItem(-1, ptr) > -1)) {
246 return;
249 const long insertPos = (batch ? GetItemCount() : GetInsertPos(ptr));
251 long newitem = InsertItem(insertPos, wxEmptyString);
252 SetItemPtrData( newitem, ptr );
254 if (!batch) {
255 ShowFilesCount();
259 void CSharedFilesCtrl::OnSetPriority( wxCommandEvent& event )
261 int priority = 0;
263 switch ( event.GetId() ) {
264 case MP_PRIOVERYLOW: priority = PR_VERYLOW; break;
265 case MP_PRIOLOW: priority = PR_LOW; break;
266 case MP_PRIONORMAL: priority = PR_NORMAL; break;
267 case MP_PRIOHIGH: priority = PR_HIGH; break;
268 case MP_PRIOVERYHIGH: priority = PR_VERYHIGH; break;
269 case MP_POWERSHARE: priority = PR_POWERSHARE; break;
272 long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
274 while( index != -1 ) {
275 CKnownFile* file = (CKnownFile*)GetItemData( index );
276 CoreNotify_KnownFile_Up_Prio_Set( file, priority );
278 RefreshItem( index );
280 index = GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
285 void CSharedFilesCtrl::OnSetPriorityAuto( wxCommandEvent& WXUNUSED(event) )
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_Auto(file);
293 RefreshItem( index );
295 index = GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
300 void CSharedFilesCtrl::OnCreateURI( wxCommandEvent& event )
302 wxString URIs;
304 if ( event.GetId() == MP_GETSOURCEED2KLINK || event.GetId() == MP_GETCRYPTSOURCEDED2KLINK) {
305 if ( !theApp->IsConnectedED2K() || theApp->serverconnect->IsLowID() ) {
306 wxMessageBox(_("You need a HighID to create a valid sourcelink"), _("Warning"), wxOK | wxICON_ERROR, this);
308 return;
312 long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
314 while( index != -1 ) {
315 CKnownFile* file = (CKnownFile*)GetItemData( index );
317 switch ( event.GetId() ) {
318 case MP_GETMAGNETLINK: URIs += theApp->CreateMagnetLink( file ) + wxT("\n"); break;
319 case MP_GETED2KLINK: URIs += theApp->CreateED2kLink( file ) + wxT("\n"); break;
320 case MP_GETSOURCEED2KLINK: URIs += theApp->CreateED2kLink( file , true) + wxT("\n"); break;
321 case MP_GETCRYPTSOURCEDED2KLINK: URIs += theApp->CreateED2kLink( file , true, false, true) + wxT("\n"); break;
322 case MP_GETHOSTNAMESOURCEED2KLINK: URIs += theApp->CreateED2kLink( file , true, true) + wxT("\n"); break;
323 case MP_GETHOSTNAMECRYPTSOURCEED2KLINK: URIs += theApp->CreateED2kLink( file, true, true, true ) + wxT("\n"); break;
324 case MP_GETAICHED2KLINK: URIs += theApp->CreateED2kAICHLink( file ) + wxT("\n"); break;
327 index = GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
330 if ( !URIs.IsEmpty() ) {
331 theApp->CopyTextToClipboard( URIs.RemoveLast() );
336 void CSharedFilesCtrl::OnEditComment( wxCommandEvent& WXUNUSED(event) )
338 long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
340 if ( index != -1 ) {
341 CKnownFile* file = (CKnownFile*)GetItemData( index );
343 CCommentDialog dialog( this, file );
345 dialog.ShowModal();
350 int CSharedFilesCtrl::SortProc(wxUIntPtr item1, wxUIntPtr item2, long sortData)
352 CKnownFile* file1 = (CKnownFile*)item1;
353 CKnownFile* file2 = (CKnownFile*)item2;
355 int mod = (sortData & CMuleListCtrl::SORT_DES) ? -1 : 1;
356 bool altSorting = (sortData & CMuleListCtrl::SORT_ALT) > 0;
358 switch (sortData & CMuleListCtrl::COLUMN_MASK) {
359 // Sort by filename.
360 case ID_SHARED_COL_NAME:
361 return mod * CmpAny(file1->GetFileName(), file2->GetFileName());
363 // Sort by filesize.
364 case ID_SHARED_COL_SIZE:
365 return mod * CmpAny( file1->GetFileSize(), file2->GetFileSize() );
367 // Sort by filetype.
368 case ID_SHARED_COL_TYPE:
369 return mod * GetFiletypeByName(file1->GetFileName()).CmpNoCase(GetFiletypeByName( file2->GetFileName()) );
371 // Sort by priority.
372 case ID_SHARED_COL_PRIO: {
373 int8 prioA = file1->GetUpPriority();
374 int8 prioB = file2->GetUpPriority();
376 // Work-around for PR_VERYLOW which has value 4. See KnownFile.h for that stupidity ...
377 return mod * CmpAny( ( prioB != PR_VERYLOW ? prioB : -1 ), ( prioA != PR_VERYLOW ? prioA : -1 ) );
380 // Sort by fileID.
381 case ID_SHARED_COL_ID:
382 return mod * file1->GetFileHash().Encode().Cmp( file2->GetFileHash().Encode() );
384 // Sort by Requests this session.
385 case ID_SHARED_COL_REQ:
386 if (altSorting) {
387 return mod * CmpAny( file1->statistic.GetAllTimeRequests(), file2->statistic.GetAllTimeRequests() );
388 } else {
389 return mod * CmpAny( file1->statistic.GetRequests(), file2->statistic.GetRequests() );
392 // Sort by accepted requests. Ascending.
393 case ID_SHARED_COL_AREQ:
394 if (altSorting) {
395 return mod * CmpAny( file1->statistic.GetAllTimeAccepts(), file2->statistic.GetAllTimeAccepts() );
396 } else {
397 return mod * CmpAny( file1->statistic.GetAccepts(), file2->statistic.GetAccepts() );
400 // Sort by transferred. Ascending.
401 case ID_SHARED_COL_TRA:
402 if (altSorting) {
403 return mod * CmpAny( file1->statistic.GetAllTimeTransferred(), file2->statistic.GetAllTimeTransferred() );
404 } else {
405 return mod * CmpAny( file1->statistic.GetTransferred(), file2->statistic.GetTransferred() );
408 // Sort by Share Ratio. Ascending.
409 case ID_SHARED_COL_RTIO:
410 return mod * CmpAny( (double)file1->statistic.GetAllTimeTransferred() / file1->GetFileSize(),
411 (double)file2->statistic.GetAllTimeTransferred() / file2->GetFileSize() );
413 // Complete sources asc
414 case ID_SHARED_COL_CMPL:
415 return mod * CmpAny( file1->m_nCompleteSourcesCount, file2->m_nCompleteSourcesCount );
417 // Folders ascending
418 case ID_SHARED_COL_PATH: {
419 if ( file1->IsPartFile() && file2->IsPartFile() )
420 return mod * 0;
421 if ( file1->IsPartFile() )
422 return mod * -1;
423 if ( file2->IsPartFile() )
424 return mod * 1;
426 return mod * CmpAny(file1->GetFilePath(), file2->GetFilePath());
429 default:
430 return 0;
435 void CSharedFilesCtrl::UpdateItem(CKnownFile* toupdate)
437 long result = FindItem( -1, reinterpret_cast<wxUIntPtr>(toupdate) );
439 if ( result > -1 ) {
440 RefreshItem(result);
442 if ( GetItemState( result, wxLIST_STATE_SELECTED ) ) {
443 theApp->amuledlg->m_sharedfileswnd->SelectionUpdated();
449 void CSharedFilesCtrl::ShowFilesCount()
451 wxString str = wxString::Format(_("Shared Files (%i)"), GetItemCount());
452 wxStaticText* label = CastByName( wxT("sharedFilesLabel"), GetParent(), wxStaticText );
454 label->SetLabel( str );
455 label->GetParent()->Layout();
459 void CSharedFilesCtrl::OnDrawItem( int item, wxDC* dc, const wxRect& rect, const wxRect& rectHL, bool highlighted )
461 CKnownFile *file = (CKnownFile*)GetItemData(item);
462 wxASSERT( file );
464 if ( highlighted ) {
465 wxColour newcol;
466 wxBrush hilBrush;
468 if (GetFocus()) {
469 newcol = SYSCOLOR(wxSYS_COLOUR_HIGHLIGHT);
470 newcol = wxColour(G_BLEND(newcol.Red(),125),
471 G_BLEND(newcol.Green(),125),
472 G_BLEND(newcol.Blue(),125));
473 hilBrush = wxBrush(newcol, wxSOLID);
474 dc->SetBackground(hilBrush);
475 } else {
476 newcol = SYSCOLOR(wxSYS_COLOUR_BTNSHADOW);
477 newcol = wxColour(G_BLEND(newcol.Red(),125),
478 G_BLEND(newcol.Green(),125),
479 G_BLEND(newcol.Blue(),125));
480 hilBrush = wxBrush(newcol, wxSOLID);
481 dc->SetBackground(hilBrush);
484 dc->SetTextForeground( SYSCOLOR(wxSYS_COLOUR_HIGHLIGHTTEXT));
486 newcol = wxColour( G_BLEND(newcol.Red(), 65),
487 G_BLEND(newcol.Green(), 65),
488 G_BLEND(newcol.Blue(), 65) );
489 dc->SetPen(wxPen(newcol,1,wxSOLID));
490 } else {
491 dc->SetBackground( wxBrush(SYSCOLOR(wxSYS_COLOUR_LISTBOX), wxSOLID) );
492 dc->SetTextForeground(SYSCOLOR(wxSYS_COLOUR_WINDOWTEXT));
493 dc->SetPen(*wxTRANSPARENT_PEN);
496 dc->SetBrush(dc->GetBackground());
497 dc->DrawRectangle(rectHL);
498 dc->SetPen(*wxTRANSPARENT_PEN);
500 // Offset based on the height of the fonts
501 const int textVOffset = ( rect.GetHeight() - dc->GetCharHeight() ) / 2;
502 // Empty space to each side of a column
503 const int SPARE_PIXELS_HORZ = 4;
505 // The leftmost position of the current column
506 int columnLeft = 0;
508 for ( int i = 0; i < GetColumnCount(); ++i ) {
509 const int columnWidth = GetColumnWidth(i);
511 if (columnWidth > 2*SPARE_PIXELS_HORZ) {
512 wxRect columnRect(
513 columnLeft + SPARE_PIXELS_HORZ, rect.y,
514 columnWidth - 2 * SPARE_PIXELS_HORZ, rect.height);
516 wxDCClipper clipper(*dc, columnRect);
518 wxString textBuffer;
519 switch ( i ) {
520 case ID_SHARED_COL_NAME:
521 textBuffer = file->GetFileName().GetPrintable();
523 if (file->GetFileRating() || file->GetFileComment().Length()) {
524 int image = Client_CommentOnly_Smiley;
525 if (file->GetFileRating()) {
526 image = Client_InvalidRating_Smiley + file->GetFileRating() - 1;
529 wxASSERT(image >= Client_InvalidRating_Smiley);
530 wxASSERT(image <= Client_CommentOnly_Smiley);
532 int imgWidth = 16;
534 theApp->amuledlg->m_imagelist.Draw(image, *dc, columnRect.x,
535 columnRect.y + 1, wxIMAGELIST_DRAW_TRANSPARENT);
537 // Move the text to the right
538 columnRect.x += (imgWidth + 4);
541 break;
543 case ID_SHARED_COL_SIZE:
544 textBuffer = CastItoXBytes(file->GetFileSize());
545 break;
547 case ID_SHARED_COL_TYPE:
548 textBuffer = GetFiletypeByName(file->GetFileName());
549 break;
551 case ID_SHARED_COL_PRIO:
552 textBuffer = PriorityToStr(file->GetUpPriority(), file->IsAutoUpPriority());
553 break;
555 case ID_SHARED_COL_ID:
556 textBuffer = file->GetFileHash().Encode();
557 break;
559 case ID_SHARED_COL_REQ:
560 textBuffer = wxString::Format(wxT("%u (%u)"),
561 file->statistic.GetRequests(),
562 file->statistic.GetAllTimeRequests());
563 break;
565 case ID_SHARED_COL_AREQ:
566 textBuffer = wxString::Format(wxT("%u (%u)"),
567 file->statistic.GetAccepts(),
568 file->statistic.GetAllTimeAccepts());
569 break;
571 case ID_SHARED_COL_TRA:
572 textBuffer = CastItoXBytes(file->statistic.GetTransferred())
573 + wxT(" (") + CastItoXBytes(file->statistic.GetAllTimeTransferred()) + wxT(")");
574 break;
576 case ID_SHARED_COL_RTIO:
577 textBuffer = wxString::Format(wxT("%.2f"),
578 (double)file->statistic.GetAllTimeTransferred() / file->GetFileSize() );
579 break;
581 case ID_SHARED_COL_PART:
582 if ( file->GetPartCount() ) {
583 wxRect barRect(columnRect.x, columnRect. y + 1,
584 columnRect.width, columnRect.height - 2);
586 DrawAvailabilityBar(file, dc, barRect);
588 break;
590 case ID_SHARED_COL_CMPL:
591 if ( file->m_nCompleteSourcesCountLo == 0 ) {
592 if ( file->m_nCompleteSourcesCountHi ) {
593 textBuffer = wxString::Format(wxT("< %u"), file->m_nCompleteSourcesCountHi );
594 } else {
595 textBuffer = wxT("0");
597 } else if (file->m_nCompleteSourcesCountLo == file->m_nCompleteSourcesCountHi) {
598 textBuffer = wxString::Format(wxT("%u"), file->m_nCompleteSourcesCountLo);
599 } else {
600 textBuffer = wxString::Format(wxT("%u - %u"),
601 file->m_nCompleteSourcesCountLo,
602 file->m_nCompleteSourcesCountHi);
605 break;
607 case ID_SHARED_COL_PATH:
608 if ( file->IsPartFile() ) {
609 textBuffer = _("[PartFile]");
610 } else {
611 textBuffer = file->GetFilePath().GetPrintable();
615 if (!textBuffer.IsEmpty()) {
616 dc->DrawText(textBuffer, columnRect.x, columnRect.y + textVOffset);
620 // Move to the next column
621 columnLeft += columnWidth;
626 wxString CSharedFilesCtrl::GetTTSText(unsigned item) const
628 return reinterpret_cast<CKnownFile*>(GetItemData(item))->GetFileName().GetPrintable();
632 bool CSharedFilesCtrl::AltSortAllowed(unsigned column) const
634 switch ( column ) {
635 case ID_SHARED_COL_REQ:
636 case ID_SHARED_COL_AREQ:
637 case ID_SHARED_COL_TRA:
638 return true;
640 default:
641 return false;
646 void CSharedFilesCtrl::DrawAvailabilityBar(CKnownFile* file, wxDC* dc, const wxRect& rect ) const
648 // Reference to the availability list
649 const ArrayOfUInts16& list = file->IsPartFile() ?
650 ((CPartFile*)file)->m_SrcpartFrequency :
651 file->m_AvailPartFrequency;
652 static CBarShader s_ChunkBar;
653 s_ChunkBar.SetFileSize( file->GetFileSize() );
654 s_ChunkBar.SetHeight( rect.GetHeight() );
655 s_ChunkBar.SetWidth( rect.GetWidth() );
656 s_ChunkBar.Set3dDepth( CPreferences::Get3DDepth() );
657 uint64 end = 0;
658 for ( unsigned int i = 0; i < list.size(); ++i ) {
659 COLORREF color = list[i] ? (RGB(0, (210-(22*( list[i] - 1 ) ) < 0) ? 0 : 210-(22*( list[i] - 1 ) ), 255))
660 : RGB(255, 0, 0);
661 uint64 start = PARTSIZE * static_cast<uint64>(i);
662 end = PARTSIZE * static_cast<uint64>(i + 1);
663 s_ChunkBar.FillRange(start, end, color);
665 s_ChunkBar.FillRange(end + 1, file->GetFileSize() - 1, RGB(255, 0, 0));
666 s_ChunkBar.Draw(dc, rect.GetLeft(), rect.GetTop(), CPreferences::UseFlatBar() );
669 void CSharedFilesCtrl::OnGetRazorStats( wxCommandEvent& WXUNUSED(event) )
671 int item = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
672 if ( item != -1 ) {
673 CKnownFile* file = (CKnownFile*)GetItemData( item );
675 theApp->amuledlg->LaunchUrl(wxT("http://stats.razorback2.com/ed2khistory?ed2k=") + file->GetFileHash().Encode());
680 void CSharedFilesCtrl::OnRename( wxCommandEvent& WXUNUSED(event) )
682 int item = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
683 if ( item != -1 ) {
684 CKnownFile* file = (CKnownFile*)GetItemData(item);
686 // Currently renaming of completed files causes problem with kad
687 if (file->IsPartFile()) {
688 wxString strNewName = ::wxGetTextFromUser(
689 _("Enter new name for this file:"),
690 _("File rename"), file->GetFileName().GetPrintable());
692 CPath newName = CPath(strNewName);
693 if (newName.IsOk() && (newName != file->GetFileName())) {
694 theApp->sharedfiles->RenameFile(file, newName);
701 void CSharedFilesCtrl::OnKeyPressed( wxKeyEvent& event )
703 if (event.GetKeyCode() == WXK_F2) {
704 wxCommandEvent evt;
705 OnRename(evt);
707 return;
709 event.Skip();
713 void CSharedFilesCtrl::OnAddCollection( wxCommandEvent& WXUNUSED(evt) )
715 int item = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
716 if (item != -1) {
717 CKnownFile *file = (CKnownFile*)GetItemData(item);
718 wxString CollectionFile = file->GetFilePath().JoinPaths(file->GetFileName()).GetRaw();
719 CMuleCollection my_collection;
720 if (my_collection.Open( (std::string)CollectionFile.mb_str() )) {
721 //#warning This is probably not working on Unicode
722 for (size_t e = 0; e < my_collection.GetFileCount(); ++e) {
723 theApp->downloadqueue->AddLink(
724 wxString(my_collection.GetEd2kLink(e).c_str(), wxConvUTF8));
731 // File_checked_for_headers