Skip to content

Commit

Permalink
Add author, contributors and icon to metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
Holt59 committed Nov 10, 2023
1 parent dd8a759 commit d4669ec
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
45 changes: 40 additions & 5 deletions src/extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QDirIterator>
#include <QFile>
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonDocument>

#include "log.h"
Expand Down Expand Up @@ -40,20 +41,53 @@ namespace
return files;
}

// parse an author from a JSON value
//
ExtensionContributor parseContributor(QJsonValue const& value)
{
if (value.isNull()) {
return ExtensionContributor("");
}

// TODO: handle more fields in the future, handle string authors similar to NPM

if (value.isObject()) {
const auto contrib = value.toObject();
return ExtensionContributor(contrib["name"].toString());
}

return ExtensionContributor(value.toString());
}

} // namespace

ExtensionMetaData::ExtensionMetaData(QJsonObject const& jsonData) : m_JsonData{jsonData}
ExtensionContributor::ExtensionContributor(QString name) : m_Name{name} {}

ExtensionMetaData::ExtensionMetaData(std::filesystem::path const& path,
QJsonObject const& jsonData)
: m_JsonData{jsonData}
{
// read basic fields
m_Identifier = jsonData["id"].toString();
m_Type = parseType(jsonData["type"].toString());
m_Name = jsonData["name"].toString();
m_Author = parseContributor(jsonData["author"]);
m_Description = jsonData["description"].toString();
m_Version.parse(jsonData["version"].toString("0.0.0"));

// TODO: name of the key
// translation context
m_TranslationContext = jsonData["translationContext"].toString("");

if (jsonData.contains("icon")) {
m_Icon = QIcon(QDir(path).filePath(jsonData["icon"].toString()));
}

if (jsonData.contains("contributors")) {
for (const auto& jsonContributor : jsonData["contributors"].toArray()) {
m_Contributors.push_back(parseContributor(jsonContributor));
}
}
}

bool ExtensionMetaData::isValid() const
Expand Down Expand Up @@ -142,7 +176,8 @@ ExtensionFactory::loadExtension(std::filesystem::path directory)
return nullptr;
}

return loadExtension(std::move(directory), ExtensionMetaData(jsonMetaData.object()));
return loadExtension(std::move(directory),
ExtensionMetaData(directory, jsonMetaData.object()));
}

std::unique_ptr<IExtension>
Expand Down Expand Up @@ -318,9 +353,9 @@ PluginExtension::loadExtension(std::filesystem::path path, ExtensionMetaData met

if (jsonTranslations.contains("*")) {
// * is a custom entry - * should point to a list of file prefix, e.g.,
// ["translations/foo_", "translations/bar_"] meaning that the translations files
// are prefixed by foo_ and bar_ inside the translations folder, language is
// extracted by removing the prefix
// ["translations/foo_", "translations/bar_"] meaning that the translations
// files are prefixed by foo_ and bar_ inside the translations folder, language
// is extracted by removing the prefix
//
// TODO: remove this option
//
Expand Down
38 changes: 36 additions & 2 deletions src/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,26 @@ enum class ExtensionType
GAME
};

class QDLLEXPORT ExtensionMetaData
class QDLLEXPORT ExtensionContributor
{
public:
ExtensionMetaData(QJsonObject const& jsonData);
ExtensionContributor(QString name);

// retrieve the name of the contributor
//
const auto& name() const { return m_Name; }

private:
ExtensionContributor() = default;

friend class ExtensionMetaData;

QString m_Name;
};

class QDLLEXPORT ExtensionMetaData
{
public:
// check if that metadata object is valid
//
bool isValid() const;
Expand All @@ -45,6 +60,14 @@ class QDLLEXPORT ExtensionMetaData
//
auto name() const { return localized(m_Name); }

// retrieve the author of the extension if set
//
const auto& author() const { return m_Author; }

// retrieve the list of contributors of the extension
//
const auto& contributors() const { return m_Contributors; }

// retrieve the type of the extension
//
auto type() const { return m_Type; }
Expand All @@ -53,6 +76,10 @@ class QDLLEXPORT ExtensionMetaData
//
auto description() const { return localized(m_Description); }

// retrieve the icon for the extension (might be an empty icon)
//
const auto& icon() const { return m_Icon; }

// retrieve the version of the extension.
//
const auto& version() const { return m_Version; }
Expand All @@ -69,19 +96,26 @@ class QDLLEXPORT ExtensionMetaData
QString localized(QString const& value) const;

private:
friend class ExtensionFactory;

constexpr static const char* DEFAULT_TRANSLATIONS_FOLDER = "translations";
constexpr static const char* DEFAULT_STYLESHEET_PATH = "stylesheets";

ExtensionType parseType(QString const& value) const;

ExtensionMetaData(std::filesystem::path const& path, const QJsonObject& jsonData);

private:
QJsonObject m_JsonData;
QString m_TranslationContext;

QString m_Identifier;
QString m_Name;
ExtensionContributor m_Author;
std::vector<ExtensionContributor> m_Contributors;
ExtensionType m_Type;
QString m_Description;
QIcon m_Icon;
VersionInfo m_Version;

std::filesystem::path m_TranslationFilesPrefix;
Expand Down

0 comments on commit d4669ec

Please sign in to comment.