1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-30 22:51:16 +01:00

Improve SQLite-based maps error reporting

This commit is contained in:
Martin Tůma 2023-04-25 22:52:55 +02:00
parent 1d589e25d8
commit 8cea06f2ad
6 changed files with 41 additions and 3 deletions

View File

@ -10,6 +10,7 @@
#endif // Q_OS_ANDROID #endif // Q_OS_ANDROID
#include "util.h" #include "util.h"
#define SQLITE_DB_MAGIC "SQLite format 3"
#ifdef Q_OS_ANDROID #ifdef Q_OS_ANDROID
static QString documentName(const QString &path) static QString documentName(const QString &path)
@ -156,3 +157,21 @@ const QTemporaryDir &Util::tempDir()
static QTemporaryDir dir; static QTemporaryDir dir;
return dir; return dir;
} }
bool Util::isSQLiteDB(const QString &path, QString &errorString)
{
QFile file(path);
char magic[sizeof(SQLITE_DB_MAGIC)];
if (!file.open(QFile::ReadOnly)) {
errorString = file.errorString();
return false;
}
if ((file.read(magic, sizeof(magic)) < (qint64)sizeof(magic))
|| memcmp(SQLITE_DB_MAGIC, magic, sizeof(SQLITE_DB_MAGIC))) {
errorString = "Not a SQLite database file";
return false;
}
return true;
}

View File

@ -16,6 +16,7 @@ namespace Util
QString file2name(const QString &path); QString file2name(const QString &path);
QString displayName(const QString &path); QString displayName(const QString &path);
const QTemporaryDir &tempDir(); const QTemporaryDir &tempDir();
bool isSQLiteDB(const QString &path, QString &errorString);
} }
#endif // UTIL_H #endif // UTIL_H

View File

@ -1,6 +1,7 @@
#include <QSqlQuery> #include <QSqlQuery>
#include <QSqlRecord> #include <QSqlRecord>
#include <QSqlField> #include <QSqlField>
#include <QSqlError>
#include <QPainter> #include <QPainter>
#include <QPixmapCache> #include <QPixmapCache>
#include <QImageReader> #include <QImageReader>
@ -48,12 +49,15 @@ MBTilesMap::MBTilesMap(const QString &fileName, QObject *parent)
: Map(fileName, parent), _mapRatio(1.0), _tileRatio(1.0), _scalable(false), : Map(fileName, parent), _mapRatio(1.0), _tileRatio(1.0), _scalable(false),
_scaledSize(0), _valid(false) _scaledSize(0), _valid(false)
{ {
if (!Util::isSQLiteDB(fileName, _errorString))
return;
_db = QSqlDatabase::addDatabase("QSQLITE", fileName); _db = QSqlDatabase::addDatabase("QSQLITE", fileName);
_db.setDatabaseName(fileName); _db.setDatabaseName(fileName);
_db.setConnectOptions("QSQLITE_OPEN_READONLY"); _db.setConnectOptions("QSQLITE_OPEN_READONLY");
if (!_db.open()) { if (!_db.open()) {
_errorString = "Error opening database file"; _errorString = _db.lastError().text();
return; return;
} }

View File

@ -413,6 +413,12 @@ OruxMap::OruxMap(const QString &fileName, QObject *parent)
if (dir.exists("OruxMapsImages.db")) { if (dir.exists("OruxMapsImages.db")) {
QString dbFile(dir.absoluteFilePath("OruxMapsImages.db")); QString dbFile(dir.absoluteFilePath("OruxMapsImages.db"));
if (!Util::isSQLiteDB(dbFile, _errorString)) {
_errorString = "OruxMapsImages.db: " + _errorString;
return;
}
_db = QSqlDatabase::addDatabase("QSQLITE", dbFile); _db = QSqlDatabase::addDatabase("QSQLITE", dbFile);
_db.setDatabaseName(dbFile); _db.setDatabaseName(dbFile);
_db.setConnectOptions("QSQLITE_OPEN_READONLY"); _db.setConnectOptions("QSQLITE_OPEN_READONLY");

View File

@ -1,6 +1,7 @@
#include <QSqlQuery> #include <QSqlQuery>
#include <QSqlRecord> #include <QSqlRecord>
#include <QSqlField> #include <QSqlField>
#include <QSqlError>
#include <QPainter> #include <QPainter>
#include <QPixmapCache> #include <QPixmapCache>
#include <QImageReader> #include <QImageReader>
@ -18,12 +19,15 @@ OsmdroidMap::OsmdroidMap(const QString &fileName, QObject *parent)
{ {
quint64 z, l = 0, r = 0, t = 0, b = 0; quint64 z, l = 0, r = 0, t = 0, b = 0;
if (!Util::isSQLiteDB(fileName, _errorString))
return;
_db = QSqlDatabase::addDatabase("QSQLITE", fileName); _db = QSqlDatabase::addDatabase("QSQLITE", fileName);
_db.setDatabaseName(fileName); _db.setDatabaseName(fileName);
_db.setConnectOptions("QSQLITE_OPEN_READONLY"); _db.setConnectOptions("QSQLITE_OPEN_READONLY");
if (!_db.open()) { if (!_db.open()) {
_errorString = "Error opening database file"; _errorString = _db.lastError().text();
return; return;
} }

View File

@ -1,6 +1,7 @@
#include <QSqlQuery> #include <QSqlQuery>
#include <QSqlRecord> #include <QSqlRecord>
#include <QSqlField> #include <QSqlField>
#include <QSqlError>
#include <QPainter> #include <QPainter>
#include <QPixmapCache> #include <QPixmapCache>
#include <QImageReader> #include <QImageReader>
@ -16,12 +17,15 @@
SqliteMap::SqliteMap(const QString &fileName, QObject *parent) SqliteMap::SqliteMap(const QString &fileName, QObject *parent)
: Map(fileName, parent), _mapRatio(1.0), _valid(false) : Map(fileName, parent), _mapRatio(1.0), _valid(false)
{ {
if (!Util::isSQLiteDB(fileName, _errorString))
return;
_db = QSqlDatabase::addDatabase("QSQLITE", fileName); _db = QSqlDatabase::addDatabase("QSQLITE", fileName);
_db.setDatabaseName(fileName); _db.setDatabaseName(fileName);
_db.setConnectOptions("QSQLITE_OPEN_READONLY"); _db.setConnectOptions("QSQLITE_OPEN_READONLY");
if (!_db.open()) { if (!_db.open()) {
_errorString = "Error opening database file"; _errorString = _db.lastError().text();
return; return;
} }