public void SetReceiptPartOfMessage(Model.Message message, ReceiptType receiptType)
        {
            String receipt = receiptType.ToString();

            if (message.ReceiptType == receipt)
            {
                return;
            }

            message.ReceiptType = receipt;
            switch (receiptType)
            {
            case ReceiptType.ServerReceived:
                message.ReceiptSource = "msg_check";
                break;

            case ReceiptType.ClientReceived:
                message.ReceiptSource = "msg_not_read";
                break;

            case ReceiptType.ClientRead:
                message.ReceiptSource = "msg_read";
                break;

            case ReceiptType.None:
                message.ReceiptSource = "msg_sending";
                break;
            }
        }
        private void AddToModelRbMessages(List <Rainbow.Model.Message> rbMessagesList)
        {
            List <InstantMessaging.Model.Message> messagesList = new List <InstantMessaging.Model.Message>();

            foreach (Rainbow.Model.Message rbMessage in rbMessagesList)
            {
                InstantMessaging.Model.Message msg = GetMessageFromRBMessage(rbMessage, rbConversation.Type);
                if (msg != null)
                {
                    messagesList.Insert(0, msg);
                    msg.AvatarSource = Helper.GetContactAvatarImageSource(msg.PeerId);
                }
            }

            lock (lockObservableMessagesList)
            {
                if (MessagesList.Count == 0)
                {
                    MessagesList.ReplaceRange(messagesList);
                }
                else
                {
                    MessagesList.AddRange(messagesList, System.Collections.Specialized.NotifyCollectionChangedAction.Reset, 0);
                }
            }
        }
        private void FilePool_FileDescriptorAvailable(object sender, Rainbow.Events.IdEventArgs e)
        {
            String conversationId = filePool.GetConversationIdForFileDescriptorId(e.Id);

            if (conversationId == this.conversationId)
            {
                Model.Message message = GetMessageByFileDescriptorId(e.Id);
                if (message != null)
                {
                }
            }
        }
 private void RbInstantMessaging_ReceiptReceived(object sender, Rainbow.Events.ReceiptReceivedEventArgs e)
 {
     if (e.ConversationId == this.conversationId)
     {
         log.Debug("[RbInstantMessaging_ReceiptReceived] MessageId:[{0}] - ReceiptType:[{1}]", e.MessageId, e.ReceiptType);
         Model.Message message = GetMessageByMessageId(e.MessageId);
         if (message != null)
         {
             SetReceiptPartOfMessage(message, e.ReceiptType);
         }
     }
 }
        private void FilePool_ThumbnailAvailable(object sender, Rainbow.Events.IdEventArgs e)
        {
            String conversationId = filePool.GetConversationIdForFileDescriptorId(e.Id);

            if (conversationId == this.conversationId)
            {
                Model.Message message = GetMessageByFileDescriptorId(e.Id);
                if (message != null)
                {
                    SetFileAttachmentSourceOfMessage(message, e.Id);
                }
            }
        }
 private InstantMessaging.Model.Message GetMessageByFileDescriptorId(String fileId)
 {
     InstantMessaging.Model.Message result = null;
     lock (lockObservableMessagesList)
     {
         foreach (InstantMessaging.Model.Message msg in MessagesList)
         {
             if (msg.FileId == fileId)
             {
                 result = msg;
                 break;
             }
         }
     }
     return(result);
 }
 private InstantMessaging.Model.Message GetMessageByMessageId(String messageId)
 {
     InstantMessaging.Model.Message result = null;
     lock (lockObservableMessagesList)
     {
         foreach (InstantMessaging.Model.Message msg in MessagesList)
         {
             if (msg.Id == messageId)
             {
                 result = msg;
                 break;
             }
         }
     }
     return(result);
 }
