message queued tested
[tpsession] / tpsession-0.1 / tpsession / tpsessionaccount.cpp
index 512ad8b..657e4ba 100644 (file)
@@ -79,7 +79,7 @@ TpSessionAccount::TpSessionAccount(Tp::AccountManagerPtr am,const QString &objec
 
 void TpSessionAccount::onReady(Tp::PendingOperation *op)
 {
-
+    Q_UNUSED(op);
     acc = mAcc.data();
     //    qDebug() << "TpSessionAccount::onReady cmName=" << acc->cmName() << "haveConnection=" <<
     //    (acc->haveConnection()? ( acc->connection()->isReady() ? "Ready":"notReady"):"no");
@@ -124,7 +124,6 @@ void TpSessionAccount::onContactsConnectionReady(Tp::PendingOperation *op)
 #endif
     //    qDebug() << "TpSessionAccount::onContactsConnectionReady "<< acc->cmName() ;
     //    RosterItem *item;
-    bool exists;
     myContacts=contactsConn->contactManager()->allKnownContacts();
     foreach (const Tp::ContactPtr &contact, myContacts) {
       //        qDebug() << "id=" <<contact->id() << " alias=" << contact->alias() << " presence=" << contact->presenceStatus()  ;
@@ -150,9 +149,10 @@ void TpSessionAccount::onContactsConnectionReady(Tp::PendingOperation *op)
 
 Tp::ContactPtr TpSessionAccount::getContactFromAddress(QString id)
 {
+    Q_UNUSED(id);
     Tp::ContactPtr p;
     foreach (const Tp::ContactPtr &contact, myContacts) {
-    if(contact->id()==reqContact) return p=contact;
+    if(contact->id()==id) return p=contact;
     }
     return p;
 }
@@ -168,7 +168,7 @@ TpSessionChannel* TpSessionAccount::getChannelFromPeerAddress(QString id)
 {
   TpSessionChannel* p=NULL;
     foreach (TpSessionChannel* channel, myChannels) {
-    if(channel->peerId()==id) return p=channel;
+        if(channel->peerId()==id) p=channel;
     }
     return p;
 }
@@ -176,28 +176,45 @@ TpSessionChannel* TpSessionAccount::getChannelFromPeerAddress(QString id)
  * Creates new contact with given address. This function is Acynchronous, it sends request to contact manager for contact creation,
  *
  * \param address           Contact address/id, as example email address, telephone number etc.
- */
+ * \return                  true in success
+ *
+ * \return    emits contactRetrieved(Tp:.contactPtr,bool success,bool requested)
+*/
 
-void TpSessionAccount::makeContactFromAddress(QString address)
+bool TpSessionAccount::makeContactFromAddress(QString address)
 {
+     if(!contactsConn) return false; // No contacts connection, return fail
      reqContact=address;  // When we get retrieved signal, we check if it is this one
      Tp::PendingContacts *pc=contactsConn->contactManager()->contactsForIdentifiers(QStringList(address));
      connect(pc,SIGNAL(finished(Tp::PendingOperation *)),SLOT(onNewContactRetrieved(Tp::PendingOperation *)));
+     return true;
 }
 
 void TpSessionAccount::onNewContactRetrieved(Tp::PendingOperation *op)
 {
+    Tp::ContactPtr contact;
     Tp::PendingContacts *pcontacts = qobject_cast<Tp::PendingContacts *>(op);
     QList<Tp::ContactPtr> contacts = pcontacts->contacts();
     QString username = pcontacts->identifiers().first();
     if (contacts.size() != 1 || !contacts.first()) {
         qDebug() << "Unable to add contact " <<reqContact;
+        emit contactRetrieved(contact,false,false);
+        if(!reqMessage.isEmpty()) { // If there is requesting messsage, can't queue if contact failed
+          emit messageQueued(this,false);
+          reqMessage.clear();
+        };
+
         return;
     }
 
-    Tp::ContactPtr contact = contacts.first();
+    contact = contacts.first();
     //    qDebug() << "TpSessionAccount::onContactRetrieved" << reqContact;
-    if(!reqContact.isEmpty()) addOutgoingChannel(contacts.first());
+    if(!reqContact.isEmpty()) {
+        addOutgoingChannel(contacts.first());
+        emit contactRetrieved(contact,true,true);
+    }
+    else
+        emit contactRetrieved(contact,true,false);
 }
 /**
  * Send message to given address. This function is compled Acynchronous function that may produce multiple state transitions beforecomletion.
@@ -217,8 +234,9 @@ void TpSessionAccount::sendMessageToAddress(QString address,QString message)
     Tp::ContactPtr p;
     TpSessionChannel* channel=getChannelFromPeerAddress(address);
     if(channel) {
+      //  qDebug() << "TpSessionAccount::sendMessageToAddress peer:" << channel->peerId() << "queuing:" << message;
         channel->sendMessage(message); // We have already channel
-        emit messageQueued(this);
+        emit messageQueued(this,true);
     }
     else {
         reqMessage=message;
@@ -239,6 +257,8 @@ void TpSessionAccount::addOutgoingChannel(const Tp::ContactPtr &contact)
      TpSessionChannel* newChannel=new TpSessionChannel(contact->manager()->connection(),contact);
      connect(newChannel,SIGNAL(messageReceived(const Tp::ReceivedMessage &,TpSessionChannel *)),
              SLOT(onMessageReceived(const Tp::ReceivedMessage &,TpSessionChannel *)));
+     connect(newChannel,SIGNAL(messageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &,TpSessionChannel *)),
+             SLOT(onMessageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &,TpSessionChannel *)));
      connect(newChannel,SIGNAL(channelReady(TpSessionChannel *)),
             SLOT(onOutgoingChannelReady(TpSessionChannel*)));
      myChannels+=newChannel;
@@ -247,26 +267,29 @@ void TpSessionAccount::addOutgoingChannel(const Tp::ContactPtr &contact)
 
 void TpSessionAccount::onOutgoingChannelReady(TpSessionChannel *ch)
 {
-  // qDebug() << "TpSessionAccoiunt::onOutgoingChannelReady";
+  // qDebug() << "TpSessionAccount::onOutgoingChannelReady";
  emit channelReady(this);
  if(!reqMessage.isEmpty()) {
+   //  qDebug() << "TpSessionAccount::onOutgoingChannelReady peer:" << ch->peerId() << "queuing:" << reqMessage;
    ch->sendMessage(reqMessage);
-   emit messageQueued(this);
+   emit messageQueued(this,true);
  };
  reqMessage.clear();
 }
 
 
-void TpSessionAccount::onMessageSent(const Tp::Message &msg,Tp::MessageSendingFlags, const QString &flags)
+void TpSessionAccount::onMessageSent(const Tp::Message &msg,Tp::MessageSendingFlags, const QString &flags,TpSessionChannel *ch)
 {
-  //    qDebug() << "TpSessionAccount::onMessageSent";
+    Q_UNUSED(flags);
+    // qDebug() << "TpSessionAccount::onMessageSent peer:" << ch->peerId() <<"txt:" << msg.text();
     emit messageSent(msg,this);
 };
 
 void TpSessionAccount::onMessageReceived(const Tp::ReceivedMessage &msg,TpSessionChannel *ch)
 {
+  Q_UNUSED(ch);
   //    qDebug() << "TpSessionAccount::onMessageReceived " << msg.text();
-    emit messageReceived(msg,this);
+  emit messageReceived(msg,this);
 };
 
 void TpSessionAccount::onNewChannels(const Tp::ChannelDetailsList &channels)
@@ -289,6 +312,8 @@ void TpSessionAccount::onNewChannels(const Tp::ChannelDetailsList &channels)
             TpSessionChannel* newChannel=new TpSessionChannel( myIngoingTextChannel);
             connect(newChannel,SIGNAL(messageReceived(const Tp::ReceivedMessage &,TpSessionChannel *)),
              SLOT(onMessageReceived(const Tp::ReceivedMessage &,TpSessionChannel *)));
+            connect(newChannel,SIGNAL(messageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &,TpSessionChannel *)),
+                    SLOT(onMessageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &,TpSessionChannel *)));
             myChannels+=newChannel;
         }
         if (channelType == TELEPATHY_INTERFACE_CHANNEL_TYPE_STREAMED_MEDIA && !requested) {