Some code refactoring.
[MUtilities.git] / src / Utils_Win32.cpp
blobd1759e9252b6988fc28891e2cd36fc00a39db74f
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
22 #include "Utils_Win32.h"
24 //Win32 API
25 #ifndef _INC_WINDOWS
26 #define WIN32_LEAN_AND_MEAN 1
27 #include <Windows.h>
28 #include <ObjIdl.h> // required by QWinMime in QtWinExtras
29 #endif //_INC_WINDOWS
31 //Qt
32 #include <QIcon>
33 #include <QPair>
34 #include <QReadWriteLock>
35 #include <QLibrary>
36 #include <QHash>
37 #if QT_VERSION > QT_VERSION_CHECK(5,0,0)
38 #include <QtWinExtras>
39 #endif
41 //Qt5 support
42 #if QT_VERSION > QT_VERSION_CHECK(5,0,0)
43 #define PIXMAP2HICON(X) QtWin::toHICON((X))
44 #else
45 #define PIXMAP2HICON(X) (X).toWinHICON()
46 #endif
48 ///////////////////////////////////////////////////////////////////////////////
49 // QICON TO HICON
50 ///////////////////////////////////////////////////////////////////////////////
52 uintptr_t MUtils::Win32Utils::qicon_to_hicon(const QIcon *const icon, const int w, const int h)
54 if(!icon->isNull())
56 QPixmap pixmap = icon->pixmap(w, h);
57 if(!pixmap.isNull())
59 return (uintptr_t) PIXMAP2HICON(pixmap);
62 return NULL;
65 ///////////////////////////////////////////////////////////////////////////////
66 // RESOLVE FUNCTION
67 ///////////////////////////////////////////////////////////////////////////////
69 typedef QHash<QString, uintptr_t> FunctionMap;
70 typedef QPair<QSharedPointer<QLibrary>, FunctionMap> LibraryItem;
72 static QReadWriteLock g_resolve_lock;
73 static QHash<QString, LibraryItem> g_resolve_libs;
75 const uintptr_t &MUtils::Win32Utils::resolve_helper(const QString &libraryName, const QString &functionName)
77 const QString libraryNameFolded = libraryName.toCaseFolded().trimmed();
78 const QString functionIdTrimmed = functionName.trimmed();
80 //Fuction already loaded?
81 QReadLocker rdLock(&g_resolve_lock);
82 if (g_resolve_libs.contains(libraryNameFolded))
84 LibraryItem &lib = g_resolve_libs[libraryNameFolded];
85 if (lib.second.contains(functionIdTrimmed))
87 return lib.second[functionIdTrimmed];
91 //Accquire write access!
92 rdLock.unlock();
93 QWriteLocker wrLock(&g_resolve_lock);
95 //Load library
96 if (!g_resolve_libs.contains(libraryNameFolded))
98 QSharedPointer<QLibrary> lib(new QLibrary(libraryNameFolded));
99 if (!(lib->isLoaded() || lib->load()))
101 qWarning("Failed to load dynamic library: %s", MUTILS_UTF8(libraryNameFolded));
102 lib.clear();
104 g_resolve_libs.insert(libraryNameFolded, qMakePair(lib, FunctionMap()));
107 //Is library available?
108 LibraryItem &lib = g_resolve_libs[libraryNameFolded];
109 if (lib.first.isNull() || (!lib.first->isLoaded()))
111 static const uintptr_t null = NULL;
112 return null; /*library unavailable*/
115 //Lookup the function
116 if (!lib.second.contains(functionIdTrimmed))
118 void *const ptr = lib.first->resolve(functionIdTrimmed.toLatin1().constData());
119 if (!ptr)
121 qWarning("Failed to resolve function: %s::%s", MUTILS_UTF8(libraryNameFolded), MUTILS_UTF8(functionIdTrimmed));
123 lib.second.insert(functionIdTrimmed, reinterpret_cast<uintptr_t>(ptr));
126 //Return function pointer
127 return lib.second[functionIdTrimmed];