refs #787, utility functions for shared files

* entity to shared file, shared file for DB Info
* CUrl path function
* removed redundant code in CDbInfo::getEntity() and fixed CEntityFlags::singleEntityByName
This commit is contained in:
Klaus Basan
2016-11-03 01:34:02 +01:00
parent cbcb307748
commit dcaa745e7b
7 changed files with 107 additions and 21 deletions

View File

@@ -101,14 +101,14 @@ namespace BlackCore
QString CGlobalSetup::buildBootstrapFileUrl(const QString &candidate)
{
static const QString version(QString(versionString()).append("/"));
static const QString version(QString(CGlobalSetup::versionString()).append("/"));
if (candidate.endsWith("bootstrap.json")) { return candidate; }
CUrl url(candidate);
if (candidate.contains("/bootstrap"))
{
url.appendPath("bootstrap.json");
}
else if (candidate.endsWith(versionString()) || candidate.endsWith(version))
else if (candidate.endsWith(CGlobalSetup::versionString()) || candidate.endsWith(version))
{
url.appendPath("/bootstrap/bootstrap.json");
}
@@ -123,6 +123,26 @@ namespace BlackCore
return url.getFullUrl();
}
CUrl CGlobalSetup::buildDbDataDirectory(const CUrl &candidate)
{
static const QString version(QString(versionString()).append("/"));
if (candidate.pathEndsWith("dbdata") || candidate.pathEndsWith("dbdata/")) { return candidate; }
CUrl url(candidate);
if (candidate.pathEndsWith(versionString()) || candidate.pathEndsWith(version))
{
url.appendPath("/dbdata");
}
else if (candidate.pathEndsWith("shared") || candidate.pathEndsWith("shared/"))
{
url.appendPath(CGlobalSetup::versionString() + "/dbdata/");
}
else
{
url.appendPath("shared/" + CGlobalSetup::versionString() + "/dbdata/");
}
return url;
}
CGlobalSetup CGlobalSetup::fromJsonFile(const QString &fileNameAndPath)
{
CGlobalSetup setup;
@@ -213,6 +233,10 @@ namespace BlackCore
s.append("FSD test servers: ");
s.append(getFsdTestServers().toQString(i18n));
s.append(separator);
s.append("Crash report server: ");
s.append(getCrashReportServerUrl().toQString(i18n));
return s;
}
@@ -242,12 +266,14 @@ namespace BlackCore
return CVariant::fromValue(this->m_vatsimMetarsUrls);
case IndexUpdateInfo:
return CVariant::fromValue(this->getUpdateInfoFileUrls());
case IndexBootstrap:
case IndexBootstrapFileUrls:
return CVariant::fromValue(this->getBootstrapFileUrls());
case IndexSwiftDbFiles:
return CVariant::fromValue(this->getSwiftDbDataFileLocationUrls());
case IndexShared:
case IndexSharedUrls:
return CVariant::fromValue(this->m_sharedUrls);
case IndexCrashReportServerUrl:
return CVariant::fromValue(this->m_crashReportServerUrl);
case IndexWasLoaded:
return CVariant::fromValue(this->m_wasLoaded);
default:
@@ -287,9 +313,12 @@ namespace BlackCore
case IndexVatsimMetars:
this->m_vatsimMetarsUrls = variant.value<CUrlList>();
break;
case IndexShared:
case IndexSharedUrls:
this->m_sharedUrls = variant.value<CUrlList>();
break;
case IndexCrashReportServerUrl:
this->m_crashReportServerUrl = variant.value<CUrl>();
break;
case IndexWasLoaded:
this->m_wasLoaded = variant.toBool();
break;

View File

