File indexing completed on 2024-11-24 04:50:36

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