00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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( QModelIndex(), false );
00071 }
00072
00073 void AsyncSelectionHandler::waitForItem( const Item &item )
00074 {
00075 mItem = item;
00076
00077 scanSubTree( QModelIndex(), 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"