File indexing completed on 2024-06-23 04:42:34

0001 // SPDX-FileCopyrightText: 2021 Claudio Cambra <claudio.cambra@gmail.com>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #include "attachmentsmodel.h"
0005 #include "kalendar_debug.h"
0006 #include <QDebug>
0007 #include <QMetaEnum>
0008 
0009 AttachmentsModel::AttachmentsModel(QObject *parent, KCalendarCore::Incidence::Ptr incidencePtr)
0010     : QAbstractListModel(parent)
0011     , m_incidence(incidencePtr)
0012 {
0013     for (int i = 0; i < QMetaEnum::fromType<AttachmentsModel::Roles>().keyCount(); i++) {
0014         const int value = QMetaEnum::fromType<AttachmentsModel::Roles>().value(i);
0015         const QString key = QLatin1String(roleNames().value(value));
0016         m_dataRoles[key] = value;
0017     }
0018 }
0019 
0020 KCalendarCore::Incidence::Ptr AttachmentsModel::incidencePtr()
0021 {
0022     return m_incidence;
0023 }
0024 
0025 void AttachmentsModel::setIncidencePtr(KCalendarCore::Incidence::Ptr incidence)
0026 {
0027     if (m_incidence == incidence) {
0028         return;
0029     }
0030     m_incidence = incidence;
0031     Q_EMIT incidencePtrChanged();
0032     Q_EMIT attachmentsChanged();
0033     Q_EMIT layoutChanged();
0034 }
0035 
0036 KCalendarCore::Attachment::List AttachmentsModel::attachments()
0037 {
0038     return m_incidence->attachments();
0039 }
0040 
0041 QVariantMap AttachmentsModel::dataroles()
0042 {
0043     return m_dataRoles;
0044 }
0045 
0046 QVariant AttachmentsModel::data(const QModelIndex &idx, int role) const
0047 {
0048     if (!hasIndex(idx.row(), idx.column())) {
0049         return {};
0050     }
0051 
0052     KCalendarCore::Attachment attachment = m_incidence->attachments()[idx.row()];
0053     switch (role) {
0054     case AttachmentRole:
0055         return QVariant::fromValue(attachment);
0056     case LabelRole:
0057         return attachment.label();
0058     case MimeTypeRole:
0059         return attachment.mimeType();
0060     case IconNameRole: {
0061         QMimeType type = m_mimeDb.mimeTypeForUrl(QUrl(attachment.uri()));
0062         return type.iconName();
0063     }
0064     case DataRole:
0065         return attachment.data(); // This is in bytes
0066     case SizeRole:
0067         return attachment.size();
0068     case URIRole:
0069         return attachment.uri();
0070     default:
0071         qCWarning(KALENDAR_LOG) << "Unknown role for attachment:" << QMetaEnum::fromType<Roles>().valueToKey(role);
0072         return {};
0073     }
0074 }
0075 
0076 QHash<int, QByteArray> AttachmentsModel::roleNames() const
0077 {
0078     return {
0079         {AttachmentRole, QByteArrayLiteral("attachment")},
0080         {LabelRole, QByteArrayLiteral("attachmentLabel")},
0081         {MimeTypeRole, QByteArrayLiteral("mimetype")},
0082         {IconNameRole, QByteArrayLiteral("iconName")},
0083         {DataRole, QByteArrayLiteral("data")},
0084         {SizeRole, QByteArrayLiteral("size")},
0085         {URIRole, QByteArrayLiteral("uri")},
0086     };
0087 }
0088 
0089 int AttachmentsModel::rowCount(const QModelIndex &) const
0090 {
0091     return m_incidence->attachments().size();
0092 }
0093 
0094 void AttachmentsModel::addAttachment(const QString &uri)
0095 {
0096     const QMimeType type = m_mimeDb.mimeTypeForUrl(QUrl(uri));
0097 
0098     KCalendarCore::Attachment attachment(uri);
0099     attachment.setLabel(QUrl(uri).fileName());
0100     attachment.setMimeType(type.name());
0101     m_incidence->addAttachment(attachment);
0102 
0103     Q_EMIT attachmentsChanged();
0104     Q_EMIT layoutChanged();
0105 }
0106 
0107 void AttachmentsModel::deleteAttachment(const QString &uri)
0108 {
0109     KCalendarCore::Attachment::List attachments = m_incidence->attachments();
0110 
0111     for (const auto &attachment : attachments) {
0112         if (attachment.uri() == uri) {
0113             attachments.removeAll(attachment);
0114             break;
0115         }
0116     }
0117 
0118     m_incidence->clearAttachments();
0119 
0120     for (const auto &attachment : attachments) {
0121         m_incidence->addAttachment(attachment);
0122     }
0123 
0124     Q_EMIT attachmentsChanged();
0125     Q_EMIT layoutChanged();
0126 }