Merge pull request #26148 from ksooo/fix-secondstotimestring-warning
[xbmc.git] / xbmc / utils / ProgressJob.cpp
blob56667a69b50008833aecc1203db8d4a1333aabb1
1 /*
2 * Copyright (C) 2015-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "ProgressJob.h"
11 #include "ServiceBroker.h"
12 #include "dialogs/GUIDialogExtendedProgressBar.h"
13 #include "dialogs/GUIDialogProgress.h"
14 #include "guilib/GUIComponent.h"
15 #include "guilib/GUIWindowManager.h"
16 #include "utils/Variant.h"
18 #include <math.h>
20 CProgressJob::CProgressJob()
21 : m_progress(NULL),
22 m_progressDialog(NULL)
23 { }
25 CProgressJob::CProgressJob(CGUIDialogProgressBarHandle* progressBar)
26 : m_progress(progressBar),
27 m_progressDialog(NULL)
28 { }
30 CProgressJob::~CProgressJob()
32 MarkFinished();
34 m_progress = NULL;
35 m_progressDialog = NULL;
38 bool CProgressJob::ShouldCancel(unsigned int progress, unsigned int total) const
40 if (IsCancelled())
41 return true;
43 SetProgress(progress, total);
45 return CJob::ShouldCancel(progress, total);
48 bool CProgressJob::DoModal()
50 m_progress = NULL;
52 // get a progress dialog if we don't already have one
53 if (m_progressDialog == NULL)
55 m_progressDialog = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogProgress>(WINDOW_DIALOG_PROGRESS);
57 if (m_progressDialog == NULL)
58 return false;
61 m_modal = true;
63 // do the work
64 bool result = DoWork();
66 // mark the progress dialog as finished (will close it)
67 MarkFinished();
68 m_modal = false;
70 return result;
73 void CProgressJob::SetProgressIndicators(CGUIDialogProgressBarHandle* progressBar, CGUIDialogProgress* progressDialog, bool updateProgress /* = true */, bool updateInformation /* = true */)
75 SetProgressBar(progressBar);
76 SetProgressDialog(progressDialog);
77 SetUpdateProgress(updateProgress);
78 SetUpdateInformation(updateInformation);
80 // disable auto-closing
81 SetAutoClose(false);
84 void CProgressJob::ShowProgressDialog() const
86 if (!IsModal() || m_progressDialog == NULL ||
87 m_progressDialog->IsDialogRunning())
88 return;
90 // show the progress dialog as a modal dialog with a progress bar
91 m_progressDialog->Open();
92 m_progressDialog->ShowProgressBar(true);
95 void CProgressJob::SetTitle(const std::string &title)
97 if (!m_updateInformation)
98 return;
100 if (m_progress != NULL)
101 m_progress->SetTitle(title);
102 else if (m_progressDialog != NULL)
104 m_progressDialog->SetHeading(CVariant{title});
106 // Prevent displaying the progress dialog without any heading and/or text.
107 if (m_progressDialog->HasHeading() && m_progressDialog->HasText())
108 ShowProgressDialog();
112 void CProgressJob::SetText(const std::string &text)
114 if (!m_updateInformation)
115 return;
117 if (m_progress != NULL)
118 m_progress->SetText(text);
119 else if (m_progressDialog != NULL)
121 m_progressDialog->SetText(CVariant{text});
123 // Prevent displaying the progress dialog without any heading and/or text.
124 if (m_progressDialog->HasText() && m_progressDialog->HasHeading())
125 ShowProgressDialog();
129 void CProgressJob::SetProgress(float percentage) const
131 if (!m_updateProgress)
132 return;
134 if (m_progress != NULL)
135 m_progress->SetPercentage(percentage);
136 else if (m_progressDialog != NULL)
138 ShowProgressDialog();
140 int iPercentage = static_cast<int>(ceil(percentage));
141 // only change and update the progress bar if its percentage value changed
142 // (this can have a huge impact on performance if it's called a lot)
143 if (iPercentage != m_progressDialog->GetPercentage())
145 m_progressDialog->SetPercentage(iPercentage);
146 m_progressDialog->Progress();
151 void CProgressJob::SetProgress(int currentStep, int totalSteps) const
153 if (!m_updateProgress)
154 return;
156 if (m_progress != NULL)
157 m_progress->SetProgress(currentStep, totalSteps);
158 else if (m_progressDialog != NULL)
159 SetProgress((static_cast<float>(currentStep) * 100.0f) / totalSteps);
162 void CProgressJob::MarkFinished()
164 if (m_progress != NULL)
166 if (m_updateProgress)
168 m_progress->MarkFinished();
169 // We don't own this pointer and it will be deleted after it's marked finished
170 // just set it to nullptr so we don't try to use it again
171 m_progress = nullptr;
174 else if (m_progressDialog != NULL && m_autoClose)
175 m_progressDialog->Close();
178 bool CProgressJob::IsCancelled() const
180 if (m_progressDialog != NULL)
181 return m_progressDialog->IsCanceled();
183 return false;
186 bool CProgressJob::HasProgressIndicator() const
188 return m_progress != nullptr || m_progressDialog != nullptr;