Commit f1f9904d authored by Oswald Buddenhagen's avatar Oswald Buddenhagen
Browse files

add errorString output argument to IFile::save()

this centralizes error reporting (in fact, in most cases it adds any in
the first place).

Task-number: QTCREATORBUG-1619
parent 469199e7
......@@ -35,6 +35,7 @@
#include <texteditor/fontsettings.h>
#include <texteditor/texteditorconstants.h>
#include <coreplugin/editormanager/ieditor.h>
#include <utils/fileutils.h>
#include <QtCore/QByteArrayMatcher>
#include <QtCore/QDebug>
......@@ -337,7 +338,7 @@ bool BinEditor::isReadOnly() const
return m_readOnly;
}
bool BinEditor::save(const QString &oldFileName, const QString &newFileName)
bool BinEditor::save(QString *errorString, const QString &oldFileName, const QString &newFileName)
{
if (oldFileName != newFileName) {
QString tmpName;
......@@ -354,21 +355,26 @@ bool BinEditor::save(const QString &oldFileName, const QString &newFileName)
if (!QFile::rename(tmpName, newFileName))
return false;
}
QFile output(newFileName);
if (!output.open(QIODevice::ReadWrite)) // QtBug: WriteOnly truncates.
return false;
const qint64 size = output.size();
for (BlockMap::const_iterator it = m_modifiedData.constBegin();
it != m_modifiedData.constEnd(); ++it) {
if (!output.seek(it.key() * m_blockSize))
return false;
if (output.write(it.value()) < m_blockSize)
return false;
}
Utils::FileSaver saver(newFileName, QIODevice::ReadWrite); // QtBug: WriteOnly truncates.
if (!saver.hasError()) {
QFile *output = saver.file();
const qint64 size = output->size();
for (BlockMap::const_iterator it = m_modifiedData.constBegin();
it != m_modifiedData.constEnd(); ++it) {
if (!saver.setResult(output->seek(it.key() * m_blockSize)))
break;
if (!saver.write(it.value()))
break;
if (!saver.setResult(output->flush()))
break;
}
// We may have padded the displayed data, so we have to make sure
// changes to that area are not actually written back to disk.
if (!output.resize(size))
// We may have padded the displayed data, so we have to make sure
// changes to that area are not actually written back to disk.
if (!saver.hasError())
saver.setResult(output->resize(size));
}
if (!saver.finalize(errorString))
return false;
setModified(false);
......
......@@ -72,7 +72,7 @@ public:
Q_INVOKABLE void addData(quint64 block, const QByteArray &data);
Q_INVOKABLE void setNewWindowRequestAllowed();
Q_INVOKABLE void updateContents();
bool save(const QString &oldFileName, const QString &newFileName);
bool save(QString *errorString, const QString &oldFileName, const QString &newFileName);
void zoomIn(int range = 1);
void zoomOut(int range = 1);
......
......@@ -199,10 +199,11 @@ public:
return QLatin1String(Constants::C_BINEDITOR_MIMETYPE);
}
bool save(const QString &fileName = QString()) {
bool save(QString *errorString, const QString &fileName = QString())
{
const QString fileNameToUse
= fileName.isEmpty() ? m_fileName : fileName;
if (m_editor->save(m_fileName, fileNameToUse)) {
if (m_editor->save(errorString, m_fileName, fileNameToUse)) {
m_fileName = fileNameToUse;
m_editor->editor()->setDisplayName(QFileInfo(fileNameToUse).fileName());
emit changed();
......
......@@ -735,10 +735,11 @@ CMakeFile::CMakeFile(CMakeProject *parent, QString fileName)
}
bool CMakeFile::save(const QString &fileName)
bool CMakeFile::save(QString *errorString, const QString &fileName)
{
// Once we have an texteditor open for this file, we probably do
// need to implement this, don't we.
Q_UNUSED(errorString)
Q_UNUSED(fileName)
return false;
}
......
......@@ -199,7 +199,7 @@ class CMakeFile : public Core::IFile
public:
CMakeFile(CMakeProject *parent, QString fileName);
bool save(const QString &fileName = QString());
bool save(QString *errorString, const QString &fileName = QString());
QString fileName() const;
QString defaultPath() const;
......
......@@ -1373,11 +1373,12 @@ bool EditorManager::saveFile(IFile *fileParam)
return saveFileAs(file);
bool success = false;
bool isReadOnly;
// try saving, no matter what isReadOnly tells us
success = m_d->m_core->fileManager()->saveFile(file);
success = m_d->m_core->fileManager()->saveFile(file, QString(), &isReadOnly);
if (!success) {
if (!success && isReadOnly) {
MakeWritableResult answer =
makeFileWritable(file);
if (answer == Failed)
......
......@@ -638,9 +638,24 @@ QList<IFile *> FileManager::saveModifiedFiles(const QList<IFile *> &files,
return notSaved;
}
bool FileManager::saveFile(IFile *file, const QString &fileName)
bool FileManager::saveFile(IFile *file, const QString &fileName, bool *isReadOnly)
{
const bool success = file->save(fileName);
QString effName = fileName.isEmpty() ? file->fileName() : fileName;
QString errorString;
if (!file->save(&errorString, fileName)) {
if (isReadOnly) {
QFile ofi(effName);
// Check whether the existing file is writable
if (ofi.exists() && !ofi.open(QIODevice::ReadWrite)
&& ofi.error() == QFile::PermissionsError) {
*isReadOnly = true;
return false;
}
*isReadOnly = false;
}
QMessageBox::critical(d->m_mainWindow, tr("File Error"), errorString);
return false;
}
// We are updating the lastUpdated time to the current modification time
// in changedFile we'll compare the modification time with the last updated
......@@ -655,7 +670,7 @@ bool FileManager::saveFile(IFile *file, const QString &fileName)
foreach (const QString &fileName, d->m_filesWithWatch.value(file))
updateExpectedState(fileName);
return success;
return true;
}
QString FileManager::getSaveFileName(const QString &title, const QString &pathIn,
......
......@@ -94,7 +94,7 @@ public:
// helper methods
static QString fixFileName(const QString &fileName, FixMode fixmode);
bool saveFile(IFile *file, const QString &fileName = QString());
bool saveFile(IFile *file, const QString &fileName = QString(), bool *isReadOnly = 0);
QStringList getOpenFileNames(const QString &filters,
const QString path = QString(),
......
......@@ -83,7 +83,7 @@ public:
IFile(QObject *parent = 0) : QObject(parent) {}
virtual ~IFile() {}
virtual bool save(const QString &fileName = QString()) = 0;
virtual bool save(QString *errorString, const QString &fileName = QString()) = 0;
virtual QString fileName() const = 0;
virtual QString defaultPath() const = 0;
......
......@@ -35,6 +35,7 @@
#include <coreplugin/icore.h>
#include <utils/reloadpromptutils.h>
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
#include <QtDesigner/QDesignerFormWindowInterface>
......@@ -62,7 +63,7 @@ FormWindowFile::FormWindowFile(QDesignerFormWindowInterface *form, QObject *pare
this, SLOT(slotFormWindowRemoved(QDesignerFormWindowInterface*)));
}
bool FormWindowFile::save(const QString &name /* = QString() */)
bool FormWindowFile::save(QString *errorString, const QString &name /* = QString() */)
{
const QString actualName = name.isEmpty() ? fileName() : name;
......@@ -79,12 +80,10 @@ bool FormWindowFile::save(const QString &name /* = QString() */)
const QString formName = fi.absoluteFilePath();
m_formWindow->setFileName(formName);
QString errorString;
const bool warningsEnabled = qdesigner_internal::QSimpleResource::setWarningsEnabled(false);
const bool writeOK = writeFile(actualName, errorString);
qdesigner_internal::QSimpleResource::setWarningsEnabled(warningsEnabled);
if (!writeOK) {
QMessageBox::critical(0, tr("Error saving %1").arg(formName), errorString);
m_formWindow->setFileName(oldFormName);
return false;
}
......@@ -178,29 +177,14 @@ QString FormWindowFile::mimeType() const
return m_mimeType;
}
bool FormWindowFile::writeFile(const QString &fileName, QString &errorString) const
bool FormWindowFile::writeFile(const QString &fileName, QString *errorString) const
{
if (Designer::Constants::Internal::debug)
qDebug() << Q_FUNC_INFO << m_fileName << fileName;
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
errorString = tr("Unable to open %1: %2").arg(fileName, file.errorString());
return false;
}
const bool rc = writeFile(file, errorString);
file.close();
return rc;
}
bool FormWindowFile::writeFile(QFile &file, QString &errorString) const
{
const QByteArray content = m_formWindow->contents().toUtf8();
if (!file.write(content)) {
errorString = tr("Unable to write to %1: %2").arg(file.fileName(), file.errorString());
return false;
}
return true;
Utils::FileSaver saver(fileName, QIODevice::Text);
saver.write(m_formWindow->contents().toUtf8());
return saver.finalize(errorString);
}
void FormWindowFile::setFileName(const QString &fname)
......
......@@ -53,7 +53,7 @@ public:
explicit FormWindowFile(QDesignerFormWindowInterface *form, QObject *parent = 0);
// IFile
virtual bool save(const QString &fileName = QString());
virtual bool save(QString *errorString, const QString &fileName = QString());
virtual QString fileName() const;
virtual bool isModified() const;
virtual bool isReadOnly() const;
......@@ -68,8 +68,7 @@ public:
// Internal
void setSuggestedFileName(const QString &fileName);
bool writeFile(const QString &fileName, QString &errorString) const;
bool writeFile(QFile &file, QString &errorString) const;
bool writeFile(const QString &fileName, QString *errorString) const;
QDesignerFormWindowInterface *formWindow() const;
......
......@@ -565,7 +565,7 @@ GenericProjectFile::GenericProjectFile(GenericProject *parent, QString fileName)
GenericProjectFile::~GenericProjectFile()
{ }
bool GenericProjectFile::save(const QString &)
bool GenericProjectFile::save(QString *, const QString &)
{
return false;
}
......
......@@ -159,7 +159,7 @@ public:
GenericProjectFile(GenericProject *parent, QString fileName);
virtual ~GenericProjectFile();
virtual bool save(const QString &fileName = QString());
virtual bool save(QString *errorString, const QString &fileName = QString());
virtual QString fileName() const;
virtual QString defaultPath() const;
......
......@@ -173,9 +173,9 @@ ProjectFilesDocument::ProjectFilesDocument(Manager *manager)
ProjectFilesDocument::~ProjectFilesDocument()
{ }
bool ProjectFilesDocument::save(const QString &name)
bool ProjectFilesDocument::save(QString *errorString, const QString &name)
{
if (! BaseTextDocument::save(name))
if (! BaseTextDocument::save(errorString, name))
return false;
m_manager->notifyChanged(name);
......
......@@ -113,7 +113,7 @@ public:
ProjectFilesDocument(Manager *manager);
virtual ~ProjectFilesDocument();
virtual bool save(const QString &name);
virtual bool save(QString *errorString, const QString &name);
private:
Manager *m_manager;
......
......@@ -86,8 +86,9 @@ void ImageViewerFile::reload(Core::IFile::ReloadFlag flag,
}
}
bool ImageViewerFile::save(const QString &fileName)
bool ImageViewerFile::save(QString *errorString, const QString &fileName)
{
Q_UNUSED(errorString)
Q_UNUSED(fileName);
return false;
}
......
......@@ -49,7 +49,7 @@ public:
explicit ImageViewerFile(ImageViewer *parent = 0);
~ImageViewerFile();
bool save(const QString &fileName = QString());
bool save(QString *errorString, const QString &fileName = QString());
void rename(const QString &newName);
QString fileName() const;
......
......@@ -46,7 +46,7 @@ QmlProjectFile::QmlProjectFile(QmlProject *parent, QString fileName)
QmlProjectFile::~QmlProjectFile()
{ }
bool QmlProjectFile::save(const QString &)
bool QmlProjectFile::save(QString *, const QString &)
{
return false;
}
......
......@@ -49,7 +49,7 @@ public:
QmlProjectFile(QmlProject *parent, QString fileName);
virtual ~QmlProjectFile();
virtual bool save(const QString &fileName = QString());
virtual bool save(QString *errorString, const QString &fileName = QString());
virtual QString fileName() const;
virtual void rename(const QString &newName);
......
......@@ -175,8 +175,9 @@ Qt4PriFile::Qt4PriFile(Qt4PriFileNode *qt4PriFile)
}
bool Qt4PriFile::save(const QString &fileName)
bool Qt4PriFile::save(QString *errorString, const QString &fileName)
{
Q_UNUSED(errorString);
Q_UNUSED(fileName);
return false;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment