MauiKit Calendar

attachmentsmodel.cpp
1// SPDX-FileCopyrightText: 2021 Claudio Cambra <claudio.cambra@gmail.com>
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#include "attachmentsmodel.h"
5#include "kalendar_debug.h"
6#include <QDebug>
7#include <QMetaEnum>
8
9AttachmentsModel::AttachmentsModel(QObject *parent, KCalendarCore::Incidence::Ptr incidencePtr)
10 : QAbstractListModel(parent)
11 , m_incidence(incidencePtr)
12{
13 for (int i = 0; i < QMetaEnum::fromType<AttachmentsModel::Roles>().keyCount(); i++) {
15 const QString key = QLatin1String(roleNames().value(value));
16 m_dataRoles[key] = value;
17 }
18}
19
20KCalendarCore::Incidence::Ptr AttachmentsModel::incidencePtr()
21{
22 return m_incidence;
23}
24
25void AttachmentsModel::setIncidencePtr(KCalendarCore::Incidence::Ptr incidence)
26{
27 if (m_incidence == incidence) {
28 return;
29 }
30 m_incidence = incidence;
31 Q_EMIT incidencePtrChanged();
32 Q_EMIT attachmentsChanged();
34}
35
36KCalendarCore::Attachment::List AttachmentsModel::attachments()
37{
38 return m_incidence->attachments();
39}
40
41QVariantMap AttachmentsModel::dataroles()
42{
43 return m_dataRoles;
44}
45
46QVariant AttachmentsModel::data(const QModelIndex &idx, int role) const
47{
48 if (!hasIndex(idx.row(), idx.column())) {
49 return {};
50 }
51
52 KCalendarCore::Attachment attachment = m_incidence->attachments()[idx.row()];
53 switch (role) {
54 case AttachmentRole:
55 return QVariant::fromValue(attachment);
56 case LabelRole:
57 return attachment.label();
58 case MimeTypeRole:
59 return attachment.mimeType();
60 case IconNameRole: {
61 QMimeType type = m_mimeDb.mimeTypeForUrl(QUrl(attachment.uri()));
62 return type.iconName();
63 }
64 case DataRole:
65 return attachment.data(); // This is in bytes
66 case SizeRole:
67 return attachment.size();
68 case URIRole:
69 return attachment.uri();
70 default:
71 qCWarning(KALENDAR_LOG) << "Unknown role for attachment:" << QMetaEnum::fromType<Roles>().valueToKey(role);
72 return {};
73 }
74}
75
76QHash<int, QByteArray> AttachmentsModel::roleNames() const
77{
78 return {
79 {AttachmentRole, QByteArrayLiteral("attachment")},
80 {LabelRole, QByteArrayLiteral("attachmentLabel")},
81 {MimeTypeRole, QByteArrayLiteral("mimetype")},
82 {IconNameRole, QByteArrayLiteral("iconName")},
83 {DataRole, QByteArrayLiteral("data")},
84 {SizeRole, QByteArrayLiteral("size")},
85 {URIRole, QByteArrayLiteral("uri")},
86 };
87}
88
89int AttachmentsModel::rowCount(const QModelIndex &) const
90{
91 return m_incidence->attachments().size();
92}
93
94void AttachmentsModel::addAttachment(const QString &uri)
95{
96 const QMimeType type = m_mimeDb.mimeTypeForUrl(QUrl(uri));
97
98 KCalendarCore::Attachment attachment(uri);
99 attachment.setLabel(QUrl(uri).fileName());
100 attachment.setMimeType(type.name());
101 m_incidence->addAttachment(attachment);
102
103 Q_EMIT attachmentsChanged();
105}
106
107void AttachmentsModel::deleteAttachment(const QString &uri)
108{
109 KCalendarCore::Attachment::List attachments = m_incidence->attachments();
110
111 for (const auto &attachment : attachments) {
112 if (attachment.uri() == uri) {
113 attachments.removeAll(attachment);
114 break;
115 }
116 }
117
118 m_incidence->clearAttachments();
119
120 for (const auto &attachment : attachments) {
121 m_incidence->addAttachment(attachment);
122 }
123
124 Q_EMIT attachmentsChanged();
126}
QString mimeType() const
QByteArray data() const
void setMimeType(const QString &mime)
void setLabel(const QString &label)
AKONADI_CALENDAR_EXPORT KCalendarCore::Incidence::Ptr incidence(const Akonadi::Item &item)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
bool hasIndex(int row, int column, const QModelIndex &parent) const const
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
QMetaEnum fromType()
int value(int index) const const
const char * valueToKey(int value) const const
QMimeType mimeTypeForUrl(const QUrl &url) const const
int column() const const
int row() const const
Q_EMITQ_EMIT
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:35:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.