@@ -49,10 +49,11 @@ namespace BlackCore
IndexVatsimMetars,
IndexVatsimData,
IndexSwiftDbFiles,
IndexBootstrap,
IndexBootstrapFileUrls,
IndexCrashReportServerUrl,
IndexUpdateInfo,
IndexWasLoaded,
IndexShared
IndexSharedUrls
};
//! Default constructor
@@ -83,7 +84,7 @@ namespace BlackCore
bool hasSameType(const CGlobalSetup &otherSetup) const;
//! Crash report server url
BlackMisc::Network::CUrl getCrashreportServerUrl() const { return m_crashreportServerUrl; }
BlackMisc::Network::CUrl getCrashReportServerUrl() const { return m_crashReportServerUrl; }
//! Home page url
BlackMisc::Network::CUrl getDbHomePageUrl() const;
@@ -166,6 +167,9 @@ namespace BlackCore
//! Build bootstrap file URL
static QString buildBootstrapFileUrl(const QString &candidate);
//! Build the full dbdata directory URL
static BlackMisc::Network::CUrl buildDbDataDirectory(const BlackMisc::Network::CUrl &candidate);
//! Object initialized by JSON file
static CGlobalSetup fromJsonFile(const QString &fileNameAndPath);
@@ -174,7 +178,7 @@ namespace BlackCore
int m_dbHttpPort = 80; //!< port
int m_dbHttpsPort = 443; //!< SSL port
bool m_development = false; //!< dev. version?
BlackMisc::Network::CUrl m_crashreportServerUrl; //!< crash report server
BlackMisc::Network::CUrl m_crashReportServerUrl; //!< crash report server
BlackMisc::Network::CUrl m_dbRootDirectoryUrl; //!< Root directory of DB
BlackMisc::Network::CUrl m_vatsimBookingsUrl; //!< ATC bookings
BlackMisc::Network::CUrlList m_vatsimMetarsUrls; //!< METAR data
@@ -191,7 +195,7 @@ namespace BlackCore
CGlobalSetup,
BLACK_METAMEMBER(wasLoaded),
BLACK_METAMEMBER(timestampMSecsSinceEpoch),
BLACK_METAMEMBER(crashreportServerUrl),
BLACK_METAMEMBER(crashReportServerUrl),
BLACK_METAMEMBER(dbRootDirectoryUrl),
BLACK_METAMEMBER(dbHttpPort),
BLACK_METAMEMBER(dbHttpsPort),

View File

@@ -33,15 +33,27 @@ namespace BlackMisc
CEntityFlags::Entity CDbInfo::getEntity() const
{
if (this->m_entity != CEntityFlags::NoEntity) { return this->m_entity; }
if (this->m_tableName.isEmpty()) { return CEntityFlags::NoEntity; }
if (this->m_tableName.contains("airport", Qt::CaseInsensitive)) { return CEntityFlags::AirportEntity; }
if (this->m_tableName.contains("airlineicao", Qt::CaseInsensitive)) { return CEntityFlags::AirlineIcaoEntity; }
if (this->m_tableName.contains("aircrafticao", Qt::CaseInsensitive)) { return CEntityFlags::AircraftIcaoEntity; }
if (this->m_tableName.contains("livery", Qt::CaseInsensitive)) { return CEntityFlags::LiveryEntity; }
if (this->m_tableName.contains("aircraftmodel", Qt::CaseInsensitive)) { return CEntityFlags::ModelEntity; }
if (this->m_tableName.contains("country", Qt::CaseInsensitive)) { return CEntityFlags::CountryEntity; }
if (this->m_tableName.contains("distributor", Qt::CaseInsensitive)) { return CEntityFlags::DistributorEntity; }
return CEntityFlags::NoEntity;
const QString tn(this->getTableName());
return CEntityFlags::singleEntityByName(tn);
}
const QString &CDbInfo::getSharedFileName() const
{
static const QString empty;
CEntityFlags::Entity entity = CEntityFlags::singleEntityByName(this->getTableName());
switch (entity)
{
case CEntityFlags::AircraftIcaoEntity: { return sharedFileNames().at(0); }
case CEntityFlags::AirlineIcaoEntity: { return sharedFileNames().at(1); }
case CEntityFlags::AirportEntity: { return sharedFileNames().at(2); }
case CEntityFlags::CountryEntity: { return sharedFileNames().at(3); }
case CEntityFlags::DistributorEntity: { return sharedFileNames().at(4); }
case CEntityFlags::LiveryEntity: { return sharedFileNames().at(5); }
case CEntityFlags::ModelEntity: { return sharedFileNames().at(6); }
default:
break;
}
return empty;
}
void CDbInfo::setEntity(CEntityFlags::Entity entity)
@@ -142,6 +154,29 @@ namespace BlackMisc
dbInfo.setKeyAndTimestampFromDatabaseJson(json, prefix);
return dbInfo;
}
} // namespace
} // namespace
const QStringList &CDbInfo::sharedFileNames()
{
static const QStringList names({"aircrafticao.json", "airlineicao.json", "airports.json", "countries.json", "distributors.json", "liveries.json", "models.json" });
return names;
}
const QString &CDbInfo::entityToSharedFileName(CEntityFlags::Entity entity)
{
static const QString empty;
switch (entity)
{
case CEntityFlags::AircraftIcaoEntity: return sharedFileNames().at(0);
case CEntityFlags::AirlineIcaoEntity: return sharedFileNames().at(1);
case CEntityFlags::AirportEntity: return sharedFileNames().at(2);
case CEntityFlags::CountryEntity: return sharedFileNames().at(3);
case CEntityFlags::DistributorEntity: return sharedFileNames().at(4);
case CEntityFlags::LiveryEntity: return sharedFileNames().at(5);
case CEntityFlags::ModelEntity: return sharedFileNames().at(6);
default:
break;
}
return empty;
}
} // namespace
} // namespace

