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
szülő 95e855f325
commit 79d15fb628
4 fájl változott, egészen pontosan 103 új sor hozzáadva és 66 régi sor törölve

Fájl megtekintése

@ -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,47 +148,75 @@ EntryConfig EntryProvider::readqsrunFile(const QString &path)
throw new ConfigFormatException("Error attempting to read inherited entry"); 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(' '); if(type == "system")
QString merged;
for(QString &str : args)
{ {
if(str.startsWith('"') && !str.endsWith("'")) throw new ConfigFormatException(".qsrun files cannot be designated as system entries " +
{ path.toStdString());
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 != "") 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) { else
if(!source.isEmpty()) {
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());
}
} }
}; auto assignIfSourceNotEmpty = [](QString source, QString &val) {
assignIfSourceNotEmpty(map["key"].toLower(), result.key); if(!source.isEmpty())
assignIfSourceNotEmpty(map["command"], result.command); {
assignIfSourceNotEmpty(map["icon"], result.iconPath); val = source;
assignIfSourceNotEmpty(map["name"], result.name); }
};
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.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,24 +291,29 @@ 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;
} }
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 << "row" << " " << config.row << endl;
outStream << "col" << " " << config.col << 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(); 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;
} }

Fájl megtekintése

@ -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:

Fájl megtekintése

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

Fájl megtekintése

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