2010-06-16 12:53:56 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2008 by Oliver Groß *
|
|
|
|
* z.o.gross@gmx.de *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
#include "ccrosspackage.h"
|
|
|
|
#include "cnonogram.h"
|
|
|
|
#include <QDataStream>
|
|
|
|
#include <QFile>
|
|
|
|
|
2011-12-21 11:49:33 +00:00
|
|
|
namespace libqnono {
|
2010-06-16 12:53:56 +00:00
|
|
|
//public:
|
2012-02-28 12:12:48 +00:00
|
|
|
CCrossPackage::CCrossPackage() : m_File(NULL) {}
|
|
|
|
CCrossPackage::CCrossPackage(QString fileName) : m_FileName(fileName), m_File(NULL) {}
|
2010-06-16 12:53:56 +00:00
|
|
|
|
|
|
|
CCrossPackage::~CCrossPackage() {
|
2012-02-28 12:12:48 +00:00
|
|
|
close();
|
|
|
|
|
2010-06-16 12:53:56 +00:00
|
|
|
foreach (CNonogram * i, m_PictureList) {
|
|
|
|
delete i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCrossPackage::open() {
|
2012-02-28 12:12:48 +00:00
|
|
|
close();
|
|
|
|
|
2010-06-16 12:53:56 +00:00
|
|
|
if (m_FileName.isEmpty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
qDebug("opening file: %s", m_FileName.toAscii().data());
|
2012-02-28 12:12:48 +00:00
|
|
|
m_File = new QFile(m_FileName);
|
|
|
|
if (!m_File->open(QIODevice::ReadOnly)) {
|
|
|
|
delete m_File;
|
|
|
|
m_File = NULL;
|
2010-06-16 12:53:56 +00:00
|
|
|
return false;
|
2012-02-28 12:12:48 +00:00
|
|
|
}
|
2010-06-16 12:53:56 +00:00
|
|
|
|
2012-02-28 12:12:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCrossPackage::close() {
|
|
|
|
if (m_File) {
|
|
|
|
m_File->close();
|
|
|
|
delete m_File;
|
|
|
|
m_File = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCrossPackage::readHeader() {
|
|
|
|
QDataStream in(m_File);
|
2010-06-16 12:53:56 +00:00
|
|
|
in.setVersion(QDataStream::Qt_4_0);
|
|
|
|
|
|
|
|
QString stringBuffer;
|
|
|
|
QSize sizeBuffer;
|
|
|
|
|
2012-02-28 12:12:48 +00:00
|
|
|
qDebug("reading header");
|
2010-06-16 12:53:56 +00:00
|
|
|
if (in.atEnd()) {
|
|
|
|
qCritical("invalid package file - no header");
|
2012-02-28 12:12:48 +00:00
|
|
|
close();
|
2010-06-16 12:53:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
in >> stringBuffer;
|
|
|
|
|
|
|
|
if ((stringBuffer != "QCROSSPACKAGE")) {
|
|
|
|
qCritical("invalid package file - invalid header: %s", stringBuffer.toAscii().data());
|
2012-02-28 12:12:48 +00:00
|
|
|
close();
|
2010-06-16 12:53:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in.atEnd()) {
|
|
|
|
qCritical("invalid package file - no package name");
|
2012-02-28 12:12:48 +00:00
|
|
|
close();
|
2010-06-16 12:53:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-02-28 12:12:48 +00:00
|
|
|
in >> stringBuffer;
|
|
|
|
|
|
|
|
if (stringBuffer.isEmpty()) {
|
|
|
|
qCritical("invalid package file - invalid package name");
|
|
|
|
close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_Name = stringBuffer;
|
2010-06-16 12:53:56 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-28 12:12:48 +00:00
|
|
|
bool CCrossPackage::readData() {
|
|
|
|
if (!m_File)
|
2010-06-16 12:53:56 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
m_PictureList.clear();
|
|
|
|
|
2012-02-28 12:12:48 +00:00
|
|
|
QDataStream in(m_File);
|
2010-06-16 12:53:56 +00:00
|
|
|
in.setVersion(QDataStream::Qt_4_0);
|
|
|
|
|
|
|
|
CNonogram * newPicture = NULL;
|
|
|
|
|
|
|
|
qint32 pictureCount = 0;
|
|
|
|
|
|
|
|
if (in.atEnd()) {
|
|
|
|
qCritical("invalid package file - no picture count");
|
2012-02-28 12:12:48 +00:00
|
|
|
close();
|
2010-06-16 12:53:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
in >> pictureCount;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < pictureCount && !in.atEnd(); i++) {
|
2012-02-28 12:12:48 +00:00
|
|
|
newPicture = new CNonogram();
|
|
|
|
if (!newPicture->readFromStream(in)) {
|
|
|
|
qCritical("invalid package file - invalid picture");
|
2010-06-16 12:53:56 +00:00
|
|
|
delete newPicture;
|
2012-02-28 12:12:48 +00:00
|
|
|
close();
|
2010-06-16 12:53:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_PictureList << newPicture;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < pictureCount)
|
|
|
|
qWarning("damaged package file - invalid picture count");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCrossPackage::save() {
|
|
|
|
QFile file(m_FileName);
|
|
|
|
if (!file.open(QIODevice::WriteOnly))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QDataStream out(&file);
|
|
|
|
out.setVersion(QDataStream::Qt_4_0);
|
|
|
|
|
|
|
|
out << QString("QCROSSPACKAGE");
|
|
|
|
out << m_Name;
|
|
|
|
out << (qint32)(m_PictureList.size());
|
|
|
|
|
2012-02-28 12:12:48 +00:00
|
|
|
foreach (CNonogram * i, m_PictureList)
|
|
|
|
i->writeToStream(out);
|
2010-06-16 12:53:56 +00:00
|
|
|
|
|
|
|
file.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|