OSDN Git Service

Bump version.
[mutilities/MUtilities.git] / src / Taskbar7_Win32.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2019 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 //MUtils
24 #include <MUtils/Taskbar7.h>
25 #include <MUtils/OSSupport.h>
26 #include <MUtils/Exception.h>
27
28 //Internal
29 #include "Utils_Win32.h"
30
31 //Qt
32 #include <QWidget>
33 #include <QIcon>
34 #if QT_VERSION > QT_VERSION_CHECK(5,0,0)
35 #include <QtWinExtras>
36 #endif
37
38 //Windows includes
39 #define NOMINMAX
40 #define WIN32_LEAN_AND_MEAN 1
41 #include <Windows.h>
42 #include <ShObjIdl.h>
43
44 ///////////////////////////////////////////////////////////////////////////////
45 // UNTILITIES
46 ///////////////////////////////////////////////////////////////////////////////
47
48 #define INITIALIZE_TASKBAR() do \
49 { \
50         if(!p->supported) \
51         { \
52                 return false; \
53         } \
54         if(!(MUTILS_BOOLIFY(p->initialized) || initialize())) \
55         { \
56                 qWarning("Taskbar initialization failed!"); \
57                 return false; \
58         } \
59 } \
60 while(0)
61
62 ///////////////////////////////////////////////////////////////////////////////
63 // PRIVATE DATA
64 ///////////////////////////////////////////////////////////////////////////////
65
66 namespace MUtils
67 {
68         class Taskbar7_Private
69         {
70                 friend class Taskbar7;
71
72         protected:
73                 Taskbar7_Private(void)
74                 {
75                         taskbarList = NULL;
76                 }
77
78                 ITaskbarList3 *taskbarList;
79                 QAtomicInt supported;
80                 QAtomicInt initialized;
81         };
82 }
83
84 ///////////////////////////////////////////////////////////////////////////////
85 // CONSTRUCTOR & DESTRUCTOR
86 ///////////////////////////////////////////////////////////////////////////////
87
88 MUtils::Taskbar7::Taskbar7(QWidget *const window)
89 :
90         p(new Taskbar7_Private()),
91         m_window(window)
92 {
93         if(!m_window)
94         {
95                 MUTILS_THROW("Taskbar7: Window pointer must not be NULL!");
96         }
97         if (!p->supported)
98         {
99                 if (OS::os_version() >= OS::Version::WINDOWS_WIN70)
100                 {
101                         p->supported.ref();
102                 }
103                 else
104                 {
105                         qWarning("Taskbar7: Taskbar progress not supported on this platform.");
106                 }
107         }
108 }
109
110 MUtils::Taskbar7::~Taskbar7(void)
111 {
112         if(p->taskbarList)
113         {
114                 p->taskbarList->Release();
115                 p->taskbarList = NULL;
116         }
117
118         delete p;
119 }
120
121 ///////////////////////////////////////////////////////////////////////////////
122 // PUBLIC INTERFACE
123 ///////////////////////////////////////////////////////////////////////////////
124
125 bool MUtils::Taskbar7::setTaskbarState(const taskbarState_t &state)
126 {
127         INITIALIZE_TASKBAR();
128         HRESULT result = HRESULT(-1);
129
130         switch(state)
131         {
132         case TASKBAR_STATE_NONE:
133                 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_NOPROGRESS);
134                 break;
135         case TASKBAR_STATE_NORMAL:
136                 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_NORMAL);
137                 break;
138         case TASKBAR_STATE_INTERMEDIATE:
139                 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_INDETERMINATE);
140                 break;
141         case TASKBAR_STATE_PAUSED:
142                 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_ERROR);
143                 break;
144         case TASKBAR_STATE_ERROR:
145                 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_PAUSED);
146                 break;
147         default:
148                 MUTILS_THROW("Taskbar7: Invalid taskbar state specified!");
149         }
150
151         return SUCCEEDED(result);
152 }
153
154 bool MUtils::Taskbar7::setTaskbarProgress(const quint64 &currentValue, const quint64 &maximumValue)
155 {
156         INITIALIZE_TASKBAR();
157         const HRESULT result = p->taskbarList->SetProgressValue(reinterpret_cast<HWND>(m_window->winId()), currentValue, maximumValue);
158         return SUCCEEDED(result);
159 }
160
161 bool MUtils::Taskbar7::setOverlayIcon(const QIcon *const icon, const QString &info)
162 {
163         INITIALIZE_TASKBAR();
164         HRESULT result = HRESULT(-1);
165         if(icon)
166         {
167                 if(const HICON hIcon = (HICON)MUtils::Win32Utils::qicon_to_hicon(icon, 16, 16))
168                 {
169                         result = p->taskbarList->SetOverlayIcon(reinterpret_cast<HWND>(m_window->winId()), hIcon, MUTILS_WCHR(info));
170                         DestroyIcon(hIcon);
171                 }
172         }
173         else
174         {
175                 result = p->taskbarList->SetOverlayIcon(reinterpret_cast<HWND>(m_window->winId()), NULL, MUTILS_WCHR(info));
176         }
177         return SUCCEEDED(result);
178 }
179
180 ///////////////////////////////////////////////////////////////////////////////
181 // INTERNAL
182 ///////////////////////////////////////////////////////////////////////////////
183
184 bool MUtils::Taskbar7::initialize(void)
185 {
186         while(!p->taskbarList)
187         {
188                 ITaskbarList3 *ptbl = NULL;
189                 const HRESULT hr = CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&ptbl));
190                 if(!SUCCEEDED(hr))
191                 {
192                         qWarning("ITaskbarList3 could not be created!");
193                         return false;
194                 }
195                 p->taskbarList = ptbl;
196         }
197
198         if(!p->initialized)
199         {
200                 bool okay = false;
201                 for(int i = 0; i < 8; i++)
202                 {
203                         if(SUCCEEDED(p->taskbarList->HrInit()))
204                         {
205                                 okay = true;
206                                 break;
207                         }
208                         Sleep(1);
209                 }
210                 if(!okay)
211                 {
212                         qWarning("ITaskbarList3::HrInit() has failed!");
213                         return false;
214                 }
215                 p->initialized.ref(); /*success*/
216         }
217
218         return true;
219 }