From 693dd7a06e7c2217923b5ba8bc664cf6f135489b Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 3 Oct 2015 17:16:28 +0300 Subject: [PATCH] Fixed Tape client/server work. Disable client/server work in test mode. --HG-- branch : develop --- src/app/tape/main.cpp | 6 +-- src/app/tape/mapplication.cpp | 80 ++++++++++++++++++----------------- src/app/tape/mapplication.h | 5 ++- 3 files changed, 45 insertions(+), 46 deletions(-) diff --git a/src/app/tape/main.cpp b/src/app/tape/main.cpp index 9bdbc408b..197a0cb02 100644 --- a/src/app/tape/main.cpp +++ b/src/app/tape/main.cpp @@ -49,11 +49,7 @@ int main(int argc, char *argv[]) qt_qhash_seed.store(0); // Lock producing random attribute order in XML MApplication app(argc, argv); - if (not app.IsTheOnly()) - { - return V_EX_OK; - } app.InitOptions(); - app.ParseCommandLine(app.arguments()); + app.ParseCommandLine(SocketConnection::Client, app.arguments()); return app.IsTestMode() ? V_EX_OK : app.exec(); // single return point is always better than more } diff --git a/src/app/tape/mapplication.cpp b/src/app/tape/mapplication.cpp index d2fe0884e..d28056229 100644 --- a/src/app/tape/mapplication.cpp +++ b/src/app/tape/mapplication.cpp @@ -198,30 +198,6 @@ MApplication::MApplication(int &argc, char **argv) // Setting the Application version setApplicationVersion(APP_VERSION_STR); setWindowIcon(QIcon(":/tapeicon/64x64/logo.png")); - - const QString serverName = QCoreApplication::applicationName(); - QLocalSocket socket; - socket.connectToServer(serverName); - if (socket.waitForConnected(500)) - { - QTextStream stream(&socket); - stream << QCoreApplication::arguments().join(";;"); - stream.flush(); - socket.waitForBytesWritten(); - return; - } - - localServer = new QLocalServer(this); - connect(localServer, &QLocalServer::newConnection, this, &MApplication::NewLocalSocketConnection); - if (!localServer->listen(serverName)) - { - if (localServer->serverError() == QAbstractSocket::AddressInUseError - && QFile::exists(localServer->serverName())) - { - QFile::remove(localServer->serverName()); - localServer->listen(serverName); - } - } } //--------------------------------------------------------------------------------------------------------------------- @@ -311,12 +287,6 @@ bool MApplication::IsTestMode() const return testMode; } -//--------------------------------------------------------------------------------------------------------------------- -bool MApplication::IsTheOnly() const -{ - return (localServer != 0); -} - //--------------------------------------------------------------------------------------------------------------------- TMainWindow *MApplication::MainWindow() { @@ -347,12 +317,12 @@ void MApplication::InitOptions() OpenSettings(); - qDebug()<<"Version:"<arguments(); - qDebug()<<"Process ID:"<applicationPid(); + qCDebug(mApp, "Version: %s", qUtf8Printable(APP_VERSION_STR)); + qCDebug(mApp, "Build revision: %s", BUILD_REVISION); + qCDebug(mApp, "%s", qUtf8Printable(buildCompatibilityString())); + qCDebug(mApp, "Built on %s at %s", __DATE__, __TIME__); + qCDebug(mApp, "Command-line arguments: %s", qUtf8Printable(this->arguments().join(", "))); + qCDebug(mApp, "Process ID: %s", qUtf8Printable(QString().setNum(this->applicationPid()))); LoadTranslation(QLocale::system().name());// By default the console version uses system locale @@ -526,7 +496,7 @@ void MApplication::RetranslateTables() } //--------------------------------------------------------------------------------------------------------------------- -void MApplication::ParseCommandLine(const QStringList &arguments) +void MApplication::ParseCommandLine(const SocketConnection &connection, const QStringList &arguments) { QCommandLineParser parser; parser.setApplicationDescription(QCoreApplication::translate("main", "Valentina's measurements editor.")); @@ -630,8 +600,40 @@ void MApplication::ParseCommandLine(const QStringList &arguments) testMode = parser.isSet(testOption); - if (not testMode) + if (not testMode && connection == SocketConnection::Client) { + const QString serverName = QCoreApplication::applicationName(); + QLocalSocket socket; + socket.connectToServer(serverName); + if (socket.waitForConnected(1000)) + { + qCDebug(mApp, "Connected to the server '%s'", qUtf8Printable(serverName)); + QTextStream stream(&socket); + stream << QCoreApplication::arguments().join(";;"); + stream.flush(); + socket.waitForBytesWritten(); + std::exit(V_EX_OK); + } + + qCDebug(mApp, "Can't establish connection to the server '%s'", qUtf8Printable(serverName)); + + localServer = new QLocalServer(this); + connect(localServer, &QLocalServer::newConnection, this, &MApplication::NewLocalSocketConnection); + if (not localServer->listen(serverName)) + { + qCDebug(mApp, "Can't begin to listen for incoming connections on name '%s'", + qUtf8Printable(serverName)); + if (localServer->serverError() == QAbstractSocket::AddressInUseError) + { + QLocalServer::removeServer(serverName); + if (not localServer->listen(serverName)) + { + qCWarning(mApp, "Can't begin to listen for incoming connections on name '%s'", + qUtf8Printable(serverName)); + } + } + } + LoadTranslation(TapeSettings()->GetLocale()); } @@ -732,7 +734,7 @@ void MApplication::NewLocalSocketConnection() const QString arg = stream.readAll(); if (not arg.isEmpty()) { - ParseCommandLine(arg.split(";;")); + ParseCommandLine(SocketConnection::Server, arg.split(";;")); } delete socket; MainWindow()->raise(); diff --git a/src/app/tape/mapplication.h b/src/app/tape/mapplication.h index 8ca35bee7..69de119a3 100644 --- a/src/app/tape/mapplication.h +++ b/src/app/tape/mapplication.h @@ -44,6 +44,8 @@ class QLocalServer; #endif #define qApp (static_cast(VAbstractApplication::instance())) +enum class SocketConnection : bool {Client = false, Server = true}; + class MApplication : public VAbstractApplication { Q_OBJECT @@ -55,7 +57,6 @@ public: virtual bool notify(QObject * receiver, QEvent * event) Q_DECL_OVERRIDE; bool IsTestMode() const; - bool IsTheOnly() const; TMainWindow *MainWindow(); QList MainWindows(); @@ -77,7 +78,7 @@ public: void RetranslateGroups(); void RetranslateTables(); - void ParseCommandLine(const QStringList &arguments); + void ParseCommandLine(const SocketConnection &connection, const QStringList &arguments); public slots: TMainWindow *NewMainWindow();