Skip to content

Commit

Permalink
Refactor: customData keys defined as const
Browse files Browse the repository at this point in the history
Imrpove code readbility and reduce bug potential byt using static const Qstring for all customData keys in Metadata.
  • Loading branch information
jNullj committed Feb 20, 2023
1 parent 7e70c62 commit 3bfe866
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/core/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ const int Metadata::DefaultAutosaveDelayMin = 0;
// Fallback icon for return by reference
static const Metadata::CustomIconData NULL_ICON{};

namespace customDataKeys
{
static const QString savedSearch = QStringLiteral("KPXC_SavedSearch");
static const QString autosaveDelay = QStringLiteral("KPXC_autosaveDelayMin");
}; // namespace customDataKeys

Metadata::Metadata(QObject* parent)
: ModifiableObject(parent)
, m_customData(new CustomData(this))
Expand Down Expand Up @@ -268,7 +274,7 @@ int Metadata::historyMaxSize() const

int Metadata::autosaveDelayMin() const
{
QString autosaveDelayMinStr = m_customData->value("KPXC_autosaveDelayMin");
QString autosaveDelayMinStr = m_customData->value(customDataKeys::autosaveDelay);
if (autosaveDelayMinStr.isNull()) {
// data is not set yet, use default
return Metadata::DefaultAutosaveDelayMin;
Expand Down Expand Up @@ -495,7 +501,7 @@ void Metadata::setHistoryMaxSize(int value)
void Metadata::setAutosaveDelayMin(int value)
{
Q_ASSERT(value >= 0 && value <= 420000000);
m_customData->set("KPXC_autosaveDelayMin", QString::number(value));
m_customData->set(customDataKeys::autosaveDelay, QString::number(value));
}

QDateTime Metadata::settingsChanged() const
Expand All @@ -514,20 +520,20 @@ void Metadata::addSavedSearch(const QString& name, const QString& searchtext)
auto searches = savedSearches();
searches.insert(name, searchtext);
auto json = QJsonDocument::fromVariant(searches);
m_customData->set("KPXC_SavedSearch", json.toJson());
m_customData->set(customDataKeys::savedSearch, json.toJson());
}

void Metadata::deleteSavedSearch(const QString& name)
{
auto searches = savedSearches();
searches.remove(name);
auto json = QJsonDocument::fromVariant(searches);
m_customData->set("KPXC_SavedSearch", json.toJson());
m_customData->set(customDataKeys::savedSearch, json.toJson());
}

QVariantMap Metadata::savedSearches()
{
auto searches = m_customData->value("KPXC_SavedSearch");
auto searches = m_customData->value(customDataKeys::savedSearch);
auto json = QJsonDocument::fromJson(searches.toUtf8());
return json.toVariant().toMap();
}

0 comments on commit 3bfe866

Please sign in to comment.