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.hidden = args == "true";
} }
} }
result.type = EntryType::SYSTEM;
return result; return result;
} }
@ -125,15 +126,15 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
} }
QString key = line.mid(0, spacePos); 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 == "") if(key == "" || value == "")
{ {
throw new 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;
} }
if(map.contains("inherit")) if(map.contains("inherit"))
{ {
auto entry = readEntryFromPath(map["inherit"]); auto entry = readEntryFromPath(map["inherit"]);
@ -147,6 +148,33 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
throw new ConfigFormatException("Error attempting to read inherited entry"); throw new ConfigFormatException("Error attempting to read inherited entry");
} }
} }
QString type = map["type"];
if(!type.isEmpty())
{
if(type == "system")
{
throw new ConfigFormatException(".qsrun files cannot be designated as system entries " +
path.toStdString());
}
else if(type == "inherit")
{
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());
}
}
else
{
result.type = EntryType::USER;
}
if(result.type != EntryType::INHERIT)
{
if(map.contains("arguments")) if(map.contains("arguments"))
{ {
auto args = map["arguments"].split(' '); auto args = map["arguments"].split(' ');
@ -188,6 +216,7 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
assignIfSourceNotEmpty(map["command"], result.command); assignIfSourceNotEmpty(map["command"], result.command);
assignIfSourceNotEmpty(map["icon"], result.iconPath); assignIfSourceNotEmpty(map["icon"], result.iconPath);
assignIfSourceNotEmpty(map["name"], result.name); assignIfSourceNotEmpty(map["name"], result.name);
}
result.col = map["col"].toInt(); result.col = map["col"].toInt();
result.row = map["row"].toInt(); result.row = map["row"].toInt();
return result; return result;
@ -216,7 +245,7 @@ QString EntryProvider::resolveEntryPath(QString path)
return {}; return {};
} }
QVector<EntryConfig> EntryProvider::readConfig(QStringList paths, bool userentrymode) QVector<EntryConfig> EntryProvider::readConfig(QStringList paths)
{ {
QVector<EntryConfig> result; QVector<EntryConfig> result;
for(QString &configPath : paths) for(QString &configPath : paths)
@ -230,10 +259,6 @@ QVector<EntryConfig> EntryProvider::readConfig(QStringList paths, bool userentry
{ {
if(!entry->hidden) if(!entry->hidden)
{ {
if(userentrymode)
{
entry->userEntry = true;
}
entry->entryPath = path; entry->entryPath = path;
result.append(*entry); result.append(*entry);
} }
@ -245,7 +270,7 @@ QVector<EntryConfig> EntryProvider::readConfig(QStringList paths, bool userentry
QVector<EntryConfig> EntryProvider::getUserEntries() QVector<EntryConfig> EntryProvider::getUserEntries()
{ {
return readConfig(this->userEntriesDirsPaths, true); return readConfig(this->userEntriesDirsPaths);
} }
QVector<EntryConfig> EntryProvider::getSystemEntries() QVector<EntryConfig> EntryProvider::getSystemEntries()
@ -255,9 +280,9 @@ QVector<EntryConfig> EntryProvider::getSystemEntries()
void EntryProvider::saveUserEntry(const EntryConfig &config) 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"; QString transitPath = config.entryPath + ".transit";
QFile file{transitPath}; QFile file{transitPath};
@ -266,10 +291,15 @@ void EntryProvider::saveUserEntry(const EntryConfig &config)
throw std::runtime_error("Error: Can not open file for writing"); throw std::runtime_error("Error: Can not open file for writing");
} }
QTextStream outStream(&file); QTextStream outStream(&file);
outStream << "type" << " " << ((config.type == EntryType::USER) ? "user" : "inherit") << endl;
if(!config.inherit.isEmpty()) if(!config.inherit.isEmpty())
{ {
outStream << "inherit" << " " << config.inherit << endl; outStream << "inherit" << " " << config.inherit << endl;
} }
outStream << "row" << " " << config.row << endl;
outStream << "col" << " " << config.col << endl;
if(config.type == EntryType::USER)
{
if(!config.name.isEmpty()) if(!config.name.isEmpty())
{ {
outStream << "name" << " " << config.name << endl; outStream << "name" << " " << config.name << endl;
@ -282,8 +312,8 @@ void EntryProvider::saveUserEntry(const EntryConfig &config)
{ {
outStream << "icon" << " " << config.iconPath << endl; outStream << "icon" << " " << config.iconPath << endl;
} }
outStream << "row" << " " << config.row << endl; }
outStream << "col" << " " << config.col << endl;
outStream.flush(); outStream.flush();
file.close(); file.close();
@ -298,11 +328,11 @@ void EntryProvider::saveUserEntry(const EntryConfig &config)
bool EntryProvider::deleteUserEntry(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(); return file.remove();
} }
@ -329,6 +359,6 @@ EntryConfig &EntryConfig::update(const EntryConfig &o)
assignIfDestDefault(this->hidden, o.hidden); assignIfDestDefault(this->hidden, o.hidden);
assignIfDestDefault(this->inherit, o.inherit); assignIfDestDefault(this->inherit, o.inherit);
assignIfDestDefault(this->entryPath, o.entryPath); assignIfDestDefault(this->entryPath, o.entryPath);
assignIfDestDefault(this->userEntry, o.userEntry); assignIfDestDefault(this->type, o.type);
return *this; return *this;
} }

View File

@ -15,10 +15,17 @@ class ConfigFormatException : public std::runtime_error
} }
}; };
enum EntryType
{
USER,
INHERIT,
SYSTEM
};
class EntryConfig class EntryConfig
{ {
public: public:
bool userEntry = false; EntryType type = SYSTEM;
bool hidden = false; bool hidden = false;
QString entryPath; QString entryPath;
QString key; QString key;
@ -42,7 +49,7 @@ class EntryProvider
EntryConfig readqsrunFile(const QString &path); EntryConfig readqsrunFile(const QString &path);
EntryConfig readFromDesktopFile(const QString &path); EntryConfig readFromDesktopFile(const QString &path);
std::optional<EntryConfig> readEntryFromPath(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); QString resolveEntryPath(QString path);
public: public:

View File

@ -94,7 +94,7 @@ void EntryPushButton::mousePressEvent(QMouseEvent *event)
} }
if(event->button() == Qt::RightButton) 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()); this->userEntryMenu.exec(QCursor::pos());
} }
@ -108,7 +108,7 @@ void EntryPushButton::mousePressEvent(QMouseEvent *event)
void EntryPushButton::mouseMoveEvent(QMouseEvent *event) void EntryPushButton::mouseMoveEvent(QMouseEvent *event)
{ {
if(!this->config.userEntry) if(this->config.type == EntryType::SYSTEM)
{ {
return; return;
} }

View File

@ -128,11 +128,11 @@ void Window::addToFavourites(const EntryConfig &config)
{ {
std::pair<int, int> cell = getNextFreeCell(); std::pair<int, int> cell = getNextFreeCell();
EntryConfig userConfig; EntryConfig userConfig;
userConfig.userEntry = true; userConfig.type = EntryType::INHERIT;
userConfig.row = cell.first; userConfig.row = cell.first;
userConfig.col = cell.second; userConfig.col = cell.second;
userConfig.inherit = userConfig.entryPath; userConfig.inherit = config.entryPath;
QFileInfo fi{userConfig.entryPath}; QFileInfo fi{config.entryPath};
QString entryName = fi.completeBaseName() + ".qsrun"; QString entryName = fi.completeBaseName() + ".qsrun";
userConfig.entryPath = this->settingsProvider->userEntriesPaths()[0] + "/" + entryName; userConfig.entryPath = this->settingsProvider->userEntriesPaths()[0] + "/" + entryName;
try try