37
|
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 |
* Zhiqiang Yang <zhiqiang.yang@nokia.com>
|
|
14 |
*
|
|
15 |
* Description:
|
|
16 |
* The source file of the file manager utilities
|
|
17 |
*/
|
|
18 |
|
|
19 |
#include "fmutils.h"
|
|
20 |
|
46
|
21 |
#include <QRegExp>
|
37
|
22 |
#include <QFileInfo>
|
|
23 |
|
|
24 |
#include <hbglobal.h>
|
|
25 |
|
46
|
26 |
|
37
|
27 |
/*!
|
|
28 |
Used to get drive type for convenience.
|
|
29 |
*/
|
|
30 |
FmDriverInfo::DriveType FmDriverInfo::driveType()
|
|
31 |
{
|
|
32 |
FmDriverInfo::DriveType driveType;
|
|
33 |
if( mDriveState & FmDriverInfo::EDriveRemovable ) {
|
|
34 |
// if drive is removable, it may be mass, usb, or remote drive
|
|
35 |
if( mDriveState & FmDriverInfo::EDriveMassStorage ) {
|
|
36 |
driveType = FmDriverInfo::EDriveTypeMassStorage;
|
|
37 |
} else if( mDriveState & FmDriverInfo::EDriveUsbMemory ) {
|
|
38 |
driveType = FmDriverInfo::EDriveTypeUsbMemory;
|
|
39 |
} else if( mDriveState & FmDriverInfo::EDriveRemote ){
|
|
40 |
driveType = FmDriverInfo::EDriveTypeRemote;
|
|
41 |
} else {
|
|
42 |
driveType = FmDriverInfo::EDriveTypeMemoryCard;
|
|
43 |
}
|
|
44 |
} else if( mDriveState & FmDriverInfo::EDriveRom ) {
|
|
45 |
driveType = FmDriverInfo::EDriveTypeRom;
|
|
46 |
} else if( mDriveState & FmDriverInfo::EDriveRam ) {
|
|
47 |
driveType = FmDriverInfo::EDriveTypeRam;
|
|
48 |
} else {
|
|
49 |
// if drive type is not recognized in previous condition,
|
|
50 |
// it should be an un-existed drive or phone memory
|
|
51 |
if( mDriveState & EDriveNotPresent ){
|
|
52 |
// if come here, means 2 conditions:
|
|
53 |
// 1 this drive is not exist( FmUtils::queryDriverInfo will give EDriveNotPresent to mDriveState )
|
|
54 |
// 2 un-mounted removable drive
|
|
55 |
// removable drive filted in previous condifiton. So, give EDriveTypeNotExist
|
|
56 |
driveType = FmDriverInfo::EDriveTypeNotExist;
|
|
57 |
} else {
|
|
58 |
// drive is existed, only remain the type of EDriveTypePhoneMemory
|
|
59 |
driveType = FmDriverInfo::EDriveTypePhoneMemory;
|
|
60 |
}
|
|
61 |
}
|
|
62 |
|
|
63 |
return driveType;
|
|
64 |
}
|
|
65 |
|
|
66 |
/*!
|
|
67 |
return drive name for \a path
|
|
68 |
*/
|
|
69 |
QString FmUtils::getDriveNameFromPath( const QString &path )
|
|
70 |
{
|
|
71 |
// fillPathWithSplash make sure path length will be at least 3 if it is not empty.
|
|
72 |
QString checkedPath( fillPathWithSplash( path ) );
|
|
73 |
if( checkedPath.length() < 3 ) {
|
|
74 |
return QString();
|
|
75 |
}
|
|
76 |
return checkedPath.left( 3 );
|
|
77 |
}
|
|
78 |
|
|
79 |
/*!
|
|
80 |
return drive letter for \a path
|
|
81 |
*/
|
|
82 |
QString FmUtils::getDriveLetterFromPath( const QString &path )
|
|
83 |
{
|
|
84 |
if( path.length() <2 ) {
|
|
85 |
return QString();
|
|
86 |
}
|
|
87 |
return path.left( 1 );
|
|
88 |
}
|
|
89 |
|
|
90 |
/*!
|
|
91 |
return formatted string for size.
|
|
92 |
*/
|
|
93 |
QString FmUtils::formatStorageSize( quint64 size )
|
|
94 |
{
|
|
95 |
if ( size < 1000 ) {
|
|
96 |
return QString::number( size ) + " B";
|
|
97 |
} else if ( size < 1000 * 1000 ) {
|
|
98 |
return QString::number( size / 1024.0, 'f', 2 ) + " KB";
|
|
99 |
} else if ( size < 1000 * 1000 * 1000 ) {
|
|
100 |
return QString::number( size / (1024.0 * 1024.0), 'f', 1 ) + " MB";
|
|
101 |
} else {
|
|
102 |
return QString::number( size / ( 1024.0 * 1024.0 * 1024.0 ), 'f', 1 ) + " GB";
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
/*!
|
|
107 |
Judge if a path is drive path, not a folder or a file.
|
|
108 |
For example, true for C:\ while false for C:\data
|
|
109 |
*/
|
|
110 |
bool FmUtils::isDrive( const QString &path )
|
|
111 |
{
|
|
112 |
bool ret( false );
|
|
113 |
if( path.length() <= 3 && path.length() >=2 ) {
|
|
114 |
ret = true;
|
|
115 |
}
|
|
116 |
|
|
117 |
return ret;
|
|
118 |
}
|
|
119 |
|
|
120 |
/*!
|
46
|
121 |
All "/" and "\" in \a path will be changed to \a splitter
|
|
122 |
QDir::separator is default value for splitter
|
37
|
123 |
\sa fillPathWithSplash, fillPathWithSplash will append QDir::separator in the end
|
|
124 |
*/
|
46
|
125 |
QString FmUtils::formatPath( const QString &path, const QChar &splitter )
|
37
|
126 |
{
|
|
127 |
QString formatPath;
|
|
128 |
if( path.isEmpty() ) {
|
|
129 |
return formatPath;
|
|
130 |
}
|
|
131 |
|
|
132 |
foreach( const QChar &ch, path ) {
|
|
133 |
if( ch == QChar('\\') || ch == QChar('/') ) {
|
46
|
134 |
formatPath.append( splitter );
|
37
|
135 |
} else {
|
|
136 |
formatPath.append( ch );
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
return formatPath;
|
|
141 |
}
|
|
142 |
|
|
143 |
/*!
|
46
|
144 |
Fill splash in the end of \a filePath. And all "/" and "\" will be changed to \a splitter
|
|
145 |
QDir::separator is default value for splitter
|
37
|
146 |
Please do not call this function if path is a file.
|
|
147 |
Use \a formatPath instead, \a formatPath will not append QDir::separator in the end.
|
|
148 |
\sa formatPath only changed "/" and "\" to QDir::separator
|
|
149 |
*/
|
46
|
150 |
QString FmUtils::fillPathWithSplash( const QString &filePath, const QChar &splitter )
|
37
|
151 |
{
|
|
152 |
QString newFilePath;
|
|
153 |
if( filePath.isEmpty() ) {
|
|
154 |
return newFilePath;
|
|
155 |
}
|
|
156 |
|
46
|
157 |
newFilePath = formatPath( filePath, splitter );
|
37
|
158 |
|
46
|
159 |
if( newFilePath.right( 1 )!= splitter ){
|
|
160 |
newFilePath.append( splitter );
|
37
|
161 |
}
|
|
162 |
return newFilePath;
|
|
163 |
}
|
|
164 |
|
|
165 |
/*!
|
|
166 |
Remove splash in the end of \a filePath.
|
|
167 |
*/
|
|
168 |
QString FmUtils::removePathSplash( const QString &filePath )
|
|
169 |
{
|
|
170 |
QString newFilePath( filePath );
|
|
171 |
if( filePath.right( 1 ) == QChar( '/' ) || filePath.right(1) == QString( "\\" ) ) {
|
|
172 |
newFilePath = filePath.left( filePath.length() - 1 );
|
|
173 |
}
|
|
174 |
return newFilePath;
|
|
175 |
}
|
|
176 |
|
|
177 |
/*!
|
|
178 |
fill \a driveList of drives can be shown in driveListView
|
|
179 |
*/
|
|
180 |
void FmUtils::getDriveList( QStringList &driveList, bool isHideUnAvailableDrive )
|
|
181 |
{
|
|
182 |
if( isHideUnAvailableDrive ) {
|
|
183 |
FM_LOG( QString( "getDriveList HideUnAvailableDrive_true" ) );
|
|
184 |
} else {
|
|
185 |
FM_LOG( QString( "getDriveList HideUnAvailableDrive_false" ) );
|
|
186 |
}
|
|
187 |
QFileInfoList infoList = QDir::drives();
|
|
188 |
|
|
189 |
foreach( const QFileInfo &fileInfo, infoList ) {
|
|
190 |
QString driveName = fileInfo.absolutePath();
|
|
191 |
if( checkDriveAccessFilter( driveName ) ) {
|
|
192 |
if( !isHideUnAvailableDrive ) {
|
|
193 |
driveList.append( driveName );
|
|
194 |
}
|
|
195 |
else if ( isDriveAvailable( driveName ) ) {
|
|
196 |
driveList.append( driveName );
|
|
197 |
}
|
|
198 |
}
|
|
199 |
}
|
|
200 |
return;
|
|
201 |
}
|
|
202 |
|
|
203 |
/*!
|
|
204 |
return the combined string of drive name and volume( e.g. C: Phone memory for C drive)
|
|
205 |
\a driveName the drive name.
|
|
206 |
If set \a isFillWithDefaultVolume true, fill result with default volume if drive volume is empty.
|
|
207 |
*/
|
|
208 |
QString FmUtils::fillDriveVolume( QString driveName, bool isFillWithDefaultVolume )
|
|
209 |
{
|
|
210 |
QString ret;
|
|
211 |
if( driveName.isEmpty() ) {
|
|
212 |
return ret;
|
|
213 |
}
|
|
214 |
QString tempDriveName = fillPathWithSplash( driveName );
|
|
215 |
|
|
216 |
QString checkedDriveName( removePathSplash( driveName ) );
|
|
217 |
|
|
218 |
FmDriverInfo driverInfo = FmUtils::queryDriverInfo( tempDriveName );
|
|
219 |
QString volumeName = driverInfo.volumeName();
|
|
220 |
|
|
221 |
if( volumeName.isEmpty() && isFillWithDefaultVolume ){
|
|
222 |
switch ( driverInfo.driveType() )
|
|
223 |
{
|
|
224 |
case FmDriverInfo::EDriveTypeMassStorage:
|
|
225 |
ret = hbTrId( "txt_fmgr_dblist_1_mass_storage" ).arg( checkedDriveName );
|
|
226 |
break;
|
|
227 |
case FmDriverInfo::EDriveTypeUsbMemory:
|
|
228 |
ret = hbTrId( "txt_fmgr_dblist_1_usb_memory" ).arg( checkedDriveName );
|
|
229 |
break;
|
|
230 |
case FmDriverInfo::EDriveTypeMemoryCard:
|
|
231 |
ret = hbTrId( "txt_fmgr_dblist_1_memory_card" ).arg( checkedDriveName );
|
|
232 |
break;
|
|
233 |
case FmDriverInfo::EDriveTypePhoneMemory:
|
|
234 |
ret = hbTrId( "txt_fmgr_dblist_1_device_memory" ).arg( checkedDriveName );
|
|
235 |
break;
|
|
236 |
case FmDriverInfo::EDriveTypeNotExist:// no such drive
|
|
237 |
default:// do not need get volume for other drive types, e.g. Ram, Rom
|
|
238 |
break;
|
|
239 |
}
|
|
240 |
}
|
|
241 |
|
|
242 |
if( ret.isEmpty() ) {
|
|
243 |
// ret is not got. fill ret as default method
|
|
244 |
// txt_fmgr_dblist_1_2 is not correct, can not use.
|
|
245 |
ret = hbTrId( "%1 %2" ).arg( checkedDriveName ).arg( volumeName );
|
|
246 |
}
|
|
247 |
return ret;
|
|
248 |
}
|
|
249 |
|
|
250 |
/*!
|
|
251 |
return the default volume for drive
|
|
252 |
\a driveName the drive.
|
|
253 |
\sa getVolumeNameWithDefaultNameIfNull
|
|
254 |
*/
|
|
255 |
QString FmUtils::getDefaultVolumeName( const QString &driveName )
|
|
256 |
{
|
|
257 |
QString defaultVolumeName;
|
|
258 |
FmDriverInfo driverInfo = FmUtils::queryDriverInfo( driveName );
|
|
259 |
|
|
260 |
switch ( driverInfo.driveType() )
|
|
261 |
{
|
|
262 |
case FmDriverInfo::EDriveTypeMassStorage:
|
|
263 |
defaultVolumeName = hbTrId("Mass storage");
|
|
264 |
break;
|
|
265 |
case FmDriverInfo::EDriveTypeUsbMemory:
|
|
266 |
defaultVolumeName = hbTrId("USB memory");
|
|
267 |
break;
|
|
268 |
case FmDriverInfo::EDriveTypeMemoryCard:
|
|
269 |
defaultVolumeName = hbTrId("Memory card");
|
|
270 |
break;
|
|
271 |
case FmDriverInfo::EDriveTypePhoneMemory:
|
|
272 |
defaultVolumeName = hbTrId("Device memory");
|
|
273 |
break;
|
|
274 |
case FmDriverInfo::EDriveTypeNotExist:// no such drive, break and return empty string
|
|
275 |
default:// do not have default volume for other drive types, e.g. Ram, Rom
|
|
276 |
break;
|
|
277 |
}
|
|
278 |
return defaultVolumeName;
|
|
279 |
}
|
|
280 |
|
|
281 |
/*!
|
|
282 |
get the volume name of the drive, if it is null, then return the default name.
|
|
283 |
\a driveName the drive letter.
|
|
284 |
\a defaultName whether it is the default name.
|
|
285 |
return the volume name.
|
|
286 |
*/
|
|
287 |
QString FmUtils::getVolumeNameWithDefaultNameIfNull( const QString &driveName, bool &defaultName )
|
|
288 |
{
|
|
289 |
// do not re-use FmUtils::getDefaultVolumeName to save one time for call FmUtils::queryDriverInfo
|
|
290 |
FmDriverInfo driverInfo = FmUtils::queryDriverInfo( driveName );
|
|
291 |
|
|
292 |
QString volumeName = driverInfo.volumeName();
|
|
293 |
//save the volume status, whether it is default name
|
|
294 |
defaultName = false;
|
|
295 |
//volume name may be null if not set, it will be set at least for one time in the following while cycling.
|
|
296 |
if ( volumeName.isEmpty() ) {
|
|
297 |
switch ( driverInfo.driveType() )
|
|
298 |
{
|
|
299 |
case FmDriverInfo::EDriveTypeMassStorage:
|
|
300 |
volumeName = hbTrId("Mass storage");
|
|
301 |
break;
|
|
302 |
case FmDriverInfo::EDriveTypeUsbMemory:
|
|
303 |
volumeName = hbTrId("USB memory");
|
|
304 |
break;
|
|
305 |
case FmDriverInfo::EDriveTypeMemoryCard:
|
|
306 |
volumeName = hbTrId("Memory card");
|
|
307 |
break;
|
|
308 |
case FmDriverInfo::EDriveTypePhoneMemory:
|
|
309 |
volumeName = hbTrId("Device memory");
|
|
310 |
break;
|
|
311 |
case FmDriverInfo::EDriveTypeNotExist:// no such drive
|
|
312 |
default:// do not need get volume for other drive types, e.g. Ram, Rom
|
|
313 |
break;
|
|
314 |
}
|
|
315 |
if( !volumeName.isEmpty() ) {
|
|
316 |
defaultName = true;
|
|
317 |
}
|
|
318 |
}
|
|
319 |
return volumeName;
|
|
320 |
}
|
|
321 |
|
|
322 |
/*!
|
|
323 |
Check if two path are the same.
|
|
324 |
*/
|
|
325 |
bool FmUtils::isPathEqual( const QString &pathFst, const QString &pathLast )
|
|
326 |
{
|
|
327 |
QString fst( fillPathWithSplash( pathFst ) );
|
|
328 |
QString last( fillPathWithSplash( pathLast ) );
|
|
329 |
if( fst.compare( last, Qt::CaseInsensitive ) == 0 ) {
|
|
330 |
return true;
|
|
331 |
}
|
|
332 |
return false;
|
|
333 |
}
|
|
334 |
|
|
335 |
/*!
|
|
336 |
Check if a name is illegal for folder/file naming rule.
|
|
337 |
*/
|
|
338 |
bool FmUtils::checkFolderFileName( const QString& name )
|
|
339 |
{
|
|
340 |
// trim space firest, because there may be some spaces after "." , it is also not valid
|
|
341 |
QString trimmedName( name.trimmed() );
|
|
342 |
if( trimmedName.isEmpty() ) {
|
|
343 |
return false;
|
|
344 |
}
|
|
345 |
if( trimmedName.endsWith( QChar('.'), Qt::CaseInsensitive ) ) {
|
|
346 |
return false;
|
|
347 |
}
|
|
348 |
if( trimmedName.contains( QChar('\\'), Qt::CaseInsensitive ) ||
|
|
349 |
trimmedName.contains( QChar('/'), Qt::CaseInsensitive ) ||
|
|
350 |
trimmedName.contains( QChar(':'), Qt::CaseInsensitive ) ||
|
|
351 |
trimmedName.contains( QChar('*'), Qt::CaseInsensitive ) ||
|
|
352 |
trimmedName.contains( QChar('?'), Qt::CaseInsensitive ) ||
|
|
353 |
trimmedName.contains( QChar('\"'), Qt::CaseInsensitive ) ||
|
|
354 |
trimmedName.contains( QChar('<'), Qt::CaseInsensitive ) ||
|
|
355 |
trimmedName.contains( QChar('>'), Qt::CaseInsensitive ) ||
|
|
356 |
trimmedName.contains( QChar('|'), Qt::CaseInsensitive ) ){
|
|
357 |
return false;
|
|
358 |
}
|
|
359 |
// use original name to exam max size of file name
|
|
360 |
if( name.length() > getMaxFileNameLength() ) {
|
|
361 |
return false;
|
|
362 |
}
|
|
363 |
return true;
|
|
364 |
}
|
|
365 |
|
|
366 |
/*!
|
|
367 |
check file or folder path is illegal or not.
|
|
368 |
\a fileName file/folder name, used to check illegal characters
|
|
369 |
\a path file/folder path, used to check if path is available to use.
|
|
370 |
\a errString if return false, errString will be set for error note.
|
|
371 |
return true for not illegal and false for illegal path.
|
|
372 |
*/
|
|
373 |
bool FmUtils::checkNewFolderOrFile( const QString &fileName, const QString &path, QString &errString )
|
|
374 |
{
|
|
375 |
// first check if fileName is valid, then check if path length is valid, and check if file/foler is existed at last
|
|
376 |
QFileInfo fileInfo( path );
|
|
377 |
bool ret( true );
|
|
378 |
if (!FmUtils::checkFolderFileName( fileName ) ) {
|
|
379 |
errString = hbTrId( "Invalid file or folder name!" );
|
|
380 |
ret = false;
|
|
381 |
} else if( !FmUtils::checkMaxPathLength( path ) ) {
|
|
382 |
errString = hbTrId( "the path you specified is too long!" );
|
|
383 |
ret = false;
|
|
384 |
} else if (fileInfo.exists()) {
|
|
385 |
errString = hbTrId( "%1 already exist!" ).arg( fileInfo.fileName() );
|
|
386 |
ret = false;
|
|
387 |
}
|
|
388 |
return ret;
|
|
389 |
}
|
|
390 |
|
|
391 |
/*!
|
|
392 |
Check if \a dest is sub level path of \a src
|
|
393 |
Used to check True/False when copy a folder to itself or its subfolder
|
|
394 |
For example, c:\data\test is sub path of c:\data.
|
|
395 |
But c:\data123\test is not sub path of c:\data.
|
|
396 |
So after got right part of path, the first char must be \ or /
|
|
397 |
*/
|
|
398 |
bool FmUtils::isSubLevelPath( const QString &src, const QString &dest )
|
|
399 |
{
|
|
400 |
FM_LOG("FmUtils::isSubFolder: src=" + src + " dest=" + dest);
|
|
401 |
QString checkedSrc( FmUtils::fillPathWithSplash( src ) );
|
|
402 |
QString checkedDest( FmUtils::fillPathWithSplash( dest ) );
|
|
403 |
|
|
404 |
if( checkedDest.contains( checkedSrc, Qt::CaseInsensitive) &&
|
|
405 |
checkedDest.length() > checkedSrc.length() ) {
|
|
406 |
// for example c:\data\ vs c:\data\123\
|
|
407 |
FM_LOG("FmUtils::isSubFolder: true");
|
|
408 |
return true;
|
|
409 |
}
|
|
410 |
// for example c:\data\ vs c:\data\
|
|
411 |
// for example c:\data\ vs c:\data123\
|
|
412 |
|
|
413 |
FM_LOG("FmUtils::isSubFolder: false");
|
|
414 |
return false;
|
|
415 |
}
|
46
|
416 |
|
|
417 |
/*!
|
|
418 |
Check if \a path is system path.
|
|
419 |
*/
|
|
420 |
bool FmUtils::isSystemFolder( const QString &path )
|
|
421 |
{
|
|
422 |
QFileInfo fileInfo( path );
|
|
423 |
if( fileInfo.isDir() ) {
|
|
424 |
QString checkedPath( fillPathWithSplash( path, Char_Slash ) );
|
|
425 |
|
|
426 |
QRegExp systemFolder ( RegexWidecard_SystemFolder, Qt::CaseInsensitive, QRegExp::Wildcard );
|
|
427 |
QRegExp sysFolder ( RegexWidecard_SysFolder, Qt::CaseInsensitive, QRegExp::Wildcard );
|
|
428 |
QRegExp privateFolder ( RegexWidecard_PrivateFolder, Qt::CaseInsensitive, QRegExp::Wildcard );
|
|
429 |
QRegExp resourceFolder( RegexWidecard_ResourceFolder, Qt::CaseInsensitive, QRegExp::Wildcard );
|
|
430 |
|
|
431 |
if( systemFolder.exactMatch( checkedPath ) ||
|
|
432 |
sysFolder.exactMatch( checkedPath ) ||
|
|
433 |
privateFolder.exactMatch( checkedPath ) ||
|
|
434 |
resourceFolder.exactMatch( checkedPath ) ) {
|
|
435 |
return true;
|
|
436 |
}
|
|
437 |
}
|
|
438 |
return false;
|
|
439 |
}
|