From: <dal...@us...> - 2007-02-09 15:01:03
|
Revision: 106 http://mp-plugins.svn.sourceforge.net/mp-plugins/?rev=106&view=rev Author: dalanorth Date: 2007-02-09 07:00:57 -0800 (Fri, 09 Feb 2007) Log Message: ----------- Added functionality to the configuration. Now possible to import IE and Firefox bookmarks, and to automatically get start pages from their settings. Modified Paths: -------------- trunk/plugins/WebBrowser/GUIWebBrowser.csproj trunk/plugins/WebBrowser/WebBrowserSetup.Designer.cs trunk/plugins/WebBrowser/WebBrowserSetup.cs trunk/plugins/WebBrowser/WebBrowserSetup.resx Added Paths: ----------- trunk/plugins/WebBrowser/MozillaFile.cs trunk/plugins/WebBrowser/MozillaNode.cs trunk/plugins/WebBrowser/Node.cs Modified: trunk/plugins/WebBrowser/GUIWebBrowser.csproj =================================================================== --- trunk/plugins/WebBrowser/GUIWebBrowser.csproj 2007-02-07 20:11:58 UTC (rev 105) +++ trunk/plugins/WebBrowser/GUIWebBrowser.csproj 2007-02-09 15:00:57 UTC (rev 106) @@ -72,6 +72,9 @@ <Compile Include="GUIFavorites.cs" /> <Compile Include="GUIWebBrowser.cs" /> <Compile Include="INIFile.cs" /> + <Compile Include="MozillaFile.cs" /> + <Compile Include="MozillaNode.cs" /> + <Compile Include="Node.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="WebBrowserControl.cs"> <SubType>UserControl</SubType> Added: trunk/plugins/WebBrowser/MozillaFile.cs =================================================================== --- trunk/plugins/WebBrowser/MozillaFile.cs (rev 0) +++ trunk/plugins/WebBrowser/MozillaFile.cs 2007-02-09 15:00:57 UTC (rev 106) @@ -0,0 +1,205 @@ +using System; +using System.Collections.Generic; +using System.Text; + +public class MozillaFile +{ + int _headerIndex = -1; + private MozillaNode _root = null; + private List<string> _lines; + protected List<string> Lines + { + get { return _lines; } + } + + public MozillaFile(string[] lines) + { + if (lines == null) + { + throw new ArgumentNullException("string[] lines"); + } + if (lines.Length == 0) + { + throw new ArgumentException("string[] lines"); + } + _lines = new List<string>(lines); + _root = MozillaNode.Create("Bookmarks"); + ResolveHeaderIndex(); + ResolveFoldersAndLinks(_root, ref _headerIndex, _lines); + } + + public MozillaNode GetRoot() + { + return _root; + } + + private bool IsBeginFolderHTMLMarkup(string line) + { + return MozillaNode.BeginFolderHTMLMarkup.IsMatch(line); + } + private bool IsEndFolderHTMLMarkup(string line) + { + return MozillaNode.EndFolderHTMLMarkup.IsMatch(line); + } + + private int ResolveHeaderIndex() + { + foreach (string line in _lines) + { + _headerIndex++; + if (IsBeginFolderHTMLMarkup(line)) + return _headerIndex; + } + throw new IndexOutOfRangeException(); + } + + private void ResolveFoldersAndLinks(MozillaNode parentNode, ref int lineNumber, List<string> lines) + { + while (lineNumber < lines.Count) + { + string line = lines[lineNumber++]; + if (IsEndFolderHTMLMarkup(line)) + break; + MozillaNode node = MozillaNode.Create(parentNode, line); + if (node.IsFolder) + ResolveFoldersAndLinks(node, ref lineNumber, lines); + } + } + + public IEnumerable<MozillaNode> FindDuplicateNodes(MozillaNode matchNode) + { + Predicate<MozillaNode> predicate = + delegate(MozillaNode current) { return current.CompareTo(matchNode) == 0; }; + foreach (MozillaNode node in this._root.FindAll(predicate)) + { + yield return node; + } + } + + public int RemoveDuplicateLinks() + { + int removed = 0; + List<MozillaNode> nodes = new List<MozillaNode>(this.GetAllLinkNodes(_root)); + foreach (MozillaNode node in nodes) + { + List<MozillaNode> copies = new List<MozillaNode>(FindDuplicateNodes(node)); + if (copies.Count > 1) + { + for (int i = 1; i < copies.Count; i++) + { + MozillaNode duplicateNode = copies[i]; + MozillaNode.DeleteRelationShip(duplicateNode.Parent, duplicateNode); + removed++; + } + } + } + return removed; + } + + public void Save(string fullyQualifiedFilename) + { + string newMozillaLinks = ""; + foreach (string s in Header) + { + newMozillaLinks += s + Environment.NewLine; + } + newMozillaLinks += "<DL><p>" + Environment.NewLine; + + FormatLines(_root, ref newMozillaLinks); + + + + System.IO.File.WriteAllText(fullyQualifiedFilename, newMozillaLinks); + } + + private IEnumerable<MozillaNode> GetAllLinkNodes(MozillaNode fromNode) + { + Predicate<MozillaNode> predicate = delegate(MozillaNode current) { return current.IsLink; }; + return fromNode.FindAll(predicate); + } + + public MozillaNode Find(MozillaNode matchNode) + { + Predicate<MozillaNode> predicate = + delegate(MozillaNode current) { return current.IsLink && current.CompareTo(matchNode) == 0; }; + foreach (MozillaNode node in _root.FindAll(predicate)) + { + return node; + } + return null; + } + + public IEnumerable<string> Header + { + get + { + foreach (string line in _lines) + { + if (IsBeginFolderHTMLMarkup(line)) + yield break; + else + yield return line; + } + } + } + + private void FormatLines(MozillaNode folder, ref string lines) + { + if (folder.Parent != null) + lines += folder.Line + "\n" + "<DL><p>" + Environment.NewLine; + foreach (MozillaNode node in folder.GetChildren()) + { + if (node.IsFolder) + { + FormatLines(node, ref lines); + } + else + lines += node.Line + Environment.NewLine; + } + lines += "</DL><p>" + Environment.NewLine; + } + + private int _numberOfNewLinksFound = 0; + public int NumberOfNewLinksFound + { + get { return _numberOfNewLinksFound; } + } + + private void FindNewNodes(MozillaNode folder, List<MozillaNode> newNodes) + { + foreach (MozillaNode child in folder.GetChildren()) + { + if (child.IsFolder) + { + FindNewNodes(child, newNodes); + } + else + { + if (Find(child) == null) + { + newNodes.Add(child); + } + } + } + } + + public void Merge(MozillaFile other) + { + List<MozillaNode> newNodes = new List<MozillaNode>(); + _numberOfNewLinksFound = 0; + FindNewNodes(other._root, newNodes); + MoveNodes(_root, newNodes); + _numberOfNewLinksFound = newNodes.Count; + } + + private static void MoveNodes(MozillaNode toNode, List<MozillaNode> nodes) + { + foreach (MozillaNode node in nodes) + { + MozillaNode.DeleteRelationShip(node.Parent, node); + MozillaNode.SetRelationShip(toNode, node); + } + } + + +} Added: trunk/plugins/WebBrowser/MozillaNode.cs =================================================================== --- trunk/plugins/WebBrowser/MozillaNode.cs (rev 0) +++ trunk/plugins/WebBrowser/MozillaNode.cs 2007-02-09 15:00:57 UTC (rev 106) @@ -0,0 +1,158 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +public enum NodeEnum { Unknown, Link, Folder } + +public class MozillaNode : Node +{ + public static void SetUrlComparison() + { + Comparison = delegate(Node x, Node y) { return string.Compare(((MozillaNode)x)._href, ((MozillaNode)y)._href); }; + } + + public static void SetNameComparison() + { + Comparison = delegate(Node x, Node y) { return string.Compare(((MozillaNode)x).Name, ((MozillaNode)y).Name); }; + } + + static MozillaNode() + { + SetNameComparison(); + } + + private NodeEnum _nodeType = NodeEnum.Unknown; + public NodeEnum NodeType { get { return _nodeType; } } + + public static readonly Regex BeginFolderHTMLMarkup = new Regex( + @"\<DL\>", + RegexOptions.IgnoreCase | + RegexOptions.Singleline); + + + public static readonly Regex FolderRegex = new Regex( + @"<DT><H3.*?(ADD_DATE=\""(.*?)\"")* ID=""(.*?)"".*?>(.*?)<\/H3>", + RegexOptions.IgnoreCase | + RegexOptions.Singleline); + + + //match a link + private static readonly Regex LinkRegex = new Regex( + @"<DT><A HREF=""(.+?)"".*?>(.*?)<\/A>", + RegexOptions.IgnoreCase | + RegexOptions.Singleline); + + + public static readonly Regex EndFolderHTMLMarkup = + new Regex(@"\<\/DL\>", + RegexOptions.IgnoreCase | + RegexOptions.Singleline); + + + + private string _name = ""; + private string _href = ""; + private string _line; + + + public static bool IsLinkLine(string line) + { + return LinkRegex.IsMatch(line); + } + + public static bool IsFolderLine(string line) + { + return FolderRegex.IsMatch(line); + } + + public bool IsFolder { get { return _nodeType == NodeEnum.Folder; } } + public bool IsLink { get { return _nodeType == NodeEnum.Link; } } + + public string Line + { + get { return _line; } + } + + public string URL + { + get + { + if (IsLink) + { + return _href; + } + else + { + return string.Empty; + } + } + } + + public static MozillaNode Create(MozillaNode parent, string originalLine) + { + + MozillaNode node = new MozillaNode(); + if (MozillaNode.IsFolderLine(originalLine) || MozillaNode.IsLinkLine(originalLine)) + { + + SetRelationShip(parent, node); + node._line = originalLine; + if (MozillaNode.IsFolderLine(originalLine)) + { + node._nodeType = NodeEnum.Folder; + Match match = FolderRegex.Match(originalLine); + node._name = match.Groups[4].Value; + } + else + { + node._nodeType = NodeEnum.Link; + Match match = LinkRegex.Match(originalLine); + node._href = match.Groups[1].Value; + node._name = match.Groups[2].Value; + if (string.IsNullOrEmpty(node._href) || string.IsNullOrEmpty(node._name)) + { + throw new ArgumentException("_href or _name not set properly"); + } + } + } + return node; + } + + + private MozillaNode() + { + + } + + + public string Name + { + get { return _name; } + } + + + public static MozillaNode Create(string name) + { + MozillaNode node = new MozillaNode(); + node._name = name; + node._href = "root"; + node._line = "root"; + return node; + } + + public override string ToString() + { + const string message = "Href [{0}] Parent [{1}]"; + return string.Format(message, (this._href ?? "<href??>"), (this.Parent == null ? "null" : "Parent")); + } + + public override bool IsValid + { + get + { + bool izNoGood = this.IsLink && string.IsNullOrEmpty(this._href); + return !izNoGood; + } + } +} Added: trunk/plugins/WebBrowser/Node.cs =================================================================== --- trunk/plugins/WebBrowser/Node.cs (rev 0) +++ trunk/plugins/WebBrowser/Node.cs 2007-02-09 15:00:57 UTC (rev 106) @@ -0,0 +1,146 @@ +using System; +using System.Collections.Generic; +using System.Text; + +public abstract class Node : IComparable<Node>, IComparable +{ + protected Node _parent = null; + private List<Node> _children = new List<Node>(); + + private static Comparison<Node> _comparison; + + protected static Comparison<Node> Comparison + { + get { return _comparison; } + set { _comparison = value; } + } + + + public Node() + { + + } + + + public Node Parent + { + get { return _parent; } + } + + private List<Node> Children + { + get { return _children; } + } + + public IEnumerable<Node> GetChildren() + { + foreach (Node node in Children) + { + yield return node; + } + } + + public void Remove(Node node) + { + if (!Children.Contains(node)) + { + const string message = "Node node was not found in child collection. Node {0}."; + throw new ArgumentOutOfRangeException(string.Format(message, ToString())); + } + if (node.HasChildren) + { + throw new ArgumentException("Cannot remove node. Children collection is not empty"); + } + Children.Remove(node); + } + + public void Add(Node node) + { + if (Children.Contains(node)) + { + throw new ArgumentOutOfRangeException("Node node was found in child collection"); + } + Children.Add(node); + } + + public static void SetRelationShip(Node parent, Node child) + { + parent.Add(child); + child._parent = parent; + } + + public static void DeleteRelationShip(Node parent, Node child) + { + parent.Remove(child); + child._parent = null; + } + + #region IComparable<Node> Members + + public int CompareTo(Node other) + { + return Comparison(this, other); + } + + #endregion + + public bool HasChildren { get { return this.Children.Count > 0; } } + + public T Find<T>(Predicate<T> predicate) where T : Node + { + return Find((T)this, predicate); + } + + private T Find<T>(T parent, Predicate<T> predicate) where T : Node + { + foreach (T node in FindAll(parent, predicate)) + { + return node; + } + return null; + } + + public static IEnumerable<T> GetAll<T>(T fromNode) where T : Node + { + foreach (Node child in fromNode.Children) + { + foreach (Node node in GetAll(child)) + { + yield return (T)node; + } + } + yield return fromNode; + } + + private static IEnumerable<T> FindAll<T>(T parent, Predicate<T> predicate) where T : Node + { + foreach (T child in parent.GetChildren()) + { + foreach (T node in FindAll(child, predicate)) + { + yield return node; + } + } + if (predicate(parent)) + { + yield return parent; + } + } + + public IEnumerable<T> FindAll<T>(Predicate<T> predicate) where T : Node + { + return FindAll((T)this, predicate); + } + + + #region IComparable Members + + public int CompareTo(object obj) + { + return this.CompareTo((Node)obj); + } + + public abstract bool IsValid { get; } + + #endregion +} \ No newline at end of file Modified: trunk/plugins/WebBrowser/WebBrowserSetup.Designer.cs =================================================================== --- trunk/plugins/WebBrowser/WebBrowserSetup.Designer.cs 2007-02-07 20:11:58 UTC (rev 105) +++ trunk/plugins/WebBrowser/WebBrowserSetup.Designer.cs 2007-02-09 15:00:57 UTC (rev 106) @@ -62,25 +62,30 @@ this.label1 = new MediaPortal.UserInterface.Controls.MPLabel(); this.HomePage = new MediaPortal.UserInterface.Controls.MPTextBox(); this.label2 = new MediaPortal.UserInterface.Controls.MPLabel(); + this.importFFbtn = new System.Windows.Forms.Button(); + this.importIEbtn = new System.Windows.Forms.Button(); + this.importBookmarkBtn = new System.Windows.Forms.Button(); this.SuspendLayout(); // // Ok // - this.Ok.Location = new System.Drawing.Point(234, 64); + this.Ok.Location = new System.Drawing.Point(315, 67); this.Ok.Name = "Ok"; this.Ok.Size = new System.Drawing.Size(75, 23); this.Ok.TabIndex = 0; this.Ok.Text = "&Ok"; + this.Ok.UseVisualStyleBackColor = true; this.Ok.Click += new System.EventHandler(this.Ok_Click); // // Cancel // this.Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; - this.Cancel.Location = new System.Drawing.Point(315, 64); + this.Cancel.Location = new System.Drawing.Point(396, 67); this.Cancel.Name = "Cancel"; this.Cancel.Size = new System.Drawing.Size(75, 23); this.Cancel.TabIndex = 1; this.Cancel.Text = "&Cancel"; + this.Cancel.UseVisualStyleBackColor = true; this.Cancel.Click += new System.EventHandler(this.Cancel_Click); // // folderBrowserDialog @@ -94,14 +99,17 @@ this.PickFavoritesFolder.Size = new System.Drawing.Size(75, 23); this.PickFavoritesFolder.TabIndex = 2; this.PickFavoritesFolder.Text = "Browse..."; + this.PickFavoritesFolder.UseVisualStyleBackColor = true; this.PickFavoritesFolder.Click += new System.EventHandler(this.PickFavoritesFolder_Click); // // FavoritesFolder // + this.FavoritesFolder.BorderColor = System.Drawing.Color.Empty; this.FavoritesFolder.Location = new System.Drawing.Point(103, 12); this.FavoritesFolder.Name = "FavoritesFolder"; this.FavoritesFolder.Size = new System.Drawing.Size(206, 20); this.FavoritesFolder.TabIndex = 3; + this.FavoritesFolder.TextChanged += new System.EventHandler(this.FavoritesFolder_TextChanged); // // label1 // @@ -114,6 +122,7 @@ // // HomePage // + this.HomePage.BorderColor = System.Drawing.Color.Empty; this.HomePage.Location = new System.Drawing.Point(103, 38); this.HomePage.Name = "HomePage"; this.HomePage.Size = new System.Drawing.Size(206, 20); @@ -128,10 +137,44 @@ this.label2.TabIndex = 5; this.label2.Text = "Home Page:"; // + // importFFbtn + // + this.importFFbtn.Location = new System.Drawing.Point(315, 38); + this.importFFbtn.Name = "importFFbtn"; + this.importFFbtn.Size = new System.Drawing.Size(75, 23); + this.importFFbtn.TabIndex = 7; + this.importFFbtn.Text = "Use Firefox\'"; + this.importFFbtn.UseVisualStyleBackColor = true; + this.importFFbtn.Click += new System.EventHandler(this.importFFbtn_Click); + // + // importIEbtn + // + this.importIEbtn.Location = new System.Drawing.Point(396, 38); + this.importIEbtn.Name = "importIEbtn"; + this.importIEbtn.Size = new System.Drawing.Size(77, 23); + this.importIEbtn.TabIndex = 8; + this.importIEbtn.Text = "Use IE\'s"; + this.importIEbtn.UseVisualStyleBackColor = true; + this.importIEbtn.Click += new System.EventHandler(this.importIEbtn_Click); + // + // importBookmarkBtn + // + this.importBookmarkBtn.Enabled = false; + this.importBookmarkBtn.Location = new System.Drawing.Point(396, 9); + this.importBookmarkBtn.Name = "importBookmarkBtn"; + this.importBookmarkBtn.Size = new System.Drawing.Size(77, 23); + this.importBookmarkBtn.TabIndex = 9; + this.importBookmarkBtn.Text = "Import..."; + this.importBookmarkBtn.UseVisualStyleBackColor = true; + this.importBookmarkBtn.Click += new System.EventHandler(this.importBookmarkBtn_Click); + // // WebBrowserSetup // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; - this.ClientSize = new System.Drawing.Size(384, 85); + this.ClientSize = new System.Drawing.Size(485, 98); + this.Controls.Add(this.importBookmarkBtn); + this.Controls.Add(this.importIEbtn); + this.Controls.Add(this.importFFbtn); this.Controls.Add(this.HomePage); this.Controls.Add(this.label2); this.Controls.Add(this.label1); @@ -159,5 +202,8 @@ private MediaPortal.UserInterface.Controls.MPLabel label1; private MediaPortal.UserInterface.Controls.MPTextBox HomePage; private MediaPortal.UserInterface.Controls.MPLabel label2; + private System.Windows.Forms.Button importFFbtn; + private System.Windows.Forms.Button importIEbtn; + private System.Windows.Forms.Button importBookmarkBtn; } } \ No newline at end of file Modified: trunk/plugins/WebBrowser/WebBrowserSetup.cs =================================================================== --- trunk/plugins/WebBrowser/WebBrowserSetup.cs 2007-02-07 20:11:58 UTC (rev 105) +++ trunk/plugins/WebBrowser/WebBrowserSetup.cs 2007-02-09 15:00:57 UTC (rev 106) @@ -24,18 +24,26 @@ #endregion using System; +using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; +using System.Net; +using System.IO; using System.Windows.Forms; using MediaPortal.GUI.Library; +using Microsoft.Win32; namespace MediaPortal.GUI.WebBrowser { public partial class WebBrowserSetup : Form, ISetupForm, IShowPlugin { + private bool doAlwaysAsked = false; + private bool doAlways = false; + private bool alwaysYes = true; + /// <summary> /// Constructor /// </summary> @@ -226,5 +234,377 @@ } } + private void importFFbtn_Click(object sender, EventArgs e) + { + try + { + RegistryKey ffKey = Registry.CurrentUser; + ffKey = ffKey.OpenSubKey(@"Software\Mozilla\Mozilla Firefox\"); + string curVersion = ffKey.GetValue("CurrentVersion").ToString(); + ffKey = ffKey.OpenSubKey(curVersion + @"\Main"); + string installDir = ffKey.GetValue("Install Directory").ToString(); + ffKey.Close(); + + string readLine; + StreamReader streamReader; + streamReader = File.OpenText(installDir + @"browserconfig.properties"); + readLine = streamReader.ReadLine(); + bool found = false; + while (readLine != null) + { + if (readLine.StartsWith("browser.startup.homepage=")) + { + found = true; + break; + } + readLine = streamReader.ReadLine(); + } + streamReader.Close(); + + if (found) + { + HomePage.Text = readLine.Substring(25); + } + else + { + MessageBox.Show("Could not import Firefox homepage"); + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } + } + + private void importIEbtn_Click(object sender, EventArgs e) + { + try + { + RegistryKey ffKey = Registry.CurrentUser; + ffKey = ffKey.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main"); + HomePage.Text = ffKey.GetValue("Start Page").ToString(); + ffKey.Close(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } + } + + private void FavoritesFolder_TextChanged(object sender, EventArgs e) + { + importBookmarkBtn.Enabled = (FavoritesFolder.Text != string.Empty); + } + + private void importBookmarkBtn_Click(object sender, EventArgs e) + { + // First of all, verify that the favorites folder is a valid folder + if (Directory.Exists(FavoritesFolder.Text)) + { + // Check if it is Windows Favorites folder + if (FavoritesFolder.Text.Equals(System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites), StringComparison.CurrentCultureIgnoreCase)) + { + DialogResult dr = MessageBox.Show(this, "You have selected Windows Favorites folder for bookmarks. Note that bookmark changes made to this directory will affect the list of favorites in your Internet Explorer browser. Continue anyway?", "Are you sure?", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); + if (dr != DialogResult.OK) + { + return; + } + } + + // OK, check if Firefox bookmarks exist + doAlways = false; + doAlwaysAsked = false; + string appData = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + appData += @"\Mozilla\Firefox\Profiles\"; + DirectoryInfo di = new DirectoryInfo(appData); + ArrayList bookmarks = new ArrayList(); + foreach (DirectoryInfo i in di.GetDirectories()) + { + if (File.Exists(i.FullName + @"\bookmarks.html")) + { + bookmarks.Add(i.FullName + @"\bookmarks.html"); + } + } + + bool cancelImport = false; + if (bookmarks.Count > 0) + { + DialogResult dr = MessageBox.Show(this, "Firefox bookmarks found. Import?", "Firefox", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); + if (dr == DialogResult.Yes) + { + // Import firefox bookmarks + for (int i = 0; i < bookmarks.Count; i++) + { + importFirefoxBookmarks((string) bookmarks[i]); + } + } + else if (dr == DialogResult.Cancel) + { + cancelImport = true; + } + } + + if (!cancelImport) + { + // Check for explorer bookmarks + if (!FavoritesFolder.Text.Equals(System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites), StringComparison.CurrentCultureIgnoreCase)) + { + DialogResult dr = MessageBox.Show(this, "Windows favorites found. Import?", "IE", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); + if (dr == DialogResult.Yes) + { + // Import IE bookmarks + doAlways = false; + doAlwaysAsked = false; + importIEBookmarks(System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites)); + } + // No or Cancel means we are done + } + else + { + // No IE bookmarks + if (bookmarks.Count == 0) + { + // No FF bookmarks + MessageBox.Show("Nothing to import!"); + return; + } + } + } + if (cancelImport) + { + MessageBox.Show("Import cancelled!"); + return; + } + MessageBox.Show("Import complete!"); + } + } + + private void importFirefoxBookmarks(string bookmarkFile) + { + string[] masterFileLines = File.ReadAllLines(bookmarkFile); + MozillaFile masterFile = new MozillaFile(masterFileLines); + MozillaNode rootNode = masterFile.GetRoot(); + if (!importFolder(string.Empty, rootNode)) + { + MessageBox.Show("Operation cancelled by user. Some links or folders may have been created already."); + } + } + + /// <summary> + /// Recursive function to import all folders and links that are connected to the input node + /// Exit condition is a node with no subfolders, only links (or no children at all) + /// </summary> + /// <param name="folder"></param> + private bool importFolder(string folderName, MozillaNode folder) + { + if (folder.HasChildren) + { + IEnumerator children = folder.GetChildren().GetEnumerator(); + while (children.MoveNext()) + { + MozillaNode child = (MozillaNode)children.Current; + if (child.IsFolder) + { + if (!importFolder(folderName + "\\" + child.Name, child)) + { + return false; + } + } + else + { + if (!importLink(folderName, child)) + { + return false; + } + } + } + } + return true; + } + + private bool importLink(string folder, MozillaNode link) + { + string bookmarkFolder = FavoritesFolder.Text; + // Create a URL file using the link + if (folder != string.Empty) + { + // Check if the folder exists, else create it + bookmarkFolder += folder; + if (!Directory.Exists(bookmarkFolder)) + { + Directory.CreateDirectory(bookmarkFolder); + } + } + return createURLFile(bookmarkFolder + "\\" + link.Name + ".url", link.Name, link.URL); + } + + private bool createURLFile(string urlFileName, string linkName, string url) + { + bool overwrite = true; + if (File.Exists(urlFileName)) + { + if (!doAlways) + { + DialogResult dr = MessageBox.Show(this, "Link to " + linkName + " already exists, overwrite?", "Already exists", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); + if (dr == DialogResult.Yes) + { + overwrite = true; + if (!doAlwaysAsked) + { + DialogResult alwaysRes = MessageBox.Show(this, "Do you always want to overwrite existing links?", "Always", MessageBoxButtons.YesNo, MessageBoxIcon.Question); + if (alwaysRes == DialogResult.Yes) + { + doAlways = true; + alwaysYes = true; + } + doAlwaysAsked = true; + } + } + else if (dr == DialogResult.No) + { + overwrite = false; + if (!doAlwaysAsked) + { + DialogResult alwaysRes = MessageBox.Show(this, "Do you always want to keep existing links?", "Always", MessageBoxButtons.YesNo, MessageBoxIcon.Question); + if (alwaysRes == DialogResult.Yes) + { + doAlways = true; + alwaysYes = false; + } + doAlwaysAsked = true; + } + } + else + { + return false; + } + } + else + { + overwrite = alwaysYes; + } + + if (overwrite) + { + try + { + File.Delete(urlFileName); + } + catch (Exception) + { + Log.Info("WebBrowser link import: Could not overwrite link: " + linkName); + return true; + } + } + else + { + return true; + } + } + + try + { + IniFile iniFile = new IniFile(urlFileName); + iniFile.IniWriteValue("InternetShortcut", "URL", url); + } + catch (Exception) + { + Log.Info("WebBrowser link import: Could not create link: " + linkName); + } + return true; + } + + private void importIEBookmarks(string favoritesPath) + { + if (!CopyFolders(favoritesPath, FavoritesFolder.Text)) + { + MessageBox.Show("Operation cancelled by user. Some links or folders may have been created already."); + } + } + + public bool CopyFolders(string source, string destination) + { + DirectoryInfo di = new DirectoryInfo(source); + + if (!CopyFiles(source, destination)) + { + return false; + } + + foreach (DirectoryInfo d in di.GetDirectories()) + { + string newDir = Path.Combine(destination, d.Name); + if (!Directory.Exists(newDir)) + Directory.CreateDirectory(newDir); + + if (!CopyFolders(d.FullName, newDir)) + { + return false; + } + } + return true; + } + + public bool CopyFiles(string source, string destination) + { + DirectoryInfo di = new DirectoryInfo(source); + FileInfo[] files = di.GetFiles(); + + foreach (FileInfo f in files) + { + string sourceFile = f.FullName; + string destFile = Path.Combine(destination, f.Name); + bool overwrite = true; + + if (File.Exists(destFile)) + { + if (!doAlways) + { + DialogResult dr = MessageBox.Show(this, "Link " + f.Name + " already exists, overwrite?", "Already exists", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); + if (dr == DialogResult.Yes) + { + overwrite = true; + if (!doAlwaysAsked) + { + DialogResult alwaysRes = MessageBox.Show(this, "Do you always want to overwrite existing links?", "Always", MessageBoxButtons.YesNo, MessageBoxIcon.Question); + if (alwaysRes == DialogResult.Yes) + { + doAlways = true; + alwaysYes = true; + } + doAlwaysAsked = true; + } + } + else if (dr == DialogResult.No) + { + overwrite = false; + if (!doAlwaysAsked) + { + DialogResult alwaysRes = MessageBox.Show(this, "Do you always want to keep existing links?", "Always", MessageBoxButtons.YesNo, MessageBoxIcon.Question); + if (alwaysRes == DialogResult.Yes) + { + doAlways = true; + alwaysYes = false; + } + doAlwaysAsked = true; + } + } + else + { + return false; + } + } + else + { + overwrite = alwaysYes; + } + } + if (overwrite) + { + File.Copy(sourceFile, destFile, overwrite); + } + } + return true; + } } } \ No newline at end of file Modified: trunk/plugins/WebBrowser/WebBrowserSetup.resx =================================================================== --- trunk/plugins/WebBrowser/WebBrowserSetup.resx 2007-02-07 20:11:58 UTC (rev 105) +++ trunk/plugins/WebBrowser/WebBrowserSetup.resx 2007-02-09 15:00:57 UTC (rev 106) @@ -117,33 +117,9 @@ <resheader name="writer"> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> </resheader> - <metadata name="Ok.Locked" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> - <value>True</value> - </metadata> - <metadata name="Cancel.Locked" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> - <value>True</value> - </metadata> <metadata name="folderBrowserDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <value>17, 17</value> </metadata> - <metadata name="PickFavoritesFolder.Locked" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> - <value>True</value> - </metadata> - <metadata name="FavoritesFolder.Locked" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> - <value>True</value> - </metadata> - <metadata name="label1.Locked" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> - <value>True</value> - </metadata> - <metadata name="HomePage.Locked" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> - <value>True</value> - </metadata> - <metadata name="label2.Locked" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> - <value>True</value> - </metadata> - <metadata name="$this.Locked" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> - <value>True</value> - </metadata> <assembly alias="System.Drawing" name="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> <value> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |