2021-07-21 14:37:57 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
#include "SingleImagePackModel.h"
|
|
|
|
|
2021-08-06 04:28:56 +03:00
|
|
|
#include <QFile>
|
|
|
|
#include <QMimeDatabase>
|
|
|
|
|
2021-07-21 14:37:57 +03:00
|
|
|
#include "Cache_p.h"
|
2021-08-06 02:45:47 +03:00
|
|
|
#include "ChatPage.h"
|
2021-08-06 04:28:56 +03:00
|
|
|
#include "Logging.h"
|
2021-07-21 14:37:57 +03:00
|
|
|
#include "MatrixClient.h"
|
2021-08-06 04:28:56 +03:00
|
|
|
#include "Utils.h"
|
2021-08-06 02:45:47 +03:00
|
|
|
#include "timeline/Permissions.h"
|
|
|
|
#include "timeline/TimelineModel.h"
|
|
|
|
|
2021-08-06 05:31:30 +03:00
|
|
|
Q_DECLARE_METATYPE(mtx::common::ImageInfo)
|
2021-07-21 14:37:57 +03:00
|
|
|
|
|
|
|
SingleImagePackModel::SingleImagePackModel(ImagePackInfo pack_, QObject *parent)
|
|
|
|
: QAbstractListModel(parent)
|
|
|
|
, roomid_(std::move(pack_.source_room))
|
|
|
|
, statekey_(std::move(pack_.state_key))
|
2021-08-06 02:45:47 +03:00
|
|
|
, old_statekey_(statekey_)
|
2021-07-21 14:37:57 +03:00
|
|
|
, pack(std::move(pack_.pack))
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
[[maybe_unused]] static auto imageInfoType = qRegisterMetaType<mtx::common::ImageInfo>();
|
2021-08-06 04:28:56 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!pack.pack)
|
|
|
|
pack.pack = mtx::events::msc2545::ImagePack::PackDescription{};
|
2021-07-21 14:37:57 +03:00
|
|
|
|
2021-12-29 08:01:38 +03:00
|
|
|
shortcodes.reserve(pack.images.size());
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &e : pack.images)
|
|
|
|
shortcodes.push_back(e.first);
|
2021-08-06 04:28:56 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
connect(this, &SingleImagePackModel::addImage, this, &SingleImagePackModel::addImageCb);
|
2021-12-13 08:00:32 +03:00
|
|
|
connect(this, &SingleImagePackModel::avatarUploaded, this, &SingleImagePackModel::setAvatarUrl);
|
2021-07-21 14:37:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SingleImagePackModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return (int)shortcodes.size();
|
2021-07-21 14:37:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray>
|
|
|
|
SingleImagePackModel::roleNames() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return {
|
|
|
|
{Roles::Url, "url"},
|
|
|
|
{Roles::ShortCode, "shortCode"},
|
|
|
|
{Roles::Body, "body"},
|
|
|
|
{Roles::IsEmote, "isEmote"},
|
|
|
|
{Roles::IsSticker, "isSticker"},
|
|
|
|
};
|
2021-07-21 14:37:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
SingleImagePackModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (hasIndex(index.row(), index.column(), index.parent())) {
|
|
|
|
const auto &img = pack.images.at(shortcodes.at(index.row()));
|
|
|
|
switch (role) {
|
|
|
|
case Url:
|
|
|
|
return QString::fromStdString(img.url);
|
|
|
|
case ShortCode:
|
|
|
|
return QString::fromStdString(shortcodes.at(index.row()));
|
|
|
|
case Body:
|
|
|
|
return QString::fromStdString(img.body);
|
|
|
|
case IsEmote:
|
|
|
|
return img.overrides_usage() ? img.is_emoji() : pack.pack->is_emoji();
|
|
|
|
case IsSticker:
|
|
|
|
return img.overrides_usage() ? img.is_sticker() : pack.pack->is_sticker();
|
|
|
|
default:
|
|
|
|
return {};
|
2021-07-21 14:37:57 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
return {};
|
2021-07-21 14:37:57 +03:00
|
|
|
}
|
|
|
|
|
2021-08-06 02:45:47 +03:00
|
|
|
bool
|
|
|
|
SingleImagePackModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using mtx::events::msc2545::PackUsage;
|
|
|
|
|
|
|
|
if (hasIndex(index.row(), index.column(), index.parent())) {
|
|
|
|
auto &img = pack.images.at(shortcodes.at(index.row()));
|
|
|
|
switch (role) {
|
|
|
|
case ShortCode: {
|
|
|
|
auto newCode = value.toString().toStdString();
|
|
|
|
|
|
|
|
// otherwise we delete this by accident
|
|
|
|
if (pack.images.count(newCode))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto tmp = img;
|
|
|
|
auto oldCode = shortcodes.at(index.row());
|
|
|
|
pack.images.erase(oldCode);
|
|
|
|
shortcodes[index.row()] = newCode;
|
|
|
|
pack.images.insert({newCode, tmp});
|
|
|
|
|
|
|
|
emit dataChanged(
|
|
|
|
this->index(index.row()), this->index(index.row()), {Roles::ShortCode});
|
|
|
|
return true;
|
2021-08-06 02:45:47 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
case Body:
|
|
|
|
img.body = value.toString().toStdString();
|
|
|
|
emit dataChanged(this->index(index.row()), this->index(index.row()), {Roles::Body});
|
|
|
|
return true;
|
|
|
|
case IsEmote: {
|
|
|
|
bool isEmote = value.toBool();
|
|
|
|
bool isSticker = img.overrides_usage() ? img.is_sticker() : pack.pack->is_sticker();
|
|
|
|
|
|
|
|
img.usage.set(PackUsage::Emoji, isEmote);
|
|
|
|
img.usage.set(PackUsage::Sticker, isSticker);
|
|
|
|
|
|
|
|
if (img.usage == pack.pack->usage)
|
|
|
|
img.usage.reset();
|
|
|
|
|
|
|
|
emit dataChanged(this->index(index.row()), this->index(index.row()), {Roles::IsEmote});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case IsSticker: {
|
|
|
|
bool isEmote = img.overrides_usage() ? img.is_emoji() : pack.pack->is_emoji();
|
|
|
|
bool isSticker = value.toBool();
|
|
|
|
|
|
|
|
img.usage.set(PackUsage::Emoji, isEmote);
|
|
|
|
img.usage.set(PackUsage::Sticker, isSticker);
|
|
|
|
|
|
|
|
if (img.usage == pack.pack->usage)
|
|
|
|
img.usage.reset();
|
|
|
|
|
|
|
|
emit dataChanged(
|
|
|
|
this->index(index.row()), this->index(index.row()), {Roles::IsSticker});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2021-08-06 02:45:47 +03:00
|
|
|
}
|
|
|
|
|
2021-07-21 14:37:57 +03:00
|
|
|
bool
|
|
|
|
SingleImagePackModel::isGloballyEnabled() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (auto roomPacks = cache::client()->getAccountData(mtx::events::EventType::ImagePackRooms)) {
|
|
|
|
if (auto tmp =
|
|
|
|
std::get_if<mtx::events::EphemeralEvent<mtx::events::msc2545::ImagePackRooms>>(
|
|
|
|
&*roomPacks)) {
|
|
|
|
if (tmp->content.rooms.count(roomid_) &&
|
|
|
|
tmp->content.rooms.at(roomid_).count(statekey_))
|
|
|
|
return true;
|
2021-07-21 14:37:57 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
return false;
|
2021-07-21 14:37:57 +03:00
|
|
|
}
|
|
|
|
void
|
|
|
|
SingleImagePackModel::setGloballyEnabled(bool enabled)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::events::msc2545::ImagePackRooms content{};
|
|
|
|
if (auto roomPacks = cache::client()->getAccountData(mtx::events::EventType::ImagePackRooms)) {
|
|
|
|
if (auto tmp =
|
|
|
|
std::get_if<mtx::events::EphemeralEvent<mtx::events::msc2545::ImagePackRooms>>(
|
|
|
|
&*roomPacks)) {
|
|
|
|
content = tmp->content;
|
2021-07-21 14:37:57 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-07-21 14:37:57 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (enabled)
|
|
|
|
content.rooms[roomid_][statekey_] = {};
|
|
|
|
else
|
|
|
|
content.rooms[roomid_].erase(statekey_);
|
2021-07-21 14:37:57 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
http::client()->put_account_data(content, [](mtx::http::RequestErr) {
|
|
|
|
// emit this->globallyEnabledChanged();
|
|
|
|
});
|
2021-07-21 14:37:57 +03:00
|
|
|
}
|
2021-08-06 02:45:47 +03:00
|
|
|
|
|
|
|
bool
|
|
|
|
SingleImagePackModel::canEdit() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (roomid_.empty())
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return Permissions(QString::fromStdString(roomid_))
|
|
|
|
.canChange(qml_mtx_events::ImagePackInRoom);
|
2021-08-06 02:45:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SingleImagePackModel::setPackname(QString val)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto val_ = val.toStdString();
|
|
|
|
if (val_ != this->pack.pack->display_name) {
|
|
|
|
this->pack.pack->display_name = val_;
|
|
|
|
emit packnameChanged();
|
|
|
|
}
|
2021-08-06 02:45:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SingleImagePackModel::setAttribution(QString val)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto val_ = val.toStdString();
|
|
|
|
if (val_ != this->pack.pack->attribution) {
|
|
|
|
this->pack.pack->attribution = val_;
|
|
|
|
emit attributionChanged();
|
|
|
|
}
|
2021-08-06 02:45:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SingleImagePackModel::setAvatarUrl(QString val)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto val_ = val.toStdString();
|
|
|
|
if (val_ != this->pack.pack->avatar_url) {
|
|
|
|
this->pack.pack->avatar_url = val_;
|
|
|
|
emit avatarUrlChanged();
|
|
|
|
}
|
2021-08-06 02:45:47 +03:00
|
|
|
}
|
|
|
|
|
2021-12-13 08:00:32 +03:00
|
|
|
QString
|
|
|
|
SingleImagePackModel::avatarUrl() const
|
|
|
|
{
|
|
|
|
if (!pack.pack->avatar_url.empty())
|
|
|
|
return QString::fromStdString(pack.pack->avatar_url);
|
|
|
|
else if (!pack.images.empty())
|
|
|
|
return QString::fromStdString(pack.images.begin()->second.url);
|
|
|
|
else
|
2021-12-29 06:28:08 +03:00
|
|
|
return QString();
|
2021-12-13 08:00:32 +03:00
|
|
|
}
|
|
|
|
|
2021-08-06 02:45:47 +03:00
|
|
|
void
|
|
|
|
SingleImagePackModel::setStatekey(QString val)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto val_ = val.toStdString();
|
|
|
|
if (val_ != statekey_) {
|
|
|
|
statekey_ = val_;
|
|
|
|
emit statekeyChanged();
|
|
|
|
}
|
2021-08-06 02:45:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SingleImagePackModel::setIsStickerPack(bool val)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using mtx::events::msc2545::PackUsage;
|
|
|
|
if (val != pack.pack->is_sticker()) {
|
|
|
|
pack.pack->usage.set(PackUsage::Sticker, val);
|
|
|
|
emit isStickerPackChanged();
|
|
|
|
}
|
2021-08-06 02:45:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SingleImagePackModel::setIsEmotePack(bool val)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using mtx::events::msc2545::PackUsage;
|
|
|
|
if (val != pack.pack->is_emoji()) {
|
|
|
|
pack.pack->usage.set(PackUsage::Emoji, val);
|
|
|
|
emit isEmotePackChanged();
|
|
|
|
}
|
2021-08-06 02:45:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SingleImagePackModel::save()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (roomid_.empty()) {
|
|
|
|
http::client()->put_account_data(pack, [](mtx::http::RequestErr e) {
|
|
|
|
if (e)
|
|
|
|
ChatPage::instance()->showNotification(
|
|
|
|
tr("Failed to update image pack: %1")
|
|
|
|
.arg(QString::fromStdString(e->matrix_error.error)));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (old_statekey_ != statekey_) {
|
|
|
|
http::client()->send_state_event(
|
|
|
|
roomid_,
|
|
|
|
to_string(mtx::events::EventType::ImagePackInRoom),
|
|
|
|
old_statekey_,
|
|
|
|
nlohmann::json::object(),
|
|
|
|
[](const mtx::responses::EventId &, mtx::http::RequestErr e) {
|
|
|
|
if (e)
|
|
|
|
ChatPage::instance()->showNotification(
|
|
|
|
tr("Failed to delete old image pack: %1")
|
|
|
|
.arg(QString::fromStdString(e->matrix_error.error)));
|
|
|
|
});
|
2021-08-06 02:45:47 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
http::client()->send_state_event(
|
|
|
|
roomid_,
|
|
|
|
statekey_,
|
|
|
|
pack,
|
|
|
|
[this](const mtx::responses::EventId &, mtx::http::RequestErr e) {
|
|
|
|
if (e)
|
|
|
|
ChatPage::instance()->showNotification(
|
|
|
|
tr("Failed to update image pack: %1")
|
|
|
|
.arg(QString::fromStdString(e->matrix_error.error)));
|
|
|
|
|
|
|
|
nhlog::net()->info("Uploaded image pack: %1", statekey_);
|
|
|
|
});
|
|
|
|
}
|
2021-08-06 02:45:47 +03:00
|
|
|
}
|
2021-08-06 04:28:56 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
SingleImagePackModel::addStickers(QList<QUrl> files)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &f : files) {
|
|
|
|
auto file = QFile(f.toLocalFile());
|
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
|
|
|
ChatPage::instance()->showNotification(
|
|
|
|
tr("Failed to open image: %1").arg(f.toLocalFile()));
|
|
|
|
return;
|
2021-08-06 04:28:56 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
auto bytes = file.readAll();
|
|
|
|
auto img = utils::readImage(bytes);
|
|
|
|
|
|
|
|
mtx::common::ImageInfo info{};
|
|
|
|
|
|
|
|
auto sz = img.size() / 2;
|
|
|
|
if (sz.width() > 512 || sz.height() > 512) {
|
|
|
|
sz.scale(512, 512, Qt::AspectRatioMode::KeepAspectRatio);
|
|
|
|
} else if (img.height() < 128 && img.width() < 128) {
|
|
|
|
sz = img.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
info.h = sz.height();
|
|
|
|
info.w = sz.width();
|
|
|
|
info.size = bytes.size();
|
|
|
|
info.mimetype = QMimeDatabase().mimeTypeForFile(f.toLocalFile()).name().toStdString();
|
|
|
|
|
|
|
|
auto filename = f.fileName().toStdString();
|
|
|
|
http::client()->upload(
|
|
|
|
bytes.toStdString(),
|
|
|
|
QMimeDatabase().mimeTypeForFile(f.toLocalFile()).name().toStdString(),
|
|
|
|
filename,
|
|
|
|
[this, filename, info](const mtx::responses::ContentURI &uri, mtx::http::RequestErr e) {
|
|
|
|
if (e) {
|
|
|
|
ChatPage::instance()->showNotification(
|
|
|
|
tr("Failed to upload image: %1")
|
|
|
|
.arg(QString::fromStdString(e->matrix_error.error)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit addImage(uri.content_uri, filename, info);
|
|
|
|
});
|
|
|
|
}
|
2021-08-06 04:28:56 +03:00
|
|
|
}
|
2021-08-29 18:22:49 +03:00
|
|
|
|
2021-12-13 08:00:32 +03:00
|
|
|
void
|
|
|
|
SingleImagePackModel::setAvatar(QUrl f)
|
|
|
|
{
|
|
|
|
auto file = QFile(f.toLocalFile());
|
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
|
|
|
ChatPage::instance()->showNotification(tr("Failed to open image: %1").arg(f.toLocalFile()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto bytes = file.readAll();
|
|
|
|
|
|
|
|
auto filename = f.fileName().toStdString();
|
|
|
|
http::client()->upload(
|
|
|
|
bytes.toStdString(),
|
|
|
|
QMimeDatabase().mimeTypeForFile(f.toLocalFile()).name().toStdString(),
|
|
|
|
filename,
|
|
|
|
[this, filename](const mtx::responses::ContentURI &uri, mtx::http::RequestErr e) {
|
|
|
|
if (e) {
|
|
|
|
ChatPage::instance()->showNotification(
|
|
|
|
tr("Failed to upload image: %1")
|
|
|
|
.arg(QString::fromStdString(e->matrix_error.error)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit avatarUploaded(QString::fromStdString(uri.content_uri));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-29 18:22:49 +03:00
|
|
|
void
|
|
|
|
SingleImagePackModel::remove(int idx)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (idx < (int)shortcodes.size() && idx >= 0) {
|
|
|
|
beginRemoveRows(QModelIndex(), idx, idx);
|
|
|
|
auto s = shortcodes.at(idx);
|
|
|
|
shortcodes.erase(shortcodes.begin() + idx);
|
|
|
|
pack.images.erase(s);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
2021-08-29 18:22:49 +03:00
|
|
|
}
|
|
|
|
|
2021-08-06 04:28:56 +03:00
|
|
|
void
|
|
|
|
SingleImagePackModel::addImageCb(std::string uri, std::string filename, mtx::common::ImageInfo info)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::events::msc2545::PackImage img{};
|
|
|
|
img.url = uri;
|
|
|
|
img.info = info;
|
|
|
|
beginInsertRows(
|
|
|
|
QModelIndex(), static_cast<int>(shortcodes.size()), static_cast<int>(shortcodes.size()));
|
2021-08-06 04:28:56 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
pack.images[filename] = img;
|
|
|
|
shortcodes.push_back(filename);
|
2021-08-06 04:28:56 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
endInsertRows();
|
2021-12-13 08:00:32 +03:00
|
|
|
|
|
|
|
if (this->pack.pack->avatar_url.empty())
|
|
|
|
this->setAvatarUrl(QString::fromStdString(uri));
|
2021-08-06 04:28:56 +03:00
|
|
|
}
|