presentation/ComposeAttachmentModel.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <QAbstractListModel> | ||
| 4 | #include <QDateTime> | ||
| 5 | #include <QJsonArray> | ||
| 6 | #include <QList> | ||
| 7 | #include <QPointer> | ||
| 8 | #include <QThreadPool> | ||
| 9 | #include <QTimer> | ||
| 10 | #include <QUrl> | ||
| 11 | #include <memory> | ||
| 12 | #include <atomic> | ||
| 13 | #include <functional> | ||
| 14 | #include <optional> | ||
| 15 | |||
| 16 | // Sprint 108 S108.6.1/S108.6.2 (D-17, D-18): the attachment tray of one | ||
| 17 | // composer. | ||
| 18 | // | ||
| 19 | // A file is **copied into the draft's own directory when it is added** | ||
| 20 | // (owner-only), so what is sent is what the user saw and a restored draft | ||
| 21 | // keeps its files. Copying runs off the GUI thread with progress; JPEGs lose | ||
| 22 | // their location data on the way in (ComposeImageLocation). Limits are told | ||
| 23 | // when a file is added, never at *Send*: MailJD's own limits (25 MiB per | ||
| 24 | // file, 100 MiB, 16 files) and the server's SIZE, counted as encoded size. | ||
| 25 | // Images can be shrunk all at once (Large 2048 px, Medium 1280 px, Small | ||
| 26 | // 800 px) by re-encoding a copy off the GUI thread; the original stays until | ||
| 27 | // the mail is sent. | ||
| 28 | // | ||
| 29 | // GUI thread; one model per ComposeSession. | ||
| 30 | class ComposeAttachmentModel : public QAbstractListModel { | ||
| 31 | 265 | Q_OBJECT | |
| 32 | Q_PROPERTY(int count READ rowCountProperty NOTIFY changed) | ||
| 33 | Q_PROPERTY(qint64 totalBytes READ totalBytes NOTIFY changed) | ||
| 34 | Q_PROPERTY(qint64 limitBytes READ limitBytes NOTIFY changed) | ||
| 35 | Q_PROPERTY(QString summary READ summary NOTIFY changed) | ||
| 36 | Q_PROPERTY(QString notice READ notice NOTIFY noticeChanged) | ||
| 37 | Q_PROPERTY(bool busy READ busy NOTIFY changed) | ||
| 38 | Q_PROPERTY(bool hasImages READ hasImages NOTIFY changed) | ||
| 39 | Q_PROPERTY(QString imageSize READ imageSize NOTIFY changed) | ||
| 40 | Q_PROPERTY(bool canUndoRemove READ canUndoRemove NOTIFY changed) | ||
| 41 | |||
| 42 | public: | ||
| 43 | static constexpr qint64 PerFileLimit = 25LL * 1024 * 1024; | ||
| 44 | static constexpr qint64 TotalLimit = 100LL * 1024 * 1024; | ||
| 45 | static constexpr int MaxFiles = 16; | ||
| 46 | |||
| 47 | enum Roles { | ||
| 48 | IdRole = Qt::UserRole + 1, | ||
| 49 | NameRole, | ||
| 50 | SizeRole, | ||
| 51 | SizeTextRole, | ||
| 52 | MimeTypeRole, | ||
| 53 | TypeLabelRole, | ||
| 54 | IconRole, | ||
| 55 | StateRole, // "copying", "ready", "failed" | ||
| 56 | ProgressRole, // 0…100 | ||
| 57 | ErrorRole, | ||
| 58 | IsImageRole, | ||
| 59 | ThumbnailRole, // the percent-encoded path for image://compose-thumbnails/ | ||
| 60 | InlineRole, // placed in the text | ||
| 61 | LocationRemovedRole, | ||
| 62 | }; | ||
| 63 | |||
| 64 | struct Item { | ||
| 65 | QString id; | ||
| 66 | QString name; | ||
| 67 | QString sourcePath; // where the user's file came from (not read again) | ||
| 68 | QString storedPath; // the copy in the draft directory | ||
| 69 | QString resizedPath; // the shrunk copy, when a size step applies | ||
| 70 | QString mimeType; | ||
| 71 | qint64 size = 0; | ||
| 72 | qint64 resizedSize = 0; | ||
| 73 | QString state = QStringLiteral("copying"); | ||
| 74 | int progress = 0; | ||
| 75 | QString error; | ||
| 76 | bool image = false; | ||
| 77 | bool inlinePlaced = false; | ||
| 78 | bool locationRemoved = false; | ||
| 79 | bool keepLocation = false; | ||
| 80 | QString contentId; // for inline images (cid:) | ||
| 81 |
2/2✓ Branch 3 → 4 taken 654 times.
✓ Branch 3 → 5 taken 1 time.
|
655 | qint64 sentBytes() const { return resizedPath.isEmpty() ? size : resizedSize; } |
| 82 |
1/2✓ Branch 3 → 4 taken 56 times.
✗ Branch 3 → 5 not taken.
|
56 | QString sentPath() const { return resizedPath.isEmpty() ? storedPath : resizedPath; } |
| 83 | }; | ||
| 84 | |||
| 85 | explicit ComposeAttachmentModel(const QString &directory, QObject *parent = nullptr); | ||
| 86 | ~ComposeAttachmentModel() override; | ||
| 87 | |||
| 88 | int rowCount(const QModelIndex &parent = {}) const override; | ||
| 89 | QVariant data(const QModelIndex &index, int role) const override; | ||
| 90 | QHash<int, QByteArray> roleNames() const override; | ||
| 91 | |||
| 92 | QString directory() const { return m_directory; } | ||
| 93 | // D-18: the server's SIZE for the composing identity's account (0 = none). | ||
| 94 | void setServerLimit(qint64 bytes); | ||
| 95 | qint64 limitBytes() const; | ||
| 96 | qint64 totalBytes() const; | ||
| 97 | // The encoded size (base64 plus line breaks) the message grows by. | ||
| 98 | static qint64 encodedSize(qint64 bytes); | ||
| 99 | QString summary() const; | ||
| 100 | 660 | QString notice() const { return m_notice; } | |
| 101 | bool busy() const; | ||
| 102 | bool hasImages() const; | ||
| 103 | 4 | QString imageSize() const { return m_imageSize; } | |
| 104 | 1 | bool canUndoRemove() const { return !m_removed.isEmpty(); } | |
| 105 | 626 | int rowCountProperty() const { return int(m_items.size()); } | |
| 106 | |||
| 107 | // Files from a drop, the file dialog or a paste. Returns how many were | ||
| 108 | // admitted; refusals (too large, folders, unreadable, too many) set the | ||
| 109 | // notice (§4.6: a transient row for 8 s). | ||
| 110 | Q_INVOKABLE int addUrls(const QList<QUrl> &urls); | ||
| 111 | Q_INVOKABLE int addPaths(const QStringList &paths); | ||
| 112 | // In-memory content (a pasted image, a forwarded .eml); returns the id. | ||
| 113 | // `wantedId`: the item's id when the text already names the picture by it | ||
| 114 | // (an imported data: image, ImageScheme + id); a new one when empty. | ||
| 115 | QString addData(const QString &name, const QByteArray &data, const QString &mimeType = {}, const QString &wantedId = {}); | ||
| 116 | Q_INVOKABLE void remove(const QString &id); | ||
| 117 | Q_INVOKABLE void undoRemove(); | ||
| 118 | Q_INVOKABLE bool rename(const QString &id, const QString &name); | ||
| 119 | Q_INVOKABLE void retry(const QString &id); | ||
| 120 | Q_INVOKABLE void setInline(const QString &id, bool placed); | ||
| 121 | // "original", "large", "medium", "small" — applied to every image. | ||
| 122 | Q_INVOKABLE void setImageSize(const QString &size); | ||
| 123 | // Copies the file again with its location (the card's "Keep location"). | ||
| 124 | Q_INVOKABLE void keepLocation(const QString &id); | ||
| 125 | Q_INVOKABLE QString pathFor(const QString &id) const; | ||
| 126 | |||
| 127 | 856 | QList<Item> items() const { return m_items; } | |
| 128 | std::optional<Item> item(const QString &id) const; | ||
| 129 | // Draft manifest (D-21) and its restore from the same directory. | ||
| 130 | QJsonArray manifest() const; | ||
| 131 | void restore(const QJsonArray &manifest); | ||
| 132 | // Removes the directory and every copy (send finished, draft discarded). | ||
| 133 | void removeAll(); | ||
| 134 | // Waits for running copies (tests, send). | ||
| 135 | void waitForJobs(); | ||
| 136 | |||
| 137 | signals: | ||
| 138 | void changed(); | ||
| 139 | void noticeChanged(); | ||
| 140 | void copyFinished(const QString &id, bool ok); | ||
| 141 | |||
| 142 | private: | ||
| 143 | QString m_directory; | ||
| 144 | QList<Item> m_items; | ||
| 145 | QList<QPair<int, Item>> m_removed; // undo stack of removed cards | ||
| 146 | qint64 m_serverLimit = 0; | ||
| 147 | QString m_notice; | ||
| 148 | QTimer m_noticeTimer; | ||
| 149 | QString m_imageSize = QStringLiteral("original"); | ||
| 150 | QThreadPool m_pool; | ||
| 151 | std::shared_ptr<std::atomic_bool> m_alive; | ||
| 152 | quint64 m_resizeGeneration = 0; | ||
| 153 | |||
| 154 | int indexOf(const QString &id) const; | ||
| 155 | void setNotice(const QString ¬ice); | ||
| 156 | bool admit(const QString &name, qint64 size, QString *reason) const; | ||
| 157 | void startCopy(const QString &id, const QString &source, bool stripLocation); | ||
| 158 | void startResize(const QString &id); | ||
| 159 | void updateItem(const QString &id, const std::function<void(Item &)> &change); | ||
| 160 | QString uniqueTarget(const QString &name) const; | ||
| 161 | }; | ||
| 162 |