1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "filescopier.h"
20 #include "operation.h"
22 #include "nel/misc/path.h"
28 CFilesCopier::CFilesCopier(IOperationProgressListener
*listener
):m_listener(listener
)
32 CFilesCopier::~CFilesCopier()
36 void CFilesCopier::setSourceDirectory(const QString
&src
)
38 m_sourceDirectory
= src
;
41 void CFilesCopier::setDestinationDirectory(const QString
&dst
)
43 m_destinationDirectory
= dst
;
46 void CFilesCopier::setIncludeFilter(const QStringList
&filter
)
48 m_includeFilter
= filter
;
51 void CFilesCopier::addFile(const QString
&filename
)
56 bool CFilesCopier::exec()
58 if (m_sourceDirectory
.isEmpty() || m_destinationDirectory
.isEmpty()) return false;
60 if (m_listener
) m_listener
->operationPrepare();
62 QDir().mkpath(m_destinationDirectory
);
66 // create the list of files to copy
67 CFilesCopier::getFilesList(files
);
70 return copyFiles(files
);
73 void CFilesCopier::getFile(const QFileInfo
&fileInfo
, const QDir
&srcDir
, FilesToCopy
&files
) const
76 QString fullPath
= fileInfo
.absoluteFilePath();
78 QString relativePath
= srcDir
.relativeFilePath(fullPath
);
80 QFileInfo
relativeFileInfo(relativePath
);
82 // correct absolute path
83 if (relativeFileInfo
.isAbsolute())
85 relativePath
= relativeFileInfo
.fileName();
88 // full path where to copy file
89 QString dstPath
= m_destinationDirectory
+ "/" + relativePath
;
94 QDir().mkpath(dstPath
);
96 QDir
subDir(fullPath
);
98 // get list of all files in directory
99 QFileInfoList entries
= subDir
.entryInfoList(QDir::AllEntries
| QDir::NoDotAndDotDot
);
101 // proces seach file recursively
102 foreach(const QFileInfo
&entry
, entries
)
104 getFile(entry
, srcDir
, files
);
109 // add the file to list with all useful information
111 file
.filename
= fileInfo
.fileName();
112 file
.src
= fileInfo
.filePath();
114 file
.size
= fileInfo
.size();
115 file
.date
= fileInfo
.lastModified().toTime_t();
116 file
.permissions
= fileInfo
.permissions();
122 void CFilesCopier::getFilesList(FilesToCopy
&files
) const
124 QDir
srcDir(m_sourceDirectory
);
126 // only copy all files from filter
127 QFileInfoList entries
= srcDir
.entryInfoList(m_includeFilter
);
129 foreach(const QFileInfo
&entry
, entries
)
131 getFile(entry
, srcDir
, files
);
134 // copy additional files
135 foreach(const QString
&fullpath
, m_files
)
137 QFileInfo
fileInfo(fullpath
);
139 getFile(fileInfo
, srcDir
, files
);
143 bool CFilesCopier::copyFiles(const FilesToCopy
&files
)
145 qint64 totalSize
= 0;
147 foreach(const FileToCopy
&file
, files
)
149 totalSize
+= file
.size
;
154 m_listener
->operationInit(0, totalSize
);
155 m_listener
->operationStart();
158 qint64 processedSize
= 0;
160 foreach(const FileToCopy
&file
, files
)
162 if (m_listener
&& m_listener
->operationShouldStop())
164 m_listener
->operationStop();
168 if (m_listener
) m_listener
->operationProgress(processedSize
, file
.filename
);
170 QFileInfo
dstFileInfo(file
.dst
);
172 if (dstFileInfo
.size() != file
.size
|| dstFileInfo
.lastModified().toTime_t() != file
.date
)
174 // force deleting previous file since it was incomplete
175 QFile::remove(file
.dst
);
177 if (!QFile::copy(file
.src
, file
.dst
))
179 if (m_listener
) m_listener
->operationFail(QApplication::tr("Unable to copy file %1 to %2").arg(file
.src
).arg(file
.dst
));
183 if (!QFile::setPermissions(file
.dst
, file
.permissions
))
185 nlwarning("Unable to change permissions of %s", Q2C(file
.dst
));
188 if (!NLMISC::CFile::setFileModificationDate(qToUtf8(file
.dst
), file
.date
))
190 nlwarning("Unable to change date of %s", Q2C(file
.dst
));
194 processedSize
+= file
.size
;
197 // wait 1 second to be sure all files have been copied (because to disk cache)
202 m_listener
->operationSuccess(totalSize
);