feat(cpn): save model images with models and settings (#7234)
This commit is contained in:
@@ -20,6 +20,8 @@
|
||||
*/
|
||||
|
||||
#include "etx.h"
|
||||
#include "helpers.h"
|
||||
|
||||
#include <QFile>
|
||||
|
||||
#define MZ_ALLOCATION_SIZE (32*1024)
|
||||
@@ -94,14 +96,8 @@ bool EtxFormat::loadFile(QByteArray & filedata, const QString & filename, bool o
|
||||
{
|
||||
size_t size;
|
||||
void * data = mz_zip_reader_extract_file_to_heap(&zip_archive, qPrintable(filename), &size, 0);
|
||||
if (!data) {
|
||||
if (optional) {
|
||||
//qDebug() << "File not found:" << filename;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!data) return optional;
|
||||
|
||||
qDebug() << QString("Extracted file %1, size=%2").arg(filename).arg(size);
|
||||
filedata.clear();
|
||||
@@ -110,16 +106,77 @@ bool EtxFormat::loadFile(QByteArray & filedata, const QString & filename, bool o
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EtxFormat::writeFile(const QByteArray & filedata, const QString & filename)
|
||||
bool EtxFormat::loadImageFile(const QString & filename, bool optional)
|
||||
{
|
||||
if (!mz_zip_writer_add_mem(&zip_archive, filename.toStdString().c_str(), filedata.data(), filedata.size(), MZ_DEFAULT_LEVEL)) {
|
||||
setError(tr("Error adding %1 to EdgeTX archive").arg(filename));
|
||||
QString srcpath("IMAGES/" % filename);
|
||||
size_t size;
|
||||
void * data = mz_zip_reader_extract_file_to_heap(&zip_archive, qPrintable(srcpath), &size, 0);
|
||||
|
||||
if (!data) return optional;
|
||||
|
||||
QByteArray ba;
|
||||
ba.append((char *)data, size);
|
||||
mz_free(data);
|
||||
|
||||
QFile imgfile(Helpers::getImagesCacheDir() % "/" % filename);
|
||||
|
||||
if (imgfile.open(QIODevice::WriteOnly)) {
|
||||
if (imgfile.write(ba) != (qint64)size) {
|
||||
qDebug() << "Image data written does not match expected";
|
||||
return false;
|
||||
}
|
||||
|
||||
imgfile.close();
|
||||
} else {
|
||||
setError(tr("Unable to write %1 to cache").arg(filename));
|
||||
return false;
|
||||
}
|
||||
|
||||
qDebug() << QString("Extracted file %1, size=%2").arg(srcpath).arg(size);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EtxFormat::writeFile(const QByteArray & filedata, const QString & filename)
|
||||
{
|
||||
if (!mz_zip_writer_add_mem(&zip_archive, filename.toStdString().c_str(), filedata.data(), filedata.size(), MZ_DEFAULT_LEVEL)) {
|
||||
setError(tr("Error adding %1 to archive").arg(filename));
|
||||
return false;
|
||||
}
|
||||
|
||||
qDebug() << "File" << filename << "written, size:" << filedata.size();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EtxFormat::writeImageFile(const QString & filename)
|
||||
{
|
||||
QString srcpath(Helpers::getImagePath(filename));
|
||||
|
||||
if (!srcpath.isEmpty()) {
|
||||
QByteArray ba;
|
||||
QFile imgfile(srcpath);
|
||||
|
||||
if (imgfile.open(QIODevice::ReadOnly)) {
|
||||
ba.append(imgfile.readAll());
|
||||
imgfile.close();
|
||||
} else {
|
||||
setError(tr("Unable to open image %1").arg(filename));
|
||||
return false;
|
||||
}
|
||||
|
||||
QString destpath("IMAGES/" % filename);
|
||||
|
||||
if (!mz_zip_writer_add_mem(&zip_archive, destpath.toStdString().c_str(), ba.data(), ba.size(), MZ_DEFAULT_LEVEL)) {
|
||||
setError(tr("Error adding file %1").arg(destpath));
|
||||
return false;
|
||||
}
|
||||
|
||||
qDebug() << "File" << destpath << "written, size:" << ba.size();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool EtxFormat::getFileList(std::list<std::string>& filelist)
|
||||
{
|
||||
int count = (int)mz_zip_reader_get_num_files(&zip_archive);
|
||||
|
||||
@@ -41,7 +41,9 @@ class EtxFormat : public LabelsStorageFormat
|
||||
|
||||
protected:
|
||||
virtual bool loadFile(QByteArray & fileData, const QString & fileName, bool optional = false);
|
||||
virtual bool loadImageFile(const QString & fileName, bool optional = false);
|
||||
virtual bool writeFile(const QByteArray & fileData, const QString & fileName);
|
||||
virtual bool writeImageFile(const QString & fileName);
|
||||
virtual bool getFileList(std::list<std::string>& filelist);
|
||||
virtual bool deleteFile(const QString & fileName) { return false; }
|
||||
|
||||
|
||||
@@ -71,6 +71,7 @@ bool LabelsStorageFormat::load(RadioData & radioData)
|
||||
|
||||
QByteArray labelslistBuffer;
|
||||
int sortOrder = 0;
|
||||
|
||||
if (loadFile(labelslistBuffer, "MODELS/labels.yml")) {
|
||||
try {
|
||||
if (!loadLabelsListFromYaml(radioData.labels, sortOrder, modelFiles, labelslistBuffer)) {
|
||||
@@ -108,6 +109,8 @@ bool LabelsStorageFormat::load(RadioData & radioData)
|
||||
if (hasLabels)
|
||||
radioData.models.resize(modelFiles.size());
|
||||
|
||||
QList<QString> modelImages;
|
||||
|
||||
for (const auto& mc : modelFiles) {
|
||||
qDebug() << "Filename: " << mc.filename.c_str();
|
||||
|
||||
@@ -127,6 +130,7 @@ bool LabelsStorageFormat::load(RadioData & radioData)
|
||||
|
||||
QByteArray modelBuffer;
|
||||
QString filename = "MODELS/" + QString::fromStdString(mc.filename);
|
||||
|
||||
if (!loadFile(modelBuffer, filename)) {
|
||||
setError(tr("Cannot extract ") + filename);
|
||||
return false;
|
||||
@@ -150,6 +154,14 @@ bool LabelsStorageFormat::load(RadioData & radioData)
|
||||
if (!loadChecklist(model))
|
||||
return false;
|
||||
|
||||
if (!model.isBitmapEmpty()) {
|
||||
const QString fname(model.getImageFilename());
|
||||
|
||||
if (!modelImages.contains(fname)) {
|
||||
modelImages.append(fname);
|
||||
}
|
||||
}
|
||||
|
||||
model.modelIndex = modelIdx;
|
||||
strncpy(model.filename, mc.filename.c_str(), sizeof(model.filename)-1);
|
||||
|
||||
@@ -160,6 +172,11 @@ bool LabelsStorageFormat::load(RadioData & radioData)
|
||||
modelIdx++;
|
||||
}
|
||||
|
||||
for (const auto &fname : modelImages) {
|
||||
if (!loadImageFile(fname, true))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add the labels in the models
|
||||
if (hasLabels) {
|
||||
radioData.addLabelsFromModels();
|
||||
@@ -232,8 +249,9 @@ bool LabelsStorageFormat::write(RadioData & radioData)
|
||||
}
|
||||
|
||||
EtxModelfiles modelFiles;
|
||||
for (const auto& model : radioData.models) {
|
||||
QList<QString> modelImages;
|
||||
|
||||
for (const auto& model : radioData.models) {
|
||||
if (model.isEmpty())
|
||||
continue;
|
||||
|
||||
@@ -251,10 +269,23 @@ bool LabelsStorageFormat::write(RadioData & radioData)
|
||||
if (!writeFile(modelData, modelFilename))
|
||||
return false;
|
||||
|
||||
if (!model.isBitmapEmpty()) {
|
||||
const QString fname(model.getImageFilename());
|
||||
|
||||
if (!modelImages.contains(fname)) {
|
||||
modelImages.append(fname);
|
||||
}
|
||||
}
|
||||
|
||||
if (!writeChecklist(model))
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto &fname : modelImages) {
|
||||
if (!writeImageFile(fname))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (hasLabels) {
|
||||
QByteArray labelsListBuffer;
|
||||
if (!writeLabelsListToYaml(radioData, labelsListBuffer) || !writeFile(labelsListBuffer, "MODELS/labels.yml"))
|
||||
@@ -279,11 +310,10 @@ bool LabelsStorageFormat::loadChecklist(ModelData & model)
|
||||
|
||||
bool LabelsStorageFormat::writeChecklist(const ModelData & model)
|
||||
{
|
||||
const QString fname("MODELS/" + model.getChecklistFilename());
|
||||
|
||||
// not every model has a checklist
|
||||
if (!model.checklistData.isEmpty()) {
|
||||
const QString fname("MODELS/" + model.getChecklistFilename());
|
||||
//qDebug() << "Writing checklist file:" << fname;
|
||||
|
||||
if (!writeFile(model.checklistData, fname)) {
|
||||
setError(tr("Cannot write ") + fname);
|
||||
return false;
|
||||
|
||||
@@ -46,7 +46,9 @@ class LabelsStorageFormat : public StorageFormat
|
||||
|
||||
protected:
|
||||
virtual bool loadFile(QByteArray & fileData, const QString & fileName, bool optional = false) = 0;
|
||||
virtual bool loadImageFile(const QString & fileName, bool optional = false) = 0;
|
||||
virtual bool writeFile(const QByteArray & fileData, const QString & fileName) = 0;
|
||||
virtual bool writeImageFile(const QString & fileName) = 0;
|
||||
virtual bool getFileList(std::list<std::string>& filelist) = 0;
|
||||
virtual bool deleteFile(const QString & fileName) = 0;
|
||||
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
*/
|
||||
|
||||
#include "sdcard.h"
|
||||
#include "helpers.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
|
||||
@@ -28,6 +30,7 @@ bool SdcardFormat::write(RadioData & radioData)
|
||||
QDir dir(filename);
|
||||
dir.mkdir("RADIO");
|
||||
dir.mkdir("MODELS");
|
||||
dir.mkdir("IMAGES");
|
||||
return LabelsStorageFormat::write(radioData);
|
||||
}
|
||||
|
||||
@@ -35,6 +38,7 @@ bool SdcardFormat::loadFile(QByteArray & filedata, const QString & filename, boo
|
||||
{
|
||||
QString path = this->filename + "/" + filename;
|
||||
QFile file(path);
|
||||
|
||||
if (!file.exists()) {
|
||||
if (optional) {
|
||||
//qDebug() << "File not found:" << filename;
|
||||
@@ -45,11 +49,47 @@ bool SdcardFormat::loadFile(QByteArray & filedata, const QString & filename, boo
|
||||
}
|
||||
|
||||
if (!file.open(QFile::ReadOnly)) {
|
||||
setError(tr("Error opening file %1:\n%2.").arg(path).arg(file.errorString()));
|
||||
setError(tr("Error opening file %1:\n%2.").arg(filename).arg(file.errorString()));
|
||||
return false;
|
||||
}
|
||||
|
||||
filedata = file.readAll();
|
||||
qDebug() << "File" << path << "read, size:" << filedata.size();
|
||||
qDebug() << "File" << filename << "read, size:" << filedata.size();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SdcardFormat::loadImageFile(const QString & filename, bool optional)
|
||||
{
|
||||
QString srcpath = this->filename + "/IMAGES/" + filename;
|
||||
QFile file(srcpath);
|
||||
|
||||
if (!file.exists()) return optional;
|
||||
|
||||
if (QDir(Helpers::getImagesCacheDir()).exists()) {
|
||||
QString destpath(Helpers::getImagesCacheDir() % "/" % filename);
|
||||
QFile destfile(destpath);
|
||||
|
||||
// QFile::copy fails if destination file exists
|
||||
if (destfile.exists()) {
|
||||
if (destfile.remove()) {
|
||||
qDebug() << "Deleted file from image cache" << filename;
|
||||
} else {
|
||||
qDebug() << "Unable to delete cached" << filename;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (file.copy(Helpers::getImagesCacheDir() % "/" % filename)) {
|
||||
qDebug() << "Cached image" << filename;
|
||||
} else {
|
||||
qDebug() << "Failed to cache image" << filename;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
qDebug() << "Failed to cache" << filename << ". Cache directory" << Helpers::getImagesCacheDir()
|
||||
<< "does not exist.";
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -57,25 +97,59 @@ bool SdcardFormat::writeFile(const QByteArray & data, const QString & filename)
|
||||
{
|
||||
QString path = this->filename + "/" + filename;
|
||||
QFile file(path);
|
||||
|
||||
if (!file.open(QFile::WriteOnly)) {
|
||||
setError(tr("Error opening file %1 in write mode:\n%2.").arg(path).arg(file.errorString()));
|
||||
return false;
|
||||
}
|
||||
|
||||
file.write(data.data(), data.size());
|
||||
file.close();
|
||||
qDebug() << "File" << path << "written, size:" << data.size();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SdcardFormat::writeImageFile(const QString & filename)
|
||||
{
|
||||
QString srcpath(Helpers::getImagePath(filename));
|
||||
|
||||
if (!srcpath.isEmpty()) {
|
||||
QFileInfo srcinfo(srcpath);
|
||||
QString destpath(this->filename + "/IMAGES/" + filename);
|
||||
QFileInfo destinfo(destpath);
|
||||
|
||||
if (destinfo.exists()) {
|
||||
// for performance reasons avoid overwriting the same image
|
||||
if (destinfo.size() == srcinfo.size()) {
|
||||
qDebug() << "Image file" << filename << "skipped as same size";
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!QFile(srcpath).copy(destpath)) {
|
||||
setError(tr("Error writing image file: %1").arg(filename));
|
||||
return false;
|
||||
}
|
||||
|
||||
qDebug() << "File" << destpath << "written, size:" << srcinfo.size();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SdcardFormat::getFileList(std::list<std::string>& filelist)
|
||||
{
|
||||
QDir dir(filename);
|
||||
|
||||
if (!dir.cd("MODELS")) return false;
|
||||
|
||||
QStringList ql = dir.entryList();
|
||||
|
||||
for (const auto& str : ql) {
|
||||
filelist.push_back("MODELS/" + str.toStdString());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -92,6 +166,11 @@ bool SdcardFormat::deleteFile(const QString & filename)
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
class SdcardStorageFactory
|
||||
*/
|
||||
|
||||
bool SdcardStorageFactory::probe(const QString & path)
|
||||
{
|
||||
return QDir(path).exists();
|
||||
|
||||
@@ -40,7 +40,9 @@ class SdcardFormat : public LabelsStorageFormat
|
||||
|
||||
protected:
|
||||
virtual bool loadFile(QByteArray & fileData, const QString & fileName, bool optional = false);
|
||||
virtual bool loadImageFile(const QString & fileName, bool optional = false);
|
||||
virtual bool writeFile(const QByteArray & fileData, const QString & fileName);
|
||||
virtual bool writeImageFile(const QString & fileName);
|
||||
virtual bool getFileList(std::list<std::string>& filelist);
|
||||
virtual bool deleteFile(const QString & fileName);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user