summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/ProgressJob.cpp
blob: 6ef1f24a72bce33d3112a996773c16a1cad82cad (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 *  Copyright (C) 2015-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 "ProgressJob.h"

#include "ServiceBroker.h"
#include "dialogs/GUIDialogExtendedProgressBar.h"
#include "dialogs/GUIDialogProgress.h"
#include "guilib/GUIComponent.h"
#include "guilib/GUIWindowManager.h"
#include "utils/Variant.h"

#include <math.h>

CProgressJob::CProgressJob()
  : m_progress(NULL),
    m_progressDialog(NULL)
{ }

CProgressJob::CProgressJob(CGUIDialogProgressBarHandle* progressBar)
  : m_progress(progressBar),
    m_progressDialog(NULL)
{ }

CProgressJob::~CProgressJob()
{
  MarkFinished();

  m_progress = NULL;
  m_progressDialog = NULL;
}

bool CProgressJob::ShouldCancel(unsigned int progress, unsigned int total) const
{
  if (IsCancelled())
    return true;

  SetProgress(progress, total);

  return CJob::ShouldCancel(progress, total);
}

bool CProgressJob::DoModal()
{
  m_progress = NULL;

  // get a progress dialog if we don't already have one
  if (m_progressDialog == NULL)
  {
    m_progressDialog = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogProgress>(WINDOW_DIALOG_PROGRESS);

    if (m_progressDialog == NULL)
      return false;
  }

  m_modal = true;

  // do the work
  bool result = DoWork();

  // mark the progress dialog as finished (will close it)
  MarkFinished();
  m_modal = false;

  return result;
}

void CProgressJob::SetProgressIndicators(CGUIDialogProgressBarHandle* progressBar, CGUIDialogProgress* progressDialog, bool updateProgress /* = true */, bool updateInformation /* = true */)
{
  SetProgressBar(progressBar);
  SetProgressDialog(progressDialog);
  SetUpdateProgress(updateProgress);
  SetUpdateInformation(updateInformation);

  // disable auto-closing
  SetAutoClose(false);
}

void CProgressJob::ShowProgressDialog() const
{
  if (!IsModal() || m_progressDialog == NULL ||
      m_progressDialog->IsDialogRunning())
    return;

  // show the progress dialog as a modal dialog with a progress bar
  m_progressDialog->Open();
  m_progressDialog->ShowProgressBar(true);
}

void CProgressJob::SetTitle(const std::string &title)
{
  if (!m_updateInformation)
    return;

  if (m_progress != NULL)
    m_progress->SetTitle(title);
  else if (m_progressDialog != NULL)
  {
    m_progressDialog->SetHeading(CVariant{title});

    ShowProgressDialog();
  }
}

void CProgressJob::SetText(const std::string &text)
{
  if (!m_updateInformation)
    return;

  if (m_progress != NULL)
    m_progress->SetText(text);
  else if (m_progressDialog != NULL)
  {
    m_progressDialog->SetText(CVariant{text});

    ShowProgressDialog();
  }
}

void CProgressJob::SetProgress(float percentage) const
{
  if (!m_updateProgress)
    return;

  if (m_progress != NULL)
    m_progress->SetPercentage(percentage);
  else if (m_progressDialog != NULL)
  {
    ShowProgressDialog();

    int iPercentage = static_cast<int>(ceil(percentage));
    // only change and update the progress bar if its percentage value changed
    // (this can have a huge impact on performance if it's called a lot)
    if (iPercentage != m_progressDialog->GetPercentage())
    {
      m_progressDialog->SetPercentage(iPercentage);
      m_progressDialog->Progress();
    }
  }
}

void CProgressJob::SetProgress(int currentStep, int totalSteps) const
{
  if (!m_updateProgress)
    return;

  if (m_progress != NULL)
    m_progress->SetProgress(currentStep, totalSteps);
  else if (m_progressDialog != NULL)
    SetProgress((static_cast<float>(currentStep) * 100.0f) / totalSteps);
}

void CProgressJob::MarkFinished()
{
  if (m_progress != NULL)
  {
    if (m_updateProgress)
    {
      m_progress->MarkFinished();
      // We don't own this pointer and it will be deleted after it's marked finished
      // just set it to nullptr so we don't try to use it again
      m_progress = nullptr;
    }
  }
  else if (m_progressDialog != NULL && m_autoClose)
    m_progressDialog->Close();
}

bool CProgressJob::IsCancelled() const
{
  if (m_progressDialog != NULL)
    return m_progressDialog->IsCanceled();

  return false;
}

bool CProgressJob::HasProgressIndicator() const
{
  return m_progress != nullptr || m_progressDialog != nullptr;
}