Akonadi

asyncselectionhandler.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Tobias Koenig <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "akonadicore_debug.h"
8 #include "asyncselectionhandler_p.h"
9 #include "models/entitytreemodel.h"
10 
11 using namespace Akonadi;
12 
13 AsyncSelectionHandler::AsyncSelectionHandler(QAbstractItemModel *model, QObject *parent)
14  : QObject(parent)
15  , mModel(model)
16 {
17  Q_ASSERT(mModel);
18 
19  connect(mModel, &QAbstractItemModel::rowsInserted, this, &AsyncSelectionHandler::rowsInserted);
20 }
21 
22 AsyncSelectionHandler::~AsyncSelectionHandler()
23 {
24 }
25 
26 bool AsyncSelectionHandler::scanSubTree(const QModelIndex &index, bool searchForItem)
27 {
28  if (searchForItem) {
29  const Item::Id id = index.data(EntityTreeModel::ItemIdRole).toLongLong();
30 
31  if (mItem.id() == id) {
32  Q_EMIT itemAvailable(index);
33  return true;
34  }
35  } else {
36  const Collection::Id id = index.data(EntityTreeModel::CollectionIdRole).toLongLong();
37 
38  if (mCollection.id() == id) {
39  Q_EMIT collectionAvailable(index);
40  return true;
41  }
42  }
43 
44  for (int row = 0; row < mModel->rowCount(index); ++row) {
45  const QModelIndex childIndex = mModel->index(row, 0, index);
46  // This should not normally happen, but if it does we end up in an endless loop
47  if (!childIndex.isValid()) {
48  qCWarning(AKONADICORE_LOG) << "Invalid child detected: " << index.data().toString();
49  Q_ASSERT(false);
50  return false;
51  }
52  if (scanSubTree(childIndex, searchForItem)) {
53  return true;
54  }
55  }
56 
57  return false;
58 }
59 
60 void AsyncSelectionHandler::waitForCollection(const Collection &collection)
61 {
62  mCollection = collection;
63 
64  scanSubTree(QModelIndex(), false);
65 }
66 
67 void AsyncSelectionHandler::waitForItem(const Item &item)
68 {
69  mItem = item;
70 
71  scanSubTree(QModelIndex(), true);
72 }
73 
74 void AsyncSelectionHandler::rowsInserted(const QModelIndex &parent, int start, int end)
75 {
76  for (int i = start; i <= end; ++i) {
77  scanSubTree(mModel->index(i, 0, parent), false);
78  scanSubTree(mModel->index(i, 0, parent), true);
79  }
80 }
81 
82 #include "moc_asyncselectionhandler_p.cpp"
Q_SCRIPTABLE Q_NOREPLY void start()
Represents a collection of PIM items.
Definition: collection.h:61
qlonglong toLongLong(bool *ok) const const
QVariant data(int role) const const
bool isValid() const const
void rowsInserted(const QModelIndex &parent, int first, int last)
qint64 Id
Describes the unique id type.
Definition: collection.h:79
const QList< QKeySequence > & end()
Represents a PIM item stored in Akonadi storage.
Definition: item.h:104
QString toString() const const
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jun 30 2022 03:51:45 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.