📄 sqlgib.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using FirebirdSql.Data.FirebirdClient;
using System.IO;
using FirebirdSql.Data.Isql;
using System.Collections;
using GPCore.Protocols;
namespace GPCore
{
/// <summary>
/// A struct that describes the information for a specific Gib.
/// Contains Alias, GroupKey and Accounts
/// Changes made to any Gib are automatically reflected in the
/// SqlDatabase.
/// </summary>
public struct SqlGib : IIdable
{
/// <summary>
/// Gets the Unique ID of this gib.
/// This Number is not sequential and some numbers may be skipped.
/// </summary>
public int ID
{
get {
int i = -1;
object o = Sql.ExecuteScalar("Select GibID from Gibs where Alias = ?", m_alias);
i = (o == null ? -1 : (int)o);
return i;
}
}
private String m_alias;
/// <summary>
/// Gets/Sets the Alias for this Gib.
/// </summary>
public String Alias
{
get
{
return m_alias = m_alias.Trim();
}
set
{
int i = ID;
Sql.ExecuteNonQuery("update gibs set Alias = ? where GibID = ?", value, ID);
m_alias = value;
foreach (SqlAccount a in Accounts)
{
foreach (Protocol p in a.Protocols)
{
if (p is IServerBuddyList)
{
IServerBuddyList s = p as IServerBuddyList;
s.ChangeAlias(a.Username, value);
}
}
}
}
}
/// <summary>
/// Gets a List of all of the IProtocols that can be used to contact
/// this Gib through any of its Accounts.
/// </summary>
public List<Protocol> Protocols
{
get
{
List<Protocol> lst = new List<Protocol>();
foreach (SqlAccount a in Accounts)
{
lst.AddRange(a.Protocols);
}
return lst;
}
}
public int GroupID
{
get
{
object o = Sql.ExecuteScalar("select groupID from GIbs where GibID =?", ID);
return (o == null ? -1 : (int)o);
}
}
/// <summary>
/// Gets/Sets a string representing the Group this Gib belongs to.
/// </summary>
public String GroupKey
{
get
{
string group = "";
int i = ID;
object o = Sql.ExecuteScalar("select GroupID from Gibs where GibID = ?", i) ;
if (o == null || (int)o == -1)
group = "";
else
group = Sql.ExecuteScalar("select alias from groups where GroupID = ?", (int)o) as string;
if (group == null) group = "";
return group.Trim();
}
set
{
int i = GroupID;
if (string.IsNullOrEmpty(value)) value = "Default";
Sql.ExecuteNonQuery("update groups set Alias = ? where GroupID = ?", value, i);
foreach (SqlAccount a in Accounts)
{
foreach (Protocol p in a.Protocols)
{
if (p is IServerBuddyList)
{
IServerBuddyList s = p as IServerBuddyList;
s.ChangeGroup(a.Username, value);
}
}
}
}
}
/// <summary>
/// Gets/Sets the Default Account used to contact this Gib.
/// </summary>
public SqlAccount DefaultAccount
{
get
{
int i = ID;
object o = Sql.ExecuteScalar("select DefaultAccount from Gibs where GibID = ?", i);
if (o == null)
return new SqlAccount();
else
return new SqlAccount((int)o);
}
set
{
int i = ID;
Sql.ExecuteNonQuery("Update Gibs set DefaultAccount = ? where GibID = ?", value.ID, i);
}
}
/// <summary>
/// Gets the Status of the Default Account for this Gib.
/// </summary>
public string Status
{
get
{
string s = Sql.ExecuteScalar("select status from Gibs where GibID = ?", ID) as string;
return (s == null ? "Offline" : s.Trim());
}
}
/// <summary>
/// Creates a new Gib with Alias and GroupKey = "Recent Buddies".
/// If a Gib with this Alias already exists in the table a reference
/// to that Gib is created. Otherwise the Gib is added to the list.
/// </summary>
/// <param name="Alias">The alias of this gib.</param>
public SqlGib(String Alias) : this(Alias,"Recent Buddies")
{
}
/// <summary>
/// Creates a new Gib with the given Alias and GroupKey.
/// If a Gib with this Alias already exists in the table a reference
/// to that Gib is created. Otherwise the Gib is added to the list.
/// </summary>
/// <param name="Alias">The alias of this gib.</param>
/// <param name="GroupKey">The groupkey for this gib.</param>
public SqlGib(String Alias, String GroupKey)
{
m_alias = Alias;
if (ID == -1)
{
if (String.IsNullOrEmpty(GroupKey)) GroupKey = "Default";
//object o = Sql.ExecuteScalar("select GroupID from groups where Alias = ?", GroupKey);
int i = new SqlGroup(GroupKey).ID;
Sql.ExecuteNonQuery("insert into gibs (Alias, groupid) values(?, ?)", Alias, i);
Core.GibAdded.Fire(null, new SqlGibEventArgs(this));
}
}
/// <summary>
/// Accesses the Gib with the given ID.
/// </summary>
/// <param name="ID">The Unique ID of a gib in the table.</param>
public SqlGib(int ID)
{
m_alias = Sql.ExecuteScalar("select Alias from gibs where GibID = ?", ID) as string;
if (m_alias == null)
m_alias = "";
m_alias.Trim();
}
/// <summary>
/// A Gib representing a null Gib with ID -1;
/// </summary>
public static SqlGib Zero = new SqlGib(-1);
/// <summary>
/// A list of all of the Accounts that belong to this Gib.
/// </summary>
public SqlAccountList Accounts
{
get
{
SqlAccountList al = new SqlAccountList(ID);
return al;
}
}
/// <summary>
/// Returns the Alias of this Gib.
/// </summary>
/// <returns>The alias of this Gib.</returns>
public override string ToString()
{
return m_alias;
}
#region IIdable Members
public IIdable Parent
{
get { return new SqlGroup(GroupKey); }
}
public int Count
{
get
{
return this.Accounts.Count;
}
}
#endregion
#region IEnumerable<IIdable> Members
public IEnumerator<IIdable> GetEnumerator()
{
foreach (IIdable a in Accounts)
{
yield return a;
}
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
/// <summary>
/// EventArgs that only require a Gib.
/// </summary>
public class SqlGibEventArgs :EventArgs
{
private SqlGib g;
/// <summary>
/// The Gib.
/// </summary>
public SqlGib Gib
{
get { return g; }
set { g = value; }
}
/// <summary>
/// Creates a new GibEventArgs.
/// </summary>
/// <param name="item">The Gib.</param>
public SqlGibEventArgs(SqlGib item)
{
g = item;
}
}
/// <summary>
/// A static List of all of the Gibs in the Sql Database.
/// </summary>
public static class SqlGibList
{
#region IList<SqlGib> Members
/// <summary>
/// Removes the Gib with the given ID.
/// </summary>
///<param name="ID">The ID of the Gib to remove.</param>
public static void RemoveAt(int ID)
{
SqlGib g = new SqlGib(ID);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -