OSDN Git Service

Some more improvements of MUtils::CPUFetaures code.
[mutilities/MUtilities.git] / src / Taskbar7_Win32.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 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 //Qt
29 #include <QWidget>
30 #include <QIcon>
31
32 //Windows includes
33 #define NOMINMAX
34 #define WIN32_LEAN_AND_MEAN 1
35 #include <Windows.h>
36 #include <ShObjIdl.h>
37
38 ///////////////////////////////////////////////////////////////////////////////
39 // UNTILITIES
40 ///////////////////////////////////////////////////////////////////////////////
41
42 #define INITIALIZE_TASKBAR() do \
43 { \
44         if(!p->supported) \
45         { \
46                 return false; \
47         } \
48         if(!(p->initialized || initialize())) \
49         { \
50                 qWarning("Taskbar initialization failed!"); \
51                 return false; \
52         } \
53 } \
54 while(0)
55
56 ///////////////////////////////////////////////////////////////////////////////
57 // PRIVATE DATA
58 ///////////////////////////////////////////////////////////////////////////////
59
60 namespace MUtils
61 {
62         class Taskbar7_Private
63         {
64                 friend class Taskbar7;
65
66         protected:
67                 Taskbar7_Private(void)
68                 {
69                         taskbarList = NULL;
70                         supported   = false;
71                         initialized = false;
72                 }
73
74                 ITaskbarList3 *taskbarList;
75                 volatile bool supported;
76                 volatile bool initialized;
77         };
78 }
79
80 ///////////////////////////////////////////////////////////////////////////////
81 // CONSTRUCTOR & DESTRUCTOR
82 ///////////////////////////////////////////////////////////////////////////////
83
84 MUtils::Taskbar7::Taskbar7(QWidget *const window)
85 :
86         p(new Taskbar7_Private()),
87         m_window(window)
88 {
89         if(!m_window)
90         {
91                 MUTILS_THROW("Taskbar7: Window pointer must not be NULL!");
92         }
93         if(!(p->supported = (OS::os_version() >= OS::Version::WINDOWS_WIN70)))
94         {
95                 qWarning("Taskbar7: Taskbar progress not supported on this platform.");
96         }
97 }
98
99 MUtils::Taskbar7::~Taskbar7(void)
100 {
101         if(p->taskbarList)
102         {
103                 p->taskbarList->Release();
104                 p->taskbarList = NULL;
105         }
106
107         delete p;
108 }
109
110 ///////////////////////////////////////////////////////////////////////////////
111 // PUBLIC INTERFACE
112 ///////////////////////////////////////////////////////////////////////////////
113
114 bool MUtils::Taskbar7::setTaskbarState(const taskbarState_t &state)
115 {
116         INITIALIZE_TASKBAR();
117         HRESULT result = HRESULT(-1);
118
119         switch(state)
120         {
121         case TASKBAR_STATE_NONE:
122                 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_NOPROGRESS);
123                 break;
124         case TASKBAR_STATE_NORMAL:
125                 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_NORMAL);
126                 break;
127         case TASKBAR_STATE_INTERMEDIATE:
128                 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_INDETERMINATE);
129                 break;
130         case TASKBAR_STATE_PAUSED:
131                 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_ERROR);
132                 break;
133         case TASKBAR_STATE_ERROR:
134                 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_PAUSED);
135                 break;
136         default:
137                 MUTILS_THROW("Taskbar7: Invalid taskbar state specified!");
138         }
139
140         return SUCCEEDED(result);
141 }
142
143 bool MUtils::Taskbar7::setTaskbarProgress(const quint64 &currentValue, const quint64 &maximumValue)
144 {
145         INITIALIZE_TASKBAR();
146         const HRESULT result = p->taskbarList->SetProgressValue(reinterpret_cast<HWND>(m_window->winId()), currentValue, maximumValue);
147         return SUCCEEDED(result);
148 }
149
150 bool MUtils::Taskbar7::setOverlayIcon(const QIcon *const icon, const QString &info)
151 {
152         INITIALIZE_TASKBAR();
153         HRESULT result = HRESULT(-1);
154         if(icon)
155         {
156                 if(const HICON hIcon = icon->pixmap(16,16).toWinHICON())
157                 {
158                         result = p->taskbarList->SetOverlayIcon(m_window->winId(), hIcon, MUTILS_WCHR(info));
159                         DestroyIcon(hIcon);
160                 }
161         }
162         else
163         {
164                 result = p->taskbarList->SetOverlayIcon(m_window->winId(), NULL, MUTILS_WCHR(info));
165         }
166         return SUCCEEDED(result);
167 }
168
169 ///////////////////////////////////////////////////////////////////////////////
170 // INTERNAL
171 ///////////////////////////////////////////////////////////////////////////////
172
173 bool MUtils::Taskbar7::initialize(void)
174 {
175         while(!p->taskbarList)
176         {
177                 ITaskbarList3 *ptbl = NULL;
178                 const HRESULT hr = CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&ptbl));
179                 if(!SUCCEEDED(hr))
180                 {
181                         qWarning("ITaskbarList3 could not be created!");
182                         return false;
183                 }
184                 p->taskbarList = ptbl;
185         }
186
187         while(!p->initialized)
188         {
189                 bool okay = false;
190                 for(int i = 0; i < 8; i++)
191                 {
192                         if(SUCCEEDED(p->taskbarList->HrInit()))
193                         {
194                                 okay = true;
195                                 break;
196                         }
197                         Sleep(1);
198                 }
199                 if(!okay)
200                 {
201                         qWarning("ITaskbarList3::HrInit() has failed!");
202                         return false;
203                 }
204                 p->initialized = true;
205         }
206
207         return true;
208 }