2009-03-18 20:13:38 +00:00
|
|
|
/****************************************************************
|
|
|
|
* RetroShare is distributed under the following license:
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 The RetroShare Team, Oleksiy Bilyanskyy
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
****************************************************************/
|
|
|
|
|
2009-03-12 14:23:05 +00:00
|
|
|
#include "IMHistoryReader.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
IMHistoryReader::IMHistoryReader()
|
|
|
|
:errMess("No error")
|
|
|
|
{
|
|
|
|
// nothing to do here
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
bool
|
|
|
|
IMHistoryReader::read(QList<IMHistoryItem>& resultList,
|
|
|
|
const QString fileName)
|
|
|
|
{
|
|
|
|
errMess = "No error";
|
|
|
|
|
|
|
|
QList<IMHistoryItem> result;
|
|
|
|
|
|
|
|
//==== check for file and open it
|
|
|
|
QFile fl(fileName);
|
|
|
|
if (fl.exists())
|
|
|
|
fl.open(QIODevice::ReadOnly);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errMess = QString("file not found (%1)").arg(fileName);
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==== set the file, and check it once more
|
|
|
|
setDevice(&fl);
|
|
|
|
|
|
|
|
if ( atEnd() )
|
|
|
|
{
|
|
|
|
errMess = "end of document reache before anything happened";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==== now, read the first element (it should be document element)
|
|
|
|
while (!atEnd())
|
|
|
|
{
|
|
|
|
readNext();
|
|
|
|
if ( isStartElement() )
|
|
|
|
{
|
|
|
|
if (name() == "history_file" &&
|
|
|
|
attributes().value("format_version") == "1.0")
|
|
|
|
{
|
|
|
|
result = readHistory();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errMess="The file is not a history file with format version 1.0";
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( error() )
|
|
|
|
errMess = errorString();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
resultList.clear();
|
|
|
|
|
|
|
|
QList<IMHistoryItem>::const_iterator hii;//history items iterator
|
|
|
|
for (hii = result.constBegin(); hii != result.constEnd(); ++hii)
|
|
|
|
resultList << *hii ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !error();
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
QString
|
|
|
|
IMHistoryReader::errorMessage()
|
|
|
|
{
|
|
|
|
QString result = errMess;
|
|
|
|
errMess = "No error" ;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
void
|
|
|
|
IMHistoryReader::readUnknownElement()
|
|
|
|
{
|
|
|
|
Q_ASSERT(isStartElement());
|
|
|
|
|
|
|
|
qDebug()<< " " << "unknown node " << name().toString();
|
|
|
|
|
|
|
|
while (!atEnd())
|
|
|
|
{
|
|
|
|
readNext();
|
|
|
|
if (isEndElement())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (isStartElement())
|
|
|
|
readUnknownElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
QList<IMHistoryItem>
|
|
|
|
IMHistoryReader::readHistory()
|
|
|
|
{
|
|
|
|
Q_ASSERT(isStartElement());
|
|
|
|
|
|
|
|
// qDebug()<< " " << "node with message " << name() ;
|
|
|
|
|
|
|
|
QList<IMHistoryItem> rez;
|
|
|
|
|
|
|
|
while (!atEnd())
|
|
|
|
{
|
|
|
|
readNext();
|
|
|
|
if (isEndElement())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (isStartElement())
|
|
|
|
{
|
|
|
|
if ( name() == "message" )
|
|
|
|
{
|
|
|
|
IMHistoryItem item = readMessage();
|
|
|
|
rez.append(item);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
readUnknownElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rez;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//#include <QXmlAttributes>
|
|
|
|
IMHistoryItem
|
|
|
|
IMHistoryReader::readMessage()
|
|
|
|
{
|
|
|
|
// Q_ASSERT(isStartElement() );
|
|
|
|
|
|
|
|
IMHistoryItem rez;// = new IMHistoryItem();
|
|
|
|
|
|
|
|
if ( isStartElement() && (name() == "message"))
|
|
|
|
{
|
|
|
|
//=== process attributes
|
|
|
|
int ti = attributes().value("dt").toString().toInt() ;
|
|
|
|
rez.setTime( QDateTime::fromTime_t( ti ) );
|
|
|
|
rez.setSender( attributes().value("sender").toString() ) ;
|
|
|
|
rez.setReceiver( attributes().value("receiver").toString() );
|
|
|
|
//=== after processing attributes, read the message text
|
|
|
|
QString tstr = readElementText();
|
|
|
|
|
|
|
|
//=== remove '\0' chars from the string. Is it a QXmlStuff bug,
|
|
|
|
// if they appear?
|
|
|
|
for(int i =0; i< tstr.length(); i++)
|
|
|
|
{
|
|
|
|
if (tstr.at(i) == '\n')
|
|
|
|
tstr.remove(i,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
rez.setText( tstr );
|
|
|
|
|
|
|
|
//qDebug() << QString(" readMessage: %1, %2, %3, %4" )
|
|
|
|
// .arg(rez.text()).arg(rez.sender())
|
|
|
|
// .arg(rez.receiver()).arg(ti) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rez;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//=============================================================================
|
|
|
|
//=============================================================================
|