Example #1
0
        private void RainbowInstantMessaging_MessageReceived(object sender, Rainbow.Events.MessageEventArgs e)
        {
            bool   concernCurrentSelection = false;
            string contactDisplayName      = "";

            // Check if event recevied concern the form selection or not
            if (selectionCorrect)
            {
                Conversation conversation = rainbowConversations.GetConversationByIdFromCache(e.ConversationId);
                if (contactSelected)
                {
                    if (conversation != null)
                    {
                        concernCurrentSelection = ((conversation.Type == Conversation.ConversationType.User) && (idSelected == conversation.PeerId));
                        Contact contact = rainbowContacts.GetContactFromContactId(idSelected);
                        contactDisplayName = GetContactDisplayName(contact);
                    }
                    else
                    {
                        AddStateLine($"ContactSelected - No conversation found ...");
                        return;
                    }
                }
                else
                {
                    concernCurrentSelection = (idSelected == e.ConversationId);
                    if (conversation != null)
                    {
                        if (conversation.Type == Conversation.ConversationType.User)
                        {
                            Contact contact = rainbowContacts.GetContactFromContactId(conversation.PeerId);
                            contactDisplayName = GetContactDisplayName(contact);
                        }
                        else
                        {
                            contactDisplayName = conversation.Name;
                            if (String.IsNullOrEmpty(contactDisplayName))
                            {
                                contactDisplayName = conversation.Id;
                            }
                        }
                    }
                    else
                    {
                        AddStateLine($"ConversationSelected - No conversation found ...");
                        return;
                    }
                }

                string msg;
                if (e.CarbonCopy)
                {
                    msg = $"[{SerializeDateTime(e.Message.Date)}] [YOU]: [{e.Message.Content}]";
                }
                else
                {
                    msg = $"[{SerializeDateTime(e.Message.Date)}] [{contactDisplayName}]: [{e.Message.Content}]";
                }
                if (concernCurrentSelection)
                {
                    AddMessageInStream(msg);
                    lastMessageIDReceived = e.Message.Id;
                }
                else
                {
                    AddStateLine($"Message received in another conversation - {msg}");
                }
            }
            else
            {
                String msg = $"[{SerializeDateTime(e.Message.Date)}] [{e.Message.FromJid}]: [{e.Message.Content}]";
                AddStateLine($"Message received in another conversation - {msg}");
            }
        }
Example #2
0
 private void RbInstantMessaging_MessageReceived(object sender, Rainbow.Events.MessageEventArgs e)
 {
     OnMessageReceived(sender, e);
 }
        private void RbInstantMessaging_MessageReceived(object sender, Rainbow.Events.MessageEventArgs e)
        {
            if (e.ConversationId == this.conversationId)
            {
                log.Debug("[RbInstantMessaging_MessageReceived] - FromJId:[{0}] - ToJid:[{1}] - CarbonCopy:[{2}] - Message.Id:[{3}] - Message.ReplaceId:[{4}]", e.Message.FromJid, e.Message.ToJid, e.CarbonCopy, e.Message.Id, e.Message.ReplaceId);

                InstantMessaging.Model.Message newMsg = GetMessageFromRBMessage(e.Message, rbConversation.Type);
                if (newMsg == null)
                {
                    log.Warn("[RbInstantMessaging_MessageReceived] - Impossible to have Model.Message from XMPP Message - Message.Id:[{3}]", e.Message.Id);
                    return;
                }

                // Since message is not null, set the Avatar Source
                newMsg.AvatarSource = Helper.GetContactAvatarImageSource(newMsg.PeerId);

                // Manage incoming REPLACE message
                if (!String.IsNullOrEmpty(e.Message.ReplaceId))
                {
                    InstantMessaging.Model.Message previousMessage = GetMessageByMessageId(e.Message.Id);
                    if (previousMessage == null)
                    {
                        // We do nothing in this case ... Message not in the cache, so not visible ...
                        return;
                    }

                    previousMessage.Body          = newMsg.Body;
                    previousMessage.BodyIsVisible = String.IsNullOrEmpty(previousMessage.Body) ? "False" : "True";

                    previousMessage.EditedIsVisible = "True";
                }
                // Manage incoming NEW message
                else
                {
                    // It's a new message to add in the list
                    newMessageAdded = true;

                    lock (lockObservableMessagesList)
                    {
                        // Do we have already some message ?
                        if (MessagesList.Count == 0)
                        {
                            MessagesList.Add(newMsg);
                        }
                        else
                        {
                            // Add to the list but need to check date
                            InstantMessaging.Model.Message storedMsg;
                            bool newMsgInserted = false;
                            int  nb             = MessagesList.Count - 1;

                            for (int i = nb; i > 0; i--)
                            {
                                storedMsg = MessagesList[i];
                                if (newMsg.MessageDateTime > storedMsg.MessageDateTime)
                                {
                                    if (i == nb)
                                    {
                                        MessagesList.Add(newMsg);
                                    }
                                    else
                                    {
                                        MessagesList.Insert(i, newMsg);
                                    }
                                    newMsgInserted = true;
                                    break;
                                }
                            }
                            // If we don't have already added the new message, we insert it to the first place
                            if (!newMsgInserted)
                            {
                                MessagesList.Insert(0, newMsg);
                            }
                        }
                    }
                }

                // Mark the message as read
                if (XamarinApplication.CurrentConversationId == this.conversationId)
                {
                    XamarinApplication.RbInstantMessaging.MarkMessageAsRead(this.conversationId, newMsg.Id, null);
                }
            }
        }