// Bookmarks.cs
//
//  Copyright (C) 2009 Christian Eide
//
// 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.Xml;
using System.Collections;
using System.Collections.Generic;

namespace bareFTP.Preferences.Bookmarks
{
	
	
	public class Bookmarks
	{
		BookmarkFolder rootItem;
		 
		public Bookmarks()
		{
			string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "bareftp/bookmarks.xml");
			if(System.IO.File.Exists(path))
			{
				XmlDocument bookmarks = new XmlDocument();
		 		bookmarks.Load(path);
				XmlNode blist = bookmarks.SelectSingleNode("/bookmarks");
			
				rootItem = new BookmarkFolder(string.Empty);
				Traverser((XmlElement)blist, rootItem);
			}
		}

		private void Traverser(XmlElement element, BookmarkFolder folder)
		{
			IEnumerator iter = element.ChildNodes.GetEnumerator();
			while(iter.MoveNext())
			{
				try
				{
					XmlNode node = iter.Current as XmlNode;
					if(node.Name == "folder")
					{
						XmlElement el = node as XmlElement;
						BookmarkFolder nf = new BookmarkFolder(el.GetAttribute("name"));
						folder.Items.Add(nf);
						if(el.HasChildNodes)
						{
							Traverser(el, nf);
						}
					}
					else if(node.Name == "bookmark")
					{
						XmlElement el = node as XmlElement;
						BookmarkEntry entry = new BookmarkEntry(el.GetAttribute("name"));
						entry.Host = el.SelectSingleNode("host").InnerText;
						entry.Port = el.SelectSingleNode("port").InnerText;
						entry.User = el.SelectSingleNode("user").InnerText;
						entry.Pass = el.SelectSingleNode("password").InnerText;
						entry.Protocol = Convert.ToInt32(el.SelectSingleNode("protocol").InnerText);
						entry.RemotePath = el.SelectSingleNode("remotepath").InnerText;
						entry.LocalPath = el.SelectSingleNode("localpath").InnerText;
						entry.CharSet = el.SelectSingleNode("charset").InnerText;
						entry.ShowHidden = bool.Parse(el.SelectSingleNode("showhidden").InnerText);
						entry.Passive = bool.Parse(el.SelectSingleNode("passive").InnerText);
						entry.EncryptData = el.SelectSingleNode("encryptdata").InnerText;
						folder.Items.Add(entry);
				}
				}
				catch(Exception ex)
				{
					Console.WriteLine("Bookmark parse error: " + ex.Message);
				}
				
			}
		}

		public void Save(XmlDocument doc)
		{
			string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "bareftp/bookmarks.xml");
			if(!System.IO.Directory.Exists(path))
			{
				System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path));
			}
			System.IO.StreamWriter sw = new System.IO.StreamWriter(path, false, new System.Text.UTF8Encoding(false));
			doc.Save(sw);
			sw.Close();
			
			// Set strict permissions on file and directory since we store passwords in clear text
			Mono.Unix.UnixFileSystemInfo ufsd = Mono.Unix.UnixFileSystemInfo.GetFileSystemEntry(System.IO.Path.GetDirectoryName(path));
			ufsd.FileAccessPermissions = (Mono.Unix.FileAccessPermissions)((7 << 6) | 0 | 0);
			
			Mono.Unix.UnixFileSystemInfo ufsf = Mono.Unix.UnixFileSystemInfo.GetFileSystemEntry(path);
			ufsf.FileAccessPermissions = (Mono.Unix.FileAccessPermissions)((6 << 6) | 0 | 0);
		}
		
		public BookmarkItem RootItem {
		 	get {
		 		return rootItem;
		 	}
		}
	}
}
