filemanager/src/inc/fmfilesystemproxymodel.cpp
author hgs
Mon, 18 Oct 2010 09:44:31 +0800
changeset 49 81668a704644
parent 47 12b82dc0e8db
permissions -rw-r--r--
201041
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* Initial Contributors:
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
*
hgs
parents:
diff changeset
    12
* Contributors:
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* Description: proxy model for QFileSystemModel
hgs
parents:
diff changeset
    15
*
hgs
parents:
diff changeset
    16
*/
hgs
parents:
diff changeset
    17
hgs
parents:
diff changeset
    18
#include "fmfilesystemproxymodel.h"
hgs
parents:
diff changeset
    19
#include "fmutils.h"
hgs
parents:
diff changeset
    20
#include <QFileSystemModel>
hgs
parents:
diff changeset
    21
hgs
parents:
diff changeset
    22
#include <hbglobal.h>
49
hgs
parents: 47
diff changeset
    23
#include <hbdirectorynamelocalizer.h>
47
hgs
parents:
diff changeset
    24
hgs
parents:
diff changeset
    25
// name column number, this define comes from implementation of QFileSystemModel
hgs
parents:
diff changeset
    26
const int NameColumn = 0;
hgs
parents:
diff changeset
    27
hgs
parents:
diff changeset
    28
/*!
hgs
parents:
diff changeset
    29
    Constructor
hgs
parents:
diff changeset
    30
*/
hgs
parents:
diff changeset
    31
FmFileSystemProxyModel::FmFileSystemProxyModel( QObject *parent ) :
49
hgs
parents: 47
diff changeset
    32
    QSortFilterProxyModel( parent ), localizer( 0 )
47
hgs
parents:
diff changeset
    33
{
49
hgs
parents: 47
diff changeset
    34
    localizer = new HbDirectoryNameLocalizer;
47
hgs
parents:
diff changeset
    35
}
hgs
parents:
diff changeset
    36
hgs
parents:
diff changeset
    37
/*!
hgs
parents:
diff changeset
    38
    Destructor
hgs
parents:
diff changeset
    39
*/
hgs
parents:
diff changeset
    40
FmFileSystemProxyModel::~FmFileSystemProxyModel()
hgs
parents:
diff changeset
    41
{
49
hgs
parents: 47
diff changeset
    42
    delete localizer;
47
hgs
parents:
diff changeset
    43
}
hgs
parents:
diff changeset
    44
                                                                                   
hgs
parents:
diff changeset
    45
/*!
hgs
parents:
diff changeset
    46
    return data by \a index and \a role
hgs
parents:
diff changeset
    47
    this function will localize the folder by HbDirectoryNameLocalizer
hgs
parents:
diff changeset
    48
*/
hgs
parents:
diff changeset
    49
QVariant FmFileSystemProxyModel::data ( const QModelIndex & index, int role ) const
hgs
parents:
diff changeset
    50
{
hgs
parents:
diff changeset
    51
    QAbstractItemModel *itemModel = sourceModel();
hgs
parents:
diff changeset
    52
    QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
hgs
parents:
diff changeset
    53
    if( sourceModel && ( role == Qt::DisplayRole ) ) {
49
hgs
parents: 47
diff changeset
    54
        // get absolute path
hgs
parents: 47
diff changeset
    55
        QString path( sourceModel->fileInfo( mapToSource( index ) ).absoluteFilePath() );
hgs
parents: 47
diff changeset
    56
        // get localized name
hgs
parents: 47
diff changeset
    57
        QString localizedName( localizer->translate( path ) );
hgs
parents: 47
diff changeset
    58
		
hgs
parents: 47
diff changeset
    59
        if( localizedName.isEmpty() ) {
47
hgs
parents:
diff changeset
    60
            return sourceModel->data( mapToSource( index ), role );
hgs
parents:
diff changeset
    61
        } else {
49
hgs
parents: 47
diff changeset
    62
            return localizedName;
47
hgs
parents:
diff changeset
    63
        }
hgs
parents:
diff changeset
    64
    }
hgs
parents:
diff changeset
    65
    if( sourceModel )
hgs
parents:
diff changeset
    66
    {
hgs
parents:
diff changeset
    67
	    return sourceModel->data( mapToSource( index ), role );
hgs
parents:
diff changeset
    68
    }
hgs
parents:
diff changeset
    69
		else
hgs
parents:
diff changeset
    70
    {
hgs
parents:
diff changeset
    71
	    return QVariant();
hgs
parents:
diff changeset
    72
    }
hgs
parents:
diff changeset
    73
}
hgs
parents:
diff changeset
    74
