Skip to content
Snippets Groups Projects
Commit 8adec94f authored by Nicolas Werner's avatar Nicolas Werner
Browse files

[konheko] Show file messages and allow some basic downloads

parent 61204f7a
No related branches found
No related tags found
No related merge requests found
Pipeline #139 passed
...@@ -144,6 +144,14 @@ Item { ...@@ -144,6 +144,14 @@ Item {
} }
DelegateChoice {
roleValue: EventType.FileMessage
FileMessage {
}
}
DelegateChoice { DelegateChoice {
Placeholder { Placeholder {
} }
......
import QtQuick 2.0
import QtQuick.Layouts 1.0
import Sailfish.Silica 1.0
RowLayout {
IconButton {
id: downloadButton
property var downloader
icon.source: "image://theme/icon-m-cloud-download"
onClicked: downloader = mxcDownloader.downloadToDownloadFolder(model.Url, model.Body)
}
ColumnLayout {
Text {
id: body
text: model.Body
color: Theme.primaryColor
wrapMode: Text.Wrap
Layout.fillWidth: true
}
Text {
id: fileSize
text: model.FileSize
color: Theme.primaryColor
wrapMode: Text.Wrap
Layout.fillWidth: true
}
}
}
...@@ -46,6 +46,11 @@ template <class T> auto eventWidth(const mtx::events::RoomEvent<T> &e) -> declty ...@@ -46,6 +46,11 @@ template <class T> auto eventWidth(const mtx::events::RoomEvent<T> &e) -> declty
return e.content.info.w; return e.content.info.w;
} }
template <class T> uint64_t eventFileSize(const Event<T> &) { return 0; }
template <class T> auto eventFileSize(const mtx::events::RoomEvent<T> &e) -> decltype(e.content.info.size) {
return e.content.info.size;
}
template <class T> double eventPropHeight(const mtx::events::RoomEvent<T> &e) { template <class T> double eventPropHeight(const mtx::events::RoomEvent<T> &e) {
auto w = eventWidth(e); auto w = eventWidth(e);
auto h = eventHeight(e); auto h = eventHeight(e);
...@@ -627,6 +632,7 @@ QHash<int, QByteArray> Room::roleNames() const { ...@@ -627,6 +632,7 @@ QHash<int, QByteArray> Room::roleNames() const {
roles[ThumbnailUrl] = "ThumbnailUrl"; roles[ThumbnailUrl] = "ThumbnailUrl";
roles[Height] = "Height"; roles[Height] = "Height";
roles[Width] = "Width"; roles[Width] = "Width";
roles[FileSize] = "FileSize";
roles[ProportionalHeight] = "ProportionalHeight"; roles[ProportionalHeight] = "ProportionalHeight";
roles[IsSender] = "IsSender"; roles[IsSender] = "IsSender";
return roles; return roles;
...@@ -673,6 +679,22 @@ QVariant Room::data(const QModelIndex &index, int role) const { ...@@ -673,6 +679,22 @@ QVariant Room::data(const QModelIndex &index, int role) const {
return std::visit([](const auto &e) -> uint64_t { return eventHeight(e); }, event); return std::visit([](const auto &e) -> uint64_t { return eventHeight(e); }, event);
case Width: case Width:
return std::visit([](const auto &e) -> uint64_t { return eventWidth(e); }, event); return std::visit([](const auto &e) -> uint64_t { return eventWidth(e); }, event);
case FileSize: {
auto bytes = std::visit([](const auto &e) -> uint64_t { return eventFileSize(e); }, event);
// from nheko
constexpr static const char *units[] = {"B", "KiB", "MiB", "GiB", "TiB"};
constexpr static const int length = sizeof(units) / sizeof(units[0]);
int u = 0;
double size = static_cast<double>(bytes);
while (size >= 1024.0 && u < length) {
++u;
size /= 1024.0;
}
return QString::number(size, 'g', 4) + ' ' + units[u];
}
case ProportionalHeight: case ProportionalHeight:
return std::visit([](const auto &e) -> double { return eventPropHeight(e); }, event); return std::visit([](const auto &e) -> double { return eventPropHeight(e); }, event);
case IsSender: case IsSender:
......
...@@ -111,6 +111,7 @@ struct Room : public QAbstractListModel { ...@@ -111,6 +111,7 @@ struct Room : public QAbstractListModel {
ThumbnailUrl, ThumbnailUrl,
Height, Height,
Width, Width,
FileSize,
ProportionalHeight, ProportionalHeight,
IsSender, IsSender,
}; };
......
...@@ -51,3 +51,13 @@ bool MXCDownloader::cached(QString url) { ...@@ -51,3 +51,13 @@ bool MXCDownloader::cached(QString url) {
return cacheFile.exists(); return cacheFile.exists();
} }
Q_INVOKABLE QObject *MXCDownloader::downloadToDownloadFolder(QString url, QString filename) {
auto temp = new MXCDownloadResponse(url);
connect(temp, &MXCDownloadResponse::downloaded, temp, [filename](QString oldName) {
auto newName = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + "/" + filename;
QFile::rename(oldName, newName);
});
return temp;
}
...@@ -24,5 +24,6 @@ class MXCDownloader : public QObject { ...@@ -24,5 +24,6 @@ class MXCDownloader : public QObject {
public: public:
Q_INVOKABLE QObject *download(QString url) { return new MXCDownloadResponse(url); } Q_INVOKABLE QObject *download(QString url) { return new MXCDownloadResponse(url); }
Q_INVOKABLE QObject *downloadToDownloadFolder(QString url, QString filename);
Q_INVOKABLE bool cached(QString url); Q_INVOKABLE bool cached(QString url);
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment