| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 
 | /***************************************************************************
                                CopyThread.cpp
                                -------------------
 
     Begin      : Fri Dec 21 2007 22:48 alpha_one_x86
     Project    : Ultracopier
     Email      : alpha.super-one@laposte.net
     Note        : See README for copyright and developer
     Target      : Define the class of the copy threads
 
****************************************************************************/
 
#include "env.h"
 
#include "CopyThread.h"
 
/*! \todo checksun while copy
  \todo remove empty source folder in moving mode
  \todo support folder empty in copy and move mode
  \warning check empty dest folder is created
  \todo call copyThread::flush() at the end of copy
  \todo adaptative total size
  \todo, when skip remove destination
  */
 
/*! \brief Initialise the copy thread
*/
copyThread::copyThread()
{
	RightCopy=false;
	FileName="";
	maxSpeed=0;
	connect(&clockForTheCopySpeed, SIGNAL(timeout()), this, SLOT(timeOfTheBlockCopyFinished()));
	stopped = true;
	movingMode=false;
	blockSizeCurrent=64;
	tooLateForWait=false;
	preallocation=false;
	previousSize=0;
	intervalCopySpeed.start();
	keepDate=false;
	waitInPause=false;
	theCurrentStat=Stopped;
	sourceFile=new QFile("");							///< Pointer on the source file
	destinationFile=new QFile("");
	totalCopiedSize=0;
	previousSizeReturned=0;
}
 
/// \brief set action on file exist
void copyThread::fileExistsAction(int action)
{
	actionAfterUnlock=action;
	resumeCopy();
}
 
/// \brief set action on error
void copyThread::errorAction(int action)
{
	actionAfterUnlock=action;
	resumeCopy();
}
 
//pause the copy
void copyThread::pauseCopy()
{
	if(currentStat!=copyThread::Paused)
		waitInPause=true;
	else
	{
		DEBUGCONSOLE(10,"copyThread::pauseCopy","Copy thread already in pause");
	}
}
 
//resume the copy
void copyThread::resumeCopy()
{
	if(currentStat==copyThread::Paused)
	{
		controlMutex.wakeAll();
		waitInPause=false;
	}
	else
	{
		DEBUGCONSOLE(10,"copyThread::resumeCopy","Copy thread not in pause");
	}
}
 
/** \brief set block size
\param block the new block size
\return Return true if succes */
bool copyThread::setBlockSize(const int block)
{
	DEBUGCONSOLE(50,"copyThread::setBlockSize","Set new block size: "+QString::number(block));
	if(block<1 || block>16384)
	{
		blockSizeCurrent=block*1024;
		//set the new max speed because the timer have changed
		setMaxSpeed(maxSpeed);
		return true;
	}
	else
		return false;
}
 
/** \brief set keep date
\param value The value setted */
void copyThread::setKeepDate(const int value)
{
	keepDate=value;
}
 
/** \brief Add item to list
\param source The source
\param destination The folder or file destination
*/
void copyThread::addToList(const QString& source,const QString& destination)
{
	QList<QString> oneEntry;
	oneEntry.append(source);
	oneEntry.append(destination);
	theCopyList.append(oneEntry);
}
 
/** \brief return the progression percent
\param max the return value it should be beteen 0 and max value
\return the file progression
*/
int copyThread::theProgressionPercent(const int max)
{
	if(!sourceFile->isOpen() || !destinationFile->isOpen())
		return 0;
	else
		return (int)((((double)(destinationFile->pos()))/(sourceFile->size()))*max);
}
 
/** \brief get copy speed in byte per second
\return Return the speed in byte per second */
qint64 copyThread::getCopySpeed()
{
	//for have fixed value (not change durring copy)
	qint64 totalCopiedSize=this->totalCopiedSize;
	//for calcul copied size
	qint64 returnedValue=totalCopiedSize-previousSizeReturned;
	if(returnedValue==0)
		return 0;
	//get elapsed time
	int timeFromNextRestart=intervalCopySpeed.elapsed();
	if(timeFromNextRestart<=0)
		return -1;
	else
	{
		returnedValue=(returnedValue*1000/timeFromNextRestart);
		intervalCopySpeed.restart();
	}
	previousSizeReturned=totalCopiedSize;
	return returnedValue;
}
 
/** \brief set prealoc file size
\param prealloc True if should be prealloc */
void copyThread::setPreallocateFileSize(const bool prealloc)
{
	preallocation=prealloc;
}
 
/*! \brief Set if in copy or move mode
\param setMove If true is in moving mode
*/
void copyThread::setMovingMode(const bool setMove)
{
	DEBUGCONSOLE(30,"copyThread::setMovingMode","Set copy thread as moving mode");
	movingMode=setMove;
}
 
