1 /* 2 * Copyright (C) 2017-2018 Team Kodi 3 * This file is part of Kodi - https://kodi.tv 4 * 5 * SPDX-License-Identifier: GPL-2.0-or-later 6 * See LICENSES/README.md for more information. 7 */ 8 9 #include "PVRGUIProgressHandler.h" 10 11 #include "ServiceBroker.h" 12 #include "dialogs/GUIDialogExtendedProgressBar.h" 13 #include "guilib/GUIComponent.h" 14 #include "guilib/GUIWindowManager.h" 15 #include "guilib/WindowIDs.h" 16 17 #include <algorithm> 18 #include <cmath> 19 #include <string> 20 21 namespace PVR 22 { CPVRGUIProgressHandler(const std::string & strTitle)23 CPVRGUIProgressHandler::CPVRGUIProgressHandler(const std::string& strTitle) 24 : CThread("PVRGUIProgressHandler"), 25 m_strTitle(strTitle), 26 m_fProgress(0.0f), 27 m_bChanged(false) 28 { 29 Create(true /* bAutoDelete */); 30 } 31 UpdateProgress(const std::string & strText,float fProgress)32 void CPVRGUIProgressHandler::UpdateProgress(const std::string& strText, float fProgress) 33 { 34 CSingleLock lock(m_critSection); 35 m_bChanged = true; 36 m_strText = strText; 37 m_fProgress = fProgress; 38 } 39 UpdateProgress(const std::string & strText,int iCurrent,int iMax)40 void CPVRGUIProgressHandler::UpdateProgress(const std::string& strText, int iCurrent, int iMax) 41 { 42 float fPercentage = (iCurrent * 100.0f) / iMax; 43 if (!std::isnan(fPercentage)) 44 fPercentage = std::min(100.0f, fPercentage); 45 46 UpdateProgress(strText, fPercentage); 47 } 48 DestroyProgress()49 void CPVRGUIProgressHandler::DestroyProgress() 50 { 51 CSingleLock lock(m_critSection); 52 m_bStop = true; 53 m_bChanged = false; 54 } 55 Process()56 void CPVRGUIProgressHandler::Process() 57 { 58 CGUIDialogExtendedProgressBar* progressBar = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogExtendedProgressBar>(WINDOW_DIALOG_EXT_PROGRESS); 59 if (m_bStop || !progressBar) 60 return; 61 62 CGUIDialogProgressBarHandle* progressHandle = progressBar->GetHandle(m_strTitle); 63 if (!progressHandle) 64 return; 65 66 while (!m_bStop) 67 { 68 float fProgress = 0.0; 69 std::string strText; 70 bool bUpdate = false; 71 72 { 73 CSingleLock lock(m_critSection); 74 if (m_bChanged) 75 { 76 m_bChanged = false; 77 fProgress = m_fProgress; 78 strText = m_strText; 79 bUpdate = true; 80 } 81 } 82 83 if (bUpdate) 84 { 85 progressHandle->SetPercentage(fProgress); 86 progressHandle->SetText(strText); 87 } 88 89 CThread::Sleep( 90 100); // Intentionally ignore some changes that come in too fast. Humans cannot read as fast as Mr. Data ;-) 91 } 92 93 progressHandle->MarkFinished(); 94 } 95 96 } // namespace PVR 97