From a1a9dfb729bd1327f6fc5b8b86a676358bba9947 Mon Sep 17 00:00:00 2001 From: Albert S Date: Mon, 3 Jun 2019 07:42:54 +0200 Subject: [PATCH] Added basic single instance functionality --- main.cpp | 61 ++++++++++++++++++++++++---------------- singleinstanceserver.cpp | 33 ++++++++++++++++++++++ singleinstanceserver.h | 21 ++++++++++++++ 3 files changed, 90 insertions(+), 25 deletions(-) create mode 100644 singleinstanceserver.cpp create mode 100644 singleinstanceserver.h diff --git a/main.cpp b/main.cpp index 5dfb16d..fc83d15 100644 --- a/main.cpp +++ b/main.cpp @@ -18,7 +18,10 @@ #include #include #include +#include +#include "configprovider.h" #include "window.h" +#include "singleinstanceserver.h" @@ -36,44 +39,52 @@ int main(int argc, char *argv[]) configDirectoryPath = QDir::homePath() + "/.config/qsRun/"; } qRegisterMetaType >("QVector"); - + + QDir dir; if(!dir.exists(configDirectoryPath)) { QMessageBox::warning(nullptr, "Directory not found", configDirectoryPath + " was not found!"); return 1; } + QSettings settings(configDirectoryPath + "qsrun.config", QSettings::NativeFormat); - QVector configs; - try + ConfigProvider configProvider(configDirectoryPath, settings); + //TODO if setting single instance mode + QLocalSocket localSocket; + localSocket.connectToServer("/tmp/qsrun.socket"); + SingleInstanceServer server; + if(localSocket.isOpen() && localSocket.isWritable()) { - ConfigReader reader({configDirectoryPath}); - configs = reader.readConfig(); + QDataStream stream(&localSocket); + stream << (int)0x01; //maximize + localSocket.flush(); + localSocket.waitForBytesWritten(); + localSocket.disconnectFromServer(); + return 0; } - catch(std::exception &e) + else { - std::cerr << e.what() << std::endl; - } - - Window w(configs); - try - { - QStringList systemApplicationsPaths = settings.value("sysAppsPaths", "/usr/share/applications/").toStringList(); - ConfigReader systemConfigReader(systemApplicationsPaths); - QVector systemconfigs = systemConfigReader.readConfig(); - if(systemconfigs.count() > 0) + if(!server.listen("/tmp/qsrun.socket")) { - w.setSystemConfig(systemconfigs); + qDebug() << "Failed to listen on socket!"; } - } - catch(std::exception &e) - { - std::cerr << e.what() << std::endl; - } - - + Window *w = new Window { configProvider }; + QObject::connect(&server, &SingleInstanceServer::receivedMaximizationRequest, [&w]{ + if(w != nullptr) + { + qInfo() << "maximizing as requested by other instance"; + w->showMaximized(); + w->activateWindow(); + w->raise(); + } + }); + w->showMaximized(); + + } + + - w.showMaximized(); return app.exec(); } diff --git a/singleinstanceserver.cpp b/singleinstanceserver.cpp new file mode 100644 index 0000000..6723f1e --- /dev/null +++ b/singleinstanceserver.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +#include "singleinstanceserver.h" + +SingleInstanceServer::SingleInstanceServer() +{ + connect(&this->server, &QLocalServer::newConnection, this, &SingleInstanceServer::handleNewConnection); +} + +bool SingleInstanceServer::listen(QString socketPath) +{ + QFile::remove(socketPath); + return this->server.listen(socketPath); +} +void SingleInstanceServer::handleNewConnection() +{ + QScopedPointer socket { this->server.nextPendingConnection() }; + if(!socket.isNull()) + { + socket->waitForReadyRead(); + QDataStream stream ( socket.get()); + int command; + stream >> command; + if(command == 0x01) + { + emit receivedMaximizationRequest(); + } + + } +} + diff --git a/singleinstanceserver.h b/singleinstanceserver.h new file mode 100644 index 0000000..382af0e --- /dev/null +++ b/singleinstanceserver.h @@ -0,0 +1,21 @@ +#ifndef SINGLEINSTANCESERVER_H +#define SINGLEINSTANCESERVER_H +#include +#include +#include + +class SingleInstanceServer : public QObject +{ + Q_OBJECT +private: + QLocalServer server; +private slots: + void handleNewConnection(); +public: + SingleInstanceServer(); + bool listen(QString socketPath); + signals: + void receivedMaximizationRequest(); +}; + +#endif // SINGLEINSTANCESERVER_H