summaryrefslogtreecommitdiffstats
path: root/xbmc/pvr/guilib/PVRGUIProgressHandler.cpp
blob: f8765227ddb31b776af210cdafd18ffd4b3d4ab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 *  Copyright (C) 2017-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "PVRGUIProgressHandler.h"

#include "ServiceBroker.h"
#include "dialogs/GUIDialogExtendedProgressBar.h"
#include "guilib/GUIComponent.h"
#include "guilib/GUIWindowManager.h"
#include "guilib/WindowIDs.h"

#include <algorithm>
#include <cmath>
#include <mutex>
#include <string>

using namespace std::chrono_literals;

namespace PVR
{

CPVRGUIProgressHandler::CPVRGUIProgressHandler(const std::string& strTitle)
  : CThread("PVRGUIProgressHandler"), m_strTitle(strTitle)
{
}

void CPVRGUIProgressHandler::UpdateProgress(const std::string& strText, float fProgress)
{
  std::unique_lock<CCriticalSection> lock(m_critSection);
  m_bChanged = true;
  m_strText = strText;
  m_fProgress = fProgress;

  if (!m_bCreated)
  {
    m_bCreated = true;
    Create();
  }
}

void CPVRGUIProgressHandler::UpdateProgress(const std::string& strText, int iCurrent, int iMax)
{
  float fPercentage = (iCurrent * 100.0f) / iMax;
  if (!std::isnan(fPercentage))
    fPercentage = std::min(100.0f, fPercentage);

  UpdateProgress(strText, fPercentage);
}

void CPVRGUIProgressHandler::Process()
{
  CGUIDialogExtendedProgressBar* progressBar =
      CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogExtendedProgressBar>(
          WINDOW_DIALOG_EXT_PROGRESS);
  if (m_bStop || !progressBar)
    return;

  CGUIDialogProgressBarHandle* progressHandle = progressBar->GetHandle(m_strTitle);
  if (!progressHandle)
    return;

  while (!m_bStop)
  {
    float fProgress = 0.0;
    std::string strText;
    bool bUpdate = false;

    {
      std::unique_lock<CCriticalSection> lock(m_critSection);
      if (m_bChanged)
      {
        m_bChanged = false;
        fProgress = m_fProgress;
        strText = m_strText;
        bUpdate = true;
      }
    }

    if (bUpdate)
    {
      progressHandle->SetPercentage(fProgress);
      progressHandle->SetText(strText);
    }

    // Intentionally ignore some changes that come in too fast. Humans cannot read as fast as Mr. Data ;-)
    CThread::Sleep(100ms);
  }

  progressHandle->MarkFinished();
}

} // namespace PVR