1
0
qsrun/main.cpp

101 Zeilen
2.9 KiB
C++

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>
#include <QFuture>
#include <QFutureWatcher>
#include <QtConcurrent/QtConcurrentRun>
#include <QSettings>
#include <QLocalSocket>
#include <QDir>
#include "settingsprovider.h"
#include "entryprovider.h"
2018-01-03 09:52:05 +01:00
#include "window.h"
#include "singleinstanceserver.h"
2018-01-03 09:52:05 +01:00
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QString configDirectoryPath;
QDir dir;
2018-01-03 09:52:05 +01:00
if(argc >= 2)
{
configDirectoryPath = QCoreApplication::arguments().at(1);
if(!dir.exists(configDirectoryPath))
{
QMessageBox::warning(nullptr, "Directory not found", configDirectoryPath + " was not found");
return 1;
}
2018-01-03 09:52:05 +01:00
}
else
{
2019-08-23 23:58:43 +02:00
configDirectoryPath = QDir::homePath() + "/.config/qsrun/";
2018-01-03 09:52:05 +01:00
}
qRegisterMetaType<QVector<QString> >("QVector<QString>");
2018-01-03 09:52:05 +01:00
if(!dir.exists(configDirectoryPath))
{
if(!dir.mkdir(configDirectoryPath))
{
QMessageBox::warning(nullptr, "Failed to create dir", configDirectoryPath + " was not found and could not be created!");
return 1;
}
2018-01-03 09:52:05 +01:00
}
2018-01-03 09:52:05 +01:00
QSettings settings(configDirectoryPath + "qsrun.config", QSettings::NativeFormat);
SettingsProvider settingsProvider { settings };
EntryProvider entryProvider(settingsProvider.userEntriesPaths(), settingsProvider.systemApplicationsEntriesPaths());
//TODO if setting single instance mode
QLocalSocket localSocket;
localSocket.connectToServer("/tmp/qsrun.socket");
SingleInstanceServer server;
if(localSocket.isOpen() && localSocket.isWritable())
2018-01-03 09:52:05 +01:00
{
QDataStream stream(&localSocket);
stream << (int)0x01; //maximize
localSocket.flush();
localSocket.waitForBytesWritten();
localSocket.disconnectFromServer();
return 0;
2018-01-03 09:52:05 +01:00
}
else
2018-01-03 09:52:05 +01:00
{
if(!server.listen("/tmp/qsrun.socket"))
2018-01-03 09:52:05 +01:00
{
qDebug() << "Failed to listen on socket!";
2018-01-03 09:52:05 +01:00
}
Window *w = new Window { entryProvider, settingsProvider };
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();
w->focusInput();
2019-08-24 09:19:49 +02:00
}
});
w->showMaximized();
w->focusInput();
2018-01-03 09:52:05 +01:00
}
2018-01-03 09:52:05 +01:00
return app.exec();
}