Akonadi

partfetcher.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "partfetcher.h"
8
9#include "entitytreemodel.h"
10#include "itemfetchjob.h"
11#include "itemfetchscope.h"
12#include "session.h"
13#include <KLocalizedString>
14
15Q_DECLARE_METATYPE(QSet<QByteArray>)
16
17using namespace Akonadi;
18
19namespace Akonadi
20{
21class PartFetcherPrivate
22{
23 PartFetcherPrivate(PartFetcher *partFetcher, const QModelIndex &index, const QByteArray &partName)
24 : m_persistentIndex(index)
25 , m_partName(partName)
26 , q_ptr(partFetcher)
27 {
28 }
29
30 void fetchJobDone(KJob *job);
31
32 void modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
33
34 QPersistentModelIndex m_persistentIndex;
35 QByteArray m_partName;
36 Item m_item;
37
38 Q_DECLARE_PUBLIC(PartFetcher)
39 PartFetcher *q_ptr;
40};
41
42} // namespace Akonadi
43
44void PartFetcherPrivate::fetchJobDone(KJob *job)
45{
46 Q_Q(PartFetcher);
47 if (job->error()) {
48 q->setError(KJob::UserDefinedError);
49 q->setErrorText(i18n("Unable to fetch item for index"));
50 q->emitResult();
51 return;
52 }
53
54 auto fetchJob = qobject_cast<ItemFetchJob *>(job);
55
56 const Item::List list = fetchJob->items();
57
58 Q_ASSERT(list.size() == 1);
59
60 // If m_persistentIndex comes from a selection proxy model, it could become
61 // invalid if the user clicks around a lot.
62 if (!m_persistentIndex.isValid()) {
63 q->setError(KJob::UserDefinedError);
64 q->setErrorText(i18n("Index is no longer available"));
65 q->emitResult();
66 return;
67 }
68
69 const auto loadedParts = m_persistentIndex.data(EntityTreeModel::LoadedPartsRole).value<QSet<QByteArray>>();
70
71 Q_ASSERT(!loadedParts.contains(m_partName));
72
73 Item item = m_persistentIndex.data(EntityTreeModel::ItemRole).value<Item>();
74
75 item.apply(list.at(0));
76
77 auto model = const_cast<QAbstractItemModel *>(m_persistentIndex.model());
78
79 Q_ASSERT(model);
80
81 QVariant itemVariant = QVariant::fromValue(item);
82 model->setData(m_persistentIndex, itemVariant, EntityTreeModel::ItemRole);
83
84 m_item = item;
85
86 q->emitResult();
87}
88
89PartFetcher::PartFetcher(const QModelIndex &index, const QByteArray &partName, QObject *parent)
90 : KJob(parent)
91 , d_ptr(new PartFetcherPrivate(this, index, partName))
92{
93}
94
96
98{
100
101 const QModelIndex index = d->m_persistentIndex;
102
104
105 if (loadedParts.contains(d->m_partName)) {
106 d->m_item = d->m_persistentIndex.data(EntityTreeModel::ItemRole).value<Item>();
107 emitResult();
108 return;
109 }
110
112 if (!availableParts.contains(d->m_partName)) {
113 setError(UserDefinedError);
114 setErrorText(i18n("Payload part '%1' is not available for this index", QString::fromLatin1(d->m_partName)));
115 emitResult();
116 return;
117 }
118
119 auto session = qobject_cast<Akonadi::Session *>(qvariant_cast<QObject *>(index.data(EntityTreeModel::SessionRole)));
120
121 if (!session) {
122 setError(UserDefinedError);
123 setErrorText(i18n("No session available for this index"));
124 emitResult();
125 return;
126 }
127
129
130 if (!item.isValid()) {
131 setError(UserDefinedError);
132 setErrorText(i18n("No item available for this index"));
133 emitResult();
134 return;
135 }
136
137 ItemFetchScope scope;
138 scope.fetchPayloadPart(d->m_partName);
139 auto itemFetchJob = new Akonadi::ItemFetchJob(item, session);
140 itemFetchJob->setFetchScope(scope);
141 connect(itemFetchJob, &KJob::result, this, [d](KJob *job) {
142 d->fetchJobDone(job);
143 });
144}
145
147{
148 Q_D(const PartFetcher);
149
150 return d->m_persistentIndex;
151}
152
154{
155 Q_D(const PartFetcher);
156
157 return d->m_partName;
158}
159
161{
162 Q_D(const PartFetcher);
163
164 return d->m_item;
165}
166
167#include "moc_partfetcher.cpp"
@ AvailablePartsRole
Parts available in the Akonadi server for the item.
@ LoadedPartsRole
Parts available in the model for the item.
Job that fetches items from the Akonadi storage.
Specifies which parts of an item should be fetched from the Akonadi storage.
void fetchPayloadPart(const QByteArray &part, bool fetch=true)
Sets which payload parts shall be fetched.
Represents a PIM item stored in Akonadi storage.
Definition item.h:100
bool isValid() const
Returns whether the item is valid.
Definition item.cpp:88
Convenience class for getting payload parts from an Akonadi Model.
Definition partfetcher.h:61
void start() override
Starts the fetch operation.
Item item() const
Returns the item that contains the fetched payload part.
~PartFetcher() override
Destroys the part fetcher.
PartFetcher(const QModelIndex &index, const QByteArray &partName, QObject *parent=nullptr)
Creates a new part fetcher.
QByteArray partName() const
Returns the name of the part that has been fetched.
QModelIndex index() const
Returns the index of the item the part was fetched from.
void setErrorText(const QString &errorText)
void emitResult()
int error() const
void result(KJob *job)
void setError(int errorCode)
QString i18n(const char *text, const TYPE &arg...)
Helper integration between Akonadi and Qt.
KIOCORE_EXPORT QStringList list(const QString &fileClass)
const_reference at(qsizetype i) const const
qsizetype size() const const
QVariant data(int role) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
QVariant data(int role) const const
bool isValid() const const
const QAbstractItemModel * model() const const
QString fromLatin1(QByteArrayView str)
QVariant fromValue(T &&value)
T value() const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.