filemanager/src/filemanager/src/fmfilebrowsewidget.cpp
changeset 25 b7bfdea70ca2
parent 16 ada7962b4308
child 27 df183af6b92f
--- a/filemanager/src/filemanager/src/fmfilebrowsewidget.cpp	Mon May 03 12:24:39 2010 +0300
+++ b/filemanager/src/filemanager/src/fmfilebrowsewidget.cpp	Fri Jun 25 17:08:34 2010 +0800
@@ -24,9 +24,10 @@
 #include "fmviewmanager.h"
 #include "fmfiledialog.h"
 #include "fmdlgutils.h"
+#include "fmfileiconprovider.h"
 
 #include <QFile>
-#include <QDirModel>
+#include <QFileSystemModel>
 #include <QSizePolicy>
 #include <QGraphicsLinearLayout>
 
@@ -36,9 +37,14 @@
 #include <hbabstractviewitem.h>
 #include <hbaction.h>
 #include <hbsearchpanel.h>
-#include <hbmessagebox.h>
 #include <hblabel.h>
 
+// These define comes from implementation of QFileSystemModel
+#define QFileSystemSortName 0
+#define QFileSystemSortSize 1 
+#define QFileSystemSortType 2
+#define QFileSystemSortTime 3
+
 FmFileBrowseWidget::FmFileBrowseWidget( HbWidget *parent, FmFileBrowseWidget::Style style )
     : HbWidget( parent ),
       mTreeView( 0 ),
@@ -50,7 +56,8 @@
       mFileBrowseStyle( style ),
       mCurrentItem( 0 ),
       mOperationService( 0 ),
-      mSearchPanel( 0 )
+      mSearchPanel( 0 ),
+      mListLongPressed( false )
 {
     initFileModel();
     initListView();
@@ -70,19 +77,11 @@
     //since there is a thread running in background
     //if the model destroy later, the thread might not quit properly.
 
-    if( mStyle == ListStyle || mStyle == TreeStyle ) {
-        QFileInfo oldFileInfo = mModel->fileInfo( mListView->rootIndex() );
-        if( oldFileInfo.exists() ) {
-            FmViewManager *viewManager = FmViewManager::viewManager();
-            if( viewManager ) {
-                viewManager->removeWatchPath( oldFileInfo.absoluteFilePath() );
-            }   
-        }
-    }
-
     mTreeView->setModel( 0 );
     mListView->setModel( 0 );
     delete mModel;
+    
+    delete mFileIconProvider;
 }
 
 QFileInfo FmFileBrowseWidget::currentPath() const
@@ -123,18 +122,35 @@
     QString logString = "FmFileBrowseWidget::setRootPath(" + pathName + ')';
     FmLogger::log( logString );
 
-    if( checkPathAndSetStyle( pathName ) ) {
-        mListView->setModel(0);
-        mTreeView->setModel(0);
-        delete mModel;
-        mModel = new QDirModel(this);
-        mListView->setModel(mModel);
-        mTreeView->setModel(mModel);
-        
-        mListView->setRootIndex( mModel->index( pathName ) );
-        mTreeView->setRootIndex( mModel->index( pathName ) );
-        FmViewManager::viewManager()->addWatchPath( pathName );
-    }
+    int err = checkPathAndSetStyle( pathName );
+    switch( err )
+        {
+        case FmErrNone:
+            {
+            mListView->setRootIndex( mModel->setRootPath( pathName ) );
+            emit currentPathChanged( pathName );
+            break;
+            }
+        case FmErrPathNotExist:
+            {
+            FmDlgUtils::information( hbTrId( "Path is not exist" ) );
+            break;
+            }
+        case FmErrDriveNotAvailable:
+            {
+            // do not take any action as widget set note in label already. 
+            break;
+            }
+        case FmErrDriveDenied:
+        case FmErrPathDenied:
+            {
+            FmDlgUtils::information( hbTrId( "Can not access" ) );
+            break;
+            }
+        default:
+            Q_ASSERT_X( false, "setRootPath", "please handle all error from isPathAccessabel" );
+            break;
+        }
     mCurrentDrive = pathName.left( 3 );
 }
 
@@ -207,14 +223,6 @@
 bool FmFileBrowseWidget::rename( const QString &oldName, const QString &newName )
 {
     return QFile::rename( oldName, newName );
-    /*
-    if (QFile::rename( oldName, newName )) {
-    QModelIndex index = mModel->index( newName );
-    mModel->refresh( index );
-    index = mModel->index( oldName );
-    mModel->refresh( index );
-    }
-    */
 }
 
 
