valentina/src/app/puzzle/vpapplication.cpp

812 lines
30 KiB
C++
Raw Normal View History

2020-02-16 18:18:39 +01:00
/************************************************************************
**
2020-05-23 14:10:05 +02:00
** @file vpapplication.cpp
2020-02-16 18:18:39 +01:00
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 16 2, 2020
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2020 Valentina project
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
**
** Valentina is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Valentina is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
2020-05-23 14:10:05 +02:00
#include "vpapplication.h"
#include "../fervor/fvupdater.h"
2020-02-16 18:18:39 +01:00
#include "../ifc/exception/vexceptionbadid.h"
#include "../ifc/exception/vexceptionconversionerror.h"
#include "../ifc/exception/vexceptionemptyparameter.h"
2023-06-08 12:20:20 +02:00
#include "../ifc/exception/vexceptionobjecterror.h"
2020-02-16 18:18:39 +01:00
#include "../ifc/exception/vexceptionwrongid.h"
2023-06-27 13:15:21 +02:00
#include "../vganalytics/vganalytics.h"
#include "../vmisc/projectversion.h"
#include "../vmisc/qt_dispatch/qt_dispatch.h"
#include "../vmisc/theme/vtheme.h"
2020-02-16 18:18:39 +01:00
#include "../vmisc/vsysexits.h"
2023-06-08 12:20:20 +02:00
#include "vpmainwindow.h"
2023-10-23 15:57:22 +02:00
#include "vpuzzleshortcutmanager.h"
2022-08-03 13:41:19 +02:00
#include <QCommandLineParser>
#include <QEvent>
#include <QFileOpenEvent>
#include <QLocalServer>
#include <QLocalSocket>
#include <QLoggingCategory>
2023-06-08 12:20:20 +02:00
#include <QMessageBox>
#include <QPixmapCache>
#include <QStyleFactory>
2020-02-16 18:18:39 +01:00
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
#include "../vmisc/compatibility.h"
#endif
2024-02-17 15:46:59 +01:00
#if !defined(BUILD_REVISION) && defined(QBS_BUILD)
#include <vcsRepoState.h>
#define BUILD_REVISION VCS_REPO_STATE_REVISION
#endif
using namespace Qt::Literals::StringLiterals;
2020-02-16 18:18:39 +01:00
QT_WARNING_PUSH
QT_WARNING_DISABLE_CLANG("-Wmissing-prototypes")
QT_WARNING_DISABLE_INTEL(1418)
2022-08-03 13:41:19 +02:00
Q_LOGGING_CATEGORY(pApp, "p.application") // NOLINT
2020-02-16 18:18:39 +01:00
QT_WARNING_POP
#define VER_INTERNALNAME_STR "Puzzle"
#define VER_ORIGINALFILENAME_STR "puzzle.exe"
#define VER_PRODUCTNAME_STR "Puzzle"
#define VER_FILEDESCRIPTION_STR "Valentina's manual layout creator."
2020-02-16 18:18:39 +01:00
//---------------------------------------------------------------------------------------------------------------------
2023-06-08 12:20:20 +02:00
inline void noisyFailureMsgHandler(QtMsgType type, const QMessageLogContext &context,
const QString &msg) // NOLINT(readability-function-cognitive-complexity)
2020-02-16 18:18:39 +01:00
{
// only the GUI thread should display message boxes. If you are
// writing a multithreaded application and the error happens on
// a non-GUI thread, you'll have to queue the message to the GUI
QCoreApplication *instance = QCoreApplication::instance();
2021-05-20 16:10:43 +02:00
const bool isGuiThread = (instance != nullptr) && (QThread::currentThread() == instance->thread());
2020-02-16 18:18:39 +01:00
if (not isGuiThread)
{
auto Handler = [](QtMsgType type, const QMessageLogContext &context, const QString &msg)
2023-06-08 12:20:20 +02:00
{ noisyFailureMsgHandler(type, context, msg); };
2020-02-16 18:18:39 +01:00
q_dispatch_async_main(Handler, type, context, msg);
return;
}
2024-01-06 13:20:56 +01:00
if (VAbstractApplication::VApp()->IsWarningMessage(msg))
{
return;
}
2020-02-16 18:18:39 +01:00
// Why on earth didn't Qt want to make failed signal/slot connections qWarning?
if ((type == QtDebugMsg) && msg.contains(QStringLiteral("::connect")))
{
type = QtWarningMsg;
}
#if defined(V_NO_ASSERT)
// I have decided to hide this annoing message for release builds.
if ((type == QtWarningMsg) && msg.contains(QStringLiteral("QSslSocket: cannot resolve")))
{
type = QtDebugMsg;
}
if ((type == QtWarningMsg) && msg.contains(QStringLiteral("setGeometry: Unable to set geometry")))
{
type = QtDebugMsg;
}
2023-06-08 12:20:20 +02:00
#endif // defined(V_NO_ASSERT)
2020-02-16 18:18:39 +01:00
#if defined(Q_OS_MAC)
// Hide anything that starts with QMacCGContext
if ((type == QtWarningMsg) && msg.contains(QStringLiteral("QMacCGContext::")))
{
type = QtDebugMsg;
}
2020-02-16 18:18:39 +01:00
// See issue #568
if (msg.contains(QStringLiteral("Error receiving trust for a CA certificate")))
{
type = QtDebugMsg;
}
#endif
// this is another one that doesn't make sense as just a debug message. pretty serious
// sign of a problem
// http://www.developer.nokia.com/Community/Wiki/QPainter::begin:Paint_device_returned_engine_%3D%3D_0_(Known_Issue)
2023-06-08 12:20:20 +02:00
if ((type == QtDebugMsg) && msg.contains(QStringLiteral("QPainter::begin")) &&
msg.contains(QStringLiteral("Paint device returned engine")))
2020-02-16 18:18:39 +01:00
{
type = QtWarningMsg;
}
// This qWarning about "Cowardly refusing to send clipboard message to hung application..."
// is something that can easily happen if you are debugging and the application is paused.
// As it is so common, not worth popping up a dialog.
2023-06-08 12:20:20 +02:00
if ((type == QtWarningMsg) && msg.contains(QStringLiteral("QClipboard::event")) &&
msg.contains(QStringLiteral("Cowardly refusing")))
2020-02-16 18:18:39 +01:00
{
type = QtDebugMsg;
}
2023-06-22 17:30:43 +02:00
// Annoying warning that we can ignore
if ((type == QtWarningMsg) && (msg.contains(QStringLiteral("OpenType support missing for")) ||
msg.contains(QStringLiteral("DirectWrite: CreateFontFaceFromHDC() failed (Indicates "
"an error in an input file such as a font file.)"))))
{
type = QtDebugMsg;
}
2020-02-16 18:18:39 +01:00
{
QString debugdate = "["_L1 + QDateTime::currentDateTime().toString(QStringLiteral("yyyy.MM.dd hh:mm:ss"));
QString const file = VAbstractApplication::ReduceLogContextFilePath(context.file);
2020-02-16 18:18:39 +01:00
switch (type)
{
case QtDebugMsg:
debugdate += QStringLiteral(":DEBUG:%1(%2)] %3: %4: %5")
.arg(file)
.arg(context.line)
.arg(context.function, context.category, msg);
vStdOut() << QApplication::translate("mNoisyHandler", "DEBUG:") << msg << "\n";
break;
case QtWarningMsg:
debugdate += QStringLiteral(":WARNING:%1(%2)] %3: %4: %5")
.arg(file)
.arg(context.line)
.arg(context.function, context.category, msg);
vStdErr() << QApplication::translate("mNoisyHandler", "WARNING:") << msg << "\n";
break;
case QtCriticalMsg:
debugdate += QStringLiteral(":CRITICAL:%1(%2)] %3: %4: %5")
.arg(file)
.arg(context.line)
.arg(context.function, context.category, msg);
vStdErr() << QApplication::translate("mNoisyHandler", "CRITICAL:") << msg << "\n";
break;
case QtFatalMsg:
debugdate += QStringLiteral(":FATAL:%1(%2)] %3: %4: %5")
.arg(file)
.arg(context.line)
.arg(context.function, context.category, msg);
vStdErr() << QApplication::translate("mNoisyHandler", "FATAL:") << msg << "\n";
break;
case QtInfoMsg:
debugdate += QStringLiteral(":INFO:%1(%2)] %3: %4: %5")
.arg(file)
.arg(context.line)
.arg(context.function, context.category, msg);
vStdOut() << QApplication::translate("mNoisyHandler", "INFO:") << msg << "\n";
break;
default:
break;
}
vStdOut().flush();
vStdErr().flush();
(*VPApplication::VApp()->LogFile()) << debugdate << Qt::endl;
}
2020-02-16 18:18:39 +01:00
if (isGuiThread)
{
2023-06-08 12:20:20 +02:00
// fixme: trying to make sure there are no save/load dialogs are opened, because error message during them will
// lead to crash
2020-02-16 18:18:39 +01:00
const bool topWinAllowsPop = (QApplication::activeModalWidget() == nullptr) ||
2023-06-08 12:20:20 +02:00
!QApplication::activeModalWidget()->inherits("QFileDialog");
2020-02-16 18:18:39 +01:00
QMessageBox messageBox;
switch (type)
{
case QtWarningMsg:
messageBox.setWindowTitle(QApplication::translate("mNoisyHandler", "Warning"));
messageBox.setIcon(QMessageBox::Warning);
break;
case QtCriticalMsg:
messageBox.setWindowTitle(QApplication::translate("mNoisyHandler", "Critical error"));
messageBox.setIcon(QMessageBox::Critical);
break;
case QtFatalMsg:
messageBox.setWindowTitle(QApplication::translate("mNoisyHandler", "Fatal error"));
messageBox.setIcon(QMessageBox::Critical);
break;
case QtInfoMsg:
messageBox.setWindowTitle(QApplication::translate("mNoisyHandler", "Information"));
messageBox.setIcon(QMessageBox::Information);
break;
case QtDebugMsg:
default:
break;
}
if (type == QtWarningMsg || type == QtCriticalMsg || type == QtFatalMsg)
{
2021-05-20 16:10:43 +02:00
if (VPApplication::VApp()->IsAppInGUIMode())
2020-02-16 18:18:39 +01:00
{
if (topWinAllowsPop)
{
messageBox.setText(msg);
2020-02-16 18:18:39 +01:00
messageBox.setStandardButtons(QMessageBox::Ok);
messageBox.setWindowModality(Qt::ApplicationModal);
messageBox.setModal(true);
2023-06-08 12:20:20 +02:00
#ifndef QT_NO_CURSOR
2020-02-16 18:18:39 +01:00
QGuiApplication::setOverrideCursor(Qt::ArrowCursor);
2023-06-08 12:20:20 +02:00
#endif
2020-02-16 18:18:39 +01:00
messageBox.exec();
2023-06-08 12:20:20 +02:00
#ifndef QT_NO_CURSOR
2020-02-16 18:18:39 +01:00
QGuiApplication::restoreOverrideCursor();
2023-06-08 12:20:20 +02:00
#endif
2020-02-16 18:18:39 +01:00
}
}
}
if (QtFatalMsg == type)
{
abort();
}
}
else
{
if (type != QtDebugMsg)
{
abort(); // be NOISY unless overridden!
}
}
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:10:05 +02:00
VPApplication::VPApplication(int &argc, char **argv)
2023-06-08 12:20:20 +02:00
: VAbstractApplication(argc, argv)
2020-02-16 18:18:39 +01:00
{
setApplicationDisplayName(QStringLiteral(VER_PRODUCTNAME_STR));
setApplicationName(QStringLiteral(VER_INTERNALNAME_STR));
2022-08-03 13:41:19 +02:00
setOrganizationName(QStringLiteral(VER_COMPANYNAME_STR));
setOrganizationDomain(QStringLiteral(VER_COMPANYDOMAIN_STR));
2020-02-16 18:18:39 +01:00
// Setting the Application version
2023-07-25 13:01:41 +02:00
setApplicationVersion(AppVersionStr());
2020-02-16 18:18:39 +01:00
// We have been running Puzzle in two different cases.
// The first inside own bundle where info.plist is works fine, but the second,
// when we run inside Valentina's bundle, require direct setting the icon.
setWindowIcon(QIcon(":/puzzleicon/64x64/logo.png"));
VTheme::Instance()->StoreDefaultThemeName(QIcon::themeName());
2020-02-16 18:18:39 +01:00
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:10:05 +02:00
VPApplication::~VPApplication()
2020-02-16 18:18:39 +01:00
{
if (VPApplication::IsAppInGUIMode() && settings->IsCollectStatistic())
2023-06-27 13:15:21 +02:00
{
auto *statistic = VGAnalytics::Instance();
2024-02-19 17:09:56 +01:00
QString const clientID = settings->GetClientID();
2023-06-27 13:15:21 +02:00
if (!clientID.isEmpty())
{
statistic->SendAppCloseEvent(m_uptimeTimer.elapsed());
}
}
2022-08-03 13:41:19 +02:00
qDeleteAll(m_mainWindows);
2020-02-16 18:18:39 +01:00
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief notify Reimplemented from QApplication::notify().
* @param receiver receiver.
* @param event event.
* @return value that is returned from the receiver's event handler.
*/
// reimplemented from QApplication so we can throw exceptions in slots
2021-05-20 16:10:43 +02:00
auto VPApplication::notify(QObject *receiver, QEvent *event) -> bool
2020-02-16 18:18:39 +01:00
{
try
{
return QApplication::notify(receiver, event);
}
catch (const VExceptionObjectError &e)
{
2023-06-08 12:20:20 +02:00
qCCritical(pApp, "%s\n\n%s\n\n%s",
qUtf8Printable(tr("Error parsing file. Program will be terminated.")), //-V807
2020-02-16 18:18:39 +01:00
qUtf8Printable(e.ErrorMessage()), qUtf8Printable(e.DetailedInformation()));
exit(V_EX_DATAERR);
}
catch (const VExceptionBadId &e)
{
qCCritical(pApp, "%s\n\n%s\n\n%s", qUtf8Printable(tr("Error bad id. Program will be terminated.")),
2020-02-16 18:18:39 +01:00
qUtf8Printable(e.ErrorMessage()), qUtf8Printable(e.DetailedInformation()));
exit(V_EX_DATAERR);
}
catch (const VExceptionConversionError &e)
{
qCCritical(pApp, "%s\n\n%s\n\n%s", qUtf8Printable(tr("Error can't convert value. Program will be terminated.")),
2020-02-16 18:18:39 +01:00
qUtf8Printable(e.ErrorMessage()), qUtf8Printable(e.DetailedInformation()));
exit(V_EX_DATAERR);
}
catch (const VExceptionEmptyParameter &e)
{
qCCritical(pApp, "%s\n\n%s\n\n%s", qUtf8Printable(tr("Error empty parameter. Program will be terminated.")),
2020-02-16 18:18:39 +01:00
qUtf8Printable(e.ErrorMessage()), qUtf8Printable(e.DetailedInformation()));
exit(V_EX_DATAERR);
}
catch (const VExceptionWrongId &e)
{
qCCritical(pApp, "%s\n\n%s\n\n%s", qUtf8Printable(tr("Error wrong id. Program will be terminated.")),
2020-02-16 18:18:39 +01:00
qUtf8Printable(e.ErrorMessage()), qUtf8Printable(e.DetailedInformation()));
exit(V_EX_DATAERR);
}
catch (const VExceptionToolWasDeleted &e)
{
qCCritical(pApp, "%s\n\n%s\n\n%s",
2022-08-12 17:50:13 +02:00
qUtf8Printable(QStringLiteral("Unhadled deleting tool. Continue use object after deleting!")),
2020-02-16 18:18:39 +01:00
qUtf8Printable(e.ErrorMessage()), qUtf8Printable(e.DetailedInformation()));
exit(V_EX_DATAERR);
}
catch (const VException &e)
{
2023-06-08 12:20:20 +02:00
qCCritical(pApp, "%s\n\n%s\n\n%s", qUtf8Printable(tr("Something's wrong!!")), qUtf8Printable(e.ErrorMessage()),
qUtf8Printable(e.DetailedInformation()));
2020-02-16 18:18:39 +01:00
return true;
}
catch (std::exception &e)
{
qCCritical(pApp, "%s", qUtf8Printable(tr("Exception thrown: %1. Program will be terminated.").arg(e.what())));
2020-02-16 18:18:39 +01:00
exit(V_EX_SOFTWARE);
}
return false;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief IsAppInGUIMode little hack that allow to have access to application state from VAbstractApplication class.
*/
2021-05-20 16:10:43 +02:00
auto VPApplication::IsAppInGUIMode() const -> bool
2020-02-16 18:18:39 +01:00
{
return CommandLine()->IsGuiEnabled();
2020-02-16 18:18:39 +01:00
}
//---------------------------------------------------------------------------------------------------------------------
2023-06-08 12:20:20 +02:00
auto VPApplication::MainWindow() -> VPMainWindow *
2020-02-16 18:18:39 +01:00
{
Clean();
2022-08-03 13:41:19 +02:00
if (m_mainWindows.isEmpty())
2020-02-16 18:18:39 +01:00
{
2021-05-21 10:25:43 +02:00
NewMainWindow();
2020-02-16 18:18:39 +01:00
}
2022-08-03 13:41:19 +02:00
return m_mainWindows[0];
2020-02-16 18:18:39 +01:00
}
//---------------------------------------------------------------------------------------------------------------------
2021-05-21 19:51:46 +02:00
auto VPApplication::MainWindows() -> QList<VPMainWindow *>
2020-02-16 18:18:39 +01:00
{
Clean();
2023-06-08 12:20:20 +02:00
QList<VPMainWindow *> list;
2022-08-03 13:41:19 +02:00
list.reserve(m_mainWindows.size());
for (auto &w : m_mainWindows)
2020-02-16 18:18:39 +01:00
{
list.append(w);
}
return list;
}
//---------------------------------------------------------------------------------------------------------------------
2021-05-21 10:25:43 +02:00
auto VPApplication::NewMainWindow() -> VPMainWindow *
{
2021-05-21 10:25:43 +02:00
VPCommandLinePtr cmd;
VPCommandLine::ProcessInstance(cmd, {VPApplication::arguments().constFirst()});
2021-05-21 10:25:43 +02:00
return NewMainWindow(cmd);
}
2020-02-16 18:18:39 +01:00
//---------------------------------------------------------------------------------------------------------------------
2021-05-20 16:10:43 +02:00
auto VPApplication::NewMainWindow(const VPCommandLinePtr &cmd) -> VPMainWindow *
2020-02-16 18:18:39 +01:00
{
2021-05-21 10:25:43 +02:00
auto *puzzle = new VPMainWindow(cmd);
2022-08-03 13:41:19 +02:00
m_mainWindows.prepend(puzzle);
puzzle->show();
puzzle->UpdateWindowTitle();
2020-11-13 23:31:22 +01:00
puzzle->InitZoom();
2020-02-16 18:18:39 +01:00
return puzzle;
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:10:05 +02:00
void VPApplication::InitOptions()
2020-02-16 18:18:39 +01:00
{
OpenSettings();
StartLogging();
2023-07-25 13:01:41 +02:00
qCDebug(pApp, "Version: %s", qUtf8Printable(AppVersionStr()));
qCDebug(pApp, "Build revision: %s", BUILD_REVISION);
qCDebug(pApp, "%s", qUtf8Printable(buildCompatibilityString()));
qCDebug(pApp, "Built on %s at %s", __DATE__, __TIME__);
2022-08-03 13:41:19 +02:00
qCDebug(pApp, "Command-line arguments: %s", qUtf8Printable(arguments().join(QStringLiteral(", "))));
qCDebug(pApp, "Process ID: %s", qUtf8Printable(QString().setNum(applicationPid())));
2020-02-16 18:18:39 +01:00
QPixmapCache::setCacheLimit(50 * 1024 /* 50 MB */);
2023-06-08 12:20:20 +02:00
LoadTranslation(QString()); // By default the console version uses system locale
2020-02-16 18:18:39 +01:00
2021-09-16 13:18:36 +02:00
VPCommandLine::Instance();
2022-09-15 14:02:44 +02:00
CheckSystemLocale();
2023-10-04 16:02:18 +02:00
QTimer::singleShot(0, this,
[]()
{
2024-02-19 17:09:56 +01:00
QString const country = VGAnalytics::CountryCode();
if (country == "ru"_L1 || country == "by"_L1)
2023-10-04 16:02:18 +02:00
{
2023-10-31 18:55:29 +01:00
qFatal("country not detected");
2023-10-04 16:02:18 +02:00
}
});
VTheme::InitApplicationStyle();
VTheme::SetIconTheme();
VTheme::InitThemeMode();
2023-06-27 13:15:21 +02:00
2024-02-20 10:00:24 +01:00
VGAnalytics::Init(settings);
2023-10-23 15:57:22 +02:00
m_shortcutManager = new VPuzzleShortcutManager(this);
2020-02-16 18:18:39 +01:00
}
//---------------------------------------------------------------------------------------------------------------------
void VPApplication::StartLogging()
{
if (CreateLogDir())
{
BeginLogging();
ClearOldLogs();
}
}
//---------------------------------------------------------------------------------------------------------------------
auto VPApplication::LogFile() -> QTextStream *
{
return m_out.get();
}
2020-02-16 18:18:39 +01:00
//---------------------------------------------------------------------------------------------------------------------
2021-05-20 16:10:43 +02:00
auto VPApplication::TrVars() -> const VTranslateVars *
2020-02-16 18:18:39 +01:00
{
return nullptr;
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:10:05 +02:00
void VPApplication::OpenSettings()
2020-02-16 18:18:39 +01:00
{
#if defined(Q_OS_WIN)
QString const docPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
if (!docPath.isEmpty())
{
QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, docPath);
}
#endif
2020-05-23 15:44:44 +02:00
settings = new VPSettings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(),
2023-06-22 17:30:43 +02:00
QCoreApplication::applicationName(), this);
connect(settings, &VPSettings::SVGFontsPathChanged, this, &VPApplication::SVGFontsPathChanged);
2020-02-16 18:18:39 +01:00
}
//---------------------------------------------------------------------------------------------------------------------
2021-05-20 16:10:43 +02:00
auto VPApplication::PuzzleSettings() -> VPSettings *
2020-02-16 18:18:39 +01:00
{
SCASSERT(settings != nullptr)
2020-05-23 15:44:44 +02:00
return qobject_cast<VPSettings *>(settings);
2020-02-16 18:18:39 +01:00
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:10:05 +02:00
void VPApplication::ParseCommandLine(const SocketConnection &connection, const QStringList &arguments)
2020-02-16 18:18:39 +01:00
{
2020-05-23 14:43:57 +02:00
VPCommandLinePtr cmd;
VPCommandLine::ProcessInstance(cmd, arguments);
2020-02-16 18:18:39 +01:00
if (cmd->IsGuiEnabled() && connection == SocketConnection::Client)
2020-02-16 18:18:39 +01:00
{
const QString serverName = QCoreApplication::applicationName();
QLocalSocket socket;
socket.connectToServer(serverName);
if (socket.waitForConnected(1000))
{
qCDebug(pApp, "Connected to the server '%s'", qUtf8Printable(serverName));
2020-02-16 18:18:39 +01:00
QTextStream stream(&socket);
2022-08-03 13:41:19 +02:00
stream << arguments.join(QStringLiteral(";;"));
2020-02-16 18:18:39 +01:00
stream.flush();
socket.waitForBytesWritten();
2021-07-29 16:11:18 +02:00
QCoreApplication::exit(V_EX_OK);
2020-02-16 18:18:39 +01:00
return;
}
qCDebug(pApp, "Can't establish connection to the server '%s'", qUtf8Printable(serverName));
2022-08-03 13:41:19 +02:00
StartLocalServer(serverName);
2020-02-16 18:18:39 +01:00
LoadTranslation(PuzzleSettings()->GetLocale());
}
ProcessArguments(cmd);
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:43:57 +02:00
void VPApplication::ProcessArguments(const VPCommandLinePtr &cmd)
{
const QStringList rawLayouts = cmd->OptionRawLayouts();
const QStringList args = cmd->OptionFileNames();
2022-08-08 15:14:46 +02:00
if (bool const success = !args.isEmpty() ? StartWithFiles(cmd, rawLayouts) : SingleStart(cmd, rawLayouts);
not success)
2022-08-08 15:14:46 +02:00
{
return;
}
2020-02-16 18:18:39 +01:00
if (not cmd->IsGuiEnabled())
2020-02-16 18:18:39 +01:00
{
2021-07-29 16:11:18 +02:00
QCoreApplication::exit(V_EX_OK); // close program after processing in console mode
2020-02-16 18:18:39 +01:00
}
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:10:05 +02:00
void VPApplication::ProcessCMD()
2020-02-16 18:18:39 +01:00
{
ParseCommandLine(SocketConnection::Client, arguments());
if (IsAppInGUIMode() && Settings()->IsAutomaticallyCheckUpdates())
{
// Set feed URL before doing anything else
FvUpdater::sharedUpdater()->SetFeedURL(FvUpdater::CurrentFeedURL());
// Check for updates automatically
FvUpdater::sharedUpdater()->CheckForUpdatesSilent();
}
2020-02-16 18:18:39 +01:00
}
//---------------------------------------------------------------------------------------------------------------------
2021-05-20 16:10:43 +02:00
auto VPApplication::event(QEvent *e) -> bool
2020-02-16 18:18:39 +01:00
{
2023-06-08 12:20:20 +02:00
switch (e->type())
2020-02-16 18:18:39 +01:00
{
// In Mac OS X the QFileOpenEvent event is generated when user perform "Open With" from Finder (this event is
// Mac specific).
case QEvent::FileOpen:
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
auto *fileOpenEvent = static_cast<QFileOpenEvent *>(e);
if (const QString macFileOpen = fileOpenEvent->file(); not macFileOpen.isEmpty())
2020-02-16 18:18:39 +01:00
{
if (VPMainWindow *mw = MainWindow(); mw != nullptr)
2020-02-16 18:18:39 +01:00
{
2023-06-08 12:20:20 +02:00
mw->LoadFile(macFileOpen); // open file in existing window
2020-02-16 18:18:39 +01:00
}
return true;
}
break;
}
#if defined(Q_OS_MAC)
case QEvent::ApplicationStateChange:
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
if (static_cast<QApplicationStateChangeEvent *>(e)->applicationState() == Qt::ApplicationActive)
2020-02-16 18:18:39 +01:00
{
Clean();
if (VPMainWindow *mw = MainWindow(); mw && not mw->isMinimized())
{
mw->show();
}
2023-11-09 17:41:59 +01:00
return true;
2020-02-16 18:18:39 +01:00
}
2023-11-09 17:41:59 +01:00
break;
2023-06-08 12:20:20 +02:00
#endif // defined(Q_OS_MAC)
2020-02-16 18:18:39 +01:00
default:
return VAbstractApplication::event(e);
}
return VAbstractApplication::event(e);
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:10:05 +02:00
void VPApplication::InitTrVars()
2020-02-16 18:18:39 +01:00
{
// do nothing
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:10:05 +02:00
void VPApplication::AboutToQuit()
2020-02-16 18:18:39 +01:00
{
// If try to use the method QApplication::exit program can't sync settings and show warning about QApplication
// instance. Solution is to call sync() before quit.
// Connect this slot with VApplication::aboutToQuit.
Settings()->sync();
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:10:05 +02:00
void VPApplication::NewLocalSocketConnection()
2020-02-16 18:18:39 +01:00
{
2024-02-19 17:09:56 +01:00
QScopedPointer<QLocalSocket> const socket(m_localServer->nextPendingConnection());
if (socket.isNull())
2020-02-16 18:18:39 +01:00
{
return;
}
socket->waitForReadyRead(1000);
QTextStream stream(socket.data());
if (const QString arg = stream.readAll(); not arg.isEmpty())
2020-02-16 18:18:39 +01:00
{
2022-08-03 13:41:19 +02:00
ParseCommandLine(SocketConnection::Server, arg.split(QStringLiteral(";;")));
2020-02-16 18:18:39 +01:00
}
MainWindow()->raise();
MainWindow()->activateWindow();
}
2021-08-21 15:13:56 +02:00
//---------------------------------------------------------------------------------------------------------------------
auto VPApplication::PreferencesDialog() const -> QSharedPointer<DialogPuzzlePreferences>
{
return m_preferencesDialog;
}
//---------------------------------------------------------------------------------------------------------------------
void VPApplication::SetPreferencesDialog(const QSharedPointer<DialogPuzzlePreferences> &newPreferencesDialog)
{
m_preferencesDialog = newPreferencesDialog;
}
2020-02-16 18:18:39 +01:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:10:05 +02:00
void VPApplication::Clean()
2020-02-16 18:18:39 +01:00
{
// cleanup any deleted main windows first
for (vsizetype i = m_mainWindows.count() - 1; i >= 0; --i)
2020-02-16 18:18:39 +01:00
{
2022-08-03 13:41:19 +02:00
if (m_mainWindows.at(i).isNull())
2020-02-16 18:18:39 +01:00
{
2022-08-03 13:41:19 +02:00
m_mainWindows.removeAt(i);
2020-02-16 18:18:39 +01:00
}
}
}
2022-08-03 13:41:19 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPApplication::StartLocalServer(const QString &serverName)
{
m_localServer = new QLocalServer(this);
connect(m_localServer, &QLocalServer::newConnection, this, &VPApplication::NewLocalSocketConnection);
if (not m_localServer->listen(serverName))
{
2023-06-08 12:20:20 +02:00
qCDebug(pApp, "Can't begin to listen for incoming connections on name '%s'", qUtf8Printable(serverName));
2022-08-03 13:41:19 +02:00
if (m_localServer->serverError() == QAbstractSocket::AddressInUseError)
{
QLocalServer::removeServer(serverName);
if (not m_localServer->listen(serverName))
{
2023-06-08 12:20:20 +02:00
qCWarning(
pApp, "%s",
qUtf8Printable(tr("Can't begin to listen for incoming connections on name '%1'").arg(serverName)));
2022-08-03 13:41:19 +02:00
}
}
}
}
//---------------------------------------------------------------------------------------------------------------------
2022-08-08 15:14:46 +02:00
auto VPApplication::StartWithFiles(const VPCommandLinePtr &cmd, const QStringList &rawLayouts) -> bool
2022-08-03 13:41:19 +02:00
{
const QStringList args = cmd->OptionFileNames();
2022-08-08 15:14:46 +02:00
if (args.count() <= 0)
2022-08-03 13:41:19 +02:00
{
2022-08-08 15:14:46 +02:00
QCoreApplication::exit(V_EX_DATAERR);
return false;
}
2022-08-03 13:41:19 +02:00
2022-08-08 15:14:46 +02:00
if (not cmd->IsGuiEnabled() && args.count() > 1)
{
qCCritical(pApp, "%s\n", qPrintable(tr("Export mode doesn't support opening several files.")));
cmd.get()->parser.showHelp(V_EX_USAGE);
}
2022-08-03 13:41:19 +02:00
2022-08-08 15:14:46 +02:00
if (args.count() > 1 && not rawLayouts.isEmpty())
{
qCCritical(pApp, "%s\n",
qPrintable(tr("Import raw layout data does not support opening several layout files.")));
cmd.get()->parser.showHelp(V_EX_USAGE);
}
2022-08-12 17:50:13 +02:00
for (const auto &arg : args) // NOLINT(readability-use-anyofallof)
2022-08-08 15:14:46 +02:00
{
NewMainWindow(cmd);
if (not MainWindow()->LoadFile(arg))
2022-08-03 13:41:19 +02:00
{
2022-08-08 15:14:46 +02:00
if (not cmd->IsGuiEnabled())
2022-08-03 13:41:19 +02:00
{
delete MainWindow();
2022-08-08 15:14:46 +02:00
return false; // process only one input file
2022-08-03 13:41:19 +02:00
}
2022-08-08 15:14:46 +02:00
delete MainWindow();
if (not rawLayouts.isEmpty())
{
// Maybe already opened
QList<VPMainWindow *> list = VPApplication::VApp()->MainWindows();
auto w = std::find_if(list.begin(), list.end(),
[arg](VPMainWindow *window) { return window->CurrentFile() == arg; });
if (w != list.end())
{
(*w)->activateWindow();
(*w)->ImportRawLayouts(rawLayouts);
}
}
2022-08-08 15:14:46 +02:00
continue;
}
2022-08-03 13:41:19 +02:00
2022-08-08 15:14:46 +02:00
if (not rawLayouts.isEmpty())
{
MainWindow()->ImportRawLayouts(rawLayouts);
2022-08-03 13:41:19 +02:00
}
}
2022-08-08 15:14:46 +02:00
return true;
2022-08-03 13:41:19 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2022-08-08 15:14:46 +02:00
auto VPApplication::SingleStart(const VPCommandLinePtr &cmd, const QStringList &rawLayouts) -> bool
2022-08-03 13:41:19 +02:00
{
NewMainWindow(cmd);
if (not rawLayouts.isEmpty())
{
MainWindow()->ImportRawLayouts(rawLayouts);
}
2022-08-08 15:14:46 +02:00
return true;
2022-08-03 13:41:19 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
auto VPApplication::LogPath() -> QString
{
// Keep in sync with VCrashPaths::GetAttachmentPath
return QStringLiteral("%1/puzzle-pid%2.log").arg(LogDirPath()).arg(applicationPid());
}
//---------------------------------------------------------------------------------------------------------------------
void VPApplication::BeginLogging()
{
VlpCreateLock(m_lockLog, LogPath(), []() { return new QFile(LogPath()); });
if (m_lockLog->IsLocked())
{
if (m_lockLog->GetProtected()->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
{
m_out.reset(new QTextStream(m_lockLog->GetProtected().data()));
qInstallMessageHandler(noisyFailureMsgHandler);
qCDebug(pApp, "Log file %s was locked.", qUtf8Printable(LogPath()));
}
else
{
qCDebug(pApp, "Error opening log file \'%s\'. All debug output redirected to console.",
qUtf8Printable(LogPath()));
}
}
else
{
qCDebug(pApp, "Failed to lock %s", qUtf8Printable(LogPath()));
}
}
2021-05-20 16:10:43 +02:00
//---------------------------------------------------------------------------------------------------------------------
auto VPApplication::CommandLine() -> VPCommandLinePtr
{
2020-05-23 14:43:57 +02:00
return VPCommandLine::instance;
}
2021-05-20 16:10:43 +02:00
//---------------------------------------------------------------------------------------------------------------------
2022-08-03 13:41:19 +02:00
auto VPApplication::VApp() -> VPApplication *
2021-05-20 16:10:43 +02:00
{
2023-06-08 12:20:20 +02:00
return qobject_cast<VPApplication *>(QCoreApplication::instance());
2021-05-20 16:10:43 +02:00
}