diff --git a/entryprovider.cpp b/entryprovider.cpp index 1aa9bc7..6071ab1 100644 --- a/entryprovider.cpp +++ b/entryprovider.cpp @@ -80,6 +80,7 @@ EntryConfig EntryProvider::readFromDesktopFile(const QString &path) result.hidden = args == "true"; } } + result.type = EntryType::SYSTEM; return result; } @@ -125,15 +126,15 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path) } QString key = line.mid(0, spacePos); - QString value = line.mid(spacePos+1); + QString value = line.mid(spacePos + 1); - QStringList splitted = line.split(" "); if(key == "" || value == "") { throw new ConfigFormatException("empty key or value in .qsrun config file " + path.toStdString()); } map[key] = value; } + if(map.contains("inherit")) { auto entry = readEntryFromPath(map["inherit"]); @@ -147,47 +148,75 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path) throw new ConfigFormatException("Error attempting to read inherited entry"); } } - if(map.contains("arguments")) + QString type = map["type"]; + if(!type.isEmpty()) { - auto args = map["arguments"].split(' '); - QString merged; - for(QString &str : args) + if(type == "system") { - if(str.startsWith('"') && !str.endsWith("'")) - { - merged += str.mid(1) + " "; - } - else if(str.endsWith('"')) - { - str.chop(1); - merged += str; - result.arguments.append(merged); - merged = ""; - } - else if(merged != "") - { - merged += str + " "; - } - else - { - result.arguments.append(str); - } + throw new ConfigFormatException(".qsrun files cannot be designated as system entries " + + path.toStdString()); } - if(merged != "") + else if(type == "inherit") { - throw ConfigFormatException("non-closed \" in config file " + path.toStdString()); + result.type = EntryType::INHERIT; + } + else if(type == "user") + { + result.type = EntryType::USER; + } + else + { + throw new ConfigFormatException("Invalid value for type provided in file: " + path.toStdString()); } } - auto assignIfSourceNotEmpty = [](QString source, QString &val) { - if(!source.isEmpty()) + else + { + result.type = EntryType::USER; + } + if(result.type != EntryType::INHERIT) + { + if(map.contains("arguments")) { - val = source; + auto args = map["arguments"].split(' '); + QString merged; + for(QString &str : args) + { + if(str.startsWith('"') && !str.endsWith("'")) + { + merged += str.mid(1) + " "; + } + else if(str.endsWith('"')) + { + str.chop(1); + merged += str; + result.arguments.append(merged); + merged = ""; + } + else if(merged != "") + { + merged += str + " "; + } + else + { + result.arguments.append(str); + } + } + if(merged != "") + { + throw ConfigFormatException("non-closed \" in config file " + path.toStdString()); + } } - }; - assignIfSourceNotEmpty(map["key"].toLower(), result.key); - assignIfSourceNotEmpty(map["command"], result.command); - assignIfSourceNotEmpty(map["icon"], result.iconPath); - assignIfSourceNotEmpty(map["name"], result.name); + auto assignIfSourceNotEmpty = [](QString source, QString &val) { + if(!source.isEmpty()) + { + val = source; + } + }; + assignIfSourceNotEmpty(map["key"].toLower(), result.key); + assignIfSourceNotEmpty(map["command"], result.command); + assignIfSourceNotEmpty(map["icon"], result.iconPath); + assignIfSourceNotEmpty(map["name"], result.name); + } result.col = map["col"].toInt(); result.row = map["row"].toInt(); return result; @@ -216,7 +245,7 @@ QString EntryProvider::resolveEntryPath(QString path) return {}; } -QVector EntryProvider::readConfig(QStringList paths, bool userentrymode) +QVector EntryProvider::readConfig(QStringList paths) { QVector result; for(QString &configPath : paths) @@ -230,10 +259,6 @@ QVector EntryProvider::readConfig(QStringList paths, bool userentry { if(!entry->hidden) { - if(userentrymode) - { - entry->userEntry = true; - } entry->entryPath = path; result.append(*entry); } @@ -245,7 +270,7 @@ QVector EntryProvider::readConfig(QStringList paths, bool userentry QVector EntryProvider::getUserEntries() { - return readConfig(this->userEntriesDirsPaths, true); + return readConfig(this->userEntriesDirsPaths); } QVector EntryProvider::getSystemEntries() @@ -255,9 +280,9 @@ QVector EntryProvider::getSystemEntries() void EntryProvider::saveUserEntry(const EntryConfig &config) { - if(!config.userEntry || config.entryPath.isEmpty()) + if(config.type == EntryType::SYSTEM || config.entryPath.isEmpty()) { - throw std::runtime_error("Only user entries can be saved"); + throw std::runtime_error("Only user/inherited entries can be saved"); } QString transitPath = config.entryPath + ".transit"; QFile file{transitPath}; @@ -266,24 +291,29 @@ void EntryProvider::saveUserEntry(const EntryConfig &config) throw std::runtime_error("Error: Can not open file for writing"); } QTextStream outStream(&file); + outStream << "type" << " " << ((config.type == EntryType::USER) ? "user" : "inherit") << endl; if(!config.inherit.isEmpty()) { outStream << "inherit" << " " << config.inherit << endl; } - if(!config.name.isEmpty()) - { - outStream << "name" << " " << config.name << endl; - } - if(!config.command.isEmpty()) - { - outStream << "command" << " " << config.command << endl; - } - if(!config.iconPath.isEmpty()) - { - outStream << "icon" << " " << config.iconPath << endl; - } outStream << "row" << " " << config.row << endl; outStream << "col" << " " << config.col << endl; + if(config.type == EntryType::USER) + { + if(!config.name.isEmpty()) + { + outStream << "name" << " " << config.name << endl; + } + if(!config.command.isEmpty()) + { + outStream << "command" << " " << config.command << endl; + } + if(!config.iconPath.isEmpty()) + { + outStream << "icon" << " " << config.iconPath << endl; + } + } + outStream.flush(); file.close(); @@ -298,11 +328,11 @@ void EntryProvider::saveUserEntry(const EntryConfig &config) bool EntryProvider::deleteUserEntry(const EntryConfig &config) { - if(!config.userEntry || config.entryPath.isEmpty()) + if(config.type == EntryType::SYSTEM || config.entryPath.isEmpty()) { - throw std::runtime_error("Only user entries can be saved"); + throw std::runtime_error("Only user/inherited entries can be deleted"); } - QFile file { config.entryPath }; + QFile file{config.entryPath}; return file.remove(); } @@ -329,6 +359,6 @@ EntryConfig &EntryConfig::update(const EntryConfig &o) assignIfDestDefault(this->hidden, o.hidden); assignIfDestDefault(this->inherit, o.inherit); assignIfDestDefault(this->entryPath, o.entryPath); - assignIfDestDefault(this->userEntry, o.userEntry); + assignIfDestDefault(this->type, o.type); return *this; } diff --git a/entryprovider.h b/entryprovider.h index c2e6535..40f1339 100644 --- a/entryprovider.h +++ b/entryprovider.h @@ -15,10 +15,17 @@ class ConfigFormatException : public std::runtime_error } }; +enum EntryType +{ + USER, + INHERIT, + SYSTEM +}; + class EntryConfig { public: - bool userEntry = false; + EntryType type = SYSTEM; bool hidden = false; QString entryPath; QString key; @@ -42,7 +49,7 @@ class EntryProvider EntryConfig readqsrunFile(const QString &path); EntryConfig readFromDesktopFile(const QString &path); std::optional readEntryFromPath(const QString &path); - QVector readConfig(QStringList paths, bool userentrymode = false); + QVector readConfig(QStringList paths); QString resolveEntryPath(QString path); public: diff --git a/entrypushbutton.cpp b/entrypushbutton.cpp index d585b71..2f44b51 100644 --- a/entrypushbutton.cpp +++ b/entrypushbutton.cpp @@ -94,7 +94,7 @@ void EntryPushButton::mousePressEvent(QMouseEvent *event) } if(event->button() == Qt::RightButton) { - if(this->config.userEntry) + if(this->config.type == EntryType::USER || this->config.type == EntryType::INHERIT) { this->userEntryMenu.exec(QCursor::pos()); } @@ -108,7 +108,7 @@ void EntryPushButton::mousePressEvent(QMouseEvent *event) void EntryPushButton::mouseMoveEvent(QMouseEvent *event) { - if(!this->config.userEntry) + if(this->config.type == EntryType::SYSTEM) { return; } diff --git a/window.cpp b/window.cpp index b476691..a5b4c56 100644 --- a/window.cpp +++ b/window.cpp @@ -128,11 +128,11 @@ void Window::addToFavourites(const EntryConfig &config) { std::pair cell = getNextFreeCell(); EntryConfig userConfig; - userConfig.userEntry = true; + userConfig.type = EntryType::INHERIT; userConfig.row = cell.first; userConfig.col = cell.second; - userConfig.inherit = userConfig.entryPath; - QFileInfo fi{userConfig.entryPath}; + userConfig.inherit = config.entryPath; + QFileInfo fi{config.entryPath}; QString entryName = fi.completeBaseName() + ".qsrun"; userConfig.entryPath = this->settingsProvider->userEntriesPaths()[0] + "/" + entryName; try