1 // case - file manager for N900
2 // Copyright (C) 2010 Lukas Hrazky <lukkash@email.cz>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (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 General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "operationthread.h"
26 #define BLOCK_SIZE (256 * 1024)
31 emit operationPaused(); \
36 #define SHOW_ERROR_PROMPT(promptString, fileName) \
38 if (ignoreAll[errno]) { \
42 char *realBuf = buf; \
44 strcpy(buf, tr("File is sequential").toStdString().c_str()); \
46 realBuf = strerror_r(errno, buf, 255); \
48 emit showErrorPrompt(this, promptString + " " + realBuf + ".", fileName, errno); \
53 #define ERROR_PROMPT(operation, promptString, fileName) \
56 while (!abort && operation) { \
57 SHOW_ERROR_PROMPT(promptString, fileName) \
58 if (response == IGNORE) { \
66 #define SPECIAL_COPY_ERROR_PROMPT(operation, promptString, fileName) \
68 ERROR_PROMPT(operation, promptString, fileName) \
69 if (abort || response == IGNORE) { \
71 updateProgress(fileSizeMap[path]); \
72 removeExcludeFiles.insert(path); \
79 #define OVERWRITE_PROMPT(file, newFile) \
83 while (!abort && response == NONE && newFile.exists()) { \
84 if (overwriteAll != NONE) { \
85 response = overwriteAll; \
87 emit showOverwritePrompt(this, newFile.absoluteFilePath(), \
88 newFile.isDir() && file.isDir()); \
92 else if (response == NONE) { \
93 emit showInputFilenamePrompt(this, newFile, file.isDir()); \
95 if (newNameFromDialog.size()) { \
96 newFile.setFile(newNameFromDialog); \
101 if (response == ASK) response = NONE; \
105 OperationThread::OperationThread(const QFileInfoList files, QDir dest) :
116 memset(ignoreAll, false, sizeof(ignoreAll));
120 void OperationThread::setResponse(
121 const Response response,
122 const bool applyToAll,
127 this->response = response;
131 || response == OVERWRITE
134 overwriteAll = response;
137 if (response == IGNORE) {
138 ignoreAll[err] = true;
142 if (response == ABORT) abort = true;
149 void OperationThread::processFiles(const QFileInfoList &files) {
150 for (QFileInfoList::const_iterator it = files.begin(); it != files.end(); ++it) {
158 bool OperationThread::remove(QString &fileName, const bool doUpdates) {
159 return remove(QFileInfo(fileName), doUpdates);
163 bool OperationThread::remove(const QFileInfoList &files, const bool doUpdates) {
165 for (QFileInfoList::const_iterator it = files.begin(); it != files.end(); ++it) {
166 if (!remove(*it, doUpdates)) res = false;
174 bool OperationThread::remove(const QFileInfo &file, const bool doUpdates) {
175 QString path = file.absoluteFilePath();
177 if (removeExcludeFiles.contains(path)) {
178 if (doUpdates) updateProgress(1);
182 QFSFileEngine engine(path);
184 if (doUpdates) updateFile(path);
187 if (!remove(listDirFiles(path), doUpdates)) {
188 if (doUpdates) updateProgress(1);
192 if (!listDirFiles(path).size()) {
193 ERROR_PROMPT(!engine.rmdir(path, false), tr("Error deleting directory %1."), path)
196 ERROR_PROMPT(!engine.remove(), tr("Error deleting file %1."), path)
199 if (!abort && doUpdates) updateProgress(1);
202 if (abort || response == IGNORE) return false;
207 void OperationThread::copy(const QFileInfo &file) {
208 QString path(file.absoluteFilePath());
209 QFSFileEngine engine(path);
210 QFileInfo newFile(dest.absolutePath() + "/" + file.fileName());
214 // hack to prevent asking about the same file if we already asked in the rename(...) function
215 if (overwriteAll == DONT_ASK_ONCE) {
218 OVERWRITE_PROMPT(file, newFile)
221 QString newPath(newFile.absoluteFilePath());
222 QFSFileEngine newEngine(newPath);
228 // save the overwrite response, because the response variable will get ovewritten in remove(...)
229 Response overwriteResponse = response;
231 if (newFile.exists() && !newFile.isDir()) {
232 // overwriting a file, so check for KEEP and handle it
233 if (response == KEEP) {
234 updateProgress(fileSizeMap[path]);
235 removeExcludeFiles.insert(path);
239 // if it should not be kept, remove it and return on failure
240 if(!remove(newPath)) {
241 updateProgress(fileSizeMap[path]);
244 // create new info since we deleted the file - is it needed?
245 newFile = QFileInfo(newPath);
247 // overwriting a directory - response KEEP means to keep the files inside,
248 // SKIP_DIR means to skip the dir completely
249 if (response == SKIP_DIR) {
250 updateProgress(fileSizeMap[path]);
251 removeExcludeFiles.insert(path);
256 if (!newFile.exists()) {
257 SPECIAL_COPY_ERROR_PROMPT(!engine.mkdir(newPath, false),
258 tr("Error creating directory %1."), newPath)
261 // we've done the job with the dir, so update progress and recurse into the dir
264 // change the dest for the recursion
265 QDir destBackup = dest;
268 // and set overwriteAll to the response we got a while ago
269 // because it applies to the files inside the dir
270 Response tmpResp = overwriteAll;
271 overwriteAll = overwriteResponse;
273 processFiles(listDirFiles(path));
275 overwriteAll = tmpResp;
277 ERROR_PROMPT(!newEngine.setPermissions(file.permissions()),
278 tr("Error setting permissions for directory %1."), newPath)
285 if (response == KEEP) {
286 updateProgress(fileSizeMap[path]);
287 removeExcludeFiles.insert(path);
291 SPECIAL_COPY_ERROR_PROMPT(checkSequentialFile(engine), tr("Cannot copy file %1."), path)
293 if (newFile.exists() && newFile.isDir()) {
294 SPECIAL_COPY_ERROR_PROMPT(!remove(newPath),
295 tr("Cannot replace directory %1 due to previous errors."), newPath)
298 SPECIAL_COPY_ERROR_PROMPT(!engine.open(QIODevice::ReadOnly), tr("Error reading file %1."), path)
300 bool ignore = false, newFileWritten = false;
301 while (!abort && !ignore) {
305 ERROR_PROMPT(!newEngine.open(QIODevice::WriteOnly | QIODevice::Truncate),
306 tr("Error writing file %1."), newPath)
308 if (abort || response == IGNORE) {
309 if (response == IGNORE) {
310 updateProgress(fileSizeMap[path]);
311 removeExcludeFiles.insert(path);
317 newFileWritten = true;
320 char block[BLOCK_SIZE];
323 while ((bytes = engine.read(block, sizeof(block))) > 0) {
324 if (bytes == -1 || bytes != newEngine.write(block, bytes)) {
326 SHOW_ERROR_PROMPT(tr("Error while reading from file %1."), path);
328 SHOW_ERROR_PROMPT(tr("Error while writing to file %1."), newPath);
332 if (response == IGNORE) {
333 updateProgress(fileSizeMap[path] - fileValue);
334 removeExcludeFiles.insert(path);
337 updateProgress(-fileValue);
358 if (abort || ignore) {
359 if (newFileWritten) {
363 ERROR_PROMPT(!newEngine.setPermissions(file.permissions()),
364 tr("Error setting permissions for file %1."), newPath)
370 unsigned int OperationThread::calculateFileSize(const QFileInfoList &files,
374 unsigned int res = 0;
376 for (QFileInfoList::const_iterator it = files.begin(); it != files.end(); ++it) {
377 unsigned int size = 0;
383 size += calculateFileSize(listDirFiles(it->absoluteFilePath()), count, addSize);
390 size += ceil(static_cast<float>(it->size()) / BLOCK_SIZE);
392 fileSizeMap[it->absoluteFilePath()] = size;
406 QFileInfoList OperationThread::listDirFiles(const QString &dirPath) {
408 return dir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries | QDir::System | QDir::Hidden);
412 void OperationThread::setTotalSize(unsigned int size) {
414 emit totalSizeChanged(size);
418 void OperationThread::updateProgress(int value) {
421 emit progressUpdate(value);
425 void OperationThread::updateFile(const QString &name) {
427 emit fileNameUpdated(shortenPath(name));
431 void OperationThread::waitOnCond() {
432 time_t waitTime = time(0);
433 waitCond.wait(&mutex);
434 emit operationResumed(time(0) - waitTime);
438 bool OperationThread::checkSequentialFile(const QFSFileEngine &engine) {
440 if (engine.isSequential()) {
441 if (!errno) errno = 255;
449 void OperationThread::wake() {
455 void DeleteThread::run() {
458 setTotalSize(calculateFileSize(files, true));
459 emit operationStarted(time(0));
468 void DeleteThread::perform(const QFileInfo &file) {
473 void CopyThread::run() {
476 setTotalSize(calculateFileSize(files, false, true));
477 emit operationStarted(time(0));
486 void CopyThread::perform(const QFileInfo &file) {
491 void MoveThread::run() {
501 void MoveThread::rename(const QFileInfoList &files, const QDir &dest) {
502 setTotalSize(totalSize + files.size());
503 emit operationStarted(time(0));
505 for (int i = 0; i < files.size(); ++i) {
506 QString path = files[i].absoluteFilePath();
507 QFSFileEngine engine(path);
508 QFileInfo newFile(dest.absolutePath() + "/" + files[i].fileName());
512 OVERWRITE_PROMPT(files[i], newFile)
514 // if we are owerwriting dir over a dir, we will get SKIP_DIR
515 // as a response from OVERWRITE_PROMT meaning we should skip it
516 // (KEEP would mean to keep the files inside)
517 if (files[i].isDir() && newFile.exists() && newFile.isDir()) {
518 if (response == SKIP_DIR) {
522 removeExcludeFiles.insert(path);
526 if (response == KEEP) {
530 removeExcludeFiles.insert(path);
535 QString newPath(newFile.absoluteFilePath());
536 QFSFileEngine newEngine(newPath);
540 while (!abort && !engine.rename(newPath)) {
541 // source and target are on different partitions
542 // this should happen on the first file, unless some are skipped by overwrite prompt
543 // we calculate the actual file sizes, because from now on copy & remove takes over
544 if (errno == EXDEV) {
545 overwriteAll = response;
546 // hack: we already checked the first file we are sending to processFiles(...)
547 // so we don't want to ask about this one again
548 if (overwriteAll == NONE) overwriteAll = DONT_ASK_ONCE;
550 QFileInfoList remainingFiles = files.mid(i);
552 setTotalSize(totalValue + calculateFileSize(remainingFiles, true, true));
554 processFiles(remainingFiles);
556 emit removeAfterCopy();
558 remove(remainingFiles, true);
562 // the target is nonempty dir. lets call this recursively and rename the contents one by one
563 } else if (errno == ENOTEMPTY || errno == EEXIST) {
564 Response tmpResp = overwriteAll;
565 overwriteAll = response;
567 rename(listDirFiles(path), QDir(newPath));
571 overwriteAll = tmpResp;
576 // source and target are nonmatching types(file and dir)
577 // remove the target and let it loop once again
578 } else if (errno == ENOTDIR || errno == EISDIR) {
579 if (!remove(newPath)) break;
581 SHOW_ERROR_PROMPT(tr("Error moving %1."), path)
583 if (response == IGNORE) {
599 void MoveThread::perform(const QFileInfo &file) {