285 lines
9.7 KiB
C++
285 lines
9.7 KiB
C++
#include "genericcore.h"
|
|
|
|
#include <QAbstractItemModelTester>
|
|
#include <QCoreApplication>
|
|
#include <QDateTime>
|
|
#include <QDebug>
|
|
#include <QJsonDocument>
|
|
#include <QProcess>
|
|
#include <QSettings>
|
|
#include <QString>
|
|
|
|
#include "../../ApplicationConfig.h"
|
|
#include "CoreConfig.h"
|
|
#include "constants.h"
|
|
#include "data/filehandler.h"
|
|
#include "data/settingshandler.h"
|
|
#include "model/generalsortfiltermodel.h"
|
|
#include "model/metadata.h"
|
|
#include "model/tablemodel.h"
|
|
#include "network/servercommunicator.h"
|
|
|
|
#include <QtGui/QUndoStack>
|
|
|
|
using namespace std;
|
|
|
|
GenericCore::GenericCore() {
|
|
qDebug() << "Creating core...";
|
|
|
|
QCoreApplication::setOrganizationName("Working-Copy Collective");
|
|
QCoreApplication::setOrganizationDomain("working-copy.org");
|
|
|
|
#ifdef QT_DEBUG
|
|
QCoreApplication::setApplicationName(QString(APPLICATION_NAME) + "-dev");
|
|
#else
|
|
QCoreApplication::setApplicationName(QString(APPLICATION_NAME));
|
|
#endif
|
|
|
|
// TODO let the model own its undo stack (& use TableModel::getUndoStack() if necessary)
|
|
m_modelUndoStack = new QUndoStack(this);
|
|
|
|
setupModels();
|
|
setupServerConfiguration();
|
|
}
|
|
|
|
GenericCore::~GenericCore() { qDebug() << "Destroying core..."; }
|
|
|
|
QString GenericCore::toString() const {
|
|
return QString("%1 (Version %2)").arg(CORE_NAME).arg(CORE_VERSION);
|
|
}
|
|
|
|
void GenericCore::sayHello() const { qDebug() << "Hello from the core!"; }
|
|
|
|
bool GenericCore::isApplicationUpdateAvailable() {
|
|
QProcess process;
|
|
const QString programmString = getMaintenanceToolFilePath();
|
|
const QStringList checkArgs("--checkupdates");
|
|
process.start(programmString, checkArgs);
|
|
process.waitForFinished();
|
|
|
|
const int exitCode = process.exitCode();
|
|
if (process.error() != QProcess::UnknownError) {
|
|
qDebug() << "Error checking for updates";
|
|
emit displayStatusMessage("Error checking for updates");
|
|
return false;
|
|
}
|
|
|
|
QByteArray data = process.readAllStandardOutput();
|
|
|
|
QSettings settings;
|
|
settings.beginGroup("Application");
|
|
settings.setValue("lastCheckForUpdate", QDateTime::currentDateTimeUtc());
|
|
settings.endGroup();
|
|
settings.sync();
|
|
|
|
if (data.isEmpty() || data.contains("currently no updates")) {
|
|
qInfo() << "No updates available";
|
|
emit displayStatusMessage("No updates available.");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void GenericCore::triggerApplicationUpdate(const bool saveChanges) {
|
|
if (saveChanges && !m_modelUndoStack->isClean()) {
|
|
saveItems();
|
|
}
|
|
QStringList args("--start-updater");
|
|
QString toolFilePath = getMaintenanceToolFilePath();
|
|
QProcess::startDetached(toolFilePath, args);
|
|
}
|
|
|
|
QUndoStack* GenericCore::getModelUndoStack() const { return m_modelUndoStack; }
|
|
|
|
std::shared_ptr<TableModel> GenericCore::getModel() const { return m_mainModel; }
|
|
|
|
std::shared_ptr<GeneralSortFilterModel> GenericCore::getSortFilterModel() const {
|
|
return m_sortFilterModel;
|
|
}
|
|
|
|
/**
|
|
* Save items to default file (in standard location).
|
|
* @brief GenericCore::saveItems Saves item fo file.
|
|
*/
|
|
void GenericCore::saveItems() {
|
|
qDebug() << "saving items...";
|
|
|
|
const QJsonDocument doc = m_mainModel->getAllItemsAsJsonDoc();
|
|
const bool successfulSave = FileHandler::saveToFile(doc, ITEMS_FILE_NAME);
|
|
if (successfulSave) {
|
|
m_modelUndoStack->setClean();
|
|
emit displayStatusMessage(QString("Items saved."));
|
|
} else {
|
|
emit displayStatusMessage(QString("Error: Items couldn't be saved."));
|
|
}
|
|
}
|
|
|
|
void GenericCore::importCSVFile(const QString& filePath) {
|
|
qInfo() << "importing items from CSV...";
|
|
qDebug() << "filePath:" << filePath;
|
|
const QList<ModelItemValues> itemValuesList = FileHandler::getItemValuesFromCSVFile(filePath);
|
|
if (itemValuesList.isEmpty()) {
|
|
qDebug() << "No items found. Doing nothing...";
|
|
displayStatusMessage("No items found in CSV file. Either empty or not compatible.");
|
|
return;
|
|
}
|
|
// qDebug() << "CSV file content:" << itemValuesList;
|
|
m_mainModel->insertItems(m_mainModel->rowCount(), itemValuesList);
|
|
const QString messageString =
|
|
QString(tr("Imported %1 item(s) from CSV file.")).arg(itemValuesList.size());
|
|
displayStatusMessage(messageString);
|
|
}
|
|
|
|
bool GenericCore::exportCSVFile(const QString& filePath) {
|
|
qInfo() << "exporting items to CSV...";
|
|
qDebug() << "filePath:" << filePath;
|
|
const QList<QStringList> itemsAsStringLists = m_mainModel->getItemsAsStringLists();
|
|
return FileHandler::exportToCSVFile(itemsAsStringLists, filePath);
|
|
}
|
|
|
|
QVariantMap GenericCore::getSettings(QString group) const {
|
|
return SettingsHandler::getSettings(group);
|
|
}
|
|
|
|
void GenericCore::applySettings(QVariantMap settingMap, QString group) {
|
|
SettingsHandler::saveSettings(settingMap, group);
|
|
|
|
if (group == "Server") {
|
|
setupServerConfiguration();
|
|
}
|
|
}
|
|
|
|
bool GenericCore::isSyncServerSetup() const {
|
|
if (m_serverCommunicator) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void GenericCore::onSendItemTriggered(const QByteArray& jsonData) {
|
|
m_serverCommunicator->postItems(jsonData);
|
|
}
|
|
|
|
void GenericCore::onItemsFetched(const QByteArray jsonData) {
|
|
emit displayStatusMessage("New items fetched.");
|
|
// TODO ? check compability of JSON structure beforehand?
|
|
// NEXT check if item already exists and apply changes (UUID,...) ? ;
|
|
m_mainModel->appendItems(jsonData);
|
|
}
|
|
|
|
void GenericCore::onItemsFetchFailure(const QString errorString) {
|
|
emit displayStatusMessage(QString("Error: %1").arg(errorString));
|
|
}
|
|
|
|
void GenericCore::onPostRequestSuccessful(const QByteArray responseData) {
|
|
const QString message = m_mainModel->updateItemsFromJson(responseData);
|
|
emit displayStatusMessage(message);
|
|
}
|
|
|
|
void GenericCore::onPostRequestFailure(const QString errorString) {
|
|
emit displayStatusMessage(QString("Error: %1").arg(errorString));
|
|
}
|
|
|
|
void GenericCore::onDeleteRequestSuccessful(const QByteArray responseData) {
|
|
qWarning() << "TODO: Process success response!!!";
|
|
}
|
|
|
|
void GenericCore::onDeleteRequestFailure(const QString errorString) {
|
|
qWarning() << "TODO: Process error response!!!";
|
|
}
|
|
|
|
void GenericCore::setupModels() {
|
|
m_mainModel = make_shared<TableModel>(m_modelUndoStack, this);
|
|
m_sortFilterModel = make_shared<GeneralSortFilterModel>(m_mainModel);
|
|
|
|
/// QAbstractItemModelTester
|
|
#ifdef QT_DEBUG
|
|
m_mainModelTester = make_unique<QAbstractItemModelTester>(
|
|
m_mainModel.get(), QAbstractItemModelTester::FailureReportingMode::Fatal);
|
|
m_proxyModelTester = make_unique<QAbstractItemModelTester>(
|
|
m_sortFilterModel.get(), QAbstractItemModelTester::FailureReportingMode::Fatal);
|
|
#else
|
|
m_mainModelTester = make_unique<QAbstractItemModelTester>(
|
|
m_mainModel.get(), QAbstractItemModelTester::FailureReportingMode::Warning);
|
|
m_proxyModelTester = make_unique<QAbstractItemModelTester>(
|
|
m_sortFilterModel.get(), QAbstractItemModelTester::FailureReportingMode::Warning);
|
|
#endif
|
|
|
|
initModelData();
|
|
}
|
|
|
|
/**
|
|
* Initializing model with data. Tries to read items from default file. Generating example items as
|
|
* fallback.
|
|
* @brief GenericCore::initModelData
|
|
*/
|
|
void GenericCore::initModelData() {
|
|
qInfo() << "Trying to read model data from file...";
|
|
const QByteArray jsonDoc = FileHandler::loadJSONDataFromFile(ITEMS_FILE_NAME);
|
|
// qDebug() << "jsonDoc:" << jsonDoc;
|
|
// TODO decide on lack of file(s) (config, data) if example items should be generated
|
|
// (see welcome wizard)
|
|
if (jsonDoc.isEmpty()) {
|
|
qDebug() << "No item content in file. Generating example items...";
|
|
const QByteArray exampleItems = m_mainModel->generateExampleItems();
|
|
m_mainModel->insertItems(0, exampleItems);
|
|
} else {
|
|
qDebug() << "Item in file found.";
|
|
m_mainModel->insertItems(0, jsonDoc);
|
|
}
|
|
m_modelUndoStack->clear();
|
|
}
|
|
|
|
QString GenericCore::getMaintenanceToolFilePath() const {
|
|
QString applicationDirPath = QCoreApplication::applicationDirPath();
|
|
|
|
/// setting the applicationDirPath hard coded to test update feature from IDE
|
|
#ifdef QT_DEBUG
|
|
applicationDirPath = QString("/opt/%1").arg(APPLICATION_NAME);
|
|
#endif
|
|
|
|
#ifdef Q_OS_MAC
|
|
applicationDirPath.append("/../../..");
|
|
#endif
|
|
const QString filePath = applicationDirPath + "/" + UPDATER_EXE;
|
|
return filePath;
|
|
}
|
|
|
|
void GenericCore::setupServerConfiguration() {
|
|
m_serverCommunicator = make_unique<ServerCommunicator>(this);
|
|
/// request connections
|
|
connect(this, &GenericCore::fetchItemsFromServer, m_serverCommunicator.get(),
|
|
&ServerCommunicator::fetchItems);
|
|
connect(this, &GenericCore::postItemToServer, this, &GenericCore::onSendItemTriggered);
|
|
connect(this, &GenericCore::deleteItemFromServer, m_serverCommunicator.get(),
|
|
&ServerCommunicator::deleteItem);
|
|
|
|
/// response connections
|
|
connect(m_serverCommunicator.get(), &ServerCommunicator::itemsFetched, this,
|
|
&GenericCore::onItemsFetched);
|
|
connect(m_serverCommunicator.get(), &ServerCommunicator::itemsFetchFailure, this,
|
|
&GenericCore::onItemsFetchFailure);
|
|
connect(m_serverCommunicator.get(), &ServerCommunicator::postRequestSuccessful, this,
|
|
&GenericCore::onPostRequestSuccessful);
|
|
connect(m_serverCommunicator.get(), &ServerCommunicator::postRequestFailure, this,
|
|
&GenericCore::onPostRequestFailure);
|
|
connect(m_serverCommunicator.get(), &ServerCommunicator::deleteRequestSuccessful, this,
|
|
&GenericCore::onDeleteRequestSuccessful);
|
|
connect(m_serverCommunicator.get(), &ServerCommunicator::deleteRequestFailure, this,
|
|
&GenericCore::onDeleteRequestFailure);
|
|
|
|
applyServerConfiguration();
|
|
}
|
|
|
|
void GenericCore::applyServerConfiguration() {
|
|
const QVariantMap serverSettings = SettingsHandler::getSettings("Server");
|
|
const QString urlValue = serverSettings.value("url").toString();
|
|
if (!urlValue.isEmpty()) {
|
|
const QString emailValue = serverSettings.value("email").toString();
|
|
const QString passwordValue = serverSettings.value("password").toString();
|
|
m_serverCommunicator->setServerConfiguration(urlValue, emailValue, passwordValue);
|
|
}
|
|
}
|