OSDN Git Service

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