Use postFile from libqmatrixclient.
Remove crappy json code.
This commit is contained in:
parent
635dbbff39
commit
1c76f2653b
|
@ -26,6 +26,10 @@ SpectralRoom::SpectralRoom(Connection* connection, QString roomId,
|
||||||
connect(this, &SpectralRoom::highlightCountChanged, this,
|
connect(this, &SpectralRoom::highlightCountChanged, this,
|
||||||
&SpectralRoom::countChanged);
|
&SpectralRoom::countChanged);
|
||||||
connect(this, &Room::addedMessages, this, [=] { setBusy(false); });
|
connect(this, &Room::addedMessages, this, [=] { setBusy(false); });
|
||||||
|
connect(this, &Room::fileTransferCompleted, this, [=] {
|
||||||
|
setFileUploadingProgress(0);
|
||||||
|
setHasFileUploading(false);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
inline QString getMIME(const QUrl& fileUrl) {
|
inline QString getMIME(const QUrl& fileUrl) {
|
||||||
|
@ -37,69 +41,33 @@ inline QSize getImageSize(const QUrl& imageUrl) {
|
||||||
return reader.size();
|
return reader.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int getFileSize(const QUrl& url) {
|
|
||||||
QFileInfo info(url.toLocalFile());
|
|
||||||
return int(info.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
void SpectralRoom::chooseAndUploadFile() {
|
void SpectralRoom::chooseAndUploadFile() {
|
||||||
auto localFile = QFileDialog::getOpenFileUrl(Q_NULLPTR, tr("Save File as"));
|
auto localFile = QFileDialog::getOpenFileUrl(Q_NULLPTR, tr("Save File as"));
|
||||||
if (!localFile.isEmpty()) {
|
if (!localFile.isEmpty()) {
|
||||||
UploadContentJob* job =
|
QString txnID = postFile(localFile.fileName(), localFile, false);
|
||||||
connection()->uploadFile(localFile.toLocalFile(), getMIME(localFile));
|
|
||||||
if (isJobRunning(job)) {
|
|
||||||
setHasFileUploading(true);
|
setHasFileUploading(true);
|
||||||
connect(job, &BaseJob::uploadProgress, this,
|
connect(this, &Room::fileTransferCompleted,
|
||||||
[=](qint64 bytesSent, qint64 bytesTotal) {
|
[=](QString id, QUrl localFile, QUrl mxcUrl) {
|
||||||
if (bytesTotal != 0) {
|
if (id == txnID) {
|
||||||
setFileUploadingProgress(bytesSent * 100 / bytesTotal);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
connect(job, &BaseJob::success, this,
|
|
||||||
[=] { postFile(localFile, job->contentUri()); });
|
|
||||||
connect(job, &BaseJob::finished, this, [=] {
|
|
||||||
setHasFileUploading(false);
|
|
||||||
setFileUploadingProgress(0);
|
setFileUploadingProgress(0);
|
||||||
|
setHasFileUploading(false);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
connect(this, &Room::fileTransferFailed, [=](QString id, QString error) {
|
||||||
qDebug() << "Failed transfer.";
|
if (id == txnID) {
|
||||||
|
setFileUploadingProgress(0);
|
||||||
|
setHasFileUploading(false);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
connect(
|
||||||
|
this, &Room::fileTransferProgress,
|
||||||
|
[=](QString id, qint64 progress, qint64 total) {
|
||||||
|
if (id == txnID) {
|
||||||
|
qDebug() << "Progress:" << progress << total;
|
||||||
|
setFileUploadingProgress(int(float(progress) / float(total) * 100));
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
void SpectralRoom::postFile(const QUrl& localFile, const QUrl& mxcUrl) {
|
|
||||||
const QString mime = getMIME(localFile);
|
|
||||||
const QString fileName = localFile.fileName();
|
|
||||||
QString msgType = "m.file";
|
|
||||||
int fileSize = getFileSize(localFile);
|
|
||||||
QJsonObject json;
|
|
||||||
if (mime.startsWith("image")) {
|
|
||||||
msgType = "m.image";
|
|
||||||
QSize imageSize = getImageSize(localFile);
|
|
||||||
json = {{"msgtype", msgType},
|
|
||||||
{"body", fileName},
|
|
||||||
{"filename", fileName},
|
|
||||||
{"url", mxcUrl.url()},
|
|
||||||
{"info", QJsonObject{{"h", imageSize.height()},
|
|
||||||
{"w", imageSize.width()},
|
|
||||||
{"size", fileSize},
|
|
||||||
{"mimetype", mime},
|
|
||||||
{"thumbnail_url", mxcUrl.url()},
|
|
||||||
{"thumbnail_info",
|
|
||||||
QJsonObject{{"h", imageSize.height()},
|
|
||||||
{"w", imageSize.width()},
|
|
||||||
{"size", fileSize},
|
|
||||||
{"mimetype", mime}}}}}};
|
|
||||||
} else {
|
|
||||||
if (mime.startsWith("video")) msgType = "m.video";
|
|
||||||
if (mime.startsWith("audio")) msgType = "m.audio";
|
|
||||||
json = {{"msgtype", msgType},
|
|
||||||
{"body", fileName},
|
|
||||||
{"filename", fileName},
|
|
||||||
{"url", mxcUrl.url()},
|
|
||||||
{"info", QJsonObject{{"size", fileSize}, {"mimetype", mime}}}};
|
|
||||||
}
|
}
|
||||||
postJson("m.room.message", json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpectralRoom::saveFileAs(QString eventId) {
|
void SpectralRoom::saveFileAs(QString eventId) {
|
||||||
|
|
|
@ -85,8 +85,6 @@ class SpectralRoom : public Room {
|
||||||
|
|
||||||
bool m_busy = false;
|
bool m_busy = false;
|
||||||
|
|
||||||
void postFile(const QUrl& localFile, const QUrl& mxcUrl);
|
|
||||||
|
|
||||||
void checkForHighlights(const QMatrixClient::TimelineItem& ti);
|
void checkForHighlights(const QMatrixClient::TimelineItem& ti);
|
||||||
|
|
||||||
void onAddNewTimelineEvents(timeline_iter_t from) override;
|
void onAddNewTimelineEvents(timeline_iter_t from) override;
|
||||||
|
|
Loading…
Reference in New Issue