2018-01-05 15:52:11 +01:00
|
|
|
/*
|
2019-09-30 21:58:11 +02:00
|
|
|
* Copyright (c) 2018-2019 Albert S. <mail at quitesimple dot org>
|
2018-01-05 15:52:11 +01:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
2018-01-03 09:52:05 +01:00
|
|
|
#include <QApplication>
|
2020-10-08 23:08:16 +02:00
|
|
|
#include <QCommandLineParser>
|
2018-01-03 09:52:05 +01:00
|
|
|
#include <QFuture>
|
|
|
|
#include <QFutureWatcher>
|
|
|
|
#include <QtConcurrent/QtConcurrentRun>
|
|
|
|
#include <QSettings>
|
2019-06-03 07:42:54 +02:00
|
|
|
#include <QLocalSocket>
|
2020-09-06 19:43:17 +02:00
|
|
|
#include <QDir>
|
|
|
|
#include "settingsprovider.h"
|
|
|
|
#include "entryprovider.h"
|
2018-01-03 09:52:05 +01:00
|
|
|
#include "window.h"
|
2019-06-03 07:42:54 +02:00
|
|
|
#include "singleinstanceserver.h"
|
2018-01-03 09:52:05 +01:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QApplication app(argc, argv);
|
|
|
|
QString configDirectoryPath;
|
2019-09-01 18:52:04 +02:00
|
|
|
QDir dir;
|
2020-10-10 21:53:24 +02:00
|
|
|
bool newInstanceRequested = false;
|
2018-01-03 09:52:05 +01:00
|
|
|
if(argc >= 2)
|
|
|
|
{
|
2020-10-08 23:08:16 +02:00
|
|
|
QCommandLineParser parser;
|
|
|
|
parser.addOptions({
|
|
|
|
{"new-instance", "Launch a new instance, ignoring any running ones"},
|
|
|
|
{"config", "Use supplied config dir instead of default"},
|
|
|
|
});
|
|
|
|
parser.addHelpOption();
|
|
|
|
parser.process(app.arguments());
|
|
|
|
configDirectoryPath = parser.value("config");
|
2020-10-10 21:53:24 +02:00
|
|
|
newInstanceRequested = parser.isSet("new-instance");
|
|
|
|
|
2020-10-10 20:55:30 +02:00
|
|
|
if(!configDirectoryPath.isEmpty() && !dir.exists(configDirectoryPath))
|
2019-09-01 18:52:04 +02:00
|
|
|
{
|
|
|
|
QMessageBox::warning(nullptr, "Directory not found", configDirectoryPath + " was not found");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
2020-10-10 20:55:30 +02:00
|
|
|
if(configDirectoryPath.isEmpty())
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2019-08-23 23:58:43 +02:00
|
|
|
configDirectoryPath = QDir::homePath() + "/.config/qsrun/";
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
2020-10-10 20:55:30 +02:00
|
|
|
|
2020-10-08 23:08:16 +02:00
|
|
|
qRegisterMetaType<QVector<QString>>("QVector<QString>");
|
2019-06-03 07:42:54 +02:00
|
|
|
|
2018-01-03 09:52:05 +01:00
|
|
|
if(!dir.exists(configDirectoryPath))
|
|
|
|
{
|
2019-08-24 09:41:07 +02:00
|
|
|
if(!dir.mkdir(configDirectoryPath))
|
|
|
|
{
|
2020-10-08 23:08:16 +02:00
|
|
|
QMessageBox::warning(nullptr, "Failed to create dir",
|
|
|
|
configDirectoryPath + " was not found and could not be created!");
|
2019-08-24 09:41:07 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
2019-06-03 07:42:54 +02:00
|
|
|
|
2018-01-03 09:52:05 +01:00
|
|
|
QSettings settings(configDirectoryPath + "qsrun.config", QSettings::NativeFormat);
|
|
|
|
|
2020-10-08 23:08:16 +02:00
|
|
|
SettingsProvider settingsProvider{settings};
|
2020-09-06 19:43:17 +02:00
|
|
|
EntryProvider entryProvider(settingsProvider.userEntriesPaths(), settingsProvider.systemApplicationsEntriesPaths());
|
2020-10-10 21:10:53 +02:00
|
|
|
|
2020-10-10 21:53:24 +02:00
|
|
|
SingleInstanceServer *server = nullptr;
|
|
|
|
|
|
|
|
bool singleInstanceMode = !newInstanceRequested && settingsProvider.singleInstanceMode();
|
|
|
|
if(singleInstanceMode)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2020-10-10 21:53:24 +02:00
|
|
|
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()))
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2019-06-03 07:42:54 +02:00
|
|
|
qDebug() << "Failed to listen on socket!";
|
2020-10-10 21:53:24 +02:00
|
|
|
return 1;
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
2020-10-10 21:53:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Window *w = new Window{entryProvider, settingsProvider};
|
|
|
|
if(singleInstanceMode && server != nullptr)
|
|
|
|
{
|
|
|
|
QObject::connect(server, &SingleInstanceServer::receivedMaximizationRequest, [&w] {
|
2019-08-24 09:19:49 +02:00
|
|
|
if(w != nullptr)
|
|
|
|
{
|
|
|
|
qInfo() << "maximizing as requested by other instance";
|
|
|
|
w->showMaximized();
|
|
|
|
w->activateWindow();
|
|
|
|
w->raise();
|
2019-09-01 20:42:39 +02:00
|
|
|
w->focusInput();
|
2019-08-24 09:19:49 +02:00
|
|
|
}
|
|
|
|
});
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-10 21:53:24 +02:00
|
|
|
w->showMaximized();
|
|
|
|
w->focusInput();
|
|
|
|
|
2018-01-03 09:52:05 +01:00
|
|
|
return app.exec();
|
|
|
|
}
|