File indexing completed on 2024-12-01 07:42:52

0001 // SPDX-FileCopyrightText: 2021 kaniini <https://git.pleroma.social/kaniini>
0002 // SPDX-FileCopyrightText: 2021 Carl Schwan <carl@carlschwan.eu>
0003 // SPDX-License-Identifier: GPL-3.0-only
0004 
0005 #include "attachmenteditormodel.h"
0006 
0007 #include "account.h"
0008 
0009 AttachmentEditorModel::AttachmentEditorModel(QObject *parent, AbstractAccount *account)
0010     : QAbstractListModel(parent)
0011     , m_account(account)
0012 {
0013 }
0014 
0015 int AttachmentEditorModel::rowCount(const QModelIndex &parent) const
0016 {
0017     Q_UNUSED(parent);
0018     return m_attachments.size();
0019 }
0020 
0021 int AttachmentEditorModel::count() const
0022 {
0023     return rowCount({});
0024 }
0025 
0026 QVariant AttachmentEditorModel::data(const QModelIndex &index, int role) const
0027 {
0028     if (!index.isValid()) {
0029         return {};
0030     }
0031     const int row = index.row();
0032     const auto &attachment = m_attachments[row];
0033 
0034     switch (role) {
0035     case PreviewRole:
0036         return attachment->m_preview_url;
0037     case DescriptionRole:
0038         return attachment->description();
0039     case FocalXRole:
0040         return attachment->focusX();
0041     case FocalYRole:
0042         return attachment->focusY();
0043     }
0044 
0045     return {};
0046 }
0047 
0048 QHash<int, QByteArray> AttachmentEditorModel::roleNames() const
0049 {
0050     return {
0051         {PreviewRole, QByteArrayLiteral("preview")},
0052         {DescriptionRole, QByteArrayLiteral("description")},
0053         {FocalXRole, QByteArrayLiteral("focalX")},
0054         {FocalYRole, QByteArrayLiteral("focalY")},
0055     };
0056 }
0057 
0058 QNetworkReply *AttachmentEditorModel::append(const QString &filename)
0059 {
0060     if (rowCount({}) >= 4) {
0061         return nullptr;
0062     }
0063     return m_account->upload(QUrl(filename), [=](QNetworkReply *reply) {
0064         const auto doc = QJsonDocument::fromJson(reply->readAll());
0065 
0066         if (!doc.isObject()) {
0067             return;
0068         }
0069 
0070         beginInsertRows({}, m_attachments.count(), m_attachments.count());
0071         m_attachments.append(new Attachment{doc.object(), this});
0072         endInsertRows();
0073         Q_EMIT countChanged();
0074     });
0075 }
0076 
0077 void AttachmentEditorModel::appendExisting(Attachment *attachment)
0078 {
0079     beginInsertRows({}, m_attachments.count(), m_attachments.count());
0080     m_attachments.append(attachment);
0081     endInsertRows();
0082     Q_EMIT countChanged();
0083 }
0084 
0085 void AttachmentEditorModel::removeAttachment(int row)
0086 {
0087     beginRemoveRows({}, row, row);
0088     m_attachments.removeAt(row);
0089     endRemoveRows();
0090     Q_EMIT countChanged();
0091 }
0092 
0093 void AttachmentEditorModel::setDescription(int row, const QString &description)
0094 {
0095     auto &attachment = m_attachments[row];
0096     const auto id = attachment->id();
0097     attachment->setDescription(description);
0098 
0099     const auto attachementUrl = m_account->apiUrl(QStringLiteral("/api/v1/media/%1").arg(id));
0100     const QJsonObject obj{
0101         {QStringLiteral("description"), description},
0102     };
0103     const QJsonDocument doc(obj);
0104     m_account->put(attachementUrl, doc, true, this, nullptr);
0105     Q_EMIT dataChanged(index(row, 0), index(row, 0), {DescriptionRole});
0106 }
0107 
0108 void AttachmentEditorModel::setFocusPoint(int row, double x, double y)
0109 {
0110     auto &attachment = m_attachments[row];
0111     const auto id = attachment->id();
0112     attachment->setFocusX(x);
0113     attachment->setFocusY(y);
0114 
0115     const auto attachementUrl = m_account->apiUrl(QStringLiteral("/api/v1/media/%1").arg(id));
0116     const QJsonObject obj{
0117         {QStringLiteral("focus"), QStringLiteral("%1,%2").arg(x).arg(y)},
0118     };
0119     const QJsonDocument doc(obj);
0120     m_account->put(attachementUrl, doc, true, this, nullptr);
0121     Q_EMIT dataChanged(index(row, 0), index(row, 0), {FocalXRole, FocalYRole});
0122 }
0123 
0124 const QList<Attachment *> &AttachmentEditorModel::attachments() const
0125 {
0126     return m_attachments;
0127 }
0128 
0129 #include "moc_attachmenteditormodel.cpp"