View File

@@ -56,6 +56,9 @@ namespace BlackMisc
//! Get entity (based on table name
Network::CEntityFlags::Entity getEntity() const;
//! The shared file name such as "airports.json"
const QString &getSharedFileName() const;
//! Set entity, should be in sync with a corresponding table name
void setEntity(Network::CEntityFlags::Entity entity);
@@ -83,6 +86,12 @@ namespace BlackMisc
//! From our database JSON format
static CDbInfo fromDatabaseJson(const QJsonObject &json, const QString &prefix = QString());
//! The shared file names
static const QStringList &sharedFileNames();
//! Get shared file name
static const QString &entityToSharedFileName(Network::CEntityFlags::Entity entity);
private:
QString m_tableName; //!< table name
int m_entries; //!< number of entries

View File

@@ -144,12 +144,13 @@ namespace BlackMisc
CEntityFlags::Entity CEntityFlags::singleEntityByName(const QString &name)
{
// order here is crucial
if (name.contains("model", Qt::CaseInsensitive)) { return ModelEntity; }
if (name.contains("aircraft", Qt::CaseInsensitive)) { return AircraftIcaoEntity; }
if (name.contains("airline", Qt::CaseInsensitive)) { return AirlineIcaoEntity; }
if (name.contains("airport", Qt::CaseInsensitive)) { return AirportEntity; }
if (name.contains("distributor", Qt::CaseInsensitive)) { return DistributorEntity; }
if (name.contains("countr", Qt::CaseInsensitive)) { return CountryEntity; }
if (name.contains("model", Qt::CaseInsensitive)) { return ModelEntity; }
if (name.contains("liver", Qt::CaseInsensitive)) { return LiveryEntity; }
return NoEntity;
}

View File

@@ -161,6 +161,11 @@ namespace BlackMisc
return url;
}
bool CUrl::pathEndsWith(const QString ending, Qt::CaseSensitivity cs) const
{
return m_path.endsWith(ending, cs);
}
CUrl CUrl::withAppendedQuery(const QString &query) const
{
if (query.isEmpty()) { return *this; }

View File

@@ -133,6 +133,9 @@ namespace BlackMisc
//! Switch protocol
CUrl withSwitchedScheme(const QString &protocol, int port) const;
//! Path ending with?
bool pathEndsWith(const QString ending, Qt::CaseSensitivity cs = Qt::CaseInsensitive) const;
//! \copydoc BlackMisc::Mixin::Index::propertyByIndex
CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const;