filemanager/src/filemanager/src/fmfindresultmodel.cpp
changeset 46 d58987eac7e8
parent 37 15bc28c9dd51
equal deleted inserted replaced
37:15bc28c9dd51 46:d58987eac7e8
    17  */
    17  */
    18 
    18 
    19 #include "fmfindresultmodel.h"
    19 #include "fmfindresultmodel.h"
    20 #include "fmfindthread.h"
    20 #include "fmfindthread.h"
    21 #include "fmfileiconprovider.h"
    21 #include "fmfileiconprovider.h"
       
    22 #include "fmcommon.h"
    22 
    23 
    23 #include <QDateTime>
    24 #include <QDateTime>
    24 
    25 
    25 #include <hbglobal.h>
    26 #include <hbglobal.h>
       
    27 
       
    28 // wait \a circularWaitingTimeForStartFind microsecond to try to start find again
       
    29 // Since find can not be started while last find is not stopped.
       
    30 const int circularWaitingTimeForStartFind = 200;
       
    31 
       
    32 // wait \a firstWaitingTimeForStartFind microsecond to start find when accept find from caller.
       
    33 // Means user can continuously input words for search, so that find will not immediately start after accept each input event.
       
    34 // Search will be started after \a firstWaitingTimeForStartFind time of last user input
       
    35 const int firstWaitingTimeForStartFind = 750;
    26 
    36 
    27 /*!
    37 /*!
    28     \fn void finished()
    38     \fn void finished()
    29     This signal is emitted when find is finished.
    39     This signal is emitted when find is finished.
    30 */
    40 */
    37 */
    47 */
    38 
    48 
    39 FmFindResultModel::FmFindResultModel( QObject *parent )
    49 FmFindResultModel::FmFindResultModel( QObject *parent )
    40     : QAbstractListModel( parent )
    50     : QAbstractListModel( parent )
    41 {
    51 {
       
    52     FM_LOG("FmFindResultModel::FmFindResultModel");
    42     init();
    53     init();
    43     connect( mFindThread, SIGNAL(finished()), this, SIGNAL(finished()) );
    54     connect( mFindThread, SIGNAL( finished()), this, SLOT(onThreadFinished()) );
    44 	connect( mFindThread, SIGNAL(found(QStringList)), this, SLOT(on_findThread_found( QStringList) ), Qt::BlockingQueuedConnection ); 
    55 	connect( mFindThread, SIGNAL(found(QStringList)), this, SLOT(on_findThread_found( QStringList) ), Qt::BlockingQueuedConnection ); 
       
    56     connect( &mTimer, SIGNAL(timeout()), this, SLOT(startFind()));
    45 }
    57 }
    46 
    58 
    47 FmFindResultModel::~FmFindResultModel()
    59 FmFindResultModel::~FmFindResultModel()
    48 {
    60 {
       
    61     FM_LOG("FmFindResultModel::~FmFindResultModel START");
       
    62     mFindThread->stop();
       
    63     if( mFindThread->isRunning() ) {
       
    64         mStopEventLoop.exec();
       
    65     }
    49 	delete mIconProvider;
    66 	delete mIconProvider;
       
    67     FM_LOG("FmFindResultModel::~FmFindResultModel END");
       
    68 }
       
    69 
       
    70 /*!
       
    71     Send find \a regExp and \a pathList to find queue
       
    72     last un-stopped find will be stopped and start new find in 0 - 2 seconeds.
       
    73 */
       
    74 void FmFindResultModel::find( const QRegExp &regExp, const QStringList &pathList )
       
    75 {
       
    76     mFindPath   = pathList;
       
    77     mRegExp     = regExp;
       
    78 
       
    79     mTimer.stop();
       
    80 
       
    81 	if (mFindThread->isRunning()) {
       
    82         mFindThread->stop();
       
    83     }
       
    84     mTimer.start( firstWaitingTimeForStartFind );
       
    85 }
       
    86 
       
    87 /*!
       
    88     Internal start entrance, will be triggered by timer
       
    89     Wait till last find stopped and start new find.
       
    90 */
       
    91 void FmFindResultModel::startFind()
       
    92 {
       
    93     mTimer.stop();
       
    94     if (mFindThread->isRunning()) {
       
    95         mFindThread->stop();
       
    96         mTimer.start( circularWaitingTimeForStartFind );
       
    97 		return;
       
    98     }
       
    99 
       
   100     // Find starting. Initialize context.
       
   101     mFindThread->setFindPathList( mFindPath );
       
   102     mFindThread->setPattern( mRegExp );
       
   103 	removeRows( 0, rowCount() );
       
   104     emit findStarted();
       
   105     mFindThread->start();
       
   106 }
       
   107 
       
   108 /*!
       
   109     Return if find is inprogress
       
   110 */
       
   111 bool FmFindResultModel::isFinding() const
       
   112 {
       
   113     return mFindThread->isRunning();
       
   114 }
       
   115 
       
   116 /*!
       
   117     Stop find
       
   118 */
       
   119 void FmFindResultModel::stopFind()
       
   120 {
       
   121     mFindThread->stop();
       
   122     if( mFindThread->isRunning() ) {
       
   123         mStopEventLoop.exec();
       
   124     }
       
   125 }
       
   126 
       
   127 /*
       
   128     Find finish slot.
       
   129 */
       
   130 void FmFindResultModel::onThreadFinished()
       
   131 {
       
   132     FM_LOG("FmFindResultModel::onThreadFinished");
       
   133 
       
   134     // close event loop so that blocked destructor and stopFind() can be released.
       
   135     mStopEventLoop.exit();
       
   136 
       
   137     emit findFinished();
    50 }
   138 }
    51 
   139 
    52 /*!
   140 /*!
    53     Returns the number of rows in the model. This value corresponds to the
   141     Returns the number of rows in the model. This value corresponds to the
    54     number of items in the model's internal string list.
   142     number of items in the model's internal string list.
    65         return mFindResult.count();
   153         return mFindResult.count();
    66 
   154 
    67     return 0;
   155     return 0;
    68 }
   156 }
    69 
   157 
       
   158 /*!
       
   159    Get model column count
       
   160 */
    70 int FmFindResultModel::columnCount( const QModelIndex &parent ) const
   161 int FmFindResultModel::columnCount( const QModelIndex &parent ) const
    71 {
   162 {
    72     if ( !parent.isValid() )
   163     if ( !parent.isValid() )
    73         return 4;
   164         return 4;
    74     
   165     
    75     return 0;
   166     return 0;
    76 }
   167 }
    77 
   168 
       
   169 /*!
       
   170    Get model data
       
   171 */
    78 QVariant FmFindResultModel::data( const QModelIndex &index, int role ) const
   172 QVariant FmFindResultModel::data( const QModelIndex &index, int role ) const
    79 {
   173 {
    80     if (!indexValid( index ))
   174     if (!indexValid( index ))
    81         return QVariant();
   175         return QVariant();
    82 
   176 
   103     }
   197     }
   104 
   198 
   105     return QVariant();
   199     return QVariant();
   106 }
   200 }
   107 
   201 
       
   202 /*!
       
   203    Get filePath by \a index
       
   204 */
   108 QString FmFindResultModel::filePath ( const QModelIndex & index ) const
   205 QString FmFindResultModel::filePath ( const QModelIndex & index ) const
   109 {
   206 {
   110    return fileInfo( index ).filePath();
   207    return fileInfo( index ).filePath();
   111 }
   208 }
   112 
   209 
       
   210 /*!
       
   211    Get header data by column number \a section
       
   212 */
   113 QVariant FmFindResultModel::headerData( int section, Qt::Orientation orientation, int role ) const
   213 QVariant FmFindResultModel::headerData( int section, Qt::Orientation orientation, int role ) const
   114 {
   214 {
   115     if (orientation == Qt::Horizontal) {
   215     if (orientation == Qt::Horizontal) {
   116         if (role != Qt::DisplayRole)
   216         if (role != Qt::DisplayRole)
   117             return QVariant();
   217             return QVariant();
   182     emit modelCountChanged( rowCount() );
   282     emit modelCountChanged( rowCount() );
   183 
   283 
   184 	return true;
   284 	return true;
   185 }
   285 }
   186 
   286 
       
   287 /*!
       
   288     Get QFileInfo by \a index
       
   289 */
   187 QFileInfo FmFindResultModel::fileInfo( const QModelIndex &index ) const
   290 QFileInfo FmFindResultModel::fileInfo( const QModelIndex &index ) const
   188 {
   291 {
   189     if (index.row() >= 0 && index.row() < mFindResult.size())
   292     if (index.row() >= 0 && index.row() < mFindResult.size())
   190         return QFileInfo( mFindResult[index.row()] );
   293         return QFileInfo( mFindResult[index.row()] );
   191     else
   294     else
   192         return QFileInfo();
   295         return QFileInfo();
   193 }
   296 }
   194 
   297 
   195 QString FmFindResultModel::findPath() const
       
   196 {
       
   197     return mFindThread->findPath();
       
   198 }
       
   199 
       
   200 void FmFindResultModel::setFindPath( const QString &path )
       
   201 {
       
   202     mFindThread->setFindPath( path );
       
   203 }
       
   204 
       
   205 QRegExp FmFindResultModel::pattern() const
       
   206 {
       
   207     return mFindThread->pattern();
       
   208 }
       
   209 
       
   210 void FmFindResultModel::setPattern( const QRegExp &regExp )
       
   211 {
       
   212     mFindThread->setPattern( regExp );
       
   213 }
       
   214 
       
   215 void FmFindResultModel::find()
       
   216 {
       
   217 	if(mFindThread->isRunning())
       
   218 		return;
       
   219 
       
   220     if( findPath().isEmpty() ){
       
   221         mFindThread->setLastResult( mFindResult );
       
   222     }
       
   223 	removeRows( 0, rowCount() );
       
   224     mFindThread->start();
       
   225 }
       
   226 
       
   227 void FmFindResultModel::stop()
       
   228 {
       
   229     mFindThread->stop();
       
   230     mFindThread->wait();
       
   231 }
       
   232 
       
   233 bool FmFindResultModel::isFinding() const
       
   234 {
       
   235     return mFindThread->isRunning();
       
   236 }
       
   237 
       
   238 /*
   298 /*
   239     Receive \a dataList as some found result
   299     Receive \a dataList as some found result
   240     Then insert dataList to model
   300     Then insert dataList to model
   241 */
   301 */
   242 void FmFindResultModel::on_findThread_found( const QStringList &dataList )
   302 void FmFindResultModel::on_findThread_found( const QStringList &dataList )
   245 		return;
   305 		return;
   246     }
   306     }
   247     insertRows( rowCount(), dataList );
   307     insertRows( rowCount(), dataList );
   248 }
   308 }
   249 
   309 
       
   310 /*
       
   311     Get if \a index is valid
       
   312 */
   250 bool FmFindResultModel::indexValid( const QModelIndex &index ) const
   313 bool FmFindResultModel::indexValid( const QModelIndex &index ) const
   251 {
   314 {
   252     Q_UNUSED( index );
   315     if( ( index.row() < 0 ) || ( index.row() >= rowCount() ) ) {
   253     return true;
   316         return false;
   254 }
   317     } else if( ( index.column() < 0 ) || ( index.column() >= columnCount() ) ) {
   255 
   318         return false;
       
   319     } else {
       
   320         return true;
       
   321     }
       
   322 }
       
   323 
       
   324 /*
       
   325     Init model
       
   326 */
   256 void FmFindResultModel::init()
   327 void FmFindResultModel::init()
   257 {
   328 {
   258     mFindThread = new FmFindThread( this );
   329     mFindThread = new FmFindThread( this );
   259     mFindThread->setObjectName( "findThread" );
   330     mFindThread->setObjectName( "findThread" );
   260     mIconProvider = new FmFileIconProvider();
   331     mIconProvider = new FmFileIconProvider();
   261 }
   332 }
   262 
   333 
       
   334 /*
       
   335     Sort by name
       
   336 */
   263 bool FmFindResultModel::caseNameLessThan(const QPair<QString,int> &s1,
   337 bool FmFindResultModel::caseNameLessThan(const QPair<QString,int> &s1,
   264                                          const QPair<QString,int> &s2)
   338                                          const QPair<QString,int> &s2)
   265 {
   339 {
   266     QFileInfo info1( s1.first );
   340     QFileInfo info1( s1.first );
   267     QFileInfo info2( s2.first );
   341     QFileInfo info2( s2.first );
   268     
   342     
   269     return info1.fileName() < info2.fileName();
   343     return info1.fileName() < info2.fileName();
   270 }
   344 }
   271 
   345 
       
   346 /*
       
   347     Sort by time
       
   348 */
   272 bool FmFindResultModel::caseTimeLessThan(const QPair<QString,int> &s1,
   349 bool FmFindResultModel::caseTimeLessThan(const QPair<QString,int> &s1,
   273                                          const QPair<QString,int> &s2)
   350                                          const QPair<QString,int> &s2)
   274 {
   351 {
   275     QFileInfo info1( s1.first );
   352     QFileInfo info1( s1.first );
   276     QFileInfo info2( s2.first );
   353     QFileInfo info2( s2.first );
   277     
   354     
   278     return info1.lastModified() < info2.lastModified();
   355     return info1.lastModified() < info2.lastModified();
   279 }
   356 }
   280 
   357 
       
   358 /*
       
   359     Sort by size
       
   360 */
   281 bool FmFindResultModel::caseSizeLessThan(const QPair<QString,int> &s1,
   361 bool FmFindResultModel::caseSizeLessThan(const QPair<QString,int> &s1,
   282                                          const QPair<QString,int> &s2)
   362                                          const QPair<QString,int> &s2)
   283 {
   363 {
   284     QFileInfo info1( s1.first );
   364     QFileInfo info1( s1.first );
   285     QFileInfo info2( s2.first );
   365     QFileInfo info2( s2.first );
   286     
   366     
   287     return info1.size() < info2.size();
   367     return info1.size() < info2.size();
   288 }
   368 }
   289 
   369 
       
   370 /*
       
   371     Sort by type
       
   372 */
   290 bool FmFindResultModel::caseTypeLessThan(const QPair<QString,int> &s1,
   373 bool FmFindResultModel::caseTypeLessThan(const QPair<QString,int> &s1,
   291                                          const QPair<QString,int> &s2)
   374                                          const QPair<QString,int> &s2)
   292 {
   375 {
   293     QFileInfo info1( s1.first );
   376     QFileInfo info1( s1.first );
   294     QFileInfo info2( s2.first );
   377     QFileInfo info2( s2.first );