readSettings()
read ini-file for binary (e.g. ATBUpdateTool, ATBUpdateDC etc). 1: using optional path 2: under /etc... 3: under /opt...
This commit is contained in:
		@@ -1,8 +1,12 @@
 | 
			
		||||
#include "utils.h"
 | 
			
		||||
#include "utils_internal.h"
 | 
			
		||||
 | 
			
		||||
#include <QFile>
 | 
			
		||||
#include <QDir>
 | 
			
		||||
#include <QTextStream>
 | 
			
		||||
#include <QSettings>
 | 
			
		||||
#include <QDebug>
 | 
			
		||||
#include <QCryptographicHash>
 | 
			
		||||
#include <QFileInfoList>
 | 
			
		||||
 | 
			
		||||
namespace internal {
 | 
			
		||||
 | 
			
		||||
@@ -35,6 +39,11 @@ QString customerRepoDir() {
 | 
			
		||||
    return !n.isEmpty() ? QDir::cleanPath(r + QDir::separator() + n) : "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString customerRepoDcDir() {
 | 
			
		||||
    QString const &r = customerRepoDir();
 | 
			
		||||
    return QDir::cleanPath(r + QDir::separator() + "etc/dc/");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool customerRepoExists() {
 | 
			
		||||
    QString const repoDir{customerRepoDir()};
 | 
			
		||||
    return !repoDir.isEmpty() ? QDir(repoDir).exists() : false;
 | 
			
		||||
@@ -52,4 +61,104 @@ QString branchName() {
 | 
			
		||||
    return "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::unique_ptr<QSettings> readSettings(QString const &optionalDirName) {
 | 
			
		||||
    std::unique_ptr<QSettings> settings{std::make_unique<QSettings>()};
 | 
			
		||||
 | 
			
		||||
    QString const fileName{settings->applicationName() + ".ini"};
 | 
			
		||||
    QDir d;
 | 
			
		||||
 | 
			
		||||
    if (!optionalDirName.isEmpty()) {
 | 
			
		||||
        d = QDir{optionalDirName};
 | 
			
		||||
        if (d.exists()) { // try to find ini-file under optionalDirname
 | 
			
		||||
            QFileInfo fi{d, optionalDirName};
 | 
			
		||||
            if (fi.exists()) {
 | 
			
		||||
                settings.reset(new QSettings(fi.absoluteFilePath(), QSettings::IniFormat));
 | 
			
		||||
                return settings;
 | 
			
		||||
            } else {
 | 
			
		||||
                qCritical() << fi.absoluteFilePath() << "not found."
 | 
			
		||||
                            << "Try" << internal::DEFAULT_INI_DIR;
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            qCritical() << optionalDirName << "not found."
 | 
			
		||||
                        << "Try" << internal::DEFAULT_INSTALL_DIR;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    d = internal::DEFAULT_INI_DIR;
 | 
			
		||||
    if (d.exists()) { // try to find ini-file under /etc/tools/atbupdate
 | 
			
		||||
        QFileInfo fi{d, fileName};
 | 
			
		||||
        if (fi.exists()) {
 | 
			
		||||
            settings.reset(new QSettings(fi.absoluteFilePath(), QSettings::IniFormat));
 | 
			
		||||
            return settings;
 | 
			
		||||
        } else {
 | 
			
		||||
            qCritical() << fi.absoluteFilePath() << "not found."
 | 
			
		||||
                        << "Try" << internal::DEFAULT_INSTALL_DIR;
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        qCritical() << internal::DEFAULT_INI_DIR << "not found."
 | 
			
		||||
                    << "Try" << internal::DEFAULT_INSTALL_DIR;
 | 
			
		||||
    }
 | 
			
		||||
    d = QDir{internal::DEFAULT_INSTALL_DIR};
 | 
			
		||||
    if (d.exists()) { // try to find ini-file under /opt/app/tools/atbupdate
 | 
			
		||||
        QFileInfo fi{d, fileName};
 | 
			
		||||
        if (fi.exists()) {
 | 
			
		||||
            settings.reset(new QSettings(fi.absoluteFilePath(), QSettings::IniFormat));
 | 
			
		||||
            return settings;
 | 
			
		||||
        } else {
 | 
			
		||||
            qCritical() << fi.absoluteFilePath() << "not found.";
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        qCritical() << internal::DEFAULT_INSTALL_DIR << "not found.";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return settings;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::unique_ptr<QString> dcCandidateToInstall(QString const &dcDirectory) {
 | 
			
		||||
    std::unique_ptr<QString> dcCandidate{nullptr};
 | 
			
		||||
 | 
			
		||||
    QDir dcDir{dcDirectory.isEmpty() ? customerRepoDcDir() : dcDirectory};
 | 
			
		||||
    if (dcDir.exists()) {
 | 
			
		||||
 | 
			
		||||
        QFileInfoList fileInfoList =
 | 
			
		||||
            dcDir.entryInfoList(QStringList("*.bin"),
 | 
			
		||||
                                QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
 | 
			
		||||
 | 
			
		||||
        QFileInfo dc2cbin{dcDir.absoluteFilePath("dc2c.bin")};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        if (dc2cbin.exists()) {
 | 
			
		||||
 | 
			
		||||
            QCryptographicHash md5gen(QCryptographicHash::Md5);
 | 
			
		||||
            QByteArray ba_dc2cbin{};
 | 
			
		||||
            {
 | 
			
		||||
                QFile f{dc2cbin.absoluteFilePath()};
 | 
			
		||||
                if (f.open(QIODevice::ReadOnly)) {
 | 
			
		||||
                    md5gen.addData(f.readAll());
 | 
			
		||||
                    ba_dc2cbin = md5gen.result();
 | 
			
		||||
                    md5gen.reset();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (ba_dc2cbin.size() > 0) {
 | 
			
		||||
                QFileInfoList::const_iterator it;
 | 
			
		||||
                for (it = fileInfoList.cbegin(); it != fileInfoList.cend(); ++it) {
 | 
			
		||||
                    if (it->absoluteFilePath() != dc2cbin.absoluteFilePath()) {
 | 
			
		||||
                        QFile f{it->absoluteFilePath()};
 | 
			
		||||
                        if (f.open(QIODevice::ReadOnly)) {
 | 
			
		||||
                            md5gen.addData(f.readAll());
 | 
			
		||||
                            if (ba_dc2cbin == md5gen.result()) {
 | 
			
		||||
                                dcCandidate.reset(new QString(f.fileName()));
 | 
			
		||||
                                break;
 | 
			
		||||
                            }
 | 
			
		||||
                            md5gen.reset();
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return dcCandidate;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // namespace internal
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user