2010-11-14 15:47:03 +00:00
|
|
|
/* This file is (c) 2008-2011 Konstantin Isakov <ikm@users.berlios.de>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
2009-05-26 15:33:54 +00:00
|
|
|
#include <stdio.h>
|
2009-02-05 20:55:00 +00:00
|
|
|
#include <QIcon>
|
2009-01-28 20:55:45 +00:00
|
|
|
#include "mainwindow.hh"
|
2009-02-01 00:08:08 +00:00
|
|
|
#include "config.hh"
|
2009-04-19 21:32:18 +00:00
|
|
|
|
2009-04-18 18:41:11 +00:00
|
|
|
#include "processwrapper.hh"
|
2009-04-19 21:32:18 +00:00
|
|
|
#include "hotkeywrapper.hh"
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-03 12:53:35 +00:00
|
|
|
//#define __DO_DEBUG
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
#ifdef __DO_DEBUG
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
|
|
|
|
2010-01-17 13:04:55 +00:00
|
|
|
#include "termination.hh"
|
|
|
|
|
2010-06-30 16:43:08 +00:00
|
|
|
#include <QWebSecurityOrigin>
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
int main( int argc, char ** argv )
|
|
|
|
{
|
2010-06-02 20:55:07 +00:00
|
|
|
// 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 )
|
|
|
|
{}
|
|
|
|
}
|
|
|
|
|
2010-01-17 13:04:55 +00:00
|
|
|
if ( argc == 3 && strcmp( argv[ 1 ], "--show-error-file" ) == 0 )
|
|
|
|
{
|
|
|
|
// The program has crashed -- show a message about it
|
|
|
|
|
|
|
|
QApplication app( argc, argv );
|
|
|
|
|
|
|
|
QFile errFile( argv[ 2 ] );
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
#ifdef __DO_DEBUG
|
|
|
|
{
|
|
|
|
rlimit limit;
|
2009-04-18 18:41:11 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
memset( &limit, 0, sizeof( limit ) );
|
|
|
|
limit.rlim_cur = RLIM_INFINITY;
|
|
|
|
limit.rlim_max = RLIM_INFINITY;
|
|
|
|
setrlimit( RLIMIT_CORE, &limit );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-05-26 15:33:54 +00:00
|
|
|
#ifdef __WIN32
|
|
|
|
|
|
|
|
// Under Windows, increase the amount of fopen()-able file descriptors from
|
|
|
|
// the default 512 up to 2048.
|
|
|
|
_setmaxstdio( 2048 );
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-04-19 21:32:18 +00:00
|
|
|
QHotkeyApplication app( argc, argv );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-12 19:41:58 +00:00
|
|
|
app.setApplicationName( "GoldenDict" );
|
2010-11-06 19:40:07 +00:00
|
|
|
app.setOrganizationDomain( "http://goldendict.org/" );
|
2009-04-12 19:41:58 +00:00
|
|
|
|
2009-02-05 20:55:00 +00:00
|
|
|
app.setWindowIcon( QIcon( ":/icons/programicon.png" ) );
|
|
|
|
|
2009-04-12 20:46:25 +00:00
|
|
|
Config::Class cfg( Config::load() );
|
|
|
|
|
2010-05-28 16:50:54 +00:00
|
|
|
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();
|
|
|
|
}
|
2009-04-18 18:41:11 +00:00
|
|
|
|
|
|
|
// Prevent execution of the 2nd copy
|
|
|
|
|
2009-05-12 18:59:00 +00:00
|
|
|
// get pid
|
|
|
|
quint64 current_pid = QCoreApplication::applicationPid();
|
|
|
|
|
2009-05-16 00:42:51 +00:00
|
|
|
// QString app_fname = QFileInfo(QCoreApplication::applicationFilePath()).baseName();
|
|
|
|
// quint64 pid = ProcessWrapper::findProcess(
|
|
|
|
// app_fname.toAscii().data(),
|
|
|
|
// current_pid);
|
|
|
|
//
|
|
|
|
// qDebug() << "pid " << pid;
|
|
|
|
// qDebug() << "current_pid " << current_pid;
|
2009-05-12 18:59:00 +00:00
|
|
|
|
|
|
|
// check pid file
|
2009-05-12 19:13:20 +00:00
|
|
|
QFile pid_file( Config::getPidFileName() );
|
|
|
|
|
|
|
|
if ( pid_file.exists() ) // pid file exists, check it
|
2009-05-12 18:59:00 +00:00
|
|
|
{
|
2009-05-12 19:13:20 +00:00
|
|
|
pid_file.open( QIODevice::ReadWrite );
|
|
|
|
QDataStream ds( &pid_file );
|
2009-05-12 18:59:00 +00:00
|
|
|
quint64 tmp; ds >> tmp;
|
2009-05-12 19:13:20 +00:00
|
|
|
pid_file.close();
|
|
|
|
|
2009-05-16 00:42:51 +00:00
|
|
|
bool isExist = ProcessWrapper::processExists(tmp);
|
2010-01-02 12:56:33 +00:00
|
|
|
if ( isExist && tmp != current_pid )
|
2009-05-12 18:59:00 +00:00
|
|
|
{
|
2009-05-16 00:42:51 +00:00
|
|
|
puts( "Another GoldenDict copy started already." );
|
2009-05-12 18:59:00 +00:00
|
|
|
return 1;
|
2009-04-19 21:32:18 +00:00
|
|
|
}
|
2009-05-16 00:42:51 +00:00
|
|
|
|
|
|
|
// if ( tmp == pid ) // it is active - exiting
|
|
|
|
// {
|
|
|
|
// // to do: switch to pid ?
|
|
|
|
// return 1;
|
|
|
|
// }
|
2009-05-12 18:59:00 +00:00
|
|
|
}
|
|
|
|
|
2009-05-12 19:13:20 +00:00
|
|
|
pid_file.open( QIODevice::WriteOnly );
|
|
|
|
QDataStream ds( &pid_file );
|
2009-05-12 18:59:00 +00:00
|
|
|
ds << current_pid;
|
|
|
|
pid_file.close();
|
2009-04-19 21:32:18 +00:00
|
|
|
|
2009-04-18 18:41:11 +00:00
|
|
|
|
2009-04-12 19:41:58 +00:00
|
|
|
// Load translations
|
|
|
|
|
|
|
|
QTranslator qtTranslator;
|
|
|
|
|
2009-04-12 20:46:25 +00:00
|
|
|
QString localeName = cfg.preferences.interfaceLanguage;
|
|
|
|
|
|
|
|
if ( localeName.isEmpty() )
|
|
|
|
localeName = QLocale::system().name();
|
|
|
|
|
2009-05-24 15:54:39 +00:00
|
|
|
if ( !qtTranslator.load( "qt_" + localeName,
|
|
|
|
QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
|
|
|
|
qtTranslator.load( "qt_" + localeName, Config::getProgramDataDir() + "/locale/" );
|
|
|
|
|
2009-04-12 19:41:58 +00:00
|
|
|
app.installTranslator( &qtTranslator );
|
|
|
|
|
|
|
|
QTranslator translator;
|
2009-05-24 17:13:43 +00:00
|
|
|
|
2009-07-29 16:39:27 +00:00
|
|
|
translator.load( Config::getLocDir() + "/" + localeName );
|
2009-05-24 17:13:43 +00:00
|
|
|
|
2009-04-12 19:41:58 +00:00
|
|
|
app.installTranslator( &translator );
|
|
|
|
|
2010-05-08 13:57:41 +00:00
|
|
|
// Prevent app from quitting spontaneously when it works with scan popup
|
|
|
|
// and with the main window closed.
|
|
|
|
app.setQuitOnLastWindowClosed( false );
|
|
|
|
|
2010-06-30 16:43:08 +00:00
|
|
|
#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
|
|
|
|
|
2009-04-12 20:46:25 +00:00
|
|
|
MainWindow m( cfg );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-06-28 15:14:07 +00:00
|
|
|
app.addDataCommiter( m );
|
|
|
|
|
2009-05-12 18:59:00 +00:00
|
|
|
int r = app.exec();
|
|
|
|
|
2010-06-28 15:14:07 +00:00
|
|
|
app.removeDataCommiter( m );
|
|
|
|
|
2009-05-12 18:59:00 +00:00
|
|
|
// remove pid file
|
|
|
|
pid_file.remove();
|
|
|
|
|
|
|
|
return r;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|