📄 contactcollection.cs
字号:
namespace Imps.Client.Core
{
using Imps.Client.Utils;
using Imps.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
public class ContactCollection : ICollection<Contact>, IEnumerable<Contact>, IEnumerable
{
private List<Contact> _col;
private IComparer<Contact> _comparer;
private Dictionary<string, Contact> _contactDicImpsSip;
private Dictionary<string, Contact> _contactDicMsisdn;
private Dictionary<string, Contact> _contactDicVodafoneMessengerId;
private ContactList _contactList;
private ContactGroupBase _ownedGroup;
private bool _sorted;
private object _syncObj;
public ContactCollection()
{
this._syncObj = new object();
this._col = new List<Contact>();
this._contactDicMsisdn = new Dictionary<string, Contact>();
this._contactDicImpsSip = new Dictionary<string, Contact>();
}
public ContactCollection(ContactGroupBase owner) : this()
{
this._ownedGroup = owner;
}
public ContactCollection(ContactList contactList) : this()
{
this._contactList = contactList;
}
public void Add(Contact item)
{
lock (this.SyncRoot)
{
if (!this.Contains(item))
{
Contact contact = this.FindContact(item.Uri.Raw);
if (contact != null)
{
this.Remove(contact);
}
this.InnerAddContact(item);
this._col.Add(item);
if (this._ownedGroup != null)
{
this._ownedGroup.InnerContactCountChanged();
}
if (item.Uri.Belongs(IicUriType.Sip))
{
if (item.Uri.Type == IicUriType.ImpsContactSip)
{
if (!this._contactDicImpsSip.ContainsKey(item.Uri.Id))
{
this._contactDicImpsSip.Add(item.Uri.Id, item);
}
}
else if ((item.Uri.Type == IicUriType.VodafoneContactSip) && !this.ContactDicVodafoneMessengerId.ContainsKey(item.Uri.Id))
{
this.ContactDicVodafoneMessengerId.Add(item.Uri.Id, item);
}
}
else if (!this._contactDicMsisdn.ContainsKey(item.Uri.Id))
{
this._contactDicMsisdn.Add(item.Uri.Id, item);
}
this.InnerDoSort();
}
}
}
public void Clear()
{
List<Contact>.Enumerator enumerator = this._col.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
Contact c = enumerator.get_Current();
this.InnerRemoveContact(c);
}
}
finally
{
enumerator.Dispose();
}
this._col.Clear();
if (this._ownedGroup != null)
{
this._ownedGroup.InnerContactCountChanged();
}
this._contactDicMsisdn.Clear();
this._contactDicImpsSip.Clear();
this.ContactDicVodafoneMessengerId.Clear();
}
private void contact_IsBlockChanged(object sender, BlockEventArgs e)
{
Contact contact = (Contact) sender;
contact.Owner.ContactList.InnerOnContactChanged(new ContactChangedEventArgs(contact, ContactEventType.BlockChanged));
}
private void contactPersonalInfo_PropertiesChanged(object sender, PropertiesChangedEventArgs e)
{
Contact owner = ((Imps.Client.Core.ContactInfo) sender).Owner;
ContactChangedEventArgs args = new ContactChangedEventArgs(owner, ContactEventType.PropertiesChanged);
args.ContactInfoPropertiesChangedEventArgs = e;
owner.Owner.ContactList.InnerOnContactChanged(args);
}
private void contactPresence_PropertiesChanged(object sender, PropertiesChangedEventArgs e)
{
Contact owner = ((ContactPresence) sender).Owner;
ContactChangedEventArgs args = new ContactChangedEventArgs(owner, ContactEventType.PresenceChanged);
args.PresencePropertiesChangedEventArgs = e;
owner.Owner.ContactList.InnerOnContactChanged(args);
}
public bool Contains(Contact item)
{
return this._col.Contains(item);
}
public Contact FindContact(IicUri uri)
{
if (this.Count == 0)
{
return null;
}
if (((uri == null) || !uri.IsValid) || !uri.IsContact)
{
return null;
}
Contact contact = null;
if (uri.Belongs(IicUriType.Sip))
{
if (uri.Type == IicUriType.ImpsContactSip)
{
if (this._contactDicImpsSip.ContainsKey(uri.Id))
{
contact = this._contactDicImpsSip.get_Item(uri.Id);
}
return contact;
}
if ((uri.Type == IicUriType.VodafoneContactSip) && this.ContactDicVodafoneMessengerId.ContainsKey(uri.Id))
{
contact = this.ContactDicVodafoneMessengerId.get_Item(uri.Id);
}
return contact;
}
return this.FindContactByMsisdn(uri.Id);
}
public Contact FindContact(string uri)
{
if (string.IsNullOrEmpty(uri))
{
return null;
}
IicUri uri2 = new IicUri(uri);
return this.FindContact(uri2);
}
public Contact FindContactByMsisdn(string msisdn)
{
if (!string.IsNullOrEmpty(msisdn) && this._contactDicMsisdn.ContainsKey(msisdn))
{
return this._contactDicMsisdn.get_Item(msisdn);
}
return null;
}
public Contact FindContactBySid(int sid)
{
if (this._contactDicImpsSip.ContainsKey(sid.ToString()))
{
return this._contactDicImpsSip.get_Item(sid.ToString());
}
return null;
}
public IEnumerator<Contact> GetEnumerator()
{
return this._col.GetEnumerator();
}
private void InnerAddContact(Contact c)
{
if (this._contactList != null)
{
c.PersonalInfo.InnerPropertiesChanged += new EventHandler<PropertiesChangedEventArgs>(this, (IntPtr) this.contactPersonalInfo_PropertiesChanged);
c.Presence.InnerPropertiesChanged += new EventHandler<PropertiesChangedEventArgs>(this, (IntPtr) this.contactPresence_PropertiesChanged);
c.IsBlockChanged += new EventHandler<BlockEventArgs>(this, (IntPtr) this.contact_IsBlockChanged);
}
if (this._ownedGroup != null)
{
c.Presence.InnerPropertiesChanged += new EventHandler<PropertiesChangedEventArgs>(this._ownedGroup, (IntPtr) this._ownedGroup.ContactPresence_PropertiesChanged);
if ((this._ownedGroup is ContactGroup) && !c.BelongToGroups.Contains(this._ownedGroup.Id))
{
c.BelongToGroups.Add(this._ownedGroup.Id);
}
}
}
private void InnerDoSort()
{
if (this._sorted)
{
if (this.SortComparer != null)
{
this._col.Sort(this.SortComparer);
}
else
{
this._col.Sort();
}
}
}
private void InnerRemoveContact(Contact c)
{
if (this._contactList != null)
{
c.PersonalInfo.InnerPropertiesChanged -= new EventHandler<PropertiesChangedEventArgs>(this, (IntPtr) this.contactPersonalInfo_PropertiesChanged);
c.Presence.InnerPropertiesChanged -= new EventHandler<PropertiesChangedEventArgs>(this, (IntPtr) this.contactPresence_PropertiesChanged);
c.IsBlockChanged -= new EventHandler<BlockEventArgs>(this, (IntPtr) this.contact_IsBlockChanged);
}
if (this._ownedGroup != null)
{
c.Presence.InnerPropertiesChanged -= new EventHandler<PropertiesChangedEventArgs>(this._ownedGroup, (IntPtr) this._ownedGroup.ContactPresence_PropertiesChanged);
c.BelongToGroups.Remove(this._ownedGroup.Id);
}
}
public bool Remove(Contact item)
{
bool flag;
object syncRoot;
Monitor.Enter(syncRoot = this.SyncRoot);
try
{
if (this.Contains(item))
{
this.InnerRemoveContact(item);
if (this._col.Remove(item))
{
if (this._ownedGroup != null)
{
this._ownedGroup.InnerContactCountChanged();
}
if (item.Uri.Belongs(IicUriType.Sip))
{
if (item.Uri.Type == IicUriType.ImpsContactSip)
{
if (this._contactDicImpsSip.ContainsKey(item.Uri.Id))
{
this._contactDicImpsSip.Remove(item.Uri.Id);
}
}
else if ((item.Uri.Type == IicUriType.VodafoneContactSip) && this.ContactDicVodafoneMessengerId.ContainsKey(item.Uri.Id))
{
this.ContactDicVodafoneMessengerId.Remove(item.Uri.Id);
}
}
else if (this._contactDicMsisdn.ContainsKey(item.Uri.Id))
{
this._contactDicMsisdn.Remove(item.Uri.Id);
}
return true;
}
}
ClientLogger.WriteGeneral(string.Format("Remove Contact {0}({1}) Failed", item.ToString(), item.Uri.Sid.ToString()));
flag = false;
}
catch (ArgumentNullException exception)
{
ClientLogger.WriteGeneral(string.Format("Remove Contact {0}({1}) Failed", item.ToString(), item.Uri.Sid.ToString()), exception.ToString());
flag = false;
}
finally
{
Monitor.Exit(syncRoot);
}
return flag;
}
void ICollection<Contact>.CopyTo(Contact[] array, int arrayIndex)
{
throw new NotSupportedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
private Dictionary<string, Contact> ContactDicVodafoneMessengerId
{
get
{
if (this._contactDicVodafoneMessengerId == null)
{
this._contactDicVodafoneMessengerId = new Dictionary<string, Contact>();
}
return this._contactDicVodafoneMessengerId;
}
}
public int Count
{
get
{
return this._col.get_Count();
}
}
public Contact this[string uri]
{
get
{
return this.FindContact(uri);
}
}
public Contact this[IicUri uri]
{
get
{
return this.FindContact(uri);
}
}
public Contact this[int index]
{
get
{
return this._col.get_Item(index);
}
}
public List<Contact> ListContacts
{
get
{
return this._col;
}
set
{
this._col = value;
}
}
public IComparer<Contact> SortComparer
{
get
{
return this._comparer;
}
set
{
this._comparer = value;
this.InnerDoSort();
}
}
public bool Sorted
{
get
{
return this._sorted;
}
set
{
if (this._sorted != value)
{
this._sorted = value;
this.InnerDoSort();
}
}
}
public object SyncRoot
{
get
{
return this._syncObj;
}
}
bool ICollection<Contact>.IsReadOnly
{
get
{
return this._col.get_IsReadOnly();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -