diff options
author | ConfuSomu | 2023-06-16 18:45:44 -0400 |
---|---|---|
committer | ConfuSomu | 2023-06-16 18:45:44 -0400 |
commit | 730df687e65d6f49a4cddf4284ce8e70898f4411 (patch) | |
tree | fc8188cca7159f19fdc71b692214db079cef9810 | |
parent | a01366aed10563517f7bd9cde439b250bec7710d (diff) | |
download | ActorViewer-730df687e65d6f49a4cddf4284ce8e70898f4411.tar ActorViewer-730df687e65d6f49a4cddf4284ce8e70898f4411.tar.gz ActorViewer-730df687e65d6f49a4cddf4284ce8e70898f4411.zip |
Consolidate getting html templates into 1 function
This avoids redundant code and allows easy extension to more templates.
I use a QHash instead of QMap as the former is a little faster.
-rw-r--r-- | src/archive_parser.cpp | 38 |
1 files changed, 12 insertions, 26 deletions
diff --git a/src/archive_parser.cpp b/src/archive_parser.cpp index 97858c6..63acf60 100644 --- a/src/archive_parser.cpp +++ b/src/archive_parser.cpp @@ -8,6 +8,7 @@ #include <QFileInfo> #include <QDateTime> #include <QDirIterator> +#include <QHash> Archive::Archive(QString outbox_filename, ArchiveType archive_type) : outbox_filename(outbox_filename), archive_type(archive_type) {} @@ -163,36 +164,21 @@ void Archive::update_status_list(ViewStatusTypes allowed_types, QListWidget *par } } -QString* get_html_status_info_template() { - static QString* html = new QString(); +QString get_html_template(const QString& template_name) { + static QHash<QString, QString>* templates = new QHash<QString, QString>; - if (html->isNull()) { - QFile file("src/templates/status_info.html"); + if (not templates->contains(template_name)) { + QFile file("src/templates/"+ template_name +".html"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { - html->append("<i>Error loading status_info template.</i>"); - return html; + templates->insert(template_name, "<i>Error loading status_info template.</i>"); + return templates->value(template_name); } - html->append(file.readAll()); + qDebug() << "Loaded HTML template:" << template_name; + templates->insert(template_name, file.readAll()); } - return html; -} - -QString* get_html_status_info_attachment_template() { - static QString* html = new QString(); - - if (html->isNull()) { - QFile file("src/templates/attachment.html"); - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { - html->append("<i>Error loading attachment template.</i>"); - return html; - } - - html->append(file.readAll()); - } - - return html; + return templates->value(template_name); } QString get_html_status_object_as_list(QJsonObject obj, QString element_name) { @@ -238,7 +224,7 @@ QString Archive::get_html_status_attachments(QJsonValueRef attachments_ref, int int i = 1; for (auto attachment_ref : attachments) { - QString att_html(*get_html_status_info_attachment_template()); + QString att_html(get_html_template("attachment")); QJsonObject attachment = attachment_ref.toObject(); att_html.replace("{{id}}", QString::number(i)); @@ -273,7 +259,7 @@ QString Archive::get_html_status_attachments(QJsonValueRef attachments_ref, int // status_index is assumed to be a valid index QString Archive::get_html_status_info(int status_index, int text_zone_width, StatusType status_type, QLocale* locale) { - QString info_text(*get_html_status_info_template()); + QString info_text(get_html_template("status_info")); QJsonObject obj = outbox_items->at(status_index).toObject(); if (obj.contains("type") and obj["type"].isString()) |