filemanager/src/filemanager/src/fmfindthread.cpp
changeset 37 15bc28c9dd51
parent 14 1957042d8c7e
child 46 d58987eac7e8
equal deleted inserted replaced
16:ada7962b4308 37:15bc28c9dd51
    18 
    18 
    19 #include "fmfindthread.h"
    19 #include "fmfindthread.h"
    20 
    20 
    21 #include <QDir>
    21 #include <QDir>
    22 
    22 
    23 FmFindThread::FmFindThread( QStringList *r, QObject *parent )
    23 // current path, it may come from findDirs.first().entryInfoList()
       
    24 #define CurrentDir QString( "." )
       
    25 
       
    26 // parent path, it may come from findDirs.first().entryInfoList()
       
    27 #define ParentDir QString( ".." )
       
    28 
       
    29 // if got 5 result and have not send notify event, then send notify event
       
    30 const int notifyPerCount = 5;
       
    31 
       
    32 // if got notifyPerElapsedTime milliseconds and have not send notify event, then send notify event.
       
    33 const int notifyPerElapsedTime = 500;
       
    34 
       
    35 /*!
       
    36     \fn void found( const QStringList &dataList )
       
    37     This signal is emitted when some data has been found and \a dataList is provided as data list.
       
    38     Please connect this signal by Qt::BlockingQueuedConnection as dataList will be cleared immediately
       
    39 */
       
    40 
       
    41 FmFindThread::FmFindThread( QObject *parent )
    24     : QThread( parent )
    42     : QThread( parent )
    25 {
    43 {
    26     mResult = r;
    44     setPriority( LowPriority );
    27 }
    45 }
    28 
    46 
    29 FmFindThread::~FmFindThread()
    47 FmFindThread::~FmFindThread()
    30 {
    48 {
    31 }
    49 }
    56 }
    74 }
    57 
    75 
    58 void FmFindThread::run()
    76 void FmFindThread::run()
    59 {
    77 {
    60     mStop = false;
    78     mStop = false;
    61     setPriority( LowPriority );
    79     tempResultList.clear();
    62     if (findPattern.isEmpty() || !findPattern.isValid())
    80     if (findPattern.isEmpty() || !findPattern.isValid())
    63         return;
    81         return;
    64 
    82 
    65     QDir dir( mFindPath );
    83     QDir dir( mFindPath );
    66     if (!dir.exists())
    84     if (!dir.exists())
    71         return;
    89         return;
    72     }
    90     }
    73 
    91 
    74     QList<QDir> findDirs;
    92     QList<QDir> findDirs;
    75     findDirs.append( dir );
    93     findDirs.append( dir );
    76     count = 0;
       
    77     time.restart();
    94     time.restart();
    78     mStop = false;
    95     mStop = false;
    79     while (!findDirs.isEmpty()) {
    96     while (!findDirs.isEmpty()) {
    80         QFileInfoList infoList = findDirs.first().entryInfoList();
    97         QFileInfoList infoList = findDirs.first().entryInfoList();
    81         for (QFileInfoList::Iterator it = infoList.begin(); it != infoList.end(); ++it) {
    98         for (QFileInfoList::Iterator it = infoList.begin(); it != infoList.end(); ++it) {
    82 			QString name = it->fileName();
    99 			QString name = it->fileName();
    83 			QString absolutPath = it->absoluteFilePath();
   100 			QString absolutPath = it->absoluteFilePath();
    84             if (findPattern.exactMatch( it->fileName() )) {
   101             if (findPattern.exactMatch( it->fileName() )) {
    85                 mResult->append( it->absoluteFilePath() );
   102                 tempResultList.append( it->absoluteFilePath() );
    86                 ++count;
   103                 if (tempResultList.count() > notifyPerCount) {
    87                 if (count > 5)
       
    88                     emitFound();
   104                     emitFound();
    89                 if (time.elapsed() > 500 && count > 0)
   105                 } else if (time.elapsed() > notifyPerElapsedTime && tempResultList.count() > 0) {
    90                     emitFound();
   106                     emitFound();
       
   107                 }
    91             }
   108             }
    92 
   109 
    93             //We are stopped;
   110             //We are stopped;
    94             if (mStop) {
   111             if (mStop) {
    95                 if( count > 0 ) {
   112                 if( tempResultList.count() > 0 ) {
    96                     emitFound();
   113                     emitFound();
    97                 }
   114                 }
    98                 return;
   115                 return;
    99             }
   116             }
   100             
   117 
   101             if (it->isDir() && it->fileName() != ".." && it->fileName() != "." )
   118             // exclude directory named ".." and "."
       
   119             if (it->isDir() && it->fileName() != ParentDir && it->fileName() != CurrentDir ) {
   102                 findDirs.append( QDir( it->absoluteFilePath() ) );
   120                 findDirs.append( QDir( it->absoluteFilePath() ) );
       
   121             }
   103         }
   122         }
   104 
   123 
   105         findDirs.removeFirst();
   124         findDirs.removeFirst();
   106     }
   125     }
   107     
   126     
   108     if( count > 0 ) {
   127     emitFound();
   109         emitFound();
       
   110     }
       
   111 }
   128 }
   112 
   129 
       
   130 /*
       
   131     Emit signal "found" to send out found data
       
   132 */
   113 void FmFindThread::emitFound()
   133 void FmFindThread::emitFound()
   114 {
   134 {
   115     emit found( count );
   135     if( tempResultList.count() > 0 ) {
   116     count = 0;
   136         emit found( tempResultList );
   117     time.restart();
   137         tempResultList.clear();
       
   138         time.restart();
       
   139     }
   118 }
   140 }
   119 
   141 
   120 void FmFindThread::setLastResult( QStringList r )
   142 void FmFindThread::setLastResult( QStringList r )
   121 {
   143 {
   122     mLastResult = r;
   144     mLastResult = r;
   123 }
   145 }
   124 
   146 
       
   147 /*
       
   148     Find keyword in last result
       
   149     \sa setLastResult, this function must be called to set last result for findInResult
       
   150 */
   125 void FmFindThread::findInResult()
   151 void FmFindThread::findInResult()
   126 {
   152 {
   127     if( mFindPath.isEmpty() ){
   153     if( mFindPath.isEmpty() ){
   128         int count = mLastResult.count();
       
   129         for (QStringList::Iterator it = mLastResult.begin(); it != mLastResult.end(); ++it) { 
   154         for (QStringList::Iterator it = mLastResult.begin(); it != mLastResult.end(); ++it) { 
   130             if (mStop){
   155             if (mStop){
   131                 return;
   156                 return;
   132             }
   157             }
   133             QString absolutPath = (*it);
   158             QString absolutPath = (*it);
   134             QFileInfo fileInfo( absolutPath );
   159             QFileInfo fileInfo( absolutPath );
   135             QString fileName = fileInfo.fileName();
   160             QString fileName = fileInfo.fileName();
   136             
   161             
   137             if (findPattern.exactMatch( fileName ) ) {
   162             if (findPattern.exactMatch( fileName ) ) {
   138                 mResult->append( absolutPath );
   163                 tempResultList.append( absolutPath );
   139                 ++count;
   164                 if ( tempResultList.count() > notifyPerCount ) {
   140                 if (count > 5)
       
   141                     emitFound();
   165                     emitFound();
   142                 if (time.elapsed() > 500 && count > 0)
   166                 } else if (time.elapsed() > notifyPerElapsedTime && tempResultList.count() > 0) {
   143                    emitFound();
   167                     emitFound();
       
   168                 }
   144             }
   169             }
   145         }    
   170         }    
   146     }
   171     }
       
   172     emitFound();
   147 }
   173 }