hgs
parents:
diff changeset
    75
/*!
hgs
parents:
diff changeset
    76
    Return fileInfo by \a index
hgs
parents:
diff changeset
    77
    Return empty QFileInfo if sourceModel is not QFileSystemModel
hgs
parents:
diff changeset
    78
*/
hgs
parents:
diff changeset
    79
QFileInfo FmFileSystemProxyModel::fileInfo ( const QModelIndex & index ) const
hgs
parents:
diff changeset
    80
{
hgs
parents:
diff changeset
    81
    QAbstractItemModel *itemModel = sourceModel();
hgs
parents:
diff changeset
    82
    QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
hgs
parents:
diff changeset
    83
    if( sourceModel ) {
hgs
parents:
diff changeset
    84
        return sourceModel->fileInfo( mapToSource( index ) );
hgs
parents:
diff changeset
    85
    }
hgs
parents:
diff changeset
    86
hgs
parents:
diff changeset
    87
    return QFileInfo();
hgs
parents:
diff changeset
    88
}
hgs
parents:
diff changeset
    89
hgs
parents:
diff changeset
    90
/*!
hgs
parents:
diff changeset
    91
    Sets the directory \a newPath as current path to display
hgs
parents:
diff changeset
    92
    Return empty QModelIndex if sourceModel is not QFileSystemModel
hgs
parents:
diff changeset
    93
*/
hgs
parents:
diff changeset
    94
QModelIndex FmFileSystemProxyModel::setRootPath ( const QString & newPath )
hgs
parents:
diff changeset
    95
{
hgs
parents:
diff changeset
    96
    QAbstractItemModel *itemModel = sourceModel();
hgs
parents:
diff changeset
    97
    QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
hgs
parents:
diff changeset
    98
    if( sourceModel ) {
hgs
parents:
diff changeset
    99
        return mapFromSource( sourceModel->setRootPath( newPath ) );
hgs
parents:
diff changeset
   100
    }
hgs
parents:
diff changeset
   101
hgs
parents:
diff changeset
   102
    return QModelIndex();
hgs
parents:
diff changeset
   103
}
hgs
parents:
diff changeset
   104
hgs
parents:
diff changeset
   105
/*!
hgs
parents:
diff changeset
   106
    Judge if object pointed by \a index is a directory.
hgs
parents:
diff changeset
   107
    Return false also if sourceModel is not QFileSystemModel
hgs
parents:
diff changeset
   108
*/
hgs
parents:
diff changeset
   109
bool FmFileSystemProxyModel::isDir ( const QModelIndex & index ) const
hgs
parents:
diff changeset
   110
{
hgs
parents:
diff changeset
   111
    QAbstractItemModel *itemModel = sourceModel();
hgs
parents:
diff changeset
   112
    QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
hgs
parents:
diff changeset
   113
    if( sourceModel ) {
hgs
parents:
diff changeset
   114
        return sourceModel->isDir( mapToSource( index ) );
hgs
parents:
diff changeset
   115
    }
hgs
parents:
diff changeset
   116
hgs
parents:
diff changeset
   117
    return false;
hgs
parents:
diff changeset
   118
}
hgs
parents:
diff changeset
   119
hgs
parents:
diff changeset
   120
/*!
hgs
parents:
diff changeset
   121
    Get filePath by \a index. 
hgs
parents:
diff changeset
   122
    Return empty string if sourceModel is not QFileSystemModel 
hgs
parents:
diff changeset
   123
*/
hgs
parents:
diff changeset
   124
