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
#include "util.h"
#define SQLITE_DB_MAGIC "SQLite format 3"
#ifdef Q_OS_ANDROID
static QString documentName(const QString &path)
@ -156,3 +157,21 @@ const QTemporaryDir &Util::tempDir()
static QTemporaryDir 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 displayName(const QString &path);
const QTemporaryDir &tempDir();
bool isSQLiteDB(const QString &path, QString &errorString);
}
#endif // UTIL_H

View File

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

View File

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

View File

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

View File

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