classes for history support, initial revision;

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1070 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
beardog_uk 2009-03-12 14:23:05 +00:00
parent 8a08033728
commit 8ee7f4258d
8 changed files with 652 additions and 0 deletions

View File

@ -0,0 +1,93 @@
#include "IMHistoryItem.h"
//============================================================================
IMHistoryItem::IMHistoryItem()
{
}
//============================================================================
IMHistoryItem::IMHistoryItem(const QString senderID,
const QString receiverID,
const QString text,
const QDateTime time)
{
setTime(time);
setReceiver(receiverID);
setText(text);
setSender(senderID);
}
//============================================================================
QDateTime
IMHistoryItem::time() const
{
return vTime;
}
//============================================================================
QString
IMHistoryItem::sender()
{
return vSender;
}
//============================================================================
QString
IMHistoryItem::receiver()
{
return vReceiver ;
}
//============================================================================
QString
IMHistoryItem::text() const
{
return vText;
}
//============================================================================
void
IMHistoryItem::setTime(QDateTime time)
{
vTime = time;
}
//============================================================================
void
IMHistoryItem::setSender(QString sender)
{
vSender = sender ;
}
//============================================================================
void
IMHistoryItem::setReceiver(QString receiver)
{
vReceiver = receiver;
}
//============================================================================
void
IMHistoryItem::setText(QString text)
{
vText = text ;
}
//============================================================================
//! after qSort() older messages will become first
bool
IMHistoryItem::operator<(const IMHistoryItem& item) const
{
return (vTime< item.time()) ;
}

View File

@ -0,0 +1,41 @@
#ifndef __IM_history_item__
#define __IM_history_item__
#include <QDateTime>
#include <QString>
class IMHistoryItem
{
public:
IMHistoryItem();
IMHistoryItem(const QString senderID,
const QString receiverID,
const QString text,
const QDateTime time);
QDateTime time() const;
QString sender();
QString receiver();
QString text() const;
void setTime(QDateTime time);
void setTime(QString time);
void setSender(QString sender);
void setReceiver(QString receiver);
void setText(QString text);
bool operator<(const IMHistoryItem& item) const;
protected:
QDateTime vTime;
QString vSender;
QString vReceiver;
QString vText;
} ;
#endif

View File

@ -0,0 +1,178 @@
#include "IMHistoryKeeper.h"
#include <QFile>
#include <QIODevice>
#include <QtAlgorithms> //for qSort
#include <QXmlStreamReader>
#include "IMHistoryReader.h"
#include "IMHistoryWriter.h"
//#include <iostream>
//=============================================================================
IMHistoryKeeper::IMHistoryKeeper()
{
hfName = "";
};
//=============================================================================
IMHistoryKeeper::IMHistoryKeeper(QString historyFileName)
{
hfName = historyFileName ;
loadHistoryFile( historyFileName );
//setHistoryFileName( historyFileName );
//IMHistoryWriter wri;
//wri.write(hitems, hfName);
}
//=============================================================================
IMHistoryKeeper::~IMHistoryKeeper()
{
//=== we have to save all messages
qSort( hitems.begin(), hitems.end() ) ; // not nesessary, but just in case...
// it will not take a long time over
//ordered array
IMHistoryWriter wri;
wri.write(hitems, hfName);
}
//=============================================================================
void
IMHistoryKeeper::addMessage(const QString fromID, const QString toID,
const QString messageText)
{
IMHistoryItem item(fromID, toID, messageText,
QDateTime::currentDateTime());
hitems.append( item );
//std::cerr << "IMHistoryKeeper::addMessage "
// << messageText.toStdString() << "\n";
//std::cerr << "IMHistoryKeeper::addMessage count is" << hitems.count();
}
//=============================================================================
int
IMHistoryKeeper::loadHistoryFile(QString fileName)
{
qDebug() << " IMHistoryKeeper::loadHistoryFile is here";
QFile fl(fileName);
if ( !fl.exists() )
{
lastErrorMessage = QString("history file not found (%1)").arg(fileName) ;
return 1;
}
IMHistoryReader hreader;
if( !hreader.read( hitems, fileName ) )
{
lastErrorMessage = hreader.errorMessage();
return 1;
}
qSort( hitems.begin(), hitems.end() ) ;
qDebug() << " IMHistoryKeeper::loadHistoryFile finished";
return 0;
}
//=============================================================================
QString
IMHistoryKeeper::errorMessage()
{
return lastErrorMessage;
lastErrorMessage = "No error" ;
}
//=============================================================================
int
IMHistoryKeeper::getMessages(QStringList& messagesList,
const QString fromID, const QString toID,
const int messagesCount )
{
int messFound = 0;
QList<IMHistoryItem> ril;//result item list
QListIterator<IMHistoryItem> hii(hitems);
hii.toBack();
while (hii.hasPrevious() && (messFound<messagesCount))
{
IMHistoryItem hitem = hii.previous();
if ( ( (fromID.isEmpty())&&( hitem.receiver()==toID) ) ||
( (hitem.sender()==fromID)&&( hitem.receiver()==toID) ) ||
( (hitem.receiver()== fromID)&&(hitem.sender()==toID) ) )
{
ril << hitem ;
messFound++;
if (messFound>=messagesCount)
break;
}
}
/*
QList<IMHistoryItem>::iterator hii ; // history items iterator
for (hii = hitems.begin(); hii != hitems.end(); hii++)
{
//IMHistoryItem* hitem = *hii;
if ( ( (hii->sender()==fromID)&&( hii->receiver()==toID) ) ||
( (hii->receiver()== fromID)&&(hii->sender()==toID) ) )
{
ril << *hii ;
messFound++;
if (messFound>=messagesCount)
break;
}
}
*/
formStringList(ril, messagesList) ;
return 0; // successful end
}
//=============================================================================
void
IMHistoryKeeper::formStringList(QList<IMHistoryItem>& itemList,
QStringList& strList)
{
strList.clear();
/*
QList<IMHistoryItem>::const_iterator hii=itemList.constBegin();
for(hii; hii!= itemList.constEnd(); hii++)
{
strList.append( hii->text() );
}
*/
QListIterator<IMHistoryItem> hii(itemList);
hii.toBack();
while (hii.hasPrevious() )
{
IMHistoryItem hitem = hii.previous();
QString tline;
tline = QString("<strong><u>%1</u> %2 : </strong>"
"<span style=\"color:#008800\">%3</span>")
.arg(hitem.time().toString( Qt::TextDate ) )
.arg(hitem.sender())
.arg(hitem.text()) ;
strList.append( tline );
}
}
//=============================================================================

