data/ComposeHtmlImport.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "service/NativeMailDocument.h" | ||
| 4 | |||
| 5 | #include <QByteArray> | ||
| 6 | #include <QFont> | ||
| 7 | #include <QHash> | ||
| 8 | #include <QList> | ||
| 9 | #include <QString> | ||
| 10 | #include <QTextDocumentFragment> | ||
| 11 | #include <atomic> | ||
| 12 | |||
| 13 | class QTextDocument; | ||
| 14 | |||
| 15 | // Sprint 108 S108.2.3 (D-9): foreign and own HTML into the composer. | ||
| 16 | // Untrusted HTML (quotes of HTML mails, pastes from browsers and office | ||
| 17 | // programs, resumed drafts) passes NativeMailDocument in Compose mode — the | ||
| 18 | // one bounded parser of untrusted HTML — and is then reduced to the | ||
| 19 | // ComposeDocumentFormat vocabulary (D-6). Remote images are never fetched: | ||
| 20 | // they become a link with their alternative text and are counted. data: | ||
| 21 | // images become new inline images the caller registers under the returned | ||
| 22 | // names. Runs on any thread (no resources are loaded). | ||
| 23 | // | ||
| 24 | // Invariant 2 (SPRINT-108 §5.2): MailHtmlWriter output of a document, | ||
| 25 | // imported again, gives the same document (text, formats, images). | ||
| 26 | class ComposeHtmlImport { | ||
| 27 | public: | ||
| 28 | struct Image { | ||
| 29 | QString name; // ComposeDocumentFormat::ImageScheme + generated id | ||
| 30 | QByteArray data; | ||
| 31 | QString mimeType; | ||
| 32 | QString alternativeText; | ||
| 33 | }; | ||
| 34 | struct Options { | ||
| 35 | // cid → image resource name the composer already holds (inline parts of | ||
| 36 | // a quoted original, own drafts); an unknown cid keeps its alternative text. | ||
| 37 | QHash<QString, QString> contentIdImages; | ||
| 38 | QFont defaultFont; | ||
| 39 | }; | ||
| 40 | struct Result { | ||
| 41 | NativeMailDocument::Outcome outcome = NativeMailDocument::Outcome::Failed; | ||
| 42 | QTextDocumentFragment fragment; | ||
| 43 | QList<Image> images; | ||
| 44 | int remoteImagesLeftOut = 0; | ||
| 45 | bool simplified = false; | ||
| 46 | 211 | bool ok() const { return outcome == NativeMailDocument::Outcome::Ready; } | |
| 47 | }; | ||
| 48 | static Result import(const QByteArray &html, const Options &options = {}, | ||
| 49 | const std::atomic_bool *cancelled = nullptr); | ||
| 50 | // Reduces a document to the D-6 vocabulary in place (used by import and | ||
| 51 | // by pastes of Qt rich text). `alternativeTexts` maps image names to the | ||
| 52 | // text written for them. | ||
| 53 | static void normalize(QTextDocument &document, const QHash<QString, QString> &alternativeTexts = {}); | ||
| 54 | }; | ||
| 55 |