OSDN Git Service

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