From 883b71e8a821efa921c26daf1b8285a52199bc3a Mon Sep 17 00:00:00 2001 From: Albert S Date: Thu, 8 Oct 2020 23:08:16 +0200 Subject: [PATCH 1/5] Parse argv using QCommandLineParser Fixes: #17 --- main.cpp | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/main.cpp b/main.cpp index 691ea60..62b728c 100644 --- a/main.cpp +++ b/main.cpp @@ -14,6 +14,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include +#include #include #include #include @@ -32,7 +33,15 @@ int main(int argc, char *argv[]) QDir dir; if(argc >= 2) { - configDirectoryPath = QCoreApplication::arguments().at(1); + QCommandLineParser parser; + parser.addOptions({ + {"new-instance", "Launch a new instance, ignoring any running ones"}, + {"kill-instances", "Kill all running instances"}, + {"config", "Use supplied config dir instead of default"}, + }); + parser.addHelpOption(); + parser.process(app.arguments()); + configDirectoryPath = parser.value("config"); if(!dir.exists(configDirectoryPath)) { QMessageBox::warning(nullptr, "Directory not found", configDirectoryPath + " was not found"); @@ -43,30 +52,30 @@ int main(int argc, char *argv[]) { configDirectoryPath = QDir::homePath() + "/.config/qsrun/"; } - qRegisterMetaType >("QVector"); - + qRegisterMetaType>("QVector"); if(!dir.exists(configDirectoryPath)) { if(!dir.mkdir(configDirectoryPath)) { - QMessageBox::warning(nullptr, "Failed to create dir", configDirectoryPath + " was not found and could not be created!"); + QMessageBox::warning(nullptr, "Failed to create dir", + configDirectoryPath + " was not found and could not be created!"); return 1; } } QSettings settings(configDirectoryPath + "qsrun.config", QSettings::NativeFormat); - SettingsProvider settingsProvider { settings }; + SettingsProvider settingsProvider{settings}; EntryProvider entryProvider(settingsProvider.userEntriesPaths(), settingsProvider.systemApplicationsEntriesPaths()); - //TODO if setting single instance mode + // TODO if setting single instance mode QLocalSocket localSocket; localSocket.connectToServer("/tmp/qsrun.socket"); SingleInstanceServer server; if(localSocket.isOpen() && localSocket.isWritable()) { QDataStream stream(&localSocket); - stream << (int)0x01; //maximize + stream << (int)0x01; // maximize localSocket.flush(); localSocket.waitForBytesWritten(); localSocket.disconnectFromServer(); @@ -78,8 +87,8 @@ int main(int argc, char *argv[]) { qDebug() << "Failed to listen on socket!"; } - Window *w = new Window { entryProvider, settingsProvider }; - QObject::connect(&server, &SingleInstanceServer::receivedMaximizationRequest, [&w]{ + Window *w = new Window{entryProvider, settingsProvider}; + QObject::connect(&server, &SingleInstanceServer::receivedMaximizationRequest, [&w] { if(w != nullptr) { qInfo() << "maximizing as requested by other instance"; @@ -91,10 +100,7 @@ int main(int argc, char *argv[]) }); w->showMaximized(); w->focusInput(); - } - - return app.exec(); } -- 2.47.1 From 3fbbc7d65372c4bda72bbb54c6d4356f34f8d494 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sat, 10 Oct 2020 20:55:30 +0200 Subject: [PATCH 2/5] main: allow overriding config dir from commandline --- main.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 62b728c..14fcf46 100644 --- a/main.cpp +++ b/main.cpp @@ -42,16 +42,17 @@ int main(int argc, char *argv[]) parser.addHelpOption(); parser.process(app.arguments()); configDirectoryPath = parser.value("config"); - if(!dir.exists(configDirectoryPath)) + if(!configDirectoryPath.isEmpty() && !dir.exists(configDirectoryPath)) { QMessageBox::warning(nullptr, "Directory not found", configDirectoryPath + " was not found"); return 1; } } - else + if(configDirectoryPath.isEmpty()) { configDirectoryPath = QDir::homePath() + "/.config/qsrun/"; } + qRegisterMetaType>("QVector"); if(!dir.exists(configDirectoryPath)) -- 2.47.1 From 6813e54e9b3be3bc7ee7ad973921bf82fa1ca2e0 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sat, 10 Oct 2020 21:10:53 +0200 Subject: [PATCH 3/5] SettingsProvider/main: Read single instance socket path from config --- main.cpp | 6 +++--- settingsprovider.cpp | 5 +++++ settingsprovider.h | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index 14fcf46..fbcb5a2 100644 --- a/main.cpp +++ b/main.cpp @@ -69,9 +69,9 @@ int main(int argc, char *argv[]) SettingsProvider settingsProvider{settings}; EntryProvider entryProvider(settingsProvider.userEntriesPaths(), settingsProvider.systemApplicationsEntriesPaths()); - // TODO if setting single instance mode + QLocalSocket localSocket; - localSocket.connectToServer("/tmp/qsrun.socket"); + localSocket.connectToServer(settingsProvider.socketPath()); SingleInstanceServer server; if(localSocket.isOpen() && localSocket.isWritable()) { @@ -84,7 +84,7 @@ int main(int argc, char *argv[]) } else { - if(!server.listen("/tmp/qsrun.socket")) + if(!server.listen(settingsProvider.socketPath())) { qDebug() << "Failed to listen on socket!"; } diff --git a/settingsprovider.cpp b/settingsprovider.cpp index 1b7368d..1089d0c 100644 --- a/settingsprovider.cpp +++ b/settingsprovider.cpp @@ -33,3 +33,8 @@ QString SettingsProvider::getTerminalCommand() const { return settings->value("terminal", "/usr/bin/x-terminal-emulator -e %c").toString(); } + +QString SettingsProvider::socketPath() const +{ + return settings->value("singleInstanceSocket", "/tmp/qsrun").toString(); +} diff --git a/settingsprovider.h b/settingsprovider.h index 48e3ea5..99ad897 100644 --- a/settingsprovider.h +++ b/settingsprovider.h @@ -16,6 +16,7 @@ class SettingsProvider virtual int getMaxCols() const; virtual bool singleInstanceMode() const; QString getTerminalCommand() const; + QString socketPath() const; }; #endif // SETTINGSPROVIDER_H -- 2.47.1 From f424f7632f9df3dd55a4a9f7a57ed88a6b6120e5 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sat, 10 Oct 2020 21:53:24 +0200 Subject: [PATCH 4/5] main: don't ignore config value for single instance mode. allow override using cli param --- main.cpp | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/main.cpp b/main.cpp index fbcb5a2..a3f6751 100644 --- a/main.cpp +++ b/main.cpp @@ -31,6 +31,7 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); QString configDirectoryPath; QDir dir; + bool newInstanceRequested = false; if(argc >= 2) { QCommandLineParser parser; @@ -42,6 +43,8 @@ int main(int argc, char *argv[]) parser.addHelpOption(); parser.process(app.arguments()); configDirectoryPath = parser.value("config"); + newInstanceRequested = parser.isSet("new-instance"); + if(!configDirectoryPath.isEmpty() && !dir.exists(configDirectoryPath)) { QMessageBox::warning(nullptr, "Directory not found", configDirectoryPath + " was not found"); @@ -70,26 +73,35 @@ int main(int argc, char *argv[]) SettingsProvider settingsProvider{settings}; EntryProvider entryProvider(settingsProvider.userEntriesPaths(), settingsProvider.systemApplicationsEntriesPaths()); - QLocalSocket localSocket; - localSocket.connectToServer(settingsProvider.socketPath()); - SingleInstanceServer server; - if(localSocket.isOpen() && localSocket.isWritable()) + SingleInstanceServer *server = nullptr; + + bool singleInstanceMode = !newInstanceRequested && settingsProvider.singleInstanceMode(); + if(singleInstanceMode) { - QDataStream stream(&localSocket); - stream << (int)0x01; // maximize - localSocket.flush(); - localSocket.waitForBytesWritten(); - localSocket.disconnectFromServer(); - return 0; - } - else - { - if(!server.listen(settingsProvider.socketPath())) + QLocalSocket localSocket; + localSocket.connectToServer(settingsProvider.socketPath()); + if(localSocket.isOpen() && localSocket.isWritable()) + { + QDataStream stream(&localSocket); + stream << (int)0x01; // maximize + localSocket.flush(); + localSocket.waitForBytesWritten(); + localSocket.disconnectFromServer(); + return 0; + } + + server = new SingleInstanceServer(); + if(!server->listen(settingsProvider.socketPath())) { qDebug() << "Failed to listen on socket!"; + return 1; } - Window *w = new Window{entryProvider, settingsProvider}; - QObject::connect(&server, &SingleInstanceServer::receivedMaximizationRequest, [&w] { + } + + Window *w = new Window{entryProvider, settingsProvider}; + if(singleInstanceMode && server != nullptr) + { + QObject::connect(server, &SingleInstanceServer::receivedMaximizationRequest, [&w] { if(w != nullptr) { qInfo() << "maximizing as requested by other instance"; @@ -99,9 +111,10 @@ int main(int argc, char *argv[]) w->focusInput(); } }); - w->showMaximized(); - w->focusInput(); } + w->showMaximized(); + w->focusInput(); + return app.exec(); } -- 2.47.1 From 781b3f4d693b7980bee7bb8e81eaaf840e8c2713 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sat, 10 Oct 2020 21:55:27 +0200 Subject: [PATCH 5/5] fixup! Parse argv using QCommandLineParser --- main.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/main.cpp b/main.cpp index a3f6751..0e7f8f6 100644 --- a/main.cpp +++ b/main.cpp @@ -37,7 +37,6 @@ int main(int argc, char *argv[]) QCommandLineParser parser; parser.addOptions({ {"new-instance", "Launch a new instance, ignoring any running ones"}, - {"kill-instances", "Kill all running instances"}, {"config", "Use supplied config dir instead of default"}, }); parser.addHelpOption(); -- 2.47.1