forked from qt-creator/qt-creator
Core: FilePath-ify ReadOnlyFilesDialog
Change-Id: Iccde7e5cfcee0bdb02720279e8a310857c0a7a67 Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -33,6 +33,7 @@
|
||||
#include <coreplugin/iversioncontrol.h>
|
||||
#include <coreplugin/vcsmanager.h>
|
||||
|
||||
#include <utils/algorithm.h>
|
||||
#include <utils/fileutils.h>
|
||||
#include <utils/hostosinfo.h>
|
||||
#include <utils/stringutils.h>
|
||||
@@ -45,6 +46,8 @@
|
||||
#include <QPushButton>
|
||||
#include <QRadioButton>
|
||||
|
||||
using namespace Utils;
|
||||
|
||||
namespace Core {
|
||||
namespace Internal {
|
||||
|
||||
@@ -65,8 +68,8 @@ public:
|
||||
NumberOfColumns
|
||||
};
|
||||
|
||||
void initDialog(const QStringList &fileNames);
|
||||
void promptFailWarning(const QStringList &files, ReadOnlyFilesDialog::ReadOnlyResult type) const;
|
||||
void initDialog(const FilePathList &filePaths);
|
||||
void promptFailWarning(const FilePathList &files, ReadOnlyFilesDialog::ReadOnlyResult type) const;
|
||||
QRadioButton *createRadioButtonForItem(QTreeWidgetItem *item, QButtonGroup *group, ReadOnlyFilesTreeColumn type);
|
||||
|
||||
void setAll(int index);
|
||||
@@ -77,14 +80,14 @@ public:
|
||||
// Buttongroups containing the operation for one file.
|
||||
struct ButtonGroupForFile
|
||||
{
|
||||
QString fileName;
|
||||
FilePath filePath;
|
||||
QButtonGroup *group;
|
||||
};
|
||||
QList <ButtonGroupForFile> buttonGroups;
|
||||
|
||||
QMap <int, int> setAllIndexForOperation;
|
||||
// The version control systems for every file, if the file isn't in VCS the value is 0.
|
||||
QHash <QString, IVersionControl*> versionControls;
|
||||
QHash<FilePath, IVersionControl*> versionControls;
|
||||
|
||||
// Define if some specific operations should be allowed to make the files writable.
|
||||
const bool useSaveAs;
|
||||
@@ -140,19 +143,19 @@ using namespace Internal;
|
||||
* and Save As which is used to save the changes to a document in another file.
|
||||
*/
|
||||
|
||||
ReadOnlyFilesDialog::ReadOnlyFilesDialog(const QList<QString> &fileNames, QWidget *parent)
|
||||
ReadOnlyFilesDialog::ReadOnlyFilesDialog(const Utils::FilePathList &filePaths, QWidget *parent)
|
||||
: QDialog(parent)
|
||||
, d(new ReadOnlyFilesDialogPrivate(this))
|
||||
{
|
||||
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
||||
d->initDialog(fileNames);
|
||||
d->initDialog(filePaths);
|
||||
}
|
||||
|
||||
ReadOnlyFilesDialog::ReadOnlyFilesDialog(const QString &fileName, QWidget *parent)
|
||||
ReadOnlyFilesDialog::ReadOnlyFilesDialog(const Utils::FilePath &filePath, QWidget *parent)
|
||||
: QDialog(parent)
|
||||
, d(new ReadOnlyFilesDialogPrivate(this))
|
||||
{
|
||||
d->initDialog(QStringList(fileName));
|
||||
d->initDialog({filePath});
|
||||
}
|
||||
|
||||
ReadOnlyFilesDialog::ReadOnlyFilesDialog(IDocument *document, QWidget *parent,
|
||||
@@ -160,16 +163,16 @@ ReadOnlyFilesDialog::ReadOnlyFilesDialog(IDocument *document, QWidget *parent,
|
||||
: QDialog(parent)
|
||||
, d(new ReadOnlyFilesDialogPrivate(this, document, displaySaveAs))
|
||||
{
|
||||
d->initDialog(QStringList(document->filePath().toString()));
|
||||
d->initDialog({document->filePath()});
|
||||
}
|
||||
|
||||
ReadOnlyFilesDialog::ReadOnlyFilesDialog(const QList<IDocument *> &documents, QWidget *parent)
|
||||
: QDialog(parent)
|
||||
, d(new ReadOnlyFilesDialogPrivate(this))
|
||||
{
|
||||
QStringList files;
|
||||
foreach (IDocument *document, documents)
|
||||
files << document->filePath().toString();
|
||||
FilePathList files;
|
||||
for (IDocument *document : documents)
|
||||
files << document->filePath();
|
||||
d->initDialog(files);
|
||||
}
|
||||
|
||||
@@ -202,7 +205,7 @@ void ReadOnlyFilesDialog::setShowFailWarning(bool show, const QString &warning)
|
||||
* Opens a message box with an error description according to the type.
|
||||
* \internal
|
||||
*/
|
||||
void ReadOnlyFilesDialogPrivate::promptFailWarning(const QStringList &files, ReadOnlyFilesDialog::ReadOnlyResult type) const
|
||||
void ReadOnlyFilesDialogPrivate::promptFailWarning(const FilePathList &files, ReadOnlyFilesDialog::ReadOnlyResult type) const
|
||||
{
|
||||
if (files.isEmpty())
|
||||
return;
|
||||
@@ -210,7 +213,7 @@ void ReadOnlyFilesDialogPrivate::promptFailWarning(const QStringList &files, Rea
|
||||
QString message;
|
||||
QString details;
|
||||
if (files.count() == 1) {
|
||||
const QString file = files.first();
|
||||
const FilePath file = files.first();
|
||||
switch (type) {
|
||||
case ReadOnlyFilesDialog::RO_OpenVCS: {
|
||||
if (IVersionControl *vc = versionControls[file]) {
|
||||
@@ -218,31 +221,31 @@ void ReadOnlyFilesDialogPrivate::promptFailWarning(const QStringList &files, Rea
|
||||
title = tr("Failed to %1 File").arg(openText);
|
||||
message = tr("%1 file %2 from version control system %3 failed.")
|
||||
.arg(openText)
|
||||
.arg(QDir::toNativeSeparators(file))
|
||||
.arg(file.toUserOutput())
|
||||
.arg(vc->displayName())
|
||||
+ QLatin1Char('\n')
|
||||
+ '\n'
|
||||
+ failWarning;
|
||||
} else {
|
||||
title = tr("No Version Control System Found");
|
||||
message = tr("Cannot open file %1 from version control system.\n"
|
||||
"No version control system found.")
|
||||
.arg(QDir::toNativeSeparators(file))
|
||||
+ QLatin1Char('\n')
|
||||
+ failWarning;;
|
||||
.arg(file.toUserOutput())
|
||||
+ '\n'
|
||||
+ failWarning;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ReadOnlyFilesDialog::RO_MakeWritable:
|
||||
title = tr("Cannot Set Permissions");
|
||||
message = tr("Cannot set permissions for %1 to writable.")
|
||||
.arg(QDir::toNativeSeparators(file))
|
||||
+ QLatin1Char('\n')
|
||||
.arg(file.toUserOutput())
|
||||
+ '\n'
|
||||
+ failWarning;
|
||||
break;
|
||||
case ReadOnlyFilesDialog::RO_SaveAs:
|
||||
title = tr("Cannot Save File");
|
||||
message = tr("Cannot save file %1").arg(QDir::toNativeSeparators(file))
|
||||
+ QLatin1Char('\n')
|
||||
message = tr("Cannot save file %1").arg(file.toUserOutput())
|
||||
+ '\n'
|
||||
+ failWarning;
|
||||
break;
|
||||
default:
|
||||
@@ -254,7 +257,7 @@ void ReadOnlyFilesDialogPrivate::promptFailWarning(const QStringList &files, Rea
|
||||
title = tr("Could Not Change Permissions on Some Files");
|
||||
message = failWarning + QLatin1Char('\n')
|
||||
+ tr("See details for a complete list of files.");
|
||||
details = files.join(QLatin1Char('\n'));
|
||||
details = Utils::transform(files, &FilePath::toString).join('\n');
|
||||
}
|
||||
QMessageBox msgBox(QMessageBox::Warning, title, message,
|
||||
QMessageBox::Ok, ICore::dialogParent());
|
||||
@@ -278,34 +281,34 @@ int ReadOnlyFilesDialog::exec()
|
||||
return RO_Cancel;
|
||||
|
||||
ReadOnlyResult result = RO_Cancel;
|
||||
QStringList failedToMakeWritable;
|
||||
foreach (ReadOnlyFilesDialogPrivate::ButtonGroupForFile buttongroup, d->buttonGroups) {
|
||||
FilePathList failedToMakeWritable;
|
||||
for (ReadOnlyFilesDialogPrivate::ButtonGroupForFile buttongroup : qAsConst(d->buttonGroups)) {
|
||||
result = static_cast<ReadOnlyResult>(buttongroup.group->checkedId());
|
||||
switch (result) {
|
||||
case RO_MakeWritable:
|
||||
if (!Utils::FileUtils::makeWritable(Utils::FilePath::fromString(buttongroup.fileName))) {
|
||||
failedToMakeWritable << buttongroup.fileName;
|
||||
if (!Utils::FileUtils::makeWritable(buttongroup.filePath)) {
|
||||
failedToMakeWritable << buttongroup.filePath;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
case RO_OpenVCS:
|
||||
if (!d->versionControls[buttongroup.fileName]->vcsOpen(buttongroup.fileName)) {
|
||||
failedToMakeWritable << buttongroup.fileName;
|
||||
if (!d->versionControls[buttongroup.filePath]->vcsOpen(buttongroup.filePath.toString())) {
|
||||
failedToMakeWritable << buttongroup.filePath;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
case RO_SaveAs:
|
||||
if (!EditorManagerPrivate::saveDocumentAs(d->document)) {
|
||||
failedToMakeWritable << buttongroup.fileName;
|
||||
failedToMakeWritable << buttongroup.filePath;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
failedToMakeWritable << buttongroup.fileName;
|
||||
failedToMakeWritable << buttongroup.filePath;
|
||||
continue;
|
||||
}
|
||||
if (!QFileInfo(buttongroup.fileName).isWritable())
|
||||
failedToMakeWritable << buttongroup.fileName;
|
||||
if (!buttongroup.filePath.toFileInfo().isWritable())
|
||||
failedToMakeWritable << buttongroup.filePath;
|
||||
}
|
||||
if (!failedToMakeWritable.isEmpty()) {
|
||||
if (d->showWarnings)
|
||||
@@ -381,11 +384,11 @@ void ReadOnlyFilesDialogPrivate::updateSelectAll()
|
||||
/*!
|
||||
* Adds files to the dialog and checks for a possible operation to make the file
|
||||
* writable.
|
||||
* \a fileNames contains the list of the files that should be added to the
|
||||
* \a filePaths contains the list of the files that should be added to the
|
||||
* dialog.
|
||||
* \internal
|
||||
*/
|
||||
void ReadOnlyFilesDialogPrivate::initDialog(const QStringList &fileNames)
|
||||
void ReadOnlyFilesDialogPrivate::initDialog(const FilePathList &filePaths)
|
||||
{
|
||||
ui.setupUi(q);
|
||||
ui.buttonBox->addButton(tr("Change &Permission"), QDialogButtonBox::AcceptRole);
|
||||
@@ -394,15 +397,15 @@ void ReadOnlyFilesDialogPrivate::initDialog(const QStringList &fileNames)
|
||||
QString vcsOpenTextForAll;
|
||||
QString vcsMakeWritableTextForAll;
|
||||
bool useMakeWritable = false;
|
||||
foreach (const QString &fileName, fileNames) {
|
||||
const QFileInfo info = QFileInfo(fileName);
|
||||
for (const FilePath &filePath : filePaths) {
|
||||
const QFileInfo info = filePath.toFileInfo();
|
||||
const QString visibleName = info.fileName();
|
||||
const QString directory = info.absolutePath();
|
||||
|
||||
// Setup a default entry with filename folder and make writable radio button.
|
||||
auto item = new QTreeWidgetItem(ui.treeWidget);
|
||||
item->setText(FileName, visibleName);
|
||||
item->setIcon(FileName, FileIconProvider::icon(fileName));
|
||||
item->setIcon(FileName, FileIconProvider::icon(info));
|
||||
item->setText(Folder, Utils::FilePath::fromFileInfo(directory).shortNativePath());
|
||||
auto radioButtonGroup = new QButtonGroup;
|
||||
|
||||
@@ -411,7 +414,7 @@ void ReadOnlyFilesDialogPrivate::initDialog(const QStringList &fileNames)
|
||||
IVersionControl *versionControlForFile =
|
||||
VcsManager::findVersionControlForDirectory(directory);
|
||||
const bool fileManagedByVCS = versionControlForFile
|
||||
&& versionControlForFile->openSupportMode(fileName) != IVersionControl::NoOpen;
|
||||
&& versionControlForFile->openSupportMode(filePath.toString()) != IVersionControl::NoOpen;
|
||||
if (fileManagedByVCS) {
|
||||
const QString vcsOpenTextForFile =
|
||||
Utils::stripAccelerator(versionControlForFile->vcsOpenText());
|
||||
@@ -429,7 +432,7 @@ void ReadOnlyFilesDialogPrivate::initDialog(const QStringList &fileNames)
|
||||
vcsMakeWritableTextForAll.clear();
|
||||
}
|
||||
// Add make writable if it is supported by the reposetory.
|
||||
if (versionControlForFile->openSupportMode(fileName) == IVersionControl::OpenOptional) {
|
||||
if (versionControlForFile->openSupportMode(filePath.toString()) == IVersionControl::OpenOptional) {
|
||||
useMakeWritable = true;
|
||||
createRadioButtonForItem(item, radioButtonGroup, MakeWritable);
|
||||
}
|
||||
@@ -442,13 +445,10 @@ void ReadOnlyFilesDialogPrivate::initDialog(const QStringList &fileNames)
|
||||
if (useSaveAs)
|
||||
createRadioButtonForItem(item, radioButtonGroup, SaveAs);
|
||||
// If the file is managed by a version control system save the vcs for this file.
|
||||
versionControls[fileName] = fileManagedByVCS ? versionControlForFile : nullptr;
|
||||
versionControls[filePath] = fileManagedByVCS ? versionControlForFile : nullptr;
|
||||
|
||||
// Also save the buttongroup for every file to get the result for each entry.
|
||||
ReadOnlyFilesDialogPrivate::ButtonGroupForFile groupForFile;
|
||||
groupForFile.fileName = fileName;
|
||||
groupForFile.group = radioButtonGroup;
|
||||
buttonGroups.append(groupForFile);
|
||||
buttonGroups.append({filePath, radioButtonGroup});
|
||||
QObject::connect(radioButtonGroup, QOverload<int>::of(&QButtonGroup::buttonClicked),
|
||||
[this] { updateSelectAll(); });
|
||||
}
|
||||
@@ -474,7 +474,7 @@ void ReadOnlyFilesDialogPrivate::initDialog(const QStringList &fileNames)
|
||||
}
|
||||
|
||||
// If there is just one file entry, there is no need to show the select all combo box
|
||||
if (fileNames.count() < 2) {
|
||||
if (filePaths.count() < 2) {
|
||||
ui.setAll->setVisible(false);
|
||||
ui.setAllLabel->setVisible(false);
|
||||
ui.verticalLayout->removeItem(ui.setAllLayout);
|
||||
|
||||
Reference in New Issue
Block a user