/*! \brief Set the copy info and options before runing
\param doRightCopy If true copy the right of the file
*/
void copyThread::setRightCopy(const int doRightCopy)
{
	#if (DEBUG>0)
	if(doRightCopy)
		DEBUGCONSOLE(70,"copyThread::setRightCopy","doRightCopy: true");
	else
		DEBUGCONSOLE(70,"copyThread::setRightCopy","doRightCopy: false");
	#endif
	RightCopy=doRightCopy;
}
 
/*! \brief Set if need compare check sum
\param doCheckSum If true check the checksum of the files after the copy
*/
void copyThread::setCheckSum(int doCheckSum)
{
	#if (DEBUG>0)
	if(doCheckSum)
	{
		DEBUGCONSOLE(50,"copyThread::setCheckSum","doCheckSum: true");
	}
	else
	{
		DEBUGCONSOLE(50,"copyThread::setCheckSum","doCheckSum: false");
	}
	#endif
	CheckSum=doCheckSum;
}
 
/// \brief For give timer envery X ms
void copyThread::timeOfTheBlockCopyFinished()
{
	tooLateForWait=true;
	waitTimerIsEnd.wakeAll();
}
 
/*! \brief Add one entry to dir list
\param theDir Is set for define the source folder, it need be requested in moving mode
\see movingMode and AddFolderThread::putToDirList()
*/
void copyThread::addEntryToDirList(const QString& theDir)
{
	if(movingMode)
	{
		DEBUGCONSOLE(90,"copyThread::addEntryToDirList","dir: \""+theDir+"\"");
		sourceDirList<<theDir;
	}
	else
	{
		DEBUGCONSOLE(10,"copyThread::addEntryToDirList","copy mode, can't have an folder here");
	}
}
 
/** \brief add one entry to empty dir destination list
\param theDir Which empty destination folder should be created  */
void copyThread::addEntryToEmptyDestinationDirList(const QString& theDir)
{
	emptyDestDir<<theDir;
}
 
/** \brief Remove all the source empty folder
\return Return true if success */
bool copyThread::removeAllFolderEmpty(const QString& TheDir)
{
	DEBUGCONSOLE(90,"copyThread::removeAllFolderEmpty","The dir: "+TheDir);
	bool ok=true;
	QDir dir(TheDir);
	QFileInfoList list = dir.entryInfoList(QDir::AllEntries|QDir::NoDotAndDotDot|QDir::Hidden|QDir::System,QDir::DirsFirst);
	if(list.isEmpty())
	{
		DEBUGCONSOLE(90,"copyThread::removeAllFolderEmpty","The folder is empty");
	}
	else
	{
		DEBUGCONSOLE(90,"copyThread::removeAllFolderEmpty","Listing");
		for (int i = 0; i < list.size(); ++i)
		{
			QFileInfo fileInfo(list.at(i));
			if(!fileInfo.isDir())
			{
				DEBUGCONSOLE(90,"copyThread::removeAllFolderEmpty","found a file: "+fileInfo.fileName());
			}
			else
			{
				//return the fonction for scan the new folder
				ok=ok && removeAllFolderEmpty(TheDir+fileInfo.fileName()+'/');
				ok=ok && dir.rmdir(TheDir+fileInfo.fileName()+'/');
			}
		}
	}
	return ok;
}
 
/// \brief Flush the last thread
bool copyThread::flush(bool skipFirstOnError)
{
	DEBUGCONSOLE(90,"copyThread::flush","start");
	bool stateOK=true;
	tempSource="";
 
	//create here the empty destination folder
	while(!emptyDestDir.isEmpty())
	{
		DEBUGCONSOLE(90,"copyThread::flush","emptyDestDir.isEmpty() is not empty");
		dest_dir_folder->setPath(emptyDestDir.at(0));
		if(!dest_dir_folder->exists())
		{
			if(!dest_dir_folder->mkpath(emptyDestDir.at(0)))
			{
				DEBUGCONSOLE(10,"copyThread::flush","Unable to create the folder: "+emptyDestDir.at(0));
				setErrorMessage(tr("Unable to create the folder: ")+emptyDestDir.at(0));
				if(skipFirstOnError)
					skipFirstOnError=false;
				else
				{
					emit errorOnFlush(emptyDestDir.at(0));
					return false;
				}
				stateOK=false;
			}
			else
			{
				DEBUGCONSOLE(90,"copyThread::flush","Folder created: "+emptyDestDir.at(0));
			}
		}
		else
		{
			DEBUGCONSOLE(90,"copyThread::flush","dest_dir_folder->exists() not exists");
		}
		emptyDestDir.removeAt(0);
	}
 
	//remove here the empty source folder when is in moving mode
	if(movingMode)
	{
		// remove recusively here the sourceDirList list
		while(!sourceDirList.isEmpty())
		{
			DEBUGCONSOLE(90,"copyThread::flush","Clean this folder for removing: "+sourceDirList.at(0));
			if(!removeAllFolderEmpty(sourceDirList.at(0)))
				stateOK=false;
			if(sourceDirList.at(0)!=getMountPoint(sourceDirList.at(0)))
			{
				QDir *thedir = new QDir();
				if(!thedir->rmdir(sourceDirList.at(0)))
				{
					DEBUGCONSOLE(10,"copyThread::flush","Unable to remove the folder: "+sourceDirList.at(0));
					setErrorMessage(tr("Unable to remove the folder: ")+sourceDirList.at(0));
					if(skipFirstOnError)
						skipFirstOnError=false;
					else
					{
						emit errorOnFlush(sourceDirList.at(0));
						return false;
					}
				}
			}
			else
			{
				DEBUGCONSOLE(10,"copyThread::flush","Folder is the mount point: "+sourceDirList.at(0));
			}
			sourceDirList.removeAt(0);
		}
	}
 
	return stateOK;
}
 
