Qt_Logger_Example/_include/application_config.cpp

226 lines
6.8 KiB
C++

/****************************************************************************
** Copyright (c) 2025 Evgeny Teterin (nayk) <nayk@nxt.ru>
** All right reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
#include "application_config.h"
#include "config.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QTranslator>
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QStringList>
#include <QtCore/QStandardPaths>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
# include <QtCore/QTextCodec>
#endif
//==============================================================================
void Application::initialize()
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
#endif
#if defined (PROG_NAME)
QCoreApplication::setApplicationName( QString(PROG_NAME) );
#endif
#if defined (SOFT_DEVELOPER)
QCoreApplication::setOrganizationName( QString(SOFT_DEVELOPER) );
#endif
#if defined (DEVELOPER_DOMAIN)
QCoreApplication::setOrganizationDomain( QString(DEVELOPER_DOMAIN) );
#endif
#if defined (PROG_VERSION)
QCoreApplication::setApplicationVersion( QString(PROG_VERSION) );
#endif
#if defined (BUILD_DATE)
qApp->setProperty( "buildDate", QString(BUILD_DATE) );
#endif
}
//==============================================================================
QString Application::applicationRootPath()
{
QString path { QCoreApplication::applicationDirPath() };
if ( path.right(1) != "/" ) {
path += "/";
}
if (path.right(5) == "/bin/") {
path.remove( path.length() - 4, 4 );
}
return path;
}
//==============================================================================
void Application::installTranslations(const QString &lng)
{
const QString dirName { applicationRootPath() + "translations/" };
QDir translationsDir { dirName };
QStringList filesList = translationsDir.entryList(
QStringList() << QString("*_%1.qm").arg(lng),
QDir::Files
);
for (const QString &fileName: std::as_const(filesList)) {
QTranslator *translator = new QTranslator(QCoreApplication::instance());
if (translator->load( dirName + fileName )) {
QCoreApplication::instance()->installTranslator( translator );
}
else {
delete translator;
}
}
}
//==============================================================================
QString Application::applicationProfilePath()
{
QCoreApplication *application = QCoreApplication::instance();
if (!application) return QString();
if (!application->property("profilePath").isNull())
return application->property("profilePath").toString();
QString path = applicationRootPath();
QString profileDir;
QDir dir(path);
if (isPortable() && dir.mkpath(path + "profile")) {
profileDir = path + "profile/";
}
else {
application->setProperty("isPortable", false);
profileDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/";
QString appDirectory = application->organizationName()
+ "/" + application->applicationName() + "/";
if (!profileDir.contains( appDirectory )) {
profileDir += appDirectory;
}
}
application->setProperty("profilePath", profileDir);
return profileDir;
}
//==============================================================================
bool Application::isPortable()
{
QCoreApplication *application = QCoreApplication::instance();
if (!application) return false;
if (!application->property("isPortable").isNull())
return application->property("isPortable").toBool();
QFileInfo file(applicationRootPath() + "portable");
bool portable = (file.exists() && file.isFile()) || parameterExists("portable");
application->setProperty("isPortable", portable);
return portable;
}
//==============================================================================
bool Application::isDebug()
{
QCoreApplication *application = QCoreApplication::instance();
if (!application) return false;
if (!application->property("isDebug").isNull())
return application->property("isDebug").toBool();
QFileInfo file(applicationRootPath() + "debug");
bool debug = (file.exists() && file.isFile()) || parameterExists("debug");
application->setProperty("isDebug", debug);
return debug;
}
//==============================================================================
bool Application::parameterExists(const QString &name, const QString &shortName, QString *value)
{
QCoreApplication *application = QCoreApplication::instance();
if (!application) return false;
const QStringList params {application->arguments()};
int index {1};
while (index < params.size()) {
QString param = params.at(index);
++index;
while (!param.isEmpty() && ((param.at(0) == '/') || (param.at(0) == '-'))) {
param.remove(0, 1);
}
if (param.isEmpty()) continue;
if (!shortName.isEmpty() && (param == shortName)) {
if (value && (index < params.size())) *value = params.at(index);
return true;
}
else {
auto n = param.indexOf('=');
if (n > 0) {
QString val = param.mid(n + 1);
param = param.left(n);
if (param == name) {
if (value) *value = val;
return true;
}
}
else if (param == name) {
if (value && (index < params.size())) *value = params.at(index);
return true;
}
}
}
return false;
}
//==============================================================================