Introduce EntryType. Retire userEntry boolean

Introduce EntryType, allowing to distnguish between:

- inherietd entries: Those inherit their values from
other entries, typcally system entries. They do not
allow overwriting the value, except row/col.
- system entries: those not created by the user in any way,
usually .desktop files in /usr/share/applications/...
- user entries: those that have been created by the user.
they can however also inherit, and overwrite inherited values.

inherited are used for the "favourites" feature.
This commit is contained in:
Albert S. 2020-10-04 19:30:41 +02:00
parent 95e855f325
commit 79d15fb628
4 changed files with 103 additions and 66 deletions

View File

@ -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<EntryConfig> EntryProvider::readConfig(QStringList paths, bool userentrymode)
QVector<EntryConfig> EntryProvider::readConfig(QStringList paths)
{
QVector<EntryConfig> result;
for(QString &configPath : paths)
@ -230,10 +259,6 @@ QVector<EntryConfig> 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<EntryConfig> EntryProvider::readConfig(QStringList paths, bool userentry
QVector<EntryConfig> EntryProvider::getUserEntries()
{
return readConfig(this->userEntriesDirsPaths, true);
return readConfig(this->userEntriesDirsPaths);
}
QVector<EntryConfig> EntryProvider::getSystemEntries()
@ -255,9 +280,9 @@ QVector<EntryConfig> 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;
}

View File

@ -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<EntryConfig> readEntryFromPath(const QString &path);
QVector<EntryConfig> readConfig(QStringList paths, bool userentrymode = false);
QVector<EntryConfig> readConfig(QStringList paths);
QString resolveEntryPath(QString path);
public:

View File

@ -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;
}

View File

@ -128,11 +128,11 @@ void Window::addToFavourites(const EntryConfig &config)
{
std::pair<int, int> 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