/
main.cpp
186 lines (153 loc) · 4.53 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
176
177
178
179
180
181
182
183
184
185
186
#include "AppInfo.h"
#include "JuffEd.h"
#include "MainSettings.h"
#include "Settings.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QTranslator>
#include <QLibraryInfo>
#include <qtsingleapplication.h>
#include <iostream>
void printHelp() {
std::cout << "Usage:" << std::endl;
std::cout << " juffed --help Print this message" << std::endl;
std::cout << " juffed --version Print JuffEd version" << std::endl;
std::cout << " juffed [FILES] Open [FILES] in JuffEd" << std::endl;
}
void printVersion() {
std::cout << AppInfo::name().toLocal8Bit().constData() << " " << AppInfo::version().toLocal8Bit().constData() << std::endl;
}
void copyToLocalDir(const QString& subDirName) {
QDir dir = QDir(AppInfo::dataDirPath() + "/" + subDirName);
QString localPath = AppInfo::configDirPath() + "/" + subDirName;
QDir localDir(localPath);
if ( !localDir.exists() ) {
localDir.mkpath(localPath);
}
foreach (QString file, dir.entryList(QDir::Files)) {
if ( !QFileInfo(localPath + "/" + file).exists() ) {
QFile::copy(dir.absolutePath() + "/" + file, localPath + "/" + file);
}
}
}
void checkForFirstRun() {
// highlight schemes and API lists
copyToLocalDir("hlschemes");
copyToLocalDir("apis");
// sessions
QString configPath = AppInfo::configDirPath();
QDir sessionDir(configPath + "/sessions/");
if ( !sessionDir.exists() )
sessionDir.mkpath(configPath + "/sessions/");
// create the log file
QFile file(AppInfo::logFile());
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
file.close();
}
QString detectLanguage() {
#ifdef Q_OS_UNIX
QByteArray lang = qgetenv("LC_ALL");
if ( lang.isEmpty() ) {
lang = qgetenv("LC_MESSAGES");
}
if ( lang.isEmpty() ) {
lang = qgetenv("LANG");
}
if ( !lang.isEmpty() ) {
return QLocale(lang).name();
}
#endif
return QLocale::system().name();
}
void loadTranslations(QApplication& app) {
QString lng = MainSettings::get(MainSettings::Language);
if ( lng.compare("auto") == 0 ) {
lng = detectLanguage();
}
QTranslator* translator = new QTranslator();
if ( translator->load("juffed_" + lng, AppInfo::translationPath()) ) {
if ( !translator->isEmpty() ) {
app.installTranslator(translator);
}
}
QTranslator* qtTranslator = new QTranslator;
qtTranslator->load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
app.installTranslator(qtTranslator);
// TODO : load plugins translations
}
void initApp(QApplication& app) {
app.setOrganizationName("juff");
app.setApplicationName("juffed");
checkForFirstRun();
#ifdef APPLEBUNDLE
// If is the app built as an Apple Bundle, the libjuff is
// staically linked into the exe. Resources has to be initialized
// manually in this case. See Qt4 docs: The Qt Resource System.
Q_INIT_RESOURCE(LibResources);
#endif
loadTranslations(app);
}
void processParams(JuffEd& juffed, QStringList params) {
// binary name itself
params.removeFirst();
foreach ( QString param, params ) {
if ( QFileInfo(param).exists() ) {
juffed.openDoc(QFileInfo(param).absoluteFilePath());
}
}
}
bool hasValidDoubleDashParam( const QCoreApplication& app ) {
const QStringList& args = app.arguments();
if ( args.size() == 2 ) {
QString param = args[1];
if ( param.compare("--help") == 0 ) {
printHelp();
return true;
}
else if ( param.compare("--version") == 0 ) {
printVersion();
return true;
}
}
return false;
}
int runSingle(int argc, char* argv[]) {
QtSingleApplication app(argc, argv);
initApp(app);
if ( hasValidDoubleDashParam( app ) ) {
return 0;
}
// check if instance already exists
QStringList fileList;
foreach (QString param, app.arguments()) {
fileList << QFileInfo(param).absoluteFilePath();
}
if ( app.sendMessage(fileList.join("\n")) )
return 0;
// instance doesn't exist yet
JuffEd juffed;
QObject::connect(&app, SIGNAL(messageReceived(const QString&)), &juffed, SLOT(onMessageReceived(const QString&)));
app.setActivationWindow(juffed.mainWindow());
juffed.mainWindow()->show();
processParams(juffed, app.arguments());
return app.exec();
}
int runNotSingle(int argc, char* argv[]) {
QApplication app(argc, argv);
initApp(app);
if ( hasValidDoubleDashParam( app ) ) {
return 0;
}
JuffEd juffed;
juffed.mainWindow()->show();
processParams(juffed, app.arguments());
return app.exec();
}
int main(int argc, char* argv[]) {
Settings::instance()->read("juff", "juffed");
if ( MainSettings::get(MainSettings::SingleInstance) )
return runSingle(argc, argv);
else
return runNotSingle(argc, argv);
}