added Common namespace. cli: use settings instead of env for db path

This commit is contained in:
Albert S. 2020-05-23 22:52:42 +02:00
parent 2161c21e4d
commit bb4824764c
5 changed files with 84 additions and 60 deletions

View File

@ -10,6 +10,7 @@
#include <QSqlError> #include <QSqlError>
#include <QMap> #include <QMap>
#include <QDebug> #include <QDebug>
#include <QSettings>
#include <functional> #include <functional>
#include <exception> #include <exception>
#include "encodingdetector.h" #include "encodingdetector.h"
@ -23,64 +24,68 @@
#include "commandsearch.h" #include "commandsearch.h"
#include "databasefactory.h" #include "databasefactory.h"
#include "logger.h" #include "logger.h"
#include "../shared/common.h"
void printUsage(QString argv0) void printUsage(QString argv0)
{ {
qInfo() << "Usage: " << argv0 << "command"; qInfo() << "Usage: " << argv0 << "command";
} }
Command *commandFromName(QString name, SqliteDbService &dbService) Command *commandFromName(QString name, SqliteDbService &dbService)
{ {
if(name == "add") if(name == "add")
{ {
return new CommandAdd(dbService); return new CommandAdd(dbService);
} }
if(name == "delete") if(name == "delete")
{ {
return new CommandDelete(dbService); return new CommandDelete(dbService);
} }
if(name == "update") if(name == "update")
{ {
return new CommandUpdate(dbService); return new CommandUpdate(dbService);
} }
if(name == "search") if(name == "search")
{ {
return new CommandSearch(dbService); return new CommandSearch(dbService);
} }
return nullptr; return nullptr;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QCoreApplication app(argc, argv); Common::setupAppInfo();
QStringList args = app.arguments(); QCoreApplication app(argc, argv);
QString argv0 = args.takeFirst(); QStringList args = app.arguments();
if(args.length() < 1) QString argv0 = args.takeFirst();
{ if(args.length() < 1)
printUsage(argv0); {
exit(1); printUsage(argv0);
} exit(1);
}
QString commandName = args.first(); QString commandName = args.first();
QString connectionString = QProcessEnvironment::systemEnvironment().value("QSS_PATH"); QSettings settings;
DatabaseFactory dbFactory(connectionString); QString connectionString = settings.value("dbpath").toString();
SqliteDbService dbService(dbFactory); DatabaseFactory dbFactory(connectionString);
Command *cmd = commandFromName(commandName, dbService); SqliteDbService dbService(dbFactory);
if(cmd != nullptr) Command *cmd = commandFromName(commandName, dbService);
{ if(cmd != nullptr)
try {
{ try
return cmd->handle(args); {
} return cmd->handle(args);
catch(const QSSGeneralException &e) }
{ catch(const QSSGeneralException &e)
Logger::error() << "Exception caught, message: " << e.message << endl; {
} Logger::error() << "Exception caught, message: " << e.message << endl;
} }
else }
{ else
Logger::error() << "Unknown command " << commandName << endl; {
} Logger::error() << "Unknown command " << commandName << endl;
return 1; }
return 1;
} }

View File

@ -1,19 +1,19 @@
#include "mainwindow.h"
#include <QApplication> #include <QApplication>
#include <QSettings> #include <QSettings>
#include "mainwindow.h"
#include "searchresult.h" #include "searchresult.h"
#include "pdfpreview.h" #include "pdfpreview.h"
#include "../shared/common.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QCoreApplication::setOrganizationName("quitesimple.org"); Common::setupAppInfo();
QCoreApplication::setOrganizationDomain("quitesimple.org"); QApplication a(argc, argv);
QCoreApplication::setApplicationName("qss"); qRegisterMetaType<QVector<SearchResult>>("QVector<SearchResult>");
QApplication a(argc, argv); qRegisterMetaType<QVector<PdfPreview>>("QVector<PdfPreview>");
qRegisterMetaType<QVector<SearchResult>>("QVector<SearchResult>"); qRegisterMetaType<PdfPreview>("PdfPreview");
qRegisterMetaType<QVector<PdfPreview>>("QVector<PdfPreview>"); MainWindow w;
qRegisterMetaType<PdfPreview>("PdfPreview"); w.showMaximized();
MainWindow w;
w.showMaximized();
return a.exec(); return a.exec();
} }

8
shared/common.cpp Normal file
View File

@ -0,0 +1,8 @@
#include "common.h"
void Common::setupAppInfo()
{
QCoreApplication::setOrganizationName("quitesimple.org");
QCoreApplication::setOrganizationDomain("quitesimple.org");
QCoreApplication::setApplicationName("qss");
}

9
shared/common.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef COMMON_H
#define COMMON_H
#include <QCoreApplication>
namespace Common
{
void setupAppInfo();
}
#endif

View File

@ -26,14 +26,16 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += sqlitesearch.cpp \ SOURCES += sqlitesearch.cpp \
qssgeneralexception.cpp \ qssgeneralexception.cpp \
qssquery.cpp qssquery.cpp \
common.cpp
HEADERS += sqlitesearch.h \ HEADERS += sqlitesearch.h \
filedata.h \ filedata.h \
searchresult.h \ searchresult.h \
qssgeneralexception.h \ qssgeneralexception.h \
token.h \ token.h \
qssquery.h qssquery.h \
common.h
unix { unix {
target.path = /usr/lib target.path = /usr/lib
INSTALLS += target INSTALLS += target