@@ -222,11 +230,11 @@
 
 bool FmFileBrowseWidget::cdUp()
 {
-    if (mStyle == ListStyle) {
-        QModelIndex index = mListView->rootIndex().parent();
-        mModel->refresh(index);
-        if (index.isValid()) {
-            changeRootIndex( index );
+	if (mStyle == ListStyle) {
+        QModelIndex parentIndex = mListView->rootIndex().parent();
+		// QFileSystemModel will auto refresh for file/folder change
+        if (parentIndex.isValid()) {
+			changeRootIndex( parentIndex );
             return true;
         }
     }
@@ -236,17 +244,31 @@
 
 void FmFileBrowseWidget::on_list_activated( const QModelIndex &index )
 {
+    mActivatedModelIndex = index;
+    emit listActivated();
+}
+
+void FmFileBrowseWidget::on_listActivated()
+{
+    FmLogger::log("FmFileBrowseWidget::on_listActivated start");
+    if( mListLongPressed ) {
+        FmLogger::log("FmFileBrowseWidget::on_list_activated end because longPressed");
+        return;
+    }
     if (!mSelectable) {
-        if (mModel->isDir(index) ) {
-            changeRootIndex( index );
+        if (mModel->isDir(mActivatedModelIndex) ) {
+            FmLogger::log("FmFileBrowseWidget::on_list_activated changeRootIndex>>");
+            changeRootIndex( mActivatedModelIndex );
+            FmLogger::log("FmFileBrowseWidget::on_list_activated changeRootIndex<<");
         } else {
-            QString filePath( mModel->filePath( index ) );
+            QString filePath( mModel->filePath( mActivatedModelIndex ) );
             QFileInfo fileInfo( filePath );
             if ( fileInfo.isFile() ) {
                 mOperationService->syncLaunchFileOpen( filePath );
             }
         }
     }
+    FmLogger::log("FmFileBrowseWidget::on_listActivated end");
 }
 
 void FmFileBrowseWidget::on_tree_activated( const QModelIndex &index )
@@ -258,6 +280,7 @@
 
 void FmFileBrowseWidget::on_list_longPressed( HbAbstractViewItem *item, const QPointF &coords )
 {
+    mListLongPressed = true;
     HbMenu *contextMenu = new HbMenu();
     mCurrentItem = item;
     
@@ -267,7 +290,7 @@
     contextMenu->addAction( viewAction );
 
     connect( viewAction, SIGNAL( triggered() ),
-    this, SLOT( on_viewAction_triggered() ) );
+        this, SLOT( on_viewAction_triggered() ), Qt::QueuedConnection );
 
     //copy
     HbAction *copyAction = new HbAction();
@@ -276,7 +299,7 @@
     contextMenu->addAction( copyAction );
 
     connect( copyAction, SIGNAL( triggered() ),
-    this, SLOT( on_copyAction_triggered() ) );
+    this, SLOT( on_copyAction_triggered() ), Qt::QueuedConnection );
 
     
     QString filePath( mModel->filePath( item->modelIndex() ) );
@@ -291,7 +314,7 @@
         contextMenu->addAction( moveAction );
     
         connect( moveAction, SIGNAL( triggered() ),
-        this, SLOT( on_moveAction_triggered() ) );
+        this, SLOT( on_moveAction_triggered() ), Qt::QueuedConnection );
     
         //Delete
         HbAction *deleteAction = new HbAction();
@@ -300,7 +323,7 @@
         contextMenu->addAction( deleteAction );
     
         connect( deleteAction, SIGNAL( triggered() ),
-        this, SLOT( on_deleteAction_triggered() ) );
+        this, SLOT( on_deleteAction_triggered() ), Qt::QueuedConnection );
     
         //rename
         HbAction *renameAction = new HbAction();
@@ -309,7 +332,7 @@
         contextMenu->addAction( renameAction );
     
         connect( renameAction, SIGNAL( triggered() ),
-        this, SLOT( on_renameAction_triggered() ) );
+        this, SLOT( on_renameAction_triggered() ), Qt::QueuedConnection );
     }
     
 //    if( fileInfo.isFile() ){
@@ -322,7 +345,13 @@
 //        this, SLOT( on_sendAction_triggered() ) );
 //    }
     
-    contextMenu->exec( coords );     
+    contextMenu->setPreferredPos( coords );
+    contextMenu->open();
+}
+
+void FmFileBrowseWidget::on_list_pressed( const QModelIndex &  index )
+{
+    mListLongPressed = false;
 }
 
 void FmFileBrowseWidget::on_tree_longPressed( HbAbstractViewItem *item, const QPointF &coords )
