/*
* Copyright (C) 2004-2005 Jonathan Bindel
* Copyright (C) 2006-2007 Eskil Bylund
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using DCSharp.Backend.Connections;
using DCSharp.Backend.Objects;
namespace DCSharp.Backend.Managers{
public class UserManager : IEnumerable<Identity>
{
public event EventHandler<UserEventArgs> UserAdded;
public event EventHandler<UserEventArgs> UserRemoved;
public event EventHandler<UserEventArgs> UserChanged;
private Dictionary<User, Identity> users;
public UserManager()
{
users = new Dictionary<User, Identity>();
}
#region Properties
public int Count
{
get { return users.Count; }
}
public Identity this[User user]
{
get
{
lock (users)
{
return users.ContainsKey(user) ? users[user] : null;
}
}
}
public object SyncRoot
{
get { return users; }
}
#endregion
#region Methods
public void Add(Identity identity)
{
if (identity == null)
{
throw new ArgumentNullException("identity");
}
lock (users)
{
if (!users.ContainsKey(identity.User))
{
users.Add(identity.User, identity);
identity.User.AddIdentity(identity);
OnUserAdded(identity);
}
}
}
public void Remove(Identity identity)
{
if (identity == null)
{
throw new ArgumentNullException("identity");
}
lock (users)
{
if (users.ContainsKey(identity.User))
{
users.Remove(identity.User);
identity.User.RemoveIdentity(identity);
OnUserRemoved(identity);
}
}
}
public void Clear()
{
lock (users)
{
User[] userArray = new User[users.Keys.Count];
users.Keys.CopyTo(userArray, 0);
foreach (User user in userArray)
{
Remove(this[user]);
}
}
}
public bool Contains(Identity user)
{
return users.ContainsValue(user);
}
public Identity Find(Predicate<Identity> match)
{
if (match == null)
{
throw new ArgumentNullException("match");
}
lock (users)
{
foreach (Identity user in users.Values)
{
if (match(user))
{
return user;
}
}
}
return null;
}
public void ForEach(Action<Identity> action)
{
if (action == null)
{
throw new ArgumentNullException("action");
}
lock (users)
{
foreach (Identity user in users.Values)
{
action(user);
}
}
}
public IEnumerator<Identity> GetEnumerator()
{
return users.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
protected virtual void OnUserAdded(Identity user)
{
if (UserAdded != null)
{
UserAdded(this, new UserEventArgs(user));
}
}
protected virtual void OnUserRemoved(Identity user)
{
if (UserRemoved != null)
{
UserRemoved(this, new UserEventArgs(user));
}
}
protected void OnUserChanged(Identity user)
{
if (UserChanged != null)
{
UserChanged(this, new UserEventArgs(user));
}
}
internal void EmitUserChanged(Identity user)
{
OnUserChanged(user);
}
#endregion
}
}
|