-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.cpp
125 lines (110 loc) · 3.8 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
/* Author: Yuriy Kuzin
*/
#include "mainwindow.h"
#include "commandlineparser.h"
#include <QApplication>
#include <QTranslator>
#include <QStandardPaths>
#include <QFileInfo>
#include <QSettings>
#include <QtDebug>
const QString APP_VERSION = "0.63b";
void qtJsonDiffLogger(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
const char *file = context.file ? context.file : "";
const char *function = context.function ? context.function : "";
const char *timestamp = QDateTime::currentDateTime().toString(Qt::DateFormat::ISODate).toUtf8();
const char *level = "n/a";
switch (type)
{
case QtDebugMsg:
level = "DEBUG";
break;
case QtInfoMsg:
level = "INFO";
break;
case QtWarningMsg:
level = "WARNING";
break;
case QtCriticalMsg:
level = "CRITICAL";
break;
case QtFatalMsg:
level = "FATAL";
break;
}
fprintf(stderr, "%s [%s] %s (%s:%u, %s)\n",
timestamp, level, localMsg.constData(), file, context.line, function);
}
int main(int argc, char *argv[])
{
qInstallMessageHandler(qtJsonDiffLogger);
QApplication a(argc, argv);
QLoggingCategory::setFilterRules("*.debug=true\nqt.*.debug=false");
qInfo() << QLocale::system().name();
QStringList translations;
QDir dir(a.applicationDirPath());
if (dir.cdUp() && dir.cd("share"))
{
translations.append(dir.absolutePath() + "/" + a.applicationName());
}
translations.append(QStandardPaths::standardLocations(QStandardPaths::DataLocation));
translations.append(QCoreApplication::applicationDirPath());
translations.append(a.applicationDirPath() + "/.qm");
translations.append(a.applicationDirPath() + "/lang");
QString translationFilePath = "";
qInfo() << "Search for translations";
foreach (const QString &str, translations)
{
QFileInfo fileinfo(str + "/" + a.applicationName() + "_" + QLocale::system().name() + ".qm");
qInfo() << fileinfo.filePath();
if (fileinfo.exists() && fileinfo.isFile())
{
translationFilePath = fileinfo.filePath();
qInfo() << "Translation found in: " + translationFilePath;
break;
}
}
QTranslator translator;
qInfo() << translator.load(translationFilePath);
a.installTranslator(&translator);
QString platform = "";
#if __GNUC__
#if __x86_64__
platform = "-64bit";
#endif
#endif
a.setProperty("appversion", APP_VERSION + platform + " (QTbuild:" + QString(QT_VERSION_STR) + ")");
a.setProperty("appname", "QT JSON Diff");
#ifdef Q_OS_LINUX
a.setWindowIcon(QIcon(":/images/diff.png"));
#endif
QString settingsDir = "";
#ifdef Q_OS_WIN
settingsDir = qApp->applicationDirPath();
#endif
#if defined(Q_OS_LINUX) || defined(Q_OS_MAC)
settingsDir = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation)[0] + "/" + qAppName();
#endif
QSettings::setDefaultFormat(QSettings::IniFormat);
QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, settingsDir);
QApplication::setOrganizationName("ini");
QApplication::setApplicationName("qtjsondiff");
QApplication::setApplicationVersion(APP_VERSION);
MainWindow w;
// CLI options
CommandLineParser cliParser;
CliAppMode appMode = cliParser.parse();
if (appMode == Exit)
{
return 0;
}
if (appMode == Tree || appMode == Diff)
{
w.setDisplayMode(cliParser.files());
}
w.setWindowTitle(a.property("appname").toString() + " " + a.property("appversion").toString());
w.show();
return a.exec();
}