/*
* Copyright (C) 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.Generic;
namespace DCSharp{
public class UserEventArgs : EventArgs
{
public UserEventArgs(Identity identity)
{
this.identity = identity;
}
public Identity User
{
get { return identity; }
}
private Identity identity;
}
/// <summary>
/// The identity of a user on a hub.
/// </summary>
/// <remarks>
/// User information not available as properties can be handled by using the
/// methods of this class.
/// </remarks>
public class Identity : ICloneable
{
private Dictionary<string, string> parameters;
public Identity(User user) : this(user.Uid, user.Nick)
{
}
public Identity(User user, string nick) : this(user.Uid, nick)
{
}
public Identity(Uid cid, string nick) : this()
{
if (cid == null)
{
throw new ArgumentNullException("cid");
}
if (nick == null)
{
throw new ArgumentNullException("nick");
}
this.cid = cid;
this.nick = nick;
}
private Identity()
{
parameters = new Dictionary<string, string>();
}
#region Properties
/// <summary>
/// Gets the Cid of the user.
/// </summary>
/// <value>The Cid of the user</value>
public Uid Cid
{
get { return cid; }
set { cid = value; }
}
private Uid cid;
/// <summary>
/// Gets the user this identity is about.
/// </summary>
/// <value>The user this identity is about.</value>
public User User
{
get
{
if (user == null || user.Uid != cid)
{
user = User.Create(cid, nick);
}
return user;
}
}
private User user;
/// <summary>
/// Gets or sets the nickname of the user.
/// </summary>
/// <value>The nickname of the user.</value>
public string Nick
{
get { return nick; }
set { nick = value; }
}
private string nick;
/// <summary>
/// Gets or sets the user description.
/// </summary>
/// <value>The user description.</value>
public string Description
{
get { return description; }
set { description = value; }
}
private string description;
/// <summary>
/// Gets or sets the e-mail address of the user.
/// </summary>
/// <value>The e-mail address of the user.</value>
public string Email
{
get { return email; }
set { email = value; }
}
private string email;
/// <summary>
/// Gets or sets the connection of the user.
/// </summary>
/// <value>The connection of the user</value>
public string Connection
{
get { return connection; }
set { connection = value; }
}
private string connection;
/// <summary>
/// Gets or sets whether or not the user is active.
/// </summary>
/// <value>Whether or not the user is active.</value>
public bool Active
{
get { return active; }
set { active = value; }
}
private bool active;
/// <summary>
/// Gets or sets whether or not the user is an operator.
/// </summary>
/// <value>Whether or not the user is an operator.</value>
public bool Op
{
get { return isOperator; }
set { isOperator = value; }
}
private bool isOperator;
/// <summary>
/// Gets or sets the number of bytes shared by the user.
/// </summary>
/// <value>The number of bytes shared by the user.</value>
public long ShareSize
{
get { return shareSize; }
set { shareSize = value; }
}
private long shareSize;
#endregion
#region Methods
/// <summary>
/// Retrives a user parameter by the parameter name.
/// </summary>
/// <param name="name">The name of the parameter.</param>
/// <returns>The value of the parameter, if found; otherwise, null.</returns>
public string GetParameter(string name)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
lock (parameters)
{
string value;
if (parameters.TryGetValue(name, out value))
{
return value;
}
return null;
}
}
/// <summary>
/// Sets a user parameter by the parameter name.
/// </summary>
/// <param name="name">The name of the parameter.</param>
/// <param name="name">The value of the parameter.</param>
public void SetParameter(string name, string value)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
lock (parameters)
{
parameters[name] = value;
}
}
/// <summary>
/// Removes a user parameter.
/// </summary>
/// <param name="name">The parameter to remove.</param>
public void RemoveParameter(string name)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
lock (parameters)
{
parameters.Remove(name);
}
}
public object Clone()
{
return this.MemberwiseClone();
}
public override string ToString()
{
return String.Format("{0} ({1})", nick, cid);
}
#endregion
}
}
|