Akonadi

partfetcher.cpp
1 /*
2  Copyright (c) 2009 Stephen Kelly <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "partfetcher.h"
21 
22 #include "entitytreemodel.h"
23 #include "session.h"
24 #include "itemfetchjob.h"
25 #include "itemfetchscope.h"
26 #include <KLocalizedString>
27 
28 Q_DECLARE_METATYPE(QSet<QByteArray>)
29 
30 using namespace Akonadi;
31 
32 namespace Akonadi
33 {
34 
35 class PartFetcherPrivate
36 {
37  PartFetcherPrivate(PartFetcher *partFetcher, const QModelIndex &index, const QByteArray &partName)
38  : m_persistentIndex(index)
39  , m_partName(partName)
40  , q_ptr(partFetcher)
41  {
42  }
43 
44  void fetchJobDone(KJob *job);
45 
46  void modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
47 
48  QPersistentModelIndex m_persistentIndex;
49  QByteArray m_partName;
50  Item m_item;
51 
52  Q_DECLARE_PUBLIC(PartFetcher)
53  PartFetcher *q_ptr;
54 };
55 
56 }
57 
58 void PartFetcherPrivate::fetchJobDone(KJob *job)
59 {
60  Q_Q(PartFetcher);
61  if (job->error()) {
62  q->setError(KJob::UserDefinedError);
63  q->setErrorText(i18n("Unable to fetch item for index"));
64  q->emitResult();
65  return;
66  }
67 
68  ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob *>(job);
69 
70  const Item::List list = fetchJob->items();
71 
72  Q_ASSERT(list.size() == 1);
73 
74  // If m_persistentIndex comes from a selection proxy model, it could become
75  // invalid if the user clicks around a lot.
76  if (!m_persistentIndex.isValid()) {
77  q->setError(KJob::UserDefinedError);
78  q->setErrorText(i18n("Index is no longer available"));
79  q->emitResult();
80  return;
81  }
82 
83  const QSet<QByteArray> loadedParts = m_persistentIndex.data(EntityTreeModel::LoadedPartsRole).value<QSet<QByteArray> >();
84 
85  Q_ASSERT(!loadedParts.contains(m_partName));
86 
87  Item item = m_persistentIndex.data(EntityTreeModel::ItemRole).value<Item>();
88 
89  item.apply(list.at(0));
90 
91  QAbstractItemModel *model = const_cast<QAbstractItemModel *>(m_persistentIndex.model());
92 
93  Q_ASSERT(model);
94 
95  QVariant itemVariant = QVariant::fromValue(item);
96  model->setData(m_persistentIndex, itemVariant, EntityTreeModel::ItemRole);
97 
98  m_item = item;
99 
100  q->emitResult();
101 }
102 
103 PartFetcher::PartFetcher(const QModelIndex &index, const QByteArray &partName, QObject *parent)
104  : KJob(parent)
105  , d_ptr(new PartFetcherPrivate(this, index, partName))
106 {
107 }
108 
110 {
111  delete d_ptr;
112 }
113 
115 {
116  Q_D(PartFetcher);
117 
118  const QModelIndex index = d->m_persistentIndex;
119 
121 
122  if (loadedParts.contains(d->m_partName)) {
123  d->m_item = d->m_persistentIndex.data(EntityTreeModel::ItemRole).value<Item>();
124  emitResult();
125  return;
126  }
127 
129  if (!availableParts.contains(d->m_partName)) {
130  setError(UserDefinedError);
131  setErrorText(i18n("Payload part '%1' is not available for this index", QString::fromLatin1(d->m_partName)));
132  emitResult();
133  return;
134  }
135 
137 
138  if (!session) {
139  setError(UserDefinedError);
140  setErrorText(i18n("No session available for this index"));
141  emitResult();
142  return;
143  }
144 
145  const Akonadi::Item item = index.data(EntityTreeModel::ItemRole).value<Akonadi::Item>();
146 
147  if (!item.isValid()) {
148  setError(UserDefinedError);
149  setErrorText(i18n("No item available for this index"));
150  emitResult();
151  return;
152  }
153 
154  ItemFetchScope scope;
155  scope.fetchPayloadPart(d->m_partName);
156  ItemFetchJob *itemFetchJob = new Akonadi::ItemFetchJob(item, session);
157  itemFetchJob->setFetchScope(scope);
158 
159  connect(itemFetchJob, SIGNAL(result(KJob*)),
160  this, SLOT(fetchJobDone(KJob*)));
161 }
162 
164 {
165  Q_D(const PartFetcher);
166 
167  return d->m_persistentIndex;
168 }
169 
171 {
172  Q_D(const PartFetcher);
173 
174  return d->m_partName;
175 }
176 
177 Item PartFetcher::item() const
178 {
179  Q_D(const PartFetcher);
180 
181  return d->m_item;
182 }
183 
184 #include "moc_partfetcher.cpp"
Parts available in the model for the item.
void emitResult()
~PartFetcher() override
Destroys the part fetcher.
void setError(int errorCode)
T value() const const
const QAbstractItemModel * model() const const
void start() override
Starts the fetch operation.
Item::List items() const
Returns the fetched items.
void setErrorText(const QString &errorText)
bool isValid() const const
QByteArray partName() const
Returns the name of the part that has been fetched.
QVariant data(int role) const const
A communication session with the Akonadi storage.
Definition: core/session.h:67
void setFetchScope(const ItemFetchScope &fetchScope)
Sets the item fetch scope.
Specifies which parts of an item should be fetched from the Akonadi storage.
QVariant fromValue(const T &value)
bool contains(const T &value) const const
Item item() const
Returns the item that contains the fetched payload part.
QString i18n(const char *text, const TYPE &arg...)
Parts available in the Akonadi server for the item.
QVariant data(int role) const const
Helper integration between Akonadi and Qt.
Convenience class for getting payload parts from an Akonadi Model.
Definition: partfetcher.h:73
Job that fetches items from the Akonadi storage.
Definition: itemfetchjob.h:84
QModelIndex index() const
Returns the index of the item the part was fetched from.
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
QString fromLatin1(const char *str, int size)
void result(KJob *job)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
T qobject_cast(QObject *object)
void fetchPayloadPart(const QByteArray &part, bool fetch=true)
Sets which payload parts shall be fetched.
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed May 27 2020 22:43:39 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.