Move check for binary data into separate function

There are two places in the code where we check for binary data in
database cells. This commit takes the code and moves it into a separate
function so it's easier to improve the situation in both function
simultaneously.
This commit is contained in:
Martin Kleusberg
2017-12-31 15:25:01 +01:00
parent 7ce7f0c05b
commit 5562119563
6 changed files with 40 additions and 8 deletions
+17
View File
@@ -0,0 +1,17 @@
#include "Data.h"
#include <QTextCodec>
bool isTextOnly(QByteArray data, const QString& encoding, bool quickTest)
{
// Truncate to the first couple of bytes for quick testing
if(quickTest)
data = data.left(512);
// Convert to Unicode if necessary
if(!encoding.isEmpty())
data = QTextCodec::codecForName(encoding.toUtf8())->toUnicode(data).toUtf8();
// Perform check
return QString(data).toUtf8() == data;
}
+12
View File
@@ -0,0 +1,12 @@
#ifndef DATA_H
#define DATA_H
#include <QString>
// This returns false if the data in the data parameter contains binary data. If it is text only, the function returns
// true. If the second parameter is specified, it will be used to convert the data from the given encoding to Unicode
// before doing the check. The third parameter can be used to only check the first couple of bytes which speeds up the
// text but makes it less reliable
bool isTextOnly(QByteArray data, const QString& encoding = QString(), bool quickTest = false);
#endif
+3 -2
View File
@@ -4,6 +4,7 @@
#include "Settings.h"
#include "src/qhexedit.h"
#include "FileDialog.h"
#include "Data.h"
#include <QMainWindow>
#include <QKeySequence>
@@ -619,8 +620,8 @@ int EditDialog::checkDataType(const QByteArray& data)
return Image;
// Check if it's text only
if (QString(cellData).toUtf8() == cellData) { // Is there a better way to check this?
if(isTextOnly(cellData))
{
QJsonDocument jsonDoc = QJsonDocument::fromJson(QString(cellData).toUtf8());
if (!jsonDoc.isNull())
return JSON;
+2 -4
View File
@@ -2,6 +2,7 @@
#include "sqlitedb.h"
#include "sqlite.h"
#include "Settings.h"
#include "Data.h"
#include <QDebug>
#include <QMessageBox>
@@ -871,10 +872,7 @@ void SqliteTableModel::clearCache()
bool SqliteTableModel::isBinary(const QModelIndex& index) const
{
// We're using the same way to detect binary data here as in the EditDialog class. For performance reasons we're only looking at
// the first couple of bytes though.
QByteArray data = decode(m_data.at(index.row()).at(index.column()).left(512));
return QString(data).toUtf8() != data;
return !isTextOnly(m_data.at(index.row()).at(index.column()), m_encoding, true);
}
QByteArray SqliteTableModel::encode(const QByteArray& str) const
+4 -2
View File
@@ -61,7 +61,8 @@ HEADERS += \
RemotePushDialog.h \
jsontextedit.h \
FindReplaceDialog.h \
ExtendedScintilla.h
ExtendedScintilla.h \
Data.h
SOURCES += \
sqlitedb.cpp \
@@ -100,7 +101,8 @@ SOURCES += \
RemotePushDialog.cpp \
jsontextedit.cpp \
FindReplaceDialog.cpp \
ExtendedScintilla.cpp
ExtendedScintilla.cpp \
Data.cpp
RESOURCES += icons/icons.qrc \
translations/flags/flags.qrc \