summaryrefslogtreecommitdiff
path: root/plugins/CopyEngine/Ultracopier-0.3/RmPath.cpp
blob: a65fb9710d95a1321d4c2df9f843ddaf75a715ad (plain)
1
2
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
#include "RmPath.h"

RmPath::RmPath()
{
	stopIt=false;
	waitAction=false;
	setObjectName("RmPath");
	moveToThread(this);
	start();
}

RmPath::~RmPath()
{
	stopIt=true;
	quit();
	wait();
}

void RmPath::addPath(const QString &path)
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start: "+path);
	if(stopIt)
		return;
	emit internalStartAddPath(path);
}

void RmPath::skip()
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	emit internalStartSkip();
}

void RmPath::retry()
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	emit internalStartRetry();
}

void RmPath::run()
{
	connect(this,SIGNAL(internalStartAddPath(QString)),this,SLOT(internalAddPath(QString)),Qt::QueuedConnection);
	connect(this,SIGNAL(internalStartDoThisPath()),this,SLOT(internalDoThisPath()),Qt::QueuedConnection);
	connect(this,SIGNAL(internalStartSkip()),this,SLOT(internalSkip()),Qt::QueuedConnection);
	connect(this,SIGNAL(internalStartRetry()),this,SLOT(internalRetry()),Qt::QueuedConnection);
	exec();
}

void RmPath::internalDoThisPath()
{
	if(waitAction || pathList.isEmpty())
		return;
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start: "+pathList.first());
	if(!rmpath(pathList.first()))
	{
		if(stopIt)
			return;
		waitAction=true;
		ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"Unable to remove the folder: "+pathList.first());
		emit errorOnFolder(pathList.first(),tr("Unable to remove the folder"));
		return;
	}
	pathList.removeFirst();
	emit firstFolderFinish();
	checkIfCanDoTheNext();
}

/** remplace QDir::rmpath() because it return false if the folder not exists
  and seam bug with parent folder */
bool RmPath::rmpath(const QDir &dir)
{
	if(!dir.exists())
		return true;
	bool allHaveWork=true;
	QFileInfoList list = dir.entryInfoList(QDir::AllEntries|QDir::NoDotAndDotDot|QDir::Hidden|QDir::System,QDir::DirsFirst);
	for (int i = 0; i < list.size(); ++i)
	{
		QFileInfo fileInfo(list.at(i));
		if(!fileInfo.isDir())
		{
			ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"found a file: "+fileInfo.fileName());
			allHaveWork=false;
		}
		else
		{
			//return the fonction for scan the new folder
			if(!rmpath(dir.absolutePath()+'/'+fileInfo.fileName()+'/'))
				allHaveWork=false;
		}
	}
	if(!allHaveWork)
		return allHaveWork;
	allHaveWork=dir.rmdir(dir.absolutePath());
	if(!allHaveWork)
		ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"unable to remove the folder: "+dir.absolutePath());
	return allHaveWork;
}

void RmPath::internalAddPath(const QString &path)
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start: "+path);
	pathList << path;
	if(!waitAction)
		checkIfCanDoTheNext();
}

void RmPath::checkIfCanDoTheNext()
{
	if(!waitAction && !stopIt && pathList.size()>0)
		emit internalStartDoThisPath();
}

void RmPath::internalSkip()
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	waitAction=false;
	pathList.removeFirst();
	emit firstFolderFinish();
	checkIfCanDoTheNext();
}

void RmPath::internalRetry()
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	waitAction=false;
	checkIfCanDoTheNext();
}