#include "fileoperator.h"
#include <QtGui>
-#include <QDir>
#include <QMessageBox>
-#include <QHBoxLayout>
-#include <QChar>
+#include <QVBoxLayout>
+#include <QMaemo5InformationBox>
-#include <math.h>
-#include <errno.h>
-#include <iostream>
-
-
-#define BLOCK_SIZE 524288
-
-
-#define SHOW_ERROR_PROMPT(promptString, fileName) \
- response = FileOperator::NONE; \
- if (ignoreAll[errno]) { \
- response = FileOperator::IGNORE; \
- } else { \
- char buf[255]; \
- char *realBuf = strerror_r(errno, buf, 255); \
- emit showErrorPrompt(this, promptString + " " + realBuf + ".", fileName, errno); \
- waitCond.wait(&mutex); \
- }
-
-
-#define ERROR_PROMPT(operation, promptString, fileName) \
-{ \
- response = FileOperator::NONE; \
- while (!abort && operation) { \
- SHOW_ERROR_PROMPT(promptString, fileName) \
- if (response == FileOperator::IGNORE) { \
- break; \
- } \
- } \
-}
-
-
-#define ERROR_PROMPT_XP(operation, promptString, fileName, onIgnore, quitCmd) \
-{ \
- ERROR_PROMPT(operation, promptString, fileName) \
- if (abort || response == FileOperator::IGNORE) { \
- if (!abort) onIgnore; \
- quitCmd; \
- } \
-}
-
-
-#define OVERWRITE_PROMPT(file, newFile) \
-{ \
- response = FileOperator::NONE; \
- \
- if (newFile.exists()) { \
- if (overwriteAll != FileOperator::NONE) { \
- response = overwriteAll; \
- } else { \
- bool dirOverDir = false; \
- if (newFile.isDir() && file.isDir()) dirOverDir = true; \
- emit showOverwritePrompt(this, newFile.absoluteFilePath(), dirOverDir); \
- waitCond.wait(&mutex); \
- } \
- } \
-}
+#include "dialog.h"
+#include "utils.h"
FileOperator::FileOperator(QWidget *parent) : QWidget(parent) {
- QHBoxLayout *layout = new QHBoxLayout;
+ QVBoxLayout *layout = new QVBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
- layout->setSpacing(0);
+ layout->setSpacing(1);
+ topRow = new QHBoxLayout;
+ topRow->setContentsMargins(0, 0, 0, 0);
+ topRow->setSpacing(1);
+ layout->addLayout(topRow);
+ bottomRow = new QHBoxLayout;
+ bottomRow->setContentsMargins(0, 0, 0, 0);
+ bottomRow->setSpacing(1);
+ layout->addLayout(bottomRow);
setLayout(layout);
-}
-
-QString FileOperator::shortenPath(const QString &path) {
- QString homePath = QFSFileEngine::homePath();
- QString result = path;
- if (path.indexOf(homePath, 0) == 0) {
- result.replace(0, homePath.size(), "~");
- }
+ qRegisterMetaType<QFileInfo>("QFileInfo");
+ qRegisterMetaType<time_t>("time_t");
- return result;
+ loadOperationIcons(palette(), "delete_small", deleteIcon, inverseDeleteIcon);
+ loadOperationIcons(palette(), "copy_small", copyIcon, inverseCopyIcon);
+ loadOperationIcons(palette(), "move_small", moveIcon, inverseMoveIcon);
}
void FileOperator::deleteFiles(const QFileInfoList &files) {
+ if (checkMaxOpsNumber()) return;
+
QString title, desc;
if (files.size() == 1) {
title = tr("Delete file");
desc = tr("Are you sure you want to delete %1?")
- .arg(FileOperator::shortenPath(files[0].absoluteFilePath()));
+ .arg(shortenPath(files[0].absoluteFilePath()));
} else {
title = tr("Delete files");
desc = tr("You are about to delete %1 files. Are you sure you want to continue?").arg(files.size());
);
if(confirm == QMessageBox::Yes) {
- caterNewThread(new DeleteThread(files));
+ ProgressBar *bar = new ProgressBar(deleteIcon, inverseDeleteIcon);
+ initOperation(new DeleteThread(files), bar);
}
}
void FileOperator::copyFiles(const QFileInfoList &files, QDir &destination) {
+ if (checkMaxOpsNumber()) return;
+
QString title, desc;
if (files.size() == 1) {
title = tr("Copy file");
desc = tr("Are you sure you want to copy %1 to %2?")
- .arg(FileOperator::shortenPath(files[0].absoluteFilePath()))
- .arg(FileOperator::shortenPath(destination.absolutePath()));
+ .arg(shortenPath(files[0].absoluteFilePath()))
+ .arg(shortenPath(destination.absolutePath()));
} else {
title = tr("Copy files");
desc = tr("You are about to copy %1 files to %2. Are you sure you want to continue?")
- .arg(files.size()).arg(FileOperator::shortenPath(destination.absolutePath()));
+ .arg(files.size()).arg(shortenPath(destination.absolutePath()));
}
int confirm = QMessageBox::warning(
);
if(confirm == QMessageBox::Yes) {
- caterNewThread(new CopyThread(files, destination));
+ ProgressBar *bar = new ProgressBar(copyIcon, inverseCopyIcon);
+ bar->setBottomTexts(shortenPath(files[0].absolutePath()), shortenPath(destination.absolutePath()));
+ initOperation(new CopyThread(files, destination), bar);
}
}
void FileOperator::moveFiles(const QFileInfoList &files, QDir &destination) {
+ if (checkMaxOpsNumber()) return;
+
// for move we don't wanna move to the same dir
if (files[0].absolutePath() == destination.absolutePath()) return;
if (files.size() == 1) {
title = tr("Move file");
desc = tr("Are you sure you want to move %1 to %2?")
- .arg(FileOperator::shortenPath(files[0].absoluteFilePath()))
- .arg(FileOperator::shortenPath(destination.absolutePath()));
+ .arg(shortenPath(files[0].absoluteFilePath()))
+ .arg(shortenPath(destination.absolutePath()));
} else {
title = tr("Move files");
desc = tr("You are about to move %1 files to %2. Are you sure you want to continue?")
- .arg(files.size()).arg(FileOperator::shortenPath(destination.absolutePath()));
+ .arg(files.size()).arg(shortenPath(destination.absolutePath()));
}
int confirm = QMessageBox::warning(
);
if(confirm == QMessageBox::Yes) {
- caterNewThread(new MoveThread(files, destination));
+ ProgressBar *bar = new ProgressBar(moveIcon, inverseMoveIcon);
+ bar->setBottomTexts(shortenPath(files[0].absolutePath()), shortenPath(destination.absolutePath()));
+ initOperation(new MoveThread(files, destination), bar);
}
}
-void FileOperator::showErrorPrompt(FileManipulatorThread* manipulator,
+void FileOperator::showErrorPrompt(OperationThread* op,
const QString &message,
const QString &fileName,
const int err)
{
QMessageBox msgBox;
- msgBox.addButton(QMessageBox::Cancel);
+ QAbstractButton *cancelButton = msgBox.addButton(QMessageBox::Cancel);
QAbstractButton *abortButton = msgBox.addButton(tr("Abort"), QMessageBox::DestructiveRole);
QAbstractButton *retryButton = msgBox.addButton(QMessageBox::Retry);
QAbstractButton *ignoreButton = msgBox.addButton(QMessageBox::Ignore);
QAbstractButton *ignoreAllButton = msgBox.addButton(tr("Ignore All"), QMessageBox::AcceptRole);
- msgBox.setText(message.arg(FileOperator::shortenPath(fileName)));
+ msgBox.setText(message.arg(shortenPath(fileName)));
msgBox.exec();
- if (msgBox.clickedButton() == abortButton) {
- manipulator->setResponse(ABORT);
+ if (msgBox.clickedButton() == cancelButton) {
+ op->pause = true;
+ op->setResponse(OperationThread::RETRY);
+ } else if (msgBox.clickedButton() == abortButton) {
+ op->setResponse(OperationThread::ABORT);
} else if (msgBox.clickedButton() == retryButton) {
- manipulator->setResponse(RETRY);
+ op->setResponse(OperationThread::RETRY);
} else if (msgBox.clickedButton() == ignoreButton) {
- manipulator->setResponse(IGNORE);
+ op->setResponse(OperationThread::IGNORE);
} else if (msgBox.clickedButton() == ignoreAllButton) {
- manipulator->setResponse(IGNORE, true, err);
+ op->setResponse(OperationThread::IGNORE, true, err);
}
}
void FileOperator::showOverwritePrompt(
- FileManipulatorThread* manipulator,
+ OperationThread* op,
const QString &fileName,
const bool dirOverDir)
{
- QMessageBox msgBox;
- msgBox.addButton(QMessageBox::Cancel);
- QAbstractButton *yesButton = msgBox.addButton(QMessageBox::Yes);
- QAbstractButton *yesToAllButton = msgBox.addButton(QMessageBox::YesToAll);
- QAbstractButton *noButton = msgBox.addButton(QMessageBox::No);
- QAbstractButton *noToAllButton = msgBox.addButton(QMessageBox::NoToAll);
- QAbstractButton *abortButton = msgBox.addButton(tr("Abort"), QMessageBox::DestructiveRole);
+ Dialog msgBox;
+ QAbstractButton *yesButton = msgBox.addButtonFirst(QDialogButtonBox::Yes);
+ QAbstractButton *yesToAllButton = msgBox.addButtonFirst(QDialogButtonBox::YesToAll);
+ QAbstractButton *noButton = msgBox.addButtonSecond(QDialogButtonBox::No);
+ QAbstractButton *noToAllButton = msgBox.addButtonSecond(QDialogButtonBox::NoToAll);
+ QAbstractButton *abortButton = msgBox.addButtonSecond(tr("Abort"), QDialogButtonBox::DestructiveRole);
+ QAbstractButton *newNameButton = msgBox.addButtonFirst(tr("New Name"), QDialogButtonBox::AcceptRole);
QAbstractButton *askButton = 0;
+ QAbstractButton *skipDirButton = 0;
if (dirOverDir) {
msgBox.setText(tr("Directory %1 already exists. Overwrite the files inside?")
- .arg(FileOperator::shortenPath(fileName)));
- askButton = msgBox.addButton(tr("Ask"), QMessageBox::AcceptRole);
+ .arg(shortenPath(fileName)));
+ askButton = msgBox.addButtonFirst(tr("Ask"), QDialogButtonBox::AcceptRole);
+ skipDirButton = msgBox.addButtonSecond(tr("Skip"), QDialogButtonBox::NoRole);
} else {
- msgBox.setText(tr("File %1 already exists. Overwrite?").arg(FileOperator::shortenPath(fileName)));
+ msgBox.setText(tr("File %1 already exists. Overwrite?").arg(shortenPath(fileName)));
}
msgBox.exec();
- if (msgBox.clickedButton() == abortButton) {
- manipulator->setResponse(ABORT);
- } else if (msgBox.clickedButton() == yesButton) {
- manipulator->setResponse(OVERWRITE);
- } else if (msgBox.clickedButton() == yesToAllButton) {
- manipulator->setResponse(OVERWRITE, true);
- } else if (msgBox.clickedButton() == noButton) {
- manipulator->setResponse(KEEP);
- } else if (msgBox.clickedButton() == noToAllButton) {
- manipulator->setResponse(KEEP, true);
- } else if (msgBox.clickedButton() == askButton) {
- manipulator->setResponse(NONE, true);
- }
-}
-
-
-void FileOperator::remove(FileManipulatorThread* manipulator) {
- manipulator->wait();
- layout()->removeWidget(manipulator->progressBar);
- manipulatorList.removeAll(manipulator);
- delete manipulator;
-}
-
-
-void FileOperator::setBarSize(FileManipulatorThread* manipulator, unsigned int size) {
- if (!manipulator->progressBar->maximum()) {
- manipulator->startTime = time(0);
+ if (msgBox.clickedButton == 0) {
+ op->pause = true;
+ op->setResponse(OperationThread::NONE);
+ } else if (msgBox.clickedButton == abortButton) {
+ op->setResponse(OperationThread::ABORT);
+ } else if (msgBox.clickedButton == yesButton) {
+ op->setResponse(OperationThread::OVERWRITE);
+ } else if (msgBox.clickedButton == yesToAllButton) {
+ op->setResponse(OperationThread::OVERWRITE, true);
+ } else if (msgBox.clickedButton == noButton) {
+ op->setResponse(OperationThread::KEEP);
+ } else if (msgBox.clickedButton == noToAllButton) {
+ op->setResponse(OperationThread::KEEP, true);
+ } else if (msgBox.clickedButton == askButton) {
+ op->setResponse(OperationThread::ASK);
+ } else if (msgBox.clickedButton == newNameButton) {
+ op->setResponse(OperationThread::NONE);
+ } else if (msgBox.clickedButton == skipDirButton) {
+ op->setResponse(OperationThread::SKIP_DIR);
}
- manipulator->progressBar->setMinimum(0);
- manipulator->progressBar->setMaximum(size);
-}
-
-
-void FileOperator::updateProgress(FileManipulatorThread* manipulator, int value) {
- manipulator->setText(value);
-}
-
-
-void FileOperator::caterNewThread(FileManipulatorThread *thread) {
- manipulatorList.append(thread);
-
- connect(thread, SIGNAL(showErrorPrompt(FileManipulatorThread*, const QString&, const QString&, const int)),
- this, SLOT(showErrorPrompt(FileManipulatorThread*, const QString&, const QString&, const int)));
- connect(thread, SIGNAL(showOverwritePrompt(FileManipulatorThread*, const QString&, bool)),
- this, SLOT(showOverwritePrompt(FileManipulatorThread*, const QString&, bool)));
- connect(thread, SIGNAL(finished(FileManipulatorThread*)),
- this, SLOT(remove(FileManipulatorThread*)));
- connect(thread, SIGNAL(setBarSize(FileManipulatorThread*, unsigned int)),
- this, SLOT(setBarSize(FileManipulatorThread*, unsigned int)));
- connect(thread, SIGNAL(updateProgress(FileManipulatorThread*, int)),
- this, SLOT(updateProgress(FileManipulatorThread*, int)));
-
- thread->progressBar->setValue(0);
-
- layout()->addWidget(thread->progressBar);
- thread->start(QThread::LowestPriority);
}
-FileManipulatorThread::FileManipulatorThread(const QFileInfoList files, QDir dest) :
- progressBar(new QProgressBar()),
- startTime(0),
- files(files),
- dest(dest),
- response(FileOperator::NONE),
- overwriteAll(FileOperator::NONE),
- abort(false),
- lastTimeUpdate(0),
- barSize(0),
- barValue(0),
- fileSize(0),
- fileValue(0)
+void FileOperator::showInputFilenamePrompt(OperationThread* op,
+ const QFileInfo &file,
+ const bool dir)
{
- memset(ignoreAll, false, sizeof(ignoreAll));
- progressBar->setMaximum(0);
- QFont barFont = progressBar->font();
- barFont.setPointSize(12);
- progressBar->setFont(barFont);
- progressBar->setFormat(tr("Gathering information..."));
- progressBar->setMinimumHeight(44);
- progressBar->setStyle(new QPlastiqueStyle);
- //progressBar->setStyle(new QMotifStyle);
-}
-
+ bool ok;
+ QString prompt, error;
-FileManipulatorThread::~FileManipulatorThread() {
- if (progressBar->value() < progressBar->maximum()) {
- std::cout << "WARNING: deleting a progressbar which's value " << progressBar->value() <<
- " has not reached maximum of " << progressBar->maximum() << std::endl;
+ if (dir) {
+ prompt = tr("Enter the new directory name.");
+ } else {
+ prompt = tr("Enter the new file name.");
}
- delete progressBar;
-}
-
-void FileManipulatorThread::setResponse(
- const FileOperator::Response response,
- const bool applyToAll,
- const int err)
-{
- mutex.lock();
+ op->newNameFromDialog = "";
+ QString text = file.fileName();
- this->response = response;
+ while (true) {
+ text = QInputDialog::getText(this, QString(), prompt + error, QLineEdit::Normal, text, &ok);
- if (applyToAll) {
- if (response == FileOperator::KEEP
- || response == FileOperator::OVERWRITE
- || response == FileOperator::NONE)
- {
- overwriteAll = response;
- }
+ if (!ok) break;
- if (response == FileOperator::IGNORE) {
- ignoreAll[err] = true;
+ error = "";
+ if (text.contains(QRegExp("[\"*/:<>?\\\\|]"))) {
+ error = "<small><br/><font color = 'red'>" +
+ tr("The name cannot contain any of the following characters: ") +
+ "\"*/:<>?\\|</font></small>";
+ } else if (ok && !text.isEmpty()) {
+ QFileInfo info(file.path() + "/" + text);
+ op->newNameFromDialog = info.absoluteFilePath();
+ break;
}
}
- if (response == FileOperator::ABORT) abort = true;
-
- mutex.unlock();
- waitCond.wakeAll();
+ op->wake();
}
-void FileManipulatorThread::processFiles(const QFileInfoList &files) {
- for (QFileInfoList::const_iterator it = files.begin(); it != files.end(); ++it) {
- perform(*it);
- if (abort) break;
- }
+void FileOperator::remove(OperationThread* op) {
+ op->wait();
+ ProgressBar *bar = get(op);
+ removeBarFromLayout(bar);
+ opList.removeAll(qMakePair(op, bar));
+ delete op;
+ delete bar;
}
-bool FileManipulatorThread::remove(QString &fileName, const bool doUpdates) {
- return remove(QFileInfo(fileName), doUpdates);
-}
+void FileOperator::togglePauseOperation(ProgressBar* bar) {
+ OperationThread *op = get(bar);
-
-bool FileManipulatorThread::remove(const QFileInfoList &files, const bool doUpdates) {
- bool res = true;
- for (QFileInfoList::const_iterator it = files.begin(); it != files.end(); ++it) {
- if (!remove(*it, doUpdates)) res = false;
- if (abort) break;
- }
- return res;
-}
-
-
-bool FileManipulatorThread::remove(const QFileInfo &file, const bool doUpdates) {
- std::cout << "DELETING " << file.absoluteFilePath().toStdString() << std::endl;
-
- QString path = file.absoluteFilePath();
- QFSFileEngine engine(path);
-
- if (doUpdates) updateFile(path);
-
- if (file.isDir()) {
- if (!remove(listDirFiles(path), doUpdates)) return false;
-
- if (!listDirFiles(path).size()) {
- ERROR_PROMPT(!engine.rmdir(path, false), tr("Error deleting directory %1."), path)
- }
+ if (op->pause) {
+ op->wake();
} else {
- ERROR_PROMPT(!engine.remove(), tr("Error deleting file %1."), path)
+ op->pause = true;
}
-
- if (!abort && doUpdates) updateProgress(1);
-
- if (abort || response == FileOperator::IGNORE) return false;
- return true;
}
-void FileManipulatorThread::copy(const QFileInfo &file) {
- std::cout << "COPYING " << file.absoluteFilePath().toStdString()
- << " to " << dest.absolutePath().toStdString() << std::endl;
+void FileOperator::abortOperation(ProgressBar* bar) {
+ OperationThread *op = get(bar);
- QString path(file.absoluteFilePath());
- QString newPath(dest.absolutePath() + "/" + file.fileName());
- QFSFileEngine engine(path);
- QFSFileEngine newEngine(newPath);
- QFileInfo newFile(newPath);
-
- updateFile(path);
-
- // hack to prevent asking about the same file if we already asked in the rename(...) function
- if (overwriteAll == FileOperator::DONT_ASK_ONCE) {
- overwriteAll = FileOperator::NONE;
- } else {
- OVERWRITE_PROMPT(file, newFile)
- }
-
- if (abort) return;
-
- if (response == FileOperator::KEEP) {
- updateProgress(fileSizeMap[path]);
- return;
- }
-
- if (file.isDir()) {
- FileOperator::Response overwriteResponse = response;
-
- if (newFile.exists() && !newFile.isDir()) {
- if(!remove(newPath)) {
- updateProgress(fileSizeMap[path]);
- return;
- }
- newFile = QFileInfo(newPath);
- }
-
- if (!newFile.exists()) {
- ERROR_PROMPT_XP(!engine.mkdir(newPath, false),
- tr("Error creating directory %1."), newPath,
- updateProgress(fileSizeMap[path]),
- return)
- }
-
- updateProgress(1);
-
- QDir destBackup = dest;
- dest = newPath;
-
- FileOperator::Response tmpResp = overwriteAll;
- overwriteAll = overwriteResponse;
-
- processFiles(listDirFiles(path));
-
- overwriteAll = tmpResp;
-
- ERROR_PROMPT(!newEngine.setPermissions(file.permissions()),
- tr("Error setting permissions for directory %1."), newPath)
-
- if (abort) return;
-
- dest = destBackup;
- } else {
- ERROR_PROMPT_XP(engine.isSequential(),
- tr("Cannot copy sequential file %1."), path,
- updateProgress(fileSizeMap[path]),
- return)
-
- if (newFile.exists() && newFile.isDir()) {
- ERROR_PROMPT_XP(!remove(newPath),
- tr("Cannot replace directory %1 due to previous errors."), newPath,
- updateProgress(fileSizeMap[path]),
- return)
- }
-
- ERROR_PROMPT_XP(!engine.open(QIODevice::ReadOnly),
- tr("Error reading file %1."), path,
- updateProgress(fileSizeMap[path]),
- return)
-
- bool ignore = false;
- while (!abort && !ignore) {
- engine.seek(0);
-
- ERROR_PROMPT(!newEngine.open(QIODevice::WriteOnly | QIODevice::Truncate),
- tr("Error writing file %1."), newPath)
-
- if (abort || response == FileOperator::IGNORE) {
- if (response == FileOperator::IGNORE) {
- updateProgress(fileSizeMap[path] - fileValue);
- ignore = true;
- }
- break;
- }
-
- bool error = false;
- char block[BLOCK_SIZE];
- qint64 bytes;
- while ((bytes = engine.read(block, sizeof(block))) > 0) {
- if (bytes == -1 || bytes != newEngine.write(block, bytes)) {
- if (bytes == -1) {
- SHOW_ERROR_PROMPT(tr("Error while reading from file %1."), path);
- } else {
- SHOW_ERROR_PROMPT(tr("Error while writing to file %1."), newPath);
- }
-
- if (!abort) {
- if (response == FileOperator::IGNORE) {
- updateProgress(fileSizeMap[path] - fileValue);
- ignore = true;
- } else {
- updateProgress(-fileValue);
- }
- }
- error = true;
- break;
- }
-
- updateProgress(1);
- }
-
- if (!error) break;
- }
-
- engine.close();
- newEngine.close();
-
- if (abort || ignore) {
- newEngine.remove();
- } else {
- ERROR_PROMPT(!newEngine.setPermissions(file.permissions()),
- tr("Error setting permissions for file %1."), newPath)
- }
- }
-}
-
-
-unsigned int FileManipulatorThread::calculateFileSize(const QFileInfoList &files,
- const bool count,
- const bool addSize)
-{
- unsigned int res = 0;
-
- for (QFileInfoList::const_iterator it = files.begin(); it != files.end(); ++it) {
- unsigned int size = 0;
-
- if (it->isDir()) {
- size += calculateFileSize(listDirFiles(it->absoluteFilePath()), count, addSize);
- }
-
- if (addSize) {
- if (it->isDir()) {
- ++size;
- } else {
- size += ceil(static_cast<float>(it->size()) / BLOCK_SIZE);
- }
- fileSizeMap[it->absoluteFilePath()] = size;
- }
-
- if (count) {
- ++size;
- }
+ int confirm = QMessageBox::warning(
+ 0,
+ tr("Abort operation"),
+ tr("Are you sure you want to abort the operation?"),
+ QMessageBox::Yes,
+ QMessageBox::No
+ );
- res += size;
+ if(confirm == QMessageBox::Yes) {
+ op->abort = true;
+ op->pause = false;
+ op->wake();
}
-
- return res;
}
-QFileInfoList FileManipulatorThread::listDirFiles(const QString &dirPath) {
- QDir dir = dirPath;
- return dir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries | QDir::System | QDir::Hidden);
-}
+void FileOperator::initOperation(OperationThread *thread, ProgressBar *bar) {
+ addBarToLayout(bar);
+ opList.append(qMakePair(thread, bar));
+ connect(thread, SIGNAL(showErrorPrompt(OperationThread*, const QString&, const QString&, const int)),
+ this, SLOT(showErrorPrompt(OperationThread*, const QString&, const QString&, const int)));
+ connect(thread, SIGNAL(showOverwritePrompt(OperationThread*, const QString&, bool)),
+ this, SLOT(showOverwritePrompt(OperationThread*, const QString&, bool)));
+ connect(thread, SIGNAL(showInputFilenamePrompt(OperationThread*, const QFileInfo&, bool)),
+ this, SLOT(showInputFilenamePrompt(OperationThread*, const QFileInfo&, bool)));
+ connect(thread, SIGNAL(finished(OperationThread*)),
+ this, SLOT(remove(OperationThread*)));
-void FileManipulatorThread::setBarSize(unsigned int size) {
- barSize = size;
- emit setBarSize(this, size);
-}
+ connect(thread, SIGNAL(totalSizeChanged(int)), bar, SLOT(setMaximum(int)));
+ connect(thread, SIGNAL(progressUpdate(int)), bar, SLOT(updateProgress(int)));
+ connect(thread, SIGNAL(fileNameUpdated(QString)), bar, SLOT(updateMainText(QString)));
+ connect(thread, SIGNAL(operationStarted(time_t)), bar, SLOT(setStartTime(time_t)));
+ connect(thread, SIGNAL(operationPaused()), bar, SLOT(pause()));
+ connect(thread, SIGNAL(operationResumed(time_t)), bar, SLOT(resume(time_t)));
+ connect(thread, SIGNAL(removeAfterCopy()), bar, SLOT(showRemoveNotice()));
+ connect(bar, SIGNAL(togglePauseOperation(ProgressBar*)), this, SLOT(togglePauseOperation(ProgressBar*)));
+ connect(bar, SIGNAL(abortOperation(ProgressBar*)), this, SLOT(abortOperation(ProgressBar*)));
-void FileManipulatorThread::updateProgress(int value) {
- barValue += value;
- fileValue += value;
- emit updateProgress(this, value);
+ thread->start(QThread::LowestPriority);
}
-void FileManipulatorThread::updateFile(const QString &name) {
- fileValue = 0;
- fileName = FileOperator::shortenPath(name);
- emit updateProgress(this, 0);
+ProgressBar *FileOperator::get(OperationThread *op) const {
+ for (OperationList::const_iterator it = opList.begin(); it != opList.end(); ++it) {
+ if (it->first == op) return it->second;
+ }
+ return 0;
}
-void FileManipulatorThread::setText(int value) {
- if (progressBar->value() + value > progressBar->maximum()) {
- std::cout << "WARNING: exceeding progressbar maximum (" << progressBar->maximum()
- << ") by " << value << std::endl;
+OperationThread *FileOperator::get(ProgressBar *bar) const {
+ for (OperationList::const_iterator it = opList.begin(); it != opList.end(); ++it) {
+ if (it->second == bar) return it->first;
}
-
- time_t now = time(0);
- if (lastTimeUpdate < now) {
- lastTimeUpdate = now;
-
- time_t elapsed = now - startTime;
- time_t remaining = (time_t) ((float) elapsed / barValue * (barSize - barValue));
- struct tm *ts = gmtime(&remaining);
-
- if (remaining < 60) {
- strftime(timeBuf, sizeof(timeBuf), "%Ss", ts);
- } else if (remaining < 3600) {
- strftime(timeBuf, sizeof(timeBuf), "%M:%S", ts);
- } else {
- strftime(timeBuf, sizeof(timeBuf), "%H:%M:%S", ts);
- }
+ return 0;
+}
+
+
+void FileOperator::addBarToLayout(ProgressBar *bar) {
+ switch (opList.size()) {
+ case 0:
+ case 1:
+ case 2:
+ topRow->addWidget(bar);
+ break;
+ case 4:
+ topRow->addItem(bottomRow->takeAt(0));
+ bottomRow->addWidget(bar);
+ break;
+ case 3:
+ bottomRow->addItem(topRow->takeAt(2));
+ default:
+ bottomRow->addWidget(bar);
}
-
- progressBar->setFormat(barText.arg(fileName) + "\n%p% ETA " + timeBuf);
- progressBar->setValue(progressBar->value() + value);
}
-DeleteThread::DeleteThread(const QFileInfoList &files) : FileManipulatorThread(files) {
- barText = tr("deleting %1");
-}
-
-
-void DeleteThread::run() {
- mutex.lock();
-
- setBarSize(calculateFileSize(files, true));
-
- processFiles(files);
-
- sleep(0.5);
- emit finished(this);
-}
-
-
-void DeleteThread::perform(const QFileInfo &file) {
- remove(file, true);
-}
-
-
-CopyThread::CopyThread(const QFileInfoList &files, QDir &dest) : FileManipulatorThread(files, dest) {
- barText = tr("copying %1");
-}
-
-
-void CopyThread::run() {
- mutex.lock();
-
- setBarSize(calculateFileSize(files, false, true));
-
- processFiles(files);
-
- sleep(0.5);
- emit finished(this);
-}
-
-
-void CopyThread::perform(const QFileInfo &file) {
- copy(file);
-}
-
-
-MoveThread::MoveThread(const QFileInfoList &files, QDir &dest) : FileManipulatorThread(files, dest) {
- barText = tr("moving %1");
-}
-
-
-void MoveThread::run() {
- mutex.lock();
-
- rename(files, dest);
-
- sleep(0.5);
- emit finished(this);
-}
-
-
-void MoveThread::rename(const QFileInfoList &files, const QDir &dest) {
- setBarSize(barSize + files.size());
-
- for (int i = 0; i < files.size(); ++i) {
- QString path = files[i].absoluteFilePath();
- QFSFileEngine engine(path);
- QString newPath = dest.absolutePath() + "/" + files[i].fileName();
-
- updateFile(path);
-
- OVERWRITE_PROMPT(files[i], QFileInfo(newPath))
-
- if (response == FileOperator::KEEP) {
- // TODO lets not remove the source for now, I'm not sure what is correct behavior
- // remove(path);
- if (abort) break;
- updateProgress(1);
- continue;
+void FileOperator::removeBarFromLayout(ProgressBar *bar) {
+ int index = topRow->indexOf(bar);
+ if (index != -1) {
+ topRow->takeAt(index);
+ switch (opList.size()) {
+ case 4:
+ topRow->addItem(bottomRow->takeAt(0));
+ case 6:
+ topRow->addItem(bottomRow->takeAt(0));
+ break;
}
-
- while (!abort && !engine.rename(newPath)) {
- // source and target are on different partitions
- // this should happen on the first file, unless some are skipped by overwrite prompt
- // we calculate the actual file sizes, because from now on copy & remove takes over
- if (errno == EXDEV) {
- overwriteAll = response;
- // hack: we already checked the first file we are sending to processFiles(...)
- // so we don't want to ask about this one again
- if (overwriteAll == FileOperator::NONE) overwriteAll = FileOperator::DONT_ASK_ONCE;
-
- QFileInfoList remainingFiles = files.mid(i);
-
- setBarSize(barValue + calculateFileSize(remainingFiles, true, true));
-
- processFiles(remainingFiles);
-
- barText = tr("deleting %1");
-
- remove(remainingFiles, true);
-
- // just to quit the loops, we are done
- abort = true;
- // the target is nonempty dir. lets call this recursively and rename the contents one by one
- } else if (errno == ENOTEMPTY || errno == EEXIST) {
- FileOperator::Response tmpResp = overwriteAll;
- overwriteAll = response;
-
- rename(listDirFiles(path), QDir(newPath));
- if (abort) break;
-
- overwriteAll = tmpResp;
-
- ERROR_PROMPT(!engine.rmdir(path, false), tr("Error deleting directory %1."), path)
-
- break;
- // source and target are nonmatching types(file and dir)
- // remove the target and let it loop once again
- } else if (errno == ENOTDIR || errno == EISDIR) {
- if (!remove(newPath)) break;
- } else {
- SHOW_ERROR_PROMPT(tr("Error moving %1."), path)
-
- if (response == FileOperator::IGNORE) {
- break;
- }
- }
+ } else {
+ bottomRow->removeWidget(bar);
+ switch (opList.size()) {
+ case 4:
+ topRow->addItem(bottomRow->takeAt(0));
+ break;
+ case 5:
+ bottomRow->insertWidget(0, topRow->takeAt(2)->widget());
}
-
- if (abort) break;
- updateProgress(1);
}
}
-void MoveThread::perform(const QFileInfo &file) {
- copy(file);
+bool FileOperator::checkMaxOpsNumber() {
+ if (opList.size() == 6) {
+ QMaemo5InformationBox::information(this, tr("The maximum number of file operations is %1.").arg(6));
+ return true;
+ }
+ return false;
}