From 730df687e65d6f49a4cddf4284ce8e70898f4411 Mon Sep 17 00:00:00 2001 From: ConfuSomu Date: Fri, 16 Jun 2023 18:45:44 -0400 Subject: 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. --- src/archive_parser.cpp | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) (limited to 'src') 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 #include #include +#include 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* templates = new QHash; - 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("Error loading status_info template."); - return html; + templates->insert(template_name, "Error loading status_info template."); + 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("Error loading attachment template."); - 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()) -- cgit v1.2.3-54-g00ecf