mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-04-27 23:52:34 -05:00
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:
@@ -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
@@ -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
@@ -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,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
@@ -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 \
|
||||
|
||||
Reference in New Issue
Block a user