forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
439 lines (344 loc) · 10.3 KB
/
main.cc
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* This file is (c) 2008-2012 Konstantin Isakov <[email protected]>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include <stdio.h>
#include <QIcon>
#include "gdappstyle.hh"
#include "mainwindow.hh"
#include "config.hh"
#include "processwrapper.hh"
#include "hotkeywrapper.hh"
//#define __DO_DEBUG
#define LOG_TO_FILE_KEY "--log-to-file"
#ifdef Q_OS_WIN32
#include <QtCore/qt_windows.h>
#endif
#ifdef __DO_DEBUG
#include <sys/resource.h>
#endif
#include "termination.hh"
#include "atomic_rename.hh"
#include <QWebSecurityOrigin>
#include <QMessageBox>
#include <QDebug>
#include <QFile>
#include <QString>
#include "gddebug.hh"
#if defined( Q_OS_MAC ) && QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
#include "lionsupport.h"
#endif
#if ( QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 ) )
void gdMessageHandler( QtMsgType type, const QMessageLogContext &context, const QString &mess )
{
Q_UNUSED( context );
QString message( mess );
const char * msg = message.toUtf8().constData();
#else
void gdMessageHandler( QtMsgType type, const char *msg )
{
QString message = QString::fromUtf8( msg );
#endif
switch (type) {
case QtDebugMsg:
if( logFile.isOpen() )
message.insert( 0, "Debug: " );
else
fprintf(stderr, "Debug: %s\n", msg);
break;
case QtWarningMsg:
if( logFile.isOpen() )
message.insert( 0, "Warning: " );
else
fprintf(stderr, "Warning: %s\n", msg);
break;
case QtCriticalMsg:
if( logFile.isOpen() )
message.insert( 0, "Critical: " );
else
fprintf(stderr, "Critical: %s\n", msg);
break;
case QtFatalMsg:
if( logFile.isOpen() )
{
logFile.write( "Fatal: " );
logFile.write( msg );
logFile.flush();
}
else
fprintf(stderr, "Fatal: %s\n", msg);
abort();
#if QT_VERSION >= QT_VERSION_CHECK( 5, 5, 0 )
case QtInfoMsg:
if( logFile.isOpen() )
message.insert( 0, "Info: " );
else
fprintf(stderr, "Info: %s\n", msg);
break;
#endif
}
if( logFile.isOpen() )
{
message.append( "\n" );
logFile.write( message.toUtf8() );
logFile.flush();
}
}
class GDCommandLine
{
bool crashReport, logFile;
QString word, groupName, popupGroupName, errFileName;
QVector< QString > arguments;
public:
GDCommandLine( int argc, char **argv );
inline bool needCrashReport()
{ return crashReport; }
inline QString errorFileName()
{ return errFileName; }
inline bool needSetGroup()
{ return !groupName.isEmpty(); }
inline QString getGroupName()
{ return groupName; }
inline bool needSetPopupGroup()
{ return !popupGroupName.isEmpty(); }
inline QString getPopupGroupName()
{ return popupGroupName; }
inline bool needLogFile()
{ return logFile; }
inline bool needTranslateWord()
{ return !word.isEmpty(); }
inline QString wordToTranslate()
{ return word; }
};
GDCommandLine::GDCommandLine( int argc, char **argv ):
crashReport( false ),
logFile( false )
{
if( argc > 1 )
{
#ifdef Q_OS_WIN32
(void) argv;
int num;
LPWSTR *pstr = CommandLineToArgvW( GetCommandLineW(), &num );
if( pstr && num > 1 )
{
for( int i = 1; i < num; i++ )
arguments.push_back( QString::fromWCharArray( pstr[ i ] ) );
}
#else
for( int i = 1; i < argc; i++ )
arguments.push_back( QString::fromLocal8Bit( argv[ i ] ) );
#endif
// Parse command line
for( int i = 0; i < arguments.size(); i++ )
{
if( arguments[ i ].compare( "--show-error-file" ) == 0 )
{
if( i < arguments.size() - 1 )
{
errFileName = arguments[ ++i ];
crashReport = true;
}
continue;
}
else
if( arguments[ i ].compare( "--log-to-file" ) == 0 )
{
logFile = true;
continue;
}
else
if( arguments[ i ].startsWith( "--group-name=" ) )
{
groupName = arguments[ i ].mid( arguments[ i ].indexOf( '=' ) + 1 );
continue;
}
else
if( arguments[ i ].startsWith( "--popup-group-name=" ) )
{
popupGroupName = arguments[ i ].mid( arguments[ i ].indexOf( '=' ) + 1 );
continue;
}
else
word = arguments[ i ];
}
}
}
int main( int argc, char ** argv )
{
#ifdef Q_OS_MAC
setenv("LANG", "en_US.UTF-8", 1);
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
// Check for retina display
if( LionSupport::isRetinaDisplay() )
QApplication::setGraphicsSystem( "native" );
else
QApplication::setGraphicsSystem( "raster" );
#endif
#endif
// The following clause fixes a race in the MinGW runtime where throwing
// exceptions for the first time in several threads simultaneously can cause
// an abort(). This code throws first exception in a safe, single-threaded
// manner, thus avoiding that race.
{
class Dummy {};
try
{ throw Dummy(); }
catch( Dummy )
{}
}
#if defined( Q_OS_UNIX )
setlocale( LC_ALL, "" ); // use correct char set mapping
#endif
GDCommandLine gdcl( argc, argv );
if ( gdcl.needCrashReport() )
{
// The program has crashed -- show a message about it
QApplication app( argc, argv );
QFile errFile( gdcl.errorFileName() );
QString errorText;
if ( errFile.open( QFile::ReadOnly ) )
errorText = QString::fromUtf8( errFile.readAll() );
errorText += "\n" + QString( "This information is located in file %1, "
"which will be removed once you close this dialog.").arg( errFile.fileName() );
QMessageBox::critical( 0, "GoldenDict has crashed", errorText );
errFile.remove();
return 0;
}
installTerminationHandler();
#ifdef __DO_DEBUG
{
rlimit limit;
memset( &limit, 0, sizeof( limit ) );
limit.rlim_cur = RLIM_INFINITY;
limit.rlim_max = RLIM_INFINITY;
setrlimit( RLIMIT_CORE, &limit );
}
#endif
#ifdef __WIN32
// Under Windows, increase the amount of fopen()-able file descriptors from
// the default 512 up to 2048.
_setmaxstdio( 2048 );
#endif
QHotkeyApplication app( "GoldenDict", argc, argv );
if ( app.isRunning() )
{
bool wasMessage = false;
if( gdcl.needSetGroup() )
{
app.sendMessage( QString( "setGroup: " ) + gdcl.getGroupName() );
wasMessage = true;
}
if( gdcl.needSetPopupGroup() )
{
app.sendMessage( QString( "setPopupGroup: " ) + gdcl.getPopupGroupName() );
wasMessage = true;
}
if( gdcl.needTranslateWord() )
{
app.sendMessage( QString( "translateWord: " ) + gdcl.wordToTranslate() );
wasMessage = true;
}
if( !wasMessage )
app.sendMessage("bringToFront");
return 0; // Another instance is running
}
app.setApplicationName( "GoldenDict" );
app.setOrganizationDomain( "http://goldendict.org/" );
#if QT_VERSION >= 0x040600
app.setStyle(new GdAppStyle);
#endif
#ifndef Q_OS_MAC
app.setWindowIcon( QIcon( ":/icons/programicon.png" ) );
#endif
#ifdef MAKE_CHINESE_CONVERSION_SUPPORT
// OpenCC needs to load it's data files by relative path on Windows and OS X
QDir::setCurrent( Config::getProgramDataDir() );
#endif
// Load translations for system locale
QTranslator qtTranslator;
QString localeName = QLocale::system().name();
if ( !qtTranslator.load( "qt_" + localeName, Config::getLocDir() ) )
qtTranslator.load( "qt_" + localeName,
QLibraryInfo::location( QLibraryInfo::TranslationsPath ) );
app.installTranslator( &qtTranslator );
QTranslator translator;
translator.load( Config::getLocDir() + "/" + localeName );
app.installTranslator( &translator );
Config::Class cfg;
for( ; ; )
{
try
{
cfg = Config::load();
}
catch( Config::exError )
{
QMessageBox mb( QMessageBox::Warning, app.applicationName(),
app.translate( "Main", "Error in configuration file. Continue with default settings?" ),
QMessageBox::Yes | QMessageBox::No );
mb.exec();
if( mb.result() != QMessageBox::Yes )
return -1;
QString configFile = Config::getConfigFileName();
renameAtomically( configFile, configFile + ".bad" );
continue;
}
break;
}
if( gdcl.needLogFile() )
{
// Open log file
logFile.setFileName( Config::getConfigDir() + "gd_log.txt" );
logFile.remove();
logFile.open( QFile::ReadWrite );
// Write UTF-8 BOM
QByteArray line;
line.append( 0xEF ).append( 0xBB ).append( 0xBF );
logFile.write( line );
// Install message handler
#if ( QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 ) )
qInstallMessageHandler( gdMessageHandler );
#else
qInstallMsgHandler( gdMessageHandler );
#endif
}
if ( Config::isPortableVersion() )
{
// For portable version, hardcode some settings
cfg.paths.clear();
cfg.paths.push_back( Config::Path( Config::getPortableVersionDictionaryDir(), true ) );
cfg.soundDirs.clear();
cfg.hunspell.dictionariesPath = Config::getPortableVersionMorphoDir();
}
// Reload translations for user selected locale is nesessary
if( !cfg.preferences.interfaceLanguage.isEmpty() && localeName != cfg.preferences.interfaceLanguage )
{
localeName = cfg.preferences.interfaceLanguage;
if ( !qtTranslator.load( "qt_" + localeName, Config::getLocDir() ) )
qtTranslator.load( "qt_" + localeName,
QLibraryInfo::location( QLibraryInfo::TranslationsPath ) );
translator.load( Config::getLocDir() + "/" + localeName );
}
// Prevent app from quitting spontaneously when it works with scan popup
// and with the main window closed.
app.setQuitOnLastWindowClosed( false );
#if QT_VERSION >= 0x040600
// Add the dictionary scheme we use as local, so that the file:// links would
// work in the articles. The function was introduced in Qt 4.6.
QWebSecurityOrigin::addLocalScheme( "gdlookup" );
#endif
MainWindow m( cfg );
app.addDataCommiter( m );
QObject::connect( &app, SIGNAL(messageReceived(const QString&)),
&m, SLOT(messageFromAnotherInstanceReceived(const QString&)));
if( gdcl.needSetGroup() )
m.setGroupByName( gdcl.getGroupName(), true );
if( gdcl.needSetPopupGroup() )
m.setGroupByName( gdcl.getPopupGroupName(), false );
if( gdcl.needTranslateWord() )
m.wordReceived( gdcl.wordToTranslate() );
int r = app.exec();
app.removeDataCommiter( m );
if( logFile.isOpen() )
logFile.close();
return r;
}