Import from 1.9a8 tarball
[mozilla-extra.git] / extensions / manticore / bookmarks / bookmarks.cs
blobb2671a437c2c8597e426ea1a1df1c767893902b0
1 /* -*- Mode: C#; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Manticore.
18 * The Initial Developer of the Original Code is
19 * Silverstone Interactive.
20 * Portions created by the Initial Developer are Copyright (C) 2001
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Ben Goodger <ben@netscape.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 namespace Silverstone.Manticore.Bookmarks
42 using System;
44 using System.Xml;
45 using System.IO;
47 using System.Collections;
49 using Silverstone.Manticore.Core;
50 using Silverstone.Manticore.App;
52 public class Bookmarks : IDataStore
54 private XmlDocument mBookmarksDocument;
55 private String mBookmarksFile;
56 private Queue mObservers = null;
57 private bool mBookmarksLoaded = false;
59 ///////////////////////////////////////////////////////////////////////////
60 // Bookmarks implementation
62 public Bookmarks()
64 mBookmarksDocument = new XmlDocument();
67 ~Bookmarks()
69 mBookmarksDocument.Save(mBookmarksFile);
72 public void LoadBookmarks()
74 // First of all, the user may have chosen to place the bookmark file
75 // in a specific location.
76 mBookmarksFile = ServiceManager.Preferences.GetStringPref("browser.bookmarksfile");
78 // Otherwise, look in the profile directory.
79 if (mBookmarksFile == "")
80 mBookmarksFile = FileLocator.GetManticorePath("LocalBookmarks");
82 // Support remote bookmarks by setting up an asynchronous load & parse.
83 try
85 mBookmarksDocument.Load(mBookmarksFile);
87 catch (XmlException)
89 // Something went wrong, we'll just assume a malformed or non-existant
90 // preferences file, blow it away and insert a new one. Could potentially
91 // be dangerous.
92 try
94 File.Copy(@"defaults\bookmarks.xml", mBookmarksFile, true);
96 catch (DirectoryNotFoundException)
98 String manticoreAppData = FileLocator.GetManticorePath("AppData");
99 Directory.CreateDirectory(manticoreAppData);
100 File.Copy(@"defaults\bookmarks.xml", mBookmarksFile, true);
103 mBookmarksLoaded = true;
106 public void ImportBookmarks()
108 // Import a Netscape bookmarks file.
111 public string ResolveKeyword(string aURL)
113 // XXX implement me
114 return "";
117 public String CreateBookmark(String aLabel, String aParentID, int aPosition)
119 XmlElement parentElt = GetElementById(aParentID);
121 XmlElement childElt = mBookmarksDocument.CreateElement("item");
122 childElt.SetAttribute("label", aLabel);
123 String bookmarkID = Bookmarks.GenerateID();
124 childElt.SetAttribute("id", bookmarkID);
125 if (aPosition != -1)
126 parentElt.InsertBefore(childElt, parentElt.ChildNodes[aPosition]);
127 else
128 parentElt.AppendChild(childElt);
130 CommandTarget target = new CommandTarget(aLabel, "", bookmarkID, false);
131 CommandTarget parentTarget = new CommandTarget("", "", aParentID, true);
132 IEnumerator observers = mObservers.GetEnumerator();
133 while (observers.MoveNext())
135 IDataStoreObserver idso = observers.Current as IDataStoreObserver;
136 idso.OnNodeAdded(target, parentTarget, -1);
139 return bookmarkID;
142 public void DeleteBookmark(Bookmark aBookmark)
147 /// <summary>
148 /// Retrieve the string value of a given Bookmark property. We expose this API
149 /// so as not to have to expose the internal DOM implementation of Bookmarks.
150 /// </summary>
151 /// <param name="aBookmarkID"></param>
152 /// <param name="aAttribute"></param>
153 /// <returns></returns>
154 public String GetBookmarkAttribute(String aBookmarkID, String aAttribute)
156 XmlElement elt = GetElementById(aBookmarkID);
157 return elt.GetAttribute(aAttribute);
160 public void SetBookmarkAttribute(String aBookmarkID, String aAttribute, String aValue)
162 XmlElement elt = GetElementById(aBookmarkID);
164 CommandTarget oldTarget = new CommandTarget();
165 switch (aAttribute)
167 case "label":
168 oldTarget.Label = elt.GetAttribute(aAttribute);
169 break;
170 case "icon":
171 oldTarget.IconURL = elt.GetAttribute(aAttribute);
172 break;
173 case "container":
174 oldTarget.IsContainer = elt.GetAttribute(aAttribute) == "true";
175 break;
176 case "open":
177 oldTarget.IsOpen = elt.GetAttribute(aAttribute) == "true";
178 break;
180 oldTarget.Data = aBookmarkID;
182 elt.SetAttribute(aAttribute, aValue);
184 // Enumerate the attributes presentation cares about and set the attribute on the
185 // target if it is being changed.
186 // XXX could optimize this.
187 CommandTarget newTarget = new CommandTarget();
188 switch (aAttribute)
190 case "label":
191 newTarget.Label = aValue;
192 break;
193 case "icon":
194 newTarget.IconURL = aValue;
195 break;
196 case "container":
197 newTarget.IsContainer = aValue == "true";
198 break;
199 case "open":
200 newTarget.IsOpen = aValue == "true";
201 break;
203 newTarget.Data = aBookmarkID;
205 IEnumerator observers = mObservers.GetEnumerator();
206 while (observers.MoveNext())
208 IDataStoreObserver idso = observers.Current as IDataStoreObserver;
209 idso.OnNodeChanged(oldTarget, newTarget);
213 public static String GenerateID()
215 // Generate a random ID for a bookmark item
216 Random rand = new Random();
217 return String.Format("NC:Bookmark${0:X}", rand.Next());
220 ///////////////////////////////////////////////////////////////////////////
221 // IDataStore implementation
223 public void GetElements(String aContainerID, out IEnumerator aElements)
225 XmlElement container = GetElementById(aContainerID);
226 if (container == null)
227 container = mBookmarksDocument.DocumentElement.FirstChild as XmlElement;
228 int itemCount = container.ChildNodes.Count;
229 Queue items = new Queue();
230 for (int i = 0; i < itemCount; ++i)
232 XmlElement currElt = container.ChildNodes[i] as XmlElement;
233 // If the bookmark does not have an ID, generate one and set it.
234 if (!currElt.HasAttribute("id") || currElt.GetAttribute("id") == "")
235 currElt.SetAttribute("id", Bookmarks.GenerateID());
237 CommandTarget target = new CommandTarget();
238 target.Label = currElt.GetAttribute("label");
239 target.AccessKey = currElt.GetAttribute("accesskey");
240 target.Data = currElt.GetAttribute("id");
241 target.IsContainer = currElt.HasChildNodes;
242 target.IconURL = currElt.GetAttribute("icon");
243 target.IsOpen = currElt.GetAttribute("open") == "true";
245 items.Enqueue(target);
247 aElements = items.GetEnumerator();
250 public void AddObserver(IDataStoreObserver aObserver)
252 if (mObservers == null)
253 mObservers = new Queue();
254 mObservers.Enqueue(aObserver);
257 /// <summary>
258 /// XXX This is a hack, is really bad and inefficient and should go away when we figure
259 /// out how to make XmlDocument.GetElementById to work.
260 /// </summary>
261 /// <param name="aID"></param>
262 /// <returns></returns>
263 public XmlElement GetElementById(String aID)
265 XmlNodeList list = mBookmarksDocument.GetElementsByTagName("item");
266 int count = list.Count;
267 for (int i = 0; i < count; ++i)
269 XmlElement elt = list[i] as XmlElement;
270 if (elt != null)
272 if (elt.GetAttribute("id") == aID)
273 return elt;
276 return null;
280 public class Bookmark
282 public String Name;
283 public String URL;
284 public String ID;
285 public String Keyword;
286 public String Description;
287 public String IconURL;
288 public BookmarkType Type;
291 public enum BookmarkType
293 Bookmark,
294 Folder,
295 Separator,
296 IEFavoriteFolder,
297 IEFavorite,
298 FileSystemObject,
299 PersonalToolbarFolder