Exemplo n.º 1
0
        private void OnConversationClosed(object sender, ConversationEventArgs e)
        {
            if (ServiceManager.Instance.Dispatcher.Thread != Thread.CurrentThread)
            {
                ServiceManager.Instance.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                               new EventHandler <ConversationEventArgs>(OnConversationClosed), sender, new object[] { e });
                return;
            }
            VATRPContact contact = e.Conversation.Contact;

            if (contact != null)
            {
                if (ChatViewContact != null && ChatViewContact.Contact == e.Conversation.Contact)
                {
                    ChatViewContact = null;
                }

                RemoveContact(contact);

                if (this.Chat == e.Conversation)
                {
                    this._chat       = null;
                    MessagesListView = null;
                }
                OnPropertyChanged("Chat");
            }
        }
Exemplo n.º 2
0
        public bool ComposeAndSendMessage(VATRPChat chat, string text)
        {
            VATRPChat chatID = this.FindChat(chat);
            if ((chatID == null) || (chatID.Contact == null))
            {
                return false;
            }
            var declineMessage = text.StartsWith(VATRPChatMessage.DECLINE_PREFIX);
            var message = new VATRPChatMessage(MessageContentType.Text)
            {
                Direction = MessageDirection.Outgoing,
                Status = LinphoneChatMessageState.LinphoneChatMessageStateIdle,
                MessageTime = DateTime.Now,
                IsIncompleteMessage = false,
                Chat = chatID,
                IsDeclineMessage = declineMessage
            };

            message.Content = declineMessage ? text.Substring(VATRPChatMessage.DECLINE_PREFIX.Length) : text;

            // send message to linphone
            IntPtr msgPtr = IntPtr.Zero;
            _linphoneSvc.SendChatMessage(chat, text, ref msgPtr);
            if (msgPtr != IntPtr.Zero)
                message.MessageTime = Time.ConvertUtcTimeToLocalTime(LinphoneAPI.linphone_chat_message_get_time(msgPtr));

            message.NativePtr = msgPtr;

            chat.AddMessage(message, false);
            chat.UpdateLastMessage(false);

            chat.UnreadMsgCount = 0;
            chat.Contact.UnreadMsgCount = 0;

            OnConversationUnReadStateChanged(chat);
            this.OnConversationUpdated(chat, true);

            return true;
        }
 public ConversationEventArgs(VATRPChat chat)
 {
     this.Conversation = chat;
 }
Exemplo n.º 4
0
        public bool SendChatMessage(VATRPChat chat, string message, ref IntPtr msgPtr)
        {
            if (chat == null)
                return false;

            lock (messagingLock)
            {
                IntPtr chatPtr = LinphoneAPI.linphone_core_get_chat_room_from_uri(linphoneCore, chat.Contact.ID);
                chat.NativePtr = chatPtr;

                msgPtr = LinphoneAPI.linphone_chat_room_create_message(chat.NativePtr, message);
                LinphoneAPI.linphone_chat_message_ref(msgPtr);
            }
            if (msgPtr != IntPtr.Zero)
            {
                var cmd = new SendChatMessageCommand(msgPtr, chat.NativePtr);
                lock (commandQueue)
                {
                    commandQueue.Enqueue(cmd);
                }
            }

            return true;
        }
        private void OnConversationClosed(object sender, ConversationEventArgs e)
        {
            if (ServiceManager.Instance.Dispatcher.Thread != Thread.CurrentThread)
            {
                ServiceManager.Instance.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    new EventHandler<ConversationEventArgs>(OnConversationClosed), sender, new object[] { e });
                return;
            }
            VATRPContact contact = e.Conversation.Contact;
            if (contact != null)
            {
                if (_contactViewModel != null && _contactViewModel.Contact == e.Conversation.Contact)
                {
                    _contactViewModel = null;
                }

                RemoveContact(contact);

                if (this.Chat == e.Conversation)
                {
                    this._chat = null;
                    MessagesListView = null;
                }
                OnPropertyChanged("Chat");
            }
        }
Exemplo n.º 6
0
 private void CheckTopPosition(VATRPChat chat)
 {
     if ((chat != null) && (this._chatItems.IndexOf(chat) > 0))
     {
         this._chatItems.ReplaceToTop<VATRPChat>(chat);
     }
 }
