3 Commits

7 changed files with 43 additions and 84 deletions

View File

@ -13,11 +13,6 @@ EntryProvider::EntryProvider(QStringList userEntriesDirsPaths, QStringList syste
<< "%u"; << "%u";
} }
bool EntryProvider::isSavable(const EntryConfig &config) const
{
return ! config.entryPath.isEmpty() && (config.type == EntryType::USER || config.type == EntryType::INHERIT);
}
EntryConfig EntryProvider::readFromDesktopFile(const QString &path) EntryConfig EntryProvider::readFromDesktopFile(const QString &path)
{ {
EntryConfig result; EntryConfig result;
@ -25,7 +20,7 @@ EntryConfig EntryProvider::readFromDesktopFile(const QString &path)
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{ {
// TODO: better exception class // TODO: better exception class
throw std::runtime_error("Failed to open file"); throw new std::runtime_error("Failed to open file");
} }
QTextStream stream(&file); QTextStream stream(&file);
// There should be nothing preceding this group in the desktop entry file but possibly one or more comments. // There should be nothing preceding this group in the desktop entry file but possibly one or more comments.
@ -120,7 +115,7 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{ {
// TODO: better exception class // TODO: better exception class
throw std::runtime_error("Failed to open file"); throw new std::runtime_error("Failed to open file");
} }
QHash<QString, QString> map; QHash<QString, QString> map;
QTextStream stream(&file); QTextStream stream(&file);
@ -131,7 +126,7 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
int spacePos = line.indexOf(' '); int spacePos = line.indexOf(' ');
if(spacePos == -1) if(spacePos == -1)
{ {
throw ConfigFormatException("misformated line in .qsrun config file " + path.toStdString()); throw new ConfigFormatException("misformated line in .qsrun config file " + path.toStdString());
} }
QString key = line.mid(0, spacePos); QString key = line.mid(0, spacePos);
@ -139,7 +134,7 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
if(key == "" || value == "") if(key == "" || value == "")
{ {
throw ConfigFormatException("empty key or value in .qsrun config file " + path.toStdString()); throw new ConfigFormatException("empty key or value in .qsrun config file " + path.toStdString());
} }
map[key] = value; map[key] = value;
} }
@ -154,7 +149,7 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
} }
else else
{ {
throw ConfigFormatException("Error attempting to read inherited entry"); throw new ConfigFormatException("Error attempting to read inherited entry");
} }
} }
QString type = map["type"]; QString type = map["type"];
@ -162,7 +157,7 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
{ {
if(type == "system") if(type == "system")
{ {
throw ConfigFormatException(".qsrun files cannot be designated as system entries " + throw new ConfigFormatException(".qsrun files cannot be designated as system entries " +
path.toStdString()); path.toStdString());
} }
else if(type == "inherit") else if(type == "inherit")
@ -175,7 +170,7 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
} }
else else
{ {
throw ConfigFormatException("Invalid value for type provided in file: " + path.toStdString()); throw new ConfigFormatException("Invalid value for type provided in file: " + path.toStdString());
} }
} }
else else
@ -290,7 +285,7 @@ QVector<EntryConfig> EntryProvider::getSystemEntries()
void EntryProvider::saveUserEntry(const EntryConfig &config) void EntryProvider::saveUserEntry(const EntryConfig &config)
{ {
if(!isSavable(config)) if(config.type == EntryType::SYSTEM || config.entryPath.isEmpty())
{ {
throw std::runtime_error("Only user/inherited entries can be saved"); throw std::runtime_error("Only user/inherited entries can be saved");
} }
@ -309,10 +304,7 @@ void EntryProvider::saveUserEntry(const EntryConfig &config)
outStream << "row" << " " << config.row << endl; outStream << "row" << " " << config.row << endl;
outStream << "col" << " " << config.col << endl; outStream << "col" << " " << config.col << endl;
outStream << "hidden" << " " << config.hidden << endl; outStream << "hidden" << " " << config.hidden << endl;
if(!config.key.isEmpty()) outStream << "key" << " " << config.key << endl;
{
outStream << "key" << " " << config.key << endl;
}
if(config.type == EntryType::USER) if(config.type == EntryType::USER)
{ {
if(!config.name.isEmpty()) if(!config.name.isEmpty())
@ -347,7 +339,7 @@ void EntryProvider::saveUserEntry(const EntryConfig &config)
bool EntryProvider::deleteUserEntry(const EntryConfig &config) bool EntryProvider::deleteUserEntry(const EntryConfig &config)
{ {
if(!isSavable(config)) if(config.type == EntryType::SYSTEM || config.entryPath.isEmpty())
{ {
throw std::runtime_error("Only user/inherited entries can be deleted"); throw std::runtime_error("Only user/inherited entries can be deleted");
} }

View File

@ -19,8 +19,7 @@ enum EntryType
{ {
USER, USER,
INHERIT, INHERIT,
SYSTEM, SYSTEM
DYNAMIC
}; };
class EntryConfig class EntryConfig
@ -56,7 +55,6 @@ class EntryProvider
public: public:
EntryProvider(QStringList userEntriesDirsPaths, QStringList systemEntriesDirsPaths); EntryProvider(QStringList userEntriesDirsPaths, QStringList systemEntriesDirsPaths);
bool isSavable(const EntryConfig &config) const;
QVector<EntryConfig> getUserEntries(); QVector<EntryConfig> getUserEntries();
QVector<EntryConfig> getSystemEntries(); QVector<EntryConfig> getSystemEntries();
void saveUserEntry(const EntryConfig &config); void saveUserEntry(const EntryConfig &config);

View File

@ -106,7 +106,7 @@ void EntryPushButton::mousePressEvent(QMouseEvent *event)
{ {
this->userEntryMenu.exec(QCursor::pos()); this->userEntryMenu.exec(QCursor::pos());
} }
else if(this->config.type == EntryType::SYSTEM) else
{ {
this->systemEntryMenu.exec(QCursor::pos()); this->systemEntryMenu.exec(QCursor::pos());
} }
@ -116,7 +116,7 @@ void EntryPushButton::mousePressEvent(QMouseEvent *event)
void EntryPushButton::mouseMoveEvent(QMouseEvent *event) void EntryPushButton::mouseMoveEvent(QMouseEvent *event)
{ {
if(this->config.type == EntryType::SYSTEM || this->config.type == EntryType::DYNAMIC) if(this->config.type == EntryType::SYSTEM)
{ {
return; return;
} }

View File

@ -14,7 +14,6 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <QApplication> #include <QApplication>
#include <QCommandLineParser>
#include <QFuture> #include <QFuture>
#include <QFutureWatcher> #include <QFutureWatcher>
#include <QtConcurrent/QtConcurrentRun> #include <QtConcurrent/QtConcurrentRun>
@ -31,76 +30,56 @@ int main(int argc, char *argv[])
QApplication app(argc, argv); QApplication app(argc, argv);
QString configDirectoryPath; QString configDirectoryPath;
QDir dir; QDir dir;
bool newInstanceRequested = false;
if(argc >= 2) if(argc >= 2)
{ {
QCommandLineParser parser; configDirectoryPath = QCoreApplication::arguments().at(1);
parser.addOptions({ if(!dir.exists(configDirectoryPath))
{"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");
newInstanceRequested = parser.isSet("new-instance");
if(!configDirectoryPath.isEmpty() && !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;
} }
} }
if(configDirectoryPath.isEmpty()) else
{ {
configDirectoryPath = QDir::homePath() + "/.config/qsrun/"; configDirectoryPath = QDir::homePath() + "/.config/qsrun/";
} }
qRegisterMetaType<QVector<QString> >("QVector<QString>");
qRegisterMetaType<QVector<QString>>("QVector<QString>");
if(!dir.exists(configDirectoryPath)) if(!dir.exists(configDirectoryPath))
{ {
if(!dir.mkdir(configDirectoryPath)) if(!dir.mkdir(configDirectoryPath))
{ {
QMessageBox::warning(nullptr, "Failed to create dir", QMessageBox::warning(nullptr, "Failed to create dir", configDirectoryPath + " was not found and could not be created!");
configDirectoryPath + " was not found and could not be created!");
return 1; return 1;
} }
} }
QSettings settings(configDirectoryPath + "qsrun.config", QSettings::NativeFormat); QSettings settings(configDirectoryPath + "qsrun.config", QSettings::NativeFormat);
SettingsProvider settingsProvider{settings}; SettingsProvider settingsProvider { settings };
EntryProvider entryProvider(settingsProvider.userEntriesPaths(), settingsProvider.systemApplicationsEntriesPaths()); EntryProvider entryProvider(settingsProvider.userEntriesPaths(), settingsProvider.systemApplicationsEntriesPaths());
//TODO if setting single instance mode
SingleInstanceServer *server = nullptr; QLocalSocket localSocket;
localSocket.connectToServer("/tmp/qsrun.socket");
bool singleInstanceMode = !newInstanceRequested && settingsProvider.singleInstanceMode(); SingleInstanceServer server;
if(singleInstanceMode) if(localSocket.isOpen() && localSocket.isWritable())
{ {
QLocalSocket localSocket; QDataStream stream(&localSocket);
localSocket.connectToServer(settingsProvider.socketPath()); stream << (int)0x01; //maximize
if(localSocket.isOpen() && localSocket.isWritable()) localSocket.flush();
{ localSocket.waitForBytesWritten();
QDataStream stream(&localSocket); localSocket.disconnectFromServer();
stream << (int)0x01; // maximize return 0;
localSocket.flush(); }
localSocket.waitForBytesWritten(); else
localSocket.disconnectFromServer(); {
return 0; if(!server.listen("/tmp/qsrun.socket"))
}
server = new SingleInstanceServer();
if(!server->listen(settingsProvider.socketPath()))
{ {
qDebug() << "Failed to listen on socket!"; 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) if(w != nullptr)
{ {
qInfo() << "maximizing as requested by other instance"; qInfo() << "maximizing as requested by other instance";
@ -110,10 +89,12 @@ int main(int argc, char *argv[])
w->focusInput(); w->focusInput();
} }
}); });
w->showMaximized();
w->focusInput();
} }
w->showMaximized();
w->focusInput();
return app.exec(); return app.exec();
} }

View File

@ -33,8 +33,3 @@ QString SettingsProvider::getTerminalCommand() const
{ {
return settings->value("terminal", "/usr/bin/x-terminal-emulator -e %c").toString(); return settings->value("terminal", "/usr/bin/x-terminal-emulator -e %c").toString();
} }
QString SettingsProvider::socketPath() const
{
return settings->value("singleInstanceSocket", "/tmp/qsrun").toString();
}

View File

@ -16,7 +16,6 @@ class SettingsProvider
virtual int getMaxCols() const; virtual int getMaxCols() const;
virtual bool singleInstanceMode() const; virtual bool singleInstanceMode() const;
QString getTerminalCommand() const; QString getTerminalCommand() const;
QString socketPath() const;
}; };
#endif // SETTINGSPROVIDER_H #endif // SETTINGSPROVIDER_H

View File

@ -120,10 +120,10 @@ void Window::populateGrid(const QVector<EntryPushButton *> &list)
void Window::executeConfig(const EntryConfig &config) void Window::executeConfig(const EntryConfig &config)
{ {
if(config.isTerminalCommand || QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier)) if(config.isTerminalCommand)
{ {
QString cmd = settingsProvider->getTerminalCommand(); QString cmd = settingsProvider->getTerminalCommand();
cmd.replace("%c", config.command); cmd.replace("%c", config.command + " " + config.arguments.join(' '));
QProcess::startDetached(cmd); QProcess::startDetached(cmd);
} }
else else
@ -166,7 +166,6 @@ void Window::addToFavourites(const EntryConfig &config)
when we add it to the favourites. the alternative would be to reload the whole config, when we add it to the favourites. the alternative would be to reload the whole config,
but that's probably overkill. */ but that's probably overkill. */
userConfig.update(config); userConfig.update(config);
userConfig.key = "";
userEntryButtons.append(createEntryButton(userConfig)); userEntryButtons.append(createEntryButton(userConfig));
} }
@ -266,7 +265,6 @@ void Window::addPATHSuggestion(const QString &text)
e.row = 0; e.row = 0;
e.command = suggestions[0]; e.command = suggestions[0];
e.iconPath = suggestions[0]; e.iconPath = suggestions[0];
e.type = EntryType::DYNAMIC;
EntryPushButton *button = createEntryButton(e); EntryPushButton *button = createEntryButton(e);
clearGrid(); clearGrid();
grid->addWidget(button, 0, 0); grid->addWidget(button, 0, 0);
@ -344,7 +342,6 @@ void Window::lineEditTextChanged(QString text)
} }
e.command = arguments[0]; e.command = arguments[0];
e.iconPath = "utilities-terminal"; e.iconPath = "utilities-terminal";
e.type = EntryType::DYNAMIC;
EntryPushButton *button = createEntryButton(e); EntryPushButton *button = createEntryButton(e);
clearGrid(); clearGrid();
@ -384,10 +381,7 @@ void Window::keyPressEvent(QKeyEvent *event)
for(EntryPushButton *button : buttonsInGrid) for(EntryPushButton *button : buttonsInGrid)
{ {
if(!button->getEntryConfig().key.isEmpty()) button->showShortcut();
{
button->showShortcut();
}
} }
QKeySequence seq(event->key()); QKeySequence seq(event->key());