1 //
2 // C++ Implementation: InProgressList
3 //
4 // Description: Implements the list of downloads in progress.
5 //
6 //
7 // Author: Max Magalhães Velasques <maxvelasques@gmail.com>, (C) 2006
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12 
13 #include "wxDFast.h"
14 
IMPLEMENT_DYNAMIC_CLASS(mInProgressList,wxListCtrl)15 IMPLEMENT_DYNAMIC_CLASS(mInProgressList, wxListCtrl)
16 
17 BEGIN_EVENT_TABLE(mInProgressList, wxListCtrl)
18     EVT_LIST_ITEM_SELECTED(XRCID("inprogresslist"), mInProgressList::OnSelected)
19     EVT_LIST_ITEM_DESELECTED(XRCID("inprogresslist"), mInProgressList::OnDeselected)
20     EVT_LIST_ITEM_RIGHT_CLICK(XRCID("inprogresslist"), mInProgressList::OnRClick)
21     EVT_LIST_ITEM_ACTIVATED(XRCID("inprogresslist"), mInProgressList::OnDoubleClick)
22     EVT_ENTER_WINDOW(mInProgressList::OnEnterWindow)
23     EVT_LEAVE_WINDOW(mInProgressList::OnLeaveWindow)
24 END_EVENT_TABLE()
25 
26 mInProgressList::mInProgressList()
27 {
28     this->handleselectdeselectevents = TRUE;
29     this->lastselection = -1;
30 }
31 
OnEnterWindow(wxMouseEvent & event)32 void mInProgressList::OnEnterWindow(wxMouseEvent& event)
33 {
34     if (mainframe->statusbar)
35         mainframe->statusbar->SetStatusText(_("Double-click on the item to Start/Stop the download"));
36 }
37 
OnLeaveWindow(wxMouseEvent & event)38 void mInProgressList::OnLeaveWindow(wxMouseEvent& event)
39 {
40     if (mainframe->statusbar)
41         mainframe->statusbar->SetStatusText(mainframe->defaultstatusbarmessage);
42 }
43 
OnRClick(wxListEvent & event)44 void mInProgressList::OnRClick(wxListEvent& event)
45 {
46     if (this->GetCurrentSelection().GetCount() >= 0)
47     {
48         mainframe->menupopup->Enable(XRCID("menuschedule"),TRUE);
49         mainframe->menupopup->Enable(XRCID("menustart"),TRUE);
50         mainframe->menupopup->Enable(XRCID("menustop"),TRUE);
51         mainframe->menupopup->Enable(XRCID("menuremove"),TRUE);
52         mainframe->menupopup->Enable(XRCID("menumove"),FALSE);
53         mainframe->menupopup->Enable(XRCID("menucopyurl"),TRUE);
54         mainframe->menupopup->Enable(XRCID("menucopydownloaddata"),TRUE);
55         mainframe->menupopup->Enable(XRCID("menumd5"),FALSE);
56         mainframe->menupopup->Enable(XRCID("menuopendestination"),FALSE);
57         mainframe->menupopup->Enable(XRCID("menuproperties"),TRUE);
58         mainframe->menupopup->Enable(XRCID("menuagain"),FALSE);
59         mainframe->PopupMenu(mainframe->menupopup,event.GetPoint());
60     }
61 }
62 
OnDoubleClick(wxListEvent & event)63 void mInProgressList::OnDoubleClick(wxListEvent& event)
64 {
65     if (this->GetCurrentSelection().GetCount() > 0)
66     {
67         mDownloadList::Node *node = wxGetApp().downloadlist.Item(this->GetCurrentLastSelection());
68         if (node)
69         {
70             mDownloadFile *file = node->GetData();
71             wxCommandEvent event;
72             int status = file->GetStatus();
73             if (status == STATUS_STOPED)
74                 mainframe->OnStart(event);
75             else if ((status == STATUS_ACTIVE) || (status == STATUS_QUEUE) || (status == STATUS_SCHEDULE_QUEUE))
76                 mainframe->OnStop(event);
77         }
78     }
79 }
80 
GetCurrentLastSelection()81 int mInProgressList::GetCurrentLastSelection()
82 {
83     if (this->lastselection >= this->GetItemCount())
84         this->lastselection = -1;
85     return this->lastselection;
86 }
87 
GetCurrentSelection()88 mListSelection mInProgressList::GetCurrentSelection()
89 {
90     wxNotebook *notebook = XRCCTRL(*(mainframe), "notebook01",wxNotebook );
91     mListSelection selection;
92     if (notebook->GetSelection() == 0)
93     {
94         int j;
95         for (j = 0 ; j < this->GetItemCount();j++)
96             if (this->GetItemState(j,wxLIST_STATE_SELECTED)&wxLIST_STATE_SELECTED )
97                 selection.Add(j);
98     }
99     return selection;
100 }
101 
SetCurrentSelection(int selection)102 void mInProgressList::SetCurrentSelection(int selection)
103 {
104     int i;
105     for (i=0;i<GetItemCount();i++)
106         this->SetItemState(i,0,wxLIST_STATE_SELECTED);
107     if (selection >= 0)
108         this->SetItemState(selection,wxLIST_STATE_SELECTED,wxLIST_STATE_SELECTED);
109     this->lastselection = selection;
110 }
111 
OnSelected(wxListEvent & event)112 void mInProgressList::OnSelected(wxListEvent& event)
113 {
114     if (this->handleselectdeselectevents)
115         this->SelectUnselect(TRUE,event.GetIndex());
116 }
117 
OnDeselected(wxListEvent & event)118 void mInProgressList::OnDeselected(wxListEvent& event)
119 {
120     if (this->handleselectdeselectevents)
121         if (this->GetCurrentSelection().GetCount()==0)
122             this->SelectUnselect(FALSE,-1);
123 }
124 
SelectUnselect(bool selected,int selection)125 void mInProgressList::SelectUnselect(bool selected,int selection)
126 {
127     if (!selected)
128     {
129         wxString contentstring = _("File type");
130         contentstring += wxT(":\n");
131         XRCCTRL(*mainframe, "btnpreview", wxButton )->Enable(FALSE);
132         XRCCTRL(*mainframe, "lblpreview", wxStaticText )->SetLabel(contentstring);
133         this->SetCurrentSelection(selection);
134     }
135     this->lastselection = selection;
136     if (mainframe->menubar)
137     {
138         mainframe->menubar->GetMenu(0)->Enable(XRCID("menuremove"),selected);
139         mainframe->menubar->GetMenu(0)->Enable(XRCID("menuschedule"),selected);
140         mainframe->menubar->GetMenu(0)->Enable(XRCID("menustart"),selected);
141         mainframe->menubar->GetMenu(0)->Enable(XRCID("menustop"),selected);
142         if (this->GetItemCount() == 0)
143         {
144             mainframe->menubar->GetMenu(0)->Enable(XRCID("menustartall"),FALSE);
145             mainframe->menubar->GetMenu(0)->Enable(XRCID("menustopall"),FALSE);
146         }
147         else
148             mainframe->menubar->GetMenu(0)->Enable(XRCID("menustartall"),TRUE);
149             mainframe->menubar->GetMenu(0)->Enable(XRCID("menustopall"),TRUE);
150         mainframe->menubar->GetMenu(1)->Enable(XRCID("menucopyurl"),selected);
151         mainframe->menubar->GetMenu(1)->Enable(XRCID("menucopydownloaddata"),selected);
152         mainframe->menubar->GetMenu(3)->Enable(XRCID("menuproperties"),selected);
153         mainframe->menubar->GetMenu(3)->Enable(XRCID("menumove"),FALSE);
154         mainframe->menubar->GetMenu(3)->Enable(XRCID("menumd5"),FALSE);
155         mainframe->menubar->GetMenu(3)->Enable(XRCID("menuopendestination"),FALSE);
156         mainframe->menubar->GetMenu(3)->Enable(XRCID("menuagain"),FALSE);
157     }
158     if (mainframe->toolbar)
159     {
160         mainframe->toolbar-> EnableTool(XRCID("toolremove"),selected);
161         mainframe->toolbar-> EnableTool(XRCID("toolschedule"),selected);
162         mainframe->toolbar-> EnableTool(XRCID("toolstart"),selected);
163         mainframe->toolbar-> EnableTool(XRCID("toolstop"),selected);
164         if (this->GetItemCount() == 0)
165         {
166             mainframe->toolbar->EnableTool(XRCID("toolstartall"),FALSE);
167             mainframe->toolbar->EnableTool(XRCID("toolstopall"),FALSE);
168         }
169         else
170         {
171             mainframe->toolbar->EnableTool(XRCID("toolstartall"),TRUE);
172             mainframe->toolbar->EnableTool(XRCID("toolstopall"),TRUE);
173         }
174         mainframe->toolbar-> EnableTool(XRCID("toolup"),selected);
175         mainframe->toolbar-> EnableTool(XRCID("tooldown"),selected);
176         mainframe->toolbar-> EnableTool(XRCID("toolproperties"),selected);
177     }
178 }
179 
Insert(mDownloadFile * current,int item,bool ontop)180 int mInProgressList::Insert(mDownloadFile *current, int item,bool ontop)
181 {
182     long tmp;
183     int currentstatus;
184     bool newdownload = false;
185     wxString currentstring;
186     wxListItem listitem;
187 
188     listitem.SetId(item);
189     listitem.SetMask(wxLIST_MASK_DATA|wxLIST_MASK_STATE|wxLIST_MASK_TEXT|wxLIST_MASK_IMAGE);
190     if (current != NULL)
191     {
192         currentstatus = current->GetStatus();
193         if (item == -1)
194         {
195             if (ontop)
196                 item = 0;
197             else
198                 item = this->GetItemCount();
199 
200             tmp = this->InsertItem(item, wxEmptyString,currentstatus);
201             this->SetItemData(tmp, item);
202             this->SetItem(item, INPROGRESS_ICON01, wxEmptyString,currentstatus);
203             newdownload = true;
204         }
205         else
206         {
207             listitem.SetColumn(INPROGRESS_ICON01);
208             this->GetItem(listitem);
209             if (currentstatus != listitem.GetImage())
210             {
211                 listitem.SetImage(currentstatus);
212                 this->SetItem(listitem);
213                 current->SetChangedSinceLastSave();
214             }
215         }
216         //UPDATE RESTART SUPPORT
217         if (current->RestartSupport() == YES)
218             currentstring = _("   [ Yes ]");
219         else if (current->RestartSupport() == NO)
220             currentstring = _("   [ No  ]");
221         else
222             currentstring = wxT("   [     ]");
223         if (!newdownload)
224         {
225             listitem.SetColumn(INPROGRESS_ICON02);
226             this->GetItem(listitem);
227             if (listitem.GetText() != currentstring)
228             {
229                 this->SetItem(item, INPROGRESS_ICON02, currentstring);
230                 current->SetChangedSinceLastSave();
231             }
232         }
233         else
234             this->SetItem(item, INPROGRESS_ICON02, currentstring);
235 
236         //UPDATE NAME STRING
237         currentstring = current->GetExposedName();
238         if (!newdownload)
239         {
240             listitem.SetColumn(INPROGRESS_NAME);
241             this->GetItem(listitem);
242             if (listitem.GetText() != currentstring)
243             {
244                 this->SetItem(item, INPROGRESS_NAME, currentstring);
245                 current->SetChangedSinceLastSave();
246             }
247         }
248         else
249             this->SetItem(item, INPROGRESS_NAME, currentstring);
250 
251         //UPDATE TOTAL SIZE STRING
252         currentstring = MyUtilFunctions::ByteString(current->totalsize);
253         if (!newdownload)
254         {
255             listitem.SetColumn(INPROGRESS_SIZE);
256             this->GetItem(listitem);
257             if (listitem.GetText() != currentstring)
258             {
259                 this->SetItem(item, INPROGRESS_SIZE, currentstring );
260                 current->SetChangedSinceLastSave();
261             }
262         }
263         else
264             this->SetItem(item, INPROGRESS_SIZE, currentstring );
265 
266 
267         //UPDATE TOTAL SIZE COMPLETED, PERCENTUAL, TIME PASSED, TIME REMAINING AND SPEED
268         currentstring = MyUtilFunctions::ByteString(current->timepassed);
269         if (!newdownload)
270         {
271             listitem.SetColumn(INPROGRESS_TIMEPASSED);
272             this->GetItem(listitem);
273             if (listitem.GetText() != currentstring)
274             {
275                 this->SetItem(item, INPROGRESS_COMPLETED, MyUtilFunctions::ByteString(current->totalsizecompleted));
276                 this->SetItem(item, INPROGRESS_PERCENTUAL, MyUtilFunctions::int2wxstr(current->GetProgress()) + wxT(" %"));
277                 this->SetItem(item, INPROGRESS_TIMEPASSED, MyUtilFunctions::TimeString(current->timepassed));
278                 this->SetItem(item, INPROGRESS_TIMEREMAINING, MyUtilFunctions::TimeString(current->timeremaining));
279                 this->SetItem(item, INPROGRESS_SPEED, MyUtilFunctions::ByteString(current->totalspeed)+wxT("/s"));
280                 current->SetChangedSinceLastSave();
281             }
282         }
283         else
284         {
285             this->SetItem(item, INPROGRESS_COMPLETED, MyUtilFunctions::ByteString(current->totalsizecompleted));
286             this->SetItem(item, INPROGRESS_PERCENTUAL, MyUtilFunctions::int2wxstr(current->GetProgress()) + wxT(" %"));
287             this->SetItem(item, INPROGRESS_TIMEPASSED, MyUtilFunctions::TimeString(current->timepassed));
288             this->SetItem(item, INPROGRESS_TIMEREMAINING, MyUtilFunctions::TimeString(current->timeremaining));
289             this->SetItem(item, INPROGRESS_SPEED, MyUtilFunctions::ByteString(current->totalspeed)+wxT("/s"));
290         }
291 
292         //UPDATE NUMBER OF ATTEMPTS
293         currentstring = MyUtilFunctions::int2wxstr(current->GetCurrentAttempt());
294         if (!newdownload)
295         {
296             listitem.SetColumn(INPROGRESS_ATTEMPTS);
297             this->GetItem(listitem);
298             if (listitem.GetText() != currentstring)
299             {
300                 this->SetItem(item, INPROGRESS_ATTEMPTS, currentstring);
301                 current->SetChangedSinceLastSave();
302             }
303         }
304         else
305             this->SetItem(item, INPROGRESS_ATTEMPTS, currentstring);
306 
307         //UPDATE URL
308         currentstring = current->GetFirstUrl().GetFullPath();
309         if (!newdownload)
310         {
311             listitem.SetColumn(INPROGRESS_URL);
312             this->GetItem(listitem);
313             if (listitem.GetText() != currentstring)
314             {
315                 this->SetItem(item, INPROGRESS_URL, currentstring);
316                 current->SetChangedSinceLastSave();
317             }
318         }
319         else
320             this->SetItem(item, INPROGRESS_URL, currentstring);
321     }
322     return item;
323 }
324 
HandleSelectDeselectEvents(bool value)325 void mInProgressList::HandleSelectDeselectEvents(bool value)
326 {
327     this->handleselectdeselectevents = value;
328 }
329 
GenerateList(wxImageList * imageslist)330 void mInProgressList::GenerateList(wxImageList *imageslist)
331 {
332     SelectUnselect(FALSE,-1);
333     this->ClearAll();
334     this->SetImageList(imageslist, wxIMAGE_LIST_SMALL);
335 
336     wxListItem itemCol;
337 
338     itemCol.m_mask = wxLIST_MASK_DATA|wxLIST_MASK_STATE|wxLIST_MASK_TEXT|wxLIST_MASK_IMAGE;
339     itemCol.m_text = wxEmptyString;
340     itemCol.m_image = -1;
341     itemCol.SetAlign(wxLIST_FORMAT_LEFT);
342 
343     //CREATE THE COLUMNS
344     this->InsertColumn(INPROGRESS_ICON01, itemCol);
345 
346     itemCol.m_text = _("Restart");
347     this->InsertColumn(INPROGRESS_ICON02, itemCol);
348 
349     itemCol.m_text = _("Filename");
350     this->InsertColumn(INPROGRESS_NAME, itemCol);
351 
352     itemCol.m_text = _("Size");
353     this->InsertColumn(INPROGRESS_SIZE, itemCol);
354 
355     itemCol.m_text = _("Completed");
356     this->InsertColumn(INPROGRESS_COMPLETED, itemCol);
357 
358     itemCol.m_text = _("Percentage");
359     this->InsertColumn(INPROGRESS_PERCENTUAL, itemCol);
360 
361     itemCol.m_text = _("Time Passed");
362     this->InsertColumn(INPROGRESS_TIMEPASSED, itemCol);
363 
364     itemCol.m_text = _("Remaining");
365     this->InsertColumn(INPROGRESS_TIMEREMAINING, itemCol);
366 
367     itemCol.m_text = _("Speed");
368     this->InsertColumn(INPROGRESS_SPEED, itemCol);
369 
370     itemCol.m_text = _("Attempts");
371     this->InsertColumn(INPROGRESS_ATTEMPTS, itemCol);
372 
373     itemCol.m_text = _("URL");
374     this->InsertColumn(INPROGRESS_URL, itemCol);
375 
376     this->Hide();
377     {
378         this->SetColumnWidth(INPROGRESS_ICON01,20);
379         this->SetColumnWidth(INPROGRESS_ICON02,80);
380         this->SetColumnWidth(INPROGRESS_NAME,160);
381         this->SetColumnWidth(INPROGRESS_SIZE,100);
382         this->SetColumnWidth(INPROGRESS_COMPLETED,100);
383         this->SetColumnWidth(INPROGRESS_PERCENTUAL,100);
384         this->SetColumnWidth(INPROGRESS_TIMEPASSED,100);
385         this->SetColumnWidth(INPROGRESS_TIMEREMAINING,100);
386         this->SetColumnWidth(INPROGRESS_SPEED,100);
387         this->SetColumnWidth(INPROGRESS_ATTEMPTS,100);
388         this->SetColumnWidth(INPROGRESS_URL,300);
389     }
390     //wxGetApp().downloadlist.RecreateIndex();
391     for ( mDownloadList::Node *node = wxGetApp().downloadlist.GetFirst(); node; node = node->GetNext() )
392     {
393         mDownloadFile *current = node->GetData();
394         this->Insert(current,-1);
395     }
396 
397     this->SelectUnselect(FALSE,-1);
398     this->Show();
399     this->lastselection = -1;
400 }
401