From bb4824764cc871abddf558876485f41c2ed71302 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sat, 23 May 2020 22:52:42 +0200 Subject: [PATCH] added Common namespace. cli: use settings instead of env for db path --- cli/main.cpp | 99 +++++++++++++++++++++++++---------------------- gui/main.cpp | 22 +++++------ shared/common.cpp | 8 ++++ shared/common.h | 9 +++++ shared/shared.pro | 6 ++- 5 files changed, 84 insertions(+), 60 deletions(-) create mode 100644 shared/common.cpp create mode 100644 shared/common.h diff --git a/cli/main.cpp b/cli/main.cpp index 4bcd6b5..bae01f6 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include "encodingdetector.h" @@ -23,64 +24,68 @@ #include "commandsearch.h" #include "databasefactory.h" #include "logger.h" +#include "../shared/common.h" + void printUsage(QString argv0) { - qInfo() << "Usage: " << argv0 << "command"; + qInfo() << "Usage: " << argv0 << "command"; } Command *commandFromName(QString name, SqliteDbService &dbService) { - if(name == "add") - { - return new CommandAdd(dbService); - } - if(name == "delete") - { - return new CommandDelete(dbService); - } - if(name == "update") - { - return new CommandUpdate(dbService); - } - if(name == "search") - { - return new CommandSearch(dbService); - } + if(name == "add") + { + return new CommandAdd(dbService); + } + if(name == "delete") + { + return new CommandDelete(dbService); + } + if(name == "update") + { + return new CommandUpdate(dbService); + } + if(name == "search") + { + return new CommandSearch(dbService); + } - return nullptr; + return nullptr; } int main(int argc, char *argv[]) { - QCoreApplication app(argc, argv); + Common::setupAppInfo(); - QStringList args = app.arguments(); - QString argv0 = args.takeFirst(); - if(args.length() < 1) - { - printUsage(argv0); - exit(1); - } + QCoreApplication app(argc, argv); + QStringList args = app.arguments(); + QString argv0 = args.takeFirst(); + if(args.length() < 1) + { + printUsage(argv0); + exit(1); + } - QString commandName = args.first(); - QString connectionString = QProcessEnvironment::systemEnvironment().value("QSS_PATH"); - DatabaseFactory dbFactory(connectionString); - SqliteDbService dbService(dbFactory); - Command *cmd = commandFromName(commandName, dbService); - if(cmd != nullptr) - { - try - { - return cmd->handle(args); - } - catch(const QSSGeneralException &e) - { - Logger::error() << "Exception caught, message: " << e.message << endl; - } - } - else - { - Logger::error() << "Unknown command " << commandName << endl; - } - return 1; + QString commandName = args.first(); + QSettings settings; + QString connectionString = settings.value("dbpath").toString(); + DatabaseFactory dbFactory(connectionString); + SqliteDbService dbService(dbFactory); + Command *cmd = commandFromName(commandName, dbService); + if(cmd != nullptr) + { + try + { + return cmd->handle(args); + } + catch(const QSSGeneralException &e) + { + Logger::error() << "Exception caught, message: " << e.message << endl; + } + } + else + { + Logger::error() << "Unknown command " << commandName << endl; + } + return 1; } diff --git a/gui/main.cpp b/gui/main.cpp index 4a960fe..c093291 100644 --- a/gui/main.cpp +++ b/gui/main.cpp @@ -1,19 +1,19 @@ -#include "mainwindow.h" #include #include +#include "mainwindow.h" #include "searchresult.h" #include "pdfpreview.h" +#include "../shared/common.h" + int main(int argc, char *argv[]) { - QCoreApplication::setOrganizationName("quitesimple.org"); - QCoreApplication::setOrganizationDomain("quitesimple.org"); - QCoreApplication::setApplicationName("qss"); - QApplication a(argc, argv); - qRegisterMetaType>("QVector"); - qRegisterMetaType>("QVector"); - qRegisterMetaType("PdfPreview"); - MainWindow w; - w.showMaximized(); + Common::setupAppInfo(); + QApplication a(argc, argv); + qRegisterMetaType>("QVector"); + qRegisterMetaType>("QVector"); + qRegisterMetaType("PdfPreview"); + MainWindow w; + w.showMaximized(); - return a.exec(); + return a.exec(); } diff --git a/shared/common.cpp b/shared/common.cpp new file mode 100644 index 0000000..60fec1a --- /dev/null +++ b/shared/common.cpp @@ -0,0 +1,8 @@ +#include "common.h" + +void Common::setupAppInfo() +{ + QCoreApplication::setOrganizationName("quitesimple.org"); + QCoreApplication::setOrganizationDomain("quitesimple.org"); + QCoreApplication::setApplicationName("qss"); +} diff --git a/shared/common.h b/shared/common.h new file mode 100644 index 0000000..c6ec556 --- /dev/null +++ b/shared/common.h @@ -0,0 +1,9 @@ +#ifndef COMMON_H +#define COMMON_H +#include + +namespace Common +{ + void setupAppInfo(); +} +#endif diff --git a/shared/shared.pro b/shared/shared.pro index 3b53026..40cc3ff 100644 --- a/shared/shared.pro +++ b/shared/shared.pro @@ -26,14 +26,16 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += sqlitesearch.cpp \ qssgeneralexception.cpp \ - qssquery.cpp + qssquery.cpp \ + common.cpp HEADERS += sqlitesearch.h \ filedata.h \ searchresult.h \ qssgeneralexception.h \ token.h \ - qssquery.h + qssquery.h \ + common.h unix { target.path = /usr/lib INSTALLS += target