|
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 "fmutils.h" |
|
20 |
|
21 #include <QDir> |
|
22 #include <QFileInfo> |
|
23 |
|
24 FmDriveModel::FmDriveModel( QObject *parent, Options options ) : |
|
25 QAbstractListModel( parent ), mOptions( options ) |
|
26 { |
|
27 mIconProvider = new QFileIconProvider(); |
|
28 refresh(); |
|
29 } |
|
30 |
|
31 FmDriveModel::~FmDriveModel(void) |
|
32 { |
|
33 delete mIconProvider; |
|
34 } |
|
35 |
|
36 void FmDriveModel::refresh() |
|
37 { |
|
38 QFileInfoList infoList = QDir::drives(); |
|
39 |
|
40 mDriveList.clear(); |
|
41 if( mOptions & HideUnAvailableDrive ) { |
|
42 FmUtils::getDriveList( mDriveList, true ); |
|
43 } else { |
|
44 FmUtils::getDriveList( mDriveList, false ); |
|
45 } |
|
46 emit layoutChanged(); |
|
47 } |
|
48 |
|
49 |
|
50 int FmDriveModel::rowCount( const QModelIndex &parent ) const |
|
51 { |
|
52 if (!parent.isValid()) |
|
53 return mDriveList.size(); |
|
54 |
|
55 return 0; |
|
56 } |
|
57 |
|
58 int FmDriveModel::columnCount( const QModelIndex &parent ) const |
|
59 { |
|
60 Q_UNUSED( parent ); |
|
61 return 1; |
|
62 } |
|
63 |
|
64 QVariant FmDriveModel::data( const QModelIndex &index, int role ) const |
|
65 { |
|
66 if (!indexValid( index )) |
|
67 return QVariant(); |
|
68 |
|
69 if (role == Qt::DisplayRole ) { |
|
70 return displayString( index ); |
|
71 } else if (role == Qt::UserRole ) { |
|
72 return driveName( index ); |
|
73 } |
|
74 |
|
75 if (index.column() == 0) { |
|
76 if (role == Qt::DecorationRole ) { |
|
77 QString path = driveName( index ); |
|
78 return mIconProvider->icon( QFileInfo( path ) ); |
|
79 } |
|
80 } |
|
81 |
|
82 if (index.column() == 1 && role == Qt::TextAlignmentRole) { |
|
83 return Qt::AlignRight; |
|
84 } |
|
85 |
|
86 return QVariant(); |
|
87 } |
|
88 |
|
89 |
|
90 QVariant FmDriveModel::headerData( int section, Qt::Orientation orientation, int role ) const |
|
91 { |
|
92 if (orientation == Qt::Horizontal) { |
|
93 if (role != Qt::DisplayRole) |
|
94 return QVariant(); |
|
95 |
|
96 switch (section) { |
|
97 case 0: return tr("Name"); |
|
98 case 1: return tr("Size"); |
|
99 case 2: return tr("Type"); |
|
100 case 3: return tr("Date Modified"); |
|
101 default: return QVariant(); |
|
102 } |
|
103 } |
|
104 |
|
105 return QAbstractItemModel::headerData( section, orientation, role ); |
|
106 } |
|
107 |
|
108 bool FmDriveModel::indexValid( const QModelIndex &index ) const |
|
109 { |
|
110 if( !(&index) ) |
|
111 return false; |
|
112 return true; |
|
113 } |
|
114 |
|
115 QString FmDriveModel::driveName( const QModelIndex &index ) const |
|
116 { |
|
117 QString data; |
|
118 if (index.row() >= 0 && index.row() < mDriveList.size()) { |
|
119 int row = index.row(); |
|
120 QString diskName = mDriveList[ row ]; |
|
121 data = diskName; |
|
122 } |
|
123 return data; |
|
124 } |
|
125 |
|
126 QString FmDriveModel::displayString( const QModelIndex &index ) const |
|
127 { |
|
128 QString data; |
|
129 if (index.row() >= 0 && index.row() < mDriveList.size()) { |
|
130 int row = index.row(); |
|
131 QString diskName = mDriveList[ row ]; |
|
132 |
|
133 if( mOptions & FillWithVolume ) { |
|
134 data = FmUtils::fillDriveVolume( diskName, mOptions & FillWithDefaultVolume ); |
|
135 } else { |
|
136 data = FmUtils::removePathSplash( diskName ); |
|
137 } |
|
138 } |
|
139 return data; |
|
140 } |