diff options
Diffstat (limited to 'src/archive_parser.cpp')
-rw-r--r-- | src/archive_parser.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
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 <QFile> +#include <QJsonParseError> +#include <QDebug> + +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; +} |