[cstdstring] demise Format, replacing with StringUtils::Format
[vuplus_xbmc] / xbmc / utils / AsyncFileCopy.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "threads/SystemClock.h"
22 #include "AsyncFileCopy.h"
23 #include "dialogs/GUIDialogProgress.h"
24 #include "guilib/GUIWindowManager.h"
25 #include "log.h"
26 #include "utils/TimeUtils.h"
27 #include "utils/StringUtils.h"
28 #include "URL.h"
29
30 CAsyncFileCopy::CAsyncFileCopy() : CThread("AsyncFileCopy")
31 {
32   m_cancelled = false;
33   m_succeeded = false;
34   m_running = false;
35   m_percent = 0;
36   m_speed = 0;
37 }
38
39 CAsyncFileCopy::~CAsyncFileCopy()
40 {
41   StopThread();
42 }
43
44 bool CAsyncFileCopy::Copy(const CStdString &from, const CStdString &to, const CStdString &heading)
45 {
46   // reset the variables to their appropriate states
47   m_from = from;
48   m_to = to;
49   m_cancelled = false;
50   m_succeeded = false;
51   m_percent = 0;
52   m_speed = 0;
53   m_running = true;
54   CURL url1(from);
55   CURL url2(to);
56
57   // create our thread, which starts the file copy operation
58   Create();
59   CGUIDialogProgress *dlg = (CGUIDialogProgress *)g_windowManager.GetWindow(WINDOW_DIALOG_PROGRESS);
60   unsigned int time = XbmcThreads::SystemClockMillis();
61   while (m_running)
62   {
63     m_event.WaitMSec(1000 / 30);
64     if (!m_running)
65       break;
66     // start the dialog up as needed
67     if (dlg && !dlg->IsDialogRunning() && (XbmcThreads::SystemClockMillis() - time) > 500) // wait 0.5 seconds before starting dialog
68     {
69       dlg->SetHeading(heading);
70       dlg->SetLine(0, url1.GetWithoutUserDetails());
71       dlg->SetLine(1, url2.GetWithoutUserDetails());
72       dlg->SetPercentage(0);
73       dlg->StartModal();
74     }
75     // and update the dialog as we go
76     if (dlg && dlg->IsDialogRunning())
77     {
78       CStdString speedString = StringUtils::Format("%2.2f KB/s", m_speed / 1024);
79       dlg->SetHeading(heading);
80       dlg->SetLine(0, url1.Get());
81       dlg->SetLine(1, url2.Get());
82       dlg->SetLine(2, speedString);
83       dlg->SetPercentage(m_percent);
84       dlg->Progress();
85       m_cancelled = dlg->IsCanceled();
86     }
87   }
88   if (dlg)
89     dlg->Close();
90   return !m_cancelled && m_succeeded;
91 }
92
93 bool CAsyncFileCopy::OnFileCallback(void *pContext, int ipercent, float avgSpeed)
94 {
95   m_percent = ipercent;
96   m_speed = avgSpeed;
97   m_event.Set();
98   return !m_cancelled;
99 }
100
101 void CAsyncFileCopy::Process()
102 {
103   try
104   {
105     m_succeeded = XFILE::CFile::Cache(m_from, m_to, this);
106   }
107   catch (...)
108   {
109     m_succeeded = false;
110     CLog::Log(LOGERROR, "%s: unhandled exception copying file", __FUNCTION__);
111   }
112   m_running = false;
113 }