Issue #77 Factor out parts of CDirectoryUtils into CSwiftDirectories

CDirectoryUtils is utilities for manipulating directories.
CSwiftDirectories is getters that return directories used by swift.
This commit is contained in:
Mat Sutcliffe
2020-08-24 22:04:10 +01:00
parent 802576eda9
commit 8e065a8fef
61 changed files with 880 additions and 803 deletions

View File

@@ -15,6 +15,7 @@
#include "blackmisc/db/datastoreutility.h"
#include "blackmisc/network/networkutils.h"
#include "blackmisc/network/entityflags.h"
#include "blackmisc/swiftdirectories.h"
#include "blackmisc/directoryutils.h"
#include "blackmisc/logcategory.h"
#include "blackmisc/logcategorylist.h"
@@ -621,14 +622,14 @@ namespace BlackCore
if (inBackground || !CThreadUtils::isInThisThread(this))
{
const bool s = this->readFromJsonFilesInBackground(CDirectoryUtils::staticDbFilesDirectory(), entities, overrideNewerOnly);
const bool s = this->readFromJsonFilesInBackground(CSwiftDirectories::staticDbFilesDirectory(), entities, overrideNewerOnly);
return s ?
CStatusMessage(this).info(u"Started reading in background from '%1' of entities: '%2'") << CDirectoryUtils::staticDbFilesDirectory() << CEntityFlags::flagToString(entities) :
CStatusMessage(this).error(u"Starting reading in background from '%1' of entities: '%2' failed") << CDirectoryUtils::staticDbFilesDirectory() << CEntityFlags::flagToString(entities);
CStatusMessage(this).info(u"Started reading in background from '%1' of entities: '%2'") << CSwiftDirectories::staticDbFilesDirectory() << CEntityFlags::flagToString(entities) :
CStatusMessage(this).error(u"Starting reading in background from '%1' of entities: '%2' failed") << CSwiftDirectories::staticDbFilesDirectory() << CEntityFlags::flagToString(entities);
}
else
{
return this->readFromJsonFiles(CDirectoryUtils::staticDbFilesDirectory(), entities, overrideNewerOnly);
return this->readFromJsonFiles(CSwiftDirectories::staticDbFilesDirectory(), entities, overrideNewerOnly);
}
}
//! \endcond

View File

@@ -687,38 +687,42 @@ namespace BlackCore
return true;
}
bool CIcaoDataReader::writeToJsonFiles(const QString &dir) const
bool CIcaoDataReader::writeToJsonFiles(const QString &dir)
{
QDir directory(dir);
if (!directory.exists()) { return false; }
QList<QPair<CEntityFlags::EntityFlag, QString>> fileContents;
if (this->getCountriesCount() > 0)
{
const QString json(QJsonDocument(this->getCountries().toJson()).toJson());
const bool s = CFileUtils::writeStringToFileInBackground(json, CFileUtils::appendFilePaths(directory.absolutePath(), CDbInfo::entityToSharedName(CEntityFlags::CountryEntity)));
if (!s) { return false; }
fileContents.push_back({ CEntityFlags::CountryEntity, json });
}
if (this->getAircraftIcaoCodesCount() > 0)
{
const QString json(QJsonDocument(this->getAircraftIcaoCodes().toJson()).toJson());
const bool s = CFileUtils::writeStringToFileInBackground(json, CFileUtils::appendFilePaths(directory.absolutePath(), CDbInfo::entityToSharedName(CEntityFlags::AircraftIcaoEntity)));
if (!s) { return false; }
fileContents.push_back({ CEntityFlags::AircraftIcaoEntity, json });
}
if (this->getAirlineIcaoCodesCount() > 0)
{
const QString json(QJsonDocument(this->getAirlineIcaoCodes().toJson()).toJson());
const bool s = CFileUtils::writeStringToFileInBackground(json, CFileUtils::appendFilePaths(directory.absolutePath(), CDbInfo::entityToSharedName(CEntityFlags::AirlineIcaoEntity)));
if (!s) { return false; }
fileContents.push_back({ CEntityFlags::AirlineIcaoEntity, json });
}
if (this->getAircraftCategoryCount() > 0)
{
const QString json(QJsonDocument(this->getAirlineIcaoCodes().toJson()).toJson());
const bool s = CFileUtils::writeStringToFileInBackground(json, CFileUtils::appendFilePaths(directory.absolutePath(), CDbInfo::entityToSharedName(CEntityFlags::AircraftCategoryEntity)));
if (!s) { return false; }
fileContents.push_back({ CEntityFlags::AircraftCategoryEntity, json });
}
for (const auto &pair : fileContents)
{
CWorker::fromTask(this, Q_FUNC_INFO, [pair, directory]
{
CFileUtils::writeStringToFile(CFileUtils::appendFilePaths(directory.absolutePath(), CDbInfo::entityToSharedName(pair.first)), pair.second);
});
}
return true;
}

View File

@@ -140,7 +140,7 @@ namespace BlackCore
bool areAllDataRead() const;
//! Write to static DB data file
bool writeToJsonFiles(const QString &dir) const;
bool writeToJsonFiles(const QString &dir);
// data read from local data
virtual BlackMisc::CStatusMessageList readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead, bool overrideNewerOnly) override;

View File

@@ -599,29 +599,35 @@ namespace BlackCore
return true;
}
bool CModelDataReader::writeToJsonFiles(const QString &dir) const
bool CModelDataReader::writeToJsonFiles(const QString &dir)
{
QDir directory(dir);
if (!directory.exists()) { return false; }
QList<QPair<QString, QString>> fileContents;
if (this->getLiveriesCount() > 0)
{
const QString json(QJsonDocument(this->getLiveries().toJson()).toJson());
const bool s = CFileUtils::writeStringToFileInBackground(json, CFileUtils::appendFilePaths(directory.absolutePath(), "liveries.json"));
if (!s) { return false; }
fileContents.push_back({ "liveries.json", json });
}
if (this->getModelsCount() > 0)
{
const QString json(QJsonDocument(this->getModels().toJson()).toJson());
const bool s = CFileUtils::writeStringToFileInBackground(json, CFileUtils::appendFilePaths(directory.absolutePath(), "models.json"));
if (!s) { return false; }
fileContents.push_back({ "models.json", json });
}
if (this->getDistributorsCount() > 0)
{
const QString json(QJsonDocument(this->getDistributors().toJson()).toJson());
const bool s = CFileUtils::writeStringToFileInBackground(json, CFileUtils::appendFilePaths(directory.absolutePath(), "distributors.json"));
if (!s) { return false; }
fileContents.push_back({ "distributors.json", json });
}
for (const auto &pair : fileContents)
{
CWorker::fromTask(this, Q_FUNC_INFO, [pair, directory]
{
CFileUtils::writeStringToFile(CFileUtils::appendFilePaths(directory.absolutePath(), pair.first), pair.second);
});
}
return true;
}

View File

@@ -134,7 +134,7 @@ namespace BlackCore
bool areAllDataRead() const;
//! Write to JSON file
bool writeToJsonFiles(const QString &dir) const;
bool writeToJsonFiles(const QString &dir);
// Data read from local data
virtual BlackMisc::CStatusMessageList readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead, bool overrideNewerOnly) override;