// 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.Controllers;
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Xsl;
namespace Favalias.Forms
{
/// <summary>
/// To add a new favorite.
/// </summary>
public class FormAddFavorite : System.Windows.Forms.Form
{
#region Fields
private string _url;
private string _favoritesFile;
private FormFavalias _formParent;
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.ImageList imageList1;
private System.ComponentModel.IContainer components;
#endregion
#region Initialization
private FormAddFavorite()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
public FormAddFavorite(FormFavalias parent, string url, string favoritesFile) : this()
{
_formParent = parent;
_url = url;
_favoritesFile = favoritesFile;
textBox2.Text = _url;
loadCategories();
}
#endregion
#region IDisposable
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#endregion
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormAddFavorite));
this.treeView1 = new System.Windows.Forms.TreeView();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.btnAdd = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.SuspendLayout();
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(8, 32);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(184, 232);
this.treeView1.TabIndex = 0;
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(176, 23);
this.label1.TabIndex = 1;
this.label1.Text = "Select a category :";
//
// label2
//
this.label2.Location = new System.Drawing.Point(208, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(152, 23);
this.label2.TabIndex = 2;
this.label2.Text = "Favorite\'s name :";
//
// textBox1
//
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox1.Location = new System.Drawing.Point(208, 64);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(312, 20);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
//
// label3
//
this.label3.Location = new System.Drawing.Point(208, 128);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(152, 23);
this.label3.TabIndex = 4;
this.label3.Text = "URL :";
//
// textBox2
//
this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox2.Location = new System.Drawing.Point(208, 152);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(312, 20);
this.textBox2.TabIndex = 5;
this.textBox2.Text = "";
//
// btnAdd
//
this.btnAdd.BackColor = System.Drawing.Color.OldLace;
this.btnAdd.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnAdd.Location = new System.Drawing.Point(352, 240);
this.btnAdd.Name = "btnAdd";
this.btnAdd.TabIndex = 6;
this.btnAdd.Text = "Add";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnCancel
//
this.btnCancel.BackColor = System.Drawing.Color.OldLace;
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnCancel.Location = new System.Drawing.Point(440, 240);
this.btnCancel.Name = "btnCancel";
this.btnCancel.TabIndex = 7;
this.btnCancel.Text = "Cancel";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// FormAddFavorite
//
this.AcceptButton = this.btnAdd;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.Gainsboro;
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(528, 269);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.treeView1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "FormAddFavorite";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Add a Favorite";
this.TopMost = true;
this.ResumeLayout(false);
}
#endregion
#region Methods
private void loadCategories()
{
try
{
TreeNode rootNode = new TreeNode("Favorites", 0, 0);
treeView1.Nodes.Add(rootNode);
// Sort the xml file :
XslTransform xslt = new XslTransform();
xslt.Load(FavaliasController.appFolder + @"\SortFavoritesAsc.xsl");
MemoryStream memStream = new MemoryStream(100);
XmlDocument doc = new XmlDocument();
doc.Load(_favoritesFile);
xslt.Transform(doc, null, memStream, null);
memStream.Seek(0, SeekOrigin.Begin);
doc = new XmlDocument();
doc.Load(memStream);
XmlNodeList categories = doc.SelectNodes("favorites/category");
// Generate the treeview items
foreach(XmlNode node in categories)
{
TreeNode item = new TreeNode(node.Attributes["name"].Value, 0, 0);
rootNode.Nodes.Add(item);
addCategory(node, item);
}
}
catch(Exception exc)
{
Debug.WriteLine(exc);
if(log.IsErrorEnabled) log.Error(System.Reflection.MethodBase.GetCurrentMethod().Name, exc);
}
}
/// <summary>
/// Recursive method to add categories
/// </summary>
/// <param name="node"></param>
/// <param name="item"></param>
private void addCategory(XmlNode node, TreeNode item)
{
XmlNodeList categories = node.SelectNodes("category");
foreach(XmlNode xnode in categories)
{
TreeNode xitem = new TreeNode(xnode.Attributes["name"].Value, 0, 0);
item.Nodes.Add(xitem);
addCategory(xnode, xitem);
}
}
private void addFavorite()
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(_favoritesFile);
XmlNode category = doc.SelectSingleNode("//category[@name=\"" + treeView1.SelectedNode.Text + "\"]");
XmlElement newFav = doc.CreateElement(null, "favorite", "");
newFav.SetAttribute("name", "", textBox1.Text);
newFav.InnerText = textBox2.Text;
category.AppendChild(newFav);
doc.Save(_favoritesFile);
}
catch(Exception exc)
{
MessageBox.Show("Error during modification of : " + _favoritesFile + ". (" + exc.Message + ")", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
if(log.IsErrorEnabled) log.Error(System.Reflection.MethodBase.GetCurrentMethod().Name, exc);
}
}
#endregion
#region Controls Event Handlers
private void btnAdd_Click(object sender, System.EventArgs e)
{
if(treeView1.SelectedNode != null && treeView1.SelectedNode.Text != "Favorites")
{
addFavorite();
this.Close();
_formParent.loadFavorites();
}
else
{
MessageBox.Show("Choose a category", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
this.Close();
}
#endregion
}
}