From 477a7257e3aaec13dac7096ab91749399543502e Mon Sep 17 00:00:00 2001 From: ConfuSomu Date: Mon, 26 Dec 2022 17:41:45 -0500 Subject: Implement initial archive parsing --- src/archive_parser.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/archive_parser.cpp (limited to 'src/archive_parser.cpp') diff --git a/src/archive_parser.cpp b/src/archive_parser.cpp new file mode 100644 index 0000000..09b8cef --- /dev/null +++ b/src/archive_parser.cpp @@ -0,0 +1,46 @@ +#include "archive_parser.h" + +#include +#include +#include + +Archive::Archive(QString outbox_filename, ArchiveType archive_type) : + outbox_filename(outbox_filename), archive_type(archive_type) {} + +Archive::InitError Archive::init() { + QFile outbox_file(outbox_filename); + + if (!outbox_file.open(QIODevice::ReadOnly | QIODevice::Text)) + return FailedOpeningFile; + + QJsonParseError json_error; + QJsonDocument outbox_json_document = QJsonDocument::fromJson(outbox_file.readAll(), &json_error); + outbox_file.close(); + + if (json_error.error != QJsonParseError::NoError) + return JsonParseError; + if (outbox_json_document.isEmpty()) + return JsonEmpty; + if (outbox_json_document.isNull()) + return JsonNull; + + if (outbox_json_document.isObject()) + outbox_json = new QJsonObject (outbox_json_document.object()); + else + return JsonNotObject; + + // Do some more throughful checks to make sure that the JSON is actually valid. + if (not (outbox_json->contains("@context") and outbox_json->value("@context").isString() and + (outbox_json->value("@context").toString() == "https://www.w3.org/ns/activitystreams"))) + return JsonNotActivityStream; + if (outbox_json->contains("orderedItems") and outbox_json->value("orderedItems").isArray()) { + outbox_items = new QJsonArray(outbox_json->value("orderedItems").toArray()); // we'll need it during Archive's lifetime + } + + return NoError; +} + +Archive::~Archive() { + delete outbox_json; outbox_json = nullptr; + delete outbox_items; outbox_items = nullptr; +} -- cgit v1.2.3-54-g00ecf