2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2010-03-12 15:54:09 +01:00
|
|
|
|
|
|
|
|
#include "cleandialog.h"
|
2022-08-05 14:37:37 +02:00
|
|
|
|
2014-08-26 00:02:47 +02:00
|
|
|
#include "vcsoutputwindow.h"
|
2010-03-12 15:54:09 +01:00
|
|
|
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <coreplugin/progressmanager/progressmanager.h>
|
2022-08-05 14:37:37 +02:00
|
|
|
|
|
|
|
|
#include <utils/layoutbuilder.h>
|
2016-01-21 15:34:14 +01:00
|
|
|
#include <utils/runextensions.h>
|
2010-03-12 15:54:09 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QApplication>
|
2022-08-05 14:37:37 +02:00
|
|
|
#include <QCheckBox>
|
|
|
|
|
#include <QDateTime>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QDialogButtonBox>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QFileInfo>
|
2022-08-05 14:37:37 +02:00
|
|
|
#include <QGroupBox>
|
|
|
|
|
#include <QHeaderView>
|
|
|
|
|
#include <QIcon>
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QStandardItemModel>
|
|
|
|
|
#include <QStyle>
|
2016-01-21 15:34:14 +01:00
|
|
|
#include <QTimer>
|
2022-08-05 14:37:37 +02:00
|
|
|
#include <QTreeView>
|
2010-03-12 15:54:09 +01:00
|
|
|
|
2012-01-07 12:31:48 +01:00
|
|
|
namespace VcsBase {
|
2011-12-08 13:07:00 +01:00
|
|
|
namespace Internal {
|
2011-03-28 14:19:17 +02:00
|
|
|
|
2010-03-12 15:54:09 +01:00
|
|
|
enum { nameColumn, columnCount };
|
|
|
|
|
enum { fileNameRole = Qt::UserRole, isDirectoryRole = Qt::UserRole + 1 };
|
|
|
|
|
|
|
|
|
|
// Helper for recursively removing files.
|
2016-01-21 15:34:14 +01:00
|
|
|
static void removeFileRecursion(QFutureInterface<void> &futureInterface,
|
|
|
|
|
const QFileInfo &f, QString *errorMessage)
|
2010-03-12 15:54:09 +01:00
|
|
|
{
|
2016-01-21 15:34:14 +01:00
|
|
|
if (futureInterface.isCanceled())
|
|
|
|
|
return;
|
2010-03-12 15:54:09 +01:00
|
|
|
// The version control system might list files/directory in arbitrary
|
|
|
|
|
// order, causing files to be removed from parent directories.
|
|
|
|
|
if (!f.exists())
|
|
|
|
|
return;
|
|
|
|
|
if (f.isDir()) {
|
|
|
|
|
const QDir dir(f.absoluteFilePath());
|
2022-05-19 16:04:26 +02:00
|
|
|
const QList<QFileInfo> infos = dir.entryInfoList(QDir::AllEntries|QDir::NoDotAndDotDot|QDir::Hidden);
|
|
|
|
|
for (const QFileInfo &fi : infos)
|
2016-01-21 15:34:14 +01:00
|
|
|
removeFileRecursion(futureInterface, fi, errorMessage);
|
2010-03-12 15:54:09 +01:00
|
|
|
QDir parent = f.absoluteDir();
|
|
|
|
|
if (!parent.rmdir(f.fileName()))
|
2012-01-07 12:31:48 +01:00
|
|
|
errorMessage->append(VcsBase::CleanDialog::tr("The directory %1 could not be deleted.").
|
2010-07-05 09:52:32 +02:00
|
|
|
arg(QDir::toNativeSeparators(f.absoluteFilePath())));
|
2010-03-12 15:54:09 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!QFile::remove(f.absoluteFilePath())) {
|
|
|
|
|
if (!errorMessage->isEmpty())
|
|
|
|
|
errorMessage->append(QLatin1Char('\n'));
|
2012-01-07 12:31:48 +01:00
|
|
|
errorMessage->append(VcsBase::CleanDialog::tr("The file %1 could not be deleted.").
|
2010-07-05 09:52:32 +02:00
|
|
|
arg(QDir::toNativeSeparators(f.absoluteFilePath())));
|
2010-03-12 15:54:09 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 15:34:14 +01:00
|
|
|
// Cleaning files in the background
|
|
|
|
|
static void runCleanFiles(QFutureInterface<void> &futureInterface,
|
|
|
|
|
const QString &repository, const QStringList &files,
|
|
|
|
|
const std::function<void(const QString&)> &errorHandler)
|
2010-03-12 15:54:09 +01:00
|
|
|
{
|
2016-01-21 15:34:14 +01:00
|
|
|
QString errorMessage;
|
|
|
|
|
futureInterface.setProgressRange(0, files.size());
|
|
|
|
|
futureInterface.setProgressValue(0);
|
2022-05-19 16:04:26 +02:00
|
|
|
for (const QString &name : files) {
|
2016-01-21 15:34:14 +01:00
|
|
|
removeFileRecursion(futureInterface, QFileInfo(name), &errorMessage);
|
|
|
|
|
if (futureInterface.isCanceled())
|
|
|
|
|
break;
|
|
|
|
|
futureInterface.setProgressValue(futureInterface.progressValue() + 1);
|
|
|
|
|
}
|
|
|
|
|
if (!errorMessage.isEmpty()) {
|
2010-03-12 15:54:09 +01:00
|
|
|
// Format and emit error.
|
2010-07-05 09:52:32 +02:00
|
|
|
const QString msg = CleanDialog::tr("There were errors when cleaning the repository %1:").
|
2016-01-21 15:34:14 +01:00
|
|
|
arg(QDir::toNativeSeparators(repository));
|
|
|
|
|
errorMessage.insert(0, QLatin1Char('\n'));
|
|
|
|
|
errorMessage.insert(0, msg);
|
|
|
|
|
errorHandler(errorMessage);
|
2010-03-12 15:54:09 +01:00
|
|
|
}
|
2016-01-21 15:34:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void handleError(const QString &errorMessage)
|
|
|
|
|
{
|
|
|
|
|
QTimer::singleShot(0, VcsOutputWindow::instance(), [errorMessage]() {
|
|
|
|
|
VcsOutputWindow::instance()->appendSilently(errorMessage);
|
|
|
|
|
});
|
2010-03-12 15:54:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------------- CleanDialogPrivate ----------------
|
2011-12-08 13:07:00 +01:00
|
|
|
|
|
|
|
|
class CleanDialogPrivate
|
|
|
|
|
{
|
|
|
|
|
public:
|
2022-08-05 14:37:37 +02:00
|
|
|
CleanDialogPrivate() :
|
|
|
|
|
m_filesModel(new QStandardItemModel(0, columnCount))
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
QGroupBox *m_groupBox;
|
|
|
|
|
QCheckBox *m_selectAllCheckBox;
|
|
|
|
|
QTreeView *m_filesTreeView;
|
2010-03-12 15:54:09 +01:00
|
|
|
|
|
|
|
|
QStandardItemModel *m_filesModel;
|
|
|
|
|
QString m_workingDirectory;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2011-12-08 13:07:00 +01:00
|
|
|
} // namespace Internal
|
|
|
|
|
|
|
|
|
|
/*!
|
2012-01-07 12:31:48 +01:00
|
|
|
\class VcsBase::CleanDialog
|
2011-12-08 13:07:00 +01:00
|
|
|
|
2013-06-05 14:29:24 +02:00
|
|
|
\brief The CleanDialog class provides a file selector dialog for files not
|
|
|
|
|
under version control.
|
2011-12-08 13:07:00 +01:00
|
|
|
|
|
|
|
|
Completely clean a directory under version control
|
|
|
|
|
from all files that are not under version control based on a list
|
|
|
|
|
generated from the version control system. Presents the user with
|
|
|
|
|
a checkable list of files and/or directories. Double click opens a file.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-03-12 15:54:09 +01:00
|
|
|
CleanDialog::CleanDialog(QWidget *parent) :
|
|
|
|
|
QDialog(parent),
|
2011-12-08 13:07:00 +01:00
|
|
|
d(new Internal::CleanDialogPrivate)
|
2010-03-12 15:54:09 +01:00
|
|
|
{
|
|
|
|
|
setModal(true);
|
2022-08-05 14:37:37 +02:00
|
|
|
resize(682, 659);
|
|
|
|
|
setWindowTitle(tr("Clean Repository"));
|
|
|
|
|
|
|
|
|
|
d->m_groupBox = new QGroupBox(this);
|
|
|
|
|
|
|
|
|
|
d->m_selectAllCheckBox = new QCheckBox(tr("Select All"));
|
2010-03-12 15:54:09 +01:00
|
|
|
|
2022-08-05 14:37:37 +02:00
|
|
|
auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel);
|
|
|
|
|
buttonBox->addButton(tr("Delete..."), QDialogButtonBox::AcceptRole);
|
2010-03-12 15:54:09 +01:00
|
|
|
|
|
|
|
|
d->m_filesModel->setHorizontalHeaderLabels(QStringList(tr("Name")));
|
2022-08-05 14:37:37 +02:00
|
|
|
|
|
|
|
|
d->m_filesTreeView = new QTreeView;
|
|
|
|
|
d->m_filesTreeView->setModel(d->m_filesModel);
|
|
|
|
|
d->m_filesTreeView->setUniformRowHeights(true);
|
|
|
|
|
d->m_filesTreeView->setSelectionMode(QAbstractItemView::NoSelection);
|
|
|
|
|
d->m_filesTreeView->setAllColumnsShowFocus(true);
|
|
|
|
|
d->m_filesTreeView->setRootIsDecorated(false);
|
|
|
|
|
|
|
|
|
|
using namespace Utils::Layouting;
|
|
|
|
|
|
|
|
|
|
Column {
|
|
|
|
|
d->m_selectAllCheckBox,
|
|
|
|
|
d->m_filesTreeView
|
|
|
|
|
}.attachTo(d->m_groupBox);
|
|
|
|
|
|
|
|
|
|
Column {
|
|
|
|
|
d->m_groupBox,
|
|
|
|
|
buttonBox
|
|
|
|
|
}.attachTo(this);
|
|
|
|
|
|
|
|
|
|
connect(d->m_filesTreeView, &QAbstractItemView::doubleClicked,
|
2015-03-04 15:51:32 +01:00
|
|
|
this, &CleanDialog::slotDoubleClicked);
|
2022-08-05 14:37:37 +02:00
|
|
|
connect(d->m_selectAllCheckBox, &QAbstractButton::clicked,
|
2015-03-04 15:51:32 +01:00
|
|
|
this, &CleanDialog::selectAllItems);
|
2022-08-05 14:37:37 +02:00
|
|
|
connect(d->m_filesTreeView, &QAbstractItemView::clicked,
|
2015-03-04 15:51:32 +01:00
|
|
|
this, &CleanDialog::updateSelectAllCheckBox);
|
2022-08-05 14:37:37 +02:00
|
|
|
|
|
|
|
|
connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
|
|
|
|
|
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
|
2010-03-12 15:54:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CleanDialog::~CleanDialog()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-01 17:55:22 +03:00
|
|
|
void CleanDialog::setFileList(const QString &workingDirectory, const QStringList &files,
|
|
|
|
|
const QStringList &ignoredFiles)
|
2010-03-12 15:54:09 +01:00
|
|
|
{
|
|
|
|
|
d->m_workingDirectory = workingDirectory;
|
2022-08-05 14:37:37 +02:00
|
|
|
d->m_groupBox->setTitle(tr("Repository: %1").
|
2010-07-05 09:52:32 +02:00
|
|
|
arg(QDir::toNativeSeparators(workingDirectory)));
|
2010-03-12 15:54:09 +01:00
|
|
|
if (const int oldRowCount = d->m_filesModel->rowCount())
|
|
|
|
|
d->m_filesModel->removeRows(0, oldRowCount);
|
|
|
|
|
|
2022-05-19 16:04:26 +02:00
|
|
|
for (const QString &fileName : files)
|
2012-07-01 17:55:22 +03:00
|
|
|
addFile(workingDirectory, fileName, true);
|
2022-05-19 16:04:26 +02:00
|
|
|
for (const QString &fileName : ignoredFiles)
|
2012-07-01 17:55:22 +03:00
|
|
|
addFile(workingDirectory, fileName, false);
|
|
|
|
|
|
|
|
|
|
for (int c = 0; c < d->m_filesModel->columnCount(); c++)
|
2022-08-05 14:37:37 +02:00
|
|
|
d->m_filesTreeView->resizeColumnToContents(c);
|
2013-03-26 17:27:17 +01:00
|
|
|
|
|
|
|
|
if (ignoredFiles.isEmpty())
|
2022-08-05 14:37:37 +02:00
|
|
|
d->m_selectAllCheckBox->setChecked(true);
|
2012-07-01 17:55:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CleanDialog::addFile(const QString &workingDirectory, QString fileName, bool checked)
|
|
|
|
|
{
|
2010-03-12 15:54:09 +01:00
|
|
|
QStyle *style = QApplication::style();
|
|
|
|
|
const QIcon folderIcon = style->standardIcon(QStyle::SP_DirIcon);
|
|
|
|
|
const QIcon fileIcon = style->standardIcon(QStyle::SP_FileIcon);
|
|
|
|
|
const QChar slash = QLatin1Char('/');
|
2012-07-01 17:55:22 +03:00
|
|
|
// Clean the trailing slash of directories
|
|
|
|
|
if (fileName.endsWith(slash))
|
|
|
|
|
fileName.chop(1);
|
|
|
|
|
QFileInfo fi(workingDirectory + slash + fileName);
|
|
|
|
|
bool isDir = fi.isDir();
|
|
|
|
|
if (isDir)
|
|
|
|
|
checked = false;
|
2015-01-22 13:54:05 +01:00
|
|
|
auto nameItem = new QStandardItem(QDir::toNativeSeparators(fileName));
|
2012-07-01 17:55:22 +03:00
|
|
|
nameItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled);
|
|
|
|
|
nameItem->setIcon(isDir ? folderIcon : fileIcon);
|
|
|
|
|
nameItem->setCheckable(true);
|
|
|
|
|
nameItem->setCheckState(checked ? Qt::Checked : Qt::Unchecked);
|
|
|
|
|
nameItem->setData(QVariant(fi.absoluteFilePath()), Internal::fileNameRole);
|
|
|
|
|
nameItem->setData(QVariant(isDir), Internal::isDirectoryRole);
|
|
|
|
|
// Tooltip with size information
|
|
|
|
|
if (fi.isFile()) {
|
2020-06-19 18:49:17 +02:00
|
|
|
const QString lastModified =
|
|
|
|
|
QLocale::system().toString(fi.lastModified(), QLocale::ShortFormat);
|
2018-09-20 01:32:36 +03:00
|
|
|
nameItem->setToolTip(tr("%n bytes, last modified %1.", nullptr,
|
|
|
|
|
fi.size()).arg(lastModified));
|
2010-03-12 15:54:09 +01:00
|
|
|
}
|
2012-07-01 17:55:22 +03:00
|
|
|
d->m_filesModel->appendRow(nameItem);
|
2010-03-12 15:54:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList CleanDialog::checkedFiles() const
|
|
|
|
|
{
|
|
|
|
|
QStringList rc;
|
|
|
|
|
if (const int rowCount = d->m_filesModel->rowCount()) {
|
|
|
|
|
for (int r = 0; r < rowCount; r++) {
|
|
|
|
|
const QStandardItem *item = d->m_filesModel->item(r, 0);
|
|
|
|
|
if (item->checkState() == Qt::Checked)
|
2011-12-08 13:07:00 +01:00
|
|
|
rc.push_back(item->data(Internal::fileNameRole).toString());
|
2010-03-12 15:54:09 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CleanDialog::accept()
|
|
|
|
|
{
|
|
|
|
|
if (promptToDelete())
|
|
|
|
|
QDialog::accept();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CleanDialog::promptToDelete()
|
|
|
|
|
{
|
|
|
|
|
// Prompt the user and delete files
|
|
|
|
|
const QStringList selectedFiles = checkedFiles();
|
|
|
|
|
if (selectedFiles.isEmpty())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (QMessageBox::question(this, tr("Delete"),
|
2018-09-20 01:32:36 +03:00
|
|
|
tr("Do you want to delete %n files?", nullptr, selectedFiles.size()),
|
2010-03-12 15:54:09 +01:00
|
|
|
QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes) != QMessageBox::Yes)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Remove in background
|
2016-02-08 16:26:19 +01:00
|
|
|
QFuture<void> task = Utils::runAsync(Internal::runCleanFiles, d->m_workingDirectory,
|
|
|
|
|
selectedFiles, Internal::handleError);
|
2010-03-12 15:54:09 +01:00
|
|
|
|
2014-04-17 15:14:14 +02:00
|
|
|
const QString taskName = tr("Cleaning \"%1\"").
|
2010-07-05 09:52:32 +02:00
|
|
|
arg(QDir::toNativeSeparators(d->m_workingDirectory));
|
2013-09-03 15:18:37 +02:00
|
|
|
Core::ProgressManager::addTask(task, taskName, "VcsBase.cleanRepository");
|
2010-03-12 15:54:09 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CleanDialog::slotDoubleClicked(const QModelIndex &index)
|
|
|
|
|
{
|
|
|
|
|
// Open file on doubleclick
|
|
|
|
|
if (const QStandardItem *item = d->m_filesModel->itemFromIndex(index))
|
2011-12-08 13:07:00 +01:00
|
|
|
if (!item->data(Internal::isDirectoryRole).toBool()) {
|
2021-11-01 17:02:02 +01:00
|
|
|
const auto fname = Utils::FilePath::fromVariant(item->data(Internal::fileNameRole));
|
2013-05-31 12:52:53 +02:00
|
|
|
Core::EditorManager::openEditor(fname);
|
2010-03-12 15:54:09 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-26 17:27:17 +01:00
|
|
|
void CleanDialog::selectAllItems(bool checked)
|
|
|
|
|
{
|
|
|
|
|
if (const int rowCount = d->m_filesModel->rowCount()) {
|
|
|
|
|
for (int r = 0; r < rowCount; ++r) {
|
|
|
|
|
QStandardItem *item = d->m_filesModel->item(r, 0);
|
|
|
|
|
item->setCheckState(checked ? Qt::Checked : Qt::Unchecked);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CleanDialog::updateSelectAllCheckBox()
|
|
|
|
|
{
|
|
|
|
|
bool checked = true;
|
|
|
|
|
if (const int rowCount = d->m_filesModel->rowCount()) {
|
|
|
|
|
for (int r = 0; r < rowCount; ++r) {
|
|
|
|
|
const QStandardItem *item = d->m_filesModel->item(r, 0);
|
|
|
|
|
if (item->checkState() == Qt::Unchecked) {
|
|
|
|
|
checked = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-05 14:37:37 +02:00
|
|
|
d->m_selectAllCheckBox->setChecked(checked);
|
2013-03-26 17:27:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-07 12:31:48 +01:00
|
|
|
} // namespace VcsBase
|