From cfa4dee3be69ace34cfdd4c2b4a8accc067bdbe7 Mon Sep 17 00:00:00 2001 From: mgrojo Date: Sun, 21 Feb 2021 15:12:02 +0100 Subject: [PATCH] Import: support importing the single file argument as a CSV file This will allow associating DB4S to CSV files and dropping a CSV file to be importing into a new in-memory database, when there is no DB open yet. See issue #2589 --- src/Application.cpp | 7 +++++-- src/Data.cpp | 12 ++++++++++++ src/Data.h | 3 +++ src/MainWindow.cpp | 4 ++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index 914e4ee7..ee046078 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -127,9 +127,10 @@ Application::Application(int& argc, char** argv) : if(arguments().at(i) == "-h" || arguments().at(i) == "--help") { // Help - qWarning() << qPrintable(QString("%1: %2 [%3] [<%4>|<%5>]\n"). + qWarning() << qPrintable(QString("%1: %2 [%3] [<%4>|<%5>|%6]\n"). arg(tr("Usage")).arg(QFileInfo(argv[0]).fileName()). - arg(tr("options")).arg(tr("database")).arg(tr("project"))); + arg(tr("options")).arg(tr("database")).arg(tr("project")). + arg(tr("csv-file"))); qWarning() << qPrintable(tr("Possible command line arguments:")); printArgument(QString("-h, --help"), @@ -156,6 +157,8 @@ Application::Application(int& argc, char** argv) : tr("Open this SQLite database")); printArgument(QString("<%1>").arg(tr("project")), tr("Open this project file (*.sqbpro)")); + printArgument(QString("<%1>").arg(tr("csv-file")), + tr("Import this CSV file into an in-memory database")); m_showMainWindow = false; } else if(arguments().at(i) == "-v" || arguments().at(i) == "--version") { qWarning() << qPrintable(versionInformation()); diff --git a/src/Data.cpp b/src/Data.cpp index c1715491..7fc5c94a 100644 --- a/src/Data.cpp +++ b/src/Data.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -59,6 +60,17 @@ bool isTextOnly(QByteArray data, const QString& encoding, bool quickTest) } } +bool isTextOnlyFile(const QString& fileName) +{ + QFile file(fileName); + if (file.open(QFile::ReadOnly)) { + QByteArray data = file.read(512); + return isTextOnly(data); + } else { + return false; + } +} + bool containsRightToLeft(const QString& text) { for(QChar ch : text) { diff --git a/src/Data.h b/src/Data.h index 82845886..9479c796 100644 --- a/src/Data.h +++ b/src/Data.h @@ -10,6 +10,9 @@ // text but makes it less reliable bool isTextOnly(QByteArray data, const QString& encoding = QString(), bool quickTest = false); +// Determine if file is a text file reading first 512 bytes and calling isTextOnly(). +bool isTextOnlyFile(const QString& fileName); + // This returns true if text contains some character whose direction is right-to-left. bool containsRightToLeft(const QString& text); diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index cebd1f8c..3365bc25 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -530,6 +530,10 @@ bool MainWindow::fileOpen(const QString& fileName, bool openFromProject, bool re if(loadProject(wFile, readOnly)) { retval = true; + } else if(isTextOnlyFile(wFile)) { + // If it's a text file, cannot be an SQLite/SQLCipher database, + // so try to import it as CSV. + importCSVfiles({wFile}); } else { // Close the database. If the user didn't want to close it, though, stop here if (db.isOpen())