1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2011 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.
10 //
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.
15 //
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
24 //
25 
26 #include "PartFileConvertDlg.h"
27 
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"
34 
35 #include <wx/stdpaths.h>
36 #include "muuli_wdr.h"
37 
38 CPartFileConvertDlg*	CPartFileConvertDlg::s_convertgui = NULL;
39 
40 
41 #ifndef __WINDOWS__
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 /* ! __WINDOWS__ */
71 
72 // Modeless Dialog Implementation
73 // CPartFileConvertDlg dialog
74 
BEGIN_EVENT_TABLE(CPartFileConvertDlg,wxDialog)75 BEGIN_EVENT_TABLE(CPartFileConvertDlg, wxDialog)
76 	EVT_BUTTON(IDC_ADDITEM,		CPartFileConvertDlg::OnAddFolder)
77 	EVT_BUTTON(IDC_RETRY,		CPartFileConvertDlg::RetrySel)
78 	EVT_BUTTON(IDC_CONVREMOVE,	CPartFileConvertDlg::RemoveSel)
79 	EVT_BUTTON(wxID_CANCEL,		CPartFileConvertDlg::OnCloseButton)
80 	EVT_CLOSE(CPartFileConvertDlg::OnClose)
81 END_EVENT_TABLE()
82 
83 CPartFileConvertDlg::CPartFileConvertDlg(wxWindow* parent)
84 	: wxDialog(parent, -1, _("Import partfiles"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
85 {
86 	convertDlg(this, true, true);
87 
88 	m_joblist = CastChild(IDC_JOBLIST, wxListCtrl);
89 	m_pb_current = CastChild(IDC_CONV_PB_CURRENT, wxGauge);
90 
91 	m_joblist->InsertColumn(0, _("File name"),	wxLIST_FORMAT_LEFT, 200);
92 	m_joblist->InsertColumn(1, _("State"),		wxLIST_FORMAT_LEFT, 100);
93 	m_joblist->InsertColumn(2, _("Size"),		wxLIST_FORMAT_LEFT, 100);
94 	m_joblist->InsertColumn(3, _("Filehash"),	wxLIST_FORMAT_LEFT, 100);
95 
96 	SetIcon(wxICON(convert));
97 
98 #ifdef CLIENT_GUI
99 	// There's no remote directory browser (yet), thus disable the
100 	// directory selector unless we're using a localhost connection
101 	if (!theApp->m_connect->IsConnectedToLocalHost()) {
102 		CastChild(IDC_ADDITEM, wxButton)->Enable(false);
103 	}
104 #endif
105 }
106 
107 // Static methods
108 
ShowGUI(wxWindow * parent)109 void CPartFileConvertDlg::ShowGUI(wxWindow* parent)
110 {
111 	if (s_convertgui) {
112 		s_convertgui->Show(true);
113 		s_convertgui->Raise();
114 	} else {
115 		s_convertgui = new CPartFileConvertDlg(parent);
116 		s_convertgui->Show(true);
117 		Notify_ConvertReaddAllJobs();
118 	}
119 }
120 
CloseGUI()121 void CPartFileConvertDlg::CloseGUI()
122 {
123 	if (s_convertgui) {
124 		s_convertgui->Show(false);
125 		s_convertgui->Destroy();
126 		s_convertgui = NULL;
127 	}
128 }
129 
UpdateProgress(float percent,wxString text,wxString header)130 void CPartFileConvertDlg::UpdateProgress(float percent, wxString text, wxString header)
131 {
132 	if (s_convertgui) {
133 		s_convertgui->m_pb_current->SetValue((int)percent);
134 		wxString buffer = CFormat(wxT("%.2f %%")) % percent;
135 		wxStaticText* percentlabel = dynamic_cast<wxStaticText*>(s_convertgui->FindWindow(IDC_CONV_PROZENT));
136 		percentlabel->SetLabel(buffer);
137 
138 		if (!text.IsEmpty()) {
139 			dynamic_cast<wxStaticText*>(s_convertgui->FindWindow(IDC_CONV_PB_LABEL))->SetLabel(text);
140 		}
141 
142 		percentlabel->GetParent()->Layout();
143 
144 		if (!header.IsEmpty()) {
145 			dynamic_cast<wxStaticBoxSizer*>(IDC_CURJOB)->GetStaticBox()->SetLabel(header);
146 		}
147 	}
148 }
149 
ClearInfo()150 void CPartFileConvertDlg::ClearInfo()
151 {
152 	if (s_convertgui) {
153 		dynamic_cast<wxStaticBoxSizer*>(IDC_CURJOB)->GetStaticBox()->SetLabel(_("Waiting..."));
154 		dynamic_cast<wxStaticText*>(s_convertgui->FindWindow(IDC_CONV_PROZENT))->SetLabel(wxEmptyString);
155 		s_convertgui->m_pb_current->SetValue(0);
156 		dynamic_cast<wxStaticText*>(s_convertgui->FindWindow(IDC_CONV_PB_LABEL))->SetLabel(wxEmptyString);
157 	}
158 }
159 
UpdateJobInfo(ConvertInfo & info)160 void CPartFileConvertDlg::UpdateJobInfo(ConvertInfo& info)
161 {
162 	if (s_convertgui) {
163 		// search jobitem in listctrl
164 		long itemnr = s_convertgui->m_joblist->FindItem(-1, info.id);
165 		// if it does not exist, add it
166 		if (itemnr == -1) {
167 			itemnr = s_convertgui->m_joblist->InsertItem(s_convertgui->m_joblist->GetItemCount(), info.folder.GetPrintable());
168 			if (itemnr != -1) {
169 				s_convertgui->m_joblist->SetItemData(itemnr, info.id);
170 			}
171 		}
172 		// update columns
173 		if (itemnr != -1) {
174 			s_convertgui->m_joblist->SetItem(itemnr, 0, info.filename.IsOk() ? info.folder.GetPrintable() : info.filename.GetPrintable() );
175 			s_convertgui->m_joblist->SetItem(itemnr, 1, GetConversionState(info.state) );
176 			if (info.size > 0) {
177 				s_convertgui->m_joblist->SetItem(itemnr, 2, CFormat(_("%s (Disk: %s)")) % CastItoXBytes(info.size) % CastItoXBytes(info.spaceneeded));
178 			} else {
179 				s_convertgui->m_joblist->SetItem(itemnr, 2, wxEmptyString);
180 			}
181 			s_convertgui->m_joblist->SetItem(itemnr, 3, info.filehash);
182 		}
183 	}
184 }
185 
RemoveJobInfo(unsigned id)186 void CPartFileConvertDlg::RemoveJobInfo(unsigned id)
187 {
188 	if (s_convertgui) {
189 		long itemnr = s_convertgui->m_joblist->FindItem(-1, id);
190 		if (itemnr != -1) {
191 			s_convertgui->m_joblist->DeleteItem(itemnr);
192 		}
193 	}
194 }
195 
196 // CPartFileConvertDlg message handlers
197 
OnAddFolder(wxCommandEvent & WXUNUSED (event))198 void CPartFileConvertDlg::OnAddFolder(wxCommandEvent& WXUNUSED(event))
199 {
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));
212 		}
213 	}
214 }
215 
OnClose(wxCloseEvent & WXUNUSED (event))216 void CPartFileConvertDlg::OnClose(wxCloseEvent& WXUNUSED(event))
217 {
218 	CloseGUI();
219 }
220 
OnCloseButton(wxCommandEvent & WXUNUSED (event))221 void CPartFileConvertDlg::OnCloseButton(wxCommandEvent& WXUNUSED(event))
222 {
223 	CloseGUI();
224 }
225 
RemoveSel(wxCommandEvent & WXUNUSED (event))226 void CPartFileConvertDlg::RemoveSel(wxCommandEvent& WXUNUSED(event))
227 {
228 	if (m_joblist->GetSelectedItemCount() == 0) return;
229 
230 	long itemnr = m_joblist->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
231 	while (itemnr != -1) {
232 		Notify_ConvertRemoveJob(m_joblist->GetItemData(itemnr));
233 		itemnr = m_joblist->GetNextItem(itemnr, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
234 	}
235 }
236 
RetrySel(wxCommandEvent & WXUNUSED (event))237 void CPartFileConvertDlg::RetrySel(wxCommandEvent& WXUNUSED(event))
238 {
239 	if (m_joblist->GetSelectedItemCount() == 0) return;
240 
241 	long itemnr = m_joblist->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
242 	while (itemnr != -1) {
243 		Notify_ConvertRetryJob(m_joblist->GetItemData(itemnr));
244 		itemnr = m_joblist->GetNextItem(itemnr, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
245 	}
246 }
247