2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-04-15 11:49:11 +02:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-04-15 11:49:11 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-04-15 11:49:11 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2011-04-15 11:49:11 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2011-04-15 11:49:11 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2011-04-15 11:49:11 +02:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-04-15 11:49:11 +02:00
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
#include "idocument.h"
|
2011-04-15 11:49:11 +02:00
|
|
|
|
2011-05-06 12:48:44 +02:00
|
|
|
#include "infobar.h"
|
|
|
|
|
|
2014-03-05 15:58:12 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QFile>
|
|
|
|
|
#include <QFileInfo>
|
2011-05-10 20:43:03 +02:00
|
|
|
|
2014-01-21 14:22:33 +01:00
|
|
|
/*!
|
|
|
|
|
\class Core::IDocument
|
|
|
|
|
\brief The IDocument class describes a document that can be saved and reloaded.
|
|
|
|
|
|
|
|
|
|
The most common use for implementing an IDocument subclass, is as a document for an IEditor
|
|
|
|
|
implementation. Multiple editors can work in the same document instance, so the IDocument
|
|
|
|
|
subclass should hold all data and functions that are independent from the specific
|
|
|
|
|
IEditor instance, for example the content, highlighting information, the name of the
|
|
|
|
|
corresponding file, and functions for saving and reloading the file.
|
|
|
|
|
|
|
|
|
|
Each IDocument subclass works only with the corresponding IEditor subclasses that it
|
|
|
|
|
was designed to work with.
|
|
|
|
|
|
|
|
|
|
\mainclass
|
|
|
|
|
*/
|
|
|
|
|
|
2013-07-04 11:35:56 +02:00
|
|
|
/*!
|
2013-07-04 13:30:26 +02:00
|
|
|
\fn QString Core::IDocument::filePath() const
|
2013-07-04 11:35:56 +02:00
|
|
|
Returns the absolute path of the file that this document refers to. May be empty for
|
|
|
|
|
non-file documents.
|
2013-07-04 13:30:26 +02:00
|
|
|
\sa setFilePath()
|
2013-07-04 11:35:56 +02:00
|
|
|
*/
|
|
|
|
|
|
2011-04-15 11:49:11 +02:00
|
|
|
namespace Core {
|
|
|
|
|
|
2013-07-12 15:36:29 +02:00
|
|
|
IDocument::IDocument(QObject *parent) : QObject(parent),
|
|
|
|
|
m_temporary(false),
|
|
|
|
|
m_infoBar(0),
|
|
|
|
|
m_hasWriteWarning(false),
|
|
|
|
|
m_restored(false)
|
2011-04-15 11:49:11 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
IDocument::~IDocument()
|
2011-04-15 11:49:11 +02:00
|
|
|
{
|
2011-05-10 20:43:03 +02:00
|
|
|
removeAutoSaveFile();
|
2011-05-06 12:48:44 +02:00
|
|
|
delete m_infoBar;
|
2011-04-15 11:49:11 +02:00
|
|
|
}
|
|
|
|
|
|
2014-03-05 15:58:12 +01:00
|
|
|
void IDocument::setId(Id id)
|
|
|
|
|
{
|
|
|
|
|
m_id = id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Id IDocument::id() const
|
|
|
|
|
{
|
|
|
|
|
QTC_CHECK(m_id.isValid());
|
|
|
|
|
return m_id;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-15 15:14:10 +02:00
|
|
|
/*!
|
|
|
|
|
Used for example by EditorManager::openEditorWithContents() to set the contents
|
|
|
|
|
of this document.
|
|
|
|
|
Returns if setting the contents was successful.
|
|
|
|
|
The base implementation does nothing and returns false.
|
|
|
|
|
*/
|
|
|
|
|
bool IDocument::setContents(const QByteArray &contents)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(contents)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
IDocument::ReloadBehavior IDocument::reloadBehavior(ChangeTrigger state, ChangeType type) const
|
2011-04-15 11:53:46 +02:00
|
|
|
{
|
|
|
|
|
if (type == TypePermissions)
|
|
|
|
|
return BehaviorSilent;
|
|
|
|
|
if (type == TypeContents && state == TriggerInternal && !isModified())
|
|
|
|
|
return BehaviorSilent;
|
|
|
|
|
return BehaviorAsk;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
void IDocument::checkPermissions()
|
2011-04-15 11:49:11 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
bool IDocument::shouldAutoSave() const
|
2011-05-10 20:43:03 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
bool IDocument::isFileReadOnly() const
|
2012-02-14 12:10:29 +01:00
|
|
|
{
|
2013-07-04 13:30:26 +02:00
|
|
|
if (filePath().isEmpty())
|
2012-02-14 12:10:29 +01:00
|
|
|
return false;
|
2013-07-04 13:30:26 +02:00
|
|
|
return !QFileInfo(filePath()).isWritable();
|
2012-02-14 12:10:29 +01:00
|
|
|
}
|
|
|
|
|
|
2013-07-12 15:36:29 +02:00
|
|
|
/*!
|
|
|
|
|
Returns if the document is a temporary that should for example not be considered
|
|
|
|
|
when saving/restoring the session state, recent files, etc. Defaults to false.
|
|
|
|
|
\sa setTemporary()
|
|
|
|
|
*/
|
|
|
|
|
bool IDocument::isTemporary() const
|
|
|
|
|
{
|
|
|
|
|
return m_temporary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Sets if the document is \a temporary.
|
|
|
|
|
\sa isTemporary()
|
|
|
|
|
*/
|
|
|
|
|
void IDocument::setTemporary(bool temporary)
|
|
|
|
|
{
|
|
|
|
|
m_temporary = temporary;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-26 02:15:34 +02:00
|
|
|
void IDocument::setMimeType(const QString &mimeType)
|
|
|
|
|
{
|
|
|
|
|
if (m_mimeType != mimeType) {
|
|
|
|
|
m_mimeType = mimeType;
|
|
|
|
|
emit mimeTypeChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
bool IDocument::autoSave(QString *errorString, const QString &fileName)
|
2011-05-10 20:43:03 +02:00
|
|
|
{
|
|
|
|
|
if (!save(errorString, fileName, true))
|
|
|
|
|
return false;
|
|
|
|
|
m_autoSaveName = fileName;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const char kRestoredAutoSave[] = "RestoredAutoSave";
|
|
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
void IDocument::setRestoredFrom(const QString &name)
|
2011-05-10 20:43:03 +02:00
|
|
|
{
|
|
|
|
|
m_autoSaveName = name;
|
|
|
|
|
m_restored = true;
|
2012-11-08 21:12:56 +02:00
|
|
|
InfoBarEntry info(Id(kRestoredAutoSave),
|
2011-05-10 20:43:03 +02:00
|
|
|
tr("File was restored from auto-saved copy. "
|
2012-10-01 10:14:11 +02:00
|
|
|
"Select Save to confirm or Revert to Saved to discard changes."));
|
2011-05-10 20:43:03 +02:00
|
|
|
infoBar()->addInfo(info);
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
void IDocument::removeAutoSaveFile()
|
2011-05-10 20:43:03 +02:00
|
|
|
{
|
|
|
|
|
if (!m_autoSaveName.isEmpty()) {
|
|
|
|
|
QFile::remove(m_autoSaveName);
|
|
|
|
|
m_autoSaveName.clear();
|
|
|
|
|
if (m_restored) {
|
|
|
|
|
m_restored = false;
|
2012-11-08 21:12:56 +02:00
|
|
|
infoBar()->removeInfo(Id(kRestoredAutoSave));
|
2011-05-10 20:43:03 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
InfoBar *IDocument::infoBar()
|
2011-05-06 12:48:44 +02:00
|
|
|
{
|
|
|
|
|
if (!m_infoBar)
|
|
|
|
|
m_infoBar = new InfoBar;
|
|
|
|
|
return m_infoBar;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-04 11:35:56 +02:00
|
|
|
/*!
|
2013-07-04 13:30:26 +02:00
|
|
|
Set absolute file path for this file to \a filePath. Can be empty.
|
|
|
|
|
The default implementation sets the file name and sends filePathChanged() and changed()
|
2013-07-04 11:35:56 +02:00
|
|
|
signals. Can be reimplemented by subclasses to do more.
|
2013-07-04 13:30:26 +02:00
|
|
|
\sa filePath()
|
2013-07-04 11:35:56 +02:00
|
|
|
*/
|
2013-07-04 13:30:26 +02:00
|
|
|
void IDocument::setFilePath(const QString &filePath)
|
2013-07-04 11:35:56 +02:00
|
|
|
{
|
2013-07-04 13:30:26 +02:00
|
|
|
if (m_filePath == filePath)
|
2013-07-04 11:35:56 +02:00
|
|
|
return;
|
2013-07-04 13:30:26 +02:00
|
|
|
QString oldName = m_filePath;
|
|
|
|
|
m_filePath = filePath;
|
|
|
|
|
emit filePathChanged(oldName, m_filePath);
|
2013-07-04 11:35:56 +02:00
|
|
|
emit changed();
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-04 22:25:15 +02:00
|
|
|
/*!
|
|
|
|
|
Returns the string to display for this document, e.g. in the open document combo box
|
|
|
|
|
and pane.
|
|
|
|
|
\sa setDisplayName()
|
|
|
|
|
*/
|
|
|
|
|
QString IDocument::displayName() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_displayName.isEmpty())
|
|
|
|
|
return m_displayName;
|
|
|
|
|
return QFileInfo(m_filePath).fileName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Sets the string that is displayed for this document, e.g. in the open document combo box
|
|
|
|
|
and pane, to \a name. Defaults to the file name of the file path for this document.
|
|
|
|
|
You can reset the display name to the default by passing an empty string.
|
|
|
|
|
\sa displayName()
|
|
|
|
|
\sa filePath()
|
|
|
|
|
*/
|
|
|
|
|
void IDocument::setDisplayName(const QString &name)
|
|
|
|
|
{
|
|
|
|
|
if (name == m_displayName)
|
|
|
|
|
return;
|
|
|
|
|
m_displayName = name;
|
|
|
|
|
emit changed();
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-15 11:49:11 +02:00
|
|
|
} // namespace Core
|