#region Copyright and GPL
/*************************************************************************
 * dataMagus - The open source database learning software
 * Copyright (C) 2007 by all authors
 * Authors: Monika Schwitter (monsy@gmx.ch)
 *          Jürg Zgraggen (j.zgraggen@ejaz.net)
 *          HSLU (Hochschule Luzern - Technik+Architektur)
 * 
 *     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 3 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, see <http://www.gnu.org/licenses/>.
 *************************************************************************/
#endregion
#region Using
using System;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Collections.Generic;
using System.Reflection;
using System.Drawing;
using System.Windows.Forms;
#endregion

namespace dataMagus.Forms
{
	/// <summary>
	/// About form.
	/// </summary>
	public partial class AboutForm : Form
	{
		#region Constructors / Destructors
		/// <summary>
		/// Default constructor.
		/// </summary>
		public AboutForm()
		{
			InitializeComponent();
		}
		#endregion
		
		#region Methodes
		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">EventArgs</param>
		private void CloseButtonClick(object sender, EventArgs e)
		{
			try
			{
				this.Close();
			}
			catch (Exception ex)
			{
				MessageForm.ShowDialog(ex);
			}
		}
		
		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">EventArgs</param>
		private void AboutFormLoad(object sender, EventArgs e)
		{
			try
			{
				// read version
				this.versionTextBox.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString();
				
				// read dictionary
				string rootDir = Thread.GetDomain().BaseDirectory;
				List<TreeNode> rootNds = CreateFileVersionTreeNodes(rootDir);
				foreach (TreeNode nd in rootNds)
				{
					this.versionTtree.Nodes.Add(nd);
				}
			}
			catch (Exception ex)
			{
				MessageForm.ShowDialog(ex);
			}
		}
		
		/// <summary>
		/// Create file version tree nodes.
		/// </summary>
		/// <param name="path">Directory path where to look at the files.</param>
		/// <returns>List of TreeNodes.</returns>
		private List<TreeNode> CreateFileVersionTreeNodes(string path)
		{
			List<TreeNode> result = new List<TreeNode>();
			string[] files = Directory.GetFiles(path);
			string[] dirs = Directory.GetDirectories(path);
			
			foreach (string dir in dirs)
			{
				TreeNode dirNode = new TreeNode(dir);
				List<TreeNode> dirNodes = this.CreateFileVersionTreeNodes(dir);
				foreach (TreeNode nd in dirNodes)
				{
					dirNode.Nodes.Add(nd);
				}
				result.Add(dirNode);
			}
			
			foreach (string file in files)
			{
				FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(file);
				if (null != fvi)
				{
					result.Add(new TreeNode(Path.GetFileName(file) + " : " + fvi.FileVersion));
				}
			}
			return result;
		}
		#endregion
	}
}