Example #8
0
 protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
 {
     if (item is InstantMessaging.Model.Message)
     {
         InstantMessaging.Model.Message message = item as InstantMessaging.Model.Message;
         if (message.IsEventMessage == "True")
         {
             return(messageViewEvent);
         }
         else if (message.PeerId == GetCurrentContactId())
         {
             return(messageViewCurrentUser);
         }
         return(messageViewOtherUser);
     }
     return(null);
 }
        private void SetEventPartFromCallLog(Model.Message message)
        {
            Rainbow.Model.Contact contact = XamarinApplication.RbContacts.GetContactFromContactJid(message.CallOtherJid);
            if (contact != null)
            {
                String displayName = Util.GetContactDisplayName(contact);
                if (message.CallState == CallLog.LogState.ANSWERED.ToString())
                {
                    if (message.CallOriginator == "True")
                    {
                        message.EventMessageBodyPart1 = "You called " + displayName + ".";
                    }
                    else
                    {
                        message.EventMessageBodyPart1 = displayName + " has called you.";
                    }

                    double nbSecs = Math.Round((double)message.CallDuration / 1000);
                    int    mns    = (int)(nbSecs / 60);
                    int    sec    = (int)Math.Round(nbSecs - (mns * 60));
                    message.EventMessageBodyPart2 = "Duration: " + ((mns > 0) ? mns + ((mns > 1) ? "mns " : "mn ") : "") + ((sec > 0) ? sec + "s" : "");
                }
                else
                {
                    if (message.CallOriginator == "True")
                    {
                        message.EventMessageBodyPart1 = "You called " + displayName + ".";
                    }
                    else
                    {
                        message.EventMessageBodyPart1 = displayName + " has called you.";
                    }

                    message.EventMessageBodyPart2      = (message.CallState == CallLog.LogState.MISSED.ToString()) ? "Missed call" : "Failed call";
                    message.EventMessageBodyPart2Color = Color.Red;
                }
            }
            else
            {
                log.Debug("[SetEventPartFromCallLog] - message.Id:[{0}] - UnknowContactJid:[{1}]", message.Id, message.CallOtherJid);
                // We ask to have more info about this contact using AvatarPool
                avatarPool.AddUnknownContactToPoolByJid(message.CallOtherJid);
            }
        }
        private void SetReplyPartOfMessage(Model.Message message, Rainbow.Model.Message rbRepliedMessage)
        {
            // Store Jid of the message sender
            message.ReplyPeerJid = rbRepliedMessage.FromJid;
            if (String.IsNullOrEmpty(rbRepliedMessage.Content))
            {
                if (rbRepliedMessage.FileAttachment != null)
                {
                    message.ReplyBody = rbRepliedMessage.FileAttachment.Name;
                }
                else
                {
                    message.ReplyBody = rbRepliedMessage.Id; // Bad display but should permit to debug this situation
                }
            }
            else
            {
                message.ReplyBody = rbRepliedMessage.Content;
            }


            log.Debug("[SetReplyPartOfMessage] - message.Id:[{0}] - replyMsgId:[{1}] - replyBody:[{2}] - ContactJid:[{3}]", message.Id, rbRepliedMessage.Id, message.ReplyBody, rbRepliedMessage.FromJid);

            Rainbow.Model.Contact contactReply = XamarinApplication.RbContacts.GetContactFromContactJid(rbRepliedMessage.FromJid);
            if (contactReply != null)
            {
                // Reply part is visible
                message.ReplyPartIsVisible = "True";

                message.ReplyPeerId          = contactReply.Id;
                message.ReplyPeerDisplayName = Util.GetContactDisplayName(contactReply);
            }
            else
            {
                log.Debug("[SetReplyPartOfMessage] - message.Id:[{0}] - replyMsgId:[{1}] - UnknownContactJid[{2}]", message.Id, rbRepliedMessage.Id, rbRepliedMessage.FromJid);
                // We ask to have more info about this contact using AvatarPool
                avatarPool.AddUnknownContactToPoolByJid(rbRepliedMessage.FromJid);
            }

            // We store info about this contact in reply context
            AddRepliedContactInvolved(rbRepliedMessage.FromJid);
        }
        private void SetCallLogPartOfMessage(Model.Message message, CallLogAttachment callLogAttachment)
        {
            if (callLogAttachment != null)
            {
                message.IsEventMessage = "True";

                message.CallDuration = callLogAttachment.Duration;
                message.CallState    = callLogAttachment.State.ToString();
                message.CallType     = callLogAttachment.Type.ToString();

                if (callLogAttachment.Caller == currentContactJid)
                {
                    message.CallOriginator = "True";
                    message.CallOtherJid   = callLogAttachment.Callee;
                }
                else
                {
                    message.CallOriginator = "False";
                    message.CallOtherJid   = callLogAttachment.Caller;
                }

                SetEventPartFromCallLog(message);
            }
        }
        public void SetFileAttachmentSourceOfMessage(Model.Message message, String fileId)
        {
            string filePath = filePool.GetThumbnailFullFilePath(fileId);

            if (filePath != null)
            {
                try
                {
                    log.Debug("[SetFileAttachmentSourceOfMessage] FileId:[{0}] - Use filePath:[{1}]", fileId, filePath);
                    using (Stream stream = new MemoryStream(File.ReadAllBytes(filePath)))
                    {
                        System.Drawing.Size size = avatarPool.GetSize(stream);
                        double density           = 1;

                        message.FileAttachmentSource       = ImageSource.FromFile(filePath);
                        message.FileAttachmentSourceWidth  = (int)Math.Round(size.Width / density);
                        message.FileAttachmentSourceHeight = (int)Math.Round(size.Height / density);

                        message.FileDefaultInfoIsVisible = "False";
                    }
                }
                catch { }
            }
        }
        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);
                }
            }
        }
        private InstantMessaging.Model.Message GetMessageFromRBMessage(Rainbow.Model.Message rbMessage, String conversationType)
        {
            InstantMessaging.Model.Message message = null;
            if (rbMessage != null)
            {
                InstantMessaging.App XamarinApplication = (InstantMessaging.App)Xamarin.Forms.Application.Current;
                AvatarPool           avatarPool         = AvatarPool.Instance;

                //log.Debug("[GetMessageFromRBMessage] Message.Id:[{0}] - Message.ReplaceId:[{1}] - DateTime:[{2}] - Content:[{3}]", rbMessage.Id, rbMessage.ReplaceId, rbMessage.Date.ToString("o"), rbMessage.Content);

                message = new InstantMessaging.Model.Message();
                message.EventMessageBodyPart2Color = Color.Black; // Set default value

                Rainbow.Model.Contact contact = XamarinApplication.RbContacts.GetContactFromContactJid(rbMessage.FromJid);
                if (contact != null)
                {
                    message.PeerId = contact.Id;

                    message.PeerDisplayName = Util.GetContactDisplayName(contact);
                    message.BackgroundColor = Color.FromHex(Rainbow.Helpers.AvatarPool.GetColorFromDisplayName(message.PeerDisplayName));
                }
                else
                {
                    message.BackgroundColor = Color.FromHex(Rainbow.Helpers.AvatarPool.GetColorFromDisplayName(""));

                    // We ask to have more info about this contact usin AvatarPool
                    avatarPool.AddUnknownContactToPoolByJid(rbMessage.FromJid);
                }

                // We have the display name only in Room / Bubble context
                message.PeerDisplayNameIsVisible = (conversationType == Rainbow.Model.Conversation.ConversationType.Room) ? "True" : "False";

                message.Id      = rbMessage.Id;
                message.PeerJid = rbMessage.FromJid;

                message.MessageDateTime    = rbMessage.Date;
                message.MessageDateDisplay = Helper.HumanizeDateTime(rbMessage.Date);

                // Receipt
                SetReceiptPartOfMessage(message, rbMessage.Receipt);

                // Is-it an "Event message" ?
                String content;
                if (!String.IsNullOrEmpty(rbMessage.BubbleEvent))
                {
                    message.IsEventMessage        = "True";
                    message.EventMessageBodyPart1 = Helper.GetBubbleEventMessageBody(contact, rbMessage.BubbleEvent);
                }
                // Is-it an "CallLog message" ?
                else if (rbMessage.CallLogAttachment != null)
                {
                    SetCallLogPartOfMessage(message, rbMessage.CallLogAttachment);
                }
                else
                {
                    message.IsEventMessage     = "False";
                    message.BodyFontAttributes = FontAttributes.None;
                    message.BodyColor          = (message.PeerJid == currentContactJid) ? Color.Black : Color.White;

                    if (!String.IsNullOrEmpty(rbMessage.Content))
                    {
                        content = rbMessage.Content;
                    }
                    else
                    {
                        if (String.IsNullOrEmpty(rbMessage.ReplaceId))
                        {
                            content = "";
                        }
                        else
                        {
                            content = "This message was deleted.";
                            message.BodyFontAttributes = FontAttributes.Italic;
                            message.BodyColor          = (message.PeerJid == currentContactJid) ? Color.Gray : Color.FromHex(Rainbow.Helpers.AvatarPool.GetDarkerColorFromDisplayName(message.PeerDisplayName));
                        }
                    }

                    // Edited text visible ?
                    message.EditedIsVisible = String.IsNullOrEmpty(rbMessage.ReplaceId) ? "False" : "True";

                    // Reply part
                    // By default is not displayed
                    message.ReplyPartIsVisible = "False";
                    if (rbMessage.ReplyMessage != null)
                    {
                        // Store Id of this reply message
                        message.ReplyId = rbMessage.ReplyMessage.Id;

                        // Set background color
                        message.ReplyBackgroundColor = Color.FromHex(Rainbow.Helpers.AvatarPool.GetDarkerColorFromDisplayName(message.PeerDisplayName));

                        // We need to get Name and text of the replied message ...
                        Rainbow.Model.Message rbRepliedMessage = XamarinApplication.RbInstantMessaging.GetOneMessageFromConversationIdFromCache(this.conversationId, rbMessage.ReplyMessage.Id);
                        if (rbRepliedMessage != null)
                        {
                            SetReplyPartOfMessage(message, rbRepliedMessage);
                        }
                        else
                        {
                            AddUnknownRepliedMessageInvolved(rbMessage.ReplyMessage.Id, rbMessage.ReplyMessage.Stamp);
                        }
                    }
                    else
                    {
                        // Set default color to avoir warning
                        message.ReplyBackgroundColor = Color.White;
                    }

                    // FileAttachment
                    if (rbMessage.FileAttachment != null)
                    {
                        // Set Global info
                        message.FileAttachmentIsVisible  = "True";
                        message.FileDefaultInfoIsVisible = "True";
                        message.FileId   = rbMessage.FileAttachment.Id;
                        message.FileName = rbMessage.FileAttachment.Name;
                        message.FileSize = Helper.HumanizeFileSize(rbMessage.FileAttachment.Size);

                        if (filePool.IsThumbnailFileAvailable(conversationId, rbMessage.FileAttachment.Id, rbMessage.FileAttachment.Name))
                        {
                            SetFileAttachmentSourceOfMessage(message, rbMessage.FileAttachment.Id);
                        }
                        else
                        {
                            // Ask more info about this file
                            filePool.AskFileDescriptorDownload(this.conversationId, rbMessage.FileAttachment.Id);

                            // Set default icon
                            message.FileAttachmentSource       = "icon_unknown_blue";
                            message.FileAttachmentSourceWidth  = 50;
                            message.FileAttachmentSourceHeight = 50;
                        }
                    }
                    else
                    {
                        message.FileAttachmentIsVisible  = "False";
                        message.FileDefaultInfoIsVisible = "False";

                        message.FileAttachmentSourceWidth  = 0;
                        message.FileAttachmentSourceHeight = 0;
                    }

                    message.Body          = content;
                    message.BodyIsVisible = String.IsNullOrEmpty(message.Body) ? "False" : "True";
                }

                // We store info about this contact in message context
                AddContactInvolved(message.PeerJid);
            }
            return(message);
        }