родитель
8d6678009a
Коммит
ef78e74cdd
11
cli/main.cpp
11
cli/main.cpp
@ -56,7 +56,6 @@ Command *commandFromName(QString name, SqliteDbService &dbService)
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Common::setupAppInfo();
|
||||
|
||||
QCoreApplication app(argc, argv);
|
||||
QStringList args = app.arguments();
|
||||
QString argv0 = args.takeFirst();
|
||||
@ -66,6 +65,16 @@ int main(int argc, char *argv[])
|
||||
exit(1);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Common::ensureConfigured();
|
||||
}
|
||||
catch(QSSGeneralException &e)
|
||||
{
|
||||
Logger::error() << "Error: " << e.message;
|
||||
return 1;
|
||||
}
|
||||
|
||||
QString connectionString = Common::databasePath();
|
||||
DatabaseFactory dbFactory(connectionString);
|
||||
SqliteDbService dbService(dbFactory);
|
||||
|
11
gui/main.cpp
11
gui/main.cpp
@ -1,5 +1,6 @@
|
||||
#include <QApplication>
|
||||
#include <QSettings>
|
||||
#include <QMessageBox>
|
||||
#include "mainwindow.h"
|
||||
#include "searchresult.h"
|
||||
#include "pdfpreview.h"
|
||||
@ -9,6 +10,16 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
Common::setupAppInfo();
|
||||
QApplication a(argc, argv);
|
||||
try
|
||||
{
|
||||
Common::ensureConfigured();
|
||||
}
|
||||
catch(QSSGeneralException &e)
|
||||
{
|
||||
qDebug() << e.message;
|
||||
QMessageBox::critical(nullptr, "Error", e.message);
|
||||
return 1;
|
||||
}
|
||||
qRegisterMetaType<QVector<SearchResult>>("QVector<SearchResult>");
|
||||
qRegisterMetaType<QVector<PdfPreview>>("QVector<PdfPreview>");
|
||||
qRegisterMetaType<PdfPreview>("PdfPreview");
|
||||
|
@ -1,7 +1,93 @@
|
||||
#include <QProcessEnvironment>
|
||||
#include <QSettings>
|
||||
#include <QStandardPaths>
|
||||
#include <QDir>
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlError>
|
||||
#include <QTextStream>
|
||||
#include <QDebug>
|
||||
#include "qssgeneralexception.h"
|
||||
#include "common.h"
|
||||
|
||||
#define SETTINGS_KEY_DBPATH "dbpath"
|
||||
#define SETTINGS_KEY_FIRSTRUN "firstrun"
|
||||
|
||||
inline void initResources()
|
||||
{
|
||||
Q_INIT_RESOURCE(create);
|
||||
}
|
||||
|
||||
bool Common::initSqliteDatabase(QString path)
|
||||
{
|
||||
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
|
||||
db.setDatabaseName(path);
|
||||
if(!db.open())
|
||||
{
|
||||
qDebug() << "failed to open database: " << path;
|
||||
return false;
|
||||
}
|
||||
initResources();
|
||||
QFile file(":./create.sql");
|
||||
if(!file.open(QIODevice::ReadOnly))
|
||||
{
|
||||
qDebug() << "Failed to load SQL creation script from embedded resource";
|
||||
return false;
|
||||
}
|
||||
QTextStream stream(&file);
|
||||
db.transaction();
|
||||
while(!stream.atEnd())
|
||||
{
|
||||
QString sql = stream.readLine();
|
||||
qDebug() << sql;
|
||||
QSqlQuery sqlQuery;
|
||||
if(!sqlQuery.exec(sql))
|
||||
{
|
||||
qDebug() << "Failed to execute sql statement while initializing database: " << sqlQuery.lastError();
|
||||
db.rollback();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
db.commit();
|
||||
db.close();
|
||||
file.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
void Common::ensureConfigured()
|
||||
{
|
||||
QSettings settings;
|
||||
QVariant firstRun = settings.value(SETTINGS_KEY_FIRSTRUN);
|
||||
if(!firstRun.isValid())
|
||||
{
|
||||
QString dbpath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
|
||||
QDir dir;
|
||||
if(!dir.exists(dbpath))
|
||||
{
|
||||
if(!dir.mkpath(dbpath))
|
||||
{
|
||||
throw QSSGeneralException("Failed to create dbpath directory");
|
||||
}
|
||||
}
|
||||
dbpath += "/qss.sqlite";
|
||||
if(!initSqliteDatabase(dbpath))
|
||||
{
|
||||
throw QSSGeneralException("Failed to initialize sqlite database");
|
||||
}
|
||||
settings.setValue(SETTINGS_KEY_FIRSTRUN, false);
|
||||
settings.setValue(SETTINGS_KEY_DBPATH, dbpath);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
QString dbpath = databasePath();
|
||||
if(!QFile::exists(dbpath))
|
||||
{
|
||||
throw QSSGeneralException("Database " + dbpath + " was not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Common::setupAppInfo()
|
||||
{
|
||||
QCoreApplication::setOrganizationName("quitesimple.org");
|
||||
@ -15,7 +101,7 @@ QString Common::databasePath()
|
||||
if(env == "")
|
||||
{
|
||||
QSettings settings;
|
||||
return settings.value("dbpath").toString();
|
||||
return settings.value(SETTINGS_KEY_DBPATH).toString();
|
||||
}
|
||||
return env;
|
||||
}
|
||||
|
@ -6,5 +6,7 @@ namespace Common
|
||||
{
|
||||
void setupAppInfo();
|
||||
QString databasePath();
|
||||
bool initSqliteDatabase(QString path);
|
||||
void ensureConfigured();
|
||||
} // namespace Common
|
||||
#endif
|
||||
|
Загрузка…
Ссылка в новой задаче
Block a user