conversation: fix incoming call

Change-Id: Id503ab366006f30e76ff718deef1fc3214e50c80
diff --git a/src/AppDelegate.mm b/src/AppDelegate.mm
index 6039dd9..e835d1b 100644
--- a/src/AppDelegate.mm
+++ b/src/AppDelegate.mm
@@ -242,7 +242,7 @@
         NSString* name = interaction.authorUri.toNSString();
         auto convIt = getConversationFromUid(conversation, *lrc->getAccountModel()
                                              .getAccountInfo(accountId)
-                                             .conversationModel.get(), false);
+                                             .conversationModel.get());
         auto convQueue = lrc->getAccountModel()
         .getAccountInfo(accountId)
         .conversationModel.get()->allFilteredConversations();
diff --git a/src/ChooseContactVC.mm b/src/ChooseContactVC.mm
index 58fe50e..5340025 100644
--- a/src/ChooseContactVC.mm
+++ b/src/ChooseContactVC.mm
@@ -151,7 +151,7 @@
     auto accountID = conversation.accountId;
     auto convID = conversation.convId;
     auto convMod = convModel->owner.accountModel->getAccountInfo(accountID).conversationModel.get();
-    auto conversationInfo = getConversationFromUid(convID, *convMod, false);
+    auto conversationInfo = getConversationFromUid(convID, *convMod);
     auto convQueue = convModel->owner.accountModel->getAccountInfo(accountID).conversationModel.get()->allFilteredConversations();
     if (conversationInfo == convQueue.end()) {
         return;
@@ -212,7 +212,7 @@
             auto accountID = conversation.accountId;
             auto convID = conversation.convId;
             auto *convMod = convModel->owner.accountModel->getAccountInfo(accountID).conversationModel.get();
-            auto *conversationInfo = &(*getConversationFromUid(convID, *convMod, false));
+            auto *conversationInfo = &(*getConversationFromUid(convID, *convMod));
             if (displayNameString.length > 0) {
                 displayNameString = [displayNameString stringByAppendingString:@", "];
             }
@@ -232,7 +232,7 @@
     auto accountID = conversation.accountId;
     auto convID = conversation.convId;
     auto *convMod = convModel->owner.accountModel->getAccountInfo(accountID).conversationModel.get();
-    auto *conversationInfo = &(*getConversationFromUid(convID, *convMod, false));
+    auto *conversationInfo = &(*getConversationFromUid(convID, *convMod));
     if (conversationInfo->participants.empty()) {
         return nil;
     }
diff --git a/src/ConversationVC.mm b/src/ConversationVC.mm
index 186a8b6..8c9d960 100644
--- a/src/ConversationVC.mm
+++ b/src/ConversationVC.mm
@@ -113,9 +113,15 @@
     if (cachedConv_ != nil)
         return cachedConv_;
 
-    auto it = getConversationFromUid(convUid_, *convModel_, true);
-    if (conversationExists(it, *convModel_, true))
+    auto it = getConversationFromUid(convUid_, *convModel_);
+    if (conversationExists(it, *convModel_)) {
         cachedConv_ = &(*it);
+    } else {
+        it = getSearchResultFromUid(convUid_, *convModel_);
+        if (searchResultExists(it, *convModel_)) {
+            cachedConv_ = &(*it);
+        }
+    }
 
     return cachedConv_;
 }
diff --git a/src/CurrentCallVC.mm b/src/CurrentCallVC.mm
index 30428d3..d0022cf 100644
--- a/src/CurrentCallVC.mm
+++ b/src/CurrentCallVC.mm
@@ -159,10 +159,7 @@
     callUid_ = callUid;
     convUid_ = convUid;
     accountInfo_ = account;
-    auto convIt = getConversationFromUid(convUid_, *accountInfo_->conversationModel, true);
-    if (!conversationExists(convIt, *accountInfo_->conversationModel.get(), true)) {
-        return;
-    }
+    auto convIt = getConversationFromUid(convUid_, *accountInfo_->conversationModel);
     confUid_ = convIt->confId;
     [self.chatVC setConversationUid:convUid model:account->conversationModel.get()];
     [self connectSignals];
@@ -200,7 +197,7 @@
     QObject::connect(accountInfo_->contactModel.get(),
                      &lrc::api::ContactModel::contactAdded,
                      [self](const QString &contactUri) {
-                         auto convIt = getConversationFromUid(convUid_, *accountInfo_->conversationModel.get(), false);
+                         auto convIt = getConversationFromUid(convUid_, *accountInfo_->conversationModel.get());
                          if (convIt == accountInfo_->conversationModel->allFilteredConversations().end()) {
                              return;
                          }
@@ -319,9 +316,8 @@
     }
 
     auto currentCall = callModel->getCall(callUid_);
-    NSLog(@"\n status %@ \n",lrc::api::call::to_string(currentCall.status).toNSString());
-    auto convIt = getConversationFromUid(convUid_, *accountInfo_->conversationModel, true);
-    if (conversationExists(convIt, *accountInfo_->conversationModel, true)) {
+    auto convIt = getConversationFromUid(convUid_, *accountInfo_->conversationModel);
+    if (conversationExists(convIt, *accountInfo_->conversationModel)) {
         NSString* bestName = bestNameForConversation(*convIt, *accountInfo_->conversationModel);
         [contactNameLabel setStringValue:bestName];
         NSString* ringID = bestIDForConversation(*convIt, *accountInfo_->conversationModel);
@@ -331,7 +327,6 @@
         [contactIdLabel setStringValue:ringID];
     }
     [self setupContactInfo:contactPhoto];
-
     confUid_ = convIt->confId;
     [muteAudioButton setHidden:!confUid_.isEmpty()];
     [muteVideoButton setHidden:!confUid_.isEmpty()];
@@ -491,7 +486,7 @@
 
 -(NSImage *) getContactImageOfSize: (double) size withDefaultAvatar:(BOOL) shouldDrawDefault {
     auto* convModel = accountInfo_->conversationModel.get();
-    auto convIt = getConversationFromUid(convUid_, *convModel, false);
+    auto convIt = getConversationFromUid(convUid_, *convModel);
     if (convIt == convModel->allFilteredConversations().end()) {
         return nil;
     }
@@ -818,12 +813,8 @@
         return;
 
     // If we accept a conversation with a non trusted contact, we first accept it
-    auto convIt = getConversationFromUid(convUid_, *accountInfo_->conversationModel.get(), true);
-    if (!conversationExists(convIt,*accountInfo_->conversationModel.get(), true)) {
-        return;
-    }
-
-    if (convIt == accountInfo_->conversationModel->allFilteredConversations().end()) {
+    auto convIt = getConversationFromUid(convUid_, *accountInfo_->conversationModel.get());
+    if (convIt != accountInfo_->conversationModel->allFilteredConversations().end()) {
         auto& contact = accountInfo_->contactModel->getContact(convIt->participants[0]);
         if (contact.profileInfo.type == lrc::api::profile::Type::PENDING)
             accountInfo_->conversationModel->makePermanent(convUid_);
diff --git a/src/LeaveMessageVC.mm b/src/LeaveMessageVC.mm
index a7fac23..fbe8333 100644
--- a/src/LeaveMessageVC.mm
+++ b/src/LeaveMessageVC.mm
@@ -189,7 +189,7 @@
 }
 
 -(void) updateView {
-    auto it = getConversationFromUid(conversationUid, *conversationModel, false);
+    auto it = getConversationFromUid(conversationUid, *conversationModel);
     if (it != conversationModel->allFilteredConversations().end()) {
         auto& imgManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator());
         QVariant photo = imgManip.conversationPhoto(*it, conversationModel->owner, QSize(120, 120), NO);
diff --git a/src/MessagesVC.mm b/src/MessagesVC.mm
index 9258027..5758e7f 100644
--- a/src/MessagesVC.mm
+++ b/src/MessagesVC.mm
@@ -177,10 +177,15 @@
 
     if (cachedConv_ != nil)
         return cachedConv_;
-    auto it = getConversationFromUid(convUid_, *convModel_, true);
-    if (conversationExists(it, *convModel_, true))
+    auto it = getConversationFromUid(convUid_, *convModel_);
+    if (conversationExists(it, *convModel_)) {
         cachedConv_ = &(*it);
-
+    } else {
+        it = getSearchResultFromUid(convUid_, *convModel_);
+        if (searchResultExists(it, *convModel_)) {
+            cachedConv_ = &(*it);
+        }
+    }
     return cachedConv_;
 }
 
diff --git a/src/RingWindowController.mm b/src/RingWindowController.mm
index 45d82b2..b0b0472 100644
--- a/src/RingWindowController.mm
+++ b/src/RingWindowController.mm
@@ -609,12 +609,16 @@
     [smartViewVC setConversationModel:accInfo.conversationModel.get()];
     [smartViewVC selectConversationList];
     [self updateRingID];
-    auto convInfo = getConversationFromUid(QString::fromNSString(conversationId), *accInfo.conversationModel.get(), true);
-    if (conversationExists(convInfo, *accInfo.conversationModel.get(), true)) {
-        [conversationVC setConversationUid:convInfo->uid model:accInfo.conversationModel.get()];
-        [smartViewVC selectConversation: *convInfo model:accInfo.conversationModel.get()];
-        accInfo.conversationModel.get()->clearUnreadInteractions(QString::fromNSString(conversationId));
+    auto convInfo = getConversationFromUid(QString::fromNSString(conversationId), *accInfo.conversationModel.get());
+    if (!conversationExists(convInfo, *accInfo.conversationModel.get())) {
+        convInfo = getSearchResultFromUid(QString::fromNSString(conversationId), *accInfo.conversationModel.get());
+        if (!searchResultExists(convInfo, *accInfo.conversationModel.get())) {
+            return;
+        }
     }
+    [conversationVC setConversationUid:convInfo->uid model:accInfo.conversationModel.get()];
+    [smartViewVC selectConversation: *convInfo model:accInfo.conversationModel.get()];
+    accInfo.conversationModel.get()->clearUnreadInteractions(QString::fromNSString(conversationId));
     [self changeViewTo:SHOW_CONVERSATION_SCREEN];
 }
 
@@ -624,14 +628,20 @@
     [settingsVC setSelectedAccount:accInfo.id];
     [smartViewVC setConversationModel:accInfo.conversationModel.get()];
     [self updateRingID];
-    auto convInfo = getConversationFromUid(QString::fromNSString(conversationId), *accInfo.conversationModel.get(), true);
-    if (conversationExists(convInfo, *accInfo.conversationModel.get(), true)) {
-        if (accInfo.contactModel->getContact(convInfo->participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING)
-            [smartViewVC selectPendingList];
-        else
-            [smartViewVC selectConversationList];
-        [smartViewVC selectConversation: *convInfo model:accInfo.conversationModel.get()];
+    auto convInfo = getConversationFromUid(QString::fromNSString(conversationId), *accInfo.conversationModel.get());
+    if (!conversationExists(convInfo, *accInfo.conversationModel.get())) {
+        convInfo = getSearchResultFromUid(QString::fromNSString(conversationId), *accInfo.conversationModel.get());
+        if (!searchResultExists(convInfo, *accInfo.conversationModel.get())) {
+            return;
+        }
     }
+    if (accInfo.contactModel->getContact(convInfo->participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING) {
+        [smartViewVC selectPendingList];
+    }
+    else {
+        [smartViewVC selectConversationList];
+    }
+    [smartViewVC selectConversation: *convInfo model:accInfo.conversationModel.get()];
     [currentCallVC setCurrentCall:QString::fromNSString(callId)
                      conversation:QString::fromNSString(conversationId)
                           account:&accInfo
diff --git a/src/SmartViewVC.mm b/src/SmartViewVC.mm
index e3438bd..1d4caf1 100755
--- a/src/SmartViewVC.mm
+++ b/src/SmartViewVC.mm
@@ -203,7 +203,7 @@
     [smartView layoutSubtreeIfNeeded];
 
     if (!selectedUid_.isEmpty() && convModel_ != nil) {
-        auto it = getConversationFromUid(selectedUid_, *convModel_, false);
+        auto it = getConversationFromUid(selectedUid_, *convModel_);
         if (it != convModel_->allFilteredConversations().end()) {
             NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - convModel_->allFilteredConversations().begin())];
             [smartView selectRowIndexes:indexSet byExtendingSelection:NO];
@@ -219,12 +219,16 @@
         return;
     }
 
-    auto it = getConversationFromUid(QString::fromNSString(uid), *convModel_, true);
+    auto it = getConversationFromUid(QString::fromNSString(uid), *convModel_);
     if (it != convModel_->allFilteredConversations().end()) {
         NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - convModel_->allFilteredConversations().begin())];
         [smartView reloadDataForRowIndexes:indexSet
                              columnIndexes:[NSIndexSet indexSetWithIndex:0]];
-    } else if (it != convModel_->getAllSearchResults().end()) {
+    } else {
+        it = getSearchResultFromUid(QString::fromNSString(uid), *convModel_);
+        if (it == convModel_->getAllSearchResults().end()) {
+            return;
+        }
         NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - convModel_->getAllSearchResults().begin())];
         [searchResultsView reloadDataForRowIndexes:indexSet
                                      columnIndexes:[NSIndexSet indexSetWithIndex:0]];
@@ -328,12 +332,16 @@
         return;
     }
 
-    auto it = getConversationFromUid(uid, *convModel_, true);
+    auto it = getConversationFromUid(uid, *convModel_);
     if (it != convModel_->allFilteredConversations().end()) {
         NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - convModel_->allFilteredConversations().begin())];
         [smartView selectRowIndexes:indexSet byExtendingSelection:NO];
         selectedUid_ = uid;
-    } else if (it != convModel_->getAllSearchResults().end()) {
+    } else {
+        it = getSearchResultFromUid(uid, *convModel_);
+        if (it == convModel_->getAllSearchResults().end()) {
+            return;
+        }
         NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - convModel_->getAllSearchResults().begin())];
         [searchResultsView selectRowIndexes:indexSet byExtendingSelection:NO];
         selectedUid_ = uid;