QString FmFileSystemProxyModel::filePath ( const QModelIndex & index ) const
hgs
parents:
diff changeset
   125
{
hgs
parents:
diff changeset
   126
    QAbstractItemModel *itemModel = sourceModel();
hgs
parents:
diff changeset
   127
    QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
hgs
parents:
diff changeset
   128
    if( sourceModel ) {
hgs
parents:
diff changeset
   129
        return sourceModel->filePath( mapToSource( index ) );
hgs
parents:
diff changeset
   130
    }
hgs
parents:
diff changeset
   131
    
hgs
parents:
diff changeset
   132
    return QString();
hgs
parents:
diff changeset
   133
}
hgs
parents:
diff changeset
   134
hgs
parents:
diff changeset
   135
/*!
hgs
parents:
diff changeset
   136
    Set model filters 
hgs
parents:
diff changeset
   137
    Nothing will be done if sourceModel is not QFileSystemModel 
hgs
parents:
diff changeset
   138
*/
hgs
parents:
diff changeset
   139
void FmFileSystemProxyModel::setFilter ( QDir::Filters filters )
hgs
parents:
diff changeset
   140
{
hgs
parents:
diff changeset
   141
    QAbstractItemModel *itemModel = sourceModel();
hgs
parents:
diff changeset
   142
    QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
hgs
parents:
diff changeset
   143
    if( sourceModel ) {
hgs
parents:
diff changeset
   144
        sourceModel->setFilter( filters );
hgs
parents:
diff changeset
   145
    }
hgs
parents:
diff changeset
   146
}
hgs
parents:
diff changeset
   147
hgs
parents:
diff changeset
   148
/*!
hgs
parents:
diff changeset
   149
    Set name filters 
hgs
parents:
diff changeset
   150
    Nothing will be done if sourceModel is not QFileSystemModel 
hgs
parents:
diff changeset
   151
*/
hgs
parents:
diff changeset
   152
void FmFileSystemProxyModel::setNameFilters(const QStringList &filters)
hgs
parents:
diff changeset
   153
{
hgs
parents:
diff changeset
   154
    QAbstractItemModel *itemModel = sourceModel();
hgs
parents:
diff changeset
   155
    QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
hgs
parents:
diff changeset
   156
    if( sourceModel ) {
hgs
parents:
diff changeset
   157
        sourceModel->setNameFilters( filters );
hgs
parents:
diff changeset
   158
    }
hgs
parents:
diff changeset
   159
}
hgs
parents:
diff changeset
   160
hgs
parents:
diff changeset
   161
/*
hgs
parents:
diff changeset
   162
    Filter directories that should not be seen by user.
hgs
parents:
diff changeset
   163
    For example, return false for such folders: C:\sys\, C:\system\, C:\private\, C:\resource\
hgs
parents:
diff changeset
   164
    Return default value(true) if sourceModel is not QFileSystemModel 
hgs
parents:
diff changeset
   165
*/
hgs
parents:
diff changeset
   166
bool FmFileSystemProxyModel::filterAcceptsRow(int sourceRow,
hgs
parents:
diff changeset
   167
        const QModelIndex &sourceParent) const
hgs
parents:
diff changeset
   168
{
hgs
parents:
diff changeset
   169
    QAbstractItemModel *itemModel = sourceModel();
hgs
parents:
diff changeset
   170
    QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
hgs
parents:
diff changeset
   171
    if( sourceModel ) {
hgs
parents:
diff changeset
   172
        QModelIndex nameIndex = sourceModel->index(sourceRow, NameColumn, sourceParent);
hgs
parents:
diff changeset
   173
        QFileInfo fileInfo = sourceModel->fileInfo( nameIndex );
hgs
parents:
diff changeset
   174
        QString absoluteFilePath = fileInfo.absoluteFilePath();
hgs
parents:
diff changeset
   175
        if( FmUtils::isSystemFolder( absoluteFilePath ) ) {
hgs
parents:
diff changeset
   176
            return false;
hgs
parents:
diff changeset
   177
        }
hgs
parents:
diff changeset
   178
    }
hgs
parents:
diff changeset
   179
    return true;
hgs
parents:
diff changeset
   180
}
hgs
parents:
diff changeset
   181