Upstream tarball 9959
[amule.git] / src / PartFileConvertDlg.cpp
blobc5ec007399bf8d540be0dd0d5aed733111bd9768
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.
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 "PartFileConvertDlg.h"
28 #include <common/Format.h>
29 #include <common/Path.h>
30 #include "DataToText.h"
31 #include "OtherFunctions.h"
32 #include "PartFileConvert.h"
33 #include "GuiEvents.h"
35 #include <wx/stdpaths.h>
36 #include "muuli_wdr.h"
38 CPartFileConvertDlg* CPartFileConvertDlg::s_convertgui = NULL;
41 #ifndef __WXMSW__
42 /* XPM */
43 static const char * convert_xpm[] = {
44 "16 16 9 1",
45 " c None",
46 ". c #B20000",
47 "+ c #FF0000",
48 "@ c #FF7F7F",
49 "# c #008000",
50 "$ c #33B200",
51 "% c #10E500",
52 "& c #59FE4C",
53 "* c #FFB2B2",
54 " . ",
55 " .+. ",
56 " .+@+. ",
57 " .+@+. ",
58 ". .+@+.#######",
59 ".. .+@+. #$%%&#",
60 ".+.+@+. #$%%#",
61 ".@+@+. #$%$%#",
62 ".@@+. #$%$#$#",
63 ".*@@+. #$%$# ##",
64 ".......#$%$# #",
65 " #$%$# ",
66 " #$%$# ",
67 " #$%$# ",
68 " #$# ",
69 " # "};
70 #endif /* ! __WXMSW__ */
72 // Modeless Dialog Implementation
73 // CPartFileConvertDlg dialog
75 BEGIN_EVENT_TABLE(CPartFileConvertDlg, wxDialog)
76 #ifndef CLIENT_GUI
77 EVT_BUTTON(IDC_ADDITEM, CPartFileConvertDlg::OnAddFolder)
78 #endif
79 EVT_BUTTON(IDC_RETRY, CPartFileConvertDlg::RetrySel)
80 EVT_BUTTON(IDC_CONVREMOVE, CPartFileConvertDlg::RemoveSel)
81 EVT_BUTTON(wxID_CANCEL, CPartFileConvertDlg::OnCloseButton)
82 EVT_CLOSE(CPartFileConvertDlg::OnClose)
83 END_EVENT_TABLE()
85 CPartFileConvertDlg::CPartFileConvertDlg(wxWindow* parent)
86 : wxDialog(parent, -1, _("Import partfiles"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
88 convertDlg(this, true, true);
90 m_joblist = CastChild(IDC_JOBLIST, wxListCtrl);
91 m_pb_current = CastChild(IDC_CONV_PB_CURRENT, wxGauge);
93 m_joblist->InsertColumn(0, _("File name"), wxLIST_FORMAT_LEFT, 200);
94 m_joblist->InsertColumn(1, _("State"), wxLIST_FORMAT_LEFT, 100);
95 m_joblist->InsertColumn(2, _("Size"), wxLIST_FORMAT_LEFT, 100);
96 m_joblist->InsertColumn(3, _("Filehash"), wxLIST_FORMAT_LEFT, 100);
98 SetIcon(wxICON(convert));
100 #ifdef CLIENT_GUI
101 // There's no remote directory browser (yet)
102 CastChild(IDC_ADDITEM, wxButton)->Enable(false);
103 #endif
106 // Static methods
108 void CPartFileConvertDlg::ShowGUI(wxWindow* parent)
110 if (s_convertgui) {
111 s_convertgui->Show(true);
112 s_convertgui->Raise();
113 } else {
114 s_convertgui = new CPartFileConvertDlg(parent);
115 s_convertgui->Show(true);
116 Notify_ConvertReaddAllJobs();
120 void CPartFileConvertDlg::CloseGUI()
122 if (s_convertgui) {
123 s_convertgui->Show(false);
124 s_convertgui->Destroy();
125 s_convertgui = NULL;
129 void CPartFileConvertDlg::UpdateProgress(float percent, wxString text, wxString header)
131 if (s_convertgui) {
132 s_convertgui->m_pb_current->SetValue((int)percent);
133 wxString buffer = wxString::Format(wxT("%.2f %%"), percent);
134 wxStaticText* percentlabel = dynamic_cast<wxStaticText*>(s_convertgui->FindWindow(IDC_CONV_PROZENT));
135 percentlabel->SetLabel(buffer);
137 if (!text.IsEmpty()) {
138 dynamic_cast<wxStaticText*>(s_convertgui->FindWindow(IDC_CONV_PB_LABEL))->SetLabel(text);
141 percentlabel->GetParent()->Layout();
143 if (!header.IsEmpty()) {
144 dynamic_cast<wxStaticBoxSizer*>(IDC_CURJOB)->GetStaticBox()->SetLabel(header);
149 void CPartFileConvertDlg::ClearInfo()
151 if (s_convertgui) {
152 dynamic_cast<wxStaticBoxSizer*>(IDC_CURJOB)->GetStaticBox()->SetLabel(_("Waiting..."));
153 dynamic_cast<wxStaticText*>(s_convertgui->FindWindow(IDC_CONV_PROZENT))->SetLabel(wxEmptyString);
154 s_convertgui->m_pb_current->SetValue(0);
155 dynamic_cast<wxStaticText*>(s_convertgui->FindWindow(IDC_CONV_PB_LABEL))->SetLabel(wxEmptyString);
159 void CPartFileConvertDlg::UpdateJobInfo(ConvertInfo& info)
161 if (s_convertgui) {
162 // search jobitem in listctrl
163 long itemnr = s_convertgui->m_joblist->FindItem(-1, info.id);
164 // if it does not exist, add it
165 if (itemnr == -1) {
166 itemnr = s_convertgui->m_joblist->InsertItem(s_convertgui->m_joblist->GetItemCount(), info.folder.GetPrintable());
167 if (itemnr != -1) {
168 s_convertgui->m_joblist->SetItemData(itemnr, info.id);
171 // update columns
172 if (itemnr != -1) {
173 s_convertgui->m_joblist->SetItem(itemnr, 0, info.filename.IsOk() ? info.folder.GetPrintable() : info.filename.GetPrintable() );
174 s_convertgui->m_joblist->SetItem(itemnr, 1, GetConversionState(info.state) );
175 if (info.size > 0) {
176 s_convertgui->m_joblist->SetItem(itemnr, 2, CFormat(_("%s (Disk: %s)")) % CastItoXBytes(info.size) % CastItoXBytes(info.spaceneeded));
177 } else {
178 s_convertgui->m_joblist->SetItem(itemnr, 2, wxEmptyString);
180 s_convertgui->m_joblist->SetItem(itemnr, 3, info.filehash);
185 void CPartFileConvertDlg::RemoveJobInfo(unsigned id)
187 if (s_convertgui) {
188 long itemnr = s_convertgui->m_joblist->FindItem(-1, id);
189 if (itemnr != -1) {
190 s_convertgui->m_joblist->DeleteItem(itemnr);
195 // CPartFileConvertDlg message handlers
197 #ifndef CLIENT_GUI
198 void CPartFileConvertDlg::OnAddFolder(wxCommandEvent& WXUNUSED(event))
200 // TODO: use MuleRemoteDirSelector
201 wxString folder = ::wxDirSelector(
202 _("Please choose a folder to search for temporary downloads! (subfolders will be included)"),
203 wxStandardPaths::Get().GetDocumentsDir(), wxDD_DEFAULT_STYLE,
204 wxDefaultPosition, this);
205 if (!folder.IsEmpty()) {
206 int reply = wxMessageBox(_("Do you want the source files of succesfully imported downloads be deleted?"),
207 _("Remove sources?"),
208 wxYES_NO | wxCANCEL | wxICON_QUESTION, this);
209 if (reply != wxCANCEL) {
210 // TODO: use notification
211 CPartFileConvert::ScanFolderToAdd(CPath(folder), (reply == wxYES));
215 #endif
217 void CPartFileConvertDlg::OnClose(wxCloseEvent& WXUNUSED(event))
219 CloseGUI();
222 void CPartFileConvertDlg::OnCloseButton(wxCommandEvent& WXUNUSED(event))
224 CloseGUI();
227 void CPartFileConvertDlg::RemoveSel(wxCommandEvent& WXUNUSED(event))
229 if (m_joblist->GetSelectedItemCount() == 0) return;
231 long itemnr = m_joblist->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
232 while (itemnr != -1) {
233 Notify_ConvertRemoveJob(m_joblist->GetItemData(itemnr));
234 itemnr = m_joblist->GetNextItem(itemnr, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
238 void CPartFileConvertDlg::RetrySel(wxCommandEvent& WXUNUSED(event))
240 if (m_joblist->GetSelectedItemCount() == 0) return;
242 long itemnr = m_joblist->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
243 while (itemnr != -1) {
244 Notify_ConvertRetryJob(m_joblist->GetItemData(itemnr));
245 itemnr = m_joblist->GetNextItem(itemnr, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);