filemanager/src/inc/fmdrivemodel.cpp
branchRCL_3
changeset 21 65326cf895ed
parent 20 491b3ed49290
child 22 f5c50b8af68c
equal deleted inserted replaced
20:491b3ed49290 21:65326cf895ed
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include "fmdrivemodel.h"
       
    19 #include "fmfileiconprovider.h"
       
    20 #include "fmutils.h"
       
    21 
       
    22 #include <QDir>
       
    23 #include <QFileInfo>
       
    24 
       
    25 #include <hbglobal.h>
       
    26 
       
    27 FmDriveModel::FmDriveModel( QObject *parent, Options options,
       
    28         FmDriveListProvider *driveListProvider ) :
       
    29     QAbstractListModel( parent ), mOptions( options ), mDriveListProvider( driveListProvider )
       
    30 {
       
    31     mIconProvider = new FmFileIconProvider();
       
    32     refresh();
       
    33 }
       
    34 
       
    35 FmDriveModel::~FmDriveModel()
       
    36 {
       
    37     delete mIconProvider;
       
    38 }
       
    39 
       
    40 void FmDriveModel::refresh()
       
    41 {
       
    42     emit layoutAboutToBeChanged();
       
    43     mDriveList.clear();
       
    44     
       
    45     // if mDriveListProvider existed, use it to fetch drive list
       
    46     // otherwise use FmUtils::getDriveList to fetch drive list.
       
    47     if( mDriveListProvider ) {
       
    48         mDriveListProvider->getDriveList( mDriveList );
       
    49     } else {
       
    50         if( mOptions & HideUnAvailableDrive ) {
       
    51             FM_LOG( QString( "FmDriveModel::refresh HideUnAvailableDrive_true" ) );
       
    52             FmUtils::getDriveList( mDriveList, true );
       
    53         } else {
       
    54             FM_LOG( QString( "FmDriveModel::refresh HideUnAvailableDrive_false" ) );
       
    55             FmUtils::getDriveList( mDriveList, false );
       
    56         }
       
    57     }
       
    58 
       
    59 	emit layoutChanged();
       
    60 	for( int i=0; i<mDriveList.count(); i++ ) {
       
    61         emit dataChanged(index( i, 0 ), index( i, 0 ));
       
    62 	}
       
    63 }
       
    64 
       
    65 
       
    66 int FmDriveModel::rowCount( const QModelIndex &parent ) const
       
    67 {
       
    68     if (!parent.isValid())
       
    69         return mDriveList.size();
       
    70 
       
    71     return 0;
       
    72 }
       
    73 
       
    74 int FmDriveModel::columnCount( const QModelIndex &parent ) const
       
    75 {
       
    76     Q_UNUSED( parent );
       
    77     return 1;
       
    78 }
       
    79 
       
    80 QVariant FmDriveModel::data( const QModelIndex &index, int role ) const
       
    81 {
       
    82     if (!indexValid( index ))
       
    83         return QVariant();
       
    84 
       
    85     if (role == Qt::DisplayRole ) {
       
    86        return displayString( index );
       
    87     } 
       
    88     if (index.column() == 0) {
       
    89         if (role == Qt::DecorationRole ) {
       
    90             QString path = driveName( index );
       
    91             return mIconProvider->icon( QFileInfo( path ) );
       
    92         }
       
    93     }
       
    94 
       
    95     if (index.column() == 1 && role == Qt::TextAlignmentRole) {
       
    96         return Qt::AlignRight;
       
    97     }
       
    98 
       
    99     return QVariant();
       
   100 }
       
   101 
       
   102 
       
   103 QVariant FmDriveModel::headerData( int section, Qt::Orientation orientation, int role ) const
       
   104 {
       
   105     if (orientation == Qt::Horizontal) {
       
   106         if (role != Qt::DisplayRole)
       
   107             return QVariant();
       
   108 
       
   109         switch (section) {
       
   110             case 0: return hbTrId("Name");
       
   111             case 1: return hbTrId("Size");
       
   112             case 2: return hbTrId("Type");
       
   113             case 3: return hbTrId("Date Modified");
       
   114             default: return QVariant();
       
   115         }
       
   116     }
       
   117 
       
   118     return QAbstractItemModel::headerData( section, orientation, role );
       
   119 }
       
   120 
       
   121 bool FmDriveModel::indexValid( const QModelIndex &index ) const
       
   122 {
       
   123     if( !(&index) )
       
   124         return false;
       
   125     return true;
       
   126 }
       
   127 
       
   128 QString FmDriveModel::driveName( const QModelIndex &index ) const
       
   129 {
       
   130     QString data;
       
   131     if (index.row() >= 0 && index.row() < mDriveList.size()) {
       
   132         int row = index.row();
       
   133         QString diskName = mDriveList[ row ];
       
   134         data = diskName;
       
   135     }
       
   136     return data;
       
   137 }
       
   138 
       
   139 QVariant FmDriveModel::displayString( const QModelIndex &index ) const
       
   140 {
       
   141     QStringList data;
       
   142     if (index.row() >= 0 && index.row() < mDriveList.size()) {
       
   143         int row = index.row();
       
   144         QString diskName = mDriveList[ row ];
       
   145 
       
   146         if( mOptions & FillWithVolume ) {
       
   147             data << FmUtils::fillDriveVolume( diskName, mOptions & FillWithDefaultVolume );
       
   148         } else {
       
   149             data << FmUtils::removePathSplash( diskName );
       
   150         }
       
   151         diskName = FmUtils::fillPathWithSplash( diskName );
       
   152         FmDriverInfo driverInfo = FmUtils::queryDriverInfo( diskName );
       
   153         if ( mOptions & FillWithTotalSize ) {                    
       
   154             if( driverInfo.driveState() & FmDriverInfo::EDriveAvailable ) {
       
   155                 data << QString( hbTrId ( "Size: " ) + FmUtils::formatStorageSize( driverInfo.size() ) );                
       
   156             } else if( driverInfo.driveState() & FmDriverInfo::EDriveLocked ) {
       
   157                 data << QString( hbTrId ( "Locked" ) );                
       
   158             } else if( driverInfo.driveState() & FmDriverInfo::EDriveCorrupted ) {
       
   159                 data << QString( hbTrId ( "Corrupted" ) );                
       
   160             } else if( driverInfo.driveState() & FmDriverInfo::EDriveNotPresent ) {
       
   161                 data << QString( hbTrId ( "Not Ready" ) );                
       
   162             }            
       
   163         }
       
   164         if ( mOptions & FillWithFreeSize ) {        
       
   165             if( driverInfo.driveState() & FmDriverInfo::EDriveAvailable ) {                
       
   166                 data << QString( hbTrId ( "Free: " ) + FmUtils::formatStorageSize( driverInfo.freeSize() ) );
       
   167             } else if( ( driverInfo.driveState() & FmDriverInfo::EDriveLocked ) ||
       
   168                     ( driverInfo.driveState() & FmDriverInfo::EDriveCorrupted ) ||
       
   169                     ( driverInfo.driveState() & FmDriverInfo::EDriveNotPresent )) {                
       
   170                 data << QString( QString(" ") );
       
   171             }            
       
   172         }
       
   173     }
       
   174     return data;
       
   175 }