File indexing completed on 2024-06-16 04:50:10

0001 /*
0002     SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "akonadicore_debug.h"
0008 #include "asyncselectionhandler_p.h"
0009 #include "models/entitytreemodel.h"
0010 
0011 using namespace Akonadi;
0012 
0013 AsyncSelectionHandler::AsyncSelectionHandler(QAbstractItemModel *model, QObject *parent)
0014     : QObject(parent)
0015     , mModel(model)
0016 {
0017     Q_ASSERT(mModel);
0018 
0019     connect(mModel, &QAbstractItemModel::rowsInserted, this, &AsyncSelectionHandler::rowsInserted);
0020 }
0021 
0022 AsyncSelectionHandler::~AsyncSelectionHandler()
0023 {
0024 }
0025 
0026 bool AsyncSelectionHandler::scanSubTree(const QModelIndex &index, bool searchForItem)
0027 {
0028     if (searchForItem) {
0029         const Item::Id id = index.data(EntityTreeModel::ItemIdRole).toLongLong();
0030 
0031         if (mItem.id() == id) {
0032             Q_EMIT itemAvailable(index);
0033             return true;
0034         }
0035     } else {
0036         const Collection::Id id = index.data(EntityTreeModel::CollectionIdRole).toLongLong();
0037 
0038         if (mCollection.id() == id) {
0039             Q_EMIT collectionAvailable(index);
0040             return true;
0041         }
0042     }
0043 
0044     for (int row = 0; row < mModel->rowCount(index); ++row) {
0045         const QModelIndex childIndex = mModel->index(row, 0, index);
0046         // This should not normally happen, but if it does we end up in an endless loop
0047         if (!childIndex.isValid()) {
0048             qCWarning(AKONADICORE_LOG) << "Invalid child detected: " << index.data().toString();
0049             Q_ASSERT(false);
0050             return false;
0051         }
0052         if (scanSubTree(childIndex, searchForItem)) {
0053             return true;
0054         }
0055     }
0056 
0057     return false;
0058 }
0059 
0060 void AsyncSelectionHandler::waitForCollection(const Collection &collection)
0061 {
0062     mCollection = collection;
0063 
0064     scanSubTree(QModelIndex(), false);
0065 }
0066 
0067 void AsyncSelectionHandler::waitForItem(const Item &item)
0068 {
0069     mItem = item;
0070 
0071     scanSubTree(QModelIndex(), true);
0072 }
0073 
0074 void AsyncSelectionHandler::rowsInserted(const QModelIndex &parent, int start, int end)
0075 {
0076     for (int i = start; i <= end; ++i) {
0077         scanSubTree(mModel->index(i, 0, parent), false);
0078         scanSubTree(mModel->index(i, 0, parent), true);
0079     }
0080 }
0081 
0082 #include "moc_asyncselectionhandler_p.cpp"