OSDN Git Service

Some improvements to reg_key_delete() function.
[mutilities/MUtilities.git] / src / Startup.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2015 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.
14 //
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
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
22 //MUtils
23 #include <MUtils/Startup.h>
24 #include <MUtils/OSSupport.h>
25 #include <MUtils/Terminal.h>
26 #include <MUtils/ErrorHandler.h>
27 #include <MUtils/Registry.h>
28 #include <MUtils/Exception.h>
29
30 //Qt
31 #include <QApplication>
32 #include <QMutex>
33 #include <QStringList>
34 #include <QLibraryInfo>
35 #include <QTextCodec>
36 #include <QImageReader>
37 #include <QFont>
38 #include <QMessageBox>
39 #include <QtPlugin>
40
41 //CRT
42 #include <string.h>
43
44 ///////////////////////////////////////////////////////////////////////////////
45 // Qt Static Initialization
46 ///////////////////////////////////////////////////////////////////////////////
47
48 #ifdef QT_NODLL
49
50 #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
51 Q_IMPORT_PLUGIN(qico)
52 Q_IMPORT_PLUGIN(qsvg)
53 #else
54 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
55 Q_IMPORT_PLUGIN(QICOPlugin)
56 #endif
57
58 static void doInitializeResources(void)
59 {
60         Q_INIT_RESOURCE(MUtilsData);
61 }
62
63 static void doCleanupResources(void)
64 {
65         Q_CLEANUP_RESOURCE(MUtilsData);
66 }
67
68 namespace MUtils
69 {
70         namespace Startup
71         {
72                 namespace Internal
73                 {
74                         class ResourceInitializer
75                         {
76                         public:
77                                 ResourceInitializer(void)
78                                 {
79                                         doInitializeResources();
80                                 }
81
82                                 ~ResourceInitializer(void)
83                                 {
84                                         doCleanupResources();
85                                 }
86                         };
87
88                         static ResourceInitializer resourceInitializer;
89                 }
90         }
91 }
92
93 #endif //QT_NODLL
94
95 ///////////////////////////////////////////////////////////////////////////////
96 // MESSAGE HANDLER
97 ///////////////////////////////////////////////////////////////////////////////
98
99 static void qt_message_handler(QtMsgType type, const char *msg)
100 {
101         if((!msg) || (!(msg[0])))
102         {
103                 return;
104         }
105
106         MUtils::Terminal::write(type, msg);
107
108         if((type == QtCriticalMsg) || (type == QtFatalMsg))
109         {
110                 MUtils::OS::fatal_exit(MUTILS_WCHR(QString::fromUtf8(msg)));
111         }
112 }
113
114 static bool qt_event_filter(void *message, long *result)
115 {
116         return MUtils::OS::handle_os_message(message, result);
117 }
118
119 ///////////////////////////////////////////////////////////////////////////////
120 // STARTUP FUNCTION
121 ///////////////////////////////////////////////////////////////////////////////
122
123 static int startup_main(int &argc, char **argv, MUtils::Startup::main_function_t *const entry_point, const char* const appName, const bool &debugConsole)
124 {
125         qInstallMsgHandler(qt_message_handler);
126         MUtils::Terminal::setup(argc, argv, appName, MUTILS_DEBUG || debugConsole);
127         return entry_point(argc, argv);
128 }
129
130 static int startup_helper(int &argc, char **argv, MUtils::Startup::main_function_t *const entry_point, const char* const appName, const bool &debugConsole)
131 {
132         int iResult = -1;
133         try
134         {
135                 iResult = startup_main(argc, argv, entry_point, appName, debugConsole);
136         }
137         catch(const std::exception &error)
138         {
139                 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
140                 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
141         }
142         catch(...)
143         {
144                 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
145                 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
146         }
147         return iResult;
148 }
149
150 int MUtils::Startup::startup(int &argc, char **argv, main_function_t *const entry_point, const char* const appName, const bool &debugConsole)
151 {
152         int iResult = -1;
153 #if (MUTILS_DEBUG)
154 #ifdef _MSC_VER
155         _CrtSetDbgFlag(_CRTDBG_CHECK_ALWAYS_DF || _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
156 #endif //_MSCVER
157         iResult = startup_main(argc, argv, entry_point, appName, debugConsole);
158 #else //MUTILS_DEBUG
159 #ifdef _MSC_VER
160         __try
161         {
162                 MUtils::ErrorHandler::initialize();
163                 MUtils::OS::check_debugger();
164                 iResult = startup_helper(argc, argv, entry_point, appName, debugConsole);
165         }
166         __except(1)
167         {
168                 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnhandeled structured exception error!\n");
169                 MUtils::OS::fatal_exit(L"Unhandeled structured exception error, application will exit!");
170         }
171 #else //_MSCVER
172         MUtils::ErrorHandler::initialize();
173         MUtils::OS::check_debugger();
174         iResult = startup_helper(argc, argv, entry_point, appName, debugConsole);
175 #endif //_MSCVER
176 #endif //MUTILS_DEBUG
177         return iResult;
178 }
179
180 ///////////////////////////////////////////////////////////////////////////////
181 // QT INITIALIZATION
182 ///////////////////////////////////////////////////////////////////////////////
183
184 static QMutex g_init_lock;
185 static const char *const g_imageformats[] = {"bmp", "png", "jpg", "gif", "ico", "xpm", "svg", NULL};
186
187 static QString getExecutableName(int &argc, char **argv)
188 {
189         if(argc >= 1)
190         {
191                 const char *argv0 = argv[0];
192                 if(const char *const ptr = strrchr(argv0, '/'))
193                 {
194                         argv0 = ptr + 1;
195                 }
196                 if(const char *const ptr = strrchr(argv0, '\\'))
197                 {
198                         argv0 = ptr + 1;
199                 }
200                 if(strlen(argv0) > 1)
201                 {
202                         QString::fromLatin1(argv0);
203                 }
204         }
205         return QLatin1String("Program.exe");
206 }
207
208 static void qt_registry_cleanup(void)
209 {
210         static const wchar_t *const KEYS[] =
211         {
212                 L"Software\\Trolltech\\OrganizationDefaults\\Qt Factory Cache %s\\com.trolltech.Qt.QImageIOHandlerFactoryInterface:",
213                 L"Software\\Trolltech\\OrganizationDefaults\\Qt Plugin Cache %s.false",
214                 NULL
215         };
216
217         wchar_t version[32];
218         _snwprintf_s(version, 32, _TRUNCATE, L"%u.%u", (QT_VERSION >> 16), ((QT_VERSION >> 8) & 0xFF));
219
220         for(size_t i = 0; KEYS[i]; i++)
221         {
222                 wchar_t key[256];
223                 if(_snwprintf_s(key, 256, _TRUNCATE, KEYS[i], version) > 0)
224                 {
225                         MUtils::Registry::reg_key_delete(MUtils::Registry::root_user, MUTILS_QSTR(key), true, true);
226                 }
227         }
228 }
229
230 QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString &appName)
231 {
232         QMutexLocker lock(&g_init_lock);
233         const OS::ArgumentMap &arguments = MUtils::OS::arguments();
234
235         //Don't initialized again, if done already
236         if(QApplication::instance() != NULL)
237         {
238                 qWarning("Qt is already initialized!");
239                 return NULL;
240         }
241
242         //Extract executable name from argv[] array
243         const QString executableName = getExecutableName(argc, argv);
244
245         //Check Qt version
246 #ifdef QT_BUILD_KEY
247         qDebug("Using Qt v%s [%s], %s, %s", qVersion(), QLibraryInfo::buildDate().toString(Qt::ISODate).toLatin1().constData(), (qSharedBuild() ? "DLL" : "Static"), QLibraryInfo::buildKey().toLatin1().constData());
248         qDebug("Compiled with Qt v%s [%s], %s\n", QT_VERSION_STR, QT_PACKAGEDATE_STR, QT_BUILD_KEY);
249         if(_stricmp(qVersion(), QT_VERSION_STR))
250         {
251                 qFatal("%s", QApplication::tr("Executable '%1' requires Qt v%2, but found Qt v%3.").arg(executableName, QString::fromLatin1(QT_VERSION_STR), QString::fromLatin1(qVersion())).toLatin1().constData());
252                 return false;
253         }
254         if(QLibraryInfo::buildKey().compare(QString::fromLatin1(QT_BUILD_KEY), Qt::CaseInsensitive))
255         {
256                 qFatal("%s", QApplication::tr("Executable '%1' was built for Qt '%2', but found Qt '%3'.").arg(executableName, QString::fromLatin1(QT_BUILD_KEY), QLibraryInfo::buildKey()).toLatin1().constData());
257                 return false;
258         }
259 #else
260         qDebug("Using Qt v%s [%s], %s", qVersion(), QLibraryInfo::buildDate().toString(Qt::ISODate).toLatin1().constData(), (qSharedBuild() ? "DLL" : "Static"));
261         qDebug("Compiled with Qt v%s [%s]\n", QT_VERSION_STR, QT_PACKAGEDATE_STR);
262 #endif
263
264         //Check the Windows version
265         const MUtils::OS::Version::os_version_t &osVersion = MUtils::OS::os_version();
266         if((osVersion.type != MUtils::OS::Version::OS_WINDOWS) || (osVersion < MUtils::OS::Version::WINDOWS_WINXP))
267         {
268                 qFatal("%s", QApplication::tr("Executable '%1' requires Windows XP or later.").arg(executableName).toLatin1().constData());
269         }
270
271         //Check whether we are running on a supported Windows version
272         if(const char *const friendlyName = MUtils::OS::os_friendly_name(osVersion))
273         {
274                 qDebug("Running on %s (NT v%u.%u).\n", friendlyName, osVersion.versionMajor, osVersion.versionMinor);
275         }
276         else
277         {
278                 const QString message = QString().sprintf("Running on an unknown WindowsNT-based system (v%u.%u).", osVersion.versionMajor, osVersion.versionMinor);
279                 qWarning("%s\n", MUTILS_UTF8(message));
280                 MUtils::OS::system_message_wrn(L"LameXP", MUTILS_WCHR(message));
281         }
282
283         //Check for compat mode
284         if(osVersion.overrideFlag && (osVersion <= MUtils::OS::Version::WINDOWS_WN100))
285         {
286                 qWarning("Windows compatibility mode detected!");
287                 if(!arguments.contains("ignore-compat-mode"))
288                 {
289                         qFatal("%s", QApplication::tr("Executable '%1' doesn't support Windows compatibility mode.").arg(executableName).toLatin1().constData());
290                         return NULL;
291                 }
292         }
293
294         //Check for Wine
295         if(MUtils::OS::running_on_wine())
296         {
297                 qWarning("It appears we are running under Wine, unexpected things might happen!\n");
298         }
299
300         //Set text Codec for locale
301         QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
302
303         //Create Qt application instance
304         QApplication *application = new QApplication(argc, argv);
305
306         //Register the Qt clean-up function
307         atexit(qt_registry_cleanup);
308
309         //Load plugins from application directory
310         QCoreApplication::setLibraryPaths(QStringList() << QApplication::applicationDirPath());
311         qDebug("Library Path:\n%s\n", MUTILS_UTF8(QApplication::libraryPaths().first()));
312
313         //Set application properties
314         application->setApplicationName(appName);
315         application->setOrganizationName("LoRd_MuldeR");
316         application->setOrganizationDomain("mulder.at.gg");
317         application->setEventFilter(qt_event_filter);
318
319         //Check for supported image formats
320         QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
321         for(int i = 0; g_imageformats[i]; i++)
322         {
323                 if(!supportedFormats.contains(g_imageformats[i]))
324                 {
325                         qFatal("Qt initialization error: QImageIOHandler for '%s' missing!", g_imageformats[i]);
326                         MUTILS_DELETE(application);
327                         return NULL;
328                 }
329         }
330         
331         //Setup console icon
332         MUtils::Terminal::set_icon(QIcon(":/mutils/icons/bug.png"));
333
334         //Enable larger/smaller font size
335         double fontScaleFactor = 1.0;
336         if(arguments.contains("huge-font" )) fontScaleFactor = 1.500;
337         if(arguments.contains("big-font"  )) fontScaleFactor = 1.250;
338         if(arguments.contains("small-font")) fontScaleFactor = 0.875;
339         if(arguments.contains("tiny-font" )) fontScaleFactor = 0.750;
340         if(!qFuzzyCompare(fontScaleFactor, 1.0))
341         {
342                 qWarning("Application font scale factor set to: %.3f\n", fontScaleFactor);
343                 QFont appFont = application->font();
344                 appFont.setPointSizeF(appFont.pointSizeF() * fontScaleFactor);
345                 application->setFont(appFont);
346         }
347
348         //Check for process elevation
349         if(MUtils::OS::is_elevated() && (!MUtils::OS::running_on_wine()))
350         {
351                 QMessageBox messageBox(QMessageBox::Warning, "LameXP", "<nobr>LameXP was started with 'elevated' rights, altough LameXP does not need these rights.<br>Running an applications with unnecessary rights is a potential security risk!</nobr>", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
352                 messageBox.addButton("Quit Program (Recommended)", QMessageBox::NoRole);
353                 messageBox.addButton("Ignore", QMessageBox::NoRole);
354                 if(messageBox.exec() == 0)
355                 {
356                         MUTILS_DELETE(application);
357                         return NULL;
358                 }
359         }
360
361         //Qt created successfully
362         return application;
363 }
364
365 ///////////////////////////////////////////////////////////////////////////////
366