From 36f5545f5a150b60b363a0ffb8d03aac79a9715b Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@nokia.com>
Date: Mon, 12 Jul 2010 14:15:38 +0200
Subject: [PATCH] VCS[git]: Limit text size to be displayed.

in VCSBaseEditor  and BaseTextEditor::createNew to the size
used for limiting file size.

Reviewed-by: Robert Loehning <robert.loehning@nokia.com>
Task-number: QTCREATORBUG-1847
---
 .../coreplugin/editormanager/editormanager.cpp        |  7 ++++++-
 src/plugins/coreplugin/editormanager/editormanager.h  |  2 ++
 src/plugins/texteditor/basetexteditor.cpp             | 11 +++++++++++
 src/plugins/texteditor/basetexteditor.h               |  2 ++
 src/plugins/vcsbase/vcsbaseeditor.cpp                 |  6 +++++-
 5 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp
index ab04d11f7ff..fc3ee7d4e0d 100644
--- a/src/plugins/coreplugin/editormanager/editormanager.cpp
+++ b/src/plugins/coreplugin/editormanager/editormanager.cpp
@@ -1080,7 +1080,7 @@ IEditor *EditorManager::createEditor(const QString &editorId,
             mimeType = m_d->m_core->mimeDatabase()->findByType(QLatin1String("text/plain"));
         }
         // open text files > 48 MB in binary editor
-        if (fileInfo.size() >  qint64(3) << 24 && mimeType.type().startsWith(QLatin1String("text")))
+        if (fileInfo.size() >  maxTextFileSize() && mimeType.type().startsWith(QLatin1String("text")))
             mimeType = m_d->m_core->mimeDatabase()->findByType(QLatin1String("application/octet-stream"));
         factories = editorFactories(mimeType, true);
     } else {
@@ -2013,5 +2013,10 @@ void EditorManager::gotoOtherSplit()
         }
     }
 }
+
+qint64 EditorManager::maxTextFileSize()
+{
+    return (qint64(3) << 24);
+}
 //===================EditorClosingCoreListener======================
 
diff --git a/src/plugins/coreplugin/editormanager/editormanager.h b/src/plugins/coreplugin/editormanager/editormanager.h
index e488f040ea0..541c5d33556 100644
--- a/src/plugins/coreplugin/editormanager/editormanager.h
+++ b/src/plugins/coreplugin/editormanager/editormanager.h
@@ -199,6 +199,8 @@ public:
                                              QWidget *parent,
                                              bool displaySaveAsButton = false);
 
+    static qint64 maxTextFileSize();
+
 signals:
     void currentEditorChanged(Core::IEditor *editor);
     void editorCreated(Core::IEditor *editor, const QString &fileName);
diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index e82160232b3..3283b7b77d7 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -573,8 +573,19 @@ void BaseTextEditor::triggerQuickFix()
     emit requestQuickFix(editableInterface());
 }
 
+QString BaseTextEditor::msgTextTooLarge(quint64 size)
+{
+    return tr("The text is too large to be displayed (%1 MB).").
+           arg(size >> 20);
+}
+
 bool BaseTextEditor::createNew(const QString &contents)
 {
+    if (contents.size() > Core::EditorManager::maxTextFileSize()) {
+        setPlainText(msgTextTooLarge(contents.size()));
+        document()->setModified(false);
+        return false;
+    }
     setPlainText(contents);
     document()->setModified(false);
     return true;
diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h
index afcc2548d04..8a0cb0499ca 100644
--- a/src/plugins/texteditor/basetexteditor.h
+++ b/src/plugins/texteditor/basetexteditor.h
@@ -294,6 +294,8 @@ protected:
     bool canInsertFromMimeData(const QMimeData *source) const;
     void insertFromMimeData(const QMimeData *source);
 
+    static QString msgTextTooLarge(quint64 size);
+
 private:
     void maybeSelectLine();
 
diff --git a/src/plugins/vcsbase/vcsbaseeditor.cpp b/src/plugins/vcsbase/vcsbaseeditor.cpp
index 51865708528..af184590093 100644
--- a/src/plugins/vcsbase/vcsbaseeditor.cpp
+++ b/src/plugins/vcsbase/vcsbaseeditor.cpp
@@ -627,7 +627,11 @@ void VCSBaseEditor::jumpToChangeFromDiff(QTextCursor cursor)
 
 void VCSBaseEditor::setPlainTextData(const QByteArray &data)
 {
-    setPlainText(codec()->toUnicode(data));
+    if (data.size() > Core::EditorManager::maxTextFileSize()) {
+        setPlainText(msgTextTooLarge(data.size()));
+    } else {
+        setPlainText(codec()->toUnicode(data));
+    }
 }
 
 void VCSBaseEditor::setFontSettings(const TextEditor::FontSettings &fs)
-- 
GitLab