summaryrefslogtreecommitdiff
path: root/plugins/CopyEngine/Ultracopier-0.3/scanFileOrFolder.cpp
blob: 4d13fab9145cb560e83757cc2a127b09bd4acf40 (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
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
#include "scanFileOrFolder.h"

#include <QMessageBox>

scanFileOrFolder::scanFileOrFolder(CopyMode mode)
{
	stopped	= true;
	stopIt	= false;
	this->mode=mode;
	setObjectName("ScanFileOrFolder");
	folder_isolation=QRegExp("^(.*/)?([^/]+)/$");
}

scanFileOrFolder::~scanFileOrFolder()
{
	stop();
	quit();
	wait();
}

bool scanFileOrFolder::isFinished()
{
	return stopped;
}

void scanFileOrFolder::addToList(const QStringList& sources,const QString& destination)
{
	stopIt=false;
	this->sources=sources;
        this->destination=destination;
	if(sources.size()>1 || QFileInfo(destination).isDir())
		/* Disabled because the separator transformation product bug
		 * if(!destination.endsWith(QDir::separator()))
			this->destination+=QDir::separator();*/
		if(!destination.endsWith("/") && !destination.endsWith("\\"))
			this->destination+="/";//put unix separator because it's transformed into that's under windows too
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"addToList("+sources.join(";")+","+destination+")");
}

//set action if Folder are same or exists
void scanFileOrFolder::setFolderExistsAction(FolderExistsAction action,QString newName)
{
	this->newName=newName;
	folderExistsAction=action;
	waitOneAction.release();
}

//set action if error
void scanFileOrFolder::setFolderErrorAction(FileErrorAction action)
{
	fileErrorAction=action;
	waitOneAction.release();
}

void scanFileOrFolder::stop()
{
	stopIt=true;
	waitOneAction.release();
}

void scanFileOrFolder::run()
{
	stopped=false;
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start the listing with destination: "+destination+", mode: "+QString::number(mode));
        QDir destinationFolder(destination);
	int sourceIndex=0;
	while(sourceIndex<sources.size())
	{
		ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"size source to list: "+QString::number(sourceIndex)+"/"+QString::number(sources.size()));
		if(stopIt)
		{
			stopped=true;
			return;
		}
		QFileInfo source=sources.at(sourceIndex);
		if(source.isDir())
		{
			/* Bad way; when you copy c:\source\folder into d:\destination, you wait it create the folder d:\destination\folder
			//listFolder(source.absoluteFilePath()+QDir::separator(),destination);
			listFolder(source.absoluteFilePath()+"/",destination);//put unix separator because it's transformed into that's under windows too
			*/
			//put unix separator because it's transformed into that's under windows too
                        listFolder(source.absolutePath()+"/",destinationFolder.absolutePath()+"/",source.fileName()+"/",source.fileName()+"/");
		}
		else
			emit fileTransfer(source,destination+source.fileName(),mode);
		sourceIndex++;
	}
	stopped=true;
	if(stopIt)
		return;
	emit finishedTheListing();
}

