From 8ee7f4258dc3703776f6221a54faaaeab327ea59 Mon Sep 17 00:00:00 2001 From: beardog_uk Date: Thu, 12 Mar 2009 14:23:05 +0000 Subject: [PATCH] classes for history support, initial revision; git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1070 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- .../src/gui/im_history/IMHistoryItem.cpp | 93 +++++++++ .../src/gui/im_history/IMHistoryItem.h | 41 ++++ .../src/gui/im_history/IMHistoryKeeper.cpp | 178 +++++++++++++++++ .../src/gui/im_history/IMHistoryKeeper.h | 39 ++++ .../src/gui/im_history/IMHistoryReader.cpp | 179 ++++++++++++++++++ .../src/gui/im_history/IMHistoryReader.h | 31 +++ .../src/gui/im_history/IMHistoryWriter.cpp | 62 ++++++ .../src/gui/im_history/IMHistoryWriter.h | 29 +++ 8 files changed, 652 insertions(+) create mode 100644 retroshare-gui/src/gui/im_history/IMHistoryItem.cpp create mode 100644 retroshare-gui/src/gui/im_history/IMHistoryItem.h create mode 100644 retroshare-gui/src/gui/im_history/IMHistoryKeeper.cpp create mode 100644 retroshare-gui/src/gui/im_history/IMHistoryKeeper.h create mode 100644 retroshare-gui/src/gui/im_history/IMHistoryReader.cpp create mode 100644 retroshare-gui/src/gui/im_history/IMHistoryReader.h create mode 100644 retroshare-gui/src/gui/im_history/IMHistoryWriter.cpp create mode 100644 retroshare-gui/src/gui/im_history/IMHistoryWriter.h diff --git a/retroshare-gui/src/gui/im_history/IMHistoryItem.cpp b/retroshare-gui/src/gui/im_history/IMHistoryItem.cpp new file mode 100644 index 000000000..263218748 --- /dev/null +++ b/retroshare-gui/src/gui/im_history/IMHistoryItem.cpp @@ -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()) ; +} \ No newline at end of file diff --git a/retroshare-gui/src/gui/im_history/IMHistoryItem.h b/retroshare-gui/src/gui/im_history/IMHistoryItem.h new file mode 100644 index 000000000..5d0fcae54 --- /dev/null +++ b/retroshare-gui/src/gui/im_history/IMHistoryItem.h @@ -0,0 +1,41 @@ +#ifndef __IM_history_item__ +#define __IM_history_item__ + +#include +#include + + + +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 + diff --git a/retroshare-gui/src/gui/im_history/IMHistoryKeeper.cpp b/retroshare-gui/src/gui/im_history/IMHistoryKeeper.cpp new file mode 100644 index 000000000..52290494d --- /dev/null +++ b/retroshare-gui/src/gui/im_history/IMHistoryKeeper.cpp @@ -0,0 +1,178 @@ +#include "IMHistoryKeeper.h" + +#include +#include + +#include //for qSort + +#include + +#include "IMHistoryReader.h" +#include "IMHistoryWriter.h" + +//#include + + +//============================================================================= + +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 ril;//result item list + + QListIterator hii(hitems); + hii.toBack(); + while (hii.hasPrevious() && (messFound=messagesCount) + break; + } + } +/* + QList::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& itemList, + QStringList& strList) +{ + strList.clear(); +/* + QList::const_iterator hii=itemList.constBegin(); + for(hii; hii!= itemList.constEnd(); hii++) + { + strList.append( hii->text() ); + } + */ + QListIterator hii(itemList); + hii.toBack(); + while (hii.hasPrevious() ) + { + IMHistoryItem hitem = hii.previous(); + + QString tline; + + tline = QString("%1 %2 : " + "%3") + .arg(hitem.time().toString( Qt::TextDate ) ) + .arg(hitem.sender()) + .arg(hitem.text()) ; + + strList.append( tline ); + } +} + +//============================================================================= + diff --git a/retroshare-gui/src/gui/im_history/IMHistoryKeeper.h b/retroshare-gui/src/gui/im_history/IMHistoryKeeper.h new file mode 100644 index 000000000..ced9b1fcc --- /dev/null +++ b/retroshare-gui/src/gui/im_history/IMHistoryKeeper.h @@ -0,0 +1,39 @@ +#ifndef _HISTORY_KEEPER_H_ +#define _HISTORY_KEEPER_H_ + +#include +#include +#include +#include + +//#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& itemList, QStringList& strList); + + QList hitems; + QString hfName ; //! history file name + QString lastErrorMessage; + +}; + +#endif // _HISTORY_KEEPER_H_ diff --git a/retroshare-gui/src/gui/im_history/IMHistoryReader.cpp b/retroshare-gui/src/gui/im_history/IMHistoryReader.cpp new file mode 100644 index 000000000..aa7ca32cc --- /dev/null +++ b/retroshare-gui/src/gui/im_history/IMHistoryReader.cpp @@ -0,0 +1,179 @@ +#include "IMHistoryReader.h" + +#include + +#include + +//============================================================================= + +IMHistoryReader::IMHistoryReader() + :errMess("No error") +{ + // nothing to do here +} + +//============================================================================= + +bool +IMHistoryReader::read(QList& resultList, + const QString fileName) +{ + errMess = "No error"; + + QList 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::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 +IMHistoryReader::readHistory() +{ + Q_ASSERT(isStartElement()); + + // qDebug()<< " " << "node with message " << name() ; + + QList rez; + + while (!atEnd()) + { + readNext(); + if (isEndElement()) + break; + + if (isStartElement()) + { + if ( name() == "message" ) + { + IMHistoryItem item = readMessage(); + rez.append(item); + } + else + readUnknownElement(); + } + } + + return rez; +} + +//============================================================================= +//#include +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; +} + +//============================================================================= +//============================================================================= +//============================================================================= diff --git a/retroshare-gui/src/gui/im_history/IMHistoryReader.h b/retroshare-gui/src/gui/im_history/IMHistoryReader.h new file mode 100644 index 000000000..ad6acaf4e --- /dev/null +++ b/retroshare-gui/src/gui/im_history/IMHistoryReader.h @@ -0,0 +1,31 @@ +#ifndef __IM_History_reader__ +#define __IM_History_reader__ + +#include + +#include +#include + +#include "IMHistoryItem.h" + +class IMHistoryReader : public QXmlStreamReader +{ +public: + IMHistoryReader(); + + bool read(QList& resultList, + const QString fileName ); + + QString errorMessage(); + +private: + void readUnknownElement(); + QList readHistory(); + IMHistoryItem readMessage(); + + QString errMess; +} ; + + +#endif + diff --git a/retroshare-gui/src/gui/im_history/IMHistoryWriter.cpp b/retroshare-gui/src/gui/im_history/IMHistoryWriter.cpp new file mode 100644 index 000000000..204478937 --- /dev/null +++ b/retroshare-gui/src/gui/im_history/IMHistoryWriter.cpp @@ -0,0 +1,62 @@ +#include "IMHistoryWriter.h" + +#include + +#include + +#include + +//============================================================================= + +IMHistoryWriter::IMHistoryWriter() + :errMess("No error") +{ + // nothing to do here +} + +//============================================================================= + +bool +IMHistoryWriter::write(QList& 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(""); + 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; +} \ No newline at end of file diff --git a/retroshare-gui/src/gui/im_history/IMHistoryWriter.h b/retroshare-gui/src/gui/im_history/IMHistoryWriter.h new file mode 100644 index 000000000..8b2583615 --- /dev/null +++ b/retroshare-gui/src/gui/im_history/IMHistoryWriter.h @@ -0,0 +1,29 @@ +#ifndef __IM_History_writer__ +#define __IM_History_writer__ + +#include + +#include +//#include + +#include "IMHistoryItem.h" + +class IMHistoryWriter : public QXmlStreamWriter +{ +public: + IMHistoryWriter(); + + bool write(QList& itemList, + const QString fileName ); + + QString errorMessage(); + +private: + + + QString errMess; +} ; + + + +#endif \ No newline at end of file