forked from qt-creator/qt-creator
Utils: Return Result<> from one of the FileSaver::finalize() overloads
Change-Id: Iebd2cbbd0d577503b8fc84e95066f4dad6bbe899 Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io> Reviewed-by: David Schulz <david.schulz@qt.io> Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
@@ -82,21 +82,12 @@ FileSaverBase::FileSaverBase()
|
||||
|
||||
FileSaverBase::~FileSaverBase() = default;
|
||||
|
||||
bool FileSaverBase::finalize()
|
||||
Result<> FileSaverBase::finalize()
|
||||
{
|
||||
m_file->close();
|
||||
setResult(m_file->error() == QFile::NoError);
|
||||
m_file.reset();
|
||||
return m_result.has_value();
|
||||
}
|
||||
|
||||
bool FileSaverBase::finalize(QString *errStr)
|
||||
{
|
||||
if (finalize())
|
||||
return true;
|
||||
if (errStr)
|
||||
*errStr = errorString();
|
||||
return false;
|
||||
return m_result;
|
||||
}
|
||||
|
||||
#ifdef QT_GUI_LIB
|
||||
@@ -223,7 +214,7 @@ FileSaver::FileSaver(const FilePath &filePath, QIODevice::OpenMode mode)
|
||||
}
|
||||
}
|
||||
|
||||
bool FileSaver::finalize()
|
||||
Result<> FileSaver::finalize()
|
||||
{
|
||||
if (!m_isSafe)
|
||||
return FileSaverBase::finalize();
|
||||
@@ -236,7 +227,7 @@ bool FileSaver::finalize()
|
||||
setResult(sf->commit());
|
||||
}
|
||||
m_file.reset();
|
||||
return m_result.has_value();
|
||||
return m_result;
|
||||
}
|
||||
|
||||
TempFileSaver::TempFileSaver(const QString &templ)
|
||||
|
@@ -6,9 +6,7 @@
|
||||
#include "utils_global.h"
|
||||
|
||||
#include "filepath.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QDir>
|
||||
#include "result.h"
|
||||
|
||||
#ifdef QT_WIDGETS_LIB
|
||||
#include <QFileDialog>
|
||||
@@ -161,8 +159,7 @@ public:
|
||||
FilePath filePath() const { return m_filePath; }
|
||||
bool hasError() const { return !m_result; }
|
||||
QString errorString() const { return m_result.error(); }
|
||||
virtual bool finalize();
|
||||
bool finalize(QString *errStr);
|
||||
virtual Utils::Result<> finalize();
|
||||
#ifdef QT_GUI_LIB
|
||||
bool finalize(QWidget *parent);
|
||||
#endif
|
||||
@@ -191,7 +188,7 @@ public:
|
||||
// QIODevice::WriteOnly is implicit
|
||||
explicit FileSaver(const FilePath &filePath, QIODevice::OpenMode mode = QIODevice::NotOpen);
|
||||
|
||||
bool finalize() override;
|
||||
Utils::Result<> finalize() override;
|
||||
using FileSaverBase::finalize;
|
||||
|
||||
private:
|
||||
|
@@ -351,12 +351,11 @@ Result<> TextFileFormat::writeFile(const FilePath &filePath, QString plainText)
|
||||
saver.write(m_codec->fromUnicode(plainText));
|
||||
}
|
||||
|
||||
QString errorString;
|
||||
const bool ok = saver.finalize(&errorString);
|
||||
const Result<> result = saver.finalize();
|
||||
if (debug)
|
||||
qDebug().nospace() << Q_FUNC_INFO << filePath << ' ' << *this << ' ' << plainText.size()
|
||||
<< " bytes, returns " << ok;
|
||||
return ok ? ResultOk : ResultError(errorString);
|
||||
<< " bytes, returns " << result.has_value();
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace Utils
|
||||
|
@@ -658,9 +658,8 @@ Result<> BinEditorDocument::save(const FilePath &oldFilePath, const FilePath &ne
|
||||
saver.setResult(output->resize(size));
|
||||
}
|
||||
|
||||
QString errorString;
|
||||
if (!saver.finalize(&errorString))
|
||||
return ResultError(errorString);
|
||||
if (const Result<> res = saver.finalize(); !res)
|
||||
return res;
|
||||
|
||||
setModified(false);
|
||||
return ResultOk;
|
||||
|
@@ -192,7 +192,7 @@ bool CommandsFile::exportCommands(const QList<ShortcutItem *> &items)
|
||||
if (!saver.setResult(&w))
|
||||
qWarning() << saver.errorString();
|
||||
}
|
||||
return saver.finalize();
|
||||
return saver.finalize().has_value();
|
||||
}
|
||||
|
||||
static int translateModifiers(Qt::KeyboardModifiers state, const QString &text)
|
||||
|
@@ -459,10 +459,10 @@ static QString stringForOutputHandling(ExternalTool::OutputHandling handling)
|
||||
return QString();
|
||||
}
|
||||
|
||||
bool ExternalTool::save(QString *errorMessage) const
|
||||
Result<> ExternalTool::save() const
|
||||
{
|
||||
if (m_filePath.isEmpty())
|
||||
return false;
|
||||
return ResultError(ResultAssert); // FIXME: Find something better
|
||||
FileSaver saver(m_filePath);
|
||||
if (!saver.hasError()) {
|
||||
QXmlStreamWriter out(saver.file());
|
||||
@@ -504,7 +504,7 @@ bool ExternalTool::save(QString *errorMessage) const
|
||||
|
||||
saver.setResult(&out);
|
||||
}
|
||||
return saver.finalize(errorMessage);
|
||||
return saver.finalize();
|
||||
}
|
||||
|
||||
bool ExternalTool::operator==(const ExternalTool &other) const
|
||||
|
@@ -60,7 +60,7 @@ public:
|
||||
static Utils::Result<ExternalTool *> createFromFile(const Utils::FilePath &filePath,
|
||||
const QString &locale = {});
|
||||
|
||||
bool save(QString *errorMessage = nullptr) const;
|
||||
Utils::Result<> save() const;
|
||||
|
||||
bool operator==(const ExternalTool &other) const;
|
||||
bool operator!=(const ExternalTool &other) const { return !((*this) == other); }
|
||||
|
@@ -156,9 +156,7 @@ Result<> GeneratedFilePrivate::writeContents() const
|
||||
QIODevice::OpenMode flags = QIODevice::WriteOnly | QIODevice::Truncate;
|
||||
FileSaver saver(path, flags);
|
||||
saver.write(contents);
|
||||
QString errorMessage;
|
||||
const bool ok = saver.finalize(&errorMessage);
|
||||
return ok ? ResultOk : ResultError(errorMessage);
|
||||
return saver.finalize();
|
||||
}
|
||||
|
||||
TextFileFormat format;
|
||||
|
@@ -25,8 +25,7 @@
|
||||
|
||||
using namespace Utils;
|
||||
|
||||
namespace Gerrit {
|
||||
namespace Internal {
|
||||
namespace Gerrit::Internal {
|
||||
|
||||
static QRegularExpressionMatch entryMatch(const QString &line, const QString &type)
|
||||
{
|
||||
@@ -176,10 +175,11 @@ bool AuthenticationDialog::setupCredentials()
|
||||
}
|
||||
if (!found)
|
||||
out << "machine " << m_server->host << " login " << user << " password " << password << '\n';
|
||||
Utils::FileSaver saver(Utils::FilePath::fromString(m_netrcFileName),
|
||||
|
||||
FileSaver saver(FilePath::fromString(m_netrcFileName),
|
||||
QFile::WriteOnly | QFile::Truncate | QFile::Text);
|
||||
saver.write(netrcContents.toUtf8());
|
||||
return saver.finalize();
|
||||
return saver.finalize().has_value();
|
||||
}
|
||||
|
||||
void AuthenticationDialog::checkCredentials()
|
||||
@@ -190,5 +190,4 @@ void AuthenticationDialog::checkCredentials()
|
||||
m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(result == 200);
|
||||
}
|
||||
|
||||
} // Internal
|
||||
} // Gerrit
|
||||
} // Gerrit::Internal
|
||||
|
@@ -10,6 +10,7 @@
|
||||
|
||||
#include <QFileInfo>
|
||||
#include <QDataStream>
|
||||
#include <QCoreApplication>
|
||||
|
||||
namespace Macros::Internal {
|
||||
|
||||
|
@@ -1064,8 +1064,11 @@ std::shared_ptr<TempFileSaver> PerforcePluginPrivate::createTemporaryArgumentFil
|
||||
if (i != last)
|
||||
rc->write("\n", 1);
|
||||
}
|
||||
if (!rc->finalize(errorString))
|
||||
if (const Result<> res = rc->finalize(); !res) {
|
||||
if (errorString)
|
||||
*errorString = res.error();
|
||||
return std::shared_ptr<TempFileSaver>();
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
@@ -279,12 +279,11 @@ void SnippetsCollection::reload()
|
||||
insertSnippet(snippet);
|
||||
}
|
||||
|
||||
bool SnippetsCollection::synchronize(QString *errorString)
|
||||
Result<> SnippetsCollection::synchronize()
|
||||
{
|
||||
if (!m_userSnippetsFile.parentDir().ensureWritableDir()) {
|
||||
*errorString = Tr::tr("Cannot create user snippet directory %1")
|
||||
.arg(m_userSnippetsFile.parentDir().toUserOutput());
|
||||
return false;
|
||||
return ResultError(Tr::tr("Cannot create user snippet directory %1")
|
||||
.arg(m_userSnippetsFile.parentDir().toUserOutput()));
|
||||
}
|
||||
FileSaver saver(m_userSnippetsFile);
|
||||
if (!saver.hasError()) {
|
||||
@@ -309,11 +308,11 @@ bool SnippetsCollection::synchronize(QString *errorString)
|
||||
|
||||
saver.setResult(&writer);
|
||||
}
|
||||
if (!saver.finalize(errorString))
|
||||
return false;
|
||||
if (const Result<> res = saver.finalize(); !res)
|
||||
return res;
|
||||
|
||||
reload();
|
||||
return true;
|
||||
return ResultOk;
|
||||
}
|
||||
|
||||
void SnippetsCollection::writeSnippetXML(const Snippet &snippet, QXmlStreamWriter *writer) const
|
||||
|
@@ -68,7 +68,7 @@ public:
|
||||
QList<QString> groupIds() const;
|
||||
|
||||
void reload();
|
||||
bool synchronize(QString *errorString);
|
||||
Utils::Result<> synchronize();
|
||||
|
||||
private:
|
||||
void identifyGroups();
|
||||
|
@@ -388,12 +388,11 @@ void SnippetsSettingsWidget::apply()
|
||||
setSnippetContent();
|
||||
|
||||
if (m_snippetsCollectionChanged) {
|
||||
QString errorString;
|
||||
if (SnippetsCollection::instance()->synchronize(&errorString)) {
|
||||
if (const Result<> res = SnippetsCollection::instance()->synchronize()) {
|
||||
m_snippetsCollectionChanged = false;
|
||||
} else {
|
||||
QMessageBox::critical(Core::ICore::dialogParent(),
|
||||
Tr::tr("Error While Saving Snippet Collection"), errorString);
|
||||
Tr::tr("Error While Saving Snippet Collection"), res.error());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -67,15 +67,12 @@ Result<> SubmitEditorFile::saveImpl(const FilePath &filePath, bool autoSave)
|
||||
{
|
||||
FileSaver saver(filePath, QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
|
||||
saver.write(m_editor->fileContents());
|
||||
QString errorString;
|
||||
if (!saver.finalize(&errorString))
|
||||
return ResultError(errorString);
|
||||
if (const Result<> res = saver.finalize(); !res)
|
||||
return res;
|
||||
if (autoSave)
|
||||
return ResultOk;
|
||||
setFilePath(filePath.absoluteFilePath());
|
||||
setModified(false);
|
||||
if (!errorString.isEmpty())
|
||||
return ResultError(errorString);
|
||||
emit changed();
|
||||
return ResultOk;
|
||||
}
|
||||
|
@@ -530,8 +530,11 @@ bool VcsBaseSubmitEditor::runSubmitMessageCheckScript(const FilePath &checkScrip
|
||||
// Write out message
|
||||
TempFileSaver saver(TemporaryDirectory::masterDirectoryPath() + "/msgXXXXXX.txt");
|
||||
saver.write(fileContents());
|
||||
if (!saver.finalize(errorMessage))
|
||||
if (const Result<> res = saver.finalize(); !res) {
|
||||
if (errorMessage)
|
||||
*errorMessage = res.error();
|
||||
return false;
|
||||
}
|
||||
// Run check process
|
||||
VcsOutputWindow::appendShellCommandLine(msgCheckScript(d->m_checkScriptWorkingDirectory,
|
||||
checkScript));
|
||||
|
Reference in New Issue
Block a user