12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334 |
- #include "MyXmppClient.h"
- #include "QXmppLogger.h"
- #include "QXmppRosterManager.h"
- #include "QXmppVersionManager.h"
- #include "QXmppConfiguration.h"
- #include <QDebug>
- #include <QCryptographicHash>
- //#include <QXmppReconnectionManager.h>
- QString MyXmppClient::myVersion = "0.4.0";
- QString MyXmppClient::getBareJidByJid( const QString &jid )
- {
- QString bareJid = jid;
- if( jid.indexOf('/') >= 0 ) {
- bareJid = jid.split('/')[0];
- }
- return bareJid;
- }
- MyXmppClient::MyXmppClient() : QObject(0)
- {
- //QXmppLogger::getLogger()->setLoggingType(QXmppLogger::StdoutLogging);
- cacheIM = new MyCache(this);
- msgWrapper = new MessageWrapper(this);
- rosterXml = new RosterXml( this );
- xmppClient = new QXmppClient( this );
- QObject::connect( xmppClient, SIGNAL(stateChanged(QXmppClient::State)), this, SLOT(clientStateChanged(QXmppClient::State)) );
- QObject::connect( xmppClient, SIGNAL(messageReceived(QXmppMessage)), this, SLOT(messageReceivedSlot(QXmppMessage)) );
- QObject::connect( xmppClient, SIGNAL(presenceReceived(QXmppPresence)), this, SLOT(presenceReceived(QXmppPresence)) );
- QObject::connect( xmppClient, SIGNAL(error(QXmppClient::Error)), this, SLOT(error(QXmppClient::Error)) );
- QObject::connect( msgWrapper, SIGNAL(openChat(QString)), this, SLOT(openChat(QString)) );
- //QObject::connect( msgWrapper, SIGNAL(sendMyMsg(QXmppMessage)), xmppClient, SLOT(sendPacket(QXmppPacket)) );
- xmppMessageReceiptManager = new QXmppMessageReceiptManager();
- QObject::connect( xmppMessageReceiptManager, SIGNAL(messageDelivered(QString,QString)), msgWrapper, SLOT(messageDelivered(QString,QString)) );
- /* multi user chats */
- xmppMucManager = new QXmppMucManager();
- /* discovery manager */
- xmppDiscoveryManager = new QXmppDiscoveryManager();
- listModelChats = new ChatsListModel( this );
- flShowOffline = false;
- listModelRoster = new RosterListModel( this );
- m_bareJidLastMessage = "";
- m_resourceLastMessage = "";
- m_stateConnect = Disconnect;
- m_status = Offline;
- m_statusText = "";
- m_flTyping = false;
- m_myjid = "";
- m_password = "";
- m_host = "";
- m_port= 0;
- m_resource = "";
- m_chatJid = "";
- m_smilesmodel = 0;
- m_reconnectOnError = false;
- m_keepAlive = 60;
- flVCardRequest = "";
- qmlVCard = new QMLVCard();
- this->initXmppClient();
- rosterManager = 0;
- flSetPresenceWithoutAck = false;
- }
- MyXmppClient::~MyXmppClient()
- {
- /*if( xmppClient != NULL ) {
- delete xmppClient;
- }*/
- if( cacheIM != NULL) {
- delete cacheIM;
- }
- if( msgWrapper != NULL) {
- delete msgWrapper;
- }
- /*if( xmppMessageReceiptManager != NULL) {
- delete xmppMessageReceiptManager;
- }*/
- }
- void MyXmppClient::initXmppClient()
- {
- /* init home directory */
- /*bool res = */cacheIM->createHomeDir();
- QString p = cacheIM->getMeegIMHomePath() + "/roster.xml";
- rosterXml->createRosterXML( p );
- xmppClient->versionManager().setClientName("MeegIM");
- xmppClient->versionManager().setClientVersion( MyXmppClient::myVersion );
- /* add extension */
- xmppClient->addExtension( xmppMessageReceiptManager );
- xmppClient->addExtension( xmppMucManager );
- //xmppClient->addExtension( xmppDiscoveryManager );
- }
- void MyXmppClient::clientStateChanged(QXmppClient::State state)
- {
- if( state == QXmppClient::ConnectingState )
- {
- m_stateConnect = Connecting;
- }
- else if( state == QXmppClient::ConnectedState )
- {
- m_stateConnect = Connected;
- if( !rosterManager ) //добавленное условие исключает повторный вызов connect
- {
- rosterManager = &xmppClient->rosterManager();
- //FIXME: добавил к коннеткам флаг Qt::UniqueConnection в виду того, что это состояние
- //может вызваться дважды. Последить за поведением клиента
- qDebug() << Q_FUNC_INFO << " QObject::connect( rosterManager, SIGNAL(........).....)";
- QObject::connect( rosterManager, SIGNAL(presenceChanged(QString,QString)), this, SLOT(initPresence(const QString, const QString)), Qt::UniqueConnection );
- QObject::connect( rosterManager, SIGNAL(rosterReceived()), this, SLOT(initRoster()), Qt::UniqueConnection );
- QObject::connect( rosterManager, SIGNAL(subscriptionReceived(QString)), this, SLOT(subscriptionReceivedSlot(QString)), Qt::UniqueConnection );
- QObject::connect( rosterManager, SIGNAL(itemAdded(QString)), this, SLOT(itemAdded(QString)), Qt::UniqueConnection );
- QObject::connect( rosterManager, SIGNAL(itemRemoved(QString)), this, SLOT(itemRemoved(QString)), Qt::UniqueConnection );
- QObject::connect( rosterManager, SIGNAL(itemChanged(QString)), this, SLOT(itemChanged(QString)), Qt::UniqueConnection );
- }
- /* проверка статуса для первоочередной установки,
- * пока не принялся сигнал, непосредственно извещающий о статусе
- */
- QXmppPresence pr = xmppClient->clientPresence();
- this->presenceReceived( pr );
- }
- else if( state == QXmppClient::DisconnectedState )
- {
- m_stateConnect = Disconnect;
- this->setMyPresence( Offline, m_statusText );
- }
- emit connectingChanged();
- }
- void MyXmppClient::connectToXmppServer() //Q_INVOKABLE
- {
- //xmppConfig = mimOpt->getDefaultAccount();
- msgWrapper->setMyJid( m_myjid );
- QXmppConfiguration xmppConfig;
- xmppConfig.setJid( m_myjid );
- xmppConfig.setPassword( m_password );
- xmppConfig.setKeepAliveInterval( m_keepAlive );
- /*******************/
- //xmppConfig.setSaslAuthMechanism( "PLAIN" );
- xmppConfig.setAutoReconnectionEnabled( m_reconnectOnError );
- if( m_resource.isEmpty() || m_resource.isNull() ) {
- xmppConfig.setResource( "MeegIM" );
- } else {
- xmppConfig.setResource( m_resource );
- }
- if( !m_host.isEmpty() ) { xmppConfig.setHost( m_host ); }
- if( m_port != 0 ) { xmppConfig.setPort( m_port ); }
- xmppClient->connectToServer( xmppConfig );
- //TODO: здесь восстанавливть последний статус
- }
- void MyXmppClient::disconnectFromXmppServer() //Q_INVOKABLE
- {
- xmppClient->disconnectFromServer();
- }
- /* it initialises the list of contacts - roster */
- void MyXmppClient::initRoster()
- {
- qDebug() << "MyXmppClient::initRoster() has been called";
- if( ! rosterManager->isRosterReceived() ) {
- qDebug() << "MyXmppClient::initRoster(): roster has not received yet";
- return;
- }
- if( !vCardManager )
- {
- vCardManager = &xmppClient->vCardManager();
- QObject::connect( vCardManager, SIGNAL(vCardReceived(const QXmppVCardIq &)),
- this, SLOT(initVCard(const QXmppVCardIq &)),
- Qt::UniqueConnection );
- }
- QStringList listBareJids = rosterManager->getRosterBareJids();
- for( int j=0; j < listBareJids.length(); j++ )
- {
- QString bareJid = listBareJids.at(j);
- cacheIM->addCacheJid( bareJid );
- QXmppRosterIq::Item itemRoster = rosterManager->getRosterEntry( bareJid );
- QString name = itemRoster.name();
- QList<QString> listOfGroup = itemRoster.groups().toList();
- QString group = "";
- if( listOfGroup.length() > 0 ) {
- group = listOfGroup.at(0);
- }
- /* если в кеше есть аватарка и vCard, получим их */
- QString avatarPath = cacheIM->getAvatarCache( bareJid );
- vCardData vCdata = cacheIM->getVCard( bareJid );
- if ( avatarPath.isEmpty() && vCdata.isEmpty() )
- {
- /* кеш пустой, запрос vCard для текущего bareJid */
- vCardManager->requestVCard( bareJid );
- //qDebug() << "* request vCard for " << bareJid;
- }
- else
- {
- if( !avatarPath.isEmpty() ) {
- avatarPath = QString("file://") + avatarPath;
- }
- QString nickName = vCdata.nickName;
- if( (!nickName.isEmpty()) && (!nickName.isNull()) && (itemRoster.name().isEmpty()) ) {
- name = nickName;
- }
- }
- RosterItemModel *itemModel = new RosterItemModel( );
- itemModel->setGroup( group );
- itemModel->setPicStatus( this->getPicPresence( QXmppPresence::Unavailable ) );
- itemModel->setContactName( name );
- itemModel->setJid( bareJid );
- itemModel->setAvatar( avatarPath );
- itemModel->setUnreadMsg( 0 );
- listRoster[ bareJid ] = itemModel;
- }
- this->setQMLListRoster();
- emit rosterChanged();
- }
- void MyXmppClient::initPresence(const QString& bareJid, const QString& resource)
- {
- if( !listRoster.contains( bareJid ) ) {
- return;
- }
- QXmppPresence xmppPresence = rosterManager->getPresence( bareJid, resource );
- QXmppPresence::Type statusJid = xmppPresence.type();
- QStringList _listResources = this->getResourcesByJid( bareJid );
- //qDebug() << "*** initPresence():" << bareJid << " => " << resource << "_listResources:["<<_listResources<<"]";
- if( (_listResources.count() > 0) && (!_listResources.contains(resource)) )
- {
- /* делать выход ничего не меняя ?? */
- qDebug() << bareJid << "/" << resource << " ****************[" <<_listResources<<"]" ;
- if( statusJid == QXmppPresence::Unavailable ) {
- return;
- }
- }
- RosterItemModel *item = listRoster[ bareJid ];
- item->setResource( resource );
- QString picStatus = this->getPicPresence( xmppPresence );
- item->setPicStatus( picStatus );
- QString txtStatus = this->getTextStatus( xmppPresence.statusText(), xmppPresence );
- item->setTextStatus( txtStatus );
- qDebug() << "initPresence():" << bareJid << " => " << resource << " => " << txtStatus ;
- /* ищем контакт в ростере */
- int indxItem = -1;
- RosterItemModel *itemExists = (RosterItemModel*)listModelRoster->find( bareJid, indxItem );
- if( flShowOffline )
- {
- /* --- показать offline контакты --- */
- if( itemExists != 0 ) {
- /* обновим информацию */
- itemExists->copy( item );
- QString picStatusPrev = itemExists->picStatus();
- if( picStatusPrev != picStatus ) //FIXME: пока таким экзотическим методом выясняем смнился ли статус собеседника по сравнению с предыдущим
- {
- //qDebug() << "1>>> presenceJidChanged:" << bareJid << "|"<<txtStatus<<"|"<<picStatus;
- emit presenceJidChanged( bareJid, txtStatus, picStatus );
- }
- }
- }
- else
- {
- /* --- скрыть оффлайн контакты --- */
- if( (itemExists==0) && (statusJid != QXmppPresence::Unavailable ) )
- {
- /* собеседник вошел в сеть: добавляем контакт в ростер */
- listModelRoster->append( item );
- //qDebug() << "2>>> presenceJidChanged:" << bareJid << "|"<<txtStatus<<"|"<<picStatus;
- emit presenceJidChanged( bareJid, txtStatus, picStatus );
- }
- else if( (itemExists!=0) && (statusJid != QXmppPresence::Unavailable ) )
- {
- /* собеседник сменил статус: обновить данные на ростере */
- itemExists->copy( item );
- QString picStatusPrev = itemExists->picStatus();
- if( picStatusPrev != picStatus ) //FIXME: пока таким экзотическим методом выясняем смнился ли статус собеседника по сравнению с предыдущим
- {
- //qDebug() << "3>>> presenceJidChanged:" << bareJid << "|"<<txtStatus<<"|"<<picStatus;
- emit presenceJidChanged( bareJid, txtStatus, picStatus );
- }
- }
- else if( (itemExists!=0) && (statusJid == QXmppPresence::Unavailable ) )
- {
- /* собеседник вышел из сети: удаляем контакт из ростера */
- if( indxItem >= 0 ) {
- listModelRoster->takeRow( indxItem );
- //qDebug() << "4>>> presenceJidChanged:" << bareJid << "|"<<txtStatus<<"|"<<picStatus;
- emit presenceJidChanged( bareJid, txtStatus, picStatus );
- }
- }
- }
- /* смотри, есть ли контакт в открытых чатах */
- RosterItemModel* item_chat = reinterpret_cast<RosterItemModel*>( listModelChats->find( bareJid ) );
- if( item_chat )
- {
- item_chat->setResource( resource );
- item_chat->setPicStatus( picStatus );
- item_chat->setTextStatus( txtStatus );
- }
- }
- void MyXmppClient::setQMLListRoster()
- {
- listModelRoster->takeRows(0, listModelRoster->count() );
- if( flShowOffline )
- {
- /* show offline jids */
- QMap< QString, RosterItemModel*>::iterator itr;
- for ( itr = listRoster.begin(); itr != listRoster.end(); itr++ )
- {
- RosterItemModel* itemData = itr.value();
- listModelRoster->append( itemData );
- }
- }
- else if( m_stateConnect == Connected )
- {
- /* hide offline jids */
- QMap< QString, RosterItemModel*>::iterator itr;
- for ( itr = listRoster.begin(); itr != listRoster.end(); itr++ )
- {
- //QString jid = itr.key();
- RosterItemModel* itemData = itr.value();
- QXmppPresence presence( QXmppPresence::Unavailable );
- if( itemData->picStatus() != this->getPicPresence( presence ) ) { //пока так :(
- listModelRoster->append( itemData );
- }
- }
- }
- //qDebug() << "###!### m_status:" << m_status;
- if( m_stateConnect == Connected )
- {
- /* заполнение ростера из xml файла (muc) */
- QList<RosterItemModel *> listItems = rosterXml->getListMuc();
- QList<RosterItemModel *>::Iterator itr_xml;
- for ( itr_xml = listItems.begin(); itr_xml != listItems.end(); itr_xml++ )
- {
- QXmppPresence presence( QXmppPresence::Available );
- RosterItemModel* itemData = *itr_xml;
- itemData->setPicStatus( this->getPicPresence( presence ) );
- listModelRoster->append( itemData );
- }
- }
- }
- QString MyXmppClient::getPicPresence( const QXmppPresence &presence ) const
- {
- QString picPresenceName = "qrc:/qml/images/presence-unknown.png";
- QXmppPresence::Type status = presence.type();
- if( status != QXmppPresence::Available )
- {
- picPresenceName = "qrc:/qml/images/presence-offline.png";
- }
- else
- {
- QXmppPresence::AvailableStatusType availableStatus = presence.availableStatusType();
- if( (availableStatus == QXmppPresence::Online) || (availableStatus == QXmppPresence::Chat) ) {
- picPresenceName = "qrc:/qml/images/presence-online.png";
- } else if ( (availableStatus == QXmppPresence::Away) || (availableStatus == QXmppPresence::XA) ) {
- picPresenceName = "qrc:/qml/images/presence-away.png";
- } else if ( availableStatus == QXmppPresence::DND ) {
- picPresenceName = "qrc:/qml/images/presence-busy.png";
- }
- }
- return picPresenceName;
- }
- QString MyXmppClient::getTextStatus(const QString &textStatus, const QXmppPresence &presence ) const
- {
- if( (!textStatus.isEmpty()) && (!textStatus.isNull()) ) {
- return textStatus;
- }
- QXmppPresence::Type status = presence.type();
- QString txtStat = "";
- if( status == QXmppPresence::Unavailable )
- {
- txtStat = "Offline";
- }
- else
- {
- QXmppPresence::AvailableStatusType availableStatus = presence.availableStatusType();
- if( availableStatus == QXmppPresence::Online ) {
- txtStat = "Online";
- } else if ( availableStatus == QXmppPresence::Chat ) {
- txtStat = "Chatty";
- } else if ( availableStatus == QXmppPresence::Away ) {
- txtStat = "Away";
- } else if ( availableStatus == QXmppPresence::XA ) {
- txtStat = "Extended away";
- } else if ( availableStatus == QXmppPresence::DND ) {
- txtStat = "Do not disturb";
- }
- }
- return txtStat;
- }
- /* SLOT: it will be called when the vCardReceived signal will be received */
- void MyXmppClient::initVCard(const QXmppVCardIq &vCard)
- {
- QString bareJid = vCard.from();
- //qDebug() << "## initVCard: " << bareJid;
- RosterItemModel *item = (RosterItemModel*)listModelRoster->find( bareJid );
- vCardData dataVCard;
- if( /*item != 0*/true )
- {
- /* set nickname */
- QXmppRosterIq::Item itemRoster = rosterManager->getRosterEntry( bareJid );
- QString nickName = vCard.nickName();
- //if( (!nickName.isEmpty()) && (!nickName.isNull()) && (item!=0) ) {
- if( (!nickName.isEmpty()) && (!nickName.isNull()) && (itemRoster.name().isEmpty()) && (item!=0) ) {
- //qDebug() << "*****> setContactName( nickName ): " << nickName;
- item->setContactName( nickName );
- }
- /* avatar */
- bool isAvatarCreated = true;
- QString avatarFile = cacheIM->getAvatarCache( bareJid );
- if( avatarFile.isEmpty() || (flVCardRequest != "") ) {
- isAvatarCreated = cacheIM->setAvatarCache( bareJid, vCard.photo() );
- avatarFile = cacheIM->getAvatarCache( bareJid );
- }
- if( isAvatarCreated ) {
- avatarFile = QString("file://") + avatarFile;
- if( item != 0 ) {
- item->setAvatar( avatarFile );
- }
- }
- dataVCard.nickName = nickName;
- dataVCard.firstName = vCard.firstName();
- dataVCard.fullName = vCard.fullName();;
- dataVCard.middleName = vCard.middleName();
- dataVCard.lastName = vCard.lastName();
- dataVCard.url = vCard.url();
- dataVCard.eMail = vCard.email();
- if( flVCardRequest == bareJid ) {
- //qDebug() << "MyXmppClient::initVCard(): [" << flVCardRequest << "] is ready";
- qmlVCard->setPhoto( avatarFile );
- qmlVCard->setNickName( vCard.nickName() );
- qmlVCard->setMiddleName( vCard.middleName() );
- qmlVCard->setLastName( vCard.lastName() );
- qmlVCard->setFullName( vCard.fullName() );
- qmlVCard->setName( vCard.firstName() );
- qmlVCard->setBirthday( vCard.birthday().toString("dd.MM.yyyy") );
- qmlVCard->setEMail( vCard.email() );
- qmlVCard->setUrl( vCard.url() );
- qmlVCard->setJid( bareJid );
- flVCardRequest = "";
- emit vCardChanged();
- }
- //qDebug() << dataVCard.nickName;
- //qDebug() << dataVCard.firstName;
- //qDebug() << dataVCard.fullName;
- //qDebug() << dataVCard.middleName;
- //qDebug() << dataVCard.lastName;
- //qDebug() << dataVCard.url;
- //qDebug() << dataVCard.eMail;
- cacheIM->setVCard( bareJid, dataVCard );
- //qDebug() << "----------------------------";
- }
- }
- void MyXmppClient::setStatusText( const QString &__statusText )
- {
- if( __statusText != m_statusText )
- {
- m_statusText=__statusText;
- QXmppPresence myPresence = xmppClient->clientPresence();
- myPresence.setStatusText( __statusText );
- xmppClient->setClientPresence( myPresence );
- //mimOpt->setStatusText( __statusText );
- emit statusTextChanged();
- }
- }
- void MyXmppClient::setStatus( StatusXmpp __status)
- {
- if( __status != m_status )
- {
- QXmppPresence myPresence = xmppClient->clientPresence();
- if( __status == Online ) {
- myPresence.setType( QXmppPresence::Available );
- myPresence.setAvailableStatusType( QXmppPresence::Online );
- } else if( __status == Chat ) {
- myPresence.setType( QXmppPresence::Available );
- myPresence.setAvailableStatusType( QXmppPresence::Chat );
- } else if ( __status == Away ) {
- myPresence.setType( QXmppPresence::Available );
- myPresence.setAvailableStatusType( QXmppPresence::Away );
- } else if ( __status == XA ) {
- myPresence.setType( QXmppPresence::Available );
- myPresence.setAvailableStatusType( QXmppPresence::XA );
- } else if( __status == DND ) {
- myPresence.setType( QXmppPresence::Available );
- myPresence.setAvailableStatusType( QXmppPresence::DND );
- } else if( __status == Offline ) {
- myPresence.setType( QXmppPresence::Unavailable );
- //m_status = __status;
- //emit statusChanged();
- /* ОСТАНОВКА ПЕРЕКОННЕКТА */ //FIXME: QXmppReconnectionManager was removed
- //QXmppReconnectionManager *rm = xmppClient->reconnectionManager();
- //if( rm ) { rm->cancelReconnection(); }
- }
- xmppClient->setClientPresence( myPresence );
- this->presenceReceived( myPresence ); //NOTICE: извещаем клиент о смене статуса мгновенно
- }
- }
- void MyXmppClient::setMyPresence( StatusXmpp status, QString textStatus ) //Q_INVOKABLE
- {
- qDebug() << Q_FUNC_INFO;
- if( textStatus != m_statusText ) {
- m_statusText =textStatus;
- emit statusTextChanged();
- }
- QXmppPresence myPresence;
- if( status == Online )
- {
- if( xmppClient->state() == QXmppClient::DisconnectedState ) {
- this->connectToXmppServer();
- }
- myPresence.setType( QXmppPresence::Available );
- myPresence.setStatusText( textStatus );
- myPresence.setAvailableStatusType( QXmppPresence::Online );
- }
- else if( status == Chat )
- {
- if( xmppClient->state() == QXmppClient::DisconnectedState ) {
- this->connectToXmppServer();
- }
- myPresence.setType( QXmppPresence::Available );
- myPresence.setAvailableStatusType( QXmppPresence::Chat );
- myPresence.setStatusText( textStatus );
- }
- else if( status == Away )
- {
- if( xmppClient->state() == QXmppClient::DisconnectedState ) {
- this->connectToXmppServer();
- }
- myPresence.setType( QXmppPresence::Available );
- myPresence.setAvailableStatusType( QXmppPresence::Away );
- myPresence.setStatusText( textStatus );
- }
- else if( status == XA )
- {
- if( xmppClient->state() == QXmppClient::DisconnectedState ) {
- this->connectToXmppServer();
- }
- myPresence.setType( QXmppPresence::Available );
- myPresence.setAvailableStatusType( QXmppPresence::XA );
- myPresence.setStatusText( textStatus );
- }
- else if( status == DND )
- {
- if( xmppClient->state() == QXmppClient::DisconnectedState ) {
- this->connectToXmppServer();
- }
- myPresence.setType( QXmppPresence::Available );
- myPresence.setAvailableStatusType( QXmppPresence::DND );
- myPresence.setStatusText( textStatus );
- }
- else if( status == Offline )
- {
- if( (xmppClient->state() == QXmppClient::ConnectedState) || (xmppClient->state() == QXmppClient::ConnectingState) )
- {
- xmppClient->disconnectFromServer();
- }
- myPresence.setType( QXmppPresence::Unavailable );
- /* все контакты offline */
- QMap< QString, RosterItemModel*>::iterator itr;
- for ( itr = listRoster.begin(); itr != listRoster.end(); itr++ )
- {
- QXmppPresence presnc( QXmppPresence::Unavailable );
- RosterItemModel* itemData = itr.value();
- QString pic = this->getPicPresence( presnc );
- itemData->setPicStatus( pic );
- QString st = this->getTextStatus( "", presnc );
- itemData->setTextStatus( st );
- }
- /* извещаем об оффлайн состоянии */
- //m_status = status;
- //this->setQMLListRoster();
- //emit statusChanged();
- /* ОСТАНОВКА ПЕРЕКОННЕКТА */ //FIXME: QXmppReconnectionManager was removed
- //QXmppReconnectionManager *rm = xmppClient->reconnectionManager();
- //if( rm ) { rm->cancelReconnection(); }
- }
- xmppClient->setClientPresence( myPresence );
- this->presenceReceived( myPresence ); //NOTICE: извещаем клиент о смене статуса мгновенно
- if( status == Offline ) {
- this->setQMLListRoster();
- }
- }
- /* it sends information about typing : typing is started */
- void MyXmppClient::typingStart(QString bareJid, QString resource) //Q_INVOKABLE
- {
- //qDebug() << bareJid << " " << "start typing...";
- QXmppMessage xmppMsg;
- QString jid_to = bareJid;
- if( resource == "" ) {
- jid_to += "/resource";
- } else {
- jid_to += "/" + resource;
- }
- xmppMsg.setTo( jid_to );
- QString jid_from = m_myjid + "/" + xmppClient->configuration().resource();
- xmppMsg.setFrom( jid_from );
- xmppMsg.setReceiptRequested( false );
- QDateTime currTime = QDateTime::currentDateTime();
- xmppMsg.setStamp( currTime );
- xmppMsg.setState( QXmppMessage::Composing );
- xmppClient->sendPacket( xmppMsg );
- }
- /* it sends information about typing : typing is stoped */
- void MyXmppClient::typingStop(QString bareJid, QString resource) //Q_INVOKABLE
- {
- //qDebug() << bareJid << " " << "stop typing...";
- QXmppMessage xmppMsg;
- QString jid_to = bareJid;
- if( resource == "" ) {
- jid_to += "/resource";
- } else {
- jid_to += "/" + resource;
- }
- xmppMsg.setTo( jid_to );
- QString jid_from = m_myjid + "/" + xmppClient->configuration().resource();
- xmppMsg.setFrom( jid_from );
- xmppMsg.setReceiptRequested( false );
- QDateTime currTime = QDateTime::currentDateTime();
- xmppMsg.setStamp( currTime );
- xmppMsg.setState( QXmppMessage::Paused );
- xmppClient->sendPacket( xmppMsg );
- }
- void MyXmppClient::openChat( QString bareJid ) //Q_INVOKABLE
- {
- //qDebug() << "MyXmppClient::openChat(): new chat was opened:" << bareJid;
- /* поиск jid в ростере */
- RosterItemModel *itemRoster = reinterpret_cast<RosterItemModel*>( listModelRoster->find( bareJid ) );
- /* поиск соответствующего jid в открытых чатах */
- RosterItemModel* item = reinterpret_cast<RosterItemModel*>( listModelChats->find( bareJid ) );
- bool isMuc = false;
- if( (item == 0) && (itemRoster != 0) ) // исключаем вторичное добавление
- {
- listModelChats->append(itemRoster);
- isMuc = (itemRoster->itemType() == 1);
- }
- else if( (item == 0) && (itemRoster == 0) ) //сообщение от собеседника которого нет в ростере
- {
- //qDebug() << "XXX:" << bareJid;
- QXmppPresence presence( QXmppPresence::Unavailable );
- RosterItemModel *newItem = new RosterItemModel( );
- newItem->setGroup( "" );
- newItem->setPicStatus( this->getPicPresence( presence ) );
- newItem->setContactName( "" );
- newItem->setJid( bareJid );
- newItem->setAvatar( "" );
- newItem->setUnreadMsg( 0 );
- listModelChats->append( newItem );
- }
- emit chatOpened( bareJid, isMuc );
- emit openChatsChanged( bareJid, isMuc );
- msgWrapper->initChat( bareJid );
- this->resetUnreadMessages( bareJid );
- }
- void MyXmppClient::closeChat( QString bareJid ) //Q_INVOKABLE
- {
- int row = -1;
- this->resetUnreadMessages( bareJid );
- RosterItemModel *item = reinterpret_cast<RosterItemModel*>( listModelChats->find( bareJid, row ) );
- if( (item != NULL) && (row >= 0) )
- {
- /*bool res = */listModelChats->takeRow( row );
- if( item->itemType() == 1 )
- {
- /* chat is closed, therefor logout from the chat */
- }
- bool isMuc = item->itemType() == ROSTER_ITEM_MUC;
- emit openChatsChanged( bareJid, isMuc );
- emit chatClosed( bareJid, isMuc );
- //qDebug() << "MyXmppClient::closeChat("<<bareJid<<"): row:"<<row << " result:"<<res << " listModelChats.count():" <<listModelChats->count();
- }
- msgWrapper->removeListOfChat( bareJid );
- }
- void MyXmppClient::incUnreadMessage(QString bareJid) //Q_INVOKABLE
- {
- RosterItemModel *item = (RosterItemModel*)listModelRoster->find( bareJid );
- if( item != 0 )
- {
- int cnt = item->unreadMsg();
- item->setUnreadMsg( ++cnt );
- }
- }
- void MyXmppClient::resetUnreadMessages(QString bareJid) //Q_INVOKABLE
- {
- RosterItemModel *item = (RosterItemModel*)listModelRoster->find( bareJid );
- if( item != 0 ) {
- item->setUnreadMsg( 0 );
- }
- RosterItemModel *itemChat = (RosterItemModel*)listModelChats->find( bareJid );
- if( itemChat != 0 ) {
- itemChat->setUnreadMsg( 0 );
- }
- }
- void MyXmppClient::itemAdded(const QString &bareJid )
- {
- qDebug() << "MyXmppClient::itemAdded(): " << bareJid;
- QStringList resourcesList = rosterManager->getResources( bareJid );
- /* добавляем в ростер */
- QXmppPresence presence( QXmppPresence::Unavailable );
- RosterItemModel *itemModel = new RosterItemModel( );
- itemModel->setGroup("");
- itemModel->setPicStatus( this->getPicPresence( presence ) );
- itemModel->setContactName("");
- itemModel->setJid( bareJid );
- itemModel->setAvatar("");
- itemModel->setUnreadMsg( 0 );
- listRoster[ bareJid ] = itemModel;
- /* запрашиваем присутствие */
- for( int L = 0; L<resourcesList.length(); L++ )
- {
- QString resource = resourcesList.at(L);
- this->initPresence( bareJid, resource );
- }
- }
- void MyXmppClient::itemChanged(const QString &bareJid )
- {
- qDebug() << "MyXmppClient::itemChanged(): " << bareJid;
- QXmppRosterIq::Item rosterEntry = rosterManager->getRosterEntry( bareJid );
- QString name = rosterEntry.name();
- //qDebug() << "rosterEntry.name()=" << name;
- RosterItemModel *item = (RosterItemModel*)listModelRoster->find( bareJid );
- if( item ) {
- item->setContactName( name );
- }
- }
- void MyXmppClient::itemRemoved(const QString &bareJid )
- {
- qDebug() << "MyXmppClient::itemRemoved(): " << bareJid;
- if( listRoster.contains( bareJid ) )
- {
- listRoster.remove( bareJid );
- }
- int indxItem = -1;
- RosterItemModel *itemExists = (RosterItemModel*)listModelRoster->find( bareJid, indxItem );
- if( itemExists )
- {
- /* удаляем контакт из ростера */
- if( indxItem >= 0 ) {
- listModelRoster->takeRow( indxItem );
- }
- }
- }
- void MyXmppClient::subscriptionReceivedSlot(const QString &bareJid )
- {
- emit this->subscriptionReceived( bareJid );
- }
- void MyXmppClient::requestVCard(QString bareJid) //Q_INVOKABLE
- {
- if (vCardManager && (flVCardRequest == "") )
- {
- vCardManager->requestVCard( bareJid );
- flVCardRequest = bareJid;
- }
- }
- void MyXmppClient::messageReceivedSlot( const QXmppMessage &xmppMsg )
- {
- QString bareJid_from = MyXmppClient::getBareJidByJid( xmppMsg.from() );
- QString bareJid_to = MyXmppClient::getBareJidByJid( xmppMsg.to() );
- qDebug() << Q_FUNC_INFO << " typeMsg: " << xmppMsg.type();
- if( xmppMsg.state() == QXmppMessage::Active )
- {
- qDebug() << "Msg state is QXmppMessage::Active";
- if( !( xmppMsg.body().isEmpty() || xmppMsg.body().isNull()) )
- {
- msgWrapper->textMessage(xmppMsg);
- QString jid = xmppMsg.from();
- if( jid.indexOf('/') >= 0 ) {
- QStringList sl = jid.split('/');
- m_bareJidLastMessage = sl[0];
- if( sl.count() > 1 ) {
- m_resourceLastMessage = sl[1];
- }
- } else {
- m_bareJidLastMessage = xmppMsg.from();
- }
- this->incUnreadMessage( bareJid_from );
- emit this->messageReceived( bareJid_from, bareJid_to );
- }
- }
- else if( xmppMsg.state() == QXmppMessage::Inactive )
- {
- qDebug() << "Msg state is QXmppMessage::Inactive";
- }
- else if( xmppMsg.state() == QXmppMessage::Gone )
- {
- qDebug() << "Msg state is QXmppMessage::Gone";
- }
- else if( xmppMsg.state() == QXmppMessage::Composing )
- {
- m_flTyping = true;
- emit typingChanged( bareJid_from, true);
- }
- else if( xmppMsg.state() == QXmppMessage::Paused )
- {
- m_flTyping = false;
- emit typingChanged( bareJid_from, false);
- }
- else
- {
- QString mucInvitationJid = xmppMsg.mucInvitationJid(); //в случае если это приглшение, строка будет не пустой
- if( xmppMsg.isAttentionRequested() )
- {
- //qDebug() << "ZZZ: attentionRequest !!! from:" <<xmppMsg.from();
- msgWrapper->attention( bareJid_from, false );
- this->incUnreadMessage( bareJid_from );
- emit this->messageReceived( bareJid_from, bareJid_to );
- }
- else if( mucInvitationJid.length() > 1 )
- {
- //qDebug() << "ZZZ: mucInvitationJid:" << mucInvitationJid;
- }
- else if( !( xmppMsg.body().isEmpty() || xmppMsg.body().isNull()) )
- {
- msgWrapper->textMessage(xmppMsg);
- QString jid = xmppMsg.from();
- if( jid.indexOf('/') >= 0 ) {
- QStringList sl = jid.split('/');
- m_bareJidLastMessage = sl[0];
- if( sl.count() > 1 ) {
- m_resourceLastMessage = sl[1];
- }
- } else {
- m_bareJidLastMessage = xmppMsg.from();
- }
- this->incUnreadMessage( bareJid_from );
- emit this->messageReceived( bareJid_from, bareJid_to );
- }
- qDebug() << "MessageWrapper::messageReceived(): xmppMsg.state():" << xmppMsg.state();
- }
- }
- void MyXmppClient::setShowOffline(const bool value)
- {
- if( flShowOffline != value )
- {
- flShowOffline = value;
- this->setQMLListRoster();
- emit showOfflineChanged();
- emit rosterChanged();
- }
- }
- QString MyXmppClient::getPicPresenceByJid(QString bareJid)
- {
- QString ret = "";
- RosterItemModel *itemExists = (RosterItemModel*)listModelRoster->find( bareJid );
- if( itemExists ) {
- ret = itemExists->picStatus();
- }
- return ret;
- }
- QString MyXmppClient::getStatusTextByJid(QString bareJid)
- {
- QString ret = "";
- RosterItemModel *itemExists = (RosterItemModel*)listModelRoster->find( bareJid );
- if( itemExists ) {
- ret = itemExists->textStatus();
- }
- return ret;
- }
- QString MyXmppClient::getAvatarByJid(QString bareJid)
- {
- QString ret = "";
- RosterItemModel *itemExists = (RosterItemModel*)listModelRoster->find( bareJid );
- if( itemExists ) {
- ret = itemExists->picAvatar();
- }
- return ret;
- }
- bool MyXmppClient::sendMyMessage(QString bareJid, QString resource, QString msgBody) //Q_INVOKABLE
- {
- if( msgBody == "" ) { return false; }
- QXmppMessage xmppMsg; //???
- QString jid_from = bareJid;
- if( resource == "" ) {
- jid_from += "/resource";
- } else {
- jid_from += "/" + resource;
- }
- xmppMsg.setTo( jid_from );
- QString jid_to = m_myjid + "/" + xmppClient->configuration().resource();
- xmppMsg.setFrom( jid_to );
- //qDebug() << "SEND MSG: from:[" << jid_to << "] to:[" << jid_from << "] body:[" << msgBody << "]";
- /*QTextDocument doc;
- doc.setHtml( msgBody );
- xmppMsg.setBody( doc.toPlainText() );*/
- xmppMsg.setBody( msgBody );
- xmppMsg.setReceiptRequested( true ); /* request DLR */
- xmppMsg.setState( QXmppMessage::Active );
- //qDebug() << "MyXmppClient::sendMyMessage: "<<jid_from<<" "<<jid_to<<" "<<msgBody;
- xmppClient->sendPacket( xmppMsg );
- this->messageReceivedSlot( xmppMsg );
- return true;
- }
- QStringList MyXmppClient::getResourcesByJid(QString bareJid)
- {
- return rosterManager->getResources(bareJid);
- }
- void MyXmppClient::presenceReceived( const QXmppPresence & presence )
- {
- QString jid = presence.from();
- QString bareJid = jid;
- QString resource = "";
- if( jid.indexOf('/') >= 0 ) {
- bareJid = jid.split('/')[0];
- resource = jid.split('/')[1];
- }
- //this->initPresence( bareJid, resource );
- QString myResource = xmppClient->configuration().resource();
- //qDebug() << "### MyXmppClient::presenceReceived():" << bareJid << "|" << resource << "|" << myResource << "|" << presence.from() << "|" << presence.type()<< "|" << presence.availableStatusType();
- if( (((presence.from()).indexOf( m_myjid ) >= 0) && (resource == myResource)) || ((bareJid == "") && (resource == "")) )
- {
- QXmppPresence::Type __type = presence.type();
- if( __type == QXmppPresence::Unavailable )
- {
- m_status = Offline;
- }
- else
- {
- QXmppPresence::AvailableStatusType __status = presence.availableStatusType();
- if( __status == QXmppPresence::Online ) {
- m_status = Online;
- } else if( __status == QXmppPresence::Chat ) {
- m_status = Chat;
- } else if ( __status == QXmppPresence::Away ) {
- m_status = Away;
- } else if ( __status == QXmppPresence::XA ) {
- m_status = XA;
- } else if( __status == QXmppPresence::DND ) {
- m_status = DND;
- }
- }
- emit statusChanged();
- //qDebug() << "^^^ MyXmppClient::presenceReceived(): for " << bareJid << " status is " << presence.type();
- }
- }
- void MyXmppClient::error(QXmppClient::Error e)
- {
- QString errString;
- if( e == QXmppClient::SocketError ) {
- errString = "Socket error";
- } else if( e == QXmppClient::KeepAliveError ) {
- errString = "Keep alive error";
- } else if( e == QXmppClient::XmppStreamError ) {
- errString = "Xmpp stream error";
- }
- if( !errString.isNull() )
- {
- /* проверяем и устанавливаем текущее состояние клиента */
- QXmppPresence pr = xmppClient->clientPresence();
- this->presenceReceived( pr );
- /* ОСТАНОВКА ПЕРЕКОННЕКТА */
- //если нужен переконнект при ошибке то не выполнять две строки ниже
- if( !m_reconnectOnError ) {
- QXmppPresence presence( QXmppPresence::Unavailable );
- xmppClient->setClientPresence( presence );
- }
- /* извещаем об ошибке */
- emit errorHappened( errString );
- }
- }
- /*--- add/remove contact ---*/
- void MyXmppClient::addContact( QString bareJid, QString nick, QString group, bool sendSubscribe )
- {
- //qDebug() << "MyXmppClient::addContact(" << bareJid << ", " << nick << ", " << group << ", " << sendSubscribe << ")";
- if( rosterManager )
- {
- QSet<QString> gr;
- QString n;
- if( !(group.isEmpty() || group.isNull()) ) { gr.insert( group ); }
- if( !(nick.isEmpty() || nick.isNull()) ) { n = nick; }
- rosterManager->addItem(bareJid, n, gr );
- if( sendSubscribe ) {
- rosterManager->subscribe( bareJid );
- }
- }
- }
- void MyXmppClient::removeContact( QString bareJid ) //Q_INVOKABLE
- {
- int row = 0;
- RosterItemModel *itemExists = (RosterItemModel*)listModelRoster->find( bareJid, row );
- if( itemExists )
- {
- int type = itemExists->itemType();
- if ( type == 1 )
- {
- listModelRoster->takeRow( row );
- rosterXml->removeMuc( bareJid );
- }
- else
- {
- if( rosterManager ) {
- rosterManager->removeItem( bareJid );
- }
- }
- }
- }
- void MyXmppClient::renameContact(QString bareJid, QString name) //Q_INVOKABLE
- {
- //qDebug() << "MyXmppClient::renameContact(" << bareJid << ", " << name << ")" ;
- if( rosterManager ) {
- rosterManager->renameItem( bareJid, name );
- }
- }
- bool MyXmppClient::subscribe(const QString bareJid) //Q_INVOKABLE
- {
- //qDebug() << "MyXmppClient::subscribe(" << bareJid << ")" ;
- bool res = false;
- if( rosterManager && (!bareJid.isEmpty()) && (!bareJid.isNull()) ) {
- res = rosterManager->subscribe( bareJid );
- }
- return res;
- }
- bool MyXmppClient::unsubscribe(const QString bareJid) //Q_INVOKABLE
- {
- //qDebug() << "MyXmppClient::unsubscribe(" << bareJid << ")" ;
- bool res = false;
- if( rosterManager && (!bareJid.isEmpty()) && (!bareJid.isNull()) ) {
- res = rosterManager->unsubscribe( bareJid );
- }
- return res;
- }
- bool MyXmppClient::acceptSubscribtion(const QString bareJid) //Q_INVOKABLE
- {
- //qDebug() << "MyXmppClient::acceptSubscribtion(" << bareJid << ")" ;
- bool res = false;
- if( rosterManager && (!bareJid.isEmpty()) && (!bareJid.isNull()) ) {
- res = rosterManager->acceptSubscription( bareJid );
- }
- return res;
- }
- bool MyXmppClient::rejectSubscribtion(const QString bareJid) //Q_INVOKABLE
- {
- //qDebug() << "MyXmppClient::rejectSubscribtion(" << bareJid << ")" ;
- bool res = false;
- if( rosterManager && (!bareJid.isEmpty()) && (!bareJid.isNull()) ) {
- res = rosterManager->refuseSubscription( bareJid );
- }
- return res;
- }
- void MyXmppClient::addMucRoster( QString mucJid, QString mucSubject, bool addToRoster )
- {
- /* ищем такой jid в ростере */
- RosterItemModel *itemExists = (RosterItemModel*)listModelRoster->find( mucJid );
- QXmppPresence presence( QXmppPresence::Available );
- /* если в ростере не нашли, добавляем */
- RosterItemModel *newItem = itemExists;
- if( itemExists == 0 ) {
- newItem = new RosterItemModel( );
- newItem->setGroup( "Multi-user chats" );
- newItem->setPicStatus( this->getPicPresence( presence ) );
- newItem->setContactName( "" );
- newItem->setJid( mucJid );
- newItem->setAvatar( "" );
- newItem->setUnreadMsg( 0 );
- newItem->setTextStatus( mucSubject );
- newItem->setItemType( ROSTER_ITEM_MUC ); //1 - muc
- listModelRoster->append( newItem ); //add to roster
- }
- /* ищем среди открытых */
- RosterItemModel *openExists = (RosterItemModel*)listModelChats->find( mucJid );
- /* если не находим, открываем */
- if( openExists == 0 ) {
- listModelChats->append( newItem ); //open multi-user chat
- emit chatOpened( mucJid, true );
- emit openChatsChanged( mucJid, true );
- }
- if( addToRoster ) {
- rosterXml->addMuc( newItem );
- }
- }
- void MyXmppClient::setMucSubject( QString mucJid, QString mucSubject )
- {
- RosterItemModel *itemExists = (RosterItemModel*)listModelRoster->find( mucJid );
- if( itemExists ) {
- itemExists->setTextStatus( mucSubject );
- }
- }
- void MyXmppClient::attentionSend( QString bareJid, QString resource )
- {
- qDebug() << Q_FUNC_INFO;
- QXmppMessage xmppMsg;
- QString jid_to = bareJid;
- if( resource == "" ) {
- jid_to += "/resource";
- } else {
- jid_to += "/" + resource;
- }
- xmppMsg.setTo( jid_to );
- QString jid_from = m_myjid + "/" + xmppClient->configuration().resource();
- xmppMsg.setFrom( jid_from );
- xmppMsg.setReceiptRequested( false );
- xmppMsg.setState( QXmppMessage::None );
- xmppMsg.setType( QXmppMessage::Headline );
- xmppMsg.setAttentionRequested( true );
- xmppClient->sendPacket( xmppMsg );
- msgWrapper->attention( bareJid, true );
- }
- /*void MyXmppClient::invitationMucReceived(const QString &roomJid, const QString &inviter, const QString &reason)
- {
- qDebug() << Q_FUNC_INFO << roomJid <<" " << inviter << " " << reason;
- }*/
|