Added basic single instance functionality

This commit is contained in:
Albert S. 2019-06-03 07:42:54 +02:00
parent 67be2dbeaa
commit a1a9dfb729
3 changed files with 90 additions and 25 deletions

View File

@ -18,7 +18,10 @@
#include <QFutureWatcher> #include <QFutureWatcher>
#include <QtConcurrent/QtConcurrentRun> #include <QtConcurrent/QtConcurrentRun>
#include <QSettings> #include <QSettings>
#include <QLocalSocket>
#include "configprovider.h"
#include "window.h" #include "window.h"
#include "singleinstanceserver.h"
@ -36,44 +39,52 @@ int main(int argc, char *argv[])
configDirectoryPath = QDir::homePath() + "/.config/qsRun/"; configDirectoryPath = QDir::homePath() + "/.config/qsRun/";
} }
qRegisterMetaType<QVector<QString> >("QVector<QString>"); qRegisterMetaType<QVector<QString> >("QVector<QString>");
QDir dir; QDir dir;
if(!dir.exists(configDirectoryPath)) if(!dir.exists(configDirectoryPath))
{ {
QMessageBox::warning(nullptr, "Directory not found", configDirectoryPath + " was not found!"); QMessageBox::warning(nullptr, "Directory not found", configDirectoryPath + " was not found!");
return 1; return 1;
} }
QSettings settings(configDirectoryPath + "qsrun.config", QSettings::NativeFormat); QSettings settings(configDirectoryPath + "qsrun.config", QSettings::NativeFormat);
QVector<EntryConfig> 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}); QDataStream stream(&localSocket);
configs = reader.readConfig(); stream << (int)0x01; //maximize
localSocket.flush();
localSocket.waitForBytesWritten();
localSocket.disconnectFromServer();
return 0;
} }
catch(std::exception &e) else
{ {
std::cerr << e.what() << std::endl; if(!server.listen("/tmp/qsrun.socket"))
}
Window w(configs);
try
{
QStringList systemApplicationsPaths = settings.value("sysAppsPaths", "/usr/share/applications/").toStringList();
ConfigReader systemConfigReader(systemApplicationsPaths);
QVector<EntryConfig> systemconfigs = systemConfigReader.readConfig();
if(systemconfigs.count() > 0)
{ {
w.setSystemConfig(systemconfigs); qDebug() << "Failed to listen on socket!";
} }
} Window *w = new Window { configProvider };
catch(std::exception &e) QObject::connect(&server, &SingleInstanceServer::receivedMaximizationRequest, [&w]{
{ if(w != nullptr)
std::cerr << e.what() << std::endl; {
} qInfo() << "maximizing as requested by other instance";
w->showMaximized();
w->activateWindow();
w->raise();
}
});
w->showMaximized();
}
w.showMaximized();
return app.exec(); return app.exec();
} }

33
singleinstanceserver.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <QDataStream>
#include <QLocalSocket>
#include <QFile>
#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<QLocalSocket> socket { this->server.nextPendingConnection() };
if(!socket.isNull())
{
socket->waitForReadyRead();
QDataStream stream ( socket.get());
int command;
stream >> command;
if(command == 0x01)
{
emit receivedMaximizationRequest();
}
}
}

21
singleinstanceserver.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef SINGLEINSTANCESERVER_H
#define SINGLEINSTANCESERVER_H
#include <QObject>
#include <QString>
#include <QLocalServer>
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