void Bouchot::updateTribune()
{
#ifdef Q_OS_WIN32
QString homepath = getenv("USERPROFILE");
#else
QString homepath = getenv("HOME");
#endif
QString totozPath ;
totozPath = homepath ;
totozPath.append(DIRTOTOZ);
QFile file(backendFileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QString errorXmlString ;
int errorXmlLine;
int errorXmlCol;
QDomDocument domDocument ;
QXmlSimpleReader reader ;
// The following has been stolen from kk but does not work
reader.setFeature("http://xml.org/sax/features/namespaces", true);
reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
// Pour les tribunes comme dlfp
//reader.setFeature("http://trolltech.com/xml/features/report-start-end-entity", true);
// On parse le backend
QXmlInputSource input = QXmlInputSource(&file) ;
if ( !domDocument.setContent(&input, &reader, &errorXmlString, &errorXmlLine,
&errorXmlCol))
{
emit messageToMainWindow("Parsing error", tr("Parse error at line %1, column %2:\n%3")
.arg(errorXmlString)
.arg(errorXmlLine)
.arg(errorXmlCol));
file.close();
return ;
}
//reader.parse(input) ;
// The following was my first test with xml
/*
if (!domDocument.setContent(&file, true, &errorXmlString, &errorXmlLine,
&errorXmlCol))
{
emit messageToMainWindow("Parsing error", tr("Parse error at line %1, column %2:\n%3")
.arg(errorXmlString)
.arg(errorXmlLine)
.arg(errorXmlCol));
file.close();
return ;
}
*/
QDomElement root = domDocument.documentElement();
QDomNode child1 = domDocument.firstChild();
if ( child1.isProcessingInstruction())
{
QDomProcessingInstruction xml = child1.toProcessingInstruction();
QRegExp regexp("encoding = '((?:\\w|\\-)+)'");
if ( regexp.indexIn(xml.data()) > -1 )
{
qDebug(tr("Encodage detected %1").arg(regexp.cap(1).toLower()).toAscii().data());
this->encodage = regexp.cap(1).toLower() ;
}
}
if (root.tagName() != "board")
{
emit messageToMainWindow("Parsing error", "The file is not a board file.");
file.close();
return ;
}
QDomElement postBouchot = root.firstChildElement("post");
QString norloge_post = "" ;
int id_post = -1 ;
QString message_post = "" ;
QString login_post = "" ;
QString ua_post = "" ;
QString ligne = "" ;
bool updateTextBox = false ;
QRegExp rxNorloge = QRegExp("(([01]?[0-9])|(2[0-3])):[0-5][0-9](:[0-5][0-9])?" );
QRegExp rxTotoz = QRegExp("\\[:((?:\\w|\\d|-|_| |/)*)\\]");
QMap < int, messageBouchot * > mapTempo ;
QString totozName ;
while (!postBouchot.isNull())
{
id_post = postBouchot.attribute("id").toInt();
if ( mapPosts.contains(id_post) )
{
break ;
}
norloge_post = postBouchot.attribute("time");
ua_post = postBouchot.firstChildElement("info").text();
message_post = postBouchot.firstChildElement("message").text();
login_post = postBouchot.firstChildElement("login").text();
mapPosts[id_post] = new messageBouchot(norloge_post, ua_post, login_post, message_post);
mapTempo[id_post] = new messageBouchot(norloge_post, ua_post, login_post, message_post);
//ligne=tr("%1 %2 %3")
// .arg(login_post).arg(norloge_post).arg(message_post).arg(login_post);
updateTextBox = true ;
norloge_post = "" ;
id_post = -1 ;
message_post = "" ;
login_post = "" ;
ua_post = "" ;
postBouchot = postBouchot.nextSiblingElement("post");
}
QMap::const_iterator it = mapTempo.constBegin();
if ( mapTempo.count() == 0)
{
// c'est vide
}
else
{
while (it != mapTempo.constEnd())
{
norloge_post = convertNorloge(it.value()->getNorloge()).right(8) ;
message_post = (it.value()->getMessage()) ;
if ( message_post.contains(login) and login != "")
emit ontappelle(norloge_post, message_post);
//needed to know if the message answer to one of mines
bool answerToMine = false ;
// the following is for misc. formatting in a message
QStringList splittedMessage = message_post.split(" ");
for (QStringList::const_iterator it42 = splittedMessage.constBegin(); it42 != splittedMessage.constEnd(); ++it42 )
{
if (rxNorloge.indexIn(*it42) != -1 )
{
// check if the message answers to one of mine
QMap::const_iterator it43 = mapPosts.constBegin();
while ( it43 != mapPosts.constEnd() )
{
QString h = convertNorloge(it43.value()->getNorloge()).right(8) ;
if ( h.startsWith(*it42) )// The clock cited exists in the history
{
//The corresponding message was one of mines
if (it43.value()->getLogin() == login )
{
answerToMine = true ;
break ;
}
}
++it43 ;
}
// this is for the clock formatting in a message
message_post.replace(*it42, tr("%1").arg(*it42));
}
//this is for the totoz in a message
else if (rxTotoz.indexIn(*it42) != -1)
{
totozName = it42->mid(2,it42->size()-3) ;
QString totozFile = tr("%1%2.gif").arg(totozPath).arg(totozName);
//display the totoz only if I am able to do it
if (QImageReader(totozFile).canRead())
message_post.replace(*it42, tr("
")
.arg(totozFile).arg(totozName));
}
}
//format the login
//if it is empty or Anonyme< replace with the UA
if ( ( it.value()->getLogin() == "" ) or (it.value()->getLogin() == "Anonyme") )
{
if ( answerToMine )
login_post = tr("%1").arg(it.value()->getUa());
else
login_post = tr("%1").arg(it.value()->getUa());
}
else // it has a Login
{
//if it is me
if ( it.value()->getLogin() == login )
{
login_post = tr("%1").arg(it.value()->getLogin()) ;
}
else // It is not me
{
if ( answerToMine )
login_post = tr("%1").arg(it.value()->getLogin()) ;
else
login_post = tr("%1").arg(it.value()->getLogin()) ;
}
}
//Format the message in the QTextBrowser
ligne=tr("%1 %2 %3")
.arg(norloge_post).arg(login_post).arg(message_post);
//ligne=tr("%1
%2 %3")
// .arg(norloge_post).arg(login_post).arg(message_post);
PiniText->append(ligne);
delete it.value() ;
++it ;
}
}
file.close() ;
if (updateTextBox)
{
bool updateScroll = false;
QScrollBar *myscroll = PiniText->verticalScrollBar();
int maxvalue = myscroll->maximum();
if (myscroll->value() == maxvalue)
updateScroll = true ;
if ( updateScroll)
{
maxvalue = myscroll->maximum();
myscroll->setValue(maxvalue);
}
}
}