| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <QIODevice> |
| 8 |
#include <QImage> |
| 9 |
#include <QObject> |
| 10 |
#include <QQmlEngine> |
| 11 |
#include <QSize> |
| 12 |
#include <QStringList> |
| 13 |
#include <QTimer> |
| 14 |
#include <QUrl> |
| 15 |
#include <QVariantList> |
| 16 |
|
| 17 |
#include <deque> |
| 18 |
#include <memory> |
| 19 |
|
| 20 |
#include <mtx/common.hpp> |
| 21 |
#include <mtx/events/common.hpp> |
| 22 |
|
| 23 |
class TimelineModel; |
| 24 |
class CombinedImagePackModel; |
| 25 |
class QMimeData; |
| 26 |
class QDropEvent; |
| 27 |
|
| 28 |
struct DeleteLaterDeleter |
| 29 |
{ |
| 30 |
void operator()(QObject *p) |
| 31 |
{ |
| 32 |
if (p) |
| 33 |
p->deleteLater(); |
| 34 |
} |
| 35 |
}; |
| 36 |
|
| 37 |
enum class MarkdownOverride |
| 38 |
{ |
| 39 |
NOT_SPECIFIED, |
| 40 |
ON, |
| 41 |
OFF, |
| 42 |
CMARK, |
| 43 |
}; |
| 44 |
|
| 45 |
class MediaUpload final : public QObject |
| 46 |
{ |
| 47 |
Q_OBJECT |
| 48 |
|
| 49 |
QML_ELEMENT |
| 50 |
QML_UNCREATABLE("") |
| 51 |
|
| 52 |
Q_PROPERTY(int mediaType READ type NOTIFY mediaTypeChanged) |
| 53 |
|
| 54 |
Q_PROPERTY(QUrl thumbnail READ thumbnailDataUrl NOTIFY thumbnailChanged) |
| 55 |
|
| 56 |
Q_PROPERTY(QString filename READ filename WRITE setFilename NOTIFY filenameChanged) |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public: |
| 62 |
enum MediaType |
| 63 |
{ |
| 64 |
File, |
| 65 |
Image, |
| 66 |
Video, |
| 67 |
Audio, |
| 68 |
}; |
| 69 |
Q_ENUM(MediaType) |
| 70 |
|
| 71 |
explicit MediaUpload(std::unique_ptr<QIODevice> data, |
| 72 |
const QString &mimetype, |
| 73 |
const QString &originalFilename, |
| 74 |
bool encrypt, |
| 75 |
QObject *parent = nullptr); |
| 76 |
|
| 77 |
[[nodiscard]] int type() const |
| 78 |
{ |
| 79 |
if (mimeClass_ == u"video") |
| 80 |
return MediaType::Video; |
| 81 |
else if (mimeClass_ == u"audio") |
| 82 |
return MediaType::Audio; |
| 83 |
else if (mimeClass_ == u"image") |
| 84 |
return MediaType::Image; |
| 85 |
else |
| 86 |
return MediaType::File; |
| 87 |
} |
| 88 |
[[nodiscard]] QString url() const { return url_; } |
| 89 |
[[nodiscard]] QString mimetype() const { return mimetype_; } |
| 90 |
[[nodiscard]] QString mimeClass() const { return mimeClass_; } |
| 91 |
[[nodiscard]] QString filename() const { return originalFilename_; } |
| 92 |
[[nodiscard]] QString blurhash() const { return blurhash_; } |
| 93 |
[[nodiscard]] uint64_t size() const { return size_; } |
| 94 |
[[nodiscard]] uint64_t duration() const { return duration_; } |
| 95 |
[[nodiscard]] std::optional<mtx::crypto::EncryptedFile> encryptedFile_() |
| 96 |
{ |
| 97 |
return encryptedFile; |
| 98 |
} |
| 99 |
[[nodiscard]] std::optional<mtx::crypto::EncryptedFile> thumbnailEncryptedFile_() |
| 100 |
{ |
| 101 |
return thumbnailEncryptedFile; |
| 102 |
} |
| 103 |
[[nodiscard]] QSize dimensions() const { return dimensions_; } |
| 104 |
|
| 105 |
QImage thumbnailImg() const { return thumbnail_; } |
| 106 |
QString thumbnailUrl() const { return thumbnailUrl_; } |
| 107 |
QUrl thumbnailDataUrl() const; |
| 108 |
[[nodiscard]] uint64_t thumbnailSize() const { return thumbnailSize_; } |
| 109 |
|
| 110 |
void setFilename(QString fn) |
| 111 |
{ |
| 112 |
if (fn != originalFilename_) { |
| 113 |
originalFilename_ = std::move(fn); |
| 114 |
emit filenameChanged(); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
signals: |
| 119 |
void uploadComplete(MediaUpload *self, QString url); |
| 120 |
void uploadFailed(MediaUpload *self); |
| 121 |
void filenameChanged(); |
| 122 |
void thumbnailChanged(); |
| 123 |
void mediaTypeChanged(); |
| 124 |
|
| 125 |
public slots: |
| 126 |
void startUpload(); |
| 127 |
|
| 128 |
private slots: |
| 129 |
void setThumbnail(QImage img) |
| 130 |
{ |
| 131 |
this->thumbnail_ = std::move(img); |
| 132 |
emit thumbnailChanged(); |
| 133 |
} |
| 134 |
|
| 135 |
public: |
| 136 |
|
| 137 |
|
| 138 |
std::unique_ptr<QIODevice> source; |
| 139 |
QByteArray data; |
| 140 |
QString mimetype_; |
| 141 |
QString mimeClass_; |
| 142 |
QString originalFilename_; |
| 143 |
QString blurhash_; |
| 144 |
QString thumbnailUrl_; |
| 145 |
QString url_; |
| 146 |
std::optional<mtx::crypto::EncryptedFile> encryptedFile, thumbnailEncryptedFile; |
| 147 |
|
| 148 |
QImage thumbnail_; |
| 149 |
|
| 150 |
QSize dimensions_; |
| 151 |
uint64_t size_ = 0; |
| 152 |
uint64_t thumbnailSize_ = 0; |
| 153 |
uint64_t duration_ = 0; |
| 154 |
bool encrypt_; |
| 155 |
}; |
| 156 |
|
| 157 |
class InputBar final : public QObject |
| 158 |
{ |
| 159 |
Q_OBJECT |
| 160 |
Q_PROPERTY(bool uploading READ uploading NOTIFY uploadingChanged) |
| 161 |
Q_PROPERTY( |
| 162 |
bool containsInvalidCommand READ containsInvalidCommand NOTIFY containsInvalidCommandChanged) |
| 163 |
Q_PROPERTY(bool containsIncompleteCommand READ containsIncompleteCommand NOTIFY |
| 164 |
containsIncompleteCommandChanged) |
| 165 |
Q_PROPERTY(QString currentCommand READ currentCommand NOTIFY currentCommandChanged) |
| 166 |
Q_PROPERTY(QStringList mentions READ mentions NOTIFY mentionsChanged) |
| 167 |
Q_PROPERTY(QString text READ text NOTIFY textChanged) |
| 168 |
Q_PROPERTY(QVariantList uploads READ uploads NOTIFY uploadsChanged) |
| 169 |
|
| 170 |
public: |
| 171 |
explicit InputBar(TimelineModel *parent) |
| 172 |
: QObject() |
| 173 |
, room(parent) |
| 174 |
{ |
| 175 |
typingRefresh_.setInterval(10'000); |
| 176 |
typingRefresh_.setSingleShot(true); |
| 177 |
typingTimeout_.setInterval(5'000); |
| 178 |
typingTimeout_.setSingleShot(true); |
| 179 |
connect(&typingRefresh_, &QTimer::timeout, this, &InputBar::startTyping); |
| 180 |
connect(&typingTimeout_, &QTimer::timeout, this, &InputBar::stopTyping); |
| 181 |
} |
| 182 |
|
| 183 |
QVariantList uploads() const; |
| 184 |
|
| 185 |
public slots: |
| 186 |
[[nodiscard]] QString text() const; |
| 187 |
QString previousText(); |
| 188 |
QString nextText(); |
| 189 |
void setText(const QString &newText); |
| 190 |
|
| 191 |
[[nodiscard]] QStringList mentions() const { return mentions_; } |
| 192 |
void addMention(QString m, QString text); |
| 193 |
void removeMention(QString m); |
| 194 |
|
| 195 |
void storeForEdit() |
| 196 |
{ |
| 197 |
textBeforeEdit = text(); |
| 198 |
mentionsBefore = mentions_; |
| 199 |
mentionTextsBefore = mentionTexts_; |
| 200 |
containsAtRoomBefore = containsAtRoom_; |
| 201 |
emit mentionsChanged(); |
| 202 |
} |
| 203 |
void restoreAfterEdit() |
| 204 |
{ |
| 205 |
mentions_ = mentionsBefore; |
| 206 |
mentionTexts_ = mentionTextsBefore; |
| 207 |
containsAtRoom_ = containsAtRoomBefore; |
| 208 |
mentionsBefore.clear(); |
| 209 |
mentionTextsBefore.clear(); |
| 210 |
setText(textBeforeEdit); |
| 211 |
textBeforeEdit.clear(); |
| 212 |
emit mentionsChanged(); |
| 213 |
} |
| 214 |
void replaceMentions(QStringList newMentions, QStringList newMentionTexts) |
| 215 |
{ |
| 216 |
if (newMentions.size() != newMentionTexts.size()) |
| 217 |
return; |
| 218 |
|
| 219 |
mentions_ = newMentions; |
| 220 |
mentionTexts_ = newMentionTexts; |
| 221 |
|
| 222 |
this->containsAtRoom_ = newMentions.contains(u"@room"); |
| 223 |
|
| 224 |
emit mentionsChanged(); |
| 225 |
} |
| 226 |
|
| 227 |
bool containsInvalidCommand() const { return containsInvalidCommand_; } |
| 228 |
bool containsIncompleteCommand() const { return containsIncompleteCommand_; } |
| 229 |
QString currentCommand() const { return currentCommand_; } |
| 230 |
|
| 231 |
void send(); |
| 232 |
bool tryPasteAttachment(bool fromMouse); |
| 233 |
bool insertMimeData(const QMimeData *data); |
| 234 |
void updateState(int selectionStart, int selectionEnd, int cursorPosition, const QString &text); |
| 235 |
void openFileSelection(); |
| 236 |
[[nodiscard]] bool uploading() const { return uploading_; } |
| 237 |
void message(const QString &body, |
| 238 |
MarkdownOverride useMarkdown = MarkdownOverride::NOT_SPECIFIED, |
| 239 |
bool rainbowify = false); |
| 240 |
void reaction(const QString &reactedEvent, const QString &reactionKey); |
| 241 |
void sticker(QStringList descriptor); |
| 242 |
|
| 243 |
void acceptUploads(); |
| 244 |
void declineUploads(); |
| 245 |
|
| 246 |
private slots: |
| 247 |
void startTyping(); |
| 248 |
void stopTyping(); |
| 249 |
|
| 250 |
void finalizeUpload(MediaUpload *upload, const QString &url); |
| 251 |
void removeRunUpload(MediaUpload *upload); |
| 252 |
|
| 253 |
signals: |
| 254 |
void textChanged(QString newText); |
| 255 |
void uploadingChanged(bool value); |
| 256 |
void containsInvalidCommandChanged(); |
| 257 |
void mentionsChanged(); |
| 258 |
void containsIncompleteCommandChanged(); |
| 259 |
void currentCommandChanged(); |
| 260 |
void uploadsChanged(); |
| 261 |
|
| 262 |
private: |
| 263 |
void emote(const QString &body, bool rainbowify); |
| 264 |
void notice(const QString &body, bool rainbowify); |
| 265 |
void confetti(const QString &body, bool rainbowify); |
| 266 |
void rainfall(const QString &body); |
| 267 |
void customMsgtype(const QString &msgtype, const QString &body); |
| 268 |
bool command(const QString &name, QString args); |
| 269 |
void image(const QString &filename, |
| 270 |
const std::optional<mtx::crypto::EncryptedFile> &file, |
| 271 |
const QString &url, |
| 272 |
const QString &mime, |
| 273 |
uint64_t dsize, |
| 274 |
const QSize &dimensions, |
| 275 |
const std::optional<mtx::crypto::EncryptedFile> &thumbnailEncryptedFile, |
| 276 |
const QString &thumbnailUrl, |
| 277 |
uint64_t thumbnailSize, |
| 278 |
const QSize &thumbnailDimensions, |
| 279 |
const QString &blurhash); |
| 280 |
void file(const QString &filename, |
| 281 |
const std::optional<mtx::crypto::EncryptedFile> &encryptedFile, |
| 282 |
const QString &url, |
| 283 |
const QString &mime, |
| 284 |
uint64_t dsize); |
| 285 |
void audio(const QString &filename, |
| 286 |
const std::optional<mtx::crypto::EncryptedFile> &file, |
| 287 |
const QString &url, |
| 288 |
const QString &mime, |
| 289 |
uint64_t dsize, |
| 290 |
uint64_t duration); |
| 291 |
void video(const QString &filename, |
| 292 |
const std::optional<mtx::crypto::EncryptedFile> &file, |
| 293 |
const QString &url, |
| 294 |
const QString &mime, |
| 295 |
uint64_t dsize, |
| 296 |
uint64_t duration, |
| 297 |
const QSize &dimensions, |
| 298 |
const std::optional<mtx::crypto::EncryptedFile> &thumbnailEncryptedFile, |
| 299 |
const QString &thumbnailUrl, |
| 300 |
uint64_t thumbnailSize, |
| 301 |
const QSize &thumbnailDimensions, |
| 302 |
const QString &blurhash); |
| 303 |
|
| 304 |
QPair<QString, QString> getCommandAndArgs() const { return getCommandAndArgs(text()); } |
| 305 |
QPair<QString, QString> getCommandAndArgs(const QString ¤tText) const; |
| 306 |
mtx::common::Relations generateRelations() const; |
| 307 |
mtx::common::Mentions generateMentions() const; |
| 308 |
|
| 309 |
void startUploadFromPath(const QString &path); |
| 310 |
void startUploadFromMimeData(const QMimeData &source, const QString &format); |
| 311 |
void startUpload(std::unique_ptr<QIODevice> dev, const QString &orgPath, const QString &format); |
| 312 |
void setUploading(bool value) |
| 313 |
{ |
| 314 |
if (value != uploading_) { |
| 315 |
uploading_ = value; |
| 316 |
emit uploadingChanged(value); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
void updateTextContentProperties(const QString &t, bool textDeleted = false); |
| 321 |
|
| 322 |
void toggleIgnore(const QString &user, const bool ignored); |
| 323 |
void toggleInvitePermission(const QString &id, bool block); |
| 324 |
|
| 325 |
QTimer typingRefresh_; |
| 326 |
QTimer typingTimeout_; |
| 327 |
TimelineModel *room; |
| 328 |
std::deque<QString> history_; |
| 329 |
std::size_t history_index_ = 0; |
| 330 |
int selectionStart = 0, selectionEnd = 0, cursorPosition = 0; |
| 331 |
bool uploading_ = false; |
| 332 |
bool containsAtRoom_ = false; |
| 333 |
bool containsInvalidCommand_ = false; |
| 334 |
bool containsIncompleteCommand_ = false; |
| 335 |
QString currentCommand_; |
| 336 |
QStringList mentions_, mentionTexts_; |
| 337 |
|
| 338 |
QStringList mentionsBefore, mentionTextsBefore; |
| 339 |
QString textBeforeEdit; |
| 340 |
bool containsAtRoomBefore = false; |
| 341 |
|
| 342 |
using UploadHandle = std::unique_ptr<MediaUpload, DeleteLaterDeleter>; |
| 343 |
std::vector<UploadHandle> unconfirmedUploads; |
| 344 |
std::vector<UploadHandle> runningUploads; |
| 345 |
}; |