forked from qt-creator/qt-creator
VCS/CleanDialog: Simplify code and make some tweaks
There is no need for a separate class with signals. Add support for cancel and report some kind of progress. Also use Utils::runAsync instead of QtConcurrent. Change-Id: I5d61d32644f99ccb6ba8e0d7e17f138956822424 Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
@@ -29,6 +29,7 @@
|
|||||||
|
|
||||||
#include <coreplugin/editormanager/editormanager.h>
|
#include <coreplugin/editormanager/editormanager.h>
|
||||||
#include <coreplugin/progressmanager/progressmanager.h>
|
#include <coreplugin/progressmanager/progressmanager.h>
|
||||||
|
#include <utils/runextensions.h>
|
||||||
|
|
||||||
#include <QStandardItemModel>
|
#include <QStandardItemModel>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
@@ -41,8 +42,7 @@
|
|||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QFuture>
|
#include <QTimer>
|
||||||
#include <QtConcurrentRun>
|
|
||||||
|
|
||||||
namespace VcsBase {
|
namespace VcsBase {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
@@ -51,8 +51,11 @@ enum { nameColumn, columnCount };
|
|||||||
enum { fileNameRole = Qt::UserRole, isDirectoryRole = Qt::UserRole + 1 };
|
enum { fileNameRole = Qt::UserRole, isDirectoryRole = Qt::UserRole + 1 };
|
||||||
|
|
||||||
// Helper for recursively removing files.
|
// Helper for recursively removing files.
|
||||||
static void removeFileRecursion(const QFileInfo &f, QString *errorMessage)
|
static void removeFileRecursion(QFutureInterface<void> &futureInterface,
|
||||||
|
const QFileInfo &f, QString *errorMessage)
|
||||||
{
|
{
|
||||||
|
if (futureInterface.isCanceled())
|
||||||
|
return;
|
||||||
// The version control system might list files/directory in arbitrary
|
// The version control system might list files/directory in arbitrary
|
||||||
// order, causing files to be removed from parent directories.
|
// order, causing files to be removed from parent directories.
|
||||||
if (!f.exists())
|
if (!f.exists())
|
||||||
@@ -60,7 +63,7 @@ static void removeFileRecursion(const QFileInfo &f, QString *errorMessage)
|
|||||||
if (f.isDir()) {
|
if (f.isDir()) {
|
||||||
const QDir dir(f.absoluteFilePath());
|
const QDir dir(f.absoluteFilePath());
|
||||||
foreach (const QFileInfo &fi, dir.entryInfoList(QDir::AllEntries|QDir::NoDotAndDotDot|QDir::Hidden))
|
foreach (const QFileInfo &fi, dir.entryInfoList(QDir::AllEntries|QDir::NoDotAndDotDot|QDir::Hidden))
|
||||||
removeFileRecursion(fi, errorMessage);
|
removeFileRecursion(futureInterface, fi, errorMessage);
|
||||||
QDir parent = f.absoluteDir();
|
QDir parent = f.absoluteDir();
|
||||||
if (!parent.rmdir(f.fileName()))
|
if (!parent.rmdir(f.fileName()))
|
||||||
errorMessage->append(VcsBase::CleanDialog::tr("The directory %1 could not be deleted.").
|
errorMessage->append(VcsBase::CleanDialog::tr("The directory %1 could not be deleted.").
|
||||||
@@ -75,46 +78,35 @@ static void removeFileRecursion(const QFileInfo &f, QString *errorMessage)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// A QFuture task for cleaning files in the background.
|
// Cleaning files in the background
|
||||||
// Emits error signal if not all files can be deleted.
|
static void runCleanFiles(QFutureInterface<void> &futureInterface,
|
||||||
class CleanFilesTask : public QObject
|
const QString &repository, const QStringList &files,
|
||||||
|
const std::function<void(const QString&)> &errorHandler)
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
QString errorMessage;
|
||||||
|
futureInterface.setProgressRange(0, files.size());
|
||||||
public:
|
futureInterface.setProgressValue(0);
|
||||||
explicit CleanFilesTask(const QString &repository, const QStringList &files);
|
foreach (const QString &name, files) {
|
||||||
|
removeFileRecursion(futureInterface, QFileInfo(name), &errorMessage);
|
||||||
void run();
|
if (futureInterface.isCanceled())
|
||||||
|
break;
|
||||||
signals:
|
futureInterface.setProgressValue(futureInterface.progressValue() + 1);
|
||||||
void error(const QString &e);
|
}
|
||||||
|
if (!errorMessage.isEmpty()) {
|
||||||
private:
|
|
||||||
const QString m_repository;
|
|
||||||
const QStringList m_files;
|
|
||||||
|
|
||||||
QString m_errorMessage;
|
|
||||||
};
|
|
||||||
|
|
||||||
CleanFilesTask::CleanFilesTask(const QString &repository, const QStringList &files) :
|
|
||||||
m_repository(repository), m_files(files)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void CleanFilesTask::run()
|
|
||||||
{
|
|
||||||
foreach (const QString &name, m_files)
|
|
||||||
removeFileRecursion(QFileInfo(name), &m_errorMessage);
|
|
||||||
if (!m_errorMessage.isEmpty()) {
|
|
||||||
// Format and emit error.
|
// Format and emit error.
|
||||||
const QString msg = CleanDialog::tr("There were errors when cleaning the repository %1:").
|
const QString msg = CleanDialog::tr("There were errors when cleaning the repository %1:").
|
||||||
arg(QDir::toNativeSeparators(m_repository));
|
arg(QDir::toNativeSeparators(repository));
|
||||||
m_errorMessage.insert(0, QLatin1Char('\n'));
|
errorMessage.insert(0, QLatin1Char('\n'));
|
||||||
m_errorMessage.insert(0, msg);
|
errorMessage.insert(0, msg);
|
||||||
emit error(m_errorMessage);
|
errorHandler(errorMessage);
|
||||||
}
|
}
|
||||||
// Run in background, need to delete ourselves
|
}
|
||||||
this->deleteLater();
|
|
||||||
|
static void handleError(const QString &errorMessage)
|
||||||
|
{
|
||||||
|
QTimer::singleShot(0, VcsOutputWindow::instance(), [errorMessage]() {
|
||||||
|
VcsOutputWindow::instance()->appendSilently(errorMessage);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------- CleanDialogPrivate ----------------
|
// ---------------- CleanDialogPrivate ----------------
|
||||||
@@ -258,12 +250,9 @@ bool CleanDialog::promptToDelete()
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Remove in background
|
// Remove in background
|
||||||
auto cleanTask = new Internal::CleanFilesTask(d->m_workingDirectory, selectedFiles);
|
QFuture<void> task = Utils::runAsync<void>(Internal::runCleanFiles, d->m_workingDirectory,
|
||||||
connect(cleanTask, &Internal::CleanFilesTask::error,
|
selectedFiles, Internal::handleError);
|
||||||
VcsOutputWindow::instance(), &VcsOutputWindow::appendSilently,
|
|
||||||
Qt::QueuedConnection);
|
|
||||||
|
|
||||||
QFuture<void> task = QtConcurrent::run(cleanTask, &Internal::CleanFilesTask::run);
|
|
||||||
const QString taskName = tr("Cleaning \"%1\"").
|
const QString taskName = tr("Cleaning \"%1\"").
|
||||||
arg(QDir::toNativeSeparators(d->m_workingDirectory));
|
arg(QDir::toNativeSeparators(d->m_workingDirectory));
|
||||||
Core::ProgressManager::addTask(task, taskName, "VcsBase.cleanRepository");
|
Core::ProgressManager::addTask(task, taskName, "VcsBase.cleanRepository");
|
||||||
@@ -306,5 +295,3 @@ void CleanDialog::updateSelectAllCheckBox()
|
|||||||
}
|
}
|
||||||
|
|
||||||
} // namespace VcsBase
|
} // namespace VcsBase
|
||||||
|
|
||||||
#include "cleandialog.moc"
|
|
||||||
|
Reference in New Issue
Block a user