diff options
author | ConfuSomu | 2023-08-03 17:23:33 +0200 |
---|---|---|
committer | ConfuSomu | 2023-08-03 17:23:33 +0200 |
commit | bf73a19e11e5cccd22f58a5c97e1346dab5587c8 (patch) | |
tree | 64654503131a2eb77c5e9cbf25478330e8ce261b | |
parent | 1500681405804b932761db7a0182d498894ad81c (diff) | |
download | ActorViewer-bf73a19e11e5cccd22f58a5c97e1346dab5587c8.tar ActorViewer-bf73a19e11e5cccd22f58a5c97e1346dab5587c8.tar.gz ActorViewer-bf73a19e11e5cccd22f58a5c97e1346dab5587c8.zip |
Create an Archive base class
This class is inherited by MastodonArchive to provide Mastodon (and
compatible) archive reading support. A base Archive class allows
implementing reading support of other archive formats that are from
other services.
-rw-r--r-- | CMakeLists.txt | 6 | ||||
-rw-r--r-- | src/archive/base_archive.cpp | 13 | ||||
-rw-r--r-- | src/archive/base_archive.h | 36 | ||||
-rw-r--r-- | src/archive/mastodon.cpp (renamed from src/archive_parser.cpp) | 45 | ||||
-rw-r--r-- | src/archive/mastodon.h (renamed from src/archive_parser.h) | 30 | ||||
-rw-r--r-- | src/list_item.h | 2 | ||||
-rw-r--r-- | src/mainwindow.cpp | 6 | ||||
-rw-r--r-- | src/mainwindow.h | 2 |
8 files changed, 88 insertions, 52 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index d28f320..2d09544 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,8 +24,10 @@ set(PROJECT_SOURCES src/finddialog.cpp src/finddialog.h src/finddialog.ui - src/archive_parser.cpp - src/archive_parser.h + src/archive/base_archive.cpp + src/archive/base_archive.h + src/archive/mastodon.cpp + src/archive/mastodon.h src/types.h src/list_item.cpp src/list_item.h diff --git a/src/archive/base_archive.cpp b/src/archive/base_archive.cpp new file mode 100644 index 0000000..14fa417 --- /dev/null +++ b/src/archive/base_archive.cpp @@ -0,0 +1,13 @@ +#include "src/archive/base_archive.h" +#include "src/archive/mastodon.h" + +Archive* Archive::create_archive(ArchiveType archive_type, const QString& main_filename) { + switch (archive_type) { + case MASTODON: + return new MastodonArchive(main_filename); + default: + return nullptr; + } +} + +Archive::Archive(const QString& filename) : main_filename(filename) {} diff --git a/src/archive/base_archive.h b/src/archive/base_archive.h new file mode 100644 index 0000000..fcabcf1 --- /dev/null +++ b/src/archive/base_archive.h @@ -0,0 +1,36 @@ +#pragma once + +#include <QString> +#include <QListWidget> +#include <variant> +#include "src/types.h" + +enum ArchiveType { + MASTODON +}; + +class Archive { +public: + enum InitError { + NoError = 0, + FailedOpeningFile, + JsonParseError, + JsonEmpty, + JsonNull, + JsonNotObject, + JsonNotActivityStream // for ActivityPub archives + }; + + virtual ~Archive() {}; + virtual std::variant<QString, InitError> init() = 0; + + static Archive* create_archive(ArchiveType archive_type, const QString& main_filename); + + virtual void update_status_list(ViewStatusTypes allowed_types, QListWidget *parent) = 0; + virtual const QString get_html_status_info(int status_index, int text_zone_width, StatusType status_type, QLocale* locale) = 0; + virtual const QString get_html_status_text(int status_index) = 0; + +protected: + Archive(const QString& main_filename); + QString main_filename; +}; diff --git a/src/archive_parser.cpp b/src/archive/mastodon.cpp index ab7f67d..297383e 100644 --- a/src/archive_parser.cpp +++ b/src/archive/mastodon.cpp @@ -1,12 +1,12 @@ -#include "archive_parser.h" +#include "mastodon.h" #include "src/list_item.h" #include "src/types.h" -#include "activitypub/apactivity.h" -#include "activitypub/apobject.h" -#include "activitypub/apreblog.h" -#include "activitypub/appost.h" -#include "activitypub/apquestion.h" -#include "activitypub/fields.h" +#include "src/activitypub/apactivity.h" +#include "src/activitypub/apobject.h" +#include "src/activitypub/apreblog.h" +#include "src/activitypub/appost.h" +#include "src/activitypub/apquestion.h" +#include "src/activitypub/fields.h" #include <QFile> #include <QJsonParseError> @@ -17,11 +17,10 @@ #include <QHash> #include <QDebug> -Archive::Archive(QString outbox_filename, ArchiveType archive_type) : - outbox_filename(outbox_filename), archive_type(archive_type) {} +MastodonArchive::MastodonArchive(const QString& filename) : Archive(filename) {} -std::variant<QString, Archive::InitError> Archive::init() { - QFile outbox_file(outbox_filename); +std::variant<QString, MastodonArchive::InitError> MastodonArchive::init() { + QFile outbox_file(main_filename); if (!outbox_file.open(QIODevice::ReadOnly | QIODevice::Text)) return FailedOpeningFile; @@ -50,17 +49,17 @@ std::variant<QString, Archive::InitError> Archive::init() { } else return JsonParseError; - archive_root_dir = QFileInfo(outbox_filename).absoluteDir(); + archive_root_dir = QFileInfo(main_filename).absoluteDir(); return NoError; } -Archive::~Archive() { +MastodonArchive::~MastodonArchive() { delete outbox_json; outbox_json = nullptr; delete outbox_items; outbox_items = nullptr; } -bool Archive::is_status_type_allowed(StatusType status_type, ViewStatusTypes allowed_types) { +bool MastodonArchive::is_status_type_allowed(StatusType status_type, ViewStatusTypes allowed_types) { switch (status_type) { case PUBLIC: return allowed_types.includePublic; case UNLISTED: return allowed_types.includeUnlisted; @@ -72,7 +71,7 @@ bool Archive::is_status_type_allowed(StatusType status_type, ViewStatusTypes all } // specific to Mastodon ActivityStreams archives -StatusType Archive::get_status_type(QJsonObject obj) { +StatusType MastodonArchive::get_status_type(QJsonObject obj) { /* * public: * to: #Public @@ -126,7 +125,7 @@ StatusType Archive::get_status_type(QJsonObject obj) { } // Note: No need to create an AP… class for that (unless we want to precache the Activities but that would be very slow and use a lot of memory) -void Archive::update_status_list(ViewStatusTypes allowed_types, QListWidget *parent) { +void MastodonArchive::update_status_list(ViewStatusTypes allowed_types, QListWidget *parent) { int i = 0; for (auto&& item : *outbox_items) { if (item.isObject()){ @@ -174,7 +173,7 @@ void Archive::update_status_list(ViewStatusTypes allowed_types, QListWidget *par } } -QStringList Archive::get_status_object_list(QJsonObject obj, QString element_name) { +QStringList MastodonArchive::get_status_object_list(QJsonObject obj, QString element_name) { QStringList list; if (obj.contains(element_name)) { @@ -187,7 +186,7 @@ QStringList Archive::get_status_object_list(QJsonObject obj, QString element_nam return list; } -QStringList Archive::get_status_object_language_list(QJsonObject obj, QString element_name = "contentMap") { +QStringList MastodonArchive::get_status_object_language_list(QJsonObject obj, QString element_name = "contentMap") { QStringList list; if (obj.contains(element_name) and obj.value(element_name).isObject()) { @@ -201,7 +200,7 @@ QStringList Archive::get_status_object_language_list(QJsonObject obj, QString el return list; } -std::vector<APAttachmentFields> Archive::get_status_attachments_list(QJsonValueRef attachments_ref) { +std::vector<APAttachmentFields> MastodonArchive::get_status_attachments_list(QJsonValueRef attachments_ref) { std::vector<APAttachmentFields> list; QJsonArray attachments = attachments_ref.toArray(); @@ -234,7 +233,7 @@ std::vector<APAttachmentFields> Archive::get_status_attachments_list(QJsonValueR } // status_index is assumed to be a valid index -const QString Archive::get_html_status_info(int status_index, int text_zone_width, StatusType status_type, QLocale* locale) { +const QString MastodonArchive::get_html_status_info(int status_index, int text_zone_width, StatusType status_type, QLocale* locale) { // the JSON AP Activity QJsonObject activity = outbox_items->at(status_index).toObject(); @@ -331,7 +330,7 @@ const QString Archive::get_html_status_info(int status_index, int text_zone_widt } // TODO: make this use an APActivity object that will be present as an StatusListItem member. -const QString Archive::get_html_status_text(int status_index) { +const QString MastodonArchive::get_html_status_text(int status_index) { QString text(""); QJsonObject obj = outbox_items->at(status_index).toObject(); @@ -352,7 +351,7 @@ const QString Archive::get_html_status_text(int status_index) { return text; } -void Archive::find_attachment_dir(QString example_attachment) { +void MastodonArchive::find_attachment_dir(QString example_attachment) { // Find the root directory name of the attachment QString root_name = example_attachment.split('/', Qt::SkipEmptyParts)[0]; @@ -377,7 +376,7 @@ void Archive::find_attachment_dir(QString example_attachment) { // If the attachment directory wasn't found, it will be searched for next attachment url parsing as attachment_dir_have_to_find wasn't touched (and is still true) } -bool Archive::json_check_item(const QJsonValue& value, const QString& item) { +bool MastodonArchive::json_check_item(const QJsonValue& value, const QString& item) { // This allows us to avoid having an "and" condition that has QJsonObject::contains() and simplifies the if statement to only having this current function if (value.type() == QJsonValue::Undefined) return false; diff --git a/src/archive_parser.h b/src/archive/mastodon.h index 162787e..48be1f3 100644 --- a/src/archive_parser.h +++ b/src/archive/mastodon.h @@ -1,42 +1,26 @@ #pragma once -#include <QString> +#include "src/archive/base_archive.h" +#include "src/activitypub/fields.h" +#include "src/types.h" + #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> #include <QListWidget> #include <QDir> -#include "types.h" -#include "activitypub/fields.h" - -enum ArchiveType { - MASTODON -}; - -class Archive { +class MastodonArchive : public Archive { public: - enum InitError { - NoError = 0, - FailedOpeningFile, - JsonParseError, - JsonEmpty, - JsonNull, - JsonNotObject, - JsonNotActivityStream // for ActivityPub archives - }; - - Archive(QString outbox_filename, ArchiveType archive_type); - ~Archive(); + MastodonArchive(const QString& filename); + ~MastodonArchive(); std::variant<QString, InitError> init(); void update_status_list(ViewStatusTypes allowed_types, QListWidget *parent); const QString get_html_status_info(int status_index, int text_zone_width, StatusType status_type, QLocale* locale); const QString get_html_status_text(int status_index); private: - QString outbox_filename; QDir archive_root_dir; - ArchiveType archive_type; QDir attachment_dir; bool attachment_dir_have_to_find = true; diff --git a/src/list_item.h b/src/list_item.h index 4514903..d730551 100644 --- a/src/list_item.h +++ b/src/list_item.h @@ -1,7 +1,7 @@ #pragma once #include <QListWidgetItem> -#include "src/archive_parser.h" +#include "src/archive/base_archive.h" #include "types.h" class StatusListItem : public QListWidgetItem { diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 89f550d..4fc3970 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1,6 +1,6 @@ #include "mainwindow.h" #include "./ui_mainwindow.h" -#include "src/archive_parser.h" +#include "src/archive/base_archive.h" #include "src/finddialog.h" #include "src/list_item.h" #include "src/command_line.h" @@ -218,7 +218,9 @@ void MainWindow::open_file(const QString &filename) { open_file_filename = filename; QApplication::setOverrideCursor(Qt::WaitCursor); - data_archive = new Archive(filename, ArchiveType::MASTODON); + + data_archive = Archive::create_archive(ArchiveType::MASTODON, filename); + if (not data_archive) return; QFuture<std::variant<QString, Archive::InitError>> parse_error = QtConcurrent::run(data_archive, &Archive::init); archive_thread_watcher.setFuture(parse_error); diff --git a/src/mainwindow.h b/src/mainwindow.h index 162e6bf..fced119 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -6,7 +6,7 @@ #include <QFutureWatcher> #include <variant> -#include "archive_parser.h" +#include "archive/base_archive.h" #include "types.h" #include "command_line.h" #include "finddialog.h" |