Akonadi

asyncselectionhandler.cpp
1 /*
2  Copyright (c) 2009 Tobias Koenig <[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 "asyncselectionhandler_p.h"
21 #include "models/entitytreemodel.h"
22 #include "akonadicore_debug.h"
23 
24 using namespace Akonadi;
25 
26 AsyncSelectionHandler::AsyncSelectionHandler(QAbstractItemModel *model, QObject *parent)
27  : QObject(parent)
28  , mModel(model)
29 {
30  Q_ASSERT(mModel);
31 
32  connect(mModel, &QAbstractItemModel::rowsInserted, this, &AsyncSelectionHandler::rowsInserted);
33 }
34 
35 AsyncSelectionHandler::~AsyncSelectionHandler()
36 {
37 }
38 
39 bool AsyncSelectionHandler::scanSubTree(const QModelIndex &index, bool searchForItem)
40 {
41  if (searchForItem) {
42  const Item::Id id = index.data(EntityTreeModel::ItemIdRole).toLongLong();
43 
44  if (mItem.id() == id) {
45  Q_EMIT itemAvailable(index);
46  return true;
47  }
48  } else {
49  const Collection::Id id = index.data(EntityTreeModel::CollectionIdRole).toLongLong();
50 
51  if (mCollection.id() == id) {
52  Q_EMIT collectionAvailable(index);
53  return true;
54  }
55  }
56 
57  for (int row = 0; row < mModel->rowCount(index); ++row) {
58  const QModelIndex childIndex = mModel->index(row, 0, index);
59  //This should not normally happen, but if it does we end up in an endless loop
60  if (!childIndex.isValid()) {
61  qCWarning(AKONADICORE_LOG) << "Invalid child detected: " << index.data().toString();
62  Q_ASSERT(false);
63  return false;
64  }
65  if (scanSubTree(childIndex, searchForItem)) {
66  return true;
67  }
68  }
69 
70  return false;
71 }
72 
73 void AsyncSelectionHandler::waitForCollection(const Collection &collection)
74 {
75  mCollection = collection;
76 
77  scanSubTree(QModelIndex(), false);
78 }
79 
80 void AsyncSelectionHandler::waitForItem(const Item &item)
81 {
82  mItem = item;
83 
84  scanSubTree(QModelIndex(), true);
85 }
86 
87 void AsyncSelectionHandler::rowsInserted(const QModelIndex &parent, int start, int end)
88 {
89  for (int i = start; i <= end; ++i) {
90  scanSubTree(mModel->index(i, 0, parent), false);
91  scanSubTree(mModel->index(i, 0, parent), true);
92  }
93 }
94 
95 #include "moc_asyncselectionhandler_p.cpp"
qlonglong toLongLong(bool *ok) const const
Represents a collection of PIM items.
Definition: collection.h:76
qint64 Id
Describes the unique id type.
Definition: collection.h:82
bool isValid() const const
QVariant data(int role) const const
Helper integration between Akonadi and Qt.
void rowsInserted(const QModelIndex &parent, int first, int last)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon May 25 2020 22:46:08 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.