Upstream tarball 10142
[amule.git] / src / FileDetailDialog.cpp
blob7f1edbf3d851d8e730e5ed5d7fa02d74d100954b
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 "updownclient.h" // Needed for CUpDownClient
36 #include "PartFile.h" // Needed for CPartFile
37 #include "amule.h" // Needed for theApp
38 #include "SharedFileList.h" // Needed for CSharedFileList
39 #include "OtherFunctions.h"
40 #include "MuleColour.h"
42 #define ID_MY_TIMER 1652
44 //IMPLEMENT_DYNAMIC(CFileDetailDialog, CDialog)
45 BEGIN_EVENT_TABLE(CFileDetailDialog,wxDialog)
46 EVT_BUTTON(ID_CLOSEWNDFD, CFileDetailDialog::OnClosewnd)
47 EVT_BUTTON(IDC_BUTTONSTRIP, CFileDetailDialog::OnBnClickedButtonStrip)
48 EVT_BUTTON(IDC_TAKEOVER, CFileDetailDialog::OnBnClickedTakeOver)
49 EVT_LIST_ITEM_ACTIVATED(IDC_LISTCTRLFILENAMES, CFileDetailDialog::OnListClickedTakeOver)
50 EVT_BUTTON(IDC_CMTBT, CFileDetailDialog::OnBnClickedShowComment)
51 EVT_TEXT(IDC_FILENAME, CFileDetailDialog::OnTextFileNameChange)
52 EVT_BUTTON(IDC_APPLY_AND_CLOSE, CFileDetailDialog::OnBnClickedOk)
53 EVT_BUTTON(IDC_APPLY, CFileDetailDialog::OnBnClickedApply)
54 EVT_TIMER(ID_MY_TIMER,CFileDetailDialog::OnTimer)
55 END_EVENT_TABLE()
57 CFileDetailDialog::CFileDetailDialog(wxWindow *parent, CPartFile *file)
59 wxDialog(parent, -1, _("File Details"), wxDefaultPosition, wxDefaultSize,
60 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxMAXIMIZE_BOX | wxMINIMIZE_BOX),
61 m_file(file),
62 m_filenameChanged(false)
64 theApp->m_FileDetailDialogActive++;
65 m_timer.SetOwner(this, ID_MY_TIMER);
66 m_timer.Start(5000);
67 wxSizer *content = fileDetails(this, true);
68 UpdateData();
69 content->SetSizeHints(this);
70 content->Show(this, true);
73 CFileDetailDialog::~CFileDetailDialog()
75 theApp->m_FileDetailDialogActive = 0;
76 m_timer.Stop();
79 void CFileDetailDialog::OnTimer(wxTimerEvent& WXUNUSED(evt))
81 UpdateData();
84 void CFileDetailDialog::OnClosewnd(wxCommandEvent& WXUNUSED(evt))
86 EndModal(0);
89 void CFileDetailDialog::UpdateData()
91 wxString bufferS;
92 CastChild(IDC_FNAME, wxStaticText)->SetLabel(MakeStringEscaped(m_file->GetFileName().TruncatePath(60)));
93 CastChild(IDC_METFILE, wxStaticText)->SetLabel(MakeStringEscaped(m_file->GetFullName().TruncatePath(60, true)));
95 wxString tmp = CastChild(IDC_FILENAME, wxTextCtrl)->GetValue();
96 if (tmp.Length() < 3) {
97 resetValueForFilenameTextEdit();
100 CastChild(IDC_FHASH,wxStaticText)->SetLabel(m_file->GetFileHash().Encode());
101 bufferS = wxString::Format(wxT("%llu bytes ("), m_file->GetFileSize())
102 + CastItoXBytes(m_file->GetFileSize())
103 + wxT(")");
104 CastChild(IDC_FSIZE,wxControl)->SetLabel(bufferS);
105 CastChild(IDC_PFSTATUS,wxControl)->SetLabel(m_file->getPartfileStatus());
106 bufferS = wxString::Format(wxT("%i (%i)"),m_file->GetPartCount(),m_file->GetHashCount());
107 CastChild(IDC_PARTCOUNT,wxControl)->SetLabel(bufferS);
108 CastChild(IDC_TRANSFERRED,wxControl)->SetLabel(CastItoXBytes(m_file->GetTransferred()));
109 CastChild(IDC_FD_STATS1,wxControl)->SetLabel(CastItoXBytes(m_file->GetLostDueToCorruption()));
110 CastChild(IDC_FD_STATS2,wxControl)->SetLabel(CastItoXBytes(m_file->GetGainDueToCompression()));
111 CastChild(IDC_FD_STATS3,wxControl)->SetLabel(CastItoIShort(m_file->TotalPacketsSavedDueToICH()));
112 CastChild(IDC_COMPLSIZE,wxControl)->SetLabel(CastItoXBytes(m_file->GetCompletedSize()));
113 bufferS = wxString::Format(_("%.2f%% done"),m_file->GetPercentCompleted());
114 CastChild(IDC_PROCCOMPL,wxControl)->SetLabel(bufferS);
115 bufferS = wxString::Format(_("%.2f kB/s"),(float)m_file->GetKBpsDown());
116 CastChild(IDC_DATARATE,wxControl)->SetLabel(bufferS);
117 bufferS = wxString::Format(wxT("%i"),m_file->GetSourceCount());
118 CastChild(IDC_SOURCECOUNT,wxControl)->SetLabel(bufferS);
119 bufferS = wxString::Format(wxT("%i"),m_file->GetTransferingSrcCount());
120 CastChild(IDC_SOURCECOUNT2,wxControl)->SetLabel(bufferS);
121 bufferS = wxString::Format(wxT("%i (%.1f%%)"),
122 m_file->GetAvailablePartCount(),
123 ((m_file->GetAvailablePartCount() * 100.0f)/ m_file->GetPartCount()));
124 CastChild(IDC_PARTAVAILABLE,wxControl)->SetLabel(bufferS);
125 bufferS = CastSecondsToHM(m_file->GetDlActiveTime());
126 CastChild(IDC_DLACTIVETIME, wxControl)->SetLabel(bufferS);
128 if (m_file->lastseencomplete==0) {
129 bufferS = wxString(_("Unknown")).MakeLower();
130 } else {
131 wxDateTime last_seen(m_file->lastseencomplete);
132 bufferS = last_seen.FormatISODate() + wxT(" ") + last_seen.FormatISOTime();
135 CastChild(IDC_LASTSEENCOMPL,wxControl)->SetLabel(bufferS);
136 setEnableForApplyButton();
137 // disable "Show all comments" button if there are no comments
138 FileRatingList list;
139 m_file->GetRatingAndComments(list);
140 CastChild(IDC_CMTBT, wxControl)->Enable(!list.empty());
141 FillSourcenameList();
142 Layout();
145 // CFileDetailDialog message handlers
147 void CFileDetailDialog::FillSourcenameList()
149 CFileDetailListCtrl* pmyListCtrl;
150 int itempos;
151 int inserted = 0;
152 pmyListCtrl = CastChild(IDC_LISTCTRLFILENAMES, CFileDetailListCtrl );
154 // reset
155 for (int i=0;i<pmyListCtrl->GetItemCount();i++){
156 SourcenameItem *item = reinterpret_cast<SourcenameItem *>(pmyListCtrl->GetItemData(i));
157 item->count = 0;
160 // update
161 #ifdef CLIENT_GUI
162 const SourcenameItemMap &sources = m_file->GetSourcenameItemMap();
163 for (SourcenameItemMap::const_iterator it = sources.begin(); it != sources.end(); ++it) {
164 const SourcenameItem &cur_src = it->second;
165 itempos = pmyListCtrl->FindItem(-1,cur_src.name);
166 if (itempos == -1) {
167 int itemid = pmyListCtrl->InsertItem(0, cur_src.name);
168 SourcenameItem *item = new SourcenameItem(cur_src.name, cur_src.count);
169 pmyListCtrl->SetItemPtrData(0, reinterpret_cast<wxUIntPtr>(item));
170 // background.. argh -- PA: was in old version - do we still need this?
171 wxListItem tmpitem;
172 tmpitem.m_itemId = itemid;
173 tmpitem.SetBackgroundColour(CMuleColour(wxSYS_COLOUR_LISTBOX));
174 pmyListCtrl->SetItem(tmpitem);
175 inserted++;
176 } else {
177 SourcenameItem *item = reinterpret_cast<SourcenameItem *>(pmyListCtrl->GetItemData(itempos));
178 item->count = cur_src.count;
181 #else // CLIENT_GUI
182 const CKnownFile::SourceSet& sources = m_file->GetSourceList();
183 CKnownFile::SourceSet::const_iterator it = sources.begin();
184 for ( ; it != sources.end(); ++it ) {
185 const CUpDownClient &cur_src = **it;
186 if (cur_src.GetRequestFile() != m_file ||
187 cur_src.GetClientFilename().Length() == 0) {
188 continue;
191 itempos = pmyListCtrl->FindItem(-1,cur_src.GetClientFilename());
192 if (itempos == -1) {
193 int itemid = pmyListCtrl->InsertItem(0, cur_src.GetClientFilename());
194 SourcenameItem *item = new SourcenameItem(cur_src.GetClientFilename(), 1);
195 pmyListCtrl->SetItemPtrData(0, reinterpret_cast<wxUIntPtr>(item));
196 // background.. argh -- PA: was in old version - do we still need this?
197 wxListItem tmpitem;
198 tmpitem.m_itemId=itemid;
199 tmpitem.SetBackgroundColour(CMuleColour(wxSYS_COLOUR_LISTBOX));
200 pmyListCtrl->SetItem(tmpitem);
201 inserted++;
202 } else {
203 SourcenameItem *item = reinterpret_cast<SourcenameItem *>(pmyListCtrl->GetItemData(itempos));
204 item->count++;
207 #endif // CLIENT_GUI
209 // remove 0'er and update counts
210 for (int i = 0; i < pmyListCtrl->GetItemCount(); ++i) {
211 SourcenameItem *item = reinterpret_cast<SourcenameItem *>(pmyListCtrl->GetItemData(i));
212 if (item->count == 0) {
213 delete item;
214 pmyListCtrl->DeleteItem(i);
215 i--; // PA: one step back is enough, no need to go back to 0
216 } else {
217 pmyListCtrl->SetItem(i, 1, wxString::Format(wxT("%li"), item->count));
221 if (inserted) {
222 pmyListCtrl->SortList();
224 // no need to call Layout() here, it's called in UpdateData()
228 void CFileDetailDialog::OnBnClickedShowComment(wxCommandEvent& WXUNUSED(evt))
230 CCommentDialogLst(this,m_file).ShowModal();
234 void CFileDetailDialog::resetValueForFilenameTextEdit()
236 CastChild(IDC_FILENAME, wxTextCtrl)->SetValue(m_file->GetFileName().GetPrintable());
237 m_filenameChanged = false;
238 setEnableForApplyButton();
242 void CFileDetailDialog::setValueForFilenameTextEdit(const wxString &s)
244 CastChild(IDC_FILENAME, wxTextCtrl)->SetValue(s);
245 m_filenameChanged = true;
246 setEnableForApplyButton();
250 void CFileDetailDialog::setEnableForApplyButton()
252 bool enabled =
253 m_file->IsPartFile() && // Currently renaming of completed files causes problem with kad
254 m_file->GetStatus() != PS_COMPLETE &&
255 m_file->GetStatus() != PS_COMPLETING &&
256 m_filenameChanged;
257 CastChild(IDC_APPLY, wxControl)->Enable(enabled);
258 // Make OK button default so Text can be applied by hitting return
259 CastChild(enabled ? IDC_APPLY_AND_CLOSE : ID_CLOSEWNDFD, wxButton)->SetDefault();
263 void CFileDetailDialog::OnTextFileNameChange(wxCommandEvent& WXUNUSED(evt))
265 m_filenameChanged = true;
266 setEnableForApplyButton();
270 void CFileDetailDialog::OnBnClickedOk(wxCommandEvent& evt)
272 OnBnClickedApply(evt);
273 OnClosewnd(evt);
277 void CFileDetailDialog::OnBnClickedApply(wxCommandEvent& WXUNUSED(evt))
279 CPath fileName = CPath(CastChild(IDC_FILENAME, wxTextCtrl)->GetValue());
281 if (fileName.IsOk() && (fileName != m_file->GetFileName())) {
282 if (theApp->sharedfiles->RenameFile(m_file, fileName)) {
283 FindWindow(IDC_FNAME)->SetLabel(MakeStringEscaped(m_file->GetFileName().GetPrintable()));
284 FindWindow(IDC_METFILE)->SetLabel(m_file->GetFullName().GetPrintable());
286 resetValueForFilenameTextEdit();
288 Layout();
294 bool IsDigit(const wxChar ch)
296 switch (ch) {
297 case '0':
298 case '1':
299 case '2':
300 case '3':
301 case '4':
302 case '5':
303 case '6':
304 case '7':
305 case '8':
306 case '9': return true;
308 return false;
311 bool IsWordSeparator(const wxChar ch)
313 switch (ch) {
314 case '.':
315 case ',':
316 case '(':
317 case ')':
318 case '[':
319 case ']':
320 case '{':
321 case '}':
322 case '-':
323 case '"':
324 case ' ': return true;
326 return false;
329 void ReplaceWord(wxString& str, const wxString& replaceFrom, const wxString& replaceTo, bool numbers = false)
331 unsigned int i = 0;
332 unsigned int l = replaceFrom.Length();
333 while (i < str.Length()) {
334 if (str.Mid(i, l) == replaceFrom) {
335 if ((i == 0 || IsWordSeparator(str.GetChar(i-1))) &&
336 ((i == str.Length() - l || IsWordSeparator(str.GetChar(i+l))) ||
337 (numbers && IsDigit(str.GetChar(i+l))))) {
338 str.replace(i, l, replaceTo);
340 i += replaceTo.Length() - 1;
342 i++;
346 void CFileDetailDialog::OnBnClickedButtonStrip(wxCommandEvent& WXUNUSED(evt))
348 wxString filename;
349 filename = CastChild(IDC_FILENAME, wxTextCtrl)->GetValue();
351 int extpos = filename.Find('.', true);
352 wxString ext;
353 if (extpos > 0) {
354 // get the extension - we do not modify it except make it lowercase
355 ext = filename.Mid(extpos);
356 ext.MakeLower();
357 // get rid of extension and replace . with space
358 filename.Truncate(extpos);
359 filename.Replace(wxT("."),wxT(" "));
362 // Replace Space-holders with Spaces
363 filename.Replace(wxT("_"),wxT(" "));
364 filename.Replace(wxT("%20"),wxT(" "));
366 // Some additional formatting
367 filename.Replace(wxT("hYPNOTiC"), wxEmptyString);
368 filename.MakeLower();
369 filename.Replace(wxT("xxx"), wxT("XXX"));
370 // filename.Replace(wxT("xdmnx"), wxEmptyString);
371 // filename.Replace(wxT("pmp"), wxEmptyString);
372 // filename.Replace(wxT("dws"), wxEmptyString);
373 filename.Replace(wxT("www pornreactor com"), wxEmptyString);
374 filename.Replace(wxT("sharereactor"), wxEmptyString);
375 filename.Replace(wxT("found via www filedonkey com"), wxEmptyString);
376 filename.Replace(wxT("deviance"), wxEmptyString);
377 filename.Replace(wxT("adunanza"), wxEmptyString);
378 filename.Replace(wxT("-ftv"), wxEmptyString);
379 filename.Replace(wxT("flt"), wxEmptyString);
380 filename.Replace(wxT("[]"), wxEmptyString);
381 filename.Replace(wxT("()"), wxEmptyString);
383 // Change CD, CD#, VCD{,#}, DVD{,#}, ISO, PC to uppercase
384 ReplaceWord(filename, wxT("cd"), wxT("CD"), true);
385 ReplaceWord(filename, wxT("vcd"), wxT("VCD"), true);
386 ReplaceWord(filename, wxT("dvd"), wxT("DVD"), true);
387 ReplaceWord(filename, wxT("iso"), wxT("ISO"), false);
388 ReplaceWord(filename, wxT("pc"), wxT("PC"), false);
390 // Make leading Caps
391 // and delete 1+ spaces
392 if (filename.Length()>1)
394 bool last_char_space = true;
395 bool last_char_wordseparator = true;
396 unsigned int i = 0;
398 do {
399 wxChar c = filename.GetChar(i);
400 if (c == ' ') {
401 if (last_char_space) {
402 filename.Remove(i, 1);
403 i--;
404 } else {
405 last_char_space = true;
407 } else if (c == '.') {
408 if (last_char_space && i > 0) {
409 i--;
410 filename.Remove(i, 1);
412 last_char_space = false;
413 } else {
414 if (last_char_wordseparator) {
415 wxString tempStr(c);
416 tempStr.MakeUpper();
417 filename.SetChar(i, tempStr.GetChar(0));
418 last_char_space = false;
421 last_char_wordseparator = IsWordSeparator(c);
422 i++;
423 } while (i < filename.Length());
425 if (last_char_space && i > 0) {
426 filename.Remove(i-1, 1);
430 // should stay lowercase
431 ReplaceWord(filename, wxT("By"), wxT("by"));
433 // re-add extension
434 filename += ext;
436 setValueForFilenameTextEdit(filename);
439 void CFileDetailDialog::OnBnClickedTakeOver(wxCommandEvent& WXUNUSED(evt))
441 CFileDetailListCtrl* pmyListCtrl;
442 pmyListCtrl = CastChild( IDC_LISTCTRLFILENAMES, CFileDetailListCtrl );
443 if (pmyListCtrl->GetSelectedItemCount() > 0) {
444 long pos=-1;
445 for(;;) {
446 pos=pmyListCtrl->GetNextItem(pos,wxLIST_NEXT_ALL,wxLIST_STATE_SELECTED);
447 if(pos==-1) {
448 break;
450 setValueForFilenameTextEdit(pmyListCtrl->GetItemText(pos));
455 void CFileDetailDialog::OnListClickedTakeOver(wxListEvent& WXUNUSED(evt))
457 CFileDetailListCtrl* pmyListCtrl;
458 pmyListCtrl = CastChild( IDC_LISTCTRLFILENAMES, CFileDetailListCtrl );
459 if (pmyListCtrl->GetSelectedItemCount() > 0) {
460 long pos=-1;
461 for(;;) {
462 pos=pmyListCtrl->GetNextItem(pos,wxLIST_NEXT_ALL,wxLIST_STATE_SELECTED);
463 if(pos==-1) {
464 break;
466 setValueForFilenameTextEdit(pmyListCtrl->GetItemText(pos));
470 // File_checked_for_headers