private void chat_FileAdded(BitChat.Peer peer, MessageItem message, SharedFile sharedFile)
        {
            ChatMessageFileItem fileItem = new ChatMessageFileItem(peer, message, sharedFile);

            AddMessage(fileItem, true);

            fileItem.ShareFile += FileItem_ShareFile;
        }
        private List <CustomListViewItem> ConvertToListViewItems(MessageItem[] items, bool updateLastMessageInChatItem)
        {
            BitChat.Peer[] peerList = _chat.GetPeerList();
            SharedFile[]   fileList = _chat.GetSharedFileList();

            List <CustomListViewItem> listItems = new List <CustomListViewItem>(items.Length);
            DateTime lastItemDate    = new DateTime();
            string   lastMessage     = null;
            DateTime lastMessageDate = new DateTime();

            foreach (MessageItem item in items)
            {
                if (lastItemDate.Date < item.MessageDate.Date)
                {
                    lastItemDate = item.MessageDate;
                    listItems.Add(new ChatMessageInfoItem(new MessageItem(lastItemDate)));
                }

                switch (item.Type)
                {
                case MessageType.Info:
                    listItems.Add(new ChatMessageInfoItem(item));
                    break;

                case MessageType.TextMessage:
                case MessageType.InvitationMessage:
                {
                    BitChat.Peer sender = null;

                    foreach (BitChat.Peer peer in peerList)
                    {
                        if (peer.PeerCertificate.IssuedTo.EmailAddress.Address.Equals(item.Sender))
                        {
                            sender = peer;
                            break;
                        }
                    }

                    listItems.Add(new ChatMessageTextItem(sender, item));

                    if (sender == null)
                    {
                        lastMessage = item.Sender + ": " + item.Message;
                    }
                    else if (sender.IsSelf)
                    {
                        lastMessage = item.Message;
                    }
                    else
                    {
                        lastMessage = sender.PeerCertificate.IssuedTo.Name + ": " + item.Message;
                    }

                    lastMessageDate = item.MessageDate;
                }
                break;

                case MessageType.SharedFileMetaData:
                {
                    BitChat.Peer sender = null;
                    SharedFile   file   = null;

                    foreach (BitChat.Peer peer in peerList)
                    {
                        if (peer.PeerCertificate.IssuedTo.EmailAddress.Address.Equals(item.Sender))
                        {
                            sender = peer;
                            break;
                        }
                    }

                    foreach (SharedFile sharedFile in fileList)
                    {
                        if (sharedFile.MetaData.FileID.Equals(item.SharedFileMetaData.FileID))
                        {
                            file = sharedFile;
                            break;
                        }
                    }

                    ChatMessageFileItem fileItem = new ChatMessageFileItem(sender, item, file);
                    listItems.Add(fileItem);

                    fileItem.ShareFile += FileItem_ShareFile;

                    if (sender == null)
                    {
                        lastMessage = item.Sender + " shared a file";
                    }
                    else if (sender.IsSelf)
                    {
                        lastMessage = "file was shared";
                    }
                    else
                    {
                        lastMessage = sender.PeerCertificate.IssuedTo.Name + " shared a file";
                    }

                    lastMessageDate = item.MessageDate;
                }
                break;
                }
            }

            if (updateLastMessageInChatItem && (lastMessage != null))
            {
                _chatItem.SetLastMessage(lastMessage, lastMessageDate, false);
            }

            return(listItems);
        }