void scanFileOrFolder::listFolder(const QString& source,const QString& destination,const QString& sourceSuffixPath,QString destinationSuffixPath)
{
        ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"source: "+source+", destination: "+destination+", sourceSuffixPath: "+sourceSuffixPath+", destinationSuffixPath: "+destinationSuffixPath);
	if(stopIt)
		return;
        QString newSource	= source+sourceSuffixPath;
        QString finalDest	= destination+destinationSuffixPath;
	//if is same
	if(newSource==finalDest)
	{
		QDir dirSource(newSource);
		emit folderAlreadyExists(dirSource.absolutePath(),finalDest,true);
		waitOneAction.acquire();
		switch(folderExistsAction)
		{
			case FolderExists_Merge:
			break;
			case FolderExists_Skip:
				return;
			break;
			case FolderExists_Rename:
				if(newName=="")
				{
					/// \todo use facility here
					if(destinationSuffixPath.contains(folder_isolation))
					{
						prefix=destinationSuffixPath;
						suffix=destinationSuffixPath;
						prefix.replace(folder_isolation,"\\1");
						suffix.replace(folder_isolation,"\\2");
						ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"pattern: "+folder_isolation.pattern());
						ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"full: "+destinationSuffixPath);
						ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"prefix: "+prefix);
						ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"suffix: "+suffix);
						destinationSuffixPath = prefix+tr("Copy of ")+suffix;
					}
					else
						destinationSuffixPath = tr("Copy of ")+"Unknow";
				}
				else
					destinationSuffixPath = newName+"/";
				destinationSuffixPath+="/";
				finalDest = destination+destinationSuffixPath;
			break;
			default:
				return;
			break;
		}
	}
	//check if destination exists
	if(checkDestinationExists)
	{
		QDir finalSource(newSource);
		QDir destinationDir(finalDest);
		if(destinationDir.exists())
		{
			emit folderAlreadyExists(finalSource.absolutePath(),destinationDir.absolutePath(),false);
			waitOneAction.acquire();
			switch(folderExistsAction)
			{
				case FolderExists_Merge:
				break;
				case FolderExists_Skip:
					return;
				break;
				case FolderExists_Rename:
					if(newName=="")
					{
						/// \todo use facility here
						if(destinationSuffixPath.contains(folder_isolation))
						{
							prefix=destinationSuffixPath;
							suffix=destinationSuffixPath;
							prefix.replace(folder_isolation,"\\1");
							suffix.replace(folder_isolation,"\\2");
							ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"pattern: "+folder_isolation.pattern());
							ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"full: "+destinationSuffixPath);
							ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"prefix: "+prefix);
							ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"suffix: "+suffix);
							destinationSuffixPath = prefix+tr("Copy of ")+suffix;
						}
						else
							destinationSuffixPath = tr("Copy of ")+"Unknow";
					}
					else
						destinationSuffixPath = newName;
					destinationSuffixPath+="/";
					finalDest = destination+destinationSuffixPath;
				break;
				default:
					return;
				break;
			}
		}
	}
	//do source check
	QDir finalSource(newSource);
	QFileInfo dirInfo(newSource);
	//check of source is readable
	do
	{
		fileErrorAction=FileError_NotSet;
		if(!dirInfo.isReadable() || !dirInfo.isExecutable() || !dirInfo.exists())
		{
			if(!dirInfo.exists())
				emit errorOnFolder(dirInfo,tr("The folder not exists"));
			else
				emit errorOnFolder(dirInfo,tr("The folder is not readable"));
			waitOneAction.acquire();
			ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"actionNum: "+QString::number(fileErrorAction));
		}
	} while(fileErrorAction==FileError_Retry);
	/// \todo check here if the folder is not readable or not exists
	QFileInfoList entryList=finalSource.entryInfoList(QDir::AllEntries|QDir::NoDotAndDotDot|QDir::Hidden|QDir::System,QDir::DirsFirst);//possible wait time here
	int sizeEntryList=entryList.size();
	emit newFolderListing(newSource);
	if(sizeEntryList==0)
		emit addToMkPath(finalDest);
	for (int index=0;index<sizeEntryList;++index)
	{
		if(stopIt)
			return;
		QFileInfo fileInfo=entryList.at(index);
		if(fileInfo.isDir())//possible wait time here
			//listFolder(source,destination,suffixPath+fileInfo.fileName()+QDir::separator());
			listFolder(source,destination,sourceSuffixPath+fileInfo.fileName()+"/",destinationSuffixPath+fileInfo.fileName()+"/");//put unix separator because it's transformed into that's under windows too
		else
			emit fileTransfer(fileInfo.absoluteFilePath(),finalDest+fileInfo.fileName(),mode);
	}
	if(mode==Move)
	{
		ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"newSource: "+newSource+", sizeEntryList: "+QString::number(sizeEntryList));
		emit addToRmPath(newSource,sizeEntryList);
	}
}

//set if need check if the destination exists
void scanFileOrFolder::setCheckDestinationFolderExists(const bool checkDestinationFolderExists)
{
	this->checkDestinationExists=checkDestinationFolderExists;
}