@@ -357,6 +386,10 @@
 
     connect( mListView, SIGNAL( activated( const QModelIndex& ) ),
         this, SLOT( on_list_activated( const QModelIndex& ) ) );
+    connect( this, SIGNAL( listActivated() ),
+        this, SLOT( on_listActivated() ), Qt::QueuedConnection );
+    connect( mListView, SIGNAL( pressed( const QModelIndex & ) ),
+        this, SLOT( on_list_pressed( const QModelIndex & ) ) );
     connect( mListView, SIGNAL( longPressed( HbAbstractViewItem *, const QPointF & ) ),
         this, SLOT( on_list_longPressed( HbAbstractViewItem *, const QPointF & ) ) );
 }
@@ -375,9 +408,11 @@
 
 void FmFileBrowseWidget::initFileModel()
 {
-    mModel = new QDirModel( this );
+    mModel = new QFileSystemModel( this );
     mModel->setReadOnly( false );
-    disconnect( mModel, SIGNAL( rowsInserted( const QModelIndex &, int, int ) ), 0 ,0 );
+    
+    mFileIconProvider = new FmFileIconProvider();
+    mModel->setIconProvider( mFileIconProvider );
 }
 
 void FmFileBrowseWidget::initLayout()
@@ -397,7 +432,7 @@
     mSearchPanel->hide();
     
     connect( mSearchPanel, SIGNAL( searchOptionsClicked() ),
-        this, SLOT( on_searchPanel_searchOptionsClicked() ) );
+        this, SLOT( on_searchPanel_searchOptionsClicked() ), Qt::QueuedConnection );
     
     connect( mSearchPanel, SIGNAL( criteriaChanged( const QString & ) ),
         this, SLOT( on_searchPanel_criteriaChanged( const QString & ) ) );
@@ -416,19 +451,8 @@
 
 void FmFileBrowseWidget::changeRootIndex( const QModelIndex &index )
 {
-    QFileInfo oldFileInfo = mModel->fileInfo( mListView->rootIndex() );
-    FmViewManager::viewManager()->removeWatchPath( oldFileInfo.absoluteFilePath() );
-
-    mModel->refresh(index);
-    if ( mStyle == ListStyle ) {
-        mListView->setRootIndex( index );
-    } else if ( mStyle == TreeStyle ) {
-        mTreeView->setRootIndex( index );
-    }
-    QFileInfo fileInfo = mModel->fileInfo( mListView->rootIndex() );
-    QString string = fileInfo.absoluteFilePath();
-    emit currentPathChanged( string );
-    FmViewManager::viewManager()->addWatchPath( string );
+	QString filePath = mModel->fileInfo( index ).absoluteFilePath();
+	setRootPath( filePath );
 }
 
 bool FmFileBrowseWidget::isDriver(const QModelIndex &index) const
