Commit 99c383f3 authored by Eike Ziller's avatar Eike Ziller

Make IDocument::fileName a member with setter.

Instead of requiring subclasses to implement a method.
Also renames IDocument::rename to IDocument::setFileName,
since it doesn't really rename any files or such.

Change-Id: I1344025c24d2f74a6a983e04fb0a5245f1f37aad
Reviewed-by: default avatarDaniel Teske <daniel.teske@digia.com>
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
parent 7dd81eca
......@@ -38,9 +38,9 @@ using namespace AutotoolsProjectManager::Internal;
AutotoolsProjectFile::AutotoolsProjectFile(AutotoolsProject *project, const QString &fileName) :
Core::IDocument(project),
m_project(project),
m_fileName(fileName)
m_project(project)
{
setFileName(fileName);
}
bool AutotoolsProjectFile::save(QString *errorString, const QString &fileName, bool autoSave)
......@@ -52,11 +52,6 @@ bool AutotoolsProjectFile::save(QString *errorString, const QString &fileName, b
return false;
}
QString AutotoolsProjectFile::fileName() const
{
return m_fileName;
}
QString AutotoolsProjectFile::defaultPath() const
{
return QString();
......@@ -90,8 +85,3 @@ bool AutotoolsProjectFile::reload(QString *errorString, ReloadFlag flag, ChangeT
return false;
}
void AutotoolsProjectFile::rename(const QString &newName)
{
Q_UNUSED(newName);
}
......@@ -57,18 +57,15 @@ public:
AutotoolsProjectFile(AutotoolsProject *project, const QString &fileName);
bool save(QString *errorString, const QString &fileName, bool autoSave);
QString fileName() const;
QString defaultPath() const;
QString suggestedFileName() const;
QString mimeType() const;
bool isModified() const;
bool isSaveAsAllowed() const;
bool reload(QString *errorString, ReloadFlag flag, ChangeType type);
void rename(const QString &newName);
private:
AutotoolsProject *m_project;
QString m_fileName;
};
} // namespace Internal
......
......@@ -195,25 +195,22 @@ public:
return QLatin1String(Constants::C_BINEDITOR_MIMETYPE);
}
bool save(QString *errorString, const QString &fileName, bool autoSave)
bool save(QString *errorString, const QString &fn, bool autoSave)
{
QTC_ASSERT(!autoSave, return true); // bineditor does not support autosave - it would be a bit expensive
const QString fileNameToUse
= fileName.isEmpty() ? m_fileName : fileName;
if (m_widget->save(errorString, m_fileName, fileNameToUse)) {
m_fileName = fileNameToUse;
m_widget->editor()->setDisplayName(QFileInfo(fileNameToUse).fileName());
emit changed();
= fn.isEmpty() ? fileName() : fn;
if (m_widget->save(errorString, fileName(), fileNameToUse)) {
setFileName(fileNameToUse);
return true;
} else {
return false;
}
}
void rename(const QString &newName) {
m_fileName = newName;
m_widget->editor()->setDisplayName(QFileInfo(fileName()).fileName());
emit changed();
void setFileName(const QString &newName) {
m_widget->editor()->setDisplayName(QFileInfo(newName).fileName());
IDocument::setFileName(newName);
}
bool open(QString *errorString, const QString &fileName, quint64 offset = 0) {
......@@ -231,9 +228,8 @@ public:
return false;
if (file.open(QIODevice::ReadOnly)) {
file.close();
m_fileName = fileName;
setFileName(fileName);
m_widget->setSizes(offset, file.size());
m_widget->editor()->setDisplayName(QFileInfo(fileName).fileName());
return true;
}
QString errStr = tr("Cannot open %1: %2").arg(
......@@ -248,9 +244,10 @@ public:
private slots:
void provideData(quint64 block)
{
if (m_fileName.isEmpty())
const QString fn = fileName();
if (fn.isEmpty())
return;
QFile file(m_fileName);
QFile file(fn);
if (file.open(QIODevice::ReadOnly)) {
int blockSize = m_widget->dataBlockSize();
file.seek(block * blockSize);
......@@ -263,23 +260,17 @@ private slots:
} else {
QMessageBox::critical(Core::ICore::mainWindow(), tr("File Error"),
tr("Cannot open %1: %2").arg(
QDir::toNativeSeparators(m_fileName), file.errorString()));
QDir::toNativeSeparators(fn), file.errorString()));
}
}
void provideNewRange(quint64 offset)
{
open(0, m_fileName, offset);
open(0, fileName(), offset);
}
public:
void setFilename(const QString &filename) {
m_fileName = filename;
}
QString fileName() const { return m_fileName; }
QString defaultPath() const { return QString(); }
QString suggestedFileName() const { return QString(); }
......@@ -287,9 +278,10 @@ public:
bool isModified() const { return m_widget->isMemoryView() ? false : m_widget->isModified(); }
bool isFileReadOnly() const {
if (m_widget->isMemoryView() || m_fileName.isEmpty())
const QString fn = fileName();
if (m_widget->isMemoryView() || fn.isEmpty())
return false;
const QFileInfo fi(m_fileName);
const QFileInfo fi(fn);
return !fi.isWritable();
}
......@@ -302,7 +294,7 @@ public:
emit changed();
} else {
emit aboutToReload();
const bool success = open(errorString, m_fileName);
const bool success = open(errorString, fileName());
emit reloadFinished(success);
return success;
}
......@@ -311,7 +303,6 @@ public:
private:
BinEditorWidget *m_widget;
QString m_fileName;
};
class BinEditor : public Core::IEditor
......@@ -358,7 +349,7 @@ public:
bool createNew(const QString & /* contents */ = QString()) {
m_widget->clear();
m_file->setFilename(QString());
m_file->setFileName(QString());
return true;
}
bool open(QString *errorString, const QString &fileName, const QString &realFileName) {
......
......@@ -105,7 +105,7 @@ CMakeProject::CMakeProject(CMakeManager *manager, const QString &fileName)
: m_manager(manager),
m_activeTarget(0),
m_fileName(fileName),
m_rootNode(new CMakeProjectNode(m_fileName)),
m_rootNode(new CMakeProjectNode(fileName)),
m_lastEditor(0)
{
setProjectContext(Core::Context(CMakeProjectManager::Constants::PROJECTCONTEXT));
......@@ -897,9 +897,9 @@ void CMakeProject::buildStateChanged(ProjectExplorer::Project *project)
// CMakeFile
CMakeFile::CMakeFile(CMakeProject *parent, QString fileName)
: Core::IDocument(parent), m_project(parent), m_fileName(fileName)
: Core::IDocument(parent), m_project(parent)
{
setFileName(fileName);
}
bool CMakeFile::save(QString *errorString, const QString &fileName, bool autoSave)
......@@ -912,11 +912,6 @@ bool CMakeFile::save(QString *errorString, const QString &fileName, bool autoSav
return false;
}
QString CMakeFile::fileName() const
{
return m_fileName;
}
QString CMakeFile::defaultPath() const
{
return QString();
......@@ -943,13 +938,6 @@ bool CMakeFile::isSaveAsAllowed() const
return false;
}
void CMakeFile::rename(const QString &newName)
{
Q_ASSERT(false);
Q_UNUSED(newName);
// Can't happen....
}
Core::IDocument::ReloadBehavior CMakeFile::reloadBehavior(ChangeTrigger state, ChangeType type) const
{
Q_UNUSED(state)
......
......@@ -204,7 +204,6 @@ public:
CMakeFile(CMakeProject *parent, QString fileName);
bool save(QString *errorString, const QString &fileName, bool autoSave);
QString fileName() const;
QString defaultPath() const;
QString suggestedFileName() const;
......@@ -216,11 +215,8 @@ public:
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const;
bool reload(QString *errorString, ReloadFlag flag, ChangeType type);
void rename(const QString &newName);
private:
CMakeProject *m_project;
QString m_fileName;
};
class CMakeBuildSettingsWidget : public ProjectExplorer::NamedWidget
......
......@@ -385,7 +385,7 @@ void DocumentManager::renamedFile(const QString &from, const QString &to)
foreach (IDocument *document, documentsToRename) {
d->m_blockedIDocument = document;
removeFileInfo(document);
document->rename(to);
document->setFileName(to);
addFileInfo(document);
d->m_blockedIDocument = 0;
}
......
......@@ -34,6 +34,13 @@
#include <QFile>
#include <QFileInfo>
/*!
\fn QString Core::IDocument::fileName() const
Returns the absolute path of the file that this document refers to. May be empty for
non-file documents.
\sa setFileName()
*/
namespace Core {
IDocument::IDocument(QObject *parent) : QObject(parent), m_infoBar(0), m_hasWriteWarning(false), m_restored(false)
......@@ -110,4 +117,20 @@ InfoBar *IDocument::infoBar()
return m_infoBar;
}
/*!
Set absolute file path for this file to \a fileName. Can be empty.
The default implementation sets the file name and sends fileNameChanged() and changed()
signals. Can be reimplemented by subclasses to do more.
\sa fileName()
*/
void IDocument::setFileName(const QString &fileName)
{
if (m_fileName == fileName)
return;
QString oldName = m_fileName;
m_fileName = fileName;
emit fileNameChanged(oldName, m_fileName);
emit changed();
}
} // namespace Core
......@@ -82,7 +82,8 @@ public:
virtual ~IDocument();
virtual bool save(QString *errorString, const QString &fileName = QString(), bool autoSave = false) = 0;
virtual QString fileName() const = 0;
QString fileName() const { return m_fileName; }
virtual void setFileName(const QString &fileName);
virtual bool isFileReadOnly() const;
virtual QString defaultPath() const = 0;
......@@ -95,7 +96,6 @@ public:
virtual ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const;
virtual bool reload(QString *errorString, ReloadFlag flag, ChangeType type) = 0;
virtual void rename(const QString &newName) = 0;
virtual void checkPermissions();
......@@ -117,6 +117,7 @@ signals:
void fileNameChanged(const QString &oldName, const QString &newName);
private:
QString m_fileName;
QString m_autoSaveName;
InfoBar *m_infoBar;
bool m_hasWriteWarning;
......
......@@ -94,31 +94,21 @@ bool FormWindowFile::save(QString *errorString, const QString &name, bool autoSa
return false;
}
const QString oldFileName = m_fileName;
m_fileName = fi.absoluteFilePath();
emit setDisplayName(fi.fileName());
m_formWindow->setDirty(false);
emit fileNameChanged(oldFileName, m_fileName);
setFileName(fi.absoluteFilePath());
emit changed();
emit saved();
return true;
}
void FormWindowFile::rename(const QString &newName)
void FormWindowFile::setFileName(const QString &newName)
{
m_formWindow->setFileName(newName);
QFileInfo fi(newName);
const QString oldFileName = m_fileName;
m_fileName = fi.absoluteFilePath();
emit setDisplayName(fi.fileName());
emit fileNameChanged(oldFileName, m_fileName);
emit changed();
}
QString FormWindowFile::fileName() const
{
return m_fileName;
IDocument::setFileName(fi.absoluteFilePath());
}
bool FormWindowFile::shouldAutoSave() const
......@@ -144,7 +134,7 @@ bool FormWindowFile::reload(QString *errorString, ReloadFlag flag, ChangeType ty
emit changed();
} else {
emit aboutToReload();
emit reload(errorString, m_fileName);
emit reload(errorString, fileName());
const bool success = errorString->isEmpty();
emit reloadFinished(success);
return success;
......@@ -157,12 +147,12 @@ QString FormWindowFile::defaultPath() const
return QString();
}
void FormWindowFile::setSuggestedFileName(const QString &fileName)
void FormWindowFile::setSuggestedFileName(const QString &fn)
{
if (Designer::Constants::Internal::debug)
qDebug() << Q_FUNC_INFO << m_fileName << fileName;
qDebug() << Q_FUNC_INFO << fileName() << fn;
m_suggestedName = fileName;
m_suggestedName = fn;
}
QString FormWindowFile::suggestedFileName() const
......@@ -175,16 +165,11 @@ QString FormWindowFile::mimeType() const
return m_mimeType;
}
bool FormWindowFile::writeFile(const QString &fileName, QString *errorString) const
bool FormWindowFile::writeFile(const QString &fn, QString *errorString) const
{
if (Designer::Constants::Internal::debug)
qDebug() << Q_FUNC_INFO << m_fileName << fileName;
return write(fileName, format(), m_formWindow->contents(), errorString);
}
void FormWindowFile::setFileName(const QString &fname)
{
m_fileName = fname;
qDebug() << Q_FUNC_INFO << fileName() << fn;
return write(fn, format(), m_formWindow->contents(), errorString);
}
QDesignerFormWindowInterface *FormWindowFile::formWindow() const
......
......@@ -51,7 +51,6 @@ public:
// IDocument
virtual bool save(QString *errorString, const QString &fileName, bool autoSave);
virtual QString fileName() const;
virtual bool shouldAutoSave() const;
virtual bool isModified() const;
virtual bool isSaveAsAllowed() const;
......@@ -59,7 +58,6 @@ public:
virtual QString defaultPath() const;
virtual QString suggestedFileName() const;
virtual QString mimeType() const;
virtual void rename(const QString &newName);
// Internal
void setSuggestedFileName(const QString &fileName);
......@@ -84,7 +82,6 @@ private slots:
private:
const QString m_mimeType;
QString m_fileName;
QString m_suggestedName;
bool m_shouldAutoSave;
// Might actually go out of scope before the IEditor due
......
......@@ -41,20 +41,6 @@ DiffEditorFile::DiffEditorFile(const QString &mimeType, QObject *parent) :
{
}
void DiffEditorFile::rename(const QString &newName)
{
Q_UNUSED(newName);
return;
}
void DiffEditorFile::setFileName(const QString &name)
{
if (m_fileName == name)
return;
m_fileName = name;
emit changed();
}
void DiffEditorFile::setModified(bool modified)
{
if (m_modified == modified)
......
......@@ -48,7 +48,6 @@ public:
explicit DiffEditorFile(const QString &mimeType,
QObject *parent = 0);
QString fileName() const { return m_fileName; }
QString defaultPath() const { return QString(); }
QString suggestedFileName() const { return QString(); }
......@@ -58,9 +57,7 @@ public:
bool save(QString *errorString, const QString &fileName, bool autoSave);
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const;
bool reload(QString *errorString, ReloadFlag flag, ChangeType type);
void rename(const QString &newName);
void setFileName(const QString &name);
void setModified(bool modified = true);
signals:
......@@ -69,7 +66,6 @@ signals:
private:
const QString m_mimeType;
bool m_modified;
QString m_fileName;
};
} // namespace Internal
......
......@@ -455,18 +455,14 @@ bool GenericProject::fromMap(const QVariantMap &map)
GenericProjectFile::GenericProjectFile(GenericProject *parent, QString fileName, GenericProject::RefreshOptions options)
: IDocument(parent),
m_project(parent),
m_fileName(fileName),
m_options(options)
{ }
bool GenericProjectFile::save(QString *, const QString &, bool)
{
return false;
setFileName(fileName);
}
QString GenericProjectFile::fileName() const
bool GenericProjectFile::save(QString *, const QString &, bool)
{
return m_fileName;
return false;
}
QString GenericProjectFile::defaultPath() const
......@@ -494,13 +490,6 @@ bool GenericProjectFile::isSaveAsAllowed() const
return false;
}
void GenericProjectFile::rename(const QString &newName)
{
// Can't happen
Q_UNUSED(newName);
QTC_CHECK(false);
}
IDocument::ReloadBehavior GenericProjectFile::reloadBehavior(ChangeTrigger state, ChangeType type) const
{
Q_UNUSED(state)
......
......@@ -128,7 +128,6 @@ public:
GenericProjectFile(GenericProject *parent, QString fileName, GenericProject::RefreshOptions options);
bool save(QString *errorString, const QString &fileName, bool autoSave);
QString fileName() const;
QString defaultPath() const;
QString suggestedFileName() const;
......@@ -136,14 +135,12 @@ public:
bool isModified() const;
bool isSaveAsAllowed() const;
void rename(const QString &newName);
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const;
bool reload(QString *errorString, ReloadFlag flag, ChangeType type);
private:
GenericProject *m_project;
QString m_fileName;
GenericProject::RefreshOptions m_options;
};
......
......@@ -61,7 +61,7 @@ bool ImageViewerFile::reload(QString *errorString,
emit changed();
return true;
}
return m_editor->open(errorString, m_fileName, m_fileName);
return m_editor->open(errorString, fileName(), fileName());
}
bool ImageViewerFile::save(QString *errorString, const QString &fileName, bool autoSave)
......@@ -72,18 +72,10 @@ bool ImageViewerFile::save(QString *errorString, const QString &fileName, bool a
return false;
}
void ImageViewerFile::rename(const QString &newName)
void ImageViewerFile::setFileName(const QString &newName)
{
const QString oldFilename = m_fileName;
m_fileName = newName;
m_editor->setDisplayName(QFileInfo(m_fileName).fileName());
emit fileNameChanged(oldFilename, newName);
emit changed();
}
QString ImageViewerFile::fileName() const
{
return m_fileName;
m_editor->setDisplayName(QFileInfo(newName).fileName());
IDocument::setFileName(newName);
}
QString ImageViewerFile::defaultPath() const
......@@ -117,11 +109,5 @@ void ImageViewerFile::setMimetype(const QString &mimetype)
emit changed();
}
void ImageViewerFile::setFileName(const QString &filename)
{
m_fileName = filename;
emit changed();
}
} // namespace Internal
} // namespace ImageViewer
......@@ -46,8 +46,7 @@ public:
explicit ImageViewerFile(ImageViewer *parent = 0);
bool save(QString *errorString, const QString &fileName, bool autoSave);
void rename(const QString &newName);
QString fileName() const;
void setFileName(const QString &newName);
QString defaultPath() const;
QString suggestedFileName() const;
......@@ -60,10 +59,8 @@ public:
bool reload(QString *errorString, ReloadFlag flag, ChangeType type);
void setMimetype(const QString &mimetype);
void setFileName(const QString &filename);
private:
QString m_fileName;
QString m_mimeType;
ImageViewer *m_editor;
};
......
......@@ -56,10 +56,12 @@ class WatchableFile : public Core::IDocument
Q_OBJECT
public:
WatchableFile(const QString &fileName, QObject *parent = 0)
: Core::IDocument(parent), m_fileName(fileName) {}
: Core::IDocument(parent)
{
setFileName(fileName);
}
bool save(QString *, const QString &, bool) { return false; }
QString fileName() const { return m_fileName; }
QString defaultPath() const { return QString(); }
QString suggestedFileName() const { return QString(); }
QString mimeType() const { return QLatin1String("text/plain"); }
......@@ -67,13 +69,9 @@ public:
bool isSaveAsAllowed() const { return false; }
ReloadBehavior reloadBehavior(ChangeTrigger, ChangeType) const { return BehaviorSilent; }
bool reload(QString *, ReloadFlag, ChangeType) { emit modified(); return true; }
void rename(const QString &) {}
signals:
void modified();
private:
QString m_fileName;
};
class MaemoGlobal
......
......@@ -36,9 +36,10 @@ namespace QbsProjectManager {
namespace Internal {
QbsProjectFile::QbsProjectFile(QbsProject *parent, QString fileName) : Core::IDocument(parent),
m_project(parent),
m_fileName(fileName)
{ }
m_project(parent)
{
setFileName(fileName);
}
QbsProjectFile::~QbsProjectFile()
{ }
......@@ -48,11 +49,6 @@ bool QbsProjectFile::save(QString *, const QString &, bool)
return false;
}
QString QbsProjectFile::fileName() const
{
return m_fileName;
}
bool QbsProjectFile::isReadOnly() const
{
return true;
......@@ -100,13 +96,6 @@ bool QbsProjectFile::reload(QString *errorString, ReloadFlag flag, ChangeType ty
return true;
}
void QbsProjectFile::rename(const QString &newName)
{
// Can't happen
Q_UNUSED(newName);
Q_ASSERT(false);
}
} // namespace Internal
} // namespace QbsProjectManager
......@@ -46,7 +46,6 @@ public:
~QbsProjectFile();
bool save(QString *errorString, const QString &fileName, bool autoSave);
QString fileName() const;
bool isReadOnly() const;
QString defaultPath() const;
......@@ -58,11 +57,9 @@ public:
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const;
bool reload(QString *errorString, ReloadFlag flag, ChangeType type);
void rename(const QString &newName);