private void OnNewContactAdded(object sender, ContactEventArgs e)
        {
            //************************************************************************************************************************************
            // On New contact added in Call History and Chat.
            //************************************************************************************************************************************
            if (ServiceManager.Instance.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                ServiceManager.Instance.Dispatcher.BeginInvoke((Action)(() => this.OnNewContactAdded(sender, e)));
                return;
            }

            // search and update all items
            var contact = _contactService.FindContact(new ContactID(e.Contact));

            if (contact != null && contact.IsLinphoneContact)
            {
                lock (this.Calls)
                {
                    foreach (var call in Calls)
                    {
                        if (call.CallEvent.RemoteParty.TrimSipPrefix() == contact.ID)
                        {
                            call.UpdateContact(contact);
                            call.AllowAddContact = false;
                        }
                    }
                }
                CallsListView.Refresh();
            }
        }
 private void RemoveCallEvent(VATRPCallEvent callEvent)
 {
     lock (this.Calls)
     {
         foreach (var call in Calls)
         {
             if (call.CallEvent == callEvent)
             {
                 Calls.Remove(call);
                 if (callEvent.Status == VATRPHistoryEvent.StatusType.Missed)
                 {
                     _unseenMissedCallsCount--;
                     if (_unseenMissedCallsCount < 0)
                     {
                         _unseenMissedCallsCount = 0;
                     }
                     if (MissedCallsCountChanged != null)
                     {
                         MissedCallsCountChanged(null, EventArgs.Empty);
                     }
                 }
                 CallsListView.Refresh();
                 break;
             }
         }
     }
 }
        private void CallHistoryEventChanged(object sender, VATRP.Core.Events.VATRPCallEventArgs e)
        {
            if (ServiceManager.Instance.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                ServiceManager.Instance.Dispatcher.BeginInvoke((Action)(() => this.CallHistoryEventChanged(sender, e)));
                return;
            }

            var callEvent = sender as VATRPCallEvent;

            switch (e.historyEventType)
            {
            case HistoryEventTypes.Add:
                AddNewCallEvent(callEvent, true);
                break;

            case HistoryEventTypes.Load:
                LoadCallEvents();
                break;

            case HistoryEventTypes.Reset:
                Calls.Clear();
                _unseenMissedCallsCount = 0;
                CallsListView.Refresh();
                break;

            case HistoryEventTypes.Delete:
                RemoveCallEvent(callEvent);
                break;
            }
        }
        private void AddNewCallEvent(VATRPCallEvent callEvent, bool refreshNow = false)
        {
            if (FindCallEvent(callEvent) != null)
            {
                return;
            }


            long time_uts;
            var  lastSeenDate = ServiceManager.Instance.ConfigurationService.Get(Configuration.ConfSection.GENERAL,
                                                                                 Configuration.ConfEntry.LAST_MISSED_CALL_DATE, string.Empty);

            if (!long.TryParse(lastSeenDate, out time_uts))
            {
                time_uts = 0;
            }

            var contact = _contactService.FindContact(new ContactID(callEvent.RemoteParty.TrimSipPrefix(), IntPtr.Zero));

            lock (this.Calls)
            {
                Calls.Add(new HistoryCallEventViewModel(callEvent, contact));
                if (callEvent.Status == VATRPHistoryEvent.StatusType.Missed)
                {
                    if (callEvent.EndTime.Ticks > time_uts)
                    {
                        _unseenMissedCallsCount++;

                        if (MissedCallsCountChanged != null)
                        {
                            MissedCallsCountChanged(callEvent, EventArgs.Empty);
                        }
                    }
                }
            }

            ServiceManager.Instance.ConfigurationService.Set(Configuration.ConfSection.GENERAL,
                                                             Configuration.ConfEntry.LAST_MISSED_CALL_DATE, DateTime.UtcNow.Ticks.ToString());

            if (refreshNow)
            {
                CallsListView.Refresh();
            }
        }
        private void OnContactRemoved(object sender, ContactRemovedEventArgs e)
        {
            //*************************************************************************************************************************************************
            // When Contact removed from Contact list.
            //*************************************************************************************************************************************************
            if (ServiceManager.Instance.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                ServiceManager.Instance.Dispatcher.BeginInvoke((Action)(() => this.OnContactRemoved(sender, e)));
                return;
            }

            lock (this.Calls)
            {
                foreach (var call in Calls)
                {
                    if (call.CallEvent.Contact != null && call.CallEvent.Contact.ID == e.contactId.ID)
                    {
                        call.UpdateContact(null);
                        call.AllowAddContact = true;
                    }
                }
            }
            CallsListView.Refresh();
        }