main: don't ignore config value for single instance mode. allow override using cli param

This commit is contained in:
Albert S. 2020-10-10 21:53:24 +02:00
parent 6813e54e9b
commit f424f7632f

View File

@ -31,6 +31,7 @@ int main(int argc, char *argv[])
QApplication app(argc, argv); QApplication app(argc, argv);
QString configDirectoryPath; QString configDirectoryPath;
QDir dir; QDir dir;
bool newInstanceRequested = false;
if(argc >= 2) if(argc >= 2)
{ {
QCommandLineParser parser; QCommandLineParser parser;
@ -42,6 +43,8 @@ int main(int argc, char *argv[])
parser.addHelpOption(); parser.addHelpOption();
parser.process(app.arguments()); parser.process(app.arguments());
configDirectoryPath = parser.value("config"); configDirectoryPath = parser.value("config");
newInstanceRequested = parser.isSet("new-instance");
if(!configDirectoryPath.isEmpty() && !dir.exists(configDirectoryPath)) if(!configDirectoryPath.isEmpty() && !dir.exists(configDirectoryPath))
{ {
QMessageBox::warning(nullptr, "Directory not found", configDirectoryPath + " was not found"); QMessageBox::warning(nullptr, "Directory not found", configDirectoryPath + " was not found");
@ -70,9 +73,13 @@ int main(int argc, char *argv[])
SettingsProvider settingsProvider{settings}; SettingsProvider settingsProvider{settings};
EntryProvider entryProvider(settingsProvider.userEntriesPaths(), settingsProvider.systemApplicationsEntriesPaths()); EntryProvider entryProvider(settingsProvider.userEntriesPaths(), settingsProvider.systemApplicationsEntriesPaths());
SingleInstanceServer *server = nullptr;
bool singleInstanceMode = !newInstanceRequested && settingsProvider.singleInstanceMode();
if(singleInstanceMode)
{
QLocalSocket localSocket; QLocalSocket localSocket;
localSocket.connectToServer(settingsProvider.socketPath()); localSocket.connectToServer(settingsProvider.socketPath());
SingleInstanceServer server;
if(localSocket.isOpen() && localSocket.isWritable()) if(localSocket.isOpen() && localSocket.isWritable())
{ {
QDataStream stream(&localSocket); QDataStream stream(&localSocket);
@ -82,14 +89,19 @@ int main(int argc, char *argv[])
localSocket.disconnectFromServer(); localSocket.disconnectFromServer();
return 0; return 0;
} }
else
{ server = new SingleInstanceServer();
if(!server.listen(settingsProvider.socketPath())) if(!server->listen(settingsProvider.socketPath()))
{ {
qDebug() << "Failed to listen on socket!"; qDebug() << "Failed to listen on socket!";
return 1;
} }
}
Window *w = new Window{entryProvider, settingsProvider}; Window *w = new Window{entryProvider, settingsProvider};
QObject::connect(&server, &SingleInstanceServer::receivedMaximizationRequest, [&w] { if(singleInstanceMode && server != nullptr)
{
QObject::connect(server, &SingleInstanceServer::receivedMaximizationRequest, [&w] {
if(w != nullptr) if(w != nullptr)
{ {
qInfo() << "maximizing as requested by other instance"; qInfo() << "maximizing as requested by other instance";
@ -99,9 +111,10 @@ int main(int argc, char *argv[])
w->focusInput(); w->focusInput();
} }
}); });
}
w->showMaximized(); w->showMaximized();
w->focusInput(); w->focusInput();
}
return app.exec(); return app.exec();
} }