2022-03-06 09:05:43 +00:00
|
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
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"
|
2021-08-05 06:57:22 +00:00
|
|
|
|
#include <QWebEngineProfile>
|
2009-04-19 21:32:18 +00:00
|
|
|
|
#include "hotkeywrapper.hh"
|
2019-01-28 13:27:48 +00:00
|
|
|
|
#ifdef HAVE_X11
|
|
|
|
|
#include <fixx11h.h>
|
|
|
|
|
#endif
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
2022-01-14 15:05:00 +00:00
|
|
|
|
#if defined( Q_OS_UNIX )
|
|
|
|
|
#include <clocale>
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-11-16 13:02:56 +00:00
|
|
|
|
#ifdef Q_OS_WIN32
|
2023-05-16 14:13:23 +00:00
|
|
|
|
#include <windows.h>
|
2011-11-16 13:02:56 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2010-01-17 13:04:55 +00:00
|
|
|
|
#include "termination.hh"
|
2013-05-08 16:50:06 +00:00
|
|
|
|
#include "atomic_rename.hh"
|
2017-07-25 15:28:03 +00:00
|
|
|
|
#include <QByteArray>
|
2023-04-06 03:29:50 +00:00
|
|
|
|
#include <QCommandLineParser>
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QMessageBox>
|
2013-09-20 14:25:44 +00:00
|
|
|
|
#include <QString>
|
2023-04-06 03:29:50 +00:00
|
|
|
|
#include <QtWebEngineCore/QWebEngineUrlScheme>
|
2013-09-20 14:25:44 +00:00
|
|
|
|
|
2013-11-16 18:34:09 +00:00
|
|
|
|
#include "gddebug.hh"
|
2023-05-13 08:02:31 +00:00
|
|
|
|
#include <QMutex>
|
2013-09-20 14:25:44 +00:00
|
|
|
|
|
2023-05-01 08:01:48 +00:00
|
|
|
|
#if defined(USE_BREAKPAD)
|
|
|
|
|
#include "client/windows/handler/exception_handler.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(USE_BREAKPAD)
|
|
|
|
|
bool callback(const wchar_t* dump_path, const wchar_t* id,
|
|
|
|
|
void* context, EXCEPTION_POINTERS* exinfo,
|
|
|
|
|
MDRawAssertionInfo* assertion,
|
|
|
|
|
bool succeeded) {
|
|
|
|
|
if (succeeded) {
|
|
|
|
|
qDebug() << "Create dump file success";
|
|
|
|
|
} else {
|
|
|
|
|
qDebug() << "Create dump file failed";
|
|
|
|
|
}
|
|
|
|
|
return succeeded;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-05-13 08:02:31 +00:00
|
|
|
|
QMutex logMutex;
|
|
|
|
|
|
2013-09-27 13:03:57 +00:00
|
|
|
|
void gdMessageHandler( QtMsgType type, const QMessageLogContext &context, const QString &mess )
|
|
|
|
|
{
|
2023-05-07 15:36:42 +00:00
|
|
|
|
QString strTime = QDateTime::currentDateTime().toString( "MM-dd hh:mm:ss" );
|
2023-06-07 13:58:59 +00:00
|
|
|
|
QString message = QString( "%1 %2\r\n" )
|
2023-05-07 15:36:42 +00:00
|
|
|
|
.arg( strTime )
|
|
|
|
|
.arg( mess );
|
|
|
|
|
|
|
|
|
|
if ( ( logFilePtr != nullptr ) && logFilePtr->isOpen() ) {
|
2023-05-13 08:02:31 +00:00
|
|
|
|
//without the lock ,on multithread,there would be assert error.
|
|
|
|
|
QMutexLocker _( &logMutex );
|
2023-05-07 15:36:42 +00:00
|
|
|
|
switch ( type ) {
|
|
|
|
|
case QtDebugMsg:
|
|
|
|
|
message.insert( 0, "Debug: " );
|
|
|
|
|
break;
|
|
|
|
|
case QtWarningMsg:
|
|
|
|
|
message.insert( 0, "Warning: " );
|
|
|
|
|
break;
|
|
|
|
|
case QtCriticalMsg:
|
|
|
|
|
message.insert( 0, "Critical: " );
|
|
|
|
|
break;
|
|
|
|
|
case QtFatalMsg:
|
|
|
|
|
message.insert( 0, "Fatal: " );
|
|
|
|
|
logFilePtr->write( message.toUtf8() );
|
|
|
|
|
logFilePtr->flush();
|
|
|
|
|
abort();
|
|
|
|
|
case QtInfoMsg:
|
|
|
|
|
message.insert( 0, "Info: " );
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-09-27 13:03:57 +00:00
|
|
|
|
|
2023-05-07 15:36:42 +00:00
|
|
|
|
logFilePtr->write( message.toUtf8() );
|
|
|
|
|
logFilePtr->flush();
|
2013-09-20 14:25:44 +00:00
|
|
|
|
|
2023-05-07 15:36:42 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-05-13 08:02:31 +00:00
|
|
|
|
|
|
|
|
|
//the following code lines actually will have no chance to run, schedule to remove in the future.
|
2023-05-07 15:36:42 +00:00
|
|
|
|
QByteArray msg = mess.toUtf8().constData();
|
|
|
|
|
switch ( type ) {
|
2013-09-20 14:25:44 +00:00
|
|
|
|
case QtDebugMsg:
|
2023-05-07 15:36:42 +00:00
|
|
|
|
fprintf( stderr, "Debug: %s\n", msg.constData() );
|
2013-09-20 14:25:44 +00:00
|
|
|
|
break;
|
|
|
|
|
case QtWarningMsg:
|
2023-05-07 15:36:42 +00:00
|
|
|
|
fprintf( stderr, "Warning: %s\n", msg.constData() );
|
2013-09-20 14:25:44 +00:00
|
|
|
|
break;
|
|
|
|
|
case QtCriticalMsg:
|
2023-05-07 15:36:42 +00:00
|
|
|
|
fprintf( stderr, "Critical: %s\n", msg.constData() );
|
2013-09-20 14:25:44 +00:00
|
|
|
|
break;
|
|
|
|
|
case QtFatalMsg:
|
2023-05-07 15:36:42 +00:00
|
|
|
|
fprintf( stderr, "Fatal: %s\n", msg.constData() );
|
2013-09-20 14:25:44 +00:00
|
|
|
|
abort();
|
2015-05-20 14:52:33 +00:00
|
|
|
|
case QtInfoMsg:
|
2023-05-07 15:36:42 +00:00
|
|
|
|
fprintf( stderr, "Info: %s\n", msg.constData() );
|
2015-05-20 14:52:33 +00:00
|
|
|
|
break;
|
2013-09-20 14:25:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2010-06-30 16:43:08 +00:00
|
|
|
|
|
2023-04-18 06:56:49 +00:00
|
|
|
|
struct GDOptions
|
2017-03-10 13:29:23 +00:00
|
|
|
|
{
|
2023-04-18 06:52:28 +00:00
|
|
|
|
bool logFile = false;
|
|
|
|
|
bool togglePopup = false;
|
2023-04-06 03:29:50 +00:00
|
|
|
|
QString word, groupName, popupGroupName;
|
2017-03-10 13:29:23 +00:00
|
|
|
|
|
2023-04-18 06:56:49 +00:00
|
|
|
|
inline bool needSetGroup() const
|
2017-03-10 13:29:23 +00:00
|
|
|
|
{ return !groupName.isEmpty(); }
|
|
|
|
|
|
2023-04-18 06:56:49 +00:00
|
|
|
|
inline QString getGroupName() const
|
2017-03-10 13:29:23 +00:00
|
|
|
|
{ return groupName; }
|
|
|
|
|
|
2023-04-18 06:56:49 +00:00
|
|
|
|
inline bool needSetPopupGroup() const
|
2017-03-10 13:29:23 +00:00
|
|
|
|
{ return !popupGroupName.isEmpty(); }
|
|
|
|
|
|
2023-04-18 06:56:49 +00:00
|
|
|
|
inline QString getPopupGroupName() const
|
2017-03-10 13:29:23 +00:00
|
|
|
|
{ return popupGroupName; }
|
|
|
|
|
|
2023-04-18 06:56:49 +00:00
|
|
|
|
inline bool needLogFile() const
|
2017-03-10 13:29:23 +00:00
|
|
|
|
{ return logFile; }
|
|
|
|
|
|
2023-04-18 06:56:49 +00:00
|
|
|
|
inline bool needTranslateWord() const
|
2017-03-10 13:29:23 +00:00
|
|
|
|
{ return !word.isEmpty(); }
|
|
|
|
|
|
2023-04-18 06:56:49 +00:00
|
|
|
|
inline QString wordToTranslate() const
|
2017-03-10 13:29:23 +00:00
|
|
|
|
{ return word; }
|
2023-04-18 06:52:28 +00:00
|
|
|
|
|
|
|
|
|
inline bool needTogglePopup() const
|
|
|
|
|
{ return togglePopup; }
|
2023-05-27 01:42:58 +00:00
|
|
|
|
bool notts;
|
2017-03-10 13:29:23 +00:00
|
|
|
|
};
|
|
|
|
|
|
2023-04-18 06:56:49 +00:00
|
|
|
|
void processCommandLine( QCoreApplication * app, GDOptions * result)
|
2017-03-10 13:29:23 +00:00
|
|
|
|
{
|
2023-04-06 03:29:50 +00:00
|
|
|
|
QCommandLineParser qcmd;
|
|
|
|
|
|
|
|
|
|
qcmd.setApplicationDescription( QObject::tr( "A dictionary lookup program." ) );
|
|
|
|
|
qcmd.addHelpOption(); // -h --help
|
|
|
|
|
|
|
|
|
|
qcmd.addPositionalArgument( "word", QObject::tr( "Word or sentence to query." ), "[word]" );
|
|
|
|
|
|
|
|
|
|
QCommandLineOption logFileOption( QStringList() << "l"
|
|
|
|
|
<< "log-to-file",
|
|
|
|
|
QObject::tr( "Save debug messages to gd_log.txt in the config folder." ) );
|
|
|
|
|
|
2023-05-27 01:42:58 +00:00
|
|
|
|
QCommandLineOption notts( "no-tts", QObject::tr( "Disable tts." ) );
|
|
|
|
|
|
2023-04-06 03:29:50 +00:00
|
|
|
|
QCommandLineOption groupNameOption( QStringList() << "g"
|
|
|
|
|
<< "group-name",
|
|
|
|
|
QObject::tr( "Change the group of main window." ),
|
|
|
|
|
"groupName" );
|
|
|
|
|
QCommandLineOption popupGroupNameOption( QStringList() << "p"
|
|
|
|
|
<< "popup-group-name",
|
|
|
|
|
QObject::tr( "Change the group of popup." ),
|
|
|
|
|
"popupGroupName" );
|
|
|
|
|
|
2023-04-18 06:52:28 +00:00
|
|
|
|
QCommandLineOption togglePopupOption( QStringList() << "t"
|
|
|
|
|
<< "toggle-scan-popup",
|
|
|
|
|
QObject::tr( "Toggle scan popup." ) );
|
|
|
|
|
|
2023-04-06 03:29:50 +00:00
|
|
|
|
qcmd.addOption( logFileOption );
|
|
|
|
|
qcmd.addOption( groupNameOption );
|
|
|
|
|
qcmd.addOption( popupGroupNameOption );
|
2023-04-18 06:52:28 +00:00
|
|
|
|
qcmd.addOption( togglePopupOption );
|
2023-05-27 01:42:58 +00:00
|
|
|
|
qcmd.addOption( notts );
|
2023-04-06 03:29:50 +00:00
|
|
|
|
|
|
|
|
|
QCommandLineOption doNothingOption( "disable-web-security" ); // ignore the --disable-web-security
|
|
|
|
|
doNothingOption.setFlags( QCommandLineOption::HiddenFromHelp );
|
|
|
|
|
qcmd.addOption( doNothingOption );
|
|
|
|
|
|
|
|
|
|
qcmd.process( *app );
|
|
|
|
|
|
|
|
|
|
if ( qcmd.isSet( logFileOption ) ) {
|
2023-04-18 06:56:49 +00:00
|
|
|
|
result->logFile = true;
|
2023-04-06 03:29:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( qcmd.isSet( groupNameOption ) ) {
|
2023-04-18 06:56:49 +00:00
|
|
|
|
result->groupName = qcmd.value( groupNameOption );
|
2023-04-06 03:29:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( qcmd.isSet( popupGroupNameOption ) ) {
|
2023-04-18 06:56:49 +00:00
|
|
|
|
result->popupGroupName = qcmd.value( popupGroupNameOption );
|
2023-04-06 03:29:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 06:52:28 +00:00
|
|
|
|
if ( qcmd.isSet( togglePopupOption ) ) {
|
|
|
|
|
result->togglePopup = true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-27 01:42:58 +00:00
|
|
|
|
if ( qcmd.isSet( notts ) ) {
|
|
|
|
|
result->notts = true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 03:29:50 +00:00
|
|
|
|
const QStringList posArgs = qcmd.positionalArguments();
|
|
|
|
|
if ( !posArgs.empty() ) {
|
2023-04-18 06:56:49 +00:00
|
|
|
|
result->word = posArgs.at( 0 );
|
2023-04-06 03:29:50 +00:00
|
|
|
|
|
|
|
|
|
#if defined( Q_OS_LINUX ) || defined( Q_OS_WIN )
|
2023-04-27 12:52:55 +00:00
|
|
|
|
// handle url scheme like "goldendict://" or "dict://" on windows/linux
|
2023-05-02 03:30:51 +00:00
|
|
|
|
auto schemePos = result->word.indexOf( "://" );
|
|
|
|
|
if ( schemePos != -1 ) {
|
|
|
|
|
result->word.remove( 0, schemePos + 3 );
|
|
|
|
|
// In microsoft Words, the / will be automatically appended
|
|
|
|
|
if ( result->word.endsWith( "/" ) ) {
|
|
|
|
|
result->word.chop( 1 );
|
|
|
|
|
}
|
2017-03-10 13:29:23 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
|
int main( int argc, char ** argv )
|
|
|
|
|
{
|
2020-03-27 10:08:52 +00:00
|
|
|
|
#ifdef Q_OS_UNIX
|
2023-05-16 14:13:23 +00:00
|
|
|
|
// GoldenDict use lots of X11 functions and it currently cannot work
|
|
|
|
|
// natively on Wayland. This workaround will force GoldenDict to use
|
|
|
|
|
// XWayland.
|
|
|
|
|
char * xdg_envc = getenv( "XDG_SESSION_TYPE" );
|
|
|
|
|
QString xdg_session = xdg_envc ? QString::fromLatin1( xdg_envc ) : QString();
|
|
|
|
|
if ( !QString::compare( xdg_session, QString( "wayland" ), Qt::CaseInsensitive ) ) {
|
|
|
|
|
setenv( "QT_QPA_PLATFORM", "xcb", 1 );
|
|
|
|
|
}
|
2020-03-27 10:08:52 +00:00
|
|
|
|
#endif
|
2023-05-16 14:13:23 +00:00
|
|
|
|
|
2023-04-28 00:21:36 +00:00
|
|
|
|
#ifdef Q_OS_MAC
|
2023-05-16 14:13:23 +00:00
|
|
|
|
setenv( "LANG", "en_US.UTF-8", 1 );
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
|
// attach the new console to this application's process
|
|
|
|
|
if ( AttachConsole( ATTACH_PARENT_PROCESS ) ) {
|
|
|
|
|
// reopen the std I/O streams to redirect I/O to the new console
|
|
|
|
|
freopen( "CON", "w", stdout );
|
|
|
|
|
freopen( "CON", "w", stderr );
|
|
|
|
|
}
|
2014-06-17 13:47:04 +00:00
|
|
|
|
|
2017-03-15 14:47:07 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2023-04-28 00:21:36 +00:00
|
|
|
|
|
2023-04-06 03:29:50 +00:00
|
|
|
|
//high dpi screen support
|
|
|
|
|
#if ( QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) )
|
|
|
|
|
QApplication::setAttribute( Qt::AA_EnableHighDpiScaling );
|
|
|
|
|
QApplication::setAttribute( Qt::AA_UseHighDpiPixmaps );
|
|
|
|
|
#endif
|
|
|
|
|
qputenv( "QT_AUTO_SCREEN_SCALE_FACTOR", "1" );
|
|
|
|
|
QApplication::setHighDpiScaleFactorRoundingPolicy( Qt::HighDpiScaleFactorRoundingPolicy::PassThrough );
|
2010-01-17 13:04:55 +00:00
|
|
|
|
|
|
|
|
|
|
2023-04-06 03:29:50 +00:00
|
|
|
|
char ARG_DISABLE_WEB_SECURITY[] = "--disable-web-security";
|
|
|
|
|
int newArgc = argc + 1 + 1;
|
|
|
|
|
char ** newArgv = new char *[ newArgc ];
|
2023-05-01 08:01:48 +00:00
|
|
|
|
for ( int i = 0; i < argc; i++ ) {
|
2023-05-16 14:13:23 +00:00
|
|
|
|
newArgv[ i ] = argv[ i ];
|
2023-04-06 03:29:50 +00:00
|
|
|
|
}
|
|
|
|
|
newArgv[ argc ] = ARG_DISABLE_WEB_SECURITY;
|
|
|
|
|
newArgv[ argc + 1 ] = nullptr;
|
2010-01-17 13:04:55 +00:00
|
|
|
|
|
2023-05-05 13:38:29 +00:00
|
|
|
|
QHotkeyApplication app( "GoldenDict-ng", newArgc, newArgv );
|
2010-01-17 13:04:55 +00:00
|
|
|
|
|
2023-05-01 12:42:38 +00:00
|
|
|
|
QHotkeyApplication::setApplicationName( "GoldenDict-ng" );
|
|
|
|
|
QHotkeyApplication::setOrganizationDomain( "https://github.com/xiaoyifang/goldendict-ng" );
|
2023-04-06 03:29:50 +00:00
|
|
|
|
#ifndef Q_OS_MAC
|
2023-05-01 12:42:38 +00:00
|
|
|
|
QHotkeyApplication::setWindowIcon( QIcon( ":/icons/programicon.png" ) );
|
2023-04-06 03:29:50 +00:00
|
|
|
|
#endif
|
2010-01-17 13:04:55 +00:00
|
|
|
|
|
2023-05-01 08:01:48 +00:00
|
|
|
|
#if defined(USE_BREAKPAD)
|
|
|
|
|
QString appDirPath = QCoreApplication::applicationDirPath() + "/crash";
|
|
|
|
|
|
|
|
|
|
QDir dir;
|
2023-05-16 14:13:23 +00:00
|
|
|
|
if ( !dir.exists( appDirPath ) ) {
|
|
|
|
|
dir.mkpath( appDirPath );
|
2023-05-01 08:01:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
google_breakpad::ExceptionHandler eh(
|
|
|
|
|
appDirPath.toStdWString(), NULL, callback, NULL,
|
|
|
|
|
google_breakpad::ExceptionHandler::HANDLER_ALL);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-04-18 06:56:49 +00:00
|
|
|
|
GDOptions gdcl{};
|
|
|
|
|
|
|
|
|
|
if ( argc > 1 ) {
|
|
|
|
|
processCommandLine( &app, &gdcl );
|
|
|
|
|
}
|
2010-01-17 13:04:55 +00:00
|
|
|
|
|
|
|
|
|
installTerminationHandler();
|
|
|
|
|
|
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
|
|
|
|
|
|
2023-05-01 12:42:38 +00:00
|
|
|
|
const QStringList localSchemes =
|
|
|
|
|
{ "gdlookup", "gdau", "gico", "qrcx", "bres", "bword", "gdprg", "gdvideo", "gdpicture", "gdtts", "ifr", "entry" };
|
2021-08-21 01:41:40 +00:00
|
|
|
|
|
2023-05-01 12:42:38 +00:00
|
|
|
|
for ( const auto & localScheme : localSchemes ) {
|
2023-05-13 05:48:13 +00:00
|
|
|
|
QWebEngineUrlScheme webUiScheme( localScheme.toLatin1() );
|
|
|
|
|
webUiScheme.setFlags( QWebEngineUrlScheme::SecureScheme | QWebEngineUrlScheme::LocalScheme
|
|
|
|
|
| QWebEngineUrlScheme::LocalAccessAllowed | QWebEngineUrlScheme::CorsEnabled );
|
|
|
|
|
QWebEngineUrlScheme::registerScheme( webUiScheme );
|
2021-09-21 10:22:08 +00:00
|
|
|
|
}
|
2021-08-21 01:41:40 +00:00
|
|
|
|
|
2023-05-12 00:12:46 +00:00
|
|
|
|
QFile file;
|
|
|
|
|
logFilePtr = &file;
|
|
|
|
|
auto guard = qScopeGuard( [ &file ]() {
|
|
|
|
|
logFilePtr = nullptr;
|
|
|
|
|
file.close();
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
Q_UNUSED( guard )
|
|
|
|
|
|
2023-05-13 05:48:13 +00:00
|
|
|
|
QFont f = QApplication::font();
|
|
|
|
|
f.setStyleStrategy( QFont::PreferAntialias );
|
|
|
|
|
QApplication::setFont( f );
|
2010-11-20 13:43:55 +00:00
|
|
|
|
|
2011-11-16 13:02:56 +00:00
|
|
|
|
if ( app.isRunning() )
|
|
|
|
|
{
|
2017-03-10 13:29:23 +00:00
|
|
|
|
bool wasMessage = false;
|
|
|
|
|
|
|
|
|
|
if( gdcl.needSetGroup() )
|
2011-11-16 13:02:56 +00:00
|
|
|
|
{
|
2017-03-10 13:29:23 +00:00
|
|
|
|
app.sendMessage( QString( "setGroup: " ) + gdcl.getGroupName() );
|
|
|
|
|
wasMessage = true;
|
2011-11-16 13:02:56 +00:00
|
|
|
|
}
|
2017-03-10 13:29:23 +00:00
|
|
|
|
|
|
|
|
|
if( gdcl.needSetPopupGroup() )
|
|
|
|
|
{
|
|
|
|
|
app.sendMessage( QString( "setPopupGroup: " ) + gdcl.getPopupGroupName() );
|
|
|
|
|
wasMessage = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( gdcl.needTranslateWord() )
|
|
|
|
|
{
|
|
|
|
|
app.sendMessage( QString( "translateWord: " ) + gdcl.wordToTranslate() );
|
|
|
|
|
wasMessage = true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 06:52:28 +00:00
|
|
|
|
if ( gdcl.needTogglePopup() ) {
|
|
|
|
|
app.sendMessage( QStringLiteral( "toggleScanPopup" ) );
|
|
|
|
|
wasMessage = true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-10 13:29:23 +00:00
|
|
|
|
if( !wasMessage )
|
2011-11-16 13:02:56 +00:00
|
|
|
|
app.sendMessage("bringToFront");
|
2017-03-10 13:29:23 +00:00
|
|
|
|
|
2010-11-20 13:43:55 +00:00
|
|
|
|
return 0; // Another instance is running
|
2011-11-16 13:02:56 +00:00
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
2015-10-26 03:56:39 +00:00
|
|
|
|
#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
|
|
|
|
|
|
2013-05-09 09:37:26 +00:00
|
|
|
|
// Load translations for system locale
|
|
|
|
|
QString localeName = QLocale::system().name();
|
|
|
|
|
|
2013-05-08 16:50:06 +00:00
|
|
|
|
Config::Class cfg;
|
|
|
|
|
for( ; ; )
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-05-09 09:37:26 +00:00
|
|
|
|
cfg = Config::load();
|
2013-05-08 16:50:06 +00:00
|
|
|
|
}
|
2018-05-22 14:48:14 +00:00
|
|
|
|
catch( Config::exError & )
|
2013-05-08 16:50:06 +00:00
|
|
|
|
{
|
2023-05-07 15:36:42 +00:00
|
|
|
|
QMessageBox mb( QMessageBox::Warning, QHotkeyApplication::applicationName(),
|
|
|
|
|
QHotkeyApplication::translate( "Main", "Error in configuration file. Continue with default settings?" ),
|
2013-05-08 16:50:06 +00:00
|
|
|
|
QMessageBox::Yes | QMessageBox::No );
|
|
|
|
|
mb.exec();
|
2023-05-27 01:42:58 +00:00
|
|
|
|
if ( mb.result() != QMessageBox::Yes )
|
2013-05-08 16:50:06 +00:00
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
QString configFile = Config::getConfigFileName();
|
|
|
|
|
renameAtomically( configFile, configFile + ".bad" );
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-04-12 20:46:25 +00:00
|
|
|
|
|
2023-05-27 01:42:58 +00:00
|
|
|
|
if ( gdcl.notts ) {
|
|
|
|
|
cfg.notts = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( gdcl.needLogFile() ) {
|
2013-09-20 14:25:44 +00:00
|
|
|
|
// Open log file
|
2018-05-23 09:37:35 +00:00
|
|
|
|
logFilePtr->setFileName( Config::getConfigDir() + "gd_log.txt" );
|
|
|
|
|
logFilePtr->remove();
|
|
|
|
|
logFilePtr->open( QFile::ReadWrite );
|
2013-09-20 14:25:44 +00:00
|
|
|
|
|
2013-11-16 18:34:09 +00:00
|
|
|
|
// Write UTF-8 BOM
|
|
|
|
|
QByteArray line;
|
|
|
|
|
line.append( 0xEF ).append( 0xBB ).append( 0xBF );
|
2018-05-23 09:37:35 +00:00
|
|
|
|
logFilePtr->write( line );
|
2013-11-16 18:34:09 +00:00
|
|
|
|
|
2013-09-20 14:25:44 +00:00
|
|
|
|
// Install message handler
|
2013-09-27 13:03:57 +00:00
|
|
|
|
qInstallMessageHandler( gdMessageHandler );
|
2013-09-20 14:25:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2013-05-09 09:37:26 +00:00
|
|
|
|
// Reload translations for user selected locale is nesessary
|
2022-03-03 15:02:15 +00:00
|
|
|
|
QTranslator qtTranslator;
|
|
|
|
|
QTranslator translator;
|
2013-05-09 09:37:26 +00:00
|
|
|
|
if( !cfg.preferences.interfaceLanguage.isEmpty() && localeName != cfg.preferences.interfaceLanguage )
|
|
|
|
|
{
|
|
|
|
|
localeName = cfg.preferences.interfaceLanguage;
|
2022-03-03 15:02:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-06 09:05:43 +00:00
|
|
|
|
QLocale locale( localeName );
|
|
|
|
|
QLocale::setDefault( locale );
|
2023-04-21 01:36:26 +00:00
|
|
|
|
if( !qtTranslator.load( "qt_extra_" + localeName, Config::getLocDir() ) )
|
2022-03-06 09:05:43 +00:00
|
|
|
|
{
|
2023-04-21 01:36:26 +00:00
|
|
|
|
qtTranslator.load( "qt_extra_" + localeName, QLibraryInfo::location( QLibraryInfo::TranslationsPath ) );
|
2022-03-06 09:05:43 +00:00
|
|
|
|
app.installTranslator( &qtTranslator );
|
|
|
|
|
}
|
2022-03-03 15:02:15 +00:00
|
|
|
|
|
2022-09-21 15:40:06 +00:00
|
|
|
|
translator.load( Config::getLocDir() + "/" + localeName );
|
2022-03-06 09:05:43 +00:00
|
|
|
|
app.installTranslator( &translator );
|
2009-05-24 17:13:43 +00:00
|
|
|
|
|
2022-03-06 09:05:43 +00:00
|
|
|
|
QTranslator webengineTs;
|
|
|
|
|
if( webengineTs.load( "qtwebengine_" + localeName, Config::getLocDir() ) )
|
|
|
|
|
{
|
|
|
|
|
app.installTranslator( &webengineTs );
|
2013-05-09 09:37:26 +00:00
|
|
|
|
}
|
2009-04-12 19:41:58 +00:00
|
|
|
|
|
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 );
|
|
|
|
|
|
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 );
|
|
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
|
QObject::connect( &app, &QtSingleApplication::messageReceived, &m, &MainWindow::messageFromAnotherInstanceReceived );
|
2010-11-20 13:43:55 +00:00
|
|
|
|
|
2017-03-10 13:29:23 +00:00
|
|
|
|
if( gdcl.needSetGroup() )
|
|
|
|
|
m.setGroupByName( gdcl.getGroupName(), true );
|
|
|
|
|
|
|
|
|
|
if( gdcl.needSetPopupGroup() )
|
|
|
|
|
m.setGroupByName( gdcl.getPopupGroupName(), false );
|
|
|
|
|
|
|
|
|
|
if( gdcl.needTranslateWord() )
|
|
|
|
|
m.wordReceived( gdcl.wordToTranslate() );
|
2011-11-16 13:02:56 +00:00
|
|
|
|
|
2009-05-12 18:59:00 +00:00
|
|
|
|
int r = app.exec();
|
|
|
|
|
|
2010-06-28 15:14:07 +00:00
|
|
|
|
app.removeDataCommiter( m );
|
|
|
|
|
|
2018-05-23 09:37:35 +00:00
|
|
|
|
if( logFilePtr->isOpen() )
|
|
|
|
|
logFilePtr->close();
|
2015-05-20 14:52:33 +00:00
|
|
|
|
|
2009-05-12 18:59:00 +00:00
|
|
|
|
return r;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
}
|