Akonadi

partfetcher.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Stephen Kelly <[email protected]>
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 
15 Q_DECLARE_METATYPE(QSet<QByteArray>)
16 
17 using namespace Akonadi;
18 
19 namespace Akonadi
20 {
21 class 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 
44 void 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 
89 PartFetcher::PartFetcher(const QModelIndex &index, const QByteArray &partName, QObject *parent)
90  : KJob(parent)
91  , d_ptr(new PartFetcherPrivate(this, index, partName))
92 {
93 }
94 
95 PartFetcher::~PartFetcher() = default;
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 
111  const auto availableParts = index.data(EntityTreeModel::AvailablePartsRole).value<QSet<QByteArray>>();
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"
bool isValid() const
Returns whether the item is valid.
Definition: item.cpp:88
~PartFetcher() override
Destroys the part fetcher.
void setErrorText(const QString &errorText)
QVariant fromValue(const T &value)
void result(KJob *job)
@ LoadedPartsRole
Parts available in the model for the item.
T value() const const
void fetchPayloadPart(const QByteArray &part, bool fetch=true)
Sets which payload parts shall be fetched.
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
Convenience class for getting payload parts from an Akonadi Model.
Definition: partfetcher.h:60
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QVector< Item > List
Describes a list of items.
Definition: item.h:115
void start() override
Starts the fetch operation.
Definition: partfetcher.cpp:97
QVariant data(int role) const const
int size() const const
QString i18n(const char *text, const TYPE &arg...)
const T & at(int i) const const
QByteArray partName() const
Returns the name of the part that has been fetched.
@ AvailablePartsRole
Parts available in the Akonadi server for the item.
PartFetcher(const QModelIndex &index, const QByteArray &partName, QObject *parent=nullptr)
Creates a new part fetcher.
Definition: partfetcher.cpp:89
QString fromLatin1(const char *str, int size)
Job that fetches items from the Akonadi storage.
Definition: itemfetchjob.h:69
void emitResult()
int error() const
void apply(const Item &other)
Applies the parts of Item other to this item.
Definition: item.cpp:521
void setError(int errorCode)
Represents a PIM item stored in Akonadi storage.
Definition: item.h:104
Q_D(Todo)
QModelIndex index() const
Returns the index of the item the part was fetched from.
Specifies which parts of an item should be fetched from the Akonadi storage.
Helper integration between Akonadi and Qt.
Item item() const
Returns the item that contains the fetched payload part.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:48 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.