Refactor firmware and companion updates plus add extra supporting components

This commit is contained in:
elecpower
2022-09-06 09:29:41 +10:00
committed by Peter Feerick
parent b4e1ceb4f8
commit bbfaf86bc8
70 changed files with 5526 additions and 1310 deletions
+1
View File
@@ -16,6 +16,7 @@ set(storage_NAMES
otx
yaml
crc
minizinterface
)
set(storage_SRCS
+81 -1
View File
@@ -289,6 +289,42 @@ void Profile::resetFwVariables()
}
// ** ComponentData class********************
ComponentData::ComponentData() : CompStoreObj(), index(-1)
{
qRegisterMetaTypeStreamOperators<ComponentData::ReleaseChannel>("ComponentData::ReleaseChannel");
CompStoreObj::addObjectMapping(propertyGroup(), this);
}
// The default copy operator can not be used since the index variable would be destroyed
ComponentData & ComponentData::operator= (const ComponentData & rhs)
{
for (int i = metaObject()->propertyOffset(), e = metaObject()->propertyCount(); i < e; ++i) {
const QMetaProperty & prop = metaObject()->property(i);
if (!prop.isValid() || !prop.isWritable()) {
qWarning() << "Could not copy property" << QString(prop.name()) << "isValid:" << prop.isValid() << "isWritable:" << prop.isWritable();
continue;
}
prop.write(this, prop.read(&rhs));
}
return *this;
}
bool ComponentData::existsOnDisk()
{
return (m_settings.contains(settingsPath() % "name"));
}
void ComponentData::clearRelease()
{
release(CompStoreObj::propertyDefaultValue(this, "release").toString());
id(CompStoreObj::propertyDefaultValue(this, "id").toInt());
prerelease(CompStoreObj::propertyDefaultValue(this, "prerelease").toBool());
date(CompStoreObj::propertyDefaultValue(this, "date").toString());
version(CompStoreObj::propertyDefaultValue(this, "version").toString());
}
// ** AppData class********************
AppData::AppData() :
@@ -297,8 +333,8 @@ AppData::AppData() :
{
QMetaType::registerComparators<SimulatorOptions>();
qRegisterMetaTypeStreamOperators<SimulatorOptions>("SimulatorOptions");
qRegisterMetaTypeStreamOperators<AppData::DownloadBranchType>("AppData::DownloadBranchType");
qRegisterMetaTypeStreamOperators<AppData::NewModelAction>("AppData::NewModelAction");
qRegisterMetaTypeStreamOperators<AppData::UpdateCheckFreq>("AppData::UpdateCheckFreq");
CompStoreObj::addObjectMapping(propertyGroup(), this);
@@ -315,6 +351,10 @@ AppData::AppData() :
// Configure the joysticks
for (int i=0; i<MAX_JOYSTICKS; i++)
joystick[i].setIndex(i);
// Configure the updates
for (int i=0; i<MAX_COMPONENTS; i++)
component[i].setIndex(i);
}
static QString fmtHex(quint32 num)
@@ -346,6 +386,9 @@ void AppData::initAll()
// Initialize the joysticks
for (int i=0; i<MAX_JOYSTICKS; i++)
joystick[i].init();
// Initialize the updatess
for (int i=0; i<MAX_COMPONENTS; i++)
component[i].init();
}
void AppData::resetAllSettings()
@@ -356,6 +399,8 @@ void AppData::resetAllSettings()
profile[i].resetAll();
for (int i=0; i<MAX_JOYSTICKS; i++)
joystick[i].resetAll();
for (int i=0; i<MAX_COMPONENTS; i++)
component[i].resetAll();
firstUse = true;
}
@@ -366,6 +411,8 @@ void AppData::storeAllSettings()
profile[i].storeAll();
for (int i=0; i<MAX_JOYSTICKS; i++)
joystick[i].storeAll();
for (int i=0; i<MAX_COMPONENTS; i++)
component[i].storeAll();
}
bool AppData::hasCurrentSettings() const
@@ -406,6 +453,15 @@ QMap<int, QString> AppData::getActiveProfiles() const
return active;
}
int AppData::getComponentIndex(QString name) const
{
for (int i=0; i<MAX_COMPONENTS; i++) {
if (g.component[i].existsOnDisk() && g.component[i].name() == name)
return i;
}
return -1;
}
void AppData::convertSettings(QSettings & settings)
{
quint32 savedVer = settings.value(SETTINGS_VERSION_KEY, 0).toUInt();
@@ -580,3 +636,27 @@ bool AppData::exportSettingsToFile(const QString & expFile, QString & resultMsg)
resultMsg.append(tr("for unknown reasons."));
return false;
}
ComponentData & AppData::getComponent(int index)
{
if (index > -1 && index < MAX_COMPONENTS)
return component[index];
return component[0];
}
const ComponentData & AppData::getComponent(int index) const
{
if (index > -1 && index < MAX_COMPONENTS)
return component[index];
return component[0];
}
void AppData::resetUpdatesSettings()
{
g.updateCheckFreqReset();
g.downloadDirReset();
g.decompressDirReset();
g.decompressUseDwnldReset();
g.updateDirReset();
g.updateUseSDReset();
}
+103 -62
View File
@@ -25,8 +25,7 @@
@{
*/
#ifndef _APPDATA_H_
#define _APPDATA_H_
#pragma once
#include "constants.h"
#include "macros.h"
@@ -59,14 +58,9 @@
#define CPN_SETTINGS_INI_FILE QString(PRODUCT % " " % QCoreApplication::translate("Companion", "settings") % " %1.ini")
#define CPN_SETTINGS_INI_PATH QString(CPN_SETTINGS_BACKUP_DIR % "/" % CPN_SETTINGS_INI_FILE)
#define CPN_URL_DOWNLOAD "https://downloads.edgetx.org/"
#define CPN_URL_DOWNLOAD_CUR_VERS CPN_URL_DOWNLOAD QT_STRINGIFY(VERSION_MAJOR) "." QT_STRINGIFY(VERSION_MINOR) "/"
#define CPN_URL_DOWNLOAD_CUR_REL CPN_URL_DOWNLOAD_CUR_VERS "release/"
#define CPN_URL_DOWNLOAD_CUR_RC CPN_URL_DOWNLOAD_CUR_VERS "rc/"
#define CPN_URL_DOWNLOAD_CUR_UNST CPN_URL_DOWNLOAD_CUR_VERS "nightlies/"
#define MAX_PROFILES 20
#define MAX_JOYSTICKS 8
#define MAX_COMPONENTS 10
// It important that these function names are consistent everywhere.
#define PROP_FSIG_INIT_IMPL _init()
@@ -427,6 +421,51 @@ class Profile: public CompStoreObj
}
};
//! \brief ComponentData class stores properties related to each updateable component.
class ComponentData: public CompStoreObj
{
Q_OBJECT
public:
ComponentData & operator=(const ComponentData & rhs);
enum ReleaseChannel {
RELEASE_CHANNEL_STABLE,
RELEASE_CHANNEL_PRERELEASE,
RELEASE_CHANNEL_NIGHTLY
};
Q_ENUM(ReleaseChannel)
void clearRelease();
static QStringList releaseChannelsList() { return { tr("Releases"), tr("Pre-release"), tr("Nightly") } ; }
inline ReleaseChannel boundedReleaseChannel() const {
return qBound(RELEASE_CHANNEL_STABLE, releaseChannel(), RELEASE_CHANNEL_NIGHTLY);
}
public slots:
bool existsOnDisk();
protected:
explicit ComponentData();
void setIndex(int idx) { index = idx; }
inline QString propertyGroup() const override { return QStringLiteral("Components"); }
inline QString settingsPath() const override { return QString("%1/component%2/").arg(propertyGroup()).arg(index); }
friend class AppData;
private:
PROPERTYSTR (name)
PROPERTY (bool, checkForUpdate, false)
PROPERTY (ReleaseChannel, releaseChannel, RELEASE_CHANNEL_STABLE)
PROPERTYSTRD( release, "unknown")
PROPERTY (int, id, 0)
PROPERTY (bool, prerelease, false)
PROPERTYSTRD( version, "0")
PROPERTYSTRD( date, "")
int index;
CREATE_ENUM_FRIEND_STREAM_OPS(ComponentData::ReleaseChannel)
};
/*!
\brief The AppData class acts as the main interface for all application settings.
@@ -441,13 +480,6 @@ class AppData: public CompStoreObj
{
Q_OBJECT
public:
enum DownloadBranchType {
BRANCH_RELEASE_STABLE,
BRANCH_RC_TESTING,
BRANCH_NIGHTLY_UNSTABLE
};
Q_ENUM(DownloadBranchType)
enum NewModelAction {
MODEL_ACT_NONE,
MODEL_ACT_WIZARD,
@@ -457,13 +489,24 @@ class AppData: public CompStoreObj
};
Q_ENUM(NewModelAction)
enum UpdateCheckFreq {
UPDATE_CHECK_MANUAL,
UPDATE_CHECK_STARTUP,
UPDATE_CHECK_DAILY,
UPDATE_CHECK_WEEKLY,
UPDATE_CHECK_MONTHLY
};
Q_ENUM(UpdateCheckFreq)
static QStringList newModelActionsList() { return { tr("None"), tr("Wizard"), tr("Editor"), tr("Template"), tr("Prompt") } ; }
static QStringList updateCheckFreqsList() { return { tr("Manually"), tr("Startup"), tr("Daily"), tr("Weekly"), tr("Monthly") } ; }
explicit AppData();
void init() override;
void initAll();
void resetAllSettings();
void storeAllSettings();
void resetUpdatesSettings();
inline bool isFirstUse() const { return firstUse; }
inline QString previousVersion() const { return upgradeFromVersion; }
@@ -487,6 +530,13 @@ class AppData: public CompStoreObj
//! List of all active profiles mapped by index.
QMap<int, QString> getActiveProfiles() const;
//! Get a modifiable (non-const) index to ComponentData for \a name. Returns -1 if \a name not found.
int getComponentIndex(QString name) const;
//! Get a modifiable (non-const) reference to the ComponentData at \a index. Returns component[0] if \a index is invalid.
ComponentData & getComponent(int index);
//! Get a non-modifiable (const) reference to the ComponentData at \a index. Returns component[0] if \a index is invalid.
const ComponentData & getComponent(int index) const;
//! Find the set of settings from the last previous version installed, if any. Used at virgin startup to offer import option.
bool findPreviousVersionSettings(QString * version) const;
//! Converts any old/refactored settings to new ones and removes anything stale if \a removeUnused is true. This is only important when
@@ -500,25 +550,10 @@ class AppData: public CompStoreObj
bool exportSettings(QSettings * toSettings, bool clearDestination = true);
bool exportSettingsToFile(const QString & expFile, QString & resultMsg);
inline DownloadBranchType boundedOpenTxBranch() const {
#if defined(ALLOW_NIGHTLY_BUILDS)
return qBound(BRANCH_RELEASE_STABLE, OpenTxBranch(), BRANCH_NIGHTLY_UNSTABLE);
#else
return qBound(BRANCH_RELEASE_STABLE, OpenTxBranch(), BRANCH_RC_TESTING);
#endif
}
inline QString openTxCurrentDownloadBranchUrl() const { return openTxDownloadBranchUrl(boundedOpenTxBranch()); }
static const QString openTxDownloadBranchUrl(DownloadBranchType type)
{
static const QStringList urlList({ CPN_URL_DOWNLOAD_CUR_REL, CPN_URL_DOWNLOAD_CUR_RC, CPN_URL_DOWNLOAD_CUR_UNST });
return urlList.value(type, CPN_URL_DOWNLOAD_CUR_VERS);
}
Profile profile[MAX_PROFILES];
JStickData joystick[MAX_JOYSTICKS];
FwRevision fwRev;
ComponentData component[MAX_COMPONENTS];
signals:
void currentProfileChanged();
@@ -541,47 +576,56 @@ class AppData: public CompStoreObj
PROPERTYQBA (mdiWinState)
PROPERTYQBA (compareWinGeo)
PROPERTYSTR3(armMcu, "arm_mcu", QStringLiteral("at91sam3s4-9x"))
PROPERTYSTR3(armMcu, "arm_mcu", QStringLiteral("at91sam3s4-9x"))
PROPERTYSTR2(avrArguments, "avr_arguments")
PROPERTYSTR2(avrPort, "avr_port")
PROPERTYSTR2(avrdudeLocation, "avrdudeLocation")
PROPERTYSTR3(dfuArguments, "dfu_arguments", QStringLiteral("-a 0"))
PROPERTYSTR3(dfuArguments, "dfu_arguments", QStringLiteral("-a 0"))
PROPERTYSTR2(dfuLocation, "dfu_location")
PROPERTYSTR2(sambaLocation, "samba_location")
PROPERTYSTR3(sambaPort, "samba_port", QStringLiteral("\\USBserial\\COM23"))
PROPERTYSTR2(backupDir, "backupPath")
PROPERTYSTR2(eepromDir, "lastDir")
PROPERTYSTR2(flashDir, "lastFlashDir")
PROPERTYSTR2(imagesDir, "lastImagesDir")
PROPERTYSTR2(logDir, "lastLogDir")
PROPERTYSTR3(sambaPort, "samba_port", QStringLiteral("\\USBserial\\COM23"))
PROPERTYSTR2(libDir, "libraryPath")
PROPERTYSTR2(snapshotDir, "snapshotpath")
PROPERTYSTR2(updatesDir, "lastUpdatesDir")
PROPERTYSTR3(backupDir, "backupPath", CPN_DOCUMENTS_LOCATION % "/backups")
PROPERTYSTR3(eepromDir, "lastDir", CPN_DOCUMENTS_LOCATION % "/eeproms")
PROPERTYSTR3(flashDir, "lastFlashDir", CPN_DOCUMENTS_LOCATION % "/flash")
PROPERTYSTR3(imagesDir, "lastImagesDir", CPN_DOCUMENTS_LOCATION % "/images")
PROPERTYSTR3(logDir, "lastLogDir", CPN_DOCUMENTS_LOCATION % "/debuglogs")
PROPERTYSTRD(appLogsDir, CPN_DOCUMENTS_LOCATION % "/debuglogs")
PROPERTYSTR3(snapshotDir, "snapshotpath", CPN_DOCUMENTS_LOCATION % "/snapshots")
PROPERTYSTRD(templatesDir, CPN_DOCUMENTS_LOCATION % "/templates")
PROPERTYSTRD(downloadDir, CPN_DOCUMENTS_LOCATION % "/downloads")
PROPERTYSTRD(decompressDir, CPN_DOCUMENTS_LOCATION % "/decompress")
PROPERTY (bool, decompressUseDwnld, true)
PROPERTYSTRD(updateDir, CPN_DOCUMENTS_LOCATION % "/updates")
PROPERTY (bool, updateUseSD, true)
PROPERTY (int, updateLogLevel, 0)
PROPERTYSTR (locale)
PROPERTYSTR (gePath)
PROPERTYSTRD(mcu, QStringLiteral("m64"))
PROPERTYSTRD(programmer, QStringLiteral("usbasp"))
PROPERTYSTRD(appLogsDir, CPN_DOCUMENTS_LOCATION % "/DebugLogs")
PROPERTY(DownloadBranchType, OpenTxBranch, BRANCH_RELEASE_STABLE)
PROPERTY(NewModelAction, newModelAction, MODEL_ACT_WIZARD)
PROPERTY(NewModelAction, newModelAction, MODEL_ACT_WIZARD)
PROPERTY4(int, embedSplashes, "embedded_splashes", 0)
PROPERTY4(int, fwServerFails, "fwserver", 0)
PROPERTY4(int, iconSize, "icon_size", 2)
PROPERTY4(int, jsCtrl, "js_ctrl", 0)
PROPERTY4(int, historySize, "history_size", 10)
PROPERTY (int, generalEditTab, 0)
PROPERTY (int, theme, 1)
PROPERTY (int, warningId, 0)
PROPERTY4(int, embedSplashes, "embedded_splashes", 0)
PROPERTY4(int, fwServerFails, "fwserver", 0)
PROPERTY4(int, iconSize, "icon_size", 2)
PROPERTY4(int, jsCtrl, "js_ctrl", 0)
PROPERTY4(int, historySize, "history_size", 10)
PROPERTY (int, generalEditTab, 0)
PROPERTY (int, theme, 1)
PROPERTY (int, warningId, 0)
PROPERTY4(bool, jsSupport, "js_support", false)
PROPERTY4(bool, showSplash, "show_splash", true)
PROPERTY4(bool, snapToClpbrd, "snapshot_to_clipboard", false)
PROPERTY4(bool, autoCheckApp, "startup_check_companion", false)
PROPERTY4(bool, autoCheckFw, "startup_check_fw", false)
PROPERTY4(bool, promptProfile, "startup_prompt_profile", false)
PROPERTY4(bool, jsSupport, "js_support", false)
PROPERTY4(bool, showSplash, "show_splash", true)
PROPERTY4(bool, snapToClpbrd, "snapshot_to_clipboard", false)
PROPERTY(UpdateCheckFreq, updateCheckFreq, UPDATE_CHECK_MANUAL)
PROPERTYSTR(lastUpdateCheck)
PROPERTY4(bool, promptProfile, "startup_prompt_profile", false)
PROPERTY(bool, enableBackup, false)
PROPERTY(bool, backupOnFlash, true)
@@ -605,11 +649,8 @@ class AppData: public CompStoreObj
bool firstUse;
QString upgradeFromVersion;
CREATE_ENUM_FRIEND_STREAM_OPS(AppData::DownloadBranchType)
CREATE_ENUM_FRIEND_STREAM_OPS(AppData::NewModelAction)
CREATE_ENUM_FRIEND_STREAM_OPS(AppData::UpdateCheckFreq)
};
extern AppData g;
//! @}
#endif // _APPDATA_H_
+292
View File
@@ -0,0 +1,292 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "minizinterface.h"
#include "progresswidget.h"
#include <QDir>
#include <QDirIterator>
#include <QFileInfo>
#include <QDebug>
#include <QWidget>
MinizInterface::MinizInterface(ProgressWidget * progress, const ProgressCalcMethod progressMethod, const int & logLevel) :
progress(progress),
progressMethod(progressMethod),
progressValue(0),
logLevel(logLevel),
path(""),
archiveFile("")
{
reportProgress(tr("Progress calculation method: file %1").arg(progressMethod == PCM_COUNT ? tr("count") : tr("size")), QtDebugMsg);
}
MinizInterface::~MinizInterface()
{
}
bool MinizInterface::zipPathToFile(const QString & sourcePath, const QString & archiveFile, bool append)
{
if (progress)
progress->setInfo(tr("Compressing %1 to %2 with append %3").arg(sourcePath, archiveFile, append ? "true" : "false"));
else
qDebug() << QString("Compressing %1 to %2 with append %3").arg(sourcePath, archiveFile, append ? "true" : "false");
reportProgress(tr("Miniz version: %1").arg(MZ_VERSION), QtDebugMsg);
progressValue = 0;
path = sourcePath;
this->archiveFile = archiveFile;
mz_zip_zero_struct(&zip_archive);
QFileInfo afi(archiveFile);
if (afi.exists() && !append)
reportProgress(tr("Existing archive will be overwritten"), QtCriticalMsg);
if (afi.exists() && append) {
if (!mz_zip_reader_init_file(&zip_archive, qPrintable(archiveFile), 0)) {
reportProgress(tr("Unable to open existing archive"), QtFatalMsg);
return false;
}
else
reportProgress(tr("Existing archive opened"), QtDebugMsg);
if (!mz_zip_writer_init_from_reader(&zip_archive, qPrintable(archiveFile))) {
reportProgress(tr("Unable to write to existing archive"), QtFatalMsg);
return false;
}
else
reportProgress(tr("Existing archive initialised"), QtDebugMsg);
}
else {
if (!mz_zip_writer_init_file(&zip_archive, qPrintable(archiveFile), 0)) {
reportProgress(tr("Failure to initialise archive"), QtFatalMsg);
return false;
}
else
reportProgress(tr("Archive initialised"), QtDebugMsg);
}
QFileInfo pfi(path);
int fileCount = 0;
if (pfi.isDir()) {
if (progressMethod == PCM_COUNT) {
reportProgress(tr("Calculating number of items to archive"), QtDebugMsg);
QDirIterator itcnt(path, QDirIterator::Subdirectories);
while (itcnt.hasNext()) {
fileCount++;
}
}
if (progress) {
if (progressMethod == PCM_COUNT)
progress->setMaximum(fileCount);
else
progress->setMaximum(pfi.size());
}
fileCount = 0;
QDirIterator it(path, QDirIterator::Subdirectories);
while (it.hasNext()) {
it.next();
if (!addFileToArchive(it.filePath()))
return false;
}
}
else {
fileCount = 1;
if (progress) {
if (progressMethod == PCM_COUNT)
progress->setMaximum(fileCount);
else
progress->setMaximum(pfi.size());
}
if (!addFileToArchive(path))
return false;
}
if (!mz_zip_writer_finalize_archive(&zip_archive)) {
mz_zip_writer_end(&zip_archive);
reportProgress(tr("Failure to finalise archive"), QtFatalMsg);
return false;
}
mz_zip_writer_end(&zip_archive);
if (progress)
progress->setValue(progress->maximum());
reportProgress(tr("Compress complete"));
return true;
}
bool MinizInterface::addFileToArchive(const QString & path)
{
if (!mz_zip_writer_add_file(&zip_archive, qPrintable(archiveFile), qPrintable(path), NULL, 0, MZ_DEFAULT_COMPRESSION)) {
mz_zip_writer_end(&zip_archive);
reportProgress(tr("Failure to add %1").arg(path), QtFatalMsg);
return false;
}
if (progressMethod == PCM_COUNT)
++progressValue;
else
progressValue += QFileInfo(path).size();
if (progress)
progress->setValue(progressValue);
reportProgress(tr("Added file: %1").arg(path), QtDebugMsg);
return true;
}
bool MinizInterface::unzipArchiveToPath(const QString & archiveFile, const QString & destinationPath)
{
if (progress)
progress->setInfo(tr("Decompressing %1 to %2").arg(archiveFile, destinationPath));
else
qDebug() << QString("Decompressing %1 to %2").arg(archiveFile, destinationPath);
reportProgress(tr("Miniz version: %1").arg(MZ_VERSION), QtDebugMsg);
progressValue = 0;
path = destinationPath;
this->archiveFile = archiveFile;
mz_zip_zero_struct(&zip_archive);
if (!mz_zip_reader_init_file(&zip_archive, qPrintable(archiveFile), 0)) {
reportProgress(tr("File does not appear to be a compressed archive"), QtFatalMsg);
return false;
}
int fileCount = (int)mz_zip_reader_get_num_files(&zip_archive);
if (fileCount == 0) {
mz_zip_reader_end(&zip_archive);
reportProgress(tr("Compressed archive does not contain any files"), QtFatalMsg);
return false;
}
if (progress) {
if (progressMethod == PCM_COUNT)
progress->setMaximum(fileCount);
else
progress->setMaximum(QFileInfo(archiveFile).size());
}
mz_zip_archive_file_stat file_stat;
if (!mz_zip_reader_file_stat(&zip_archive, 0, &file_stat)) {
mz_zip_reader_end(&zip_archive);
reportProgress(tr("File status error"), QtFatalMsg);
return false;
}
for (int i = 0; i < fileCount; i++) {
if (!mz_zip_reader_file_stat(&zip_archive, i, &file_stat)) {
mz_zip_reader_end(&zip_archive);
reportProgress(tr("Unable to obtain file status for index: %1").arg(i), QtFatalMsg);
return false;
}
QString destPath = path % "/" % QString(file_stat.m_filename);
if (mz_zip_reader_is_file_a_directory(&zip_archive, i)) {
if (!createDirectory(destPath)) {
mz_zip_reader_end(&zip_archive);
return false;
}
}
else {
QFileInfo dfi(destPath);
if (!createDirectory(dfi.absolutePath())) {
mz_zip_reader_end(&zip_archive);
return false;
}
if (!mz_zip_reader_extract_to_file(&zip_archive, i, qPrintable(destPath), 0)) {
mz_zip_reader_end(&zip_archive);
reportProgress(tr("Failed to extract %1 to %2").arg(dfi.fileName(), dfi.absolutePath()), QtFatalMsg);
return false;
}
else {
if (dfi.size() != (qint64)file_stat.m_uncomp_size) {
reportProgress(tr("File %1 extracted size %2 does not match original %3").arg(file_stat.m_filename).arg(dfi.size()).arg((qint64)file_stat.m_uncomp_size), QtFatalMsg);
// return false; disable until testing complete as there maybe some adjustment factor or even OS strangeness
}
}
if (progressMethod == PCM_COUNT)
++progressValue;
else
progressValue += file_stat.m_comp_size;
if (progress)
progress->setValue(progressValue);
reportProgress(tr("Extracted file: %1").arg(file_stat.m_filename), QtDebugMsg);
}
}
mz_zip_reader_end(&zip_archive);
if (progress)
progress->setValue(progress->maximum());
reportProgress(tr("Decompress complete"));
return true;
}
bool MinizInterface::createDirectory(const QString & path)
{
if (!QDir(path).exists()) {
if (!QDir().mkpath(path)) {
reportProgress(tr("Failed to create directory: %1").arg(path), QtFatalMsg);
return false;
}
else
reportProgress(tr("Created directory: %1").arg(path));
}
return true;
}
void MinizInterface::reportProgress(const QString & text, const int & type, bool richText)
{
if (type >= logLevel) {
if (progress)
progress->addMessage(text, type);
else
qDebug() << text;
}
}
+60
View File
@@ -0,0 +1,60 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include "miniz.h"
#include <QtCore>
#include <QString>
class ProgressWidget;
class MinizInterface
{
Q_DECLARE_TR_FUNCTIONS(MinizInterface)
public:
enum ProgressCalcMethod {
PCM_COUNT,
PCM_SIZE
};
MinizInterface(ProgressWidget * progress = nullptr, const ProgressCalcMethod progressMethod = PCM_COUNT, const int & logLevel = QtWarningMsg);
~MinizInterface();
bool zipPathToFile(const QString & sourcePath, const QString & archiveFile, bool append = true);
bool unzipArchiveToPath(const QString & archiveFile, const QString & destinationPath);
private:
ProgressWidget *progress;
ProgressCalcMethod progressMethod;
qint64 progressValue;
int logLevel;
QString path;
QString archiveFile;
mz_zip_archive zip_archive;
bool addFileToArchive(const QString & path);
bool createDirectory(const QString & path);
void reportProgress(const QString & text, const int & type = QtInfoMsg, bool richText = false);
};