rosterxml.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "rosterxml.h"
  2. //#include "QXmppPresence.h"
  3. RosterXml::RosterXml(QObject *parent) : QObject(parent)
  4. {
  5. nameTagMuc = "muc";
  6. rosterXmlFile = "";
  7. }
  8. bool RosterXml::createRosterXML( QString &xmlFileRoster )
  9. {
  10. rosterXmlFile = xmlFileRoster;
  11. /* try to read roster.xml */
  12. QFile rosterFile(xmlFileRoster);
  13. if( !rosterFile.open( QIODevice::ReadWrite | QIODevice::Text ) )
  14. {
  15. qWarning() << Q_FUNC_INFO <<": Failed to open file for read/write: " << xmlFileRoster;
  16. return false;
  17. }
  18. QTextStream stream( &rosterFile );
  19. QString xmlDocStr = stream.readAll();
  20. rosterFile.close();
  21. qDebug() << "xmRoster [" << xmlDocStr << "]";
  22. xmlDocStr.trimmed();
  23. if( xmlDocStr == "" )
  24. {
  25. xmlDocStr = "<roster/>";
  26. }
  27. /* try to parse the xml */
  28. QString errorMsg = "";
  29. int errorLine = 0, errorColumn = 0;
  30. rosterXMLDoc.setContent( xmlDocStr, &errorMsg, &errorLine, &errorColumn );
  31. if( errorMsg != "" )
  32. {
  33. qWarning() << Q_FUNC_INFO <<": Cannot parse xml file. Error at " << errorLine << "," << errorColumn << ": "<< xmlFileRoster;
  34. rootRoster = rosterXMLDoc.createElement("roster");
  35. rosterXMLDoc.appendChild( rootRoster );
  36. }
  37. else
  38. {
  39. rootRoster = rosterXMLDoc.elementsByTagName("roster").item(0).toElement();
  40. }
  41. return true;
  42. }
  43. bool RosterXml::saveRosterXml()
  44. {
  45. QFile rosterFile(rosterXmlFile);
  46. if( !rosterFile.open( QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate) )
  47. {
  48. qWarning() << Q_FUNC_INFO <<": Failed to open file for read/write: " << rosterXmlFile;
  49. return false;
  50. }
  51. QTextStream stream( &rosterFile );
  52. qDebug() << rosterXMLDoc.toString();
  53. stream << rosterXMLDoc.toString();
  54. stream.flush();
  55. rosterFile.close();
  56. return true;
  57. }
  58. void RosterXml::addMuc( RosterItemModel *itemRoster )
  59. {
  60. QDomElement nodeMuc = rosterXMLDoc.createElement( nameTagMuc );
  61. rootRoster.appendChild( nodeMuc );
  62. QString jid = itemRoster->contactJid();
  63. bool flJidExists = this->itemExists( jid );
  64. if( flJidExists ) {
  65. return;
  66. }
  67. /* jid */
  68. QString s_jid = "jid";
  69. this->routineAddTextNode( nodeMuc, s_jid, jid );
  70. /* group */
  71. QString group = itemRoster->group();
  72. QString s_group = QString("group");
  73. this->routineAddTextNode( nodeMuc, s_group, group );
  74. this->saveRosterXml();
  75. }
  76. void RosterXml::removeMuc(QString &jidRoom)
  77. {
  78. QDomNodeList jidNodes = rosterXMLDoc.elementsByTagName( "jid" );
  79. for( unsigned int z=0; z< jidNodes.length(); z++ )
  80. {
  81. QDomNode nodeJid = jidNodes.item( z );
  82. QDomNode jN = nodeJid.firstChild();
  83. if( jN.isNull() ) { continue; }
  84. QString _jidRoom = jN.nodeValue();
  85. if( _jidRoom == jidRoom )
  86. {
  87. //qDebug() << "1^^^^^^^^^^^^^^^" << rosterXMLDoc.toString();
  88. QDomNode nodeMuc = nodeJid.parentNode();
  89. rootRoster.removeChild( nodeMuc );
  90. //qDebug() << "2^^^^^^^^^^^^^^^" << rosterXMLDoc.toString();
  91. this->saveRosterXml();
  92. }
  93. }
  94. }
  95. QList<RosterItemModel*> RosterXml::getListMuc()
  96. {
  97. QDomNodeList listOfMuc = rosterXMLDoc.elementsByTagName( nameTagMuc );
  98. QList<RosterItemModel*> listRet;
  99. for( unsigned int z=0; z < listOfMuc.length(); z++ )
  100. {
  101. QDomNode nodeMuc = listOfMuc.item( z );
  102. QDomElement domElementMuc = nodeMuc.toElement();
  103. QString jid = "";
  104. QDomNode nJid = domElementMuc.elementsByTagName("jid").item(0);
  105. QDomNode textNodeJid = nJid.firstChild();
  106. if( !textNodeJid.isNull() ) {
  107. jid = textNodeJid.nodeValue();
  108. }
  109. QString group = "";
  110. QDomNode nGroup = domElementMuc.elementsByTagName("group").item(0);
  111. QDomNode textNodeGroup = nGroup.firstChild();
  112. if( !textNodeGroup.isNull() ) {
  113. group = textNodeGroup.nodeValue();
  114. }
  115. if( jid != "" )
  116. {
  117. //QXmppPresence presence( QXmppPresence::Available );
  118. RosterItemModel *itemModel = new RosterItemModel( this );
  119. itemModel->setJid( jid );
  120. if( group != "" ) {
  121. itemModel->setGroup("Multi-user chats");
  122. }
  123. itemModel->setContactName( "" );
  124. itemModel->setAvatar( "" );
  125. itemModel->setUnreadMsg( 0 );
  126. itemModel->setTextStatus( "" );
  127. itemModel->setItemType( ROSTER_ITEM_MUC ); //1 - muc
  128. listRet.append( itemModel );
  129. }
  130. }
  131. return listRet;
  132. }
  133. void RosterXml::routineAddTextNode( QDomElement &rootNode, QString &nodeName, QString &textInfo )
  134. {
  135. QDomElement nodeInfo = rosterXMLDoc.createElement( nodeName );
  136. rootNode.appendChild( nodeInfo );
  137. QDomText textNode = rosterXMLDoc.createTextNode( textInfo );
  138. nodeInfo.appendChild( textNode );
  139. }
  140. bool RosterXml::itemExists(const QString &bareJid) const
  141. {
  142. bool retValue = false;
  143. QDomNodeList jidNodes = rosterXMLDoc.elementsByTagName( "jid" );
  144. for( unsigned int z=0; z< jidNodes.length(); z++ )
  145. {
  146. QDomNode nodeJid = jidNodes.item( z );
  147. QDomNode jN = nodeJid.firstChild();
  148. if( jN.isNull() ) { continue; }
  149. QString _bareJid = jN.nodeValue();
  150. if( _bareJid == bareJid )
  151. {
  152. retValue = true;
  153. break;
  154. }
  155. }
  156. return retValue;
  157. }
  158. void RosterXml::changeGroup(QString &bareJid, QString &newGroup)
  159. {
  160. }
  161. void RosterXml::addContact( RosterItemModel *itemRoster )
  162. {
  163. }