/** \brief try open file in special mode
\param theFile Pointer on the file
\param theModeShouldBeUsed Wich mode should be used
*/
bool copyThread::tryOpenFileInSpecialMode(QFile *theFile,QIODevice::OpenMode theModeShouldBeUsed)
{
        DEBUGCONSOLE(90,"copyThread::tryOpenFileInSpecialMode","start");
	//if file is already open
	if(theFile->isOpen())
	{
		//check the open mode
		if(theFile->openMode()!=theModeShouldBeUsed)
		{
			theFile->close();
			return theFile->open(theModeShouldBeUsed);
		}
		else
			return true;
	}
	else
		return theFile->open(theModeShouldBeUsed);
}
 
/*! \brief Set the max speed
\param tempMaxSpeed Set the max speed in KB/s, 0 for no limit */
void copyThread::setMaxSpeed(const int tempMaxSpeed)
{
	if(tempMaxSpeed>=0)
	{
		maxSpeed=tempMaxSpeed;
		DEBUGCONSOLE(50,"copyThread::setMaxSpeed",QString("tempMaxSpeed=")+QString::number(tempMaxSpeed));
		if(maxSpeed>0)
		{
			int NewInterval,newMultForBigSpeed=0;
			do
			{
				newMultForBigSpeed++;
				NewInterval=(blockSizeCurrent*newMultForBigSpeed)/(maxSpeed);
			}
			while (NewInterval<MINTIMERINTERVAL);
			MultForBigSpeed=newMultForBigSpeed;
			if(clockForTheCopySpeed.isActive())
			{
				clockForTheCopySpeed.stop();
				clockForTheCopySpeed.setInterval(NewInterval);
				clockForTheCopySpeed.start();
			}
			else
				clockForTheCopySpeed.setInterval(NewInterval);
			DEBUGCONSOLE(90,"copyThread::setMaxSpeed","setInterval at: "+QString::number(NewInterval)+"ms for "+QString::number(MultForBigSpeed)+" block(s).");
			DEBUGCONSOLE(90,"copyThread::setMaxSpeed","for speed of: "+QString::number(((blockSizeCurrent*MultForBigSpeed)/NewInterval))+"KB/s");
			if(!clockForTheCopySpeed.isActive() && this->isRunning())
				clockForTheCopySpeed.start();
		}
		else
		{
			if(clockForTheCopySpeed.isActive())
				clockForTheCopySpeed.stop();
			waitTimerIsEnd.wakeAll();
		}
	}
}
 
/*! \brief Query for stop the current thread
\see stopIt, run() and stop()
*/
void copyThread::stopTheCopy()
{
	DEBUGCONSOLE(50,"copyThread::stopTheCopy","stopTheCopy");
	if(!stopIt)
	{
		stopIt=true;
		if(clockForTheCopySpeed.isActive())
		{
			clockForTheCopySpeed.stop();
		}
		DEBUGCONSOLE(90,"copyThread::stopTheCopy","wakeAll");
		waitTimerIsEnd.wakeAll();
	}
	if(currentStat==copyThread::Paused)
		controlMutex.wakeAll();
}
 
/*! \brief Run the copy
\warning The pointer sould be given and thread initilised
\see QObjectOfMwindow(), stopTheCopy() and stop()
*/
void copyThread::run()
{
// ...
}
 
/*! \brief Stop the copy thread
\see stopIt, stopTheCopy() and run()
*/
void copyThread::stop()
{
	stopTheCopy();
	stopped = true;
}
 
/** \brief The destructor
\see copyThread()
*/
copyThread::~copyThread()
{
	stopTheCopy();
	currentStat=copyThread::Paused;
	delete sourceFile;
	delete destinationFile;
	delete dest_dir_folder;
} | 
Partager