Use configured resolution for login/outgame/ingame
[ryzomcore.git] / ryzom / tools / client / ryzom_installer / src / filescopier.cpp
blob8cfb3c0d5163257f48f9608ca9a4c471af0d83be
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
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.
8 //
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/>.
17 #include "stdpch.h"
18 #include "filescopier.h"
19 #include "utils.h"
20 #include "operation.h"
22 #include "nel/misc/path.h"
24 #ifdef DEBUG_NEW
25 #define new DEBUG_NEW
26 #endif
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)
53 m_files << 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);
64 FilesToCopy files;
66 // create the list of files to copy
67 CFilesCopier::getFilesList(files);
69 // copy them
70 return copyFiles(files);
73 void CFilesCopier::getFile(const QFileInfo &fileInfo, const QDir &srcDir, FilesToCopy &files) const
75 // full path to file
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;
91 if (fileInfo.isDir())
93 // create directory
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);
107 else
109 // add the file to list with all useful information
110 FileToCopy file;
111 file.filename = fileInfo.fileName();
112 file.src = fileInfo.filePath();
113 file.dst = dstPath;
114 file.size = fileInfo.size();
115 file.date = fileInfo.lastModified().toTime_t();
116 file.permissions = fileInfo.permissions();
118 files << file;
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;
152 if (m_listener)
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();
165 return true;
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));
180 return false;
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)
198 QThread::sleep(1);
200 if (m_listener)
202 m_listener->operationSuccess(totalSize);
205 return true;