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 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
#include "fmoperationcopyormove.h"
|
|
19 |
#include "fmcommon.h"
|
|
20 |
#include "fmoperationbase.h"
|
|
21 |
#include "fmdrivedetailstype.h"
|
|
22 |
#include "fmutils.h"
|
|
23 |
|
|
24 |
#include <QDir>
|
|
25 |
#include <QFileInfo>
|
|
26 |
#include <QStringList>
|
|
27 |
#include <QStack>
|
|
28 |
|
|
29 |
/* \fn void askForReplace( const QString &srcFile, const QString &destFile, bool *isAccepted )
|
|
30 |
* This signal is emitted when the same file as \a srcFile exists.
|
|
31 |
* \a destFile the destination file.
|
|
32 |
* \a isAccepted whether to replace the destination file.
|
|
33 |
*/
|
|
34 |
|
|
35 |
/* \fn void askForRename( const QString &srcFile, QString *destFile )
|
|
36 |
* This signal is emitted when \a srcFile needs to be renamed.
|
|
37 |
* \a destFile return the new name.
|
|
38 |
*/
|
|
39 |
|
|
40 |
/* \fn void driveSpaceChanged()
|
|
41 |
* This signal is emitted when copy or move is completed, and used to update the drive size.
|
|
42 |
*/
|
|
43 |
|
|
44 |
//the size of one time copy
|
|
45 |
const int FmOneTimeCopyOrMoveSize = 1024;
|
|
46 |
//the total steps of progress bar
|
|
47 |
const int FmTotalProgressBarSteps = 100;
|
|
48 |
/*
|
|
49 |
* Constructs a copy or move operation with
|
|
50 |
* \a operationService parent
|
|
51 |
* \a type the type of operation(EOperationTypeCopy or EOperationTypeMove)
|
|
52 |
* \a sourceList the source file or path lists that will be copied
|
|
53 |
* \a targetPath the target path where the source file will be copied
|
|
54 |
*/
|
|
55 |
FmOperationCopyOrMove::FmOperationCopyOrMove( FmOperationService *operationService, FmOperationService::TOperationType type, const QStringList &sourceList, const QString &targetPath )
|
|
56 |
: FmOperationBase( operationService, type ),
|
|
57 |
mOperationService( operationService ), mSourceList( sourceList ), mTargetPath( targetPath ),
|
|
58 |
mStop( 0 ), mTotalSize( 0 ), mCopiedOrMovedSize( 0 ), mTotalSteps( FmTotalProgressBarSteps ), mCurrentStep( 0 )
|
|
59 |
{
|
|
60 |
mTargetPath = FmUtils::fillPathWithSplash( mTargetPath );
|
|
61 |
connect( this, SIGNAL( askForRename( QString, QString* ) ),
|
|
62 |
mOperationService, SLOT( on_operation_askForRename( QString, QString* )), Qt::BlockingQueuedConnection );
|
|
63 |
connect( this, SIGNAL( askForReplace( QString, QString, bool* ) ),
|
|
64 |
mOperationService, SLOT( on_operation_askForReplace( QString, QString, bool* )), Qt::BlockingQueuedConnection );
|
|
65 |
connect( this, SIGNAL( driveSpaceChanged() ),
|
|
66 |
mOperationService, SLOT( on_operation_driveSpaceChanged() ) );
|
|
67 |
}
|
|
68 |
|
|
69 |
/*
|
|
70 |
* Destructs the copy or move operation.
|
|
71 |
*/
|
|
72 |
FmOperationCopyOrMove::~FmOperationCopyOrMove()
|
|
73 |
{
|
|
74 |
}
|
|
75 |
|
|
76 |
/*
|
|
77 |
* Returns the source file or path list
|
|
78 |
*/
|
|
79 |
QStringList FmOperationCopyOrMove::sourceList()
|
|
80 |
{
|
|
81 |
return mSourceList;
|
|
82 |
}
|
|
83 |
|
|
84 |
/*
|
|
85 |
* Returns the target path
|
|
86 |
*/
|
|
87 |
QString FmOperationCopyOrMove::targetPath()
|
|
88 |
{
|
|
89 |
return mTargetPath;
|
|
90 |
}
|
|
91 |
|
|
92 |
/*
|
|
93 |
* Starts the operation.
|
|
94 |
* \a isStopped flag the outside stop operation
|
|
95 |
*/
|
|
96 |
void FmOperationCopyOrMove::start( volatile bool *isStopped )
|
|
97 |
{
|
|
98 |
mStop = isStopped;
|
|
99 |
mTotalSize = 0;
|
|
100 |
mCopiedOrMovedSize = 0;
|
|
101 |
mCurrentStep = 0;
|
|
102 |
|
|
103 |
if ( mSourceList.empty() ) {
|
|
104 |
emit notifyError( FmErrWrongParam, mErrString );
|
|
105 |
return ;
|
|
106 |
}
|
|
107 |
emit notifyPreparing( true );
|
|
108 |
|
|
109 |
int numofFolders = 0;
|
|
110 |
int numofFiles = 0;
|
|
111 |
|
|
112 |
int ret = FmFolderDetails::queryDetailOfContentList( mSourceList, numofFolders,
|
|
113 |
numofFiles, mTotalSize, mStop, true );
|
|
114 |
if( ret != FmErrNone ) {
|
|
115 |
emit notifyError( ret, mErrString );
|
|
116 |
return;
|
|
117 |
}
|
|
118 |
if ( !targetHasEnoughSpace() ) {
|
|
119 |
emit notifyError( FmErrDiskFull, mErrString );
|
|
120 |
return;
|
|
121 |
}
|
|
122 |
emit notifyStart( true, mTotalSteps );
|
|
123 |
|
|
124 |
foreach( const QString& source, mSourceList ) {
|
|
125 |
// formatPath, but do not append splash in the end
|
|
126 |
// Otherwise could not get fileName in QFileInfo::fileName
|
|
127 |
QString checkedSource( FmUtils::formatPath( source ) );
|
|
128 |
QFileInfo fi( checkedSource );
|
|
129 |
if( !fi.exists() ) {
|
46
|
130 |
mErrString = checkedSource;
|
|
131 |
emit driveSpaceChanged();
|
37
|
132 |
emit notifyError( FmErrSrcPathDoNotExist, mErrString );
|
|
133 |
return;
|
|
134 |
}
|
|
135 |
QString newName;
|
|
136 |
bool isAcceptReplace = false;
|
|
137 |
QFileInfo destFi( mTargetPath + fi.fileName() );
|
|
138 |
|
|
139 |
// while for duplicated file/dir
|
|
140 |
while( destFi.exists() ) {
|
|
141 |
if( destFi.isFile() && destFi.absoluteFilePath().compare( fi.absoluteFilePath(), Qt::CaseInsensitive ) != 0 ) {
|
|
142 |
emit askForReplace( destFi.absoluteFilePath(), fi.absoluteFilePath(), &isAcceptReplace );
|
|
143 |
if( isAcceptReplace ) {
|
|
144 |
//delete src file
|
|
145 |
if( !QFile::remove( destFi.absoluteFilePath() ) ) {
|
|
146 |
mErrString = destFi.absoluteFilePath();
|
|
147 |
ret = FmErrCannotRemove;
|
|
148 |
break;
|
|
149 |
}
|
|
150 |
destFi.setFile( destFi.absoluteFilePath() );
|
|
151 |
} else {
|
|
152 |
queryForRename( destFi.absoluteFilePath(), &newName );
|
|
153 |
if( newName.isEmpty() ) {
|
|
154 |
ret = FmErrCancel;
|
|
155 |
break;
|
|
156 |
}
|
|
157 |
QString targetName = mTargetPath + newName;
|
|
158 |
destFi.setFile( targetName );
|
|
159 |
}
|
|
160 |
} else{
|
|
161 |
// destination is dir
|
|
162 |
queryForRename( destFi.absoluteFilePath(), &newName );
|
|
163 |
if( newName.isEmpty() ) {
|
|
164 |
ret = FmErrCancel;
|
|
165 |
break;
|
|
166 |
}
|
|
167 |
QString targetName = mTargetPath + newName;
|
|
168 |
destFi.setFile( targetName );
|
|
169 |
}
|
|
170 |
}
|
|
171 |
if( ret != FmErrNone ) {
|
|
172 |
// refresh drive space no care if cancel, error or finished.
|
|
173 |
// as filemanger cannot notify drive space changed
|
|
174 |
// do not refresh path as QFileSystemModel will do auto-refresh
|
|
175 |
emit driveSpaceChanged();
|
46
|
176 |
emit notifyError( ret, mErrString );
|
37
|
177 |
return;
|
|
178 |
}
|
|
179 |
ret = copyOrMove( checkedSource, mTargetPath, newName );
|
|
180 |
if( ret != FmErrNone ) {
|
46
|
181 |
emit driveSpaceChanged();
|
37
|
182 |
emit notifyError( ret, mErrString );
|
|
183 |
return;
|
|
184 |
}
|
46
|
185 |
}
|
|
186 |
emit driveSpaceChanged();
|
37
|
187 |
emit notifyFinish();
|
|
188 |
}
|
|
189 |
|
|
190 |
/*
|
|
191 |
* Copy or move the \a source to \a targetPath
|
|
192 |
* with \a newTargetName
|
|
193 |
*/
|
|
194 |
int FmOperationCopyOrMove::copyOrMove( const QString &source, const QString &targetPath,
|
|
195 |
const QString &newTargetName )
|
|
196 |
{
|
|
197 |
if( *mStop ) {
|
|
198 |
return FmErrCancel;
|
|
199 |
}
|
|
200 |
|
|
201 |
QFileInfo fi( source );
|
|
202 |
if( !fi.exists() ) {
|
|
203 |
mErrString = source;
|
|
204 |
return FmErrSrcPathDoNotExist;
|
|
205 |
}
|
|
206 |
QString newName;
|
|
207 |
if( !newTargetName.isEmpty() ) {
|
|
208 |
newName = targetPath + newTargetName;
|
|
209 |
} else {
|
|
210 |
newName = targetPath + fi.fileName();
|
|
211 |
}
|
|
212 |
|
|
213 |
int ret = FmErrNone;
|
|
214 |
|
|
215 |
if ( fi.isFile() ) {
|
|
216 |
if ( FmUtils::getDriveLetterFromPath( source ) ==
|
|
217 |
FmUtils::getDriveLetterFromPath( targetPath ) &&
|
|
218 |
operationType() == FmOperationService::EOperationTypeMove )
|
|
219 |
{
|
|
220 |
return FmUtils::moveInsideDrive( source, newName );
|
|
221 |
|
|
222 |
}
|
|
223 |
quint64 fileSize = fi.size();
|
|
224 |
ret = copyOneFile( source, newName );
|
|
225 |
if (ret != FmErrNone) {
|
|
226 |
mErrString = source;
|
|
227 |
return ret;
|
|
228 |
}
|
|
229 |
if ( operationType() == FmOperationService::EOperationTypeMove
|
|
230 |
&& !QFile::remove( source ) ) {
|
|
231 |
mErrString = source;
|
|
232 |
return FmErrCannotRemove;
|
|
233 |
}
|
|
234 |
} else if ( fi.isDir() ) {
|
|
235 |
if( operationType() == FmOperationService::EOperationTypeMove
|
|
236 |
&& FmUtils::isDefaultFolder( source ) ){
|
|
237 |
ret = FmErrRemoveDefaultFolder;
|
|
238 |
}
|
|
239 |
else{
|
|
240 |
ret = copyOrMoveDirInsideContent( source, newName );
|
|
241 |
}
|
|
242 |
if( ret!= FmErrNone ) {
|
|
243 |
return ret;
|
|
244 |
}
|
|
245 |
if ( operationType() == FmOperationService::EOperationTypeMove
|
|
246 |
&& !fi.dir().rmdir( fi.absoluteFilePath() ) ) {
|
|
247 |
mErrString = fi.absolutePath();
|
|
248 |
return FmErrCannotRemove;
|
|
249 |
}
|
|
250 |
} else {
|
|
251 |
qWarning( "Things other than file and directory are not copied" );
|
|
252 |
ret = FmErrIsNotFileOrFolder;
|
|
253 |
}
|
|
254 |
|
|
255 |
return ret;
|
|
256 |
}
|
|
257 |
|
|
258 |
/*
|
|
259 |
copy \a srcPath as \a destPath
|
|
260 |
both \a srcPath and \a destPath are Directory
|
|
261 |
*/
|
|
262 |
int FmOperationCopyOrMove::copyOrMoveDirInsideContent( const QString &srcPath, const QString &destPath )
|
|
263 |
{
|
|
264 |
QFileInfo srcInfo( srcPath );
|
|
265 |
QFileInfo destInfo( destPath );
|
|
266 |
|
|
267 |
if( FmUtils::isSubLevelPath( srcPath, destPath ) ) {
|
|
268 |
mErrString = destPath;
|
|
269 |
if ( operationType() == FmOperationService::EOperationTypeMove ) {
|
|
270 |
return FmErrMoveDestToSubFolderInSrc;
|
|
271 |
} else {
|
|
272 |
return FmErrCopyDestToSubFolderInSrc;
|
|
273 |
}
|
|
274 |
|
|
275 |
}
|
|
276 |
|
|
277 |
if( !srcInfo.isDir() || !srcInfo.exists() ) {
|
|
278 |
mErrString = srcPath;
|
|
279 |
return FmErrSrcPathDoNotExist;
|
|
280 |
}
|
|
281 |
|
|
282 |
if( !destInfo.exists() ) {
|
|
283 |
if( !destInfo.dir().mkdir( destInfo.absoluteFilePath() ) ) {
|
|
284 |
mErrString = destPath;
|
|
285 |
return FmErrCannotMakeDir;
|
|
286 |
}
|
|
287 |
}
|
|
288 |
destInfo.setFile( destPath );
|
|
289 |
if( !destInfo.isDir() ) {
|
|
290 |
mErrString = destPath;
|
|
291 |
return FmErrCannotMakeDir;
|
|
292 |
}
|
|
293 |
|
|
294 |
//start to copy
|
|
295 |
QFileInfoList infoList = QDir( srcPath ).entryInfoList( QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden | QDir::System );
|
|
296 |
while( !infoList.isEmpty() ) {
|
|
297 |
if( *mStop ) {
|
|
298 |
return FmErrCancel;
|
|
299 |
}
|
|
300 |
|
|
301 |
QFileInfo fileInfo = infoList.takeFirst();
|
|
302 |
if( fileInfo.isFile() ){
|
|
303 |
//copy file
|
|
304 |
QString newFilePath = destPath + fileInfo.absoluteFilePath().mid( srcPath.length() );
|
|
305 |
int ret = copyOneFile( fileInfo.absoluteFilePath(), newFilePath );
|
|
306 |
if ( ret != FmErrNone ) {
|
|
307 |
mErrString = fileInfo.absoluteFilePath();
|
|
308 |
return ret;
|
|
309 |
}
|
|
310 |
if( operationType() == FmOperationService::EOperationTypeMove
|
|
311 |
&& !QFile::remove( fileInfo.absoluteFilePath() ) ) {
|
|
312 |
mErrString = fileInfo.absoluteFilePath();
|
|
313 |
return FmErrCannotRemove;
|
|
314 |
}
|
|
315 |
} else if( fileInfo.isDir() ) {
|
|
316 |
//makedir
|
|
317 |
QString newDirPath = destPath + fileInfo.absoluteFilePath().mid( srcPath.length() );
|
|
318 |
if( !QDir( newDirPath ).exists() && !QDir( destPath ).mkdir( newDirPath ) ) {
|
|
319 |
mErrString = newDirPath;
|
|
320 |
return FmErrCannotMakeDir;
|
|
321 |
}
|
|
322 |
// add dir content to list.
|
|
323 |
QFileInfoList infoListDir = QDir( fileInfo.absoluteFilePath() ).entryInfoList(
|
|
324 |
QDir::NoDotAndDotDot | QDir::AllEntries );
|
|
325 |
if ( operationType() == FmOperationService::EOperationTypeMove ) {
|
|
326 |
if( infoListDir.isEmpty() ) {
|
|
327 |
if ( !fileInfo.dir().rmdir( fileInfo.absoluteFilePath() ) ) {
|
|
328 |
mErrString = fileInfo.absolutePath();
|
|
329 |
return FmErrCannotRemove;
|
|
330 |
}
|
|
331 |
} else {
|
|
332 |
infoList.push_front( fileInfo );
|
|
333 |
}
|
|
334 |
}
|
|
335 |
while( !infoListDir.isEmpty() ) {
|
|
336 |
infoList.push_front( infoListDir.takeLast() );
|
|
337 |
}
|
|
338 |
|
|
339 |
} else {
|
|
340 |
mErrString = fileInfo.absoluteFilePath();
|
|
341 |
return FmErrIsNotFileOrFolder;
|
|
342 |
}
|
|
343 |
|
|
344 |
}
|
|
345 |
|
|
346 |
return FmErrNone;
|
|
347 |
}
|
|
348 |
|
|
349 |
/*
|
|
350 |
* Increase the progress bar
|
|
351 |
* \a size the current copy or moved size
|
|
352 |
*/
|
|
353 |
void FmOperationCopyOrMove::increaseProgress( quint64 size )
|
|
354 |
{
|
|
355 |
if( mTotalSize <=0 ) {
|
|
356 |
return;
|
|
357 |
}
|
|
358 |
mCopiedOrMovedSize += size;
|
|
359 |
int step = ( mCopiedOrMovedSize * FmTotalProgressBarSteps ) / mTotalSize;
|
|
360 |
if( step > mCurrentStep ) {
|
|
361 |
mCurrentStep = step;
|
|
362 |
emit notifyProgress( mCurrentStep );
|
|
363 |
}
|
|
364 |
}
|
|
365 |
|
|
366 |
/*
|
|
367 |
* Emits askForRename signal.
|
|
368 |
* \a srcFile the source file path.
|
|
369 |
* \a destFile get the new name from user input
|
|
370 |
*/
|
|
371 |
void FmOperationCopyOrMove::queryForRename( const QString &srcFile, QString *destFile )
|
|
372 |
{
|
|
373 |
emit askForRename( srcFile, destFile );
|
|
374 |
}
|
|
375 |
|
|
376 |
/*
|
|
377 |
* Copies one file from \a srcFile to \a desFile
|
|
378 |
*/
|
|
379 |
int FmOperationCopyOrMove::copyOneFile( const QString &srcFile, const QString &desFile )
|
|
380 |
{
|
|
381 |
QFile src( srcFile );
|
|
382 |
QFile des( desFile );
|
|
383 |
if ( !src.open( QIODevice::ReadOnly ) || !des.open( QIODevice::WriteOnly ) ) {
|
|
384 |
return FmErrCannotCopy;
|
|
385 |
}
|
|
386 |
QDataStream outputStream( &src );
|
|
387 |
QDataStream inputStream( &des );
|
|
388 |
//used to cache data from source file to target file during one copy
|
|
389 |
QScopedPointer<char> tempString( new char[FmOneTimeCopyOrMoveSize] );
|
|
390 |
memset( tempString.data(), 0, FmOneTimeCopyOrMoveSize );
|
|
391 |
while ( !outputStream.atEnd() ) {
|
|
392 |
if ( *mStop ) {
|
|
393 |
src.close();
|
|
394 |
des.close();
|
|
395 |
QFile::remove( desFile );
|
|
396 |
return FmErrCancel;
|
|
397 |
}
|
|
398 |
int ret = outputStream.readRawData(tempString.data(), FmOneTimeCopyOrMoveSize );
|
|
399 |
if (ret == -1) {
|
|
400 |
src.close();
|
|
401 |
des.close();
|
|
402 |
QFile::remove( desFile );
|
|
403 |
return FmErrCannotCopy;
|
|
404 |
}
|
|
405 |
ret = inputStream.writeRawData(tempString.data(), ret);
|
|
406 |
if (ret == -1) {
|
|
407 |
src.close();
|
|
408 |
des.close();
|
|
409 |
QFile::remove( desFile );
|
|
410 |
return FmErrCannotCopy;
|
|
411 |
}
|
|
412 |
memset( tempString.data(), 0, FmOneTimeCopyOrMoveSize );
|
|
413 |
increaseProgress( ret );
|
|
414 |
}
|
|
415 |
src.close();
|
|
416 |
des.close();
|
|
417 |
if ( FmUtils::setFileAttributes( srcFile, desFile ) != FmErrNone ) {
|
|
418 |
QFile::remove( desFile );
|
|
419 |
return FmErrCannotCopy;
|
|
420 |
}
|
|
421 |
return FmErrNone;
|
|
422 |
}
|
|
423 |
|
|
424 |
/*
|
|
425 |
* Prepare some work before starting operation.
|
|
426 |
* Returns error number.
|
|
427 |
*/
|
|
428 |
int FmOperationCopyOrMove::prepare()
|
|
429 |
{
|
|
430 |
if( mSourceList.empty() ) {
|
|
431 |
return FmErrWrongParam;
|
|
432 |
} else {
|
|
433 |
return FmErrNone;
|
|
434 |
}
|
|
435 |
}
|
|
436 |
|
|
437 |
bool FmOperationCopyOrMove::targetHasEnoughSpace()
|
|
438 |
{
|
|
439 |
QString sourceDrive = FmUtils::getDriveLetterFromPath( mSourceList.front() );
|
|
440 |
QString targetDrive = FmUtils::getDriveLetterFromPath( mTargetPath );
|
|
441 |
if ( sourceDrive == targetDrive &&
|
|
442 |
operationType() == FmOperationService::EOperationTypeMove ) {
|
|
443 |
return true;
|
|
444 |
} else {
|
|
445 |
return FmUtils::hasEnoughSpace( targetDrive, mTotalSize );
|
|
446 |
}
|
|
447 |
}
|