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