View File

@ -0,0 +1,39 @@
#ifndef _HISTORY_KEEPER_H_
#define _HISTORY_KEEPER_H_
#include <QObject>
#include <QDebug>
#include <QString>
#include <QStringList>
//#include "IMHistoryReader.h"
#include "IMHistoryItem.h"
class IMHistoryKeeper//: public QObject
{
// Q_OBJECT
public:
IMHistoryKeeper();
IMHistoryKeeper(QString historyFileName);
virtual ~IMHistoryKeeper();
//int setHistoryFileName(QString historyFileName) ;
QString errorMessage();
int getMessages(QStringList& messagesList,
const QString fromID, const QString toID,
const int messagesCount = 5);
void addMessage(const QString fromID, const QString toID,
const QString messageText);
protected:
int loadHistoryFile(QString fileName);
void formStringList(QList<IMHistoryItem>& itemList, QStringList& strList);
QList<IMHistoryItem> hitems;
QString hfName ; //! history file name
QString lastErrorMessage;
};
#endif // _HISTORY_KEEPER_H_

View File

@ -0,0 +1,179 @@
#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;
}
//=============================================================================
//=============================================================================
//=============================================================================

View File

@ -0,0 +1,31 @@
#ifndef __IM_History_reader__
#define __IM_History_reader__
#include <QXmlStreamReader>
#include <QString>
#include <QStringList>
#include "IMHistoryItem.h"
class IMHistoryReader : public QXmlStreamReader
{
public:
IMHistoryReader();
bool read(QList<IMHistoryItem>& resultList,
const QString fileName );
QString errorMessage();
private:
void readUnknownElement();
QList<IMHistoryItem> readHistory();
IMHistoryItem readMessage();
QString errMess;
} ;
#endif

View File

@ -0,0 +1,62 @@
#include "IMHistoryWriter.h"
#include <QFile>
#include <QDebug>
#include <QDateTime>
//=============================================================================
IMHistoryWriter::IMHistoryWriter()
:errMess("No error")
{
// nothing to do here
}
//=============================================================================
bool
IMHistoryWriter::write(QList<IMHistoryItem>& itemList,
const QString fileName )
{
qDebug() << " IMHistoryWriter::write is here" ;
errMess = "No error";
//==== check for file and open it
QFile fl(fileName);
if (fl.open(QIODevice::WriteOnly | QIODevice::Truncate));
else
{
errMess = QString("error opening file %1 (code %2)")
.arg(fileName).arg( fl.error() );
return false ;
}
//==== set the file, and check it once more
setDevice(&fl);
writeStartDocument();
writeDTD("<!DOCTYPE history_file>");
writeStartElement("history_file");
writeAttribute("format_version", "1.0");
foreach(IMHistoryItem item, itemList)
{
writeStartElement("message");
writeAttribute( "dt", QString::number(item.time().toTime_t()) ) ;
writeAttribute( "sender", item.sender() );
writeAttribute( "receiver", item.receiver() ) ;
writeCharacters( item.text());
writeEndElement();
}
writeEndDocument() ;
fl.close();
qDebug() << " IMHistoryWriter::write done" ;
return true;
}

View File

@ -0,0 +1,29 @@
#ifndef __IM_History_writer__
#define __IM_History_writer__
#include <QXmlStreamWriter>
#include <QString>
//#include <QStringList>
#include "IMHistoryItem.h"
class IMHistoryWriter : public QXmlStreamWriter
{
public:
IMHistoryWriter();
bool write(QList<IMHistoryItem>& itemList,
const QString fileName );
QString errorMessage();
private:
QString errMess;
} ;
#endif