messagewrapper.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "messagewrapper.h"
  2. #include "MyXmppClient.h"
  3. #include "qmlxmppclient.h"
  4. #include <QDebug>
  5. MessageWrapper::MessageWrapper(QObject *parent) : QObject(parent)
  6. {
  7. //qmlObj = reinterpret_cast<QDeclarativeView*>(parent);
  8. mlm = NULL;
  9. mlm = new MsgListModel( this ); //??
  10. //QDeclarativeEngine *engine = qmlObj->engine();
  11. //engine->rootContext()->setContextProperty( "cppListOfMessages", mlm );
  12. openChatJid = "";
  13. m_myBareJid = "";
  14. }
  15. void MessageWrapper::messageReceived(const QXmppMessage &xmppMsg)
  16. {
  17. qDebug() << "MessageWrapper::messageReceived(): xmppMsg.type():" << xmppMsg.type();
  18. if( xmppMsg.state() == QXmppMessage::Active ) {
  19. this->textMessage(xmppMsg);
  20. } else if( xmppMsg.state() == QXmppMessage::Inactive ) {
  21. qDebug() << "QXmppMessage::Inactive";
  22. } else if( xmppMsg.state() == QXmppMessage::Gone ) {
  23. qDebug() << "QXmppMessage::Gone";
  24. } else if( xmppMsg.state() == QXmppMessage::Composing ) {
  25. //this->startWriteMessage(xmppMsg);
  26. } else if( xmppMsg.state() == QXmppMessage::Paused ) {
  27. //this->pauseWriteMessage(xmppMsg);
  28. } else {
  29. this->textMessage(xmppMsg); //????
  30. qDebug() << "MessageWrapper::messageReceived(): xmppMsg.state():" << xmppMsg.state();
  31. }
  32. }
  33. void MessageWrapper::textMessage(const QXmppMessage &xmppMsg)
  34. {
  35. QString id = xmppMsg.id();
  36. QString msg = xmppMsg.body();
  37. msg = this->parseMsgOnLink( msg );
  38. MeegimMessage mm;
  39. mm.dlr = false;
  40. if( (xmppMsg.stamp().isNull()) || (!xmppMsg.stamp().isValid()) ) {
  41. mm.date = QDateTime::currentDateTime();
  42. } else {
  43. mm.date = xmppMsg.stamp();
  44. }
  45. mm.msg = msg;
  46. mm.id = id;
  47. QString chatWithJid;
  48. //qDebug() << MyXmppClient::getBareJidByJid( xmppMsg.from() ) << " *** " << m_myBareJid ;
  49. if ( MyXmppClient::getBareJidByJid( xmppMsg.from() ) == m_myBareJid ) {
  50. mm.myMsg = true;
  51. chatWithJid = xmppMsg.to();
  52. } else {
  53. mm.myMsg = false;
  54. chatWithJid = xmppMsg.from();
  55. }
  56. QString resource("");
  57. if( chatWithJid.indexOf('/') >= 0 ) {
  58. resource = chatWithJid.split('/')[1];
  59. chatWithJid = chatWithJid.split('/')[0];
  60. }
  61. mm.resource = resource;
  62. if( ! listOfChats.contains(chatWithJid) ) {
  63. /* это первое сообщение от этого jid
  64. * Инициализируем список сообщени для этого jid
  65. */
  66. QList<MeegimMessage> *lm = new QList<MeegimMessage>();
  67. lm->append( mm );
  68. listOfChats[ chatWithJid ] = lm;
  69. qDebug() << "new msg: lenList:" << lm->length() << " jid:" << chatWithJid;
  70. emit openChat( chatWithJid );
  71. } else {
  72. QList<MeegimMessage> *lm = listOfChats[ chatWithJid ];
  73. lm->append( mm );
  74. listOfChats[ chatWithJid ] = lm;
  75. qDebug() << "exist msg: lenList:" << lm->length() << " jid:" << chatWithJid;
  76. }
  77. qDebug() << "textMessage: listOfChats.contains(" << chatWithJid << "):" << listOfChats.contains(chatWithJid) << " len:" << listOfChats.size() << " openChatJid:" << openChatJid;
  78. QString dataTimeMsg = mm.date.toString("hh:mm:ss");
  79. MsgItemModel *mim = new MsgItemModel( mm.id, mm.resource, dataTimeMsg, mm.msg, mm.dlr, mm.myMsg, MSGTYPE_TEXT, this );
  80. if( (mlm != NULL) && (openChatJid == chatWithJid) ) {
  81. mlm->append( mim );
  82. }
  83. //qDebug() << chatWithJid << " (" << mm.id << ") >" << msg << " " << listOfChats.contains(chatWithJid);
  84. }
  85. void MessageWrapper::initChat( QString jid ) //Q_INVOKABLE
  86. {
  87. //openChatJid = jid; //обозначаем, к каким jid открыто окно чата //не факт,вызов этого происходит и при новом сообщ.
  88. qDebug() << "initChat(): listOfChats.contains "<< jid <<": " << listOfChats.contains(jid) << " mlm.len:" << mlm->count();
  89. if( listOfChats.contains(jid) && (openChatJid == jid) )
  90. {
  91. mlm->removeRows( 0, mlm->count() ); //TODO: может takeRows() ???
  92. QList<MeegimMessage> *currChat = listOfChats[ jid ];
  93. QList< MeegimMessage >::const_iterator itr = currChat->begin();
  94. while ( itr != currChat->end() )
  95. {
  96. MeegimMessage m = *itr;
  97. itr++;
  98. QString dataTimeMsg = m.date.toString("hh:mm:ss");
  99. MsgItemModel *mim = new MsgItemModel( m.id, m.resource, dataTimeMsg, m.msg, m.dlr, m.myMsg, MSGTYPE_TEXT, this );
  100. mlm->append(mim);
  101. }
  102. }
  103. else if( ! listOfChats.contains(jid) && (openChatJid == jid) )
  104. {
  105. mlm->removeRows( 0, mlm->count() ); //TODO: может takeRows() ???
  106. QList<MeegimMessage> *lm = new QList<MeegimMessage>();
  107. listOfChats[ jid ] = lm;
  108. }
  109. //attentions
  110. if( bufAttentions.contains(jid) )
  111. {
  112. QString ca = bufAttentions[jid];
  113. if( ca != "" )
  114. {
  115. MsgItemModel *mim = new MsgItemModel( 0, "", ca, "", false, false, MSGTYPE_ATTENTION, this );
  116. mlm->append(mim);
  117. bufAttentions[ jid ] = "";
  118. }
  119. }
  120. }
  121. void MessageWrapper::clearChat(QString bareJid)
  122. {
  123. if( listOfChats.contains(bareJid) )
  124. {
  125. QList<MeegimMessage> *lm = listOfChats[ bareJid ];
  126. int L = lm->length();
  127. for( int k=0; k<L; k++ )
  128. {
  129. lm->takeLast();
  130. }
  131. mlm->removeRows( 0, mlm->count() );
  132. }
  133. }
  134. void MessageWrapper::messageDelivered( const QString &jid, const QString &id )
  135. {
  136. QString pureJid = jid.split('/')[0];
  137. qDebug() << "MessageWrapper::messageDelivered(): " << pureJid << " " << id << " listOfChats.len:" << listOfChats.size() << " jid:" << listOfChats.contains( pureJid );
  138. if( listOfChats.contains( pureJid ) ) {
  139. QList<MeegimMessage> *list_mm = listOfChats[ pureJid ];
  140. QList<MeegimMessage>::iterator itr = list_mm->end(); /* с конца искть нужное сообщ. будет быстрее :) */
  141. while( itr != list_mm->begin() )
  142. {
  143. itr--;
  144. qDebug() << "DLR*:" << (*itr).id;
  145. if( (*itr).id == id ) {
  146. (*itr).dlr = true;
  147. listOfChats[ pureJid ] = list_mm;
  148. break;
  149. }
  150. }
  151. MsgItemModel *item = reinterpret_cast<MsgItemModel*>( mlm->find( id ) );
  152. if( item != 0 ) {
  153. item->setMsgDlr( true );
  154. }
  155. }
  156. }
  157. void MessageWrapper::removeListOfChat( QString &bareJid )
  158. {
  159. if( listOfChats.contains(bareJid) ) {
  160. QList<MeegimMessage> *list_mm = listOfChats.take( bareJid );
  161. if( list_mm != NULL ) {
  162. delete list_mm;
  163. }
  164. qDebug() << "listOfChats.contains("<<bareJid<<"):"<<listOfChats.contains(bareJid);
  165. }
  166. }
  167. QString MessageWrapper::parseMsgOnLink( const QString &inString ) const
  168. {
  169. QString outString = "";
  170. int pos = 0;
  171. int pos_space = 0;
  172. bool strangeTagOpen = false;
  173. bool strangeTagClose = false;
  174. qDebug() << inString;
  175. while( (pos = inString.indexOf("http://", pos)) >= 0 )
  176. {
  177. outString += inString.mid(pos_space, pos - pos_space);
  178. QString prevSmb = inString.mid( pos-1, 1 );
  179. if( prevSmb == "<" ) { strangeTagOpen = true; }
  180. pos_space = inString.indexOf( " ", pos );
  181. if( pos_space < 0 ) {
  182. pos_space = inString.indexOf( "\"", pos );
  183. if( pos_space < 0 ) {
  184. pos_space = inString.indexOf( ">", pos );
  185. strangeTagClose = true;
  186. }
  187. }
  188. QString link = inString.mid( pos, pos_space-pos );
  189. QString nLink = "<a href=\"" + link + "\">" + link + "</a>";
  190. if( strangeTagOpen && strangeTagClose ) {
  191. nLink = "a href=\"" + link + "\">" + link + "</a";
  192. }
  193. //qDebug() << "pos="<<pos<<" pos_space="<<pos_space<<" ["<<link<<"]";
  194. outString += nLink;
  195. pos = pos_space;
  196. strangeTagOpen = false;
  197. strangeTagClose = false;
  198. }
  199. if( pos_space >= 0 ) {
  200. outString += inString.mid(pos_space);
  201. }
  202. //qDebug() << outString;
  203. return outString;
  204. }
  205. void MessageWrapper::attention(const QString &bareJid, const bool isMsgMine)
  206. {
  207. QString dataTimeMsg = QDateTime::currentDateTime().toString("hh:mm:ss");
  208. if( bareJid == openChatJid )
  209. {
  210. MsgItemModel *mim = new MsgItemModel( 0, "", dataTimeMsg, "", false, isMsgMine, MSGTYPE_ATTENTION, this );
  211. mlm->append(mim);
  212. }
  213. else
  214. {
  215. bufAttentions[ bareJid ] = dataTimeMsg;
  216. }
  217. //qDebug() << "*** " << Q_FUNC_INFO <<": "<< bufAttentions.contains(bareJid) << ": "<< bufAttentions[ bareJid ] << ": " << openChatJid;
  218. }