2016-09-24 16:19:23 +02:00
|
|
|
#include "./singleinstance.h"
|
|
|
|
|
|
|
|
#include <c++utilities/conversion/binaryconversion.h>
|
2018-04-02 20:23:54 +02:00
|
|
|
#include <c++utilities/io/ansiescapecodes.h>
|
2016-09-24 16:19:23 +02:00
|
|
|
|
2017-05-01 03:34:43 +02:00
|
|
|
#include <QCoreApplication>
|
2016-09-24 16:19:23 +02:00
|
|
|
#include <QLocalServer>
|
|
|
|
#include <QLocalSocket>
|
|
|
|
#include <QStringBuilder>
|
|
|
|
|
|
|
|
#include <iostream>
|
2017-02-11 02:42:45 +01:00
|
|
|
#include <memory>
|
2016-09-24 16:19:23 +02:00
|
|
|
|
|
|
|
using namespace std;
|
2019-06-10 22:48:26 +02:00
|
|
|
using namespace CppUtilities;
|
|
|
|
using namespace CppUtilities::EscapeCodes;
|
2016-09-24 16:19:23 +02:00
|
|
|
|
|
|
|
namespace QtGui {
|
|
|
|
|
2020-10-19 19:03:42 +02:00
|
|
|
SingleInstance::SingleInstance(int argc, const char *const *argv, bool newInstance, QObject *parent)
|
2017-05-01 03:34:43 +02:00
|
|
|
: QObject(parent)
|
|
|
|
, m_server(nullptr)
|
2016-09-24 16:19:23 +02:00
|
|
|
{
|
2020-10-19 19:03:42 +02:00
|
|
|
if (newInstance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:04:18 +02:00
|
|
|
// check for running instance
|
2016-09-24 16:19:23 +02:00
|
|
|
const QString appId(QCoreApplication::applicationName() % QStringLiteral(" by ") % QCoreApplication::organizationName());
|
2020-10-19 19:04:18 +02:00
|
|
|
passArgsToRunningInstance(argc, argv, appId);
|
2016-09-24 16:19:23 +02:00
|
|
|
|
2020-10-19 19:04:18 +02:00
|
|
|
// no previous instance running
|
|
|
|
// -> however, previous server instance might not have been cleaned up dute to crash
|
|
|
|
QLocalServer::removeServer(appId);
|
|
|
|
// -> start server
|
|
|
|
m_server = new QLocalServer(this);
|
|
|
|
connect(m_server, &QLocalServer::newConnection, this, &SingleInstance::handleNewConnection);
|
|
|
|
if (!m_server->listen(appId)) {
|
|
|
|
cerr << Phrases::Error << "Unable to launch as single instance application" << Phrases::EndFlush;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SingleInstance::passArgsToRunningInstance(int argc, const char *const *argv, const QString &appId)
|
|
|
|
{
|
2016-09-24 16:19:23 +02:00
|
|
|
QLocalSocket socket;
|
|
|
|
socket.connectToServer(appId, QLocalSocket::ReadWrite);
|
2017-05-01 03:34:43 +02:00
|
|
|
if (socket.waitForConnected(1000)) {
|
2018-04-02 20:23:54 +02:00
|
|
|
cerr << Phrases::Info << "Application already running, sending args to previous instance" << Phrases::EndFlush;
|
2017-05-01 03:34:43 +02:00
|
|
|
if (argc >= 0 && argc <= 0xFFFF) {
|
2016-09-24 16:19:23 +02:00
|
|
|
char buffer[2];
|
2019-03-13 19:12:23 +01:00
|
|
|
BE::getBytes(static_cast<std::uint16_t>(argc), buffer);
|
2016-09-24 16:19:23 +02:00
|
|
|
socket.write(buffer, 2);
|
|
|
|
*buffer = '\0';
|
2017-05-01 03:34:43 +02:00
|
|
|
for (const char *const *end = argv + argc; argv != end; ++argv) {
|
2016-09-24 16:19:23 +02:00
|
|
|
socket.write(*argv);
|
|
|
|
socket.write(buffer, 1);
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-02 20:23:54 +02:00
|
|
|
cerr << Phrases::Error << "Unable to pass the specified number of arguments" << Phrases::EndFlush;
|
2016-09-24 16:19:23 +02:00
|
|
|
}
|
|
|
|
socket.flush();
|
|
|
|
socket.close();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SingleInstance::handleNewConnection()
|
|
|
|
{
|
2018-04-02 20:23:54 +02:00
|
|
|
const QLocalSocket *const socket = m_server->nextPendingConnection();
|
2016-09-24 16:19:23 +02:00
|
|
|
connect(socket, &QLocalSocket::readChannelFinished, this, &SingleInstance::readArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SingleInstance::readArgs()
|
|
|
|
{
|
2018-04-02 20:23:54 +02:00
|
|
|
auto *const socket = static_cast<QLocalSocket *>(sender());
|
2016-09-24 16:19:23 +02:00
|
|
|
|
|
|
|
// check arg data size
|
|
|
|
const auto argDataSize = socket->bytesAvailable();
|
2017-05-01 03:34:43 +02:00
|
|
|
if (argDataSize < 2 && argDataSize > (1024 * 1024)) {
|
2018-04-02 20:23:54 +02:00
|
|
|
cerr << Phrases::Error << "Another application instance sent invalid argument data." << Phrases::EndFlush;
|
2016-09-24 16:19:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read arg data
|
|
|
|
auto argData = make_unique<char[]>(static_cast<size_t>(argDataSize));
|
|
|
|
socket->read(argData.get(), argDataSize);
|
|
|
|
socket->close();
|
|
|
|
socket->deleteLater();
|
|
|
|
|
2016-09-25 20:54:09 +02:00
|
|
|
// reconstruct argc and argv array
|
2018-04-02 20:23:54 +02:00
|
|
|
const auto argc = BE::toUInt16(argData.get());
|
2016-09-24 16:19:23 +02:00
|
|
|
vector<const char *> args;
|
|
|
|
args.reserve(argc + 1);
|
2017-05-01 03:34:43 +02:00
|
|
|
for (const char *argv = argData.get() + 2, *end = argData.get() + argDataSize, *i = argv; i != end && *argv;) {
|
|
|
|
if (!*i) {
|
2016-09-24 16:19:23 +02:00
|
|
|
args.push_back(argv);
|
|
|
|
argv = ++i;
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args.push_back(nullptr);
|
|
|
|
|
2016-09-25 20:54:09 +02:00
|
|
|
emit newInstance(static_cast<int>(args.size() - 1), args.data());
|
2016-09-24 16:19:23 +02:00
|
|
|
}
|
2020-10-19 19:04:18 +02:00
|
|
|
|
2017-09-17 21:48:15 +02:00
|
|
|
} // namespace QtGui
|