Commit bd5a1a9b authored by Friedemann Kleint's avatar Friedemann Kleint

Extract Core::TextFile from TextEditor::BaseTextDocument.

Use it in BaseTextEditor and Designer to preserve CRLF
of the files.

Task-number: QTCREATORBUG-5901
Change-Id: I7599ce78649a3b09f2e5118d02f8cbf3db27ed31
Reviewed-on: http://codereview.qt.nokia.com/3591Reviewed-by: default avatarFriedemann Kleint <Friedemann.Kleint@nokia.com>
parent e2988101
This diff is collapsed.
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#ifndef TEXTFILEUTILS_H
#define TEXTFILEUTILS_H
#include "utils_global.h"
#include <QtCore/QtGlobal>
QT_BEGIN_NAMESPACE
class QTextCodec;
class QStringList;
class QString;
class QByteArray;
QT_END_NAMESPACE
namespace Utils {
class QTCREATOR_UTILS_EXPORT TextFileFormat {
public:
enum LineTerminationMode
{
LFLineTerminator,
CRLFLineTerminator,
NativeLineTerminator =
#if defined (Q_OS_WIN)
CRLFLineTerminator,
#else
LFLineTerminator
#endif
};
enum ReadResult
{
ReadSuccess,
ReadEncodingError,
ReadMemoryAllocationError,
ReadIOError
};
TextFileFormat();
static TextFileFormat detect(const QByteArray &data);
bool decode(const QByteArray &data, QString *target) const;
bool decode(const QByteArray &data, QStringList *target) const;
static ReadResult readFile(const QString &fileName, const QTextCodec *defaultCodec,
QStringList *plainText, TextFileFormat *format, QString *errorString,
QByteArray *decodingErrorSample = 0);
static ReadResult readFile(const QString &fileName, const QTextCodec *defaultCodec,
QString *plainText, TextFileFormat *format, QString *errorString,
QByteArray *decodingErrorSample = 0);
bool writeFile(const QString &fileName, QString plainText, QString *errorString) const;
static QByteArray decodingErrorSample(const QByteArray &data);
LineTerminationMode lineTerminationMode;
bool hasUtf8Bom;
const QTextCodec *codec;
};
} // namespace Utils
#endif // TEXTFILEUTILS_H
......@@ -40,6 +40,7 @@ SOURCES += $$PWD/environment.cpp \
$$PWD/synchronousprocess.cpp \
$$PWD/savefile.cpp \
$$PWD/fileutils.cpp \
$$PWD/textfileformat.cpp \
$$PWD/submitfieldwidget.cpp \
$$PWD/consoleprocess.cpp \
$$PWD/uncommentselection.cpp \
......@@ -135,6 +136,7 @@ HEADERS += \
$$PWD/synchronousprocess.h \
$$PWD/savefile.h \
$$PWD/fileutils.h \
$$PWD/textfileformat.h \
$$PWD/submitfieldwidget.h \
$$PWD/uncommentselection.h \
$$PWD/parameteraction.h \
......
......@@ -91,7 +91,8 @@ SOURCES += mainwindow.cpp \
mimetypemagicdialog.cpp \
mimetypesettings.cpp \
dialogs/promptoverwritedialog.cpp \
fileutils.cpp
fileutils.cpp \
textfile.cpp
HEADERS += mainwindow.h \
editmode.h \
......@@ -185,7 +186,8 @@ HEADERS += mainwindow.h \
mimetypesettings.h \
dialogs/promptoverwritedialog.h \
fileutils.h \
externaltoolmanager.h
externaltoolmanager.h \
textfile.h
FORMS += dialogs/newdialog.ui \
actionmanager/commandmappings.ui \
......
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#include "textfile.h"
#include "editormanager.h"
#include <QtCore/QDebug>
#include <QtCore/QTextCodec>
/*!
\class Core::TextFile
\brief Base class for text files with encoding helpers.
Stores the format obtained from read operations and uses that when writing
out files, thus ensuring that CRLF/encodings are preserved.
\sa Utils::TextFileUtils
*/
enum { debug = 0 };
namespace Core {
namespace Internal {
class TextFilePrivate
{
public:
TextFilePrivate() : m_readResult(Utils::TextFileFormat::ReadSuccess) {}
Utils::TextFileFormat m_format;
Utils::TextFileFormat::ReadResult m_readResult;
QByteArray m_decodingErrorSample;
};
} // namespace Internal
TextFile::TextFile(QObject *parent) :
IFile(parent), d(new Internal::TextFilePrivate)
{
setCodec(Core::EditorManager::instance()->defaultTextCodec());
}
TextFile::~TextFile()
{
delete d;
}
bool TextFile::hasDecodingError() const
{
return d->m_readResult == Utils::TextFileFormat::ReadEncodingError;
}
QByteArray TextFile::decodingErrorSample() const
{
return d->m_decodingErrorSample;
}
/*!
\brief Writes out text using the format obtained from the last read.
*/
bool TextFile::write(const QString &fileName, const QString &data, QString *errorMessage) const
{
return write(fileName, format(), data, errorMessage);
}
/*!
\brief Writes out text using a custom format obtained.
*/
bool TextFile::write(const QString &fileName, const Utils::TextFileFormat &format, const QString &data, QString *errorMessage) const
{
if (debug)
qDebug() << Q_FUNC_INFO << this << fileName;
return format.writeFile(fileName, data, errorMessage);
}
/*!
\brief Autodetect format and read in a text file.
*/
TextFile::ReadResult TextFile::read(const QString &fileName, QStringList *plainTextList, QString *errorString)
{
d->m_readResult =
Utils::TextFileFormat::readFile(fileName, codec(),
plainTextList, &d->m_format, errorString, &d->m_decodingErrorSample);
return d->m_readResult;
}
/*!
\brief Autodetect format and read in a text file.
*/
TextFile::ReadResult TextFile::read(const QString &fileName, QString *plainText, QString *errorString)
{
d->m_readResult =
Utils::TextFileFormat::readFile(fileName, codec(),
plainText, &d->m_format, errorString, &d->m_decodingErrorSample);
return d->m_readResult;
}
const QTextCodec *TextFile::codec() const
{
return d->m_format.codec;
}
void TextFile::setCodec(const QTextCodec *codec)
{
if (debug)
qDebug() << Q_FUNC_INFO << this << (codec ? codec->name() : QByteArray());
d->m_format.codec = codec;
}
/*!
\brief Returns the format obtained from the last call to read().
*/
Utils::TextFileFormat TextFile::format() const
{
return d->m_format;
}
} // namespace Core
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#ifndef CORE_TEXTFILE_H
#define CORE_TEXTFILE_H
#include "ifile.h"
#include <utils/textfileformat.h>
namespace Core {
namespace Internal {
class TextFilePrivate;
}
class CORE_EXPORT TextFile : public IFile
{
Q_OBJECT
public:
typedef Utils::TextFileFormat::ReadResult ReadResult;
explicit TextFile(QObject *parent = 0);
virtual ~TextFile();
Utils::TextFileFormat format() const;
const QTextCodec *codec() const;
void setCodec(const QTextCodec *);
ReadResult read(const QString &fileName, QStringList *plainTextList, QString *errorString);
ReadResult read(const QString &fileName, QString *plainText, QString *errorString);
bool hasDecodingError() const;
QByteArray decodingErrorSample() const;
bool write(const QString &fileName, const QString &data, QString *errorMessage) const;
bool write(const QString &fileName, const Utils::TextFileFormat &format, const QString &data, QString *errorMessage) const;
private:
Internal::TextFilePrivate *d;
};
} // namespace Core
#endif // CORE_TEXTFILE_H
......@@ -157,19 +157,19 @@ bool FormWindowEditor::open(QString *errorString, const QString &fileName, const
const QFileInfo fi(fileName);
const QString absfileName = fi.absoluteFilePath();
Utils::FileReader reader;
if (!reader.fetch(realFileName, QIODevice::Text, errorString))
QString contents;
if (d->m_file.read(absfileName, &contents, errorString) != Utils::TextFileFormat::ReadSuccess)
return false;
form->setFileName(absfileName);
QByteArray contents = reader.data();
#if QT_VERSION >= 0x050000
QBuffer str(&contents);
const QByteArray contentsBA = contents.toUtf8();
QBuffer str(&contentsBA);
str.open(QIODevice::ReadOnly);
if (!form->setContents(&str, errorString))
return false;
#else
form->setContents(QString::fromUtf8(contents));
form->setContents(contents);
if (!form->mainContainer())
return false;
#endif
......
......@@ -53,16 +53,19 @@
#include <QtCore/QFileInfo>
#include <QtCore/QByteArray>
#include <QtCore/QDebug>
#include <QtCore/QTextCodec>
namespace Designer {
namespace Internal {
FormWindowFile::FormWindowFile(QDesignerFormWindowInterface *form, QObject *parent)
: Core::IFile(parent),
: Core::TextFile(parent),
m_mimeType(QLatin1String(Designer::Constants::FORM_MIMETYPE)),
m_shouldAutoSave(false),
m_formWindow(form)
{
// Designer needs UTF-8 regardless of settings.
setCodec(QTextCodec::codecForName("UTF-8"));
connect(m_formWindow->core()->formWindowManager(), SIGNAL(formWindowRemoved(QDesignerFormWindowInterface*)),
this, SLOT(slotFormWindowRemoved(QDesignerFormWindowInterface*)));
connect(m_formWindow->commandHistory(), SIGNAL(indexChanged(int)),
......@@ -190,10 +193,7 @@ bool FormWindowFile::writeFile(const QString &fileName, QString *errorString) co
{
if (Designer::Constants::Internal::debug)
qDebug() << Q_FUNC_INFO << m_fileName << fileName;
Utils::FileSaver saver(fileName, QIODevice::Text);
saver.write(m_formWindow->contents().toUtf8());
return saver.finalize(errorString);
return write(fileName, format(), m_formWindow->contents(), errorString);
}
void FormWindowFile::setFileName(const QString &fname)
......
......@@ -33,7 +33,7 @@
#ifndef FORMWINDOWFILE_H
#define FORMWINDOWFILE_H
#include <coreplugin/ifile.h>
#include <coreplugin/textfile.h>
#include <QtCore/QPointer>
......@@ -45,7 +45,7 @@ QT_END_NAMESPACE
namespace Designer {
namespace Internal {
class FormWindowFile : public Core::IFile
class FormWindowFile : public Core::TextFile
{
Q_OBJECT
......
This diff is collapsed.
......@@ -35,7 +35,7 @@
#include "texteditor_global.h"
#include <coreplugin/ifile.h>
#include <coreplugin/textfile.h>
QT_BEGIN_NAMESPACE
class QTextCursor;
......@@ -51,7 +51,7 @@ class ExtraEncodingSettings;
class SyntaxHighlighter;
class BaseTextDocumentPrivate;
class TEXTEDITOR_EXPORT BaseTextDocument : public Core::IFile
class TEXTEDITOR_EXPORT BaseTextDocument : public Core::TextFile
{
Q_OBJECT
......@@ -95,11 +95,6 @@ public:
void setSyntaxHighlighter(SyntaxHighlighter *highlighter);
SyntaxHighlighter *syntaxHighlighter() const;
bool hasDecodingError() const;
QTextCodec *codec() const;
void setCodec(QTextCodec *c);
QByteArray decodingErrorSample() const;
bool reload(QString *errorString, QTextCodec *codec);
void cleanWhitespace(const QTextCursor &cursor);
......
......@@ -5615,7 +5615,8 @@ void BaseTextEditorWidget::setTextCodec(QTextCodec *codec)
QTextCodec *BaseTextEditorWidget::textCodec() const
{
return baseTextDocument()->codec();
// TODO: Fix all QTextCodec usages to be const *.
return const_cast<QTextCodec *>(baseTextDocument()->codec());
}
void BaseTextEditorWidget::setReadOnly(bool b)
......
......@@ -387,7 +387,7 @@ void VCSBaseEditorWidget::setDiffBaseDirectory(const QString &bd)
QTextCodec *VCSBaseEditorWidget::codec() const
{
return baseTextDocument()->codec();
return const_cast<QTextCodec *>(baseTextDocument()->codec());
}
void VCSBaseEditorWidget::setCodec(QTextCodec *c)
......
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