File indexing completed on 2024-04-21 05:48:32

0001 /*
0002     SPDX-FileCopyrightText: 2016 ROSA
0003     SPDX-License-Identifier: GPL-3.0-or-later
0004 */
0005 
0006 ////////////////////////////////////////////////////////////////////////////////
0007 // Windows implementation of ExternalProgressBar
0008 
0009 
0010 #include <QWidget>
0011 
0012 #include "externalprogressbar.h"
0013 
0014 // Class with platform-specific data
0015 class ExternalProgressBarPrivate
0016 {
0017 public:
0018     ExternalProgressBarPrivate();
0019     ~ExternalProgressBarPrivate();
0020 
0021     // Windows7 Taskbar interface for mirroring the progress bar
0022     ITaskbarList3* m_Win7TaskbarList;
0023 
0024     // Main window handle for selecting the correct taskbar button
0025     HWND m_hWnd;
0026 };
0027 
0028 ExternalProgressBarPrivate::ExternalProgressBarPrivate() :
0029     m_Win7TaskbarList(NULL),
0030     m_hWnd(NULL)
0031 {
0032 }
0033 
0034 ExternalProgressBarPrivate::~ExternalProgressBarPrivate()
0035 {
0036 }
0037 
0038 
0039 ExternalProgressBar::ExternalProgressBar(QWidget* mainWindow) :
0040     d_ptr(new ExternalProgressBarPrivate()),
0041     m_MaxValue(0)
0042 {
0043     // Get the taskbar object (if NULL is returned it won't be used - e.g. on pre-7 Windows versions)
0044     CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_ALL, IID_ITaskbarList3, reinterpret_cast<void**>(&d_ptr->m_Win7TaskbarList));
0045     // Store the window handle. In Windows winId() returns HWND.
0046     d_ptr->m_hWnd = reinterpret_cast<HWND>(mainWindow->winId());
0047 }
0048 
0049 ExternalProgressBar::~ExternalProgressBar()
0050 {
0051     DestroyProgressBar();
0052     if (d_ptr->m_Win7TaskbarList != NULL)
0053         d_ptr->m_Win7TaskbarList->Release();
0054     delete d_ptr;
0055 }
0056 
0057 // Initializes the external progress bar and sets its limits
0058 bool ExternalProgressBar::InitProgressBar(quint64 maxSteps)
0059 {
0060     bool res = true;
0061     m_MaxValue = maxSteps;
0062 
0063     // When we set the progress value, TBPF_NORMAL is set automatically
0064     if (d_ptr->m_Win7TaskbarList != NULL)
0065         res = (d_ptr->m_Win7TaskbarList->SetProgressValue(d_ptr->m_hWnd, 0, maxSteps) == S_OK);
0066 
0067     return res;
0068 }
0069 
0070 // Deinitializes the external progress bar and returns into the normal state
0071 bool ExternalProgressBar::DestroyProgressBar()
0072 {
0073     bool res = true;
0074 
0075     if (d_ptr->m_Win7TaskbarList != NULL)
0076         res = (d_ptr->m_Win7TaskbarList->SetProgressState(d_ptr->m_hWnd, TBPF_NOPROGRESS) == S_OK);
0077 
0078     return res;
0079 }
0080 
0081 // Updates the current progress bar position
0082 bool ExternalProgressBar::SetProgressValue(quint64 currentSteps)
0083 {
0084     bool res = true;
0085 
0086     if (d_ptr->m_Win7TaskbarList != NULL)
0087         res = (d_ptr->m_Win7TaskbarList->SetProgressValue(d_ptr->m_hWnd, currentSteps, m_MaxValue) == S_OK);
0088 
0089     return res;
0090 }
0091 
0092 // Sets the progress bar to indicate pause
0093 bool ExternalProgressBar::ProgressSetPause()
0094 {
0095     bool res = true;
0096 
0097     if (d_ptr->m_Win7TaskbarList != NULL)
0098         res = (d_ptr->m_Win7TaskbarList->SetProgressState(d_ptr->m_hWnd, TBPF_PAUSED) == S_OK);
0099 
0100     return res;
0101 }
0102 
0103 // Sets the progress bar to indicate an error
0104 bool ExternalProgressBar::ProgressSetError()
0105 {
0106     bool res = true;
0107 
0108     if (d_ptr->m_Win7TaskbarList != NULL)
0109         res = (d_ptr->m_Win7TaskbarList->SetProgressState(d_ptr->m_hWnd, TBPF_ERROR) == S_OK);
0110 
0111     return res;
0112 }
0113