-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.cpp
175 lines (152 loc) · 5.76 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* \file main.cpp
*
* \section LICENSE
*
* Copyright (C) 2012-present Thorsten Roth
*
* This file is part of iQPuzzle.
*
* iQPuzzle is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iQPuzzle is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with iQPuzzle. If not, see <https://www.gnu.org/licenses/>.
*
* \section DESCRIPTION
* Main function, start application.
*/
/** \mainpage
* \section Introduction
* iQPuzzle is an IQ challenging pentomino puzzle.<br />
* GitHub: https://github.com/ElTh0r0/iqpuzzle
*/
#include <QApplication>
#include <QCommandLineParser>
#include <QDebug>
#include <QStandardPaths>
#include <QTextStream>
#include "./iqpuzzle.h"
static QFile logfile;
static QTextStream out(&logfile);
void setupLogger(const QString &sDebugFilePath, const QString &sAppName,
const QString &sVersion);
void LoggingHandler(QtMsgType type, const QMessageLogContext &context,
const QString &sMsg);
auto main(int argc, char *argv[]) -> int {
#if defined(Q_OS_WIN) && QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
QApplication::setStyle("Fusion"); // Supports dark scheme on Win 10/11
#endif
QApplication app(argc, argv);
app.setApplicationName(QStringLiteral(APP_NAME));
app.setApplicationVersion(QStringLiteral(APP_VERSION));
app.setApplicationDisplayName(QStringLiteral(APP_NAME));
#if !defined(Q_OS_WIN) && !defined(Q_OS_MACOS)
app.setWindowIcon(QIcon::fromTheme(QStringLiteral("iqpuzzle"),
QIcon(QStringLiteral(":/iqpuzzle.png"))));
#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
app.setDesktopFileName(QStringLiteral("com.github.elth0r0.iqpuzzle"));
#endif
#endif
QCommandLineParser cmdparser;
cmdparser.setApplicationDescription(QStringLiteral(APP_DESC));
cmdparser.addHelpOption();
cmdparser.addVersionOption();
QCommandLineOption enableDebug(QStringLiteral("debug"),
QStringLiteral("Enable debug mode"));
cmdparser.addOption(enableDebug);
cmdparser.addPositionalArgument(QStringLiteral("file"),
QStringLiteral("Board file to be opened "
"(*.conf) or savegame "
"(*.iqsav)"));
cmdparser.process(app);
// Default share data path (Windows and debugging)
QString sSharePath = app.applicationDirPath();
// Standard installation path (Linux)
QDir tmpDir(app.applicationDirPath() + "/../share/" +
app.applicationName().toLower());
if (!cmdparser.isSet(enableDebug) && tmpDir.exists()) {
sSharePath = app.applicationDirPath() + "/../share/" +
app.applicationName().toLower();
}
#if defined(Q_OS_MACOS)
sSharePath = app.applicationDirPath() + "/../Resources/";
#endif
QStringList sListPaths =
QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation);
if (sListPaths.isEmpty()) {
qCritical() << "Error while getting data standard path.";
sListPaths << app.applicationDirPath();
}
const QDir userDataDir(sListPaths[0].toLower());
// Create folder including possible parent directories (mkPATH)
if (!userDataDir.exists()) {
userDataDir.mkpath(userDataDir.absolutePath());
}
const QString sDebugFile(QStringLiteral("Debug.log"));
setupLogger(userDataDir.absolutePath() + "/" + sDebugFile,
app.applicationName(), app.applicationVersion());
IQPuzzle myIQPuzzle(userDataDir, sSharePath);
myIQPuzzle.show();
int nRet = app.exec();
logfile.close();
return nRet;
}
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
void setupLogger(const QString &sDebugFilePath, const QString &sAppName,
const QString &sVersion) {
// Remove old debug file
if (QFile(sDebugFilePath).exists()) {
QFile(sDebugFilePath).remove();
}
// Create new file
logfile.setFileName(sDebugFilePath);
if (!logfile.open(QIODevice::WriteOnly)) {
qWarning() << "Couldn't create logging file: " << sDebugFilePath;
} else {
qInstallMessageHandler(LoggingHandler);
}
qDebug() << sAppName << sVersion;
qDebug() << "Compiled with Qt" << QT_VERSION_STR;
qDebug() << "Qt runtime" << qVersion();
}
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
void LoggingHandler(QtMsgType type, const QMessageLogContext &context,
const QString &sMsg) {
QString sContext = sMsg + " (" + QString::fromLatin1(context.file) + ":" +
QString::number(context.line) + ", " +
QString::fromLatin1(context.function) + ")";
QString sTime(QTime::currentTime().toString());
switch (type) {
case QtDebugMsg:
out << sTime << " Debug: " << sMsg << "\n";
out.flush();
break;
case QtWarningMsg:
out << sTime << " Warning: " << sContext << "\n";
out.flush();
break;
case QtCriticalMsg:
out << sTime << " Critical: " << sContext << "\n";
out.flush();
break;
case QtFatalMsg:
out << sTime << " Fatal: " << sContext << "\n";
out.flush();
logfile.close();
abort();
default:
out << sTime << " OTHER INFO: " << sContext << "\n";
out.flush();
break;
}
}