@@ -453,76 +477,91 @@
 
 void FmFileBrowseWidget::refreshModel( const QString& path )
 {
+    FmLogger::log( "FmFileBrowseWidget::refreshModel start" );
+    // This slot will be triggered when drive inserted/ejected
+	// Because filemanger do not notify dir/files changed yet( QFileSystem will auto refresh.)
     QString currPath( currentPath().absoluteFilePath() );
-    QString refreshPath( path );
+    
+    if( currPath.isEmpty() ) {
+        // label style and no data shown( dirve is not present or locked, or corrupt )
     
-    if( !currPath.isEmpty() ) {
-        if( refreshPath.isEmpty() ) {
-            refreshPath = currPath;
-        }
-        if(  !FmUtils::isPathEqual( refreshPath, currPath ) ) {
-            // no need refresh other path
-            return;
+        //set path as drive root, cause refresh, so that data can be shown when insert MMC in device.
+        setRootPath( mCurrentDrive );
+        // update title
+    } else {
+        // display drive data normally
+        // ignore path refresh event as QFileSystemModel will auto refresh.
+    
+        // Handle drive refresh event as drive may be ejected.
+        if( path.isEmpty() ) { // path is empty means drive is changed.
+            checkPathAndSetStyle( currPath );
         }
-        if( checkPathAndSetStyle( refreshPath ) ) {               
-            mModel->refresh( mModel->index( refreshPath ) );
-        } else {
-            FmViewManager *viewManager = FmViewManager::viewManager();
-            if( viewManager ) {
-                viewManager->removeWatchPath( currentPath().absoluteFilePath() );
-            } 
-        }
-    } else {
-        // current path is empty, so change root path to Drive root.
-        refreshPath = mCurrentDrive;
-        setRootPath( refreshPath );
-        emit setTitle( FmUtils::fillDriveVolume( mCurrentDrive, true ) );
     }
+    emit setTitle( FmUtils::fillDriveVolume( mCurrentDrive, true ) );
+    FmLogger::log( "FmFileBrowseWidget::refreshModel end" );
 }
 
-bool FmFileBrowseWidget::checkPathAndSetStyle( const QString& path )
+int FmFileBrowseWidget::checkPathAndSetStyle( const QString& path )
 {
-    if( !FmUtils::isPathAccessabel( path ) ){
-        QString driveName = FmUtils::getDriveNameFromPath( path );
-        FmDriverInfo::DriveState state = FmUtils::queryDriverInfo( driveName ).driveState();
-        
-        if( state & FmDriverInfo::EDriveLocked ) {
-            mEmptyTipLabel->setPlainText( hbTrId( "Memory Card is locked" ) );       
-        } else if( state & FmDriverInfo::EDriveNotPresent ) {
-            mEmptyTipLabel->setPlainText( hbTrId( "Memory Card is not present" ) );
-        } else if( state & FmDriverInfo::EDriveCorrupted ) {
-            mEmptyTipLabel->setPlainText( hbTrId( "Drive is Corrupted" ) );
-        } else {
-            mEmptyTipLabel->setPlainText( hbTrId( "Drive can not be opened " ) );
+    int err = FmUtils::isPathAccessabel( path );
+    switch( err )
+        {
+        case FmErrNone:
+            {
+            setStyle( mFileBrowseStyle );
+            emit setEmptyMenu( false );
+            break;
+            }
+        case FmErrDriveNotAvailable:
+            {
+            QString driveName = FmUtils::getDriveNameFromPath( path );
+            FmDriverInfo::DriveState state = FmUtils::queryDriverInfo( driveName ).driveState();
+            
+            if( state & FmDriverInfo::EDriveLocked ) {
+                mEmptyTipLabel->setPlainText( hbTrId( "Drive is locked" ) );       
+            } else if( state & FmDriverInfo::EDriveNotPresent ) {
+                mEmptyTipLabel->setPlainText( hbTrId( "Drive is not present" ) );
+            } else if( state & FmDriverInfo::EDriveCorrupted ) {
+                mEmptyTipLabel->setPlainText( hbTrId( "Drive is Corrupted" ) );
+            } else {
+                mEmptyTipLabel->setPlainText( hbTrId( "Drive can not be opened " ) );
+            }
+            setStyle( LabelStyle );
+            emit setEmptyMenu( true );
+            break;
+            }
+        case FmErrPathNotExist:
+        case FmErrDriveDenied:
+        case FmErrPathDenied:
+            {
+            // do not tack any action, error note shoule be shown by invoker.
+            // checkPathAndSetStyle just check path and set style.
+            break;
+            }
+        default:
+            Q_ASSERT_X( false, "checkPathAndSetStyle", "please handle all error from isPathAccessabel" );
+            break;
         }
-        setStyle( LabelStyle );
-        emit setEmptyMenu( true );
-        return false;
-    } else {
-        setStyle( mFileBrowseStyle );
-        emit setEmptyMenu( false );
-        return true;
-    }
-    
+    return err;
 }
 
 void FmFileBrowseWidget::sortFiles( TSortType sortType )
 {
     switch( sortType ){
         case ESortByName:{
-            mModel->setSorting( QDir::Name );
+			mModel->sort( QFileSystemSortName );
         }
             break;
         case ESortByTime:{
-            mModel->setSorting( QDir::Time );
+            mModel->sort( QFileSystemSortTime );
         }
             break;
         case ESortBySize:{
-            mModel->setSorting( QDir::Size );
+            mModel->sort( QFileSystemSortSize );
         }
             break;
         case ESortByType:{
-            mModel->setSorting( QDir::Type | QDir::DirsFirst );
+            mModel->sort( QFileSystemSortType );
         }
             break;
         default:
@@ -539,8 +578,8 @@
 
 void FmFileBrowseWidget::on_searchPanel_searchOptionsClicked()
 {
-    mFindTargetPath = FmFileDialog::getExistingDirectory( 0, hbTrId( "Look in:" ), QString(""),
-        QStringList() );
+    mFindTargetPath = FmUtils::fillPathWithSplash( FmFileDialog::getExistingDirectory( 0, hbTrId( "Look in:" ), QString(""),
+        QStringList() ) );
 }
 
 void FmFileBrowseWidget::on_searchPanel_criteriaChanged( const QString &criteria )
@@ -585,7 +624,7 @@
 {
     QStringList fileList;
     fileList.push_back( mModel->filePath( mCurrentItem->modelIndex() ) );
-    if ( HbMessageBox::question( hbTrId("Confirm Deletion?" ) )) {
+    if ( FmDlgUtils::question( hbTrId("Confirm Deletion?" ) )) {
         int ret = mOperationService->asyncRemove( fileList );
         switch( ret ) {
             case FmErrNone:
@@ -593,13 +632,13 @@
                 break;
             case FmErrAlreadyStarted:
                 // last operation have not finished
-                HbMessageBox::information( hbTrId( "Operatin already started!" ) );
+                FmDlgUtils::information( hbTrId( "Operatin already started!" ) );
                 break;
             case FmErrWrongParam:
-                HbMessageBox::information( hbTrId( "Wrong parameters!" ) );
+                FmDlgUtils::information( hbTrId( "Wrong parameters!" ) );
                 break;
             default:
-                HbMessageBox::information( hbTrId( "Operation fail to start!" ) );
+                FmDlgUtils::information( hbTrId( "Operation fail to start!" ) );
         }
     }
 }
@@ -622,13 +661,13 @@
                 break;
             case FmErrAlreadyStarted:
                 // last operation have not finished
-                HbMessageBox::information( hbTrId( "Operatin already started!" ) );
+                FmDlgUtils::information( hbTrId( "Operatin already started!" ) );
                 break;
             case FmErrWrongParam:
-                HbMessageBox::information( hbTrId( "Wrong parameters!" ) );
+                FmDlgUtils::information( hbTrId( "Wrong parameters!" ) );
                 break;
             default:
-                HbMessageBox::information( hbTrId( "Operation fail to start!" ) );
+                FmDlgUtils::information( hbTrId( "Operation fail to start!" ) );
         }
     }
 
@@ -639,7 +678,7 @@
     QStringList fileList;
     fileList.push_back( mModel->filePath( mCurrentItem->modelIndex() ) );
 
-    QString targetPathName = FmFileDialog::getExistingDirectory( 0, tr( "move to" ),
+    QString targetPathName = FmFileDialog::getExistingDirectory( 0, hbTrId( "move to" ),
             QString(""), QStringList() );
 
     if( !targetPathName.isEmpty() ) {
@@ -652,13 +691,13 @@
                 break;
             case FmErrAlreadyStarted:
                 // last operation have not finished
-                HbMessageBox::information( hbTrId( "Operatin already started!" ) );
+                FmDlgUtils::information( hbTrId( "Operatin already started!" ) );
                 break;
             case FmErrWrongParam:
-                HbMessageBox::information( hbTrId( "Wrong parameters!" ) );
+                FmDlgUtils::information( hbTrId( "Wrong parameters!" ) );
                 break;
             default:
-                HbMessageBox::information( hbTrId( "Operation fail to start!" ) );
+                FmDlgUtils::information( hbTrId( "Operation fail to start!" ) );
         }
     }
 }
@@ -668,21 +707,31 @@
 {
     QString filePath = mModel->filePath( mCurrentItem->modelIndex() );
     QFileInfo fileInfo = mModel->fileInfo( mCurrentItem->modelIndex() );
-
+    int maxFileNameLength = FmUtils::getMaxFileNameLength();
+    QString oldSuffix( fileInfo.suffix() );
     QString newName( fileInfo.fileName() );
-    
-    while( FmDlgUtils::showTextQuery( hbTrId( "Enter new name for %1" ).arg( newName ), newName, true ) ){
+    while( FmDlgUtils::showTextQuery( hbTrId( "Enter new name for %1" ).arg( newName ), newName, true,
+            maxFileNameLength, QString() , true ) ){
+        // remove whitespace from the start and the end.
+        newName = newName.trimmed();
         QString newTargetPath = FmUtils::fillPathWithSplash(
             fileInfo.absolutePath() ) + newName;
         QFileInfo newFileInfo( newTargetPath );
-        if( newFileInfo.exists() ) {
-            HbMessageBox::information( hbTrId( "%1 already exist!" ).arg( newName ) );
+        QString errString;
+        // check if name/path is available for use
+        if( !FmUtils::checkNewFolderOrFile( newTargetPath, errString ) ) {
+            FmDlgUtils::information( errString );
             continue;
         }
-
         if( !rename( fileInfo.absoluteFilePath(), newTargetPath ) ) {
-            HbMessageBox::information( hbTrId("Rename failed!") );
+            FmDlgUtils::information( hbTrId("Rename failed!") );
+        }
+        else {
+            if ( oldSuffix != newFileInfo.suffix() ) {
+                FmDlgUtils::information( hbTrId( "File may become unusable when file name extension is changed" ) );        
+            }   
         }
         break;
-    }   
+    }
 }
+