asyncselectionhandler.cpp

00001 /*
00002     Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or modify it
00005     under the terms of the GNU Library General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or (at your
00007     option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful, but WITHOUT
00010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00012     License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to the
00016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301, USA.
00018 */
00019 
00020 #include "asyncselectionhandler_p.h"
00021 
00022 #include <akonadi/entitytreemodel.h>
00023 
00024 using namespace Akonadi;
00025 
00026 AsyncSelectionHandler::AsyncSelectionHandler( QAbstractItemModel *model, QObject *parent )
00027   : QObject( parent ), mModel( model )
00028 {
00029   Q_ASSERT( mModel );
00030 
00031   connect( mModel, SIGNAL( rowsInserted( const QModelIndex&, int, int ) ),
00032            this, SLOT( rowsInserted( const QModelIndex&, int, int ) ) );
00033 }
00034 
00035 AsyncSelectionHandler::~AsyncSelectionHandler()
00036 {
00037 }
00038 
00039 bool AsyncSelectionHandler::scanSubTree( const QModelIndex &index, bool searchForItem )
00040 {
00041   if ( searchForItem ) {
00042     const Item::Id id = index.data( EntityTreeModel::ItemIdRole ).toLongLong();
00043 
00044     if ( mItem.id() == id ) {
00045       emit itemAvailable( index );
00046       return true;
00047     }
00048   } else {
00049     const Collection::Id id = index.data( EntityTreeModel::CollectionIdRole ).toLongLong();
00050 
00051     if ( mCollection.id() == id ) {
00052       emit collectionAvailable( index );
00053       return true;
00054     }
00055   }
00056 
00057   for ( int row = 0; row < mModel->rowCount( index ); ++row ) {
00058     const QModelIndex childIndex = mModel->index( row, 0, index );
00059     if ( scanSubTree( childIndex, searchForItem ) )
00060       return true;
00061   }
00062 
00063   return false;
00064 }
00065 
00066 void AsyncSelectionHandler::waitForCollection( const Collection &collection )
00067 {
00068   mCollection = collection;
00069 
00070   scanSubTree( mModel->index( 0, 0 ), false );
00071 }
00072 
00073 void AsyncSelectionHandler::waitForItem( const Item &item )
00074 {
00075   mItem = item;
00076 
00077   scanSubTree( mModel->index( 0, 0 ), true );
00078 }
00079 
00080 void AsyncSelectionHandler::rowsInserted( const QModelIndex &parent, int start, int end )
00081 {
00082   for ( int i = start; i <= end; ++i ) {
00083     scanSubTree( mModel->index( i, 0, parent ), false );
00084     scanSubTree( mModel->index( i, 0, parent ), true );
00085   }
00086 }
00087 
00088 #include "asyncselectionhandler_p.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys