MyXmppClient.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. #include "MyXmppClient.h"
  2. #include "QXmppLogger.h"
  3. #include "QXmppRosterManager.h"
  4. #include "QXmppVersionManager.h"
  5. #include "QXmppConfiguration.h"
  6. #include <QDebug>
  7. #include <QCryptographicHash>
  8. //#include <QXmppReconnectionManager.h>
  9. QString MyXmppClient::myVersion = "0.4.0";
  10. QString MyXmppClient::getBareJidByJid( const QString &jid )
  11. {
  12. QString bareJid = jid;
  13. if( jid.indexOf('/') >= 0 ) {
  14. bareJid = jid.split('/')[0];
  15. }
  16. return bareJid;
  17. }
  18. MyXmppClient::MyXmppClient() : QObject(0)
  19. {
  20. //QXmppLogger::getLogger()->setLoggingType(QXmppLogger::StdoutLogging);
  21. cacheIM = new MyCache(this);
  22. msgWrapper = new MessageWrapper(this);
  23. rosterXml = new RosterXml( this );
  24. xmppClient = new QXmppClient( this );
  25. QObject::connect( xmppClient, SIGNAL(stateChanged(QXmppClient::State)), this, SLOT(clientStateChanged(QXmppClient::State)) );
  26. QObject::connect( xmppClient, SIGNAL(messageReceived(QXmppMessage)), this, SLOT(messageReceivedSlot(QXmppMessage)) );
  27. QObject::connect( xmppClient, SIGNAL(presenceReceived(QXmppPresence)), this, SLOT(presenceReceived(QXmppPresence)) );
  28. QObject::connect( xmppClient, SIGNAL(error(QXmppClient::Error)), this, SLOT(error(QXmppClient::Error)) );
  29. QObject::connect( msgWrapper, SIGNAL(openChat(QString)), this, SLOT(openChat(QString)) );
  30. //QObject::connect( msgWrapper, SIGNAL(sendMyMsg(QXmppMessage)), xmppClient, SLOT(sendPacket(QXmppPacket)) );
  31. xmppMessageReceiptManager = new QXmppMessageReceiptManager();
  32. QObject::connect( xmppMessageReceiptManager, SIGNAL(messageDelivered(QString,QString)), msgWrapper, SLOT(messageDelivered(QString,QString)) );
  33. /* multi user chats */
  34. xmppMucManager = new QXmppMucManager();
  35. /* discovery manager */
  36. xmppDiscoveryManager = new QXmppDiscoveryManager();
  37. listModelChats = new ChatsListModel( this );
  38. flShowOffline = false;
  39. listModelRoster = new RosterListModel( this );
  40. m_bareJidLastMessage = "";
  41. m_resourceLastMessage = "";
  42. m_stateConnect = Disconnect;
  43. m_status = Offline;
  44. m_statusText = "";
  45. m_flTyping = false;
  46. m_myjid = "";
  47. m_password = "";
  48. m_host = "";
  49. m_port= 0;
  50. m_resource = "";
  51. m_chatJid = "";
  52. m_smilesmodel = 0;
  53. m_reconnectOnError = false;
  54. m_keepAlive = 60;
  55. flVCardRequest = "";
  56. qmlVCard = new QMLVCard();
  57. this->initXmppClient();
  58. rosterManager = 0;
  59. flSetPresenceWithoutAck = false;
  60. }
  61. MyXmppClient::~MyXmppClient()
  62. {
  63. /*if( xmppClient != NULL ) {
  64. delete xmppClient;
  65. }*/
  66. if( cacheIM != NULL) {
  67. delete cacheIM;
  68. }
  69. if( msgWrapper != NULL) {
  70. delete msgWrapper;
  71. }
  72. /*if( xmppMessageReceiptManager != NULL) {
  73. delete xmppMessageReceiptManager;
  74. }*/
  75. }
  76. void MyXmppClient::initXmppClient()
  77. {
  78. /* init home directory */
  79. /*bool res = */cacheIM->createHomeDir();
  80. QString p = cacheIM->getMeegIMHomePath() + "/roster.xml";
  81. rosterXml->createRosterXML( p );
  82. xmppClient->versionManager().setClientName("MeegIM");
  83. xmppClient->versionManager().setClientVersion( MyXmppClient::myVersion );
  84. /* add extension */
  85. xmppClient->addExtension( xmppMessageReceiptManager );
  86. xmppClient->addExtension( xmppMucManager );
  87. //xmppClient->addExtension( xmppDiscoveryManager );
  88. }
  89. void MyXmppClient::clientStateChanged(QXmppClient::State state)
  90. {
  91. if( state == QXmppClient::ConnectingState )
  92. {
  93. m_stateConnect = Connecting;
  94. }
  95. else if( state == QXmppClient::ConnectedState )
  96. {
  97. m_stateConnect = Connected;
  98. if( !rosterManager ) //добавленное условие исключает повторный вызов connect
  99. {
  100. rosterManager = &xmppClient->rosterManager();
  101. //FIXME: добавил к коннеткам флаг Qt::UniqueConnection в виду того, что это состояние
  102. //может вызваться дважды. Последить за поведением клиента
  103. qDebug() << Q_FUNC_INFO << " QObject::connect( rosterManager, SIGNAL(........).....)";
  104. QObject::connect( rosterManager, SIGNAL(presenceChanged(QString,QString)), this, SLOT(initPresence(const QString, const QString)), Qt::UniqueConnection );
  105. QObject::connect( rosterManager, SIGNAL(rosterReceived()), this, SLOT(initRoster()), Qt::UniqueConnection );
  106. QObject::connect( rosterManager, SIGNAL(subscriptionReceived(QString)), this, SLOT(subscriptionReceivedSlot(QString)), Qt::UniqueConnection );
  107. QObject::connect( rosterManager, SIGNAL(itemAdded(QString)), this, SLOT(itemAdded(QString)), Qt::UniqueConnection );
  108. QObject::connect( rosterManager, SIGNAL(itemRemoved(QString)), this, SLOT(itemRemoved(QString)), Qt::UniqueConnection );
  109. QObject::connect( rosterManager, SIGNAL(itemChanged(QString)), this, SLOT(itemChanged(QString)), Qt::UniqueConnection );
  110. }
  111. /* проверка статуса для первоочередной установки,
  112. * пока не принялся сигнал, непосредственно извещающий о статусе
  113. */
  114. QXmppPresence pr = xmppClient->clientPresence();
  115. this->presenceReceived( pr );
  116. }
  117. else if( state == QXmppClient::DisconnectedState )
  118. {
  119. m_stateConnect = Disconnect;
  120. this->setMyPresence( Offline, m_statusText );
  121. }
  122. emit connectingChanged();
  123. }
  124. void MyXmppClient::connectToXmppServer() //Q_INVOKABLE
  125. {
  126. //xmppConfig = mimOpt->getDefaultAccount();
  127. msgWrapper->setMyJid( m_myjid );
  128. QXmppConfiguration xmppConfig;
  129. xmppConfig.setJid( m_myjid );
  130. xmppConfig.setPassword( m_password );
  131. xmppConfig.setKeepAliveInterval( m_keepAlive );
  132. /*******************/
  133. //xmppConfig.setSaslAuthMechanism( "PLAIN" );
  134. xmppConfig.setAutoReconnectionEnabled( m_reconnectOnError );
  135. if( m_resource.isEmpty() || m_resource.isNull() ) {
  136. xmppConfig.setResource( "MeegIM" );
  137. } else {
  138. xmppConfig.setResource( m_resource );
  139. }
  140. if( !m_host.isEmpty() ) { xmppConfig.setHost( m_host ); }
  141. if( m_port != 0 ) { xmppConfig.setPort( m_port ); }
  142. xmppClient->connectToServer( xmppConfig );
  143. //TODO: здесь восстанавливть последний статус
  144. }
  145. void MyXmppClient::disconnectFromXmppServer() //Q_INVOKABLE
  146. {
  147. xmppClient->disconnectFromServer();
  148. }
  149. /* it initialises the list of contacts - roster */
  150. void MyXmppClient::initRoster()
  151. {
  152. qDebug() << "MyXmppClient::initRoster() has been called";
  153. if( ! rosterManager->isRosterReceived() ) {
  154. qDebug() << "MyXmppClient::initRoster(): roster has not received yet";
  155. return;
  156. }
  157. if( !vCardManager )
  158. {
  159. vCardManager = &xmppClient->vCardManager();
  160. QObject::connect( vCardManager, SIGNAL(vCardReceived(const QXmppVCardIq &)),
  161. this, SLOT(initVCard(const QXmppVCardIq &)),
  162. Qt::UniqueConnection );
  163. }
  164. QStringList listBareJids = rosterManager->getRosterBareJids();
  165. for( int j=0; j < listBareJids.length(); j++ )
  166. {
  167. QString bareJid = listBareJids.at(j);
  168. cacheIM->addCacheJid( bareJid );
  169. QXmppRosterIq::Item itemRoster = rosterManager->getRosterEntry( bareJid );
  170. QString name = itemRoster.name();
  171. QList<QString> listOfGroup = itemRoster.groups().toList();
  172. QString group = "";
  173. if( listOfGroup.length() > 0 ) {
  174. group = listOfGroup.at(0);
  175. }
  176. /* если в кеше есть аватарка и vCard, получим их */
  177. QString avatarPath = cacheIM->getAvatarCache( bareJid );
  178. vCardData vCdata = cacheIM->getVCard( bareJid );
  179. if ( avatarPath.isEmpty() && vCdata.isEmpty() )
  180. {
  181. /* кеш пустой, запрос vCard для текущего bareJid */
  182. vCardManager->requestVCard( bareJid );
  183. //qDebug() << "* request vCard for " << bareJid;
  184. }
  185. else
  186. {
  187. if( !avatarPath.isEmpty() ) {
  188. avatarPath = QString("file://") + avatarPath;
  189. }
  190. QString nickName = vCdata.nickName;
  191. if( (!nickName.isEmpty()) && (!nickName.isNull()) && (itemRoster.name().isEmpty()) ) {
  192. name = nickName;
  193. }
  194. }
  195. RosterItemModel *itemModel = new RosterItemModel( );
  196. itemModel->setGroup( group );
  197. itemModel->setPicStatus( this->getPicPresence( QXmppPresence::Unavailable ) );
  198. itemModel->setContactName( name );
  199. itemModel->setJid( bareJid );
  200. itemModel->setAvatar( avatarPath );
  201. itemModel->setUnreadMsg( 0 );
  202. listRoster[ bareJid ] = itemModel;
  203. }
  204. this->setQMLListRoster();
  205. emit rosterChanged();
  206. }
  207. void MyXmppClient::initPresence(const QString& bareJid, const QString& resource)
  208. {
  209. if( !listRoster.contains( bareJid ) ) {
  210. return;
  211. }
  212. QXmppPresence xmppPresence = rosterManager->getPresence( bareJid, resource );
  213. QXmppPresence::Type statusJid = xmppPresence.type();
  214. QStringList _listResources = this->getResourcesByJid( bareJid );
  215. //qDebug() << "*** initPresence():" << bareJid << " => " << resource << "_listResources:["<<_listResources<<"]";
  216. if( (_listResources.count() > 0) && (!_listResources.contains(resource)) )
  217. {
  218. /* делать выход ничего не меняя ?? */
  219. qDebug() << bareJid << "/" << resource << " ****************[" <<_listResources<<"]" ;
  220. if( statusJid == QXmppPresence::Unavailable ) {
  221. return;
  222. }
  223. }
  224. RosterItemModel *item = listRoster[ bareJid ];
  225. item->setResource( resource );
  226. QString picStatus = this->getPicPresence( xmppPresence );
  227. item->setPicStatus( picStatus );
  228. QString txtStatus = this->getTextStatus( xmppPresence.statusText(), xmppPresence );
  229. item->setTextStatus( txtStatus );
  230. qDebug() << "initPresence():" << bareJid << " => " << resource << " => " << txtStatus ;
  231. /* ищем контакт в ростере */
  232. int indxItem = -1;
  233. RosterItemModel *itemExists = (RosterItemModel*)listModelRoster->find( bareJid, indxItem );
  234. if( flShowOffline )
  235. {
  236. /* --- показать offline контакты --- */
  237. if( itemExists != 0 ) {
  238. /* обновим информацию */
  239. itemExists->copy( item );
  240. QString picStatusPrev = itemExists->picStatus();
  241. if( picStatusPrev != picStatus ) //FIXME: пока таким экзотическим методом выясняем смнился ли статус собеседника по сравнению с предыдущим
  242. {
  243. //qDebug() << "1>>> presenceJidChanged:" << bareJid << "|"<<txtStatus<<"|"<<picStatus;
  244. emit presenceJidChanged( bareJid, txtStatus, picStatus );
  245. }
  246. }
  247. }
  248. else
  249. {
  250. /* --- скрыть оффлайн контакты --- */
  251. if( (itemExists==0) && (statusJid != QXmppPresence::Unavailable ) )
  252. {
  253. /* собеседник вошел в сеть: добавляем контакт в ростер */
  254. listModelRoster->append( item );
  255. //qDebug() << "2>>> presenceJidChanged:" << bareJid << "|"<<txtStatus<<"|"<<picStatus;
  256. emit presenceJidChanged( bareJid, txtStatus, picStatus );
  257. }
  258. else if( (itemExists!=0) && (statusJid != QXmppPresence::Unavailable ) )
  259. {
  260. /* собеседник сменил статус: обновить данные на ростере */
  261. itemExists->copy( item );
  262. QString picStatusPrev = itemExists->picStatus();
  263. if( picStatusPrev != picStatus ) //FIXME: пока таким экзотическим методом выясняем смнился ли статус собеседника по сравнению с предыдущим
  264. {
  265. //qDebug() << "3>>> presenceJidChanged:" << bareJid << "|"<<txtStatus<<"|"<<picStatus;
  266. emit presenceJidChanged( bareJid, txtStatus, picStatus );
  267. }
  268. }
  269. else if( (itemExists!=0) && (statusJid == QXmppPresence::Unavailable ) )
  270. {
  271. /* собеседник вышел из сети: удаляем контакт из ростера */
  272. if( indxItem >= 0 ) {
  273. listModelRoster->takeRow( indxItem );
  274. //qDebug() << "4>>> presenceJidChanged:" << bareJid << "|"<<txtStatus<<"|"<<picStatus;
  275. emit presenceJidChanged( bareJid, txtStatus, picStatus );
  276. }
  277. }
  278. }
  279. /* смотри, есть ли контакт в открытых чатах */
  280. RosterItemModel* item_chat = reinterpret_cast<RosterItemModel*>( listModelChats->find( bareJid ) );
  281. if( item_chat )
  282. {
  283. item_chat->setResource( resource );
  284. item_chat->setPicStatus( picStatus );
  285. item_chat->setTextStatus( txtStatus );
  286. }
  287. }
  288. void MyXmppClient::setQMLListRoster()
  289. {
  290. listModelRoster->takeRows(0, listModelRoster->count() );
  291. if( flShowOffline )
  292. {
  293. /* show offline jids */
  294. QMap< QString, RosterItemModel*>::iterator itr;
  295. for ( itr = listRoster.begin(); itr != listRoster.end(); itr++ )
  296. {
  297. RosterItemModel* itemData = itr.value();
  298. listModelRoster->append( itemData );
  299. }
  300. }
  301. else if( m_stateConnect == Connected )
  302. {
  303. /* hide offline jids */
  304. QMap< QString, RosterItemModel*>::iterator itr;
  305. for ( itr = listRoster.begin(); itr != listRoster.end(); itr++ )
  306. {
  307. //QString jid = itr.key();
  308. RosterItemModel* itemData = itr.value();
  309. QXmppPresence presence( QXmppPresence::Unavailable );
  310. if( itemData->picStatus() != this->getPicPresence( presence ) ) { //пока так :(
  311. listModelRoster->append( itemData );
  312. }
  313. }
  314. }
  315. //qDebug() << "###!### m_status:" << m_status;
  316. if( m_stateConnect == Connected )
  317. {
  318. /* заполнение ростера из xml файла (muc) */
  319. QList<RosterItemModel *> listItems = rosterXml->getListMuc();
  320. QList<RosterItemModel *>::Iterator itr_xml;
  321. for ( itr_xml = listItems.begin(); itr_xml != listItems.end(); itr_xml++ )
  322. {
  323. QXmppPresence presence( QXmppPresence::Available );
  324. RosterItemModel* itemData = *itr_xml;
  325. itemData->setPicStatus( this->getPicPresence( presence ) );
  326. listModelRoster->append( itemData );
  327. }
  328. }
  329. }
  330. QString MyXmppClient::getPicPresence( const QXmppPresence &presence ) const
  331. {
  332. QString picPresenceName = "qrc:/qml/images/presence-unknown.png";
  333. QXmppPresence::Type status = presence.type();
  334. if( status != QXmppPresence::Available )
  335. {
  336. picPresenceName = "qrc:/qml/images/presence-offline.png";
  337. }
  338. else
  339. {
  340. QXmppPresence::AvailableStatusType availableStatus = presence.availableStatusType();
  341. if( (availableStatus == QXmppPresence::Online) || (availableStatus == QXmppPresence::Chat) ) {
  342. picPresenceName = "qrc:/qml/images/presence-online.png";
  343. } else if ( (availableStatus == QXmppPresence::Away) || (availableStatus == QXmppPresence::XA) ) {
  344. picPresenceName = "qrc:/qml/images/presence-away.png";
  345. } else if ( availableStatus == QXmppPresence::DND ) {
  346. picPresenceName = "qrc:/qml/images/presence-busy.png";
  347. }
  348. }
  349. return picPresenceName;
  350. }
  351. QString MyXmppClient::getTextStatus(const QString &textStatus, const QXmppPresence &presence ) const
  352. {
  353. if( (!textStatus.isEmpty()) && (!textStatus.isNull()) ) {
  354. return textStatus;
  355. }
  356. QXmppPresence::Type status = presence.type();
  357. QString txtStat = "";
  358. if( status == QXmppPresence::Unavailable )
  359. {
  360. txtStat = "Offline";
  361. }
  362. else
  363. {
  364. QXmppPresence::AvailableStatusType availableStatus = presence.availableStatusType();
  365. if( availableStatus == QXmppPresence::Online ) {
  366. txtStat = "Online";
  367. } else if ( availableStatus == QXmppPresence::Chat ) {
  368. txtStat = "Chatty";
  369. } else if ( availableStatus == QXmppPresence::Away ) {
  370. txtStat = "Away";
  371. } else if ( availableStatus == QXmppPresence::XA ) {
  372. txtStat = "Extended away";
  373. } else if ( availableStatus == QXmppPresence::DND ) {
  374. txtStat = "Do not disturb";
  375. }
  376. }
  377. return txtStat;
  378. }
  379. /* SLOT: it will be called when the vCardReceived signal will be received */
  380. void MyXmppClient::initVCard(const QXmppVCardIq &vCard)
  381. {
  382. QString bareJid = vCard.from();
  383. //qDebug() << "## initVCard: " << bareJid;
  384. RosterItemModel *item = (RosterItemModel*)listModelRoster->find( bareJid );
  385. vCardData dataVCard;
  386. if( /*item != 0*/true )
  387. {
  388. /* set nickname */
  389. QXmppRosterIq::Item itemRoster = rosterManager->getRosterEntry( bareJid );
  390. QString nickName = vCard.nickName();
  391. //if( (!nickName.isEmpty()) && (!nickName.isNull()) && (item!=0) ) {
  392. if( (!nickName.isEmpty()) && (!nickName.isNull()) && (itemRoster.name().isEmpty()) && (item!=0) ) {
  393. //qDebug() << "*****> setContactName( nickName ): " << nickName;
  394. item->setContactName( nickName );
  395. }
  396. /* avatar */
  397. bool isAvatarCreated = true;
  398. QString avatarFile = cacheIM->getAvatarCache( bareJid );
  399. if( avatarFile.isEmpty() || (flVCardRequest != "") ) {
  400. isAvatarCreated = cacheIM->setAvatarCache( bareJid, vCard.photo() );
  401. avatarFile = cacheIM->getAvatarCache( bareJid );
  402. }
  403. if( isAvatarCreated ) {
  404. avatarFile = QString("file://") + avatarFile;
  405. if( item != 0 ) {
  406. item->setAvatar( avatarFile );
  407. }
  408. }
  409. dataVCard.nickName = nickName;
  410. dataVCard.firstName = vCard.firstName();
  411. dataVCard.fullName = vCard.fullName();;
  412. dataVCard.middleName = vCard.middleName();
  413. dataVCard.lastName = vCard.lastName();
  414. dataVCard.url = vCard.url();
  415. dataVCard.eMail = vCard.email();
  416. if( flVCardRequest == bareJid ) {
  417. //qDebug() << "MyXmppClient::initVCard(): [" << flVCardRequest << "] is ready";
  418. qmlVCard->setPhoto( avatarFile );
  419. qmlVCard->setNickName( vCard.nickName() );
  420. qmlVCard->setMiddleName( vCard.middleName() );
  421. qmlVCard->setLastName( vCard.lastName() );
  422. qmlVCard->setFullName( vCard.fullName() );
  423. qmlVCard->setName( vCard.firstName() );
  424. qmlVCard->setBirthday( vCard.birthday().toString("dd.MM.yyyy") );
  425. qmlVCard->setEMail( vCard.email() );
  426. qmlVCard->setUrl( vCard.url() );
  427. qmlVCard->setJid( bareJid );
  428. flVCardRequest = "";
  429. emit vCardChanged();
  430. }
  431. //qDebug() << dataVCard.nickName;
  432. //qDebug() << dataVCard.firstName;
  433. //qDebug() << dataVCard.fullName;
  434. //qDebug() << dataVCard.middleName;
  435. //qDebug() << dataVCard.lastName;
  436. //qDebug() << dataVCard.url;
  437. //qDebug() << dataVCard.eMail;
  438. cacheIM->setVCard( bareJid, dataVCard );
  439. //qDebug() << "----------------------------";
  440. }
  441. }
  442. void MyXmppClient::setStatusText( const QString &__statusText )
  443. {
  444. if( __statusText != m_statusText )
  445. {
  446. m_statusText=__statusText;
  447. QXmppPresence myPresence = xmppClient->clientPresence();
  448. myPresence.setStatusText( __statusText );
  449. xmppClient->setClientPresence( myPresence );
  450. //mimOpt->setStatusText( __statusText );
  451. emit statusTextChanged();
  452. }
  453. }
  454. void MyXmppClient::setStatus( StatusXmpp __status)
  455. {
  456. if( __status != m_status )
  457. {
  458. QXmppPresence myPresence = xmppClient->clientPresence();
  459. if( __status == Online ) {
  460. myPresence.setType( QXmppPresence::Available );
  461. myPresence.setAvailableStatusType( QXmppPresence::Online );
  462. } else if( __status == Chat ) {
  463. myPresence.setType( QXmppPresence::Available );
  464. myPresence.setAvailableStatusType( QXmppPresence::Chat );
  465. } else if ( __status == Away ) {
  466. myPresence.setType( QXmppPresence::Available );
  467. myPresence.setAvailableStatusType( QXmppPresence::Away );
  468. } else if ( __status == XA ) {
  469. myPresence.setType( QXmppPresence::Available );
  470. myPresence.setAvailableStatusType( QXmppPresence::XA );
  471. } else if( __status == DND ) {
  472. myPresence.setType( QXmppPresence::Available );
  473. myPresence.setAvailableStatusType( QXmppPresence::DND );
  474. } else if( __status == Offline ) {
  475. myPresence.setType( QXmppPresence::Unavailable );
  476. //m_status = __status;
  477. //emit statusChanged();
  478. /* ОСТАНОВКА ПЕРЕКОННЕКТА */ //FIXME: QXmppReconnectionManager was removed
  479. //QXmppReconnectionManager *rm = xmppClient->reconnectionManager();
  480. //if( rm ) { rm->cancelReconnection(); }
  481. }
  482. xmppClient->setClientPresence( myPresence );
  483. this->presenceReceived( myPresence ); //NOTICE: извещаем клиент о смене статуса мгновенно
  484. }
  485. }
  486. void MyXmppClient::setMyPresence( StatusXmpp status, QString textStatus ) //Q_INVOKABLE
  487. {
  488. qDebug() << Q_FUNC_INFO;
  489. if( textStatus != m_statusText ) {
  490. m_statusText =textStatus;
  491. emit statusTextChanged();
  492. }
  493. QXmppPresence myPresence;
  494. if( status == Online )
  495. {
  496. if( xmppClient->state() == QXmppClient::DisconnectedState ) {
  497. this->connectToXmppServer();
  498. }
  499. myPresence.setType( QXmppPresence::Available );
  500. myPresence.setStatusText( textStatus );
  501. myPresence.setAvailableStatusType( QXmppPresence::Online );
  502. }
  503. else if( status == Chat )
  504. {
  505. if( xmppClient->state() == QXmppClient::DisconnectedState ) {
  506. this->connectToXmppServer();
  507. }
  508. myPresence.setType( QXmppPresence::Available );
  509. myPresence.setAvailableStatusType( QXmppPresence::Chat );
  510. myPresence.setStatusText( textStatus );
  511. }
  512. else if( status == Away )
  513. {
  514. if( xmppClient->state() == QXmppClient::DisconnectedState ) {
  515. this->connectToXmppServer();
  516. }
  517. myPresence.setType( QXmppPresence::Available );
  518. myPresence.setAvailableStatusType( QXmppPresence::Away );
  519. myPresence.setStatusText( textStatus );
  520. }
  521. else if( status == XA )
  522. {
  523. if( xmppClient->state() == QXmppClient::DisconnectedState ) {
  524. this->connectToXmppServer();
  525. }
  526. myPresence.setType( QXmppPresence::Available );
  527. myPresence.setAvailableStatusType( QXmppPresence::XA );
  528. myPresence.setStatusText( textStatus );
  529. }
  530. else if( status == DND )
  531. {
  532. if( xmppClient->state() == QXmppClient::DisconnectedState ) {
  533. this->connectToXmppServer();
  534. }
  535. myPresence.setType( QXmppPresence::Available );
  536. myPresence.setAvailableStatusType( QXmppPresence::DND );
  537. myPresence.setStatusText( textStatus );
  538. }
  539. else if( status == Offline )
  540. {
  541. if( (xmppClient->state() == QXmppClient::ConnectedState) || (xmppClient->state() == QXmppClient::ConnectingState) )
  542. {
  543. xmppClient->disconnectFromServer();
  544. }
  545. myPresence.setType( QXmppPresence::Unavailable );
  546. /* все контакты offline */
  547. QMap< QString, RosterItemModel*>::iterator itr;
  548. for ( itr = listRoster.begin(); itr != listRoster.end(); itr++ )
  549. {
  550. QXmppPresence presnc( QXmppPresence::Unavailable );
  551. RosterItemModel* itemData = itr.value();
  552. QString pic = this->getPicPresence( presnc );
  553. itemData->setPicStatus( pic );
  554. QString st = this->getTextStatus( "", presnc );
  555. itemData->setTextStatus( st );
  556. }
  557. /* извещаем об оффлайн состоянии */
  558. //m_status = status;
  559. //this->setQMLListRoster();
  560. //emit statusChanged();
  561. /* ОСТАНОВКА ПЕРЕКОННЕКТА */ //FIXME: QXmppReconnectionManager was removed
  562. //QXmppReconnectionManager *rm = xmppClient->reconnectionManager();
  563. //if( rm ) { rm->cancelReconnection(); }
  564. }
  565. xmppClient->setClientPresence( myPresence );
  566. this->presenceReceived( myPresence ); //NOTICE: извещаем клиент о смене статуса мгновенно
  567. if( status == Offline ) {
  568. this->setQMLListRoster();
  569. }
  570. }
  571. /* it sends information about typing : typing is started */
  572. void MyXmppClient::typingStart(QString bareJid, QString resource) //Q_INVOKABLE
  573. {
  574. //qDebug() << bareJid << " " << "start typing...";
  575. QXmppMessage xmppMsg;
  576. QString jid_to = bareJid;
  577. if( resource == "" ) {
  578. jid_to += "/resource";
  579. } else {
  580. jid_to += "/" + resource;
  581. }
  582. xmppMsg.setTo( jid_to );
  583. QString jid_from = m_myjid + "/" + xmppClient->configuration().resource();
  584. xmppMsg.setFrom( jid_from );
  585. xmppMsg.setReceiptRequested( false );
  586. QDateTime currTime = QDateTime::currentDateTime();
  587. xmppMsg.setStamp( currTime );
  588. xmppMsg.setState( QXmppMessage::Composing );
  589. xmppClient->sendPacket( xmppMsg );
  590. }
  591. /* it sends information about typing : typing is stoped */
  592. void MyXmppClient::typingStop(QString bareJid, QString resource) //Q_INVOKABLE
  593. {
  594. //qDebug() << bareJid << " " << "stop typing...";
  595. QXmppMessage xmppMsg;
  596. QString jid_to = bareJid;
  597. if( resource == "" ) {
  598. jid_to += "/resource";
  599. } else {
  600. jid_to += "/" + resource;
  601. }
  602. xmppMsg.setTo( jid_to );
  603. QString jid_from = m_myjid + "/" + xmppClient->configuration().resource();
  604. xmppMsg.setFrom( jid_from );
  605. xmppMsg.setReceiptRequested( false );
  606. QDateTime currTime = QDateTime::currentDateTime();
  607. xmppMsg.setStamp( currTime );
  608. xmppMsg.setState( QXmppMessage::Paused );
  609. xmppClient->sendPacket( xmppMsg );
  610. }
  611. void MyXmppClient::openChat( QString bareJid ) //Q_INVOKABLE
  612. {
  613. //qDebug() << "MyXmppClient::openChat(): new chat was opened:" << bareJid;
  614. /* поиск jid в ростере */
  615. RosterItemModel *itemRoster = reinterpret_cast<RosterItemModel*>( listModelRoster->find( bareJid ) );
  616. /* поиск соответствующего jid в открытых чатах */
  617. RosterItemModel* item = reinterpret_cast<RosterItemModel*>( listModelChats->find( bareJid ) );
  618. bool isMuc = false;
  619. if( (item == 0) && (itemRoster != 0) ) // исключаем вторичное добавление
  620. {
  621. listModelChats->append(itemRoster);
  622. isMuc = (itemRoster->itemType() == 1);
  623. }
  624. else if( (item == 0) && (itemRoster == 0) ) //сообщение от собеседника которого нет в ростере
  625. {
  626. //qDebug() << "XXX:" << bareJid;
  627. QXmppPresence presence( QXmppPresence::Unavailable );
  628. RosterItemModel *newItem = new RosterItemModel( );
  629. newItem->setGroup( "" );
  630. newItem->setPicStatus( this->getPicPresence( presence ) );
  631. newItem->setContactName( "" );
  632. newItem->setJid( bareJid );
  633. newItem->setAvatar( "" );
  634. newItem->setUnreadMsg( 0 );
  635. listModelChats->append( newItem );
  636. }
  637. emit chatOpened( bareJid, isMuc );
  638. emit openChatsChanged( bareJid, isMuc );
  639. msgWrapper->initChat( bareJid );
  640. this->resetUnreadMessages( bareJid );
  641. }
  642. void MyXmppClient::closeChat( QString bareJid ) //Q_INVOKABLE
  643. {
  644. int row = -1;
  645. this->resetUnreadMessages( bareJid );
  646. RosterItemModel *item = reinterpret_cast<RosterItemModel*>( listModelChats->find( bareJid, row ) );
  647. if( (item != NULL) && (row >= 0) )
  648. {
  649. /*bool res = */listModelChats->takeRow( row );
  650. if( item->itemType() == 1 )
  651. {
  652. /* chat is closed, therefor logout from the chat */
  653. }
  654. bool isMuc = item->itemType() == ROSTER_ITEM_MUC;
  655. emit openChatsChanged( bareJid, isMuc );
  656. emit chatClosed( bareJid, isMuc );
  657. //qDebug() << "MyXmppClient::closeChat("<<bareJid<<"): row:"<<row << " result:"<<res << " listModelChats.count():" <<listModelChats->count();
  658. }
  659. msgWrapper->removeListOfChat( bareJid );
  660. }
  661. void MyXmppClient::incUnreadMessage(QString bareJid) //Q_INVOKABLE
  662. {
  663. RosterItemModel *item = (RosterItemModel*)listModelRoster->find( bareJid );
  664. if( item != 0 )
  665. {
  666. int cnt = item->unreadMsg();
  667. item->setUnreadMsg( ++cnt );
  668. }
  669. }
  670. void MyXmppClient::resetUnreadMessages(QString bareJid) //Q_INVOKABLE
  671. {
  672. RosterItemModel *item = (RosterItemModel*)listModelRoster->find( bareJid );
  673. if( item != 0 ) {
  674. item->setUnreadMsg( 0 );
  675. }
  676. RosterItemModel *itemChat = (RosterItemModel*)listModelChats->find( bareJid );
  677. if( itemChat != 0 ) {
  678. itemChat->setUnreadMsg( 0 );
  679. }
  680. }
  681. void MyXmppClient::itemAdded(const QString &bareJid )
  682. {
  683. qDebug() << "MyXmppClient::itemAdded(): " << bareJid;
  684. QStringList resourcesList = rosterManager->getResources( bareJid );
  685. /* добавляем в ростер */
  686. QXmppPresence presence( QXmppPresence::Unavailable );
  687. RosterItemModel *itemModel = new RosterItemModel( );
  688. itemModel->setGroup("");
  689. itemModel->setPicStatus( this->getPicPresence( presence ) );
  690. itemModel->setContactName("");
  691. itemModel->setJid( bareJid );
  692. itemModel->setAvatar("");
  693. itemModel->setUnreadMsg( 0 );
  694. listRoster[ bareJid ] = itemModel;
  695. /* запрашиваем присутствие */
  696. for( int L = 0; L<resourcesList.length(); L++ )
  697. {
  698. QString resource = resourcesList.at(L);
  699. this->initPresence( bareJid, resource );
  700. }
  701. }
  702. void MyXmppClient::itemChanged(const QString &bareJid )
  703. {
  704. qDebug() << "MyXmppClient::itemChanged(): " << bareJid;
  705. QXmppRosterIq::Item rosterEntry = rosterManager->getRosterEntry( bareJid );
  706. QString name = rosterEntry.name();
  707. //qDebug() << "rosterEntry.name()=" << name;
  708. RosterItemModel *item = (RosterItemModel*)listModelRoster->find( bareJid );
  709. if( item ) {
  710. item->setContactName( name );
  711. }
  712. }
  713. void MyXmppClient::itemRemoved(const QString &bareJid )
  714. {
  715. qDebug() << "MyXmppClient::itemRemoved(): " << bareJid;
  716. if( listRoster.contains( bareJid ) )
  717. {
  718. listRoster.remove( bareJid );
  719. }
  720. int indxItem = -1;
  721. RosterItemModel *itemExists = (RosterItemModel*)listModelRoster->find( bareJid, indxItem );
  722. if( itemExists )
  723. {
  724. /* удаляем контакт из ростера */
  725. if( indxItem >= 0 ) {
  726. listModelRoster->takeRow( indxItem );
  727. }
  728. }
  729. }
  730. void MyXmppClient::subscriptionReceivedSlot(const QString &bareJid )
  731. {
  732. emit this->subscriptionReceived( bareJid );
  733. }
  734. void MyXmppClient::requestVCard(QString bareJid) //Q_INVOKABLE
  735. {
  736. if (vCardManager && (flVCardRequest == "") )
  737. {
  738. vCardManager->requestVCard( bareJid );
  739. flVCardRequest = bareJid;
  740. }
  741. }
  742. void MyXmppClient::messageReceivedSlot( const QXmppMessage &xmppMsg )
  743. {
  744. QString bareJid_from = MyXmppClient::getBareJidByJid( xmppMsg.from() );
  745. QString bareJid_to = MyXmppClient::getBareJidByJid( xmppMsg.to() );
  746. qDebug() << Q_FUNC_INFO << " typeMsg: " << xmppMsg.type();
  747. if( xmppMsg.state() == QXmppMessage::Active )
  748. {
  749. qDebug() << "Msg state is QXmppMessage::Active";
  750. if( !( xmppMsg.body().isEmpty() || xmppMsg.body().isNull()) )
  751. {
  752. msgWrapper->textMessage(xmppMsg);
  753. QString jid = xmppMsg.from();
  754. if( jid.indexOf('/') >= 0 ) {
  755. QStringList sl = jid.split('/');
  756. m_bareJidLastMessage = sl[0];
  757. if( sl.count() > 1 ) {
  758. m_resourceLastMessage = sl[1];
  759. }
  760. } else {
  761. m_bareJidLastMessage = xmppMsg.from();
  762. }
  763. this->incUnreadMessage( bareJid_from );
  764. emit this->messageReceived( bareJid_from, bareJid_to );
  765. }
  766. }
  767. else if( xmppMsg.state() == QXmppMessage::Inactive )
  768. {
  769. qDebug() << "Msg state is QXmppMessage::Inactive";
  770. }
  771. else if( xmppMsg.state() == QXmppMessage::Gone )
  772. {
  773. qDebug() << "Msg state is QXmppMessage::Gone";
  774. }
  775. else if( xmppMsg.state() == QXmppMessage::Composing )
  776. {
  777. m_flTyping = true;
  778. emit typingChanged( bareJid_from, true);
  779. }
  780. else if( xmppMsg.state() == QXmppMessage::Paused )
  781. {
  782. m_flTyping = false;
  783. emit typingChanged( bareJid_from, false);
  784. }
  785. else
  786. {
  787. QString mucInvitationJid = xmppMsg.mucInvitationJid(); //в случае если это приглшение, строка будет не пустой
  788. if( xmppMsg.isAttentionRequested() )
  789. {
  790. //qDebug() << "ZZZ: attentionRequest !!! from:" <<xmppMsg.from();
  791. msgWrapper->attention( bareJid_from, false );
  792. this->incUnreadMessage( bareJid_from );
  793. emit this->messageReceived( bareJid_from, bareJid_to );
  794. }
  795. else if( mucInvitationJid.length() > 1 )
  796. {
  797. //qDebug() << "ZZZ: mucInvitationJid:" << mucInvitationJid;
  798. }
  799. else if( !( xmppMsg.body().isEmpty() || xmppMsg.body().isNull()) )
  800. {
  801. msgWrapper->textMessage(xmppMsg);
  802. QString jid = xmppMsg.from();
  803. if( jid.indexOf('/') >= 0 ) {
  804. QStringList sl = jid.split('/');
  805. m_bareJidLastMessage = sl[0];
  806. if( sl.count() > 1 ) {
  807. m_resourceLastMessage = sl[1];
  808. }
  809. } else {
  810. m_bareJidLastMessage = xmppMsg.from();
  811. }
  812. this->incUnreadMessage( bareJid_from );
  813. emit this->messageReceived( bareJid_from, bareJid_to );
  814. }
  815. qDebug() << "MessageWrapper::messageReceived(): xmppMsg.state():" << xmppMsg.state();
  816. }
  817. }
  818. void MyXmppClient::setShowOffline(const bool value)
  819. {
  820. if( flShowOffline != value )
  821. {
  822. flShowOffline = value;
  823. this->setQMLListRoster();
  824. emit showOfflineChanged();
  825. emit rosterChanged();
  826. }
  827. }
  828. QString MyXmppClient::getPicPresenceByJid(QString bareJid)
  829. {
  830. QString ret = "";
  831. RosterItemModel *itemExists = (RosterItemModel*)listModelRoster->find( bareJid );
  832. if( itemExists ) {
  833. ret = itemExists->picStatus();
  834. }
  835. return ret;
  836. }
  837. QString MyXmppClient::getStatusTextByJid(QString bareJid)
  838. {
  839. QString ret = "";
  840. RosterItemModel *itemExists = (RosterItemModel*)listModelRoster->find( bareJid );
  841. if( itemExists ) {
  842. ret = itemExists->textStatus();
  843. }
  844. return ret;
  845. }
  846. QString MyXmppClient::getAvatarByJid(QString bareJid)
  847. {
  848. QString ret = "";
  849. RosterItemModel *itemExists = (RosterItemModel*)listModelRoster->find( bareJid );
  850. if( itemExists ) {
  851. ret = itemExists->picAvatar();
  852. }
  853. return ret;
  854. }
  855. bool MyXmppClient::sendMyMessage(QString bareJid, QString resource, QString msgBody) //Q_INVOKABLE
  856. {
  857. if( msgBody == "" ) { return false; }
  858. QXmppMessage xmppMsg; //???
  859. QString jid_from = bareJid;
  860. if( resource == "" ) {
  861. jid_from += "/resource";
  862. } else {
  863. jid_from += "/" + resource;
  864. }
  865. xmppMsg.setTo( jid_from );
  866. QString jid_to = m_myjid + "/" + xmppClient->configuration().resource();
  867. xmppMsg.setFrom( jid_to );
  868. //qDebug() << "SEND MSG: from:[" << jid_to << "] to:[" << jid_from << "] body:[" << msgBody << "]";
  869. /*QTextDocument doc;
  870. doc.setHtml( msgBody );
  871. xmppMsg.setBody( doc.toPlainText() );*/
  872. xmppMsg.setBody( msgBody );
  873. xmppMsg.setReceiptRequested( true ); /* request DLR */
  874. xmppMsg.setState( QXmppMessage::Active );
  875. //qDebug() << "MyXmppClient::sendMyMessage: "<<jid_from<<" "<<jid_to<<" "<<msgBody;
  876. xmppClient->sendPacket( xmppMsg );
  877. this->messageReceivedSlot( xmppMsg );
  878. return true;
  879. }
  880. QStringList MyXmppClient::getResourcesByJid(QString bareJid)
  881. {
  882. return rosterManager->getResources(bareJid);
  883. }
  884. void MyXmppClient::presenceReceived( const QXmppPresence & presence )
  885. {
  886. QString jid = presence.from();
  887. QString bareJid = jid;
  888. QString resource = "";
  889. if( jid.indexOf('/') >= 0 ) {
  890. bareJid = jid.split('/')[0];
  891. resource = jid.split('/')[1];
  892. }
  893. //this->initPresence( bareJid, resource );
  894. QString myResource = xmppClient->configuration().resource();
  895. //qDebug() << "### MyXmppClient::presenceReceived():" << bareJid << "|" << resource << "|" << myResource << "|" << presence.from() << "|" << presence.type()<< "|" << presence.availableStatusType();
  896. if( (((presence.from()).indexOf( m_myjid ) >= 0) && (resource == myResource)) || ((bareJid == "") && (resource == "")) )
  897. {
  898. QXmppPresence::Type __type = presence.type();
  899. if( __type == QXmppPresence::Unavailable )
  900. {
  901. m_status = Offline;
  902. }
  903. else
  904. {
  905. QXmppPresence::AvailableStatusType __status = presence.availableStatusType();
  906. if( __status == QXmppPresence::Online ) {
  907. m_status = Online;
  908. } else if( __status == QXmppPresence::Chat ) {
  909. m_status = Chat;
  910. } else if ( __status == QXmppPresence::Away ) {
  911. m_status = Away;
  912. } else if ( __status == QXmppPresence::XA ) {
  913. m_status = XA;
  914. } else if( __status == QXmppPresence::DND ) {
  915. m_status = DND;
  916. }
  917. }
  918. emit statusChanged();
  919. //qDebug() << "^^^ MyXmppClient::presenceReceived(): for " << bareJid << " status is " << presence.type();
  920. }
  921. }
  922. void MyXmppClient::error(QXmppClient::Error e)
  923. {
  924. QString errString;
  925. if( e == QXmppClient::SocketError ) {
  926. errString = "Socket error";
  927. } else if( e == QXmppClient::KeepAliveError ) {
  928. errString = "Keep alive error";
  929. } else if( e == QXmppClient::XmppStreamError ) {
  930. errString = "Xmpp stream error";
  931. }
  932. if( !errString.isNull() )
  933. {
  934. /* проверяем и устанавливаем текущее состояние клиента */
  935. QXmppPresence pr = xmppClient->clientPresence();
  936. this->presenceReceived( pr );
  937. /* ОСТАНОВКА ПЕРЕКОННЕКТА */
  938. //если нужен переконнект при ошибке то не выполнять две строки ниже
  939. if( !m_reconnectOnError ) {
  940. QXmppPresence presence( QXmppPresence::Unavailable );
  941. xmppClient->setClientPresence( presence );
  942. }
  943. /* извещаем об ошибке */
  944. emit errorHappened( errString );
  945. }
  946. }
  947. /*--- add/remove contact ---*/
  948. void MyXmppClient::addContact( QString bareJid, QString nick, QString group, bool sendSubscribe )
  949. {
  950. //qDebug() << "MyXmppClient::addContact(" << bareJid << ", " << nick << ", " << group << ", " << sendSubscribe << ")";
  951. if( rosterManager )
  952. {
  953. QSet<QString> gr;
  954. QString n;
  955. if( !(group.isEmpty() || group.isNull()) ) { gr.insert( group ); }
  956. if( !(nick.isEmpty() || nick.isNull()) ) { n = nick; }
  957. rosterManager->addItem(bareJid, n, gr );
  958. if( sendSubscribe ) {
  959. rosterManager->subscribe( bareJid );
  960. }
  961. }
  962. }
  963. void MyXmppClient::removeContact( QString bareJid ) //Q_INVOKABLE
  964. {
  965. int row = 0;
  966. RosterItemModel *itemExists = (RosterItemModel*)listModelRoster->find( bareJid, row );
  967. if( itemExists )
  968. {
  969. int type = itemExists->itemType();
  970. if ( type == 1 )
  971. {
  972. listModelRoster->takeRow( row );
  973. rosterXml->removeMuc( bareJid );
  974. }
  975. else
  976. {
  977. if( rosterManager ) {
  978. rosterManager->removeItem( bareJid );
  979. }
  980. }
  981. }
  982. }
  983. void MyXmppClient::renameContact(QString bareJid, QString name) //Q_INVOKABLE
  984. {
  985. //qDebug() << "MyXmppClient::renameContact(" << bareJid << ", " << name << ")" ;
  986. if( rosterManager ) {
  987. rosterManager->renameItem( bareJid, name );
  988. }
  989. }
  990. bool MyXmppClient::subscribe(const QString bareJid) //Q_INVOKABLE
  991. {
  992. //qDebug() << "MyXmppClient::subscribe(" << bareJid << ")" ;
  993. bool res = false;
  994. if( rosterManager && (!bareJid.isEmpty()) && (!bareJid.isNull()) ) {
  995. res = rosterManager->subscribe( bareJid );
  996. }
  997. return res;
  998. }
  999. bool MyXmppClient::unsubscribe(const QString bareJid) //Q_INVOKABLE
  1000. {
  1001. //qDebug() << "MyXmppClient::unsubscribe(" << bareJid << ")" ;
  1002. bool res = false;
  1003. if( rosterManager && (!bareJid.isEmpty()) && (!bareJid.isNull()) ) {
  1004. res = rosterManager->unsubscribe( bareJid );
  1005. }
  1006. return res;
  1007. }
  1008. bool MyXmppClient::acceptSubscribtion(const QString bareJid) //Q_INVOKABLE
  1009. {
  1010. //qDebug() << "MyXmppClient::acceptSubscribtion(" << bareJid << ")" ;
  1011. bool res = false;
  1012. if( rosterManager && (!bareJid.isEmpty()) && (!bareJid.isNull()) ) {
  1013. res = rosterManager->acceptSubscription( bareJid );
  1014. }
  1015. return res;
  1016. }
  1017. bool MyXmppClient::rejectSubscribtion(const QString bareJid) //Q_INVOKABLE
  1018. {
  1019. //qDebug() << "MyXmppClient::rejectSubscribtion(" << bareJid << ")" ;
  1020. bool res = false;
  1021. if( rosterManager && (!bareJid.isEmpty()) && (!bareJid.isNull()) ) {
  1022. res = rosterManager->refuseSubscription( bareJid );
  1023. }
  1024. return res;
  1025. }
  1026. void MyXmppClient::addMucRoster( QString mucJid, QString mucSubject, bool addToRoster )
  1027. {
  1028. /* ищем такой jid в ростере */
  1029. RosterItemModel *itemExists = (RosterItemModel*)listModelRoster->find( mucJid );
  1030. QXmppPresence presence( QXmppPresence::Available );
  1031. /* если в ростере не нашли, добавляем */
  1032. RosterItemModel *newItem = itemExists;
  1033. if( itemExists == 0 ) {
  1034. newItem = new RosterItemModel( );
  1035. newItem->setGroup( "Multi-user chats" );
  1036. newItem->setPicStatus( this->getPicPresence( presence ) );
  1037. newItem->setContactName( "" );
  1038. newItem->setJid( mucJid );
  1039. newItem->setAvatar( "" );
  1040. newItem->setUnreadMsg( 0 );
  1041. newItem->setTextStatus( mucSubject );
  1042. newItem->setItemType( ROSTER_ITEM_MUC ); //1 - muc
  1043. listModelRoster->append( newItem ); //add to roster
  1044. }
  1045. /* ищем среди открытых */
  1046. RosterItemModel *openExists = (RosterItemModel*)listModelChats->find( mucJid );
  1047. /* если не находим, открываем */
  1048. if( openExists == 0 ) {
  1049. listModelChats->append( newItem ); //open multi-user chat
  1050. emit chatOpened( mucJid, true );
  1051. emit openChatsChanged( mucJid, true );
  1052. }
  1053. if( addToRoster ) {
  1054. rosterXml->addMuc( newItem );
  1055. }
  1056. }
  1057. void MyXmppClient::setMucSubject( QString mucJid, QString mucSubject )
  1058. {
  1059. RosterItemModel *itemExists = (RosterItemModel*)listModelRoster->find( mucJid );
  1060. if( itemExists ) {
  1061. itemExists->setTextStatus( mucSubject );
  1062. }
  1063. }
  1064. void MyXmppClient::attentionSend( QString bareJid, QString resource )
  1065. {
  1066. qDebug() << Q_FUNC_INFO;
  1067. QXmppMessage xmppMsg;
  1068. QString jid_to = bareJid;
  1069. if( resource == "" ) {
  1070. jid_to += "/resource";
  1071. } else {
  1072. jid_to += "/" + resource;
  1073. }
  1074. xmppMsg.setTo( jid_to );
  1075. QString jid_from = m_myjid + "/" + xmppClient->configuration().resource();
  1076. xmppMsg.setFrom( jid_from );
  1077. xmppMsg.setReceiptRequested( false );
  1078. xmppMsg.setState( QXmppMessage::None );
  1079. xmppMsg.setType( QXmppMessage::Headline );
  1080. xmppMsg.setAttentionRequested( true );
  1081. xmppClient->sendPacket( xmppMsg );
  1082. msgWrapper->attention( bareJid, true );
  1083. }
  1084. /*void MyXmppClient::invitationMucReceived(const QString &roomJid, const QString &inviter, const QString &reason)
  1085. {
  1086. qDebug() << Q_FUNC_INFO << roomJid <<" " << inviter << " " << reason;
  1087. }*/