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
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.
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
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
64 mBookmarksDocument
= new XmlDocument();
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.
85 mBookmarksDocument
.Load(mBookmarksFile
);
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
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
)
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
);
126 parentElt
.InsertBefore(childElt
, parentElt
.ChildNodes
[aPosition
]);
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);
142 public void DeleteBookmark(Bookmark aBookmark
)
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.
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();
168 oldTarget
.Label
= elt
.GetAttribute(aAttribute
);
171 oldTarget
.IconURL
= elt
.GetAttribute(aAttribute
);
174 oldTarget
.IsContainer
= elt
.GetAttribute(aAttribute
) == "true";
177 oldTarget
.IsOpen
= elt
.GetAttribute(aAttribute
) == "true";
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();
191 newTarget
.Label
= aValue
;
194 newTarget
.IconURL
= aValue
;
197 newTarget
.IsContainer
= aValue
== "true";
200 newTarget
.IsOpen
= aValue
== "true";
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
);
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.
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
;
272 if (elt
.GetAttribute("id") == aID
)
280 public class Bookmark
285 public String Keyword
;
286 public String Description
;
287 public String IconURL
;
288 public BookmarkType Type
;
291 public enum BookmarkType
299 PersonalToolbarFolder