Core: filepathify IDocument

Change-Id: I364a80d070c5f90433309c281c4906ee101a1a1a
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
David Schulz
2021-05-18 13:55:23 +02:00
parent 7020d54a0a
commit 665c090039
35 changed files with 253 additions and 227 deletions

View File

@@ -47,10 +47,10 @@ AndroidManifestDocument::AndroidManifestDocument(AndroidManifestEditorWidget *ed
this, &Core::IDocument::changed); this, &Core::IDocument::changed);
} }
bool AndroidManifestDocument::save(QString *errorString, const QString &fileName, bool autoSave) bool AndroidManifestDocument::save(QString *errorString, const Utils::FilePath &filePath, bool autoSave)
{ {
m_editorWidget->preSave(); m_editorWidget->preSave();
bool result = TextDocument::save(errorString, fileName, autoSave); bool result = TextDocument::save(errorString, filePath, autoSave);
m_editorWidget->postSave(); m_editorWidget->postSave();
return result; return result;
} }

View File

@@ -36,7 +36,7 @@ class AndroidManifestDocument : public TextEditor::TextDocument
{ {
public: public:
explicit AndroidManifestDocument(AndroidManifestEditorWidget *editorWidget); explicit AndroidManifestDocument(AndroidManifestEditorWidget *editorWidget);
bool save(QString *errorString, const QString &fileName = QString(), bool save(QString *errorString, const Utils::FilePath &filePath,
bool autoSave = false) override; bool autoSave = false) override;
bool isModified() const override; bool isModified() const override;

View File

@@ -230,28 +230,27 @@ public:
return type == TypeRemoved ? BehaviorSilent : IDocument::reloadBehavior(state, type); return type == TypeRemoved ? BehaviorSilent : IDocument::reloadBehavior(state, type);
} }
bool save(QString *errorString, const QString &fn, bool autoSave) override bool save(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override
{ {
QTC_ASSERT(!autoSave, return true); // bineditor does not support autosave - it would be a bit expensive QTC_ASSERT(!autoSave, return true); // bineditor does not support autosave - it would be a bit expensive
const FilePath fileNameToUse = fn.isEmpty() ? filePath() : FilePath::fromString(fn); const FilePath &fileNameToUse = filePath.isEmpty() ? this->filePath() : filePath;
if (m_widget->save(errorString, filePath().toString(), fileNameToUse.toString())) { if (m_widget->save(errorString, this->filePath().toString(), fileNameToUse.toString())) {
setFilePath(fileNameToUse); setFilePath(fileNameToUse);
return true; return true;
} else {
return false;
} }
return false;
} }
OpenResult open(QString *errorString, const QString &fileName, OpenResult open(QString *errorString, const Utils::FilePath &filePath,
const QString &realFileName) override const Utils::FilePath &realFilePath) override
{ {
QTC_CHECK(fileName == realFileName); // The bineditor can do no autosaving QTC_CHECK(filePath == realFilePath); // The bineditor can do no autosaving
return openImpl(errorString, fileName); return openImpl(errorString, filePath);
} }
OpenResult openImpl(QString *errorString, const QString &fileName, quint64 offset = 0) OpenResult openImpl(QString *errorString, const Utils::FilePath &filePath, quint64 offset = 0)
{ {
QFile file(fileName); QFile file(filePath.toString());
if (file.open(QIODevice::ReadOnly)) { if (file.open(QIODevice::ReadOnly)) {
file.close(); file.close();
quint64 size = static_cast<quint64>(file.size()); quint64 size = static_cast<quint64>(file.size());
@@ -274,12 +273,11 @@ public:
} }
if (offset >= size) if (offset >= size)
return OpenResult::CannotHandle; return OpenResult::CannotHandle;
setFilePath(FilePath::fromString(fileName)); setFilePath(filePath);
m_widget->setSizes(offset, file.size()); m_widget->setSizes(offset, file.size());
return OpenResult::Success; return OpenResult::Success;
} }
QString errStr = tr("Cannot open %1: %2").arg( QString errStr = tr("Cannot open %1: %2").arg(filePath.toUserOutput(), file.errorString());
QDir::toNativeSeparators(fileName), file.errorString());
if (errorString) if (errorString)
*errorString = errStr; *errorString = errStr;
else else
@@ -312,7 +310,7 @@ public:
void provideNewRange(quint64 offset) void provideNewRange(quint64 offset)
{ {
if (filePath().exists()) if (filePath().exists())
openImpl(nullptr, filePath().toString(), offset); openImpl(nullptr, filePath(), offset);
} }
public: public:
@@ -332,7 +330,7 @@ public:
emit aboutToReload(); emit aboutToReload();
int cPos = m_widget->cursorPosition(); int cPos = m_widget->cursorPosition();
m_widget->clear(); m_widget->clear();
const bool success = (openImpl(errorString, filePath().toString()) == OpenResult::Success); const bool success = (openImpl(errorString, filePath()) == OpenResult::Success);
m_widget->setCursorPosition(cPos); m_widget->setCursorPosition(cPos);
emit reloadFinished(success); emit reloadFinished(success);
return success; return success;

View File

@@ -69,7 +69,7 @@ void VirtualFileSystemOverlay::update()
.pathAppended(doc->filePath().fileName() + ".auto"); .pathAppended(doc->filePath().fileName() + ".auto");
while (saved.path.exists()) while (saved.path.exists())
saved.path = saved.path + ".1"; saved.path = saved.path + ".1";
if (!doc->save(&error, saved.path.toString(), true)) { if (!doc->save(&error, saved.path, true)) {
qCDebug(LOG) << error; qCDebug(LOG) << error;
continue; continue;
} }

View File

@@ -744,17 +744,19 @@ static bool saveModifiedFilesHelper(const QList<IDocument *> &documents,
return notSaved.isEmpty(); return notSaved.isEmpty();
} }
bool DocumentManager::saveDocument(IDocument *document, const QString &fileName, bool *isReadOnly) bool DocumentManager::saveDocument(IDocument *document,
const Utils::FilePath &filePath,
bool *isReadOnly)
{ {
bool ret = true; bool ret = true;
QString effName = fileName.isEmpty() ? document->filePath().toString() : fileName; const Utils::FilePath &savePath = filePath.isEmpty() ? document->filePath() : filePath;
expectFileChange(effName); // This only matters to other IDocuments which refer to this file expectFileChange(savePath.toString()); // This only matters to other IDocuments which refer to this file
bool addWatcher = removeDocument(document); // So that our own IDocument gets no notification at all bool addWatcher = removeDocument(document); // So that our own IDocument gets no notification at all
QString errorString; QString errorString;
if (!document->save(&errorString, fileName, false)) { if (!document->save(&errorString, filePath, false)) {
if (isReadOnly) { if (isReadOnly) {
QFile ofi(effName); QFile ofi(savePath.toString());
// Check whether the existing file is writable // Check whether the existing file is writable
if (!ofi.open(QIODevice::ReadWrite) && ofi.open(QIODevice::ReadOnly)) { if (!ofi.open(QIODevice::ReadWrite) && ofi.open(QIODevice::ReadOnly)) {
*isReadOnly = true; *isReadOnly = true;
@@ -769,7 +771,7 @@ bool DocumentManager::saveDocument(IDocument *document, const QString &fileName,
} }
addDocument(document, addWatcher); addDocument(document, addWatcher);
unexpectFileChange(effName); unexpectFileChange(savePath.toString());
m_instance->updateSaveAll(); m_instance->updateSaveAll();
return ret; return ret;
} }
@@ -1314,7 +1316,7 @@ void DocumentManager::checkForReload()
// handle deleted files // handle deleted files
EditorManager::closeDocuments(documentsToClose, false); EditorManager::closeDocuments(documentsToClose, false);
for (auto it = documentsToSave.cbegin(), end = documentsToSave.cend(); it != end; ++it) { for (auto it = documentsToSave.cbegin(), end = documentsToSave.cend(); it != end; ++it) {
saveDocument(it.key(), it.value()); saveDocument(it.key(), Utils::FilePath::fromString(it.value()));
it.key()->checkPermissions(); it.key()->checkPermissions();
} }

View File

@@ -27,6 +27,7 @@
#include <coreplugin/core_global.h> #include <coreplugin/core_global.h>
#include <utils/fileutils.h>
#include <utils/id.h> #include <utils/id.h>
#include <QObject> #include <QObject>
@@ -79,7 +80,7 @@ public:
static QString filePathKey(const QString &filePath, ResolveMode resolveMode); static QString filePathKey(const QString &filePath, ResolveMode resolveMode);
static bool saveDocument(IDocument *document, static bool saveDocument(IDocument *document,
const QString &fileName = QString(), const Utils::FilePath &filePath = Utils::FilePath(),
bool *isReadOnly = nullptr); bool *isReadOnly = nullptr);
static QString allDocumentFactoryFiltersString(QString *allFilesFilter); static QString allDocumentFactoryFiltersString(QString *allFilesFilter);

View File

@@ -861,6 +861,9 @@ IEditor *EditorManagerPrivate::openEditor(EditorView *view, const QString &fileN
IEditor *editor = nullptr; IEditor *editor = nullptr;
auto overrideCursor = Utils::OverrideCursor(QCursor(Qt::WaitCursor)); auto overrideCursor = Utils::OverrideCursor(QCursor(Qt::WaitCursor));
auto fp = Utils::FilePath::fromString(fn);
auto realFp = Utils::FilePath::fromString(realFn);
IEditorFactory *factory = factories.takeFirst(); IEditorFactory *factory = factories.takeFirst();
while (factory) { while (factory) {
editor = createEditor(factory, fn); editor = createEditor(factory, fn);
@@ -870,7 +873,7 @@ IEditor *EditorManagerPrivate::openEditor(EditorView *view, const QString &fileN
} }
QString errorString; QString errorString;
IDocument::OpenResult openResult = editor->document()->open(&errorString, fn, realFn); IDocument::OpenResult openResult = editor->document()->open(&errorString, fp, realFp);
if (openResult == IDocument::OpenResult::Success) if (openResult == IDocument::OpenResult::Success)
break; break;
@@ -933,7 +936,7 @@ IEditor *EditorManagerPrivate::openEditor(EditorView *view, const QString &fileN
return nullptr; return nullptr;
if (realFn != fn) if (realFn != fn)
editor->document()->setRestoredFrom(realFn); editor->document()->setRestoredFrom(realFp);
addEditor(editor); addEditor(editor);
if (newEditor) if (newEditor)
@@ -2351,7 +2354,7 @@ void EditorManagerPrivate::autoSave()
|| !QFileInfo(savePath).isWritable()) // FIXME: save them to a dedicated directory || !QFileInfo(savePath).isWritable()) // FIXME: save them to a dedicated directory
continue; continue;
QString errorString; QString errorString;
if (!document->autoSave(&errorString, saveName)) if (!document->autoSave(&errorString, Utils::FilePath::fromUserInput(saveName)))
errors << errorString; errors << errorString;
} }
if (!errors.isEmpty()) if (!errors.isEmpty())
@@ -2463,7 +2466,7 @@ bool EditorManagerPrivate::saveDocument(IDocument *document)
emit m_instance->aboutToSave(document); emit m_instance->aboutToSave(document);
// try saving, no matter what isReadOnly tells us // try saving, no matter what isReadOnly tells us
success = DocumentManager::saveDocument(document, QString(), &isReadOnly); success = DocumentManager::saveDocument(document, FilePath(), &isReadOnly);
if (!success && isReadOnly) { if (!success && isReadOnly) {
MakeWritableResult answer = makeFileWritable(document); MakeWritableResult answer = makeFileWritable(document);
@@ -2503,7 +2506,7 @@ bool EditorManagerPrivate::saveDocumentAs(IDocument *document)
} }
emit m_instance->aboutToSave(document); emit m_instance->aboutToSave(document);
const bool success = DocumentManager::saveDocument(document, absoluteFilePath.toString()); const bool success = DocumentManager::saveDocument(document, absoluteFilePath);
document->checkPermissions(); document->checkPermissions();
// TODO: There is an issue to be treated here. The new file might be of a different mime // TODO: There is an issue to be treated here. The new file might be of a different mime

View File

@@ -224,7 +224,7 @@ public:
Utils::FilePath filePath; Utils::FilePath filePath;
QString preferredDisplayName; QString preferredDisplayName;
QString uniqueDisplayName; QString uniqueDisplayName;
QString autoSaveName; Utils::FilePath autoSavePath;
Utils::InfoBar *infoBar = nullptr; Utils::InfoBar *infoBar = nullptr;
Id id; Id id;
optional<bool> fileIsReadOnly; optional<bool> fileIsReadOnly;
@@ -291,14 +291,14 @@ Id IDocument::id() const
The open() method is used to load the contents of a file when a document is The open() method is used to load the contents of a file when a document is
opened in an editor. opened in an editor.
If the document is opened from an auto save file, \a realFileName is the If the document is opened from an auto save file, \a realFilePath is the
name of the auto save file that should be loaded, and \a fileName is the name of the auto save file that should be loaded, and \a filePath is the
file name of the resulting file. In that case, the contents of the auto file name of the resulting file. In that case, the contents of the auto
save file should be loaded, the file name of the IDocument should be set to save file should be loaded, the file name of the IDocument should be set to
\a fileName, and the document state be set to modified. \a filePath, and the document state be set to modified.
If the editor is opened from a regular file, \a fileName and \a If the editor is opened from a regular file, \a filePath and \a
realFileName are the same. filePath are the same.
Use \a errorString to return an error message if this document cannot Use \a errorString to return an error message if this document cannot
handle the file contents. handle the file contents.
@@ -312,16 +312,16 @@ Id IDocument::id() const
\sa shouldAutoSave() \sa shouldAutoSave()
\sa setFilePath() \sa setFilePath()
*/ */
IDocument::OpenResult IDocument::open(QString *errorString, const QString &fileName, const QString &realFileName) IDocument::OpenResult IDocument::open(QString *errorString, const Utils::FilePath &filePath, const Utils::FilePath &realFilePath)
{ {
Q_UNUSED(errorString) Q_UNUSED(errorString)
Q_UNUSED(fileName) Q_UNUSED(filePath)
Q_UNUSED(realFileName) Q_UNUSED(realFilePath)
return OpenResult::CannotHandle; return OpenResult::CannotHandle;
} }
/*! /*!
Saves the contents of the document to the \a fileName on disk. Saves the contents of the document to the \a filePath on disk.
If \a autoSave is \c true, the saving is done for an auto-save, so the If \a autoSave is \c true, the saving is done for an auto-save, so the
document should avoid cleanups or other operations that it does for document should avoid cleanups or other operations that it does for
@@ -335,10 +335,10 @@ IDocument::OpenResult IDocument::open(QString *errorString, const QString &fileN
\sa shouldAutoSave() \sa shouldAutoSave()
*/ */
bool IDocument::save(QString *errorString, const QString &fileName, bool autoSave) bool IDocument::save(QString *errorString, const Utils::FilePath &filePath, bool autoSave)
{ {
Q_UNUSED(errorString) Q_UNUSED(errorString)
Q_UNUSED(fileName) Q_UNUSED(filePath)
Q_UNUSED(autoSave) Q_UNUSED(autoSave)
return false; return false;
} }
@@ -613,11 +613,11 @@ void IDocument::setMimeType(const QString &mimeType)
/*! /*!
\internal \internal
*/ */
bool IDocument::autoSave(QString *errorString, const QString &fileName) bool IDocument::autoSave(QString *errorString, const FilePath &filePath)
{ {
if (!save(errorString, fileName, true)) if (!save(errorString, filePath, true))
return false; return false;
d->autoSaveName = fileName; d->autoSavePath = filePath;
return true; return true;
} }
@@ -626,9 +626,9 @@ static const char kRestoredAutoSave[] = "RestoredAutoSave";
/*! /*!
\internal \internal
*/ */
void IDocument::setRestoredFrom(const QString &name) void IDocument::setRestoredFrom(const Utils::FilePath &path)
{ {
d->autoSaveName = name; d->autoSavePath = path;
d->restored = true; d->restored = true;
Utils::InfoBarEntry info(Id(kRestoredAutoSave), Utils::InfoBarEntry info(Id(kRestoredAutoSave),
tr("File was restored from auto-saved copy. " tr("File was restored from auto-saved copy. "
@@ -641,9 +641,9 @@ void IDocument::setRestoredFrom(const QString &name)
*/ */
void IDocument::removeAutoSaveFile() void IDocument::removeAutoSaveFile()
{ {
if (!d->autoSaveName.isEmpty()) { if (!d->autoSavePath.isEmpty()) {
QFile::remove(d->autoSaveName); QFile::remove(d->autoSavePath.toString());
d->autoSaveName.clear(); d->autoSavePath.clear();
if (d->restored) { if (d->restored) {
d->restored = false; d->restored = false;
infoBar()->removeInfo(Id(kRestoredAutoSave)); infoBar()->removeInfo(Id(kRestoredAutoSave));

View File

@@ -27,6 +27,7 @@
#include "core_global.h" #include "core_global.h"
#include <utils/fileutils.h>
#include <utils/id.h> #include <utils/id.h>
#include <QObject> #include <QObject>
@@ -87,9 +88,9 @@ public:
void setId(Utils::Id id); void setId(Utils::Id id);
Utils::Id id() const; Utils::Id id() const;
virtual OpenResult open(QString *errorString, const QString &fileName, const QString &realFileName); virtual OpenResult open(QString *errorString, const Utils::FilePath &filePath, const Utils::FilePath &realFilePath);
virtual bool save(QString *errorString, const QString &fileName = QString(), bool autoSave = false); virtual bool save(QString *errorString, const Utils::FilePath &filePath = Utils::FilePath(), bool autoSave = false);
virtual QByteArray contents() const; virtual QByteArray contents() const;
virtual bool setContents(const QByteArray &contents); virtual bool setContents(const QByteArray &contents);
@@ -124,8 +125,8 @@ public:
void checkPermissions(); void checkPermissions();
bool autoSave(QString *errorString, const QString &filePath); bool autoSave(QString *errorString, const Utils::FilePath &filePath);
void setRestoredFrom(const QString &name); void setRestoredFrom(const Utils::FilePath &path);
void removeAutoSaveFile(); void removeAutoSaveFile();
bool hasWriteWarning() const; bool hasWriteWarning() const;

View File

@@ -82,7 +82,7 @@ QByteArray BaseTextDocument::decodingErrorSample() const
} }
/*! /*!
Writes out the contents (\a data) of the text file \a fileName. Writes out the contents (\a data) of the text file \a filePath.
Uses the format obtained from the last read() of the file. Uses the format obtained from the last read() of the file.
If an error occurs while writing the file, \a errorMessage is set to the If an error occurs while writing the file, \a errorMessage is set to the
@@ -91,13 +91,15 @@ QByteArray BaseTextDocument::decodingErrorSample() const
Returns whether the operation was successful. Returns whether the operation was successful.
*/ */
bool BaseTextDocument::write(const QString &fileName, const QString &data, QString *errorMessage) const bool BaseTextDocument::write(const Utils::FilePath &filePath,
const QString &data,
QString *errorMessage) const
{ {
return write(fileName, format(), data, errorMessage); return write(filePath, format(), data, errorMessage);
} }
/*! /*!
Writes out the contents (\a data) of the text file \a fileName. Writes out the contents (\a data) of the text file \a filePath.
Uses the custom format \a format. Uses the custom format \a format.
If an error occurs while writing the file, \a errorMessage is set to the If an error occurs while writing the file, \a errorMessage is set to the
@@ -106,11 +108,14 @@ bool BaseTextDocument::write(const QString &fileName, const QString &data, QStri
Returns whether the operation was successful. Returns whether the operation was successful.
*/ */
bool BaseTextDocument::write(const QString &fileName, const Utils::TextFileFormat &format, const QString &data, QString *errorMessage) const bool BaseTextDocument::write(const Utils::FilePath &filePath,
const Utils::TextFileFormat &format,
const QString &data,
QString *errorMessage) const
{ {
if (debug) if (debug)
qDebug() << Q_FUNC_INFO << this << fileName; qDebug() << Q_FUNC_INFO << this << filePath;
return format.writeFile(Utils::FilePath::fromString(fileName), data, errorMessage); return format.writeFile(filePath, data, errorMessage);
} }
void BaseTextDocument::setSupportsUtf8Bom(bool value) void BaseTextDocument::setSupportsUtf8Bom(bool value)
@@ -124,7 +129,7 @@ void BaseTextDocument::setLineTerminationMode(Utils::TextFileFormat::LineTermina
} }
/*! /*!
Autodetects file format and reads the text file specified by \a fileName Autodetects file format and reads the text file specified by \a filePath
into a list of strings specified by \a plainTextList. into a list of strings specified by \a plainTextList.
If an error occurs while writing the file, \a errorString is set to the If an error occurs while writing the file, \a errorString is set to the
@@ -133,16 +138,21 @@ void BaseTextDocument::setLineTerminationMode(Utils::TextFileFormat::LineTermina
Returns whether the operation was successful. Returns whether the operation was successful.
*/ */
BaseTextDocument::ReadResult BaseTextDocument::read(const QString &fileName, QStringList *plainTextList, QString *errorString) BaseTextDocument::ReadResult BaseTextDocument::read(const Utils::FilePath &filePath,
QStringList *plainTextList,
QString *errorString)
{ {
d->m_readResult = d->m_readResult = Utils::TextFileFormat::readFile(filePath,
Utils::TextFileFormat::readFile(Utils::FilePath::fromString(fileName), codec(), codec(),
plainTextList, &d->m_format, errorString, &d->m_decodingErrorSample); plainTextList,
&d->m_format,
errorString,
&d->m_decodingErrorSample);
return d->m_readResult; return d->m_readResult;
} }
/*! /*!
Autodetects file format and reads the text file specified by \a fileName Autodetects file format and reads the text file specified by \a filePath
into \a plainText. into \a plainText.
If an error occurs while writing the file, \a errorString is set to the If an error occurs while writing the file, \a errorString is set to the
@@ -151,11 +161,16 @@ BaseTextDocument::ReadResult BaseTextDocument::read(const QString &fileName, QSt
Returns whether the operation was successful. Returns whether the operation was successful.
*/ */
BaseTextDocument::ReadResult BaseTextDocument::read(const QString &fileName, QString *plainText, QString *errorString) BaseTextDocument::ReadResult BaseTextDocument::read(const Utils::FilePath &filePath,
QString *plainText,
QString *errorString)
{ {
d->m_readResult = d->m_readResult = Utils::TextFileFormat::readFile(filePath,
Utils::TextFileFormat::readFile(Utils::FilePath::fromString(fileName), codec(), codec(),
plainText, &d->m_format, errorString, &d->m_decodingErrorSample); plainText,
&d->m_format,
errorString,
&d->m_decodingErrorSample);
return d->m_readResult; return d->m_readResult;
} }

View File

@@ -50,14 +50,14 @@ public:
bool supportsUtf8Bom() const; bool supportsUtf8Bom() const;
Utils::TextFileFormat::LineTerminationMode lineTerminationMode() const; Utils::TextFileFormat::LineTerminationMode lineTerminationMode() const;
ReadResult read(const QString &fileName, QStringList *plainTextList, QString *errorString); ReadResult read(const Utils::FilePath &filePath, QStringList *plainTextList, QString *errorString);
ReadResult read(const QString &fileName, QString *plainText, QString *errorString); ReadResult read(const Utils::FilePath &filePath, QString *plainText, QString *errorString);
bool hasDecodingError() const; bool hasDecodingError() const;
QByteArray decodingErrorSample() const; QByteArray decodingErrorSample() const;
bool write(const QString &fileName, const QString &data, QString *errorMessage) const; bool write(const Utils::FilePath &filePath, const QString &data, QString *errorMessage) const;
bool write(const QString &fileName, const Utils::TextFileFormat &format, const QString &data, QString *errorMessage) const; bool write(const Utils::FilePath &filePath, const Utils::TextFileFormat &format, const QString &data, QString *errorMessage) const;
void setSupportsUtf8Bom(bool value); void setSupportsUtf8Bom(bool value);
void setLineTerminationMode(Utils::TextFileFormat::LineTerminationMode mode); void setLineTerminationMode(Utils::TextFileFormat::LineTerminationMode mode);

View File

@@ -447,7 +447,7 @@ TextEditor::TabSettings CppEditorDocument::tabSettings() const
return indenter()->tabSettings().value_or(TextEditor::TextDocument::tabSettings()); return indenter()->tabSettings().value_or(TextEditor::TextDocument::tabSettings());
} }
bool CppEditorDocument::save(QString *errorString, const QString &fileName, bool autoSave) bool CppEditorDocument::save(QString *errorString, const Utils::FilePath &filePath, bool autoSave)
{ {
Utils::ExecuteOnDestruction resetSettingsOnScopeExit; Utils::ExecuteOnDestruction resetSettingsOnScopeExit;
@@ -490,7 +490,7 @@ bool CppEditorDocument::save(QString *errorString, const QString &fileName, bool
setStorageSettings(settings); setStorageSettings(settings);
} }
return TextEditor::TextDocument::save(errorString, fileName, autoSave); return TextEditor::TextDocument::save(errorString, filePath, autoSave);
} }
} // namespace Internal } // namespace Internal

View File

@@ -71,7 +71,7 @@ public:
TextEditor::TabSettings tabSettings() const override; TextEditor::TabSettings tabSettings() const override;
bool save(QString *errorString, bool save(QString *errorString,
const QString &fileName = QString(), const Utils::FilePath &filePath = Utils::FilePath(),
bool autoSave = false) override; bool autoSave = false) override;
signals: signals:

View File

@@ -106,7 +106,9 @@ public:
QScopedPointer<TextEditor::BaseTextEditor> editor( QScopedPointer<TextEditor::BaseTextEditor> editor(
TextEditor::PlainTextEditorFactory::createPlainTextEditor()); TextEditor::PlainTextEditorFactory::createPlainTextEditor());
QString error; QString error;
editor->document()->open(&error, document->fileName(), document->fileName()); editor->document()->open(&error,
Utils::FilePath::fromString(document->fileName()),
Utils::FilePath::fromString(document->fileName()));
QVERIFY(error.isEmpty()); QVERIFY(error.isEmpty());
// Set cursor position // Set cursor position

View File

@@ -65,51 +65,51 @@ FormWindowFile::FormWindowFile(QDesignerFormWindowInterface *form, QObject *pare
m_resourceHandler, &ResourceHandler::updateResources); m_resourceHandler, &ResourceHandler::updateResources);
} }
Core::IDocument::OpenResult FormWindowFile::open(QString *errorString, const QString &fileName, Core::IDocument::OpenResult FormWindowFile::open(QString *errorString,
const QString &realFileName) const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath)
{ {
if (Designer::Constants::Internal::debug) if (Designer::Constants::Internal::debug)
qDebug() << "FormWindowFile::open" << fileName; qDebug() << "FormWindowFile::open" << filePath.toUserOutput();
QDesignerFormWindowInterface *form = formWindow(); QDesignerFormWindowInterface *form = formWindow();
QTC_ASSERT(form, return OpenResult::CannotHandle); QTC_ASSERT(form, return OpenResult::CannotHandle);
if (fileName.isEmpty()) if (filePath.isEmpty())
return OpenResult::ReadError; return OpenResult::ReadError;
const QFileInfo fi(fileName);
const QString absfileName = fi.absoluteFilePath();
QString contents; QString contents;
Utils::TextFileFormat::ReadResult readResult = read(absfileName, &contents, errorString); Utils::TextFileFormat::ReadResult readResult = read(filePath.absoluteFilePath(),
&contents,
errorString);
if (readResult == Utils::TextFileFormat::ReadEncodingError) if (readResult == Utils::TextFileFormat::ReadEncodingError)
return OpenResult::CannotHandle; return OpenResult::CannotHandle;
if (readResult != Utils::TextFileFormat::ReadSuccess) if (readResult != Utils::TextFileFormat::ReadSuccess)
return OpenResult::ReadError; return OpenResult::ReadError;
form->setFileName(absfileName); form->setFileName(filePath.absoluteFilePath().toString());
const QByteArray contentsBA = contents.toUtf8(); const QByteArray contentsBA = contents.toUtf8();
QBuffer str; QBuffer str;
str.setData(contentsBA); str.setData(contentsBA);
str.open(QIODevice::ReadOnly); str.open(QIODevice::ReadOnly);
if (!form->setContents(&str, errorString)) if (!form->setContents(&str, errorString))
return OpenResult::CannotHandle; return OpenResult::CannotHandle;
form->setDirty(fileName != realFileName); form->setDirty(filePath != realFilePath);
syncXmlFromFormWindow(); syncXmlFromFormWindow();
setFilePath(Utils::FilePath::fromString(absfileName)); setFilePath(filePath.absoluteFilePath());
setShouldAutoSave(false); setShouldAutoSave(false);
resourceHandler()->updateProjectResources(); resourceHandler()->updateProjectResources();
return OpenResult::Success; return OpenResult::Success;
} }
bool FormWindowFile::save(QString *errorString, const QString &name, bool autoSave) bool FormWindowFile::save(QString *errorString, const FilePath &filePath, bool autoSave)
{ {
const FilePath actualName = name.isEmpty() ? filePath() : FilePath::fromString(name); const FilePath &actualName = filePath.isEmpty() ? this->filePath() : filePath;
if (Designer::Constants::Internal::debug) if (Designer::Constants::Internal::debug)
qDebug() << Q_FUNC_INFO << name << "->" << actualName; qDebug() << Q_FUNC_INFO << filePath << "->" << actualName;
QTC_ASSERT(m_formWindow, return false); QTC_ASSERT(m_formWindow, return false);
@@ -119,7 +119,7 @@ bool FormWindowFile::save(QString *errorString, const QString &name, bool autoSa
const QString oldFormName = m_formWindow->fileName(); const QString oldFormName = m_formWindow->fileName();
if (!autoSave) if (!autoSave)
m_formWindow->setFileName(actualName.toString()); m_formWindow->setFileName(actualName.toString());
const bool writeOK = writeFile(actualName.toString(), errorString); const bool writeOK = writeFile(actualName, errorString);
m_shouldAutoSave = false; m_shouldAutoSave = false;
if (autoSave) if (autoSave)
return writeOK; return writeOK;
@@ -228,7 +228,7 @@ bool FormWindowFile::reload(QString *errorString, ReloadFlag flag, ChangeType ty
} else { } else {
emit aboutToReload(); emit aboutToReload();
const bool success const bool success
= (open(errorString, filePath().toString(), filePath().toString()) == OpenResult::Success); = (open(errorString, filePath(), filePath()) == OpenResult::Success);
emit reloadFinished(success); emit reloadFinished(success);
return success; return success;
} }
@@ -247,11 +247,11 @@ QString FormWindowFile::fallbackSaveAsFileName() const
return m_suggestedName; return m_suggestedName;
} }
bool FormWindowFile::writeFile(const QString &fn, QString *errorString) const bool FormWindowFile::writeFile(const Utils::FilePath &filePath, QString *errorString) const
{ {
if (Designer::Constants::Internal::debug) if (Designer::Constants::Internal::debug)
qDebug() << Q_FUNC_INFO << filePath() << fn; qDebug() << Q_FUNC_INFO << this->filePath() << filePath;
return write(fn, format(), m_formWindow->contents(), errorString); return write(filePath, format(), m_formWindow->contents(), errorString);
} }
QDesignerFormWindowInterface *FormWindowFile::formWindow() const QDesignerFormWindowInterface *FormWindowFile::formWindow() const

View File

@@ -48,9 +48,9 @@ public:
~FormWindowFile() override { } ~FormWindowFile() override { }
// IDocument // IDocument
OpenResult open(QString *errorString, const QString &fileName, OpenResult open(QString *errorString, const Utils::FilePath &filePath,
const QString &realFileName) override; const Utils::FilePath &realFilePath) override;
bool save(QString *errorString, const QString &fileName, bool autoSave) override; bool save(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override;
QByteArray contents() const override; QByteArray contents() const override;
bool setContents(const QByteArray &contents) override; bool setContents(const QByteArray &contents) override;
bool shouldAutoSave() const override; bool shouldAutoSave() const override;
@@ -62,7 +62,7 @@ public:
// Internal // Internal
void setFallbackSaveAsFileName(const QString &fileName); void setFallbackSaveAsFileName(const QString &fileName);
bool writeFile(const QString &fileName, QString *errorString) const; bool writeFile(const Utils::FilePath &filePath, QString *errorString) const;
QDesignerFormWindowInterface *formWindow() const; QDesignerFormWindowInterface *formWindow() const;
void syncXmlFromFormWindow(); void syncXmlFromFormWindow();

View File

@@ -265,7 +265,7 @@ bool DiffEditorDocument::isSaveAsAllowed() const
return state() == LoadOK; return state() == LoadOK;
} }
bool DiffEditorDocument::save(QString *errorString, const QString &fileName, bool autoSave) bool DiffEditorDocument::save(QString *errorString, const Utils::FilePath &filePath, bool autoSave)
{ {
Q_UNUSED(errorString) Q_UNUSED(errorString)
Q_UNUSED(autoSave) Q_UNUSED(autoSave)
@@ -273,7 +273,7 @@ bool DiffEditorDocument::save(QString *errorString, const QString &fileName, boo
if (state() != LoadOK) if (state() != LoadOK)
return false; return false;
const bool ok = write(fileName, format(), plainText(), errorString); const bool ok = write(filePath, format(), plainText(), errorString);
if (!ok) if (!ok)
return false; return false;
@@ -282,9 +282,8 @@ bool DiffEditorDocument::save(QString *errorString, const QString &fileName, boo
setDescription(QString()); setDescription(QString());
Core::EditorManager::clearUniqueId(this); Core::EditorManager::clearUniqueId(this);
const QFileInfo fi(fileName);
setTemporary(false); setTemporary(false);
setFilePath(FilePath::fromString(fi.absoluteFilePath())); setFilePath(filePath.absoluteFilePath());
setPreferredDisplayName(QString()); setPreferredDisplayName(QString());
emit temporaryStateChanged(); emit temporaryStateChanged();
@@ -306,16 +305,16 @@ bool DiffEditorDocument::reload(QString *errorString, ReloadFlag flag, ChangeTyp
Q_UNUSED(type) Q_UNUSED(type)
if (flag == FlagIgnore) if (flag == FlagIgnore)
return true; return true;
return open(errorString, filePath().toString(), filePath().toString()) == OpenResult::Success; return open(errorString, filePath(), filePath()) == OpenResult::Success;
} }
Core::IDocument::OpenResult DiffEditorDocument::open(QString *errorString, const QString &fileName, Core::IDocument::OpenResult DiffEditorDocument::open(QString *errorString, const Utils::FilePath &filePath,
const QString &realFileName) const Utils::FilePath &realFilePath)
{ {
QTC_CHECK(fileName == realFileName); // does not support autosave QTC_CHECK(filePath == realFilePath); // does not support autosave
beginReload(); beginReload();
QString patch; QString patch;
ReadResult readResult = read(fileName, &patch, errorString); ReadResult readResult = read(filePath, &patch, errorString);
if (readResult == TextFileFormat::ReadIOError if (readResult == TextFileFormat::ReadIOError
|| readResult == TextFileFormat::ReadMemoryAllocationError) { || readResult == TextFileFormat::ReadMemoryAllocationError) {
return OpenResult::ReadError; return OpenResult::ReadError;
@@ -326,13 +325,12 @@ Core::IDocument::OpenResult DiffEditorDocument::open(QString *errorString, const
if (!ok) { if (!ok) {
*errorString = tr("Could not parse patch file \"%1\". " *errorString = tr("Could not parse patch file \"%1\". "
"The content is not of unified diff format.") "The content is not of unified diff format.")
.arg(fileName); .arg(filePath.toUserOutput());
} else { } else {
const QFileInfo fi(fileName);
setTemporary(false); setTemporary(false);
emit temporaryStateChanged(); emit temporaryStateChanged();
setFilePath(FilePath::fromString(fi.absoluteFilePath())); setFilePath(filePath.absoluteFilePath());
setDiffFiles(fileDataList, fi.absolutePath()); setDiffFiles(fileDataList, filePath.absoluteFilePath().toString());
} }
endReload(ok); endReload(ok);
if (!ok && readResult == TextFileFormat::ReadEncodingError) if (!ok && readResult == TextFileFormat::ReadEncodingError)

View File

@@ -81,11 +81,11 @@ public:
QString fallbackSaveAsFileName() const override; QString fallbackSaveAsFileName() const override;
bool isSaveAsAllowed() const override; bool isSaveAsAllowed() const override;
bool save(QString *errorString, const QString &fileName, bool autoSave) override; bool save(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override;
void reload(); void reload();
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override;
OpenResult open(QString *errorString, const QString &fileName, OpenResult open(QString *errorString, const Utils::FilePath &filePath,
const QString &realFileName) override; const Utils::FilePath &realFilePath) override;
bool selectEncoding(); bool selectEncoding();
State state() const { return m_state; } State state() const { return m_state; }

View File

@@ -202,7 +202,7 @@ void DiffEditorWidgetController::patch(bool revert, int fileIndex, int chunkInde
if (PatchTool::runPatch(EditorManager::defaultTextCodec()->fromUnicode(patch), if (PatchTool::runPatch(EditorManager::defaultTextCodec()->fromUnicode(patch),
contentsCopyDir, 0, revert)) { contentsCopyDir, 0, revert)) {
QString errorString; QString errorString;
if (textDocument->reload(&errorString, contentsCopyFileName)) if (textDocument->reload(&errorString, FilePath::fromString(contentsCopyFileName)))
m_document->reload(); m_document->reload();
} }
} }

View File

@@ -283,14 +283,14 @@ void GitEditorWidget::addDiffActions(QMenu *menu, const DiffChunk &chunk)
}); });
} }
void GitEditorWidget::aboutToOpen(const QString &fileName, const QString &realFileName) void GitEditorWidget::aboutToOpen(const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath)
{ {
Q_UNUSED(realFileName) Q_UNUSED(realFilePath)
Utils::Id editorId = textDocument()->id(); Utils::Id editorId = textDocument()->id();
if (editorId == Git::Constants::GIT_COMMIT_TEXT_EDITOR_ID if (editorId == Git::Constants::GIT_COMMIT_TEXT_EDITOR_ID
|| editorId == Git::Constants::GIT_REBASE_EDITOR_ID) { || editorId == Git::Constants::GIT_REBASE_EDITOR_ID) {
QFileInfo fi(fileName); const QString gitPath = filePath.absolutePath().toString();
const QString gitPath = fi.absolutePath();
setSource(gitPath); setSource(gitPath);
textDocument()->setCodec( textDocument()->setCodec(
GitClient::instance()->encoding(gitPath, "i18n.commitEncoding")); GitClient::instance()->encoding(gitPath, "i18n.commitEncoding"));

View File

@@ -59,7 +59,7 @@ private:
void init() override; void init() override;
void addDiffActions(QMenu *menu, const VcsBase::DiffChunk &chunk) override; void addDiffActions(QMenu *menu, const VcsBase::DiffChunk &chunk) override;
void aboutToOpen(const QString &fileName, const QString &realFileName) override; void aboutToOpen(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath) override;
QString changeUnderCursor(const QTextCursor &) const override; QString changeUnderCursor(const QTextCursor &) const override;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes) const override; VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes) const override;
QString decorateVersion(const QString &revision) const override; QString decorateVersion(const QString &revision) const override;

View File

@@ -80,22 +80,25 @@ ImageViewerFile::~ImageViewerFile()
cleanUp(); cleanUp();
} }
Core::IDocument::OpenResult ImageViewerFile::open(QString *errorString, const QString &fileName, Core::IDocument::OpenResult ImageViewerFile::open(QString *errorString,
const QString &realFileName) const Utils::FilePath &filePath,
const Utils::FilePath &realfilePath)
{ {
QTC_CHECK(fileName == realFileName); // does not support auto save QTC_CHECK(filePath == realfilePath); // does not support auto save
OpenResult success = openImpl(errorString, fileName); OpenResult success = openImpl(errorString, filePath);
emit openFinished(success == OpenResult::Success); emit openFinished(success == OpenResult::Success);
return success; return success;
} }
Core::IDocument::OpenResult ImageViewerFile::openImpl(QString *errorString, const QString &fileName) Core::IDocument::OpenResult ImageViewerFile::openImpl(QString *errorString,
const Utils::FilePath &filePath)
{ {
cleanUp(); cleanUp();
if (!QFileInfo(fileName).isReadable()) if (!filePath.isReadableFile())
return OpenResult::ReadError; return OpenResult::ReadError;
const QString &fileName = filePath.toString();
QByteArray format = QImageReader::imageFormat(fileName); QByteArray format = QImageReader::imageFormat(fileName);
// if it is impossible to recognize a file format - file will not be open correctly // if it is impossible to recognize a file format - file will not be open correctly
if (format.isEmpty()) { if (format.isEmpty()) {
@@ -141,7 +144,7 @@ Core::IDocument::OpenResult ImageViewerFile::openImpl(QString *errorString, cons
emit imageSizeChanged(m_pixmap->size()); emit imageSizeChanged(m_pixmap->size());
} }
setFilePath(Utils::FilePath::fromString(fileName)); setFilePath(filePath);
setMimeType(Utils::mimeTypeForFile(fileName).name()); setMimeType(Utils::mimeTypeForFile(fileName).name());
return OpenResult::Success; return OpenResult::Success;
} }
@@ -163,7 +166,7 @@ bool ImageViewerFile::reload(QString *errorString,
if (flag == FlagIgnore) if (flag == FlagIgnore)
return true; return true;
emit aboutToReload(); emit aboutToReload();
bool success = (openImpl(errorString, filePath().toString()) == OpenResult::Success); bool success = (openImpl(errorString, filePath()) == OpenResult::Success);
emit reloadFinished(success); emit reloadFinished(success);
return success; return success;
} }

View File

@@ -57,8 +57,8 @@ public:
ImageViewerFile(); ImageViewerFile();
~ImageViewerFile() override; ~ImageViewerFile() override;
OpenResult open(QString *errorString, const QString &fileName, OpenResult open(QString *errorString, const Utils::FilePath &filePath,
const QString &realFileName) override; const Utils::FilePath &realFilePath) override;
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override; ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override;
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override;
@@ -78,7 +78,7 @@ signals:
private: private:
void cleanUp(); void cleanUp();
OpenResult openImpl(QString *errorString, const QString &fileName); OpenResult openImpl(QString *errorString, const Utils::FilePath &filePath);
ImageType m_type = TypeInvalid; ImageType m_type = TypeInvalid;
#ifndef QT_NO_SVG #ifndef QT_NO_SVG

View File

@@ -66,26 +66,25 @@ ModelDocument::~ModelDocument()
delete d; delete d;
} }
Core::IDocument::OpenResult ModelDocument::open(QString *errorString, const QString &fileName, Core::IDocument::OpenResult ModelDocument::open(QString *errorString,
const QString &realFileName) const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath)
{ {
Q_UNUSED(fileName) Q_UNUSED(filePath)
OpenResult result = load(errorString, realFileName); OpenResult result = load(errorString, realFilePath.toString());
return result; return result;
} }
bool ModelDocument::save(QString *errorString, const QString &name, bool autoSave) bool ModelDocument::save(QString *errorString, const Utils::FilePath &filePath, bool autoSave)
{ {
if (!d->documentController) { if (!d->documentController) {
*errorString = tr("No model loaded. Cannot save."); *errorString = tr("No model loaded. Cannot save.");
return false; return false;
} }
QString actualName = filePath().toString(); const Utils::FilePath actualName = filePath.isEmpty() ? this->filePath() : filePath;
if (!name.isEmpty()) d->documentController->projectController()->setFileName(actualName.toString());
actualName = name;
d->documentController->projectController()->setFileName(actualName);
try { try {
d->documentController->projectController()->save(); d->documentController->projectController()->save();
} catch (const qmt::Exception &ex) { } catch (const qmt::Exception &ex) {

View File

@@ -48,9 +48,10 @@ signals:
void contentSet(); void contentSet();
public: public:
OpenResult open(QString *errorString, const QString &fileName, OpenResult open(QString *errorString,
const QString &realFileName) override; const Utils::FilePath &filePath,
bool save(QString *errorString, const QString &fileName, bool autoSave) override; const Utils::FilePath &realFilePath) override;
bool save(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override;
bool shouldAutoSave() const override; bool shouldAutoSave() const override;
bool isModified() const override; bool isModified() const override;
bool isSaveAsAllowed() const override; bool isSaveAsAllowed() const override;

View File

@@ -120,15 +120,15 @@ ResourceEditorW::~ResourceEditorW()
} }
Core::IDocument::OpenResult ResourceEditorDocument::open(QString *errorString, Core::IDocument::OpenResult ResourceEditorDocument::open(QString *errorString,
const QString &fileName, const Utils::FilePath &filePath,
const QString &realFileName) const Utils::FilePath &realFilePath)
{ {
if (debugResourceEditorW) if (debugResourceEditorW)
qDebug() << "ResourceEditorW::open: " << fileName; qDebug() << "ResourceEditorW::open: " << filePath;
setBlockDirtyChanged(true); setBlockDirtyChanged(true);
m_model->setFileName(realFileName); m_model->setFileName(realFilePath.toString());
OpenResult openResult = m_model->reload(); OpenResult openResult = m_model->reload();
if (openResult != OpenResult::Success) { if (openResult != OpenResult::Success) {
@@ -138,22 +138,21 @@ Core::IDocument::OpenResult ResourceEditorDocument::open(QString *errorString,
return openResult; return openResult;
} }
setFilePath(FilePath::fromString(fileName)); setFilePath(filePath);
setBlockDirtyChanged(false); setBlockDirtyChanged(false);
m_model->setDirty(fileName != realFileName); m_model->setDirty(filePath != realFilePath);
m_shouldAutoSave = false; m_shouldAutoSave = false;
emit loaded(true); emit loaded(true);
return OpenResult::Success; return OpenResult::Success;
} }
bool ResourceEditorDocument::save(QString *errorString, const QString &name, bool autoSave) bool ResourceEditorDocument::save(QString *errorString, const FilePath &filePath, bool autoSave)
{ {
if (debugResourceEditorW) if (debugResourceEditorW)
qDebug(">ResourceEditorW::save: %s", qPrintable(name)); qDebug() << ">ResourceEditorW::save: " << filePath;
const FilePath oldFileName = filePath(); const FilePath &actualName = filePath.isEmpty() ? this->filePath() : filePath;
const FilePath actualName = name.isEmpty() ? oldFileName : FilePath::fromString(name);
if (actualName.isEmpty()) if (actualName.isEmpty())
return false; return false;
@@ -161,14 +160,14 @@ bool ResourceEditorDocument::save(QString *errorString, const QString &name, boo
m_model->setFileName(actualName.toString()); m_model->setFileName(actualName.toString());
if (!m_model->save()) { if (!m_model->save()) {
*errorString = m_model->errorMessage(); *errorString = m_model->errorMessage();
m_model->setFileName(oldFileName.toString()); m_model->setFileName(this->filePath().toString());
m_blockDirtyChanged = false; m_blockDirtyChanged = false;
return false; return false;
} }
m_shouldAutoSave = false; m_shouldAutoSave = false;
if (autoSave) { if (autoSave) {
m_model->setFileName(oldFileName.toString()); m_model->setFileName(this->filePath().toString());
m_model->setDirty(true); m_model->setDirty(true);
m_blockDirtyChanged = false; m_blockDirtyChanged = false;
return true; return true;
@@ -272,8 +271,7 @@ bool ResourceEditorDocument::reload(QString *errorString, ReloadFlag flag, Chang
if (flag == FlagIgnore) if (flag == FlagIgnore)
return true; return true;
emit aboutToReload(); emit aboutToReload();
QString fn = filePath().toString(); const bool success = (open(errorString, filePath(), filePath()) == OpenResult::Success);
const bool success = (open(errorString, fn, fn) == OpenResult::Success);
emit reloadFinished(success); emit reloadFinished(success);
return success; return success;
} }

View File

@@ -51,9 +51,9 @@ public:
ResourceEditorDocument(QObject *parent = nullptr); ResourceEditorDocument(QObject *parent = nullptr);
//IDocument //IDocument
OpenResult open(QString *errorString, const QString &fileName, OpenResult open(QString *errorString, const Utils::FilePath &filePath,
const QString &realFileName) override; const Utils::FilePath &realFilePath) override;
bool save(QString *errorString, const QString &fileName, bool autoSave) override; bool save(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override;
QString plainText() const; QString plainText() const;
QByteArray contents() const override; QByteArray contents() const override;
bool setContents(const QByteArray &contents) override; bool setContents(const QByteArray &contents) override;

View File

@@ -57,32 +57,33 @@ ScxmlEditorDocument::ScxmlEditorDocument(MainWidget *designWidget, QObject *pare
}); });
} }
Core::IDocument::OpenResult ScxmlEditorDocument::open(QString *errorString, const QString &fileName, const QString &realFileName) Core::IDocument::OpenResult ScxmlEditorDocument::open(QString *errorString,
const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath)
{ {
Q_UNUSED(realFileName) Q_UNUSED(realFilePath)
if (fileName.isEmpty()) if (filePath.isEmpty())
return OpenResult::ReadError; return OpenResult::ReadError;
if (!m_designWidget) if (!m_designWidget)
return OpenResult::ReadError; return OpenResult::ReadError;
const QFileInfo fi(fileName); const FilePath &absoluteFilePath = filePath.absoluteFilePath();
const QString absfileName = fi.absoluteFilePath(); if (!m_designWidget->load(absoluteFilePath.toString())) {
if (!m_designWidget->load(absfileName)) {
*errorString = m_designWidget->errorMessage(); *errorString = m_designWidget->errorMessage();
return OpenResult::ReadError; return OpenResult::ReadError;
} }
setFilePath(Utils::FilePath::fromString(absfileName)); setFilePath(absoluteFilePath);
return OpenResult::Success; return OpenResult::Success;
} }
bool ScxmlEditorDocument::save(QString *errorString, const QString &name, bool autoSave) bool ScxmlEditorDocument::save(QString *errorString, const FilePath &filePath, bool autoSave)
{ {
const FilePath oldFileName = filePath(); const FilePath oldFileName = this->filePath();
const FilePath actualName = name.isEmpty() ? oldFileName : FilePath::fromString(name); const FilePath actualName = filePath.isEmpty() ? oldFileName : filePath;
if (actualName.isEmpty()) if (actualName.isEmpty())
return false; return false;
bool dirty = m_designWidget->isDirty(); bool dirty = m_designWidget->isDirty();

View File

@@ -49,8 +49,10 @@ public:
explicit ScxmlEditorDocument(Common::MainWidget *designWidget, QObject *parent = nullptr); explicit ScxmlEditorDocument(Common::MainWidget *designWidget, QObject *parent = nullptr);
// IDocument // IDocument
OpenResult open(QString *errorString, const QString &fileName, const QString &realFileName) override; OpenResult open(QString *errorString,
bool save(QString *errorString, const QString &fileName, bool autoSave) override; const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath) override;
bool save(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override;
bool shouldAutoSave() const override; bool shouldAutoSave() const override;
bool isSaveAsAllowed() const override; bool isSaveAsAllowed() const override;
bool isModified() const override; bool isModified() const override;

View File

@@ -600,7 +600,7 @@ SyntaxHighlighter *TextDocument::syntaxHighlighter() const
* If \a autoSave is true, the cursor will be restored and some signals suppressed * If \a autoSave is true, the cursor will be restored and some signals suppressed
* and we do not clean up the text file (cleanWhitespace(), ensureFinalNewLine()). * and we do not clean up the text file (cleanWhitespace(), ensureFinalNewLine()).
*/ */
bool TextDocument::save(QString *errorString, const QString &saveFileName, bool autoSave) bool TextDocument::save(QString *errorString, const FilePath &filePath, bool autoSave)
{ {
QTextCursor cursor(&d->m_document); QTextCursor cursor(&d->m_document);
@@ -638,11 +638,9 @@ bool TextDocument::save(QString *errorString, const QString &saveFileName, bool
if (d->m_storageSettings.m_addFinalNewLine) if (d->m_storageSettings.m_addFinalNewLine)
ensureFinalNewLine(cursor); ensureFinalNewLine(cursor);
cursor.endEditBlock(); cursor.endEditBlock();
} }
QString fName = filePath().toString(); const Utils::FilePath &savePath = filePath.isEmpty() ? this->filePath() : filePath;
if (!saveFileName.isEmpty())
fName = saveFileName;
// check if UTF8-BOM has to be added or removed // check if UTF8-BOM has to be added or removed
Utils::TextFileFormat saveFormat = format(); Utils::TextFileFormat saveFormat = format();
@@ -659,7 +657,7 @@ bool TextDocument::save(QString *errorString, const QString &saveFileName, bool
} }
} }
const bool ok = write(fName, saveFormat, d->m_document.toPlainText(), errorString); const bool ok = write(savePath, saveFormat, d->m_document.toPlainText(), errorString);
// restore text cursor and scroll bar positions // restore text cursor and scroll bar positions
if (autoSave && undos < d->m_document.availableUndoSteps()) { if (autoSave && undos < d->m_document.availableUndoSteps()) {
@@ -681,9 +679,8 @@ bool TextDocument::save(QString *errorString, const QString &saveFileName, bool
return true; return true;
// inform about the new filename // inform about the new filename
const QFileInfo fi(fName);
d->m_document.setModified(false); // also triggers update of the block revisions d->m_document.setModified(false); // also triggers update of the block revisions
setFilePath(Utils::FilePath::fromUserInput(fi.absoluteFilePath())); setFilePath(savePath.absoluteFilePath());
emit changed(); emit changed();
return true; return true;
} }
@@ -715,33 +712,35 @@ bool TextDocument::isModified() const
return d->m_document.isModified(); return d->m_document.isModified();
} }
Core::IDocument::OpenResult TextDocument::open(QString *errorString, const QString &fileName, Core::IDocument::OpenResult TextDocument::open(QString *errorString,
const QString &realFileName) const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath)
{ {
emit aboutToOpen(fileName, realFileName); emit aboutToOpen(filePath, realFilePath);
OpenResult success = openImpl(errorString, fileName, realFileName, /*reload =*/ false); OpenResult success = openImpl(errorString, filePath, realFilePath, /*reload =*/ false);
if (success == OpenResult::Success) { if (success == OpenResult::Success) {
setMimeType(Utils::mimeTypeForFile(fileName).name()); setMimeType(Utils::mimeTypeForFile(filePath.toString()).name());
emit openFinishedSuccessfully(); emit openFinishedSuccessfully();
} }
return success; return success;
} }
Core::IDocument::OpenResult TextDocument::openImpl(QString *errorString, const QString &fileName, Core::IDocument::OpenResult TextDocument::openImpl(QString *errorString,
const QString &realFileName, bool reload) const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath,
bool reload)
{ {
QStringList content; QStringList content;
ReadResult readResult = Utils::TextFileFormat::ReadIOError; ReadResult readResult = Utils::TextFileFormat::ReadIOError;
if (!fileName.isEmpty()) { if (!filePath.isEmpty()) {
const QFileInfo fi(fileName); readResult = read(realFilePath, &content, errorString);
readResult = read(realFileName, &content, errorString);
const int chunks = content.size(); const int chunks = content.size();
// Don't call setUndoRedoEnabled(true) when reload is true and filenames are different, // Don't call setUndoRedoEnabled(true) when reload is true and filenames are different,
// since it will reset the undo's clear index // since it will reset the undo's clear index
if (!reload || fileName == realFileName) if (!reload || filePath == realFilePath)
d->m_document.setUndoRedoEnabled(reload); d->m_document.setUndoRedoEnabled(reload);
QTextCursor c(&d->m_document); QTextCursor c(&d->m_document);
@@ -775,7 +774,7 @@ Core::IDocument::OpenResult TextDocument::openImpl(QString *errorString, const Q
// Don't call setUndoRedoEnabled(true) when reload is true and filenames are different, // Don't call setUndoRedoEnabled(true) when reload is true and filenames are different,
// since it will reset the undo's clear index // since it will reset the undo's clear index
if (!reload || fileName == realFileName) if (!reload || filePath == realFilePath)
d->m_document.setUndoRedoEnabled(true); d->m_document.setUndoRedoEnabled(true);
auto documentLayout = auto documentLayout =
@@ -783,8 +782,8 @@ Core::IDocument::OpenResult TextDocument::openImpl(QString *errorString, const Q
QTC_ASSERT(documentLayout, return OpenResult::CannotHandle); QTC_ASSERT(documentLayout, return OpenResult::CannotHandle);
documentLayout->lastSaveRevision = d->m_autoSaveRevision = d->m_document.revision(); documentLayout->lastSaveRevision = d->m_autoSaveRevision = d->m_document.revision();
d->updateRevisions(); d->updateRevisions();
d->m_document.setModified(fileName != realFileName); d->m_document.setModified(filePath != realFilePath);
setFilePath(Utils::FilePath::fromUserInput(fi.absoluteFilePath())); setFilePath(filePath);
} }
if (readResult == Utils::TextFileFormat::ReadIOError) if (readResult == Utils::TextFileFormat::ReadIOError)
return OpenResult::ReadError; return OpenResult::ReadError;
@@ -800,10 +799,10 @@ bool TextDocument::reload(QString *errorString, QTextCodec *codec)
bool TextDocument::reload(QString *errorString) bool TextDocument::reload(QString *errorString)
{ {
return reload(errorString, filePath().toString()); return reload(errorString, filePath());
} }
bool TextDocument::reload(QString *errorString, const QString &realFileName) bool TextDocument::reload(QString *errorString, const FilePath &realFilePath)
{ {
emit aboutToReload(); emit aboutToReload();
auto documentLayout = auto documentLayout =
@@ -812,8 +811,8 @@ bool TextDocument::reload(QString *errorString, const QString &realFileName)
if (documentLayout) if (documentLayout)
marks = documentLayout->documentClosing(); // removes text marks non-permanently marks = documentLayout->documentClosing(); // removes text marks non-permanently
const QString &file = filePath().toString(); bool success = openImpl(errorString, filePath(), realFilePath, /*reload =*/true)
bool success = openImpl(errorString, file, realFileName, /*reload =*/ true) == OpenResult::Success; == OpenResult::Success;
if (documentLayout) if (documentLayout)
documentLayout->documentReloaded(marks, this); // re-adds text marks documentLayout->documentReloaded(marks, this); // re-adds text marks

View File

@@ -112,7 +112,7 @@ public:
void removeMarkFromMarksCache(TextMark *mark); void removeMarkFromMarksCache(TextMark *mark);
// IDocument implementation. // IDocument implementation.
bool save(QString *errorString, const QString &fileName, bool autoSave) override; bool save(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override;
QByteArray contents() const override; QByteArray contents() const override;
bool setContents(const QByteArray &contents) override; bool setContents(const QByteArray &contents) override;
bool shouldAutoSave() const override; bool shouldAutoSave() const override;
@@ -127,10 +127,10 @@ public:
void setFallbackSaveAsPath(const QString &fallbackSaveAsPath); void setFallbackSaveAsPath(const QString &fallbackSaveAsPath);
void setFallbackSaveAsFileName(const QString &fallbackSaveAsFileName); void setFallbackSaveAsFileName(const QString &fallbackSaveAsFileName);
OpenResult open(QString *errorString, const QString &fileName, OpenResult open(QString *errorString, const Utils::FilePath &filePath,
const QString &realFileName) override; const Utils::FilePath &realFilePath) override;
virtual bool reload(QString *errorString); virtual bool reload(QString *errorString);
bool reload(QString *errorString, const QString &realFileName); bool reload(QString *errorString, const Utils::FilePath &realFilePath);
bool setPlainText(const QString &text); bool setPlainText(const QString &text);
QTextDocument *document() const; QTextDocument *document() const;
@@ -156,7 +156,7 @@ public:
const std::function<Utils::FilePath()> &filePath); const std::function<Utils::FilePath()> &filePath);
signals: signals:
void aboutToOpen(const QString &fileName, const QString &realFileName); void aboutToOpen(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath);
void openFinishedSuccessfully(); void openFinishedSuccessfully();
void contentsChangedWithPosition(int position, int charsRemoved, int charsAdded); void contentsChangedWithPosition(int position, int charsRemoved, int charsAdded);
void tabSettingsChanged(); void tabSettingsChanged();
@@ -167,7 +167,9 @@ protected:
virtual void applyFontSettings(); virtual void applyFontSettings();
private: private:
OpenResult openImpl(QString *errorString, const QString &fileName, const QString &realFileName, OpenResult openImpl(QString *errorString,
const Utils::FilePath &filePath,
const Utils::FilePath &realFileName,
bool reload); bool reload);
void cleanWhitespace(QTextCursor &cursor, bool inEntireDocument, bool cleanIndentation); void cleanWhitespace(QTextCursor &cursor, bool inEntireDocument, bool cleanIndentation);
void ensureFinalNewLine(QTextCursor &cursor); void ensureFinalNewLine(QTextCursor &cursor);

View File

@@ -1464,10 +1464,10 @@ TextDocument *TextEditorWidget::textDocument() const
return d->m_document.data(); return d->m_document.data();
} }
void TextEditorWidget::aboutToOpen(const QString &fileName, const QString &realFileName) void TextEditorWidget::aboutToOpen(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath)
{ {
Q_UNUSED(fileName) Q_UNUSED(filePath)
Q_UNUSED(realFileName) Q_UNUSED(realFilePath)
} }
void TextEditorWidget::openFinishedSuccessfully() void TextEditorWidget::openFinishedSuccessfully()

View File

@@ -186,7 +186,7 @@ public:
TextDocument *textDocument() const; TextDocument *textDocument() const;
QSharedPointer<TextDocument> textDocumentPtr() const; QSharedPointer<TextDocument> textDocumentPtr() const;
virtual void aboutToOpen(const QString &fileName, const QString &realFileName); virtual void aboutToOpen(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath);
virtual void openFinishedSuccessfully(); virtual void openFinishedSuccessfully();
// IEditor // IEditor
QByteArray saveState() const; QByteArray saveState() const;

View File

@@ -51,22 +51,23 @@ SubmitEditorFile::SubmitEditorFile(VcsBaseSubmitEditor *editor) :
this, &Core::IDocument::contentsChanged); this, &Core::IDocument::contentsChanged);
} }
Core::IDocument::OpenResult SubmitEditorFile::open(QString *errorString, const QString &fileName, Core::IDocument::OpenResult SubmitEditorFile::open(QString *errorString,
const QString &realFileName) const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath)
{ {
if (fileName.isEmpty()) if (filePath.isEmpty())
return OpenResult::ReadError; return OpenResult::ReadError;
FileReader reader; FileReader reader;
if (!reader.fetch(Utils::FilePath::fromString(realFileName), QIODevice::Text, errorString)) if (!reader.fetch(realFilePath, QIODevice::Text, errorString))
return OpenResult::ReadError; return OpenResult::ReadError;
const QString text = QString::fromLocal8Bit(reader.data()); const QString text = QString::fromLocal8Bit(reader.data());
if (!m_editor->setFileContents(text.toUtf8())) if (!m_editor->setFileContents(text.toUtf8()))
return OpenResult::CannotHandle; return OpenResult::CannotHandle;
setFilePath(FilePath::fromString(fileName)); setFilePath(filePath.absoluteFilePath());
setModified(fileName != realFileName); setModified(filePath != realFilePath);
return OpenResult::Success; return OpenResult::Success;
} }
@@ -88,16 +89,16 @@ void SubmitEditorFile::setModified(bool modified)
emit changed(); emit changed();
} }
bool SubmitEditorFile::save(QString *errorString, const QString &fileName, bool autoSave) bool SubmitEditorFile::save(QString *errorString, const FilePath &filePath, bool autoSave)
{ {
const FilePath fName = fileName.isEmpty() ? filePath() : FilePath::fromString(fileName); const FilePath &fName = filePath.isEmpty() ? this->filePath() : filePath;
FileSaver saver(fName, QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text); FileSaver saver(fName, QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
saver.write(m_editor->fileContents()); saver.write(m_editor->fileContents());
if (!saver.finalize(errorString)) if (!saver.finalize(errorString))
return false; return false;
if (autoSave) if (autoSave)
return true; return true;
setFilePath(FilePath::fromUserInput(fName.toFileInfo().absoluteFilePath())); setFilePath(fName.absoluteFilePath());
setModified(false); setModified(false);
if (!errorString->isEmpty()) if (!errorString->isEmpty())
return false; return false;

View File

@@ -39,13 +39,13 @@ class SubmitEditorFile : public Core::IDocument
public: public:
explicit SubmitEditorFile(VcsBaseSubmitEditor *editor); explicit SubmitEditorFile(VcsBaseSubmitEditor *editor);
OpenResult open(QString *errorString, const QString &fileName, OpenResult open(QString *errorString, const Utils::FilePath &filePath,
const QString &realFileName) override; const Utils::FilePath &realFilePath) override;
QByteArray contents() const override; QByteArray contents() const override;
bool setContents(const QByteArray &contents) override; bool setContents(const QByteArray &contents) override;
bool isModified() const override { return m_modified; } bool isModified() const override { return m_modified; }
bool save(QString *errorString, const QString &fileName, bool autoSave) override; bool save(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override;
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override; ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override;
void setModified(bool modified = true); void setModified(bool modified = true);