OSDN Git Service

75245605bc62c93bdb27e3b30d50b3fb4e12fd9c
[mutilities/MUtilities.git] / src / Startup.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2014 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/Exception.h>
28
29 //Qt
30 #include <QApplication>
31 #include <QMutex>
32 #include <QStringList>
33 #include <QLibraryInfo>
34 #include <QTextCodec>
35 #include <QImageReader>
36 #include <QFont>
37 #include <QMessageBox>
38 #include <QtPlugin>
39
40 ///////////////////////////////////////////////////////////////////////////////
41 // Qt Plugin Initialization
42 ///////////////////////////////////////////////////////////////////////////////
43
44 #ifdef QT_NODLL
45 #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
46 Q_IMPORT_PLUGIN(qico)
47 Q_IMPORT_PLUGIN(qsvg)
48 #else
49 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
50 Q_IMPORT_PLUGIN(QICOPlugin)
51 #endif
52 #endif
53
54 ///////////////////////////////////////////////////////////////////////////////
55 // MESSAGE HANDLER
56 ///////////////////////////////////////////////////////////////////////////////
57
58 static void qt_message_handler(QtMsgType type, const char *msg)
59 {
60         if((!msg) || (!(msg[0])))
61         {
62                 return;
63         }
64
65         MUtils::Terminal::write(type, msg);
66
67         if((type == QtCriticalMsg) || (type == QtFatalMsg))
68         {
69                 MUtils::OS::fatal_exit(MUTILS_WCHR(QString::fromUtf8(msg)));
70         }
71 }
72
73 static bool qt_event_filter(void *message, long *result)
74 {
75         return MUtils::OS::handle_os_message(message, result);
76 }
77
78 ///////////////////////////////////////////////////////////////////////////////
79 // STARTUP FUNCTION
80 ///////////////////////////////////////////////////////////////////////////////
81
82 static int startup_main(int &argc, char **argv, MUtils::Startup::main_function_t *const entry_point, const char* const appName, const bool &debugConsole)
83 {
84         qInstallMsgHandler(qt_message_handler);
85         MUtils::Terminal::setup(argc, argv, appName, MUTILS_DEBUG || debugConsole);
86         return entry_point(argc, argv);
87 }
88
89 static int startup_helper(int &argc, char **argv, MUtils::Startup::main_function_t *const entry_point, const char* const appName, const bool &debugConsole)
90 {
91         int iResult = -1;
92         try
93         {
94                 iResult = startup_main(argc, argv, entry_point, appName, debugConsole);
95         }
96         catch(const std::exception &error)
97         {
98                 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
99                 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
100         }
101         catch(...)
102         {
103                 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
104                 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
105         }
106         return iResult;
107 }
108
109 int MUtils::Startup::startup(int &argc, char **argv, main_function_t *const entry_point, const char* const appName, const bool &debugConsole)
110 {
111         int iResult = -1;
112 #if (MUTILS_DEBUG)
113 #ifdef _MSC_VER
114         _CrtSetDbgFlag(_CRTDBG_CHECK_ALWAYS_DF || _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
115 #endif //_MSCVER
116         iResult = startup_main(argc, argv, entry_point, appName, debugConsole);
117 #else //MUTILS_DEBUG
118 #ifdef _MSC_VER
119         __try
120         {
121                 MUtils::ErrorHandler::initialize();
122                 MUtils::OS::check_debugger();
123                 iResult = startup_helper(argc, argv, entry_point, appName, debugConsole);
124         }
125         __except(1)
126         {
127                 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnhandeled structured exception error!\n");
128                 MUtils::OS::fatal_exit(L"Unhandeled structured exception error, application will exit!");
129         }
130 #else //_MSCVER
131         MUtils::ErrorHandler::initialize();
132         MUtils::OS::check_debugger();
133         iResult = startup_helper(argc, argv, entry_point, appName, debugConsole);
134 #endif //_MSCVER
135 #endif //MUTILS_DEBUG
136         return iResult;
137 }
138
139 ///////////////////////////////////////////////////////////////////////////////
140 // QT INITIALIZATION
141 ///////////////////////////////////////////////////////////////////////////////
142
143 static QMutex g_init_lock;
144 static const char *const g_imageformats[] = {"bmp", "png", "jpg", "gif", "ico", "xpm", "svg", NULL};
145
146 QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString &appName)
147 {
148         QMutexLocker lock(&g_init_lock);
149         const QStringList &arguments = MUtils::OS::arguments();
150
151         //Don't initialized again, if done already
152         if(QApplication::instance() != NULL)
153         {
154                 qWarning("Qt is already initialized!");
155                 return NULL;
156         }
157
158         //Extract executable name from argv[] array
159         QString executableName = QLatin1String("LameXP.exe");
160         if(arguments.count() > 0)
161         {
162                 static const char *delimiters = "\\/:?";
163                 executableName = arguments[0].trimmed();
164                 for(int i = 0; delimiters[i]; i++)
165                 {
166                         int temp = executableName.lastIndexOf(QChar(delimiters[i]));
167                         if(temp >= 0) executableName = executableName.mid(temp + 1);
168                 }
169                 executableName = executableName.trimmed();
170                 if(executableName.isEmpty())
171                 {
172                         executableName = QLatin1String("LameXP.exe");
173                 }
174         }
175
176         //Check Qt version
177 #ifdef QT_BUILD_KEY
178         qDebug("Using Qt v%s [%s], %s, %s", qVersion(), QLibraryInfo::buildDate().toString(Qt::ISODate).toLatin1().constData(), (qSharedBuild() ? "DLL" : "Static"), QLibraryInfo::buildKey().toLatin1().constData());
179         qDebug("Compiled with Qt v%s [%s], %s\n", QT_VERSION_STR, QT_PACKAGEDATE_STR, QT_BUILD_KEY);
180         if(_stricmp(qVersion(), QT_VERSION_STR))
181         {
182                 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());
183                 return false;
184         }
185         if(QLibraryInfo::buildKey().compare(QString::fromLatin1(QT_BUILD_KEY), Qt::CaseInsensitive))
186         {
187                 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());
188                 return false;
189         }
190 #else
191         qDebug("Using Qt v%s [%s], %s", qVersion(), QLibraryInfo::buildDate().toString(Qt::ISODate).toLatin1().constData(), (qSharedBuild() ? "DLL" : "Static"));
192         qDebug("Compiled with Qt v%s [%s]\n", QT_VERSION_STR, QT_PACKAGEDATE_STR);
193 #endif
194
195         //Check the Windows version
196         
197         const MUtils::OS::Version::os_version_t &osVersion = MUtils::OS::os_version();
198         if((osVersion.type != MUtils::OS::Version::OS_WINDOWS) || (osVersion < MUtils::OS::Version::WINDOWS_WINXP))
199         {
200                 qFatal("%s", QApplication::tr("Executable '%1' requires Windows XP or later.").arg(executableName).toLatin1().constData());
201         }
202
203         //Check whether we are running on a supported Windows version
204         if(const char *const friendlyName = MUtils::OS::os_friendly_name(osVersion))
205         {
206                 qDebug("Running on %s (NT v%u.%u).\n", friendlyName, osVersion.versionMajor, osVersion.versionMinor);
207         }
208         else
209         {
210                 const QString message = QString().sprintf("Running on an unknown WindowsNT-based system (v%u.%u).", osVersion.versionMajor, osVersion.versionMinor);
211                 qWarning("%s\n", MUTILS_UTF8(message));
212                 MUtils::OS::system_message_wrn(MUTILS_WCHR(message), L"LameXP");
213         }
214
215         //Check for compat mode
216         if(osVersion.overrideFlag && (osVersion <= MUtils::OS::Version::WINDOWS_WN100))
217         {
218                 qWarning("Windows compatibility mode detected!");
219                 if(!arguments.contains("--ignore-compat-mode", Qt::CaseInsensitive))
220                 {
221                         qFatal("%s", QApplication::tr("Executable '%1' doesn't support Windows compatibility mode.").arg(executableName).toLatin1().constData());
222                         return NULL;
223                 }
224         }
225
226         //Check for Wine
227         if(MUtils::OS::running_on_wine())
228         {
229                 qWarning("It appears we are running under Wine, unexpected things might happen!\n");
230         }
231
232         //Set text Codec for locale
233         QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
234
235         //Create Qt application instance
236         QApplication *application = new QApplication(argc, argv);
237
238         //Load plugins from application directory
239         QCoreApplication::setLibraryPaths(QStringList() << QApplication::applicationDirPath());
240         qDebug("Library Path:\n%s\n", MUTILS_UTF8(QApplication::libraryPaths().first()));
241
242         //Set application properties
243         application->setApplicationName(appName);
244         application->setOrganizationName("LoRd_MuldeR");
245         application->setOrganizationDomain("mulder.at.gg");
246         application->setEventFilter(qt_event_filter);
247
248         //Check for supported image formats
249         QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
250         for(int i = 0; g_imageformats[i]; i++)
251         {
252                 if(!supportedFormats.contains(g_imageformats[i]))
253                 {
254                         qFatal("Qt initialization error: QImageIOHandler for '%s' missing!", g_imageformats[i]);
255                         MUTILS_DELETE(application);
256                         return NULL;
257                 }
258         }
259         
260         //Setup console icon
261         MUtils::Terminal::set_icon(QIcon(":/mutils/icons/bug.png"));
262
263         //Enable larger/smaller font size
264         double fontScaleFactor = 1.0;
265         if(arguments.contains("--huge-font",  Qt::CaseInsensitive)) fontScaleFactor = 1.500;
266         if(arguments.contains("--big-font",   Qt::CaseInsensitive)) fontScaleFactor = 1.250;
267         if(arguments.contains("--small-font", Qt::CaseInsensitive)) fontScaleFactor = 0.875;
268         if(arguments.contains("--tiny-font",  Qt::CaseInsensitive)) fontScaleFactor = 0.750;
269         if(!qFuzzyCompare(fontScaleFactor, 1.0))
270         {
271                 qWarning("Application font scale factor set to: %.3f\n", fontScaleFactor);
272                 QFont appFont = application->font();
273                 appFont.setPointSizeF(appFont.pointSizeF() * fontScaleFactor);
274                 application->setFont(appFont);
275         }
276
277         //Check for process elevation
278         if(MUtils::OS::is_elevated() && (!MUtils::OS::running_on_wine()))
279         {
280                 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);
281                 messageBox.addButton("Quit Program (Recommended)", QMessageBox::NoRole);
282                 messageBox.addButton("Ignore", QMessageBox::NoRole);
283                 if(messageBox.exec() == 0)
284                 {
285                         MUTILS_DELETE(application);
286                         return NULL;
287                 }
288         }
289
290         //Qt created successfully
291         return application;
292 }
293
294 ///////////////////////////////////////////////////////////////////////////////
295