Exemplo n.º 7
0
        private void LoadMessages(VATRPChat chat, IntPtr chatRoomPtr)
        {
            IntPtr msgListPtr = LinphoneAPI.linphone_chat_room_get_history(chatRoomPtr, 0);
            if (msgListPtr != IntPtr.Zero)
            {
                MSList msMessagePtr;

                var loggedInContact = _contactSvc.FindLoggedInContact();
                if (loggedInContact == null)
                    return;
                do
                {
                    msMessagePtr.next = IntPtr.Zero;
                    msMessagePtr.prev = IntPtr.Zero;
                    msMessagePtr.data = IntPtr.Zero;

                    msMessagePtr = (MSList) Marshal.PtrToStructure(msgListPtr, typeof (MSList));
                    if (msMessagePtr.data != IntPtr.Zero)
                    {
                        IntPtr tmpPtr = LinphoneAPI.linphone_chat_message_get_from_address(msMessagePtr.data);
                        tmpPtr = LinphoneAPI.linphone_address_as_string(tmpPtr);
                        if (tmpPtr != IntPtr.Zero)
                        {
                            var fromParty = LinphoneAPI.PtrToStringUtf8(tmpPtr).TrimSipPrefix();
                            LinphoneAPI.ortp_free(tmpPtr);

                            tmpPtr = LinphoneAPI.linphone_chat_message_get_to_address(msMessagePtr.data);
                            if (tmpPtr != IntPtr.Zero)
                            {
                                tmpPtr = LinphoneAPI.linphone_address_as_string(tmpPtr);
                                var toParty = LinphoneAPI.PtrToStringUtf8(tmpPtr).TrimSipPrefix();
                                LinphoneAPI.ortp_free(tmpPtr);

                                string dn, un, host;
                                int port;

                                if (fromParty == loggedInContact.ID || toParty == loggedInContact.ID)
                                {
                                    bool isOutgoing = false;
                                    string remoteParty = fromParty;
                                    if (fromParty == loggedInContact.ID)
                                    {
                                        isOutgoing = true;
                                        remoteParty = toParty;
                                    }

                                    if (
                                        !VATRPCall.ParseSipAddressEx(remoteParty, out dn, out un,
                                            out host, out port))
                                        un = "";

                                    if (un.NotBlank())
                                    {
                                        var contactAddress = string.Format("{0}@{1}", un, host);
                                        var contactID = new ContactID(contactAddress, IntPtr.Zero);
                                        var contact = FindContact(contactID);

                                        if (contact == null)
                                        {
                                            contact = new VATRPContact(contactID)
                                            {
                                                DisplayName = dn,
                                                Fullname = un,
                                                SipUsername = un,
                                                RegistrationName = contactAddress
                                            };
                                            _contactSvc.AddContact(contact, "");
                                        }

                                        IntPtr msgPtr = LinphoneAPI.linphone_chat_message_get_text(msMessagePtr.data);
                                        var messageString = string.Empty;
                                        if (msgPtr != IntPtr.Zero)
                                            messageString = LinphoneAPI.PtrToStringUtf8(msgPtr);

                                        if (messageString.NotBlank())
                                        {
                                            var localTime =
                                                Time.ConvertUtcTimeToLocalTime(
                                                    LinphoneAPI.linphone_chat_message_get_time(msMessagePtr.data));
                                            var declineMessage = false;
                                            if (messageString.StartsWith(VATRPChatMessage.DECLINE_PREFIX))
                                            {
                                                messageString = messageString.Substring(VATRPChatMessage.DECLINE_PREFIX.Length);
                                                declineMessage = true;
                                            }
                                            var chatMessage = new VATRPChatMessage(MessageContentType.Text)
                                            {
                                                Direction =
                                                    isOutgoing
                                                        ? MessageDirection.Outgoing
                                                        : MessageDirection.Incoming,
                                                IsIncompleteMessage = false,
                                                MessageTime = localTime,
                                                Content = messageString,
                                                IsRTTMessage = false,
                                                IsDeclineMessage = declineMessage,
                                                IsRead = LinphoneAPI.linphone_chat_message_is_read(msMessagePtr.data) == 1,
                                                Status = LinphoneAPI.linphone_chat_message_get_state(msMessagePtr.data),
                                                Chat = chat
                                            };

                                            chat.AddMessage(chatMessage, false);
                                            chat.UpdateLastMessage(false);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    msgListPtr = msMessagePtr.next;
                } while (msMessagePtr.next != IntPtr.Zero);

            }
        }
Exemplo n.º 8
0
 private void OnConversationUnReadStateChanged(VATRPChat chat)
 {
     if ((chat != null) && (this.ConversationUnReadStateChanged != null))
     {
         this.ConversationUnReadStateChanged(this, new ConversationEventArgs(chat));
     }
 }
Exemplo n.º 9
0
 private void OnNewConversationCreated(VATRPChat chat)
 {
     if ((chat != null) && (this.NewConversationCreated != null))
     {
         this.NewConversationCreated(this, new ConversationEventArgs(chat));
     }
 }
        public void CreateRttConversation(VATRPChat chatRoom)
        {
            if (chatRoom == null)
                return;

            if (this.Chat != null)
                _chatsManager.CloseChat(this.Chat);

            this.Chat = chatRoom;

            var contactVM = FindContactViewModel(chatRoom.Contact);
            if (contactVM == null)
            {
                contactVM = new ContactViewModel(chatRoom.Contact);
                this.Contacts.Add(contactVM);
            }

            this.ChatViewContact = contactVM;

            Chat.CharsCountInBubble = 0;
            Chat.UnreadMsgCount = 0;
            if (App.CurrentAccount != null)
            {
                Chat.MessageFont = App.CurrentAccount.RTTFontFamily;
            }

            ChatViewContact.IsSelected = true;

            this.MessagesListView = CollectionViewSource.GetDefaultView(this.Messages);
            this.MessagesListView.SortDescriptions.Add(new SortDescription("MessageTime",
                ListSortDirection.Ascending));

            OnPropertyChanged("Chat");
            try
            {
                if (MessagesListView != null && MessagesListView.SourceCollection != null)
                    MessagesListView.Refresh();
            }
            catch (Exception)
            {

            }

            UpdateMessagingView();
            #if false
            if (Chat != null)
                Chat.InsertRttWrapupMarkers(callPtr);
            #endif
        }
 public ConversationUpdatedEventArgs(VATRPChat chat) : base(chat)
 {
     this.AllowToChangeUnreadMessageCounter = true;
 }
Exemplo n.º 12
0
        public VATRPChat InsertRttChat(VATRPContact contact, IntPtr chatPtr, IntPtr callPtr)
        {
            VATRPChat chat = FindRTTChat(chatPtr);

            if (chat != null)
                return chat;

            chat = new VATRPChat(contact, callPtr + "_rtt", true) {NativePtr = chatPtr, CallPtr = callPtr};

            lock (this._chatItems)
            {
                this._chatItems.Add(chat);
            }

            return chat;
        }
Exemplo n.º 13
0
        private bool RemoveChat(VATRPChat chat)
        {
            var retValue = false;
            if (chat == null)
            {
                return retValue;
            }

            lock (this._chatItems)
            {
                retValue = this._chatItems.Remove(chat);
            }
            return retValue;
        }
 public ConversationUpdatedEventArgs(VATRPChat chat)
     : base(chat)
 {
     this.AllowToChangeUnreadMessageCounter = true;
 }
Exemplo n.º 15
0
        public VATRPChat CreateChat(VATRPContact contact, string dialogId)
        {
            if (contact == null)
            {
                return null;
            }

            VATRPChat chatRoom = new VATRPChat(contact, dialogId);
            var loggedContact = _contactSvc.FindLoggedInContact();
            if (loggedContact != null)
                chatRoom.AddContact(loggedContact);

            _chatItems.InsertToTop<VATRPChat>(chatRoom);
            this.OnNewConversationCreated(chatRoom);
            return chatRoom;
        }
Exemplo n.º 16
0
 private bool RemoveChat(VATRPChat chat)
 {
     if (chat == null)
     {
         return false;
     }
     return this._chatItems.Remove(chat);
 }
Exemplo n.º 17
0
 public void OnConversationUpdated(VATRPChat chat, bool allowUpdate)
 {
     if (chat != null)
     {
         if (this.ConversationUpdated != null)
         {
             var args = new ConversationUpdatedEventArgs(chat);
             args.AllowToChangeUnreadMessageCounter = allowUpdate;
             this.ConversationUpdated(this, args);
         }
     }
 }
Exemplo n.º 18
0
        private void SetOfflineStateToChat(VATRPChat chat)
        {
            if (chat != null)
            {
                if (chat.Contact != null)
                {
                    chat.Contact.Status = UserStatus.Offline;
                }

            }
        }
Exemplo n.º 19
0
        private VATRPChat FindChat(VATRPChat chat)
        {
            if (chat == null)
                return null;

            lock (this._chatItems)
            {
                foreach (VATRPChat chatItem in this._chatItems)
                {
                    if ((chatItem != null) && chatItem.ID == chat.ID)
                    {
                        return chatItem;
                    }
                }
            }

            return null;
        }
Exemplo n.º 20
0
 public static bool IsMessageAlreadyExistCheckByMsgID(VATRPChat chat, string msgID)
 {
     if ((chat != null) && !string.IsNullOrEmpty(msgID))
     {
         foreach (VATRPChatMessage message in chat.Messages)
         {
             if (message.ID == msgID)
             {
                 Debug.WriteLine("ChatList_Manager:IsMessageAlreadyExistCheckByMsgID: msg exist msgId = " +
                     msgID);
                 return true;
             }
         }
     }
     return false;
 }
        public void SetActiveChatContact(VATRPContact contact, IntPtr callPtr)
        {
            if (contact == null)
                return;

            if (Chat != null && Chat.Contact == contact)
            {
                Chat.Contact.UnreadMsgCount = 0;
                Chat.UnreadMsgCount = 0;
                ChangeUnreadCounter();
                _chatsManager.ActivateChat(Chat);
                return;
            }

            Console.WriteLine("SetActiveChat " + contact.Fullname);

            this._chat = _chatsManager.GetChat(contact);

            var contactVM = FindContactViewModel(contact);
            if (contactVM == null)
            {
                contactVM = new ContactViewModel(contact);
                this.Contacts.Add(contactVM);
            }

            if (_contactViewModel != null && _contactViewModel.Contact != contactVM.Contact)
            {
                _contactViewModel.IsSelected = false;
            }

            _contactViewModel = contactVM;

            if (Chat != null)
            {
                Chat.CallPtr = callPtr;

                if (Chat.Contact != null)
                {
                    Chat.Contact.UnreadMsgCount = 0;
                    this.Chat.Contact.PropertyChanged += this.Contact_PropertyChanged;
                }

                Chat.CharsCountInBubble = 0;
                Chat.UnreadMsgCount = 0;
                ChangeUnreadCounter();
                _chatsManager.ActivateChat(Chat);
                if (App.CurrentAccount != null)
                {
                    Chat.MessageFont = App.CurrentAccount.RTTFontFamily;
                }
            }

            _contactViewModel.IsSelected = true;
            IsMessagesLoaded = false;

            if ( Messages != null)
            {
                this.MessagesListView = CollectionViewSource.GetDefaultView(this.Messages);
                this.MessagesListView.SortDescriptions.Add(new SortDescription("MessageTime",
                    ListSortDirection.Ascending));
            }

            OnPropertyChanged("Chat");
            if (MessagesListView != null)
                 MessagesListView.Refresh();
            if (ConversationUpdated != null)
                ConversationUpdated(this, EventArgs.Empty);
        }
Exemplo n.º 22
0
        public void ActivateChat(VATRPChat chat)
        {
            lock (this._chatItems)
            {
                foreach (VATRPChat chatItem in this._chatItems)
                {
                    if (chatItem != null && chatItem != chat)
                    {
                        chatItem.IsSelected = false;
                    }
                }

                if (chat != null)
                    chat.IsSelected = true;
            }
        }
Exemplo n.º 23
0
        public void CreateRttConversation(VATRPChat chatRoom)
        {
            if (chatRoom == null)
            {
                return;
            }

            if (this.Chat != null)
            {
                _chatsManager.CloseChat(this.Chat);
            }

            this.Chat = chatRoom;

            var contactVM = FindContactViewModel(chatRoom.Contact);

            if (contactVM == null)
            {
                contactVM = new ContactViewModel(chatRoom.Contact);
                this.Contacts.Add(contactVM);
            }

            this.ChatViewContact = contactVM;

            Chat.CharsCountInBubble = 0;
            Chat.UnreadMsgCount     = 0;
            if (App.CurrentAccount != null)
            {
                Chat.MessageFont = App.CurrentAccount.RTTFontFamily;
            }

            if (App.CurrentAccount != null)
            {
                Chat.MessageFontSize = App.CurrentAccount.RTTFontSize;
            }
            ChatViewContact.IsSelected = true;

            this.MessagesListView = CollectionViewSource.GetDefaultView(this.Messages);
            this.MessagesListView.SortDescriptions.Add(new SortDescription("MessageTime",
                                                                           ListSortDirection.Ascending));

            OnPropertyChanged("Chat");
            try
            {
                if (MessagesListView != null && MessagesListView.SourceCollection != null)
                {
                    MessagesListView.Refresh();
                }
            }
            catch (Exception)
            {
            }

            UpdateMessagingView();
#if false
            if (Chat != null)
            {
                Chat.InsertRttWrapupMarkers(callPtr);
            }
#endif
        }
Exemplo n.º 24
0
 public void CloseChat(VATRPChat chat)
 {
     if (chat != null)
     {
         this.RemoveChat(chat);
         this.OnConversationClosed(chat);
     }
 }
Exemplo n.º 25
0
        public void SetActiveChatContact(VATRPContact contact, IntPtr callPtr)
        {
            if (contact == null)
            {
                return;
            }

            if (Chat != null && Chat.Contact == contact)
            {
                Chat.Contact.UnreadMsgCount = 0;
                Chat.UnreadMsgCount         = 0;
                ChangeUnreadCounter();
                _chatsManager.ActivateChat(Chat);
                return;
            }

            this._chat = _chatsManager.GetChat(contact, false);

            var contactVM = FindContactViewModel(contact);

            if (contactVM == null)
            {
                contactVM = new ContactViewModel(contact);
                this.Contacts.Add(contactVM);
            }

            if (ChatViewContact != null && ChatViewContact.Contact != contactVM.Contact)
            {
                ChatViewContact.IsSelected = false;
            }

            ChatViewContact = contactVM;

            if (Chat != null)
            {
                Chat.CallPtr = callPtr;

                if (Chat.Contact != null)
                {
                    Chat.Contact.UnreadMsgCount        = 0;
                    this.Chat.Contact.PropertyChanged += this.Contact_PropertyChanged;
                }

                Chat.CharsCountInBubble = 0;
                Chat.UnreadMsgCount     = 0;
                ChangeUnreadCounter();
                _chatsManager.ActivateChat(Chat);
                if (App.CurrentAccount != null)
                {
                    Chat.MessageFont = App.CurrentAccount.RTTFontFamily;
                }
            }

            ChatViewContact.IsSelected = true;
            IsMessagesLoaded           = false;

            this.MessagesListView = CollectionViewSource.GetDefaultView(this.Messages);
            this.MessagesListView.SortDescriptions.Add(new SortDescription("MessageTime",
                                                                           ListSortDirection.Ascending));

            OnPropertyChanged("Chat");
            try
            {
                if (MessagesListView != null && this.Messages != null)
                {
                    MessagesListView.Refresh();
                }
            }
            catch (Exception)
            {
            }
            if (ConversationUpdated != null)
            {
                ConversationUpdated(this, EventArgs.Empty);
            }
        }
Exemplo n.º 26
0
        public bool ComposeAndSendMessage(IntPtr callPtr, VATRPChat chat, char key, bool inCompleteMessage)
        {
            VATRPChat chatID = this.FindChat(chat);
            if ((chatID == null) || (chatID.Contact == null))
            {
                return false;
            }

            VATRPContact loggedContact = _contactSvc.FindLoggedInContact();

            var message = chat.SearchIncompleteMessage(MessageDirection.Outgoing);
            if (message == null)
            {
                if (key == '\r')
                    return false;

                message = new VATRPChatMessage(MessageContentType.Text)
                {
                    Direction = MessageDirection.Outgoing,
                    Status = LinphoneChatMessageState.LinphoneChatMessageStateIdle,
                    IsRTTMessage = true,
                    Chat = chatID
                };
                chatID.AddMessage(message, false);
            }
            else
            {
                message.MessageTime = DateTime.Now;
            }

            var rttCode = (uint) key;
            var createBubble = false;
            if (key != '\r')
            {
                var sb = new StringBuilder(message.Content);
                if (key == '\b')
                {
                    if (sb.Length > 0)
                        sb.Remove(sb.Length - 1, 1);
                }
                else
                    sb.Append(Convert.ToChar(rttCode));
                message.Content = sb.ToString();
                chat.UpdateLastMessage(false);
            }
            else
            {
                createBubble = true;
            }

            message.IsIncompleteMessage = !createBubble;

            // send message to linphone
            var chatPtr = chat.NativePtr;
            var msgPtr = message.NativePtr;
            _linphoneSvc.SendChar(rttCode, callPtr, ref chatPtr, ref msgPtr);

            chat.NativePtr = chatPtr;
            message.NativePtr = msgPtr;
            if (message.NativePtr != IntPtr.Zero)
                message.Status = _linphoneSvc.GetMessageStatus(message.NativePtr);

            if (createBubble)
            {
                // delete empty message
                if (!message.Content.NotBlank())
                    chatID.DeleteMessage(message);
            }
            this.OnConversationUpdated(chatID, true);
            return true;
        }
Exemplo n.º 27
0
        public void LoadChatRoom(VATRPChat chat)
        {
            var address = string.Format("sip:{1}@{2}",  chat.Contact.ID, preferences.ProxyHost);
            IntPtr friendAddressPtr = LinphoneAPI.linphone_core_create_address(linphoneCore, address);
            if (friendAddressPtr == IntPtr.Zero)
                return;

            IntPtr chatRoomPtr = LinphoneAPI.linphone_core_get_chat_room(linphoneCore, friendAddressPtr);
            if (chatRoomPtr == IntPtr.Zero)
                return;
            IntPtr historyListPtr = LinphoneAPI.linphone_chat_room_get_history(chatRoomPtr, 100); // load all messages

            MSList curStruct;
            do
            {
                curStruct.next = IntPtr.Zero;
                curStruct.prev = IntPtr.Zero;
                curStruct.data = IntPtr.Zero;
                curStruct = (MSList)Marshal.PtrToStructure(historyListPtr, typeof(MSList));
                if (curStruct.data != IntPtr.Zero)
                {
                    IntPtr msgPtr = LinphoneAPI.linphone_chat_message_get_text(curStruct.data);
                    var messageString = string.Empty;
                    if (msgPtr != IntPtr.Zero)
                        messageString = Marshal.PtrToStringAnsi(msgPtr);

                    if (!string.IsNullOrEmpty(messageString) && messageString.Length > 1)
                    {
                        var localTime =
                            Time.ConvertUtcTimeToLocalTime(LinphoneAPI.linphone_chat_message_get_time(curStruct.data));

                        var chatMessage = new VATRPChatMessage(MessageContentType.Text)
                        {
                            Direction =
                                LinphoneAPI.linphone_chat_message_is_outgoing(curStruct.data) == 1
                                    ? MessageDirection.Outgoing
                                    : MessageDirection.Incoming,
                            IsIncompleteMessage = false,
                            MessageTime = localTime,
                            Content = messageString,
                            IsRead = LinphoneAPI.linphone_chat_message_is_read(curStruct.data) == 1,
                            IsRTTMessage = false,
                            IsRTTStartMarker = false,
                            IsRTTEndMarker = false,
                            Chat = chat
                        };
                        chat.Messages.Add(chatMessage);
                    }
                }
                historyListPtr = curStruct.next;
            } while (curStruct.next != IntPtr.Zero);
        }
Exemplo n.º 28
0
 public TypingInChatEventArgs(VATRPChat chat, TypingAction action)
 {
     this.Chat   = chat;
     this.Action = action;
 }