155 lines
4.6 KiB
C++
155 lines
4.6 KiB
C++
#include "genericcore.h"
|
|
|
|
#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 "model/metadata.h"
|
|
#include "model/tablemodel.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();
|
|
}
|
|
|
|
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() {
|
|
// TODO include cleaness of undo stack
|
|
// if (!m_undoStack->isClean()) {
|
|
// saveItems();
|
|
// }
|
|
// QStringList args("update componentA componentB");
|
|
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; }
|
|
|
|
/**
|
|
* 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, ITEM_FILE_NAME);
|
|
if (successfulSave) {
|
|
// QStringList completedTaskStrings = m_model->completedTasks();
|
|
// appendCompletedTasksToFile(completedTaskStrings, "completed.txt");
|
|
m_modelUndoStack->setClean();
|
|
emit displayStatusMessage(QString("Items saved."));
|
|
} else {
|
|
emit displayStatusMessage(QString("Error: Items couldn't be saved."));
|
|
}
|
|
}
|
|
|
|
void GenericCore::setupModels() {
|
|
m_mainModel = make_shared<TableModel>(m_modelUndoStack, this);
|
|
// TODO add QAbstractItemModelTester
|
|
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(ITEM_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;
|
|
}
|