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 5b5333585c
commit 98cff50c41
1 changed files with 31 additions and 18 deletions

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;
@ -41,6 +42,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");
@ -69,9 +72,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);
@ -81,14 +88,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";
@ -98,9 +110,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();
} }