@@ -682,7 +690,7 @@
     }
     [self clearSearchField];
     auto uid = QString::fromNSString(uId);
-    auto it = getConversationFromUid(uid, *convModel_, false);
+    auto it = getConversationFromUid(uid, *convModel_);
     if (it != convModel_->allFilteredConversations().end()) {
         @try {
             auto contact = convModel_->owner.contactModel->getContact(it->participants[0]);
diff --git a/src/utils.h b/src/utils.h
index b825344..8a6c958 100755
--- a/src/utils.h
+++ b/src/utils.h
@@ -127,19 +127,25 @@
  * the iterator is invalid thus it needs to be checked by caller.
  * @param uid UID of conversation being searched
  * @param model ConversationModel in which to do the lookup
- * @param includeSearchResult include search result
  * @return iterator pointing to corresponding Conversation if any. Points to past-the-end element otherwise.
  */
-static inline lrc::api::ConversationModel::ConversationQueue::const_iterator getConversationFromUid(const QString& uid, const lrc::api::ConversationModel& model, bool includeSearchResult) {
-    auto result = std::find_if(model.allFilteredConversations().begin(), model.allFilteredConversations().end(),
+static inline lrc::api::ConversationModel::ConversationQueue::const_iterator getConversationFromUid(const QString& uid, const lrc::api::ConversationModel& model) {
+    return std::find_if(model.allFilteredConversations().begin(), model.allFilteredConversations().end(),
                         [&] (const lrc::api::conversation::Info& conv) {
-                            return uid == conv.uid;
-                        });
-    if (!includeSearchResult || (result != model.allFilteredConversations().end())) {
-        return result;
-    }
+        return uid == conv.uid;
+    });
+}
+
+/**
+* This function return an iterator pointing to a Conversation::Info in ConversationModel given its uid. If not found
+* the iterator is invalid thus it needs to be checked by caller.
+* @param uid UID of conversation being searched
+* @param model ConversationModel in which to do the lookup
+* @return iterator pointing to corresponding Conversation if any. Points to past-the-end element otherwise.
+*/
+static inline lrc::api::ConversationModel::ConversationQueue::const_iterator getSearchResultFromUid(const QString& uid, const lrc::api::ConversationModel& model) {
     return std::find_if(model.getAllSearchResults().begin(), model.getAllSearchResults().end(),
-    [&] (const lrc::api::conversation::Info& conv) {
+                        [&] (const lrc::api::conversation::Info& conv) {
         return uid == conv.uid;
     });
 }
@@ -149,11 +155,21 @@
  * the iterator is invalid thus it needs to be checked by caller.
  * @param conversation iterator pointing to a Conversation::Info in ConversationModel
  * @param model ConversationModel in which to do the lookup
- * @param includeSearchResult include search result
  * @return iterator pointing to corresponding Conversation if any. Points to past-the-end element otherwise.
  */
-static inline bool conversationExists(lrc::api::ConversationModel::ConversationQueue::const_iterator conversation, const lrc::api::ConversationModel& model, bool includeSearchResult) {
-    return includeSearchResult ? (conversation != model.allFilteredConversations().end() || conversation != model.getAllSearchResults().end()) : conversation != model.allFilteredConversations().end();
+static inline bool conversationExists(lrc::api::ConversationModel::ConversationQueue::const_iterator conversation, const lrc::api::ConversationModel& model) {
+    return conversation != model.allFilteredConversations().end();
+}
+
+/**
+ * This function true if conversation exists
+ * the iterator is invalid thus it needs to be checked by caller.
+ * @param conversation iterator pointing to a Conversation::Info in ConversationModel
+ * @param model ConversationModel in which to do the lookup
+ * @return iterator pointing to corresponding Conversation if any. Points to past-the-end element otherwise.
+ */
+static inline bool searchResultExists(lrc::api::ConversationModel::ConversationQueue::const_iterator conversation, const lrc::api::ConversationModel& model) {
+    return conversation != model.getAllSearchResults().end();
 }
 
 /**