Improve speed of category tab title updates
[amule.git] / src / FileDetailDialog.cpp
blob258a6e4be47572b4fffce93b30982bd44da04923
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 // Any parts of this program derived from the xMule, lMule or eMule project,
12 // or contributed by third-party developers are copyrighted by their
13 // respective authors.
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; either version 2 of the License, or
18 // (at your option) any later version.
20 // This program is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 // GNU General Public License for more details.
24 //
25 // You should have received a copy of the GNU General Public License
26 // along with this program; if not, write to the Free Software
27 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "muuli_wdr.h" // Needed for ID_CLOSEWNDFD,...,IDC_APPLY
32 #include "FileDetailDialog.h" // Interface declarations
33 #include "FileDetailListCtrl.h" // Needed for CFileDetailListCtrl
34 #include "CommentDialogLst.h" // Needed for CCommentDialogLst
35 #include "PartFile.h" // Needed for CPartFile
36 #include "amule.h" // Needed for theApp
37 #include "SharedFileList.h" // Needed for CSharedFileList
38 #include "OtherFunctions.h"
39 #include "MuleColour.h"
41 #define ID_MY_TIMER 1652
43 //IMPLEMENT_DYNAMIC(CFileDetailDialog, CDialog)
44 BEGIN_EVENT_TABLE(CFileDetailDialog,wxDialog)
45 EVT_BUTTON(ID_CLOSEWNDFD, CFileDetailDialog::OnClosewnd)
46 EVT_BUTTON(IDC_BUTTONSTRIP, CFileDetailDialog::OnBnClickedButtonStrip)
47 EVT_BUTTON(IDC_TAKEOVER, CFileDetailDialog::OnBnClickedTakeOver)
48 EVT_LIST_ITEM_ACTIVATED(IDC_LISTCTRLFILENAMES, CFileDetailDialog::OnListClickedTakeOver)
49 EVT_BUTTON(IDC_CMTBT, CFileDetailDialog::OnBnClickedShowComment)
50 EVT_TEXT(IDC_FILENAME, CFileDetailDialog::OnTextFileNameChange)
51 EVT_BUTTON(IDC_APPLY_AND_CLOSE, CFileDetailDialog::OnBnClickedOk)
52 EVT_BUTTON(IDC_APPLY, CFileDetailDialog::OnBnClickedApply)
53 EVT_TIMER(ID_MY_TIMER,CFileDetailDialog::OnTimer)
54 END_EVENT_TABLE()
56 CFileDetailDialog::CFileDetailDialog(wxWindow *parent, CPartFile *file)
58 wxDialog(parent, -1, _("File Details"), wxDefaultPosition, wxDefaultSize,
59 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxMAXIMIZE_BOX | wxMINIMIZE_BOX),
60 m_file(file),
61 m_filenameChanged(false)
63 theApp->m_FileDetailDialogActive++;
64 m_timer.SetOwner(this, ID_MY_TIMER);
65 m_timer.Start(5000);
66 wxSizer *content = fileDetails(this, true);
67 UpdateData();
68 content->SetSizeHints(this);
69 content->Show(this, true);
72 CFileDetailDialog::~CFileDetailDialog()
74 theApp->m_FileDetailDialogActive = 0;
75 m_timer.Stop();
78 void CFileDetailDialog::OnTimer(wxTimerEvent& WXUNUSED(evt))
80 UpdateData();
83 void CFileDetailDialog::OnClosewnd(wxCommandEvent& WXUNUSED(evt))
85 EndModal(0);
88 void CFileDetailDialog::UpdateData()
90 wxString bufferS;
91 CastChild(IDC_FNAME, wxStaticText)->SetLabel(MakeStringEscaped(m_file->GetFileName().TruncatePath(60)));
92 CastChild(IDC_METFILE, wxStaticText)->SetLabel(MakeStringEscaped(m_file->GetFullName().TruncatePath(60, true)));
94 wxString tmp = CastChild(IDC_FILENAME, wxTextCtrl)->GetValue();
95 if (tmp.Length() < 3) {
96 resetValueForFilenameTextEdit();
99 CastChild(IDC_FHASH,wxStaticText)->SetLabel(m_file->GetFileHash().Encode());
100 bufferS = CFormat(wxT("%u bytes (%s)")) % m_file->GetFileSize() % CastItoXBytes(m_file->GetFileSize());
101 CastChild(IDC_FSIZE,wxControl)->SetLabel(bufferS);
102 CastChild(IDC_PFSTATUS,wxControl)->SetLabel(m_file->getPartfileStatus());
103 bufferS = CFormat(wxT("%i (%i)")) % m_file->GetPartCount() % m_file->GetHashCount();
104 CastChild(IDC_PARTCOUNT,wxControl)->SetLabel(bufferS);
105 CastChild(IDC_TRANSFERRED,wxControl)->SetLabel(CastItoXBytes(m_file->GetTransferred()));
106 CastChild(IDC_FD_STATS1,wxControl)->SetLabel(CastItoXBytes(m_file->GetLostDueToCorruption()));
107 CastChild(IDC_FD_STATS2,wxControl)->SetLabel(CastItoXBytes(m_file->GetGainDueToCompression()));
108 CastChild(IDC_FD_STATS3,wxControl)->SetLabel(CastItoIShort(m_file->TotalPacketsSavedDueToICH()));
109 CastChild(IDC_COMPLSIZE,wxControl)->SetLabel(CastItoXBytes(m_file->GetCompletedSize()));
110 bufferS = CFormat(_("%.2f%% done")) % m_file->GetPercentCompleted();
111 CastChild(IDC_PROCCOMPL,wxControl)->SetLabel(bufferS);
112 bufferS = CFormat(_("%.2f kB/s")) % m_file->GetKBpsDown();
113 CastChild(IDC_DATARATE,wxControl)->SetLabel(bufferS);
114 bufferS = CFormat(wxT("%i")) % m_file->GetSourceCount();
115 CastChild(IDC_SOURCECOUNT,wxControl)->SetLabel(bufferS);
116 bufferS = CFormat(wxT("%i")) % m_file->GetTransferingSrcCount();
117 CastChild(IDC_SOURCECOUNT2,wxControl)->SetLabel(bufferS);
118 bufferS = CFormat(wxT("%i (%.1f%%)"))
119 % m_file->GetAvailablePartCount()
120 % ((m_file->GetAvailablePartCount() * 100.0)/ m_file->GetPartCount());
121 CastChild(IDC_PARTAVAILABLE,wxControl)->SetLabel(bufferS);
122 bufferS = CastSecondsToHM(m_file->GetDlActiveTime());
123 CastChild(IDC_DLACTIVETIME, wxControl)->SetLabel(bufferS);
125 if (m_file->lastseencomplete==0) {
126 bufferS = wxString(_("Unknown")).MakeLower();
127 } else {
128 wxDateTime last_seen(m_file->lastseencomplete);
129 bufferS = last_seen.FormatISODate() + wxT(" ") + last_seen.FormatISOTime();
132 CastChild(IDC_LASTSEENCOMPL,wxControl)->SetLabel(bufferS);
133 setEnableForApplyButton();
134 // disable "Show all comments" button if there are no comments
135 FileRatingList list;
136 m_file->GetRatingAndComments(list);
137 CastChild(IDC_CMTBT, wxControl)->Enable(!list.empty());
138 FillSourcenameList();
139 Layout();
142 // CFileDetailDialog message handlers
144 void CFileDetailDialog::FillSourcenameList()
146 CFileDetailListCtrl* pmyListCtrl;
147 int itempos;
148 int inserted = 0;
149 pmyListCtrl = CastChild(IDC_LISTCTRLFILENAMES, CFileDetailListCtrl );
151 // reset
152 for (int i=0;i<pmyListCtrl->GetItemCount();i++){
153 SourcenameItem *item = reinterpret_cast<SourcenameItem *>(pmyListCtrl->GetItemData(i));
154 item->count = 0;
157 // update
158 #ifdef CLIENT_GUI
159 const SourcenameItemMap &sources = m_file->GetSourcenameItemMap();
160 for (SourcenameItemMap::const_iterator it = sources.begin(); it != sources.end(); ++it) {
161 const SourcenameItem &cur_src = it->second;
162 itempos = pmyListCtrl->FindItem(-1,cur_src.name);
163 if (itempos == -1) {
164 int itemid = pmyListCtrl->InsertItem(0, cur_src.name);
165 SourcenameItem *item = new SourcenameItem(cur_src.name, cur_src.count);
166 pmyListCtrl->SetItemPtrData(0, reinterpret_cast<wxUIntPtr>(item));
167 // background.. argh -- PA: was in old version - do we still need this?
168 wxListItem tmpitem;
169 tmpitem.m_itemId = itemid;
170 tmpitem.SetBackgroundColour(CMuleColour(wxSYS_COLOUR_LISTBOX));
171 pmyListCtrl->SetItem(tmpitem);
172 inserted++;
173 } else {
174 SourcenameItem *item = reinterpret_cast<SourcenameItem *>(pmyListCtrl->GetItemData(itempos));
175 item->count = cur_src.count;
178 #else // CLIENT_GUI
179 const CKnownFile::SourceSet& sources = m_file->GetSourceList();
180 CKnownFile::SourceSet::const_iterator it = sources.begin();
181 for ( ; it != sources.end(); ++it ) {
182 const CClientRef &cur_src = *it;
183 if (cur_src.GetRequestFile() != m_file ||
184 cur_src.GetClientFilename().Length() == 0) {
185 continue;
188 itempos = pmyListCtrl->FindItem(-1,cur_src.GetClientFilename());
189 if (itempos == -1) {
190 int itemid = pmyListCtrl->InsertItem(0, cur_src.GetClientFilename());
191 SourcenameItem *item = new SourcenameItem(cur_src.GetClientFilename(), 1);
192 pmyListCtrl->SetItemPtrData(0, reinterpret_cast<wxUIntPtr>(item));
193 // background.. argh -- PA: was in old version - do we still need this?
194 wxListItem tmpitem;
195 tmpitem.m_itemId=itemid;
196 tmpitem.SetBackgroundColour(CMuleColour(wxSYS_COLOUR_LISTBOX));
197 pmyListCtrl->SetItem(tmpitem);
198 inserted++;
199 } else {
200 SourcenameItem *item = reinterpret_cast<SourcenameItem *>(pmyListCtrl->GetItemData(itempos));
201 item->count++;
204 #endif // CLIENT_GUI
206 // remove 0'er and update counts
207 for (int i = 0; i < pmyListCtrl->GetItemCount(); ++i) {
208 SourcenameItem *item = reinterpret_cast<SourcenameItem *>(pmyListCtrl->GetItemData(i));
209 if (item->count == 0) {
210 delete item;
211 pmyListCtrl->DeleteItem(i);
212 i--; // PA: one step back is enough, no need to go back to 0
213 } else {
214 pmyListCtrl->SetItem(i, 1, CFormat(wxT("%i")) % item->count);
218 if (inserted) {
219 pmyListCtrl->SortList();
221 // no need to call Layout() here, it's called in UpdateData()
225 void CFileDetailDialog::OnBnClickedShowComment(wxCommandEvent& WXUNUSED(evt))
227 CCommentDialogLst(this,m_file).ShowModal();
231 void CFileDetailDialog::resetValueForFilenameTextEdit()
233 CastChild(IDC_FILENAME, wxTextCtrl)->SetValue(m_file->GetFileName().GetPrintable());
234 m_filenameChanged = false;
235 setEnableForApplyButton();
239 void CFileDetailDialog::setValueForFilenameTextEdit(const wxString &s)
241 CastChild(IDC_FILENAME, wxTextCtrl)->SetValue(s);
242 m_filenameChanged = true;
243 setEnableForApplyButton();
247 void CFileDetailDialog::setEnableForApplyButton()
249 bool enabled =
250 m_file->GetStatus() != PS_COMPLETE &&
251 m_file->GetStatus() != PS_COMPLETING &&
252 m_filenameChanged;
253 CastChild(IDC_APPLY, wxControl)->Enable(enabled);
254 // Make OK button default so Text can be applied by hitting return
255 CastChild(enabled ? IDC_APPLY_AND_CLOSE : ID_CLOSEWNDFD, wxButton)->SetDefault();
259 void CFileDetailDialog::OnTextFileNameChange(wxCommandEvent& WXUNUSED(evt))
261 m_filenameChanged = true;
262 setEnableForApplyButton();
266 void CFileDetailDialog::OnBnClickedOk(wxCommandEvent& evt)
268 OnBnClickedApply(evt);
269 OnClosewnd(evt);
273 void CFileDetailDialog::OnBnClickedApply(wxCommandEvent& WXUNUSED(evt))
275 CPath fileName = CPath(CastChild(IDC_FILENAME, wxTextCtrl)->GetValue());
277 if (fileName.IsOk() && (fileName != m_file->GetFileName())) {
278 if (theApp->sharedfiles->RenameFile(m_file, fileName)) {
279 FindWindow(IDC_FNAME)->SetLabel(MakeStringEscaped(m_file->GetFileName().GetPrintable()));
280 FindWindow(IDC_METFILE)->SetLabel(m_file->GetFullName().GetPrintable());
282 resetValueForFilenameTextEdit();
284 Layout();
290 bool IsDigit(const wxChar ch)
292 switch (ch) {
293 case '0':
294 case '1':
295 case '2':
296 case '3':
297 case '4':
298 case '5':
299 case '6':
300 case '7':
301 case '8':
302 case '9': return true;
304 return false;
307 bool IsWordSeparator(const wxChar ch)
309 switch (ch) {
310 case '.':
311 case ',':
312 case '(':
313 case ')':
314 case '[':
315 case ']':
316 case '{':
317 case '}':
318 case '-':
319 case '"':
320 case ' ': return true;
322 return false;
325 void ReplaceWord(wxString& str, const wxString& replaceFrom, const wxString& replaceTo, bool numbers = false)
327 unsigned int i = 0;
328 unsigned int l = replaceFrom.Length();
329 while (i < str.Length()) {
330 if (str.Mid(i, l) == replaceFrom) {
331 if ((i == 0 || IsWordSeparator(str.GetChar(i-1))) &&
332 ((i == str.Length() - l || IsWordSeparator(str.GetChar(i+l))) ||
333 (numbers && IsDigit(str.GetChar(i+l))))) {
334 str.replace(i, l, replaceTo);
336 i += replaceTo.Length() - 1;
338 i++;
342 void CFileDetailDialog::OnBnClickedButtonStrip(wxCommandEvent& WXUNUSED(evt))
344 wxString filename;
345 filename = CastChild(IDC_FILENAME, wxTextCtrl)->GetValue();
347 int extpos = filename.Find('.', true);
348 wxString ext;
349 if (extpos > 0) {
350 // get the extension - we do not modify it except make it lowercase
351 ext = filename.Mid(extpos);
352 ext.MakeLower();
353 // get rid of extension and replace . with space
354 filename.Truncate(extpos);
355 filename.Replace(wxT("."),wxT(" "));
358 // Replace Space-holders with Spaces
359 filename.Replace(wxT("_"),wxT(" "));
360 filename.Replace(wxT("%20"),wxT(" "));
362 // Some additional formatting
363 filename.Replace(wxT("hYPNOTiC"), wxEmptyString);
364 filename.MakeLower();
365 filename.Replace(wxT("xxx"), wxT("XXX"));
366 // filename.Replace(wxT("xdmnx"), wxEmptyString);
367 // filename.Replace(wxT("pmp"), wxEmptyString);
368 // filename.Replace(wxT("dws"), wxEmptyString);
369 filename.Replace(wxT("www pornreactor com"), wxEmptyString);
370 filename.Replace(wxT("sharereactor"), wxEmptyString);
371 filename.Replace(wxT("found via www filedonkey com"), wxEmptyString);
372 filename.Replace(wxT("deviance"), wxEmptyString);
373 filename.Replace(wxT("adunanza"), wxEmptyString);
374 filename.Replace(wxT("-ftv"), wxEmptyString);
375 filename.Replace(wxT("flt"), wxEmptyString);
376 filename.Replace(wxT("[]"), wxEmptyString);
377 filename.Replace(wxT("()"), wxEmptyString);
379 // Change CD, CD#, VCD{,#}, DVD{,#}, ISO, PC to uppercase
380 ReplaceWord(filename, wxT("cd"), wxT("CD"), true);
381 ReplaceWord(filename, wxT("vcd"), wxT("VCD"), true);
382 ReplaceWord(filename, wxT("dvd"), wxT("DVD"), true);
383 ReplaceWord(filename, wxT("iso"), wxT("ISO"), false);
384 ReplaceWord(filename, wxT("pc"), wxT("PC"), false);
386 // Make leading Caps
387 // and delete 1+ spaces
388 if (filename.Length()>1)
390 bool last_char_space = true;
391 bool last_char_wordseparator = true;
392 unsigned int i = 0;
394 do {
395 wxChar c = filename.GetChar(i);
396 if (c == ' ') {
397 if (last_char_space) {
398 filename.Remove(i, 1);
399 i--;
400 } else {
401 last_char_space = true;
403 } else if (c == '.') {
404 if (last_char_space && i > 0) {
405 i--;
406 filename.Remove(i, 1);
408 last_char_space = false;
409 } else {
410 if (last_char_wordseparator) {
411 wxString tempStr(c);
412 tempStr.MakeUpper();
413 filename.SetChar(i, tempStr.GetChar(0));
414 last_char_space = false;
417 last_char_wordseparator = IsWordSeparator(c);
418 i++;
419 } while (i < filename.Length());
421 if (last_char_space && i > 0) {
422 filename.Remove(i-1, 1);
426 // should stay lowercase
427 ReplaceWord(filename, wxT("By"), wxT("by"));
429 // re-add extension
430 filename += ext;
432 setValueForFilenameTextEdit(filename);
435 void CFileDetailDialog::OnBnClickedTakeOver(wxCommandEvent& WXUNUSED(evt))
437 CFileDetailListCtrl* pmyListCtrl;
438 pmyListCtrl = CastChild( IDC_LISTCTRLFILENAMES, CFileDetailListCtrl );
439 if (pmyListCtrl->GetSelectedItemCount() > 0) {
440 // get first selected item (there is only one)
441 long pos = pmyListCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
442 if (pos != -1) { // shouldn't happen, we checked if something is selected
443 setValueForFilenameTextEdit(pmyListCtrl->GetItemText(pos));
448 void CFileDetailDialog::OnListClickedTakeOver(wxListEvent& WXUNUSED(evt))
450 wxCommandEvent ev;
451 OnBnClickedTakeOver(ev);
453 // File_checked_for_headers