1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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 //////////////////////////////////////////////////////////////////////////////////
23 #include <MUtils/Startup.h>
24 #include <MUtils/OSSupport.h>
25 #include <MUtils/Terminal.h>
26 #include <MUtils/ErrorHandler.h>
27 #include <MUtils/Exception.h>
30 #include <QApplication>
32 #include <QStringList>
33 #include <QLibraryInfo>
35 #include <QImageReader>
37 #include <QMessageBox>
40 ///////////////////////////////////////////////////////////////////////////////
41 // Qt Static Initialization
42 ///////////////////////////////////////////////////////////////////////////////
46 #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
50 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin
)
51 Q_IMPORT_PLUGIN(QICOPlugin
)
54 static void doInitializeResources(void)
56 Q_INIT_RESOURCE(MUtilsData
);
59 static void doCleanupResources(void)
61 Q_CLEANUP_RESOURCE(MUtilsData
);
70 class ResourceInitializer
73 ResourceInitializer(void)
75 doInitializeResources();
78 ~ResourceInitializer(void)
84 static ResourceInitializer resourceInitializer
;
91 ///////////////////////////////////////////////////////////////////////////////
93 ///////////////////////////////////////////////////////////////////////////////
95 static void qt_message_handler(QtMsgType type
, const char *msg
)
97 if((!msg
) || (!(msg
[0])))
102 MUtils::Terminal::write(type
, msg
);
104 if((type
== QtCriticalMsg
) || (type
== QtFatalMsg
))
106 MUtils::OS::fatal_exit(MUTILS_WCHR(QString::fromUtf8(msg
)));
110 static bool qt_event_filter(void *message
, long *result
)
112 return MUtils::OS::handle_os_message(message
, result
);
115 ///////////////////////////////////////////////////////////////////////////////
117 ///////////////////////////////////////////////////////////////////////////////
119 static int startup_main(int &argc
, char **argv
, MUtils::Startup::main_function_t
*const entry_point
, const char* const appName
, const bool &debugConsole
)
121 qInstallMsgHandler(qt_message_handler
);
122 MUtils::Terminal::setup(argc
, argv
, appName
, MUTILS_DEBUG
|| debugConsole
);
123 return entry_point(argc
, argv
);
126 static int startup_helper(int &argc
, char **argv
, MUtils::Startup::main_function_t
*const entry_point
, const char* const appName
, const bool &debugConsole
)
131 iResult
= startup_main(argc
, argv
, entry_point
, appName
, debugConsole
);
133 catch(const std::exception
&error
)
135 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error
.what());
136 MUtils::OS::fatal_exit(L
"Unhandeled C++ exception error, application will exit!");
140 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
141 MUtils::OS::fatal_exit(L
"Unhandeled C++ exception error, application will exit!");
146 int MUtils::Startup::startup(int &argc
, char **argv
, main_function_t
*const entry_point
, const char* const appName
, const bool &debugConsole
)
151 _CrtSetDbgFlag(_CRTDBG_CHECK_ALWAYS_DF
|| _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG
));
153 iResult
= startup_main(argc
, argv
, entry_point
, appName
, debugConsole
);
158 MUtils::ErrorHandler::initialize();
159 MUtils::OS::check_debugger();
160 iResult
= startup_helper(argc
, argv
, entry_point
, appName
, debugConsole
);
164 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnhandeled structured exception error!\n");
165 MUtils::OS::fatal_exit(L
"Unhandeled structured exception error, application will exit!");
168 MUtils::ErrorHandler::initialize();
169 MUtils::OS::check_debugger();
170 iResult
= startup_helper(argc
, argv
, entry_point
, appName
, debugConsole
);
172 #endif //MUTILS_DEBUG
176 ///////////////////////////////////////////////////////////////////////////////
178 ///////////////////////////////////////////////////////////////////////////////
180 static QMutex g_init_lock
;
181 static const char *const g_imageformats
[] = {"bmp", "png", "jpg", "gif", "ico", "xpm", "svg", NULL
};
183 QApplication
*MUtils::Startup::create_qt(int &argc
, char **argv
, const QString
&appName
)
185 QMutexLocker
lock(&g_init_lock
);
186 const OS::ArgumentMap
&arguments
= MUtils::OS::arguments();
188 //Don't initialized again, if done already
189 if(QApplication::instance() != NULL
)
191 qWarning("Qt is already initialized!");
195 //Extract executable name from argv[] array
196 QString executableName
= QLatin1String("LameXP.exe");
197 if(arguments
.count() > 0)
199 static const char *delimiters
= "\\/:?";
200 executableName
= arguments
[0].trimmed();
201 for(int i
= 0; delimiters
[i
]; i
++)
203 int temp
= executableName
.lastIndexOf(QChar(delimiters
[i
]));
204 if(temp
>= 0) executableName
= executableName
.mid(temp
+ 1);
206 executableName
= executableName
.trimmed();
207 if(executableName
.isEmpty())
209 executableName
= QLatin1String("LameXP.exe");
215 qDebug("Using Qt v%s [%s], %s, %s", qVersion(), QLibraryInfo::buildDate().toString(Qt::ISODate
).toLatin1().constData(), (qSharedBuild() ? "DLL" : "Static"), QLibraryInfo::buildKey().toLatin1().constData());
216 qDebug("Compiled with Qt v%s [%s], %s\n", QT_VERSION_STR
, QT_PACKAGEDATE_STR
, QT_BUILD_KEY
);
217 if(_stricmp(qVersion(), QT_VERSION_STR
))
219 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());
222 if(QLibraryInfo::buildKey().compare(QString::fromLatin1(QT_BUILD_KEY
), Qt::CaseInsensitive
))
224 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());
228 qDebug("Using Qt v%s [%s], %s", qVersion(), QLibraryInfo::buildDate().toString(Qt::ISODate
).toLatin1().constData(), (qSharedBuild() ? "DLL" : "Static"));
229 qDebug("Compiled with Qt v%s [%s]\n", QT_VERSION_STR
, QT_PACKAGEDATE_STR
);
232 //Check the Windows version
234 const MUtils::OS::Version::os_version_t
&osVersion
= MUtils::OS::os_version();
235 if((osVersion
.type
!= MUtils::OS::Version::OS_WINDOWS
) || (osVersion
< MUtils::OS::Version::WINDOWS_WINXP
))
237 qFatal("%s", QApplication::tr("Executable '%1' requires Windows XP or later.").arg(executableName
).toLatin1().constData());
240 //Check whether we are running on a supported Windows version
241 if(const char *const friendlyName
= MUtils::OS::os_friendly_name(osVersion
))
243 qDebug("Running on %s (NT v%u.%u).\n", friendlyName
, osVersion
.versionMajor
, osVersion
.versionMinor
);
247 const QString message
= QString().sprintf("Running on an unknown WindowsNT-based system (v%u.%u).", osVersion
.versionMajor
, osVersion
.versionMinor
);
248 qWarning("%s\n", MUTILS_UTF8(message
));
249 MUtils::OS::system_message_wrn(MUTILS_WCHR(message
), L
"LameXP");
252 //Check for compat mode
253 if(osVersion
.overrideFlag
&& (osVersion
<= MUtils::OS::Version::WINDOWS_WN100
))
255 qWarning("Windows compatibility mode detected!");
256 if(!arguments
.contains("ignore-compat-mode"))
258 qFatal("%s", QApplication::tr("Executable '%1' doesn't support Windows compatibility mode.").arg(executableName
).toLatin1().constData());
264 if(MUtils::OS::running_on_wine())
266 qWarning("It appears we are running under Wine, unexpected things might happen!\n");
269 //Set text Codec for locale
270 QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
272 //Create Qt application instance
273 QApplication
*application
= new QApplication(argc
, argv
);
275 //Load plugins from application directory
276 QCoreApplication::setLibraryPaths(QStringList() << QApplication::applicationDirPath());
277 qDebug("Library Path:\n%s\n", MUTILS_UTF8(QApplication::libraryPaths().first()));
279 //Set application properties
280 application
->setApplicationName(appName
);
281 application
->setOrganizationName("LoRd_MuldeR");
282 application
->setOrganizationDomain("mulder.at.gg");
283 application
->setEventFilter(qt_event_filter
);
285 //Check for supported image formats
286 QList
<QByteArray
> supportedFormats
= QImageReader::supportedImageFormats();
287 for(int i
= 0; g_imageformats
[i
]; i
++)
289 if(!supportedFormats
.contains(g_imageformats
[i
]))
291 qFatal("Qt initialization error: QImageIOHandler for '%s' missing!", g_imageformats
[i
]);
292 MUTILS_DELETE(application
);
298 MUtils::Terminal::set_icon(QIcon(":/mutils/icons/bug.png"));
300 //Enable larger/smaller font size
301 double fontScaleFactor
= 1.0;
302 if(arguments
.contains("huge-font" )) fontScaleFactor
= 1.500;
303 if(arguments
.contains("big-font" )) fontScaleFactor
= 1.250;
304 if(arguments
.contains("small-font")) fontScaleFactor
= 0.875;
305 if(arguments
.contains("tiny-font" )) fontScaleFactor
= 0.750;
306 if(!qFuzzyCompare(fontScaleFactor
, 1.0))
308 qWarning("Application font scale factor set to: %.3f\n", fontScaleFactor
);
309 QFont appFont
= application
->font();
310 appFont
.setPointSizeF(appFont
.pointSizeF() * fontScaleFactor
);
311 application
->setFont(appFont
);
314 //Check for process elevation
315 if(MUtils::OS::is_elevated() && (!MUtils::OS::running_on_wine()))
317 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
);
318 messageBox
.addButton("Quit Program (Recommended)", QMessageBox::NoRole
);
319 messageBox
.addButton("Ignore", QMessageBox::NoRole
);
320 if(messageBox
.exec() == 0)
322 MUTILS_DELETE(application
);
327 //Qt created successfully
331 ///////////////////////////////////////////////////////////////////////////////