forked from qt-creator/qt-creator
Utils: Add FilePath constructor for TempFileSaver
Change-Id: I15286be055bd69544e4283740bd0c3411573475c Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -238,6 +238,11 @@ bool FileSaver::finalize()
|
|||||||
}
|
}
|
||||||
|
|
||||||
TempFileSaver::TempFileSaver(const QString &templ)
|
TempFileSaver::TempFileSaver(const QString &templ)
|
||||||
|
{
|
||||||
|
initFromString(templ);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TempFileSaver::initFromString(const QString &templ)
|
||||||
{
|
{
|
||||||
m_file.reset(new QTemporaryFile{});
|
m_file.reset(new QTemporaryFile{});
|
||||||
auto tempFile = static_cast<QTemporaryFile *>(m_file.get());
|
auto tempFile = static_cast<QTemporaryFile *>(m_file.get());
|
||||||
@@ -253,6 +258,29 @@ TempFileSaver::TempFileSaver(const QString &templ)
|
|||||||
m_filePath = FilePath::fromString(tempFile->fileName());
|
m_filePath = FilePath::fromString(tempFile->fileName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TempFileSaver::TempFileSaver(const FilePath &templ)
|
||||||
|
{
|
||||||
|
if (templ.isEmpty() || !templ.needsDevice()) {
|
||||||
|
initFromString(templ.path());
|
||||||
|
} else {
|
||||||
|
expected_str<FilePath> result = templ.createTempFile();
|
||||||
|
if (!result) {
|
||||||
|
m_errorString = Tr::tr("Cannot create temporary file %1: %2")
|
||||||
|
.arg(templ.toUserOutput(), result.error());
|
||||||
|
m_hasError = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_file.reset(new QFile(result->toFSPathString()));
|
||||||
|
if (!m_file->open(QIODevice::WriteOnly)) {
|
||||||
|
m_errorString = Tr::tr("Cannot create temporary file %1: %2")
|
||||||
|
.arg(result->toUserOutput(), m_file->errorString());
|
||||||
|
m_hasError = true;
|
||||||
|
}
|
||||||
|
m_filePath = *result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TempFileSaver::~TempFileSaver()
|
TempFileSaver::~TempFileSaver()
|
||||||
{
|
{
|
||||||
m_file.reset();
|
m_file.reset();
|
||||||
|
@@ -222,10 +222,14 @@ class QTCREATOR_UTILS_EXPORT TempFileSaver : public FileSaverBase
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit TempFileSaver(const QString &templ = QString());
|
explicit TempFileSaver(const QString &templ = QString());
|
||||||
|
explicit TempFileSaver(const FilePath &templ);
|
||||||
~TempFileSaver() override;
|
~TempFileSaver() override;
|
||||||
|
|
||||||
void setAutoRemove(bool on) { m_autoRemove = on; }
|
void setAutoRemove(bool on) { m_autoRemove = on; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void initFromString(const QString &templ);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_autoRemove = true;
|
bool m_autoRemove = true;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user