Akonadi

asyncselectionhandler.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
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
11using namespace Akonadi;
12
13AsyncSelectionHandler::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
22AsyncSelectionHandler::~AsyncSelectionHandler()
23{
24}
25
26bool AsyncSelectionHandler::scanSubTree(const QModelIndex &index, bool searchForItem)
27{
28 if (searchForItem) {
30
31 if (mItem.id() == id) {
32 Q_EMIT itemAvailable(index);
33 return true;
34 }
35 } else {
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
60void AsyncSelectionHandler::waitForCollection(const Collection &collection)
61{
62 mCollection = collection;
63
64 scanSubTree(QModelIndex(), false);
65}
66
67void AsyncSelectionHandler::waitForItem(const Item &item)
68{
69 mItem = item;
70
71 scanSubTree(QModelIndex(), true);
72}
73
74void 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"
Represents a collection of PIM items.
Definition collection.h:62
qint64 Id
Describes the unique id type.
Definition collection.h:79
@ CollectionIdRole
The collection id.
qint64 Id
Describes the unique id type.
Definition item.h:105
Q_SCRIPTABLE Q_NOREPLY void start()
Helper integration between Akonadi and Qt.
const QList< QKeySequence > & end()
void rowsInserted(const QModelIndex &parent, int first, int last)
QVariant data(int role) const const
bool isValid() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
qlonglong toLongLong(bool *ok) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.