Made _zipDir a children of _dataMultiPart so it gets deleted at the right time.

This commit is contained in:
Atlante45 2014-03-27 11:18:32 -07:00
parent 88e7b8e68f
commit d9c48d63fe
2 changed files with 26 additions and 14 deletions

View file

@ -13,11 +13,11 @@
#include <QFileDialog> #include <QFileDialog>
#include <QStandardPaths> #include <QStandardPaths>
#include <QHttpMultiPart> #include <QHttpMultiPart>
#include <QTemporaryDir>
#include <QVariant> #include <QVariant>
#include <QMessageBox> #include <QMessageBox>
#include "AccountManager.h" #include "AccountManager.h"
#include "FstReader.h" #include "FstReader.h"
@ -30,7 +30,16 @@ static const QString MODEL_URL = "/api/v1/models";
static const int MAX_SIZE = 10 * 1024 * 1024; // 10 MB static const int MAX_SIZE = 10 * 1024 * 1024; // 10 MB
// Class providing the QObject parent system to QTemporaryDir
class TemporaryDir : public QTemporaryDir, public QObject {
public:
virtual ~TemporaryDir() {
// ensuring the entire object gets deleted by the QObject parent.
}
};
FstReader::FstReader(bool isHead) : FstReader::FstReader(bool isHead) :
_zipDir(new TemporaryDir()),
_lodCount(-1), _lodCount(-1),
_texturesCount(-1), _texturesCount(-1),
_totalSize(0), _totalSize(0),
@ -38,6 +47,8 @@ FstReader::FstReader(bool isHead) :
_readyToSend(false), _readyToSend(false),
_dataMultiPart(new QHttpMultiPart(QHttpMultiPart::FormDataType)) _dataMultiPart(new QHttpMultiPart(QHttpMultiPart::FormDataType))
{ {
_zipDir->setParent(_dataMultiPart);
} }
FstReader::~FstReader() { FstReader::~FstReader() {
@ -68,10 +79,10 @@ bool FstReader::zip() {
qDebug() << "Reading FST file : " << QFileInfo(fst).filePath(); qDebug() << "Reading FST file : " << QFileInfo(fst).filePath();
// Compress and copy the fst // Compress and copy the fst
if (!compressFile(QFileInfo(fst).filePath(), _zipDir.path() + "/" + QFileInfo(fst).fileName())) { if (!compressFile(QFileInfo(fst).filePath(), _zipDir->path() + "/" + QFileInfo(fst).fileName())) {
return false; return false;
} }
if (!addPart(_zipDir.path() + "/" + QFileInfo(fst).fileName(), if (!addPart(_zipDir->path() + "/" + QFileInfo(fst).fileName(),
QString("fst"))) { QString("fst"))) {
return false; return false;
} }
@ -103,10 +114,10 @@ bool FstReader::zip() {
return false; return false;
} }
// Compress and copy // Compress and copy
if (!compressFile(fbx.filePath(), _zipDir.path() + "/" + line[1])) { if (!compressFile(fbx.filePath(), _zipDir->path() + "/" + line[1])) {
return false; return false;
} }
if (!addPart(_zipDir.path() + "/" + line[1], "fbx")) { if (!addPart(_zipDir->path() + "/" + line[1], "fbx")) {
return false; return false;
} }
} else if (line[0] == TEXDIR_FIELD) { // Check existence } else if (line[0] == TEXDIR_FIELD) { // Check existence
@ -133,10 +144,10 @@ bool FstReader::zip() {
return false; return false;
} }
// Compress and copy // Compress and copy
if (!compressFile(lod.filePath(), _zipDir.path() + "/" + line[1])) { if (!compressFile(lod.filePath(), _zipDir->path() + "/" + line[1])) {
return false; return false;
} }
if (!addPart(_zipDir.path() + "/" + line[1], QString("lod%1").arg(++_lodCount))) { if (!addPart(_zipDir->path() + "/" + line[1], QString("lod%1").arg(++_lodCount))) {
return false; return false;
} }
} }
@ -162,6 +173,7 @@ bool FstReader::send() {
} }
AccountManager::getInstance().authenticatedRequest(MODEL_URL, QNetworkAccessManager::PostOperation, JSONCallbackParameters(), QByteArray(), _dataMultiPart); AccountManager::getInstance().authenticatedRequest(MODEL_URL, QNetworkAccessManager::PostOperation, JSONCallbackParameters(), QByteArray(), _dataMultiPart);
_zipDir = NULL;
_dataMultiPart = NULL; _dataMultiPart = NULL;
qDebug() << "Model sent."; qDebug() << "Model sent.";
@ -180,10 +192,10 @@ bool FstReader::addTextures(const QFileInfo& texdir) {
foreach (QFileInfo info, list) { foreach (QFileInfo info, list) {
if (info.isFile()) { if (info.isFile()) {
// Compress and copy // Compress and copy
if (!compressFile(info.filePath(), _zipDir.path() + "/" + info.fileName())) { if (!compressFile(info.filePath(), _zipDir->path() + "/" + info.fileName())) {
return false; return false;
} }
if (!addPart(_zipDir.path() + "/" + info.fileName(), if (!addPart(_zipDir->path() + "/" + info.fileName(),
QString("texture%1").arg(++_texturesCount))) { QString("texture%1").arg(++_texturesCount))) {
return false; return false;
} }
@ -204,7 +216,7 @@ bool FstReader::compressFile(const QString &inFileName, const QString &outFileNa
QFile outFile(outFileName); QFile outFile(outFileName);
if (!outFile.open(QIODevice::WriteOnly)) { if (!outFile.open(QIODevice::WriteOnly)) {
QDir(_zipDir.path()).mkpath(QFileInfo(outFileName).path()); QDir(_zipDir->path()).mkpath(QFileInfo(outFileName).path());
if (!outFile.open(QIODevice::WriteOnly)) { if (!outFile.open(QIODevice::WriteOnly)) {
QMessageBox::warning(NULL, QMessageBox::warning(NULL,
QString("ModelUploader::compressFile()"), QString("ModelUploader::compressFile()"),
@ -229,6 +241,7 @@ bool FstReader::addPart(const QString &path, const QString& name) {
QString("Could not open %1").arg(path), QString("Could not open %1").arg(path),
QMessageBox::Ok); QMessageBox::Ok);
qDebug() << "[Warning] " << QString("Could not open %1").arg(path); qDebug() << "[Warning] " << QString("Could not open %1").arg(path);
delete file;
return false; return false;
} }

View file

@ -10,11 +10,10 @@
#ifndef __hifi__FstReader__ #ifndef __hifi__FstReader__
#define __hifi__FstReader__ #define __hifi__FstReader__
#include <QTemporaryDir> class TemporaryDir;
class QHttpMultiPart; class QHttpMultiPart;
class FstReader { class FstReader : public QObject {
public: public:
FstReader(bool isHead); FstReader(bool isHead);
~FstReader(); ~FstReader();
@ -23,7 +22,7 @@ public:
bool send(); bool send();
private: private:
QTemporaryDir _zipDir; TemporaryDir* _zipDir;
int _lodCount; int _lodCount;
int _texturesCount; int _texturesCount;
int _totalSize; int _totalSize;