Added option for creating "pretty" file names to clean_file_name_make_pretty() function.
[MUtilities.git] / src / Taskbar7_Win32.cpp
blobf97b137109ed605ff9cb0a3c93612ea9a1fc0377
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2017 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.
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.
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.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 //MUtils
24 #include <MUtils/Taskbar7.h>
25 #include <MUtils/OSSupport.h>
26 #include <MUtils/Exception.h>
28 //Internal
29 #include "Utils_Win32.h"
31 //Qt
32 #include <QWidget>
33 #include <QIcon>
34 #if QT_VERSION > QT_VERSION_CHECK(5,0,0)
35 #include <QtWinExtras>
36 #endif
38 //Windows includes
39 #define NOMINMAX
40 #define WIN32_LEAN_AND_MEAN 1
41 #include <Windows.h>
42 #include <ShObjIdl.h>
44 ///////////////////////////////////////////////////////////////////////////////
45 // UNTILITIES
46 ///////////////////////////////////////////////////////////////////////////////
48 #define INITIALIZE_TASKBAR() do \
49 { \
50 if(!p->supported) \
51 { \
52 return false; \
53 } \
54 if(!(p->initialized || initialize())) \
55 { \
56 qWarning("Taskbar initialization failed!"); \
57 return false; \
58 } \
59 } \
60 while(0)
62 ///////////////////////////////////////////////////////////////////////////////
63 // PRIVATE DATA
64 ///////////////////////////////////////////////////////////////////////////////
66 namespace MUtils
68 class Taskbar7_Private
70 friend class Taskbar7;
72 protected:
73 Taskbar7_Private(void)
75 taskbarList = NULL;
76 supported = false;
77 initialized = false;
80 ITaskbarList3 *taskbarList;
81 volatile bool supported;
82 volatile bool initialized;
86 ///////////////////////////////////////////////////////////////////////////////
87 // CONSTRUCTOR & DESTRUCTOR
88 ///////////////////////////////////////////////////////////////////////////////
90 MUtils::Taskbar7::Taskbar7(QWidget *const window)
92 p(new Taskbar7_Private()),
93 m_window(window)
95 if(!m_window)
97 MUTILS_THROW("Taskbar7: Window pointer must not be NULL!");
99 if(!(p->supported = (OS::os_version() >= OS::Version::WINDOWS_WIN70)))
101 qWarning("Taskbar7: Taskbar progress not supported on this platform.");
105 MUtils::Taskbar7::~Taskbar7(void)
107 if(p->taskbarList)
109 p->taskbarList->Release();
110 p->taskbarList = NULL;
113 delete p;
116 ///////////////////////////////////////////////////////////////////////////////
117 // PUBLIC INTERFACE
118 ///////////////////////////////////////////////////////////////////////////////
120 bool MUtils::Taskbar7::setTaskbarState(const taskbarState_t &state)
122 INITIALIZE_TASKBAR();
123 HRESULT result = HRESULT(-1);
125 switch(state)
127 case TASKBAR_STATE_NONE:
128 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_NOPROGRESS);
129 break;
130 case TASKBAR_STATE_NORMAL:
131 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_NORMAL);
132 break;
133 case TASKBAR_STATE_INTERMEDIATE:
134 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_INDETERMINATE);
135 break;
136 case TASKBAR_STATE_PAUSED:
137 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_ERROR);
138 break;
139 case TASKBAR_STATE_ERROR:
140 result = p->taskbarList->SetProgressState(reinterpret_cast<HWND>(m_window->winId()), TBPF_PAUSED);
141 break;
142 default:
143 MUTILS_THROW("Taskbar7: Invalid taskbar state specified!");
146 return SUCCEEDED(result);
149 bool MUtils::Taskbar7::setTaskbarProgress(const quint64 &currentValue, const quint64 &maximumValue)
151 INITIALIZE_TASKBAR();
152 const HRESULT result = p->taskbarList->SetProgressValue(reinterpret_cast<HWND>(m_window->winId()), currentValue, maximumValue);
153 return SUCCEEDED(result);
156 bool MUtils::Taskbar7::setOverlayIcon(const QIcon *const icon, const QString &info)
158 INITIALIZE_TASKBAR();
159 HRESULT result = HRESULT(-1);
160 if(icon)
162 if(const HICON hIcon = (HICON)MUtils::Win32Utils::qicon_to_hicon(icon, 16, 16))
164 result = p->taskbarList->SetOverlayIcon(reinterpret_cast<HWND>(m_window->winId()), hIcon, MUTILS_WCHR(info));
165 DestroyIcon(hIcon);
168 else
170 result = p->taskbarList->SetOverlayIcon(reinterpret_cast<HWND>(m_window->winId()), NULL, MUTILS_WCHR(info));
172 return SUCCEEDED(result);
175 ///////////////////////////////////////////////////////////////////////////////
176 // INTERNAL
177 ///////////////////////////////////////////////////////////////////////////////
179 bool MUtils::Taskbar7::initialize(void)
181 while(!p->taskbarList)
183 ITaskbarList3 *ptbl = NULL;
184 const HRESULT hr = CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&ptbl));
185 if(!SUCCEEDED(hr))
187 qWarning("ITaskbarList3 could not be created!");
188 return false;
190 p->taskbarList = ptbl;
193 while(!p->initialized)
195 bool okay = false;
196 for(int i = 0; i < 8; i++)
198 if(SUCCEEDED(p->taskbarList->HrInit()))
200 okay = true;
201 break;
203 Sleep(1);
205 if(!okay)
207 qWarning("ITaskbarList3::HrInit() has failed!");
208 return false;
210 p->initialized = true;
213 return true;