|
// Jean-Christophe Magnon
// jcmag@yahoo.com
#region Copyright � 2003 The Favalias Group
/*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the
* use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim
* that you wrote the original software. If you use this software in a product,
* an acknowledgment in the product documentation is required, as shown here:
*
* Portions Copyright � 2003 The Favalias Group (http://sourceforge.net/projects/favalias).
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/
#endregion
using Favalias.DataSets;
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
namespace Favalias.Controllers
{
/// <summary>
/// Summary description for AliasesController.
/// </summary>
public class AliasesController
{
#region Fields
internal static AliasesDS _aliasesDS;
internal static string _aliasesFile;
private static readonly log4net.ILog _log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
#endregion
#region Initialization
private AliasesController()
{}
public static void Init()
{
init();
}
private static void init()
{
_aliasesDS = new AliasesDS();
readPreferences();
_aliasesDS.AcceptChanges();
}
#endregion
#region Properties
public static DataView CustomAliases
{
get
{
DataView dv = new DataView(_aliasesDS.CustomAliases);
dv.AllowDelete = true;
dv.AllowEdit = true;
dv.AllowNew = true;
dv.Sort = "Alias";
return dv;
}
}
public static DataView AddinsAliases
{
get
{
DataView dv = new DataView(_aliasesDS.AddinsAliases);
dv.AllowDelete = false;
dv.AllowEdit = true;
dv.AllowNew = false;
dv.Sort = "Alias";
return dv;
}
}
#endregion
#region Methods
/// <summary>
/// Get the command for the specified alias
/// </summary>
/// <param name="alias"></param>
/// <returns></returns>
internal static string getCommand(string alias)
{
string command = "";
AliasesDS.AddinsAliasesRow addinsRow = _aliasesDS.AddinsAliases.FindByAlias(alias);
if(addinsRow != null)
{
command = addinsRow.Command;
}
else
{
AliasesDS.CustomAliasesRow customRow = _aliasesDS.CustomAliases.FindByAlias(alias);
if(customRow != null)
{
command = customRow.Command;
}
}
return command;
}
/// <summary>
/// Deserialize aliases.xml
/// </summary>
private static void readPreferences()
{
// read the user aliases.xml and create the DataSet
_aliasesFile = FavaliasController.userPref.aliasesFile;
try
{
if(File.Exists(_aliasesFile))
_aliasesDS.ReadXml(_aliasesFile);
updateAddinsAliases();
checkErrors();
}
catch(Exception exc)
{
if(_log.IsErrorEnabled) _log.Error(MethodBase.GetCurrentMethod().Name, exc);
Debug.WriteLine(exc);
}
}
/// <summary>
/// Update data about addins
/// </summary>
private static void updateAddinsAliases()
{
AliasesDS.AddinsAliasesDataTable addinsDT = AddinsController.loadAddins();
ArrayList list = new ArrayList();
// we must use a separate list because we can't modify _aliasesDS if we use it in a foreach
foreach(AliasesDS.AddinsAliasesRow row in _aliasesDS.AddinsAliases.Rows)
{
list.Add(row.Name);
}
foreach(string name in list)
{
AliasesDS.AddinsAliasesRow[] rows = (AliasesDS.AddinsAliasesRow[])addinsDT.Select("Name = '" + name + "'");
if(rows.Length == 0)
_aliasesDS.AddinsAliases.Select("Name = '" + name + "'")[0].Delete();
else
{
AliasesDS.AddinsAliasesRow newRow = rows[0];
AliasesDS.AddinsAliasesRow existingRow = (AliasesDS.AddinsAliasesRow)_aliasesDS.AddinsAliases.Select("Name = '" + name + "'")[0];
existingRow.Category = newRow.Category;
existingRow.Command = newRow.Command;
if(!newRow.IsDescriptionNull())existingRow.Description = newRow.Description;
existingRow.Description = "";
if(!newRow.IsIconNull())existingRow.Icon = newRow.Icon;
else existingRow.Icon = "";
existingRow.Name = newRow.Name;
existingRow.ArgsNb = newRow.ArgsNb;
}
}
}
internal static void saveAliases()
{
try
{
_aliasesDS.WriteXml(_aliasesFile);
_aliasesDS.AcceptChanges();
AliasesController.Init();
}
catch(Exception exc)
{
if(_log.IsErrorEnabled) _log.Error(MethodBase.GetCurrentMethod().Name, exc);
Debug.WriteLine(exc);
}
}
private static void checkErrors()
{
foreach(AliasesDS.AddinsAliasesRow r in _aliasesDS.AddinsAliases)
{
if(_aliasesDS.CustomAliases.FindByAlias(r.Alias) != null)
r.RowError = "Change this alias";
}
}
internal static bool areAliasesValid()
{
resetErrors();
checkErrors();
return !_aliasesDS.HasErrors;
}
internal static bool aliasExists(string alias)
{
return _aliasesDS.AddinsAliases.Rows.Contains(alias);
}
internal static bool nameExists(string name)
{
return (_aliasesDS.AddinsAliases.Select("Name = '" + name + "'").Length != 0);
}
internal static void resetErrors()
{
foreach(AliasesDS.AddinsAliasesRow r in _aliasesDS.AddinsAliases)
{
r.RowError = String.Empty;
}
}
// public static void AddAlias(string Alias, string Command, string Category, string Description, bool IsConstant, int ArgsNb)
// {
// byte[] bytes = new byte[0];
// _aliasesDS.Aliases.AddAliasesRow(Alias, Command, Category, Description, IsConstant, ArgsNb);
// }
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
I am an MCSD.NET and MCT. I give a lot of Microsoft Trainings (www.bdcworld.com) in France.