Back to index

lightning-sunbird  0.9+nobinonly
bookmarks.cs
Go to the documentation of this file.
00001 /* -*- Mode: C#; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Manticore.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Silverstone Interactive.
00020  * Portions created by the Initial Developer are Copyright (C) 2001
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Ben Goodger <ben@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 namespace Silverstone.Manticore.Bookmarks
00041 {
00042   using System;
00043 
00044   using System.Xml;
00045   using System.IO;
00046 
00047   using System.Collections;
00048 
00049   using Silverstone.Manticore.Core;
00050   using Silverstone.Manticore.App;
00051   
00052   public class Bookmarks : IDataStore
00053   {
00054     private XmlDocument mBookmarksDocument;
00055     private String mBookmarksFile;
00056     private Queue mObservers = null;
00057     private bool mBookmarksLoaded = false;
00058 
00060     // Bookmarks implementation
00061 
00062     public Bookmarks()
00063     {
00064       mBookmarksDocument = new XmlDocument();
00065     }
00066 
00067     ~Bookmarks()
00068     {
00069       mBookmarksDocument.Save(mBookmarksFile);
00070     }
00071 
00072     public void LoadBookmarks()
00073     {
00074       // First of all, the user may have chosen to place the bookmark file
00075       // in a specific location.
00076       mBookmarksFile = ServiceManager.Preferences.GetStringPref("browser.bookmarksfile");
00077 
00078       // Otherwise, look in the profile directory.
00079       if (mBookmarksFile == "")
00080         mBookmarksFile = FileLocator.GetManticorePath("LocalBookmarks");
00081 
00082       // Support remote bookmarks by setting up an asynchronous load & parse. 
00083       try 
00084       {
00085         mBookmarksDocument.Load(mBookmarksFile);
00086       }
00087       catch (XmlException) 
00088       {
00089         // Something went wrong, we'll just assume a malformed or non-existant 
00090         // preferences file, blow it away and insert a new one. Could potentially
00091         // be dangerous. 
00092         try 
00093         {
00094           File.Copy(@"defaults\bookmarks.xml", mBookmarksFile, true);
00095         }
00096         catch (DirectoryNotFoundException) 
00097         {
00098           String manticoreAppData = FileLocator.GetManticorePath("AppData");
00099           Directory.CreateDirectory(manticoreAppData);
00100           File.Copy(@"defaults\bookmarks.xml", mBookmarksFile, true);
00101         }
00102       }
00103       mBookmarksLoaded = true;
00104     }
00105 
00106     public void ImportBookmarks()
00107     {
00108       // Import a Netscape bookmarks file.
00109     }
00110 
00111     public string ResolveKeyword(string aURL)
00112     {
00113       // XXX implement me
00114       return "";
00115     }
00116 
00117     public String CreateBookmark(String aLabel, String aParentID, int aPosition)
00118     {
00119       XmlElement parentElt = GetElementById(aParentID);
00120 
00121       XmlElement childElt = mBookmarksDocument.CreateElement("item");
00122       childElt.SetAttribute("label", aLabel);
00123       String bookmarkID = Bookmarks.GenerateID();
00124       childElt.SetAttribute("id", bookmarkID);
00125       if (aPosition != -1)
00126         parentElt.InsertBefore(childElt, parentElt.ChildNodes[aPosition]);
00127       else
00128         parentElt.AppendChild(childElt);
00129 
00130       CommandTarget target = new CommandTarget(aLabel, "", bookmarkID, false);
00131       CommandTarget parentTarget = new CommandTarget("", "", aParentID, true);
00132       IEnumerator observers = mObservers.GetEnumerator();
00133       while (observers.MoveNext()) 
00134       {
00135         IDataStoreObserver idso = observers.Current as IDataStoreObserver;
00136         idso.OnNodeAdded(target, parentTarget, -1);
00137       }
00138       
00139       return bookmarkID;
00140     }
00141 
00142     public void DeleteBookmark(Bookmark aBookmark)
00143     {
00144 
00145     }
00146 
00154     public String GetBookmarkAttribute(String aBookmarkID, String aAttribute)
00155     {
00156       XmlElement elt = GetElementById(aBookmarkID);
00157       return elt.GetAttribute(aAttribute);
00158     }
00159 
00160     public void SetBookmarkAttribute(String aBookmarkID, String aAttribute, String aValue)
00161     {
00162       XmlElement elt = GetElementById(aBookmarkID);
00163 
00164       CommandTarget oldTarget = new CommandTarget();
00165       switch (aAttribute) 
00166       {
00167         case "label":
00168           oldTarget.Label = elt.GetAttribute(aAttribute);
00169           break;
00170         case "icon":
00171           oldTarget.IconURL = elt.GetAttribute(aAttribute);
00172           break;
00173         case "container":
00174           oldTarget.IsContainer = elt.GetAttribute(aAttribute) == "true";
00175           break;
00176         case "open":
00177           oldTarget.IsOpen = elt.GetAttribute(aAttribute) == "true";
00178           break;
00179       }
00180       oldTarget.Data = aBookmarkID;
00181 
00182       elt.SetAttribute(aAttribute, aValue);
00183 
00184       // Enumerate the attributes presentation cares about and set the attribute on the
00185       // target if it is being changed. 
00186       // XXX could optimize this. 
00187       CommandTarget newTarget = new CommandTarget();
00188       switch (aAttribute) 
00189       {
00190         case "label":
00191           newTarget.Label = aValue;
00192           break;
00193         case "icon":
00194           newTarget.IconURL = aValue;
00195           break;
00196         case "container":
00197           newTarget.IsContainer = aValue == "true";
00198           break;
00199         case "open":
00200           newTarget.IsOpen = aValue == "true";
00201           break;
00202       }
00203       newTarget.Data = aBookmarkID;
00204 
00205       IEnumerator observers = mObservers.GetEnumerator();
00206       while (observers.MoveNext()) 
00207       {
00208         IDataStoreObserver idso = observers.Current as IDataStoreObserver;
00209         idso.OnNodeChanged(oldTarget, newTarget);
00210       }
00211     }
00212 
00213     public static String GenerateID()
00214     {
00215       // Generate a random ID for a bookmark item
00216       Random rand = new Random();
00217       return String.Format("NC:Bookmark${0:X}", rand.Next());
00218     }
00219 
00221     // IDataStore implementation
00222 
00223     public void GetElements(String aContainerID, out IEnumerator aElements)
00224     {
00225       XmlElement container = GetElementById(aContainerID);
00226       if (container == null)
00227         container = mBookmarksDocument.DocumentElement.FirstChild as XmlElement;
00228       int itemCount = container.ChildNodes.Count;
00229       Queue items = new Queue();
00230       for (int i = 0; i < itemCount; ++i) 
00231       {
00232         XmlElement currElt = container.ChildNodes[i] as XmlElement;
00233         // If the bookmark does not have an ID, generate one and set it. 
00234         if (!currElt.HasAttribute("id") || currElt.GetAttribute("id") == "")
00235           currElt.SetAttribute("id", Bookmarks.GenerateID());
00236         
00237         CommandTarget target = new CommandTarget();
00238         target.Label = currElt.GetAttribute("label");
00239         target.AccessKey = currElt.GetAttribute("accesskey");
00240         target.Data = currElt.GetAttribute("id");
00241         target.IsContainer = currElt.HasChildNodes;
00242         target.IconURL = currElt.GetAttribute("icon");
00243         target.IsOpen = currElt.GetAttribute("open") == "true";
00244         
00245         items.Enqueue(target);
00246       }
00247       aElements = items.GetEnumerator();
00248     }
00249 
00250     public void AddObserver(IDataStoreObserver aObserver)
00251     {
00252       if (mObservers == null)
00253         mObservers = new Queue();
00254       mObservers.Enqueue(aObserver);
00255     }
00256 
00263     public XmlElement GetElementById(String aID)
00264     {
00265       XmlNodeList list = mBookmarksDocument.GetElementsByTagName("item");
00266       int count = list.Count;
00267       for (int i = 0; i < count; ++i) 
00268       {
00269         XmlElement elt = list[i] as XmlElement;
00270         if (elt != null) 
00271         {
00272           if (elt.GetAttribute("id") == aID)
00273             return elt;
00274         }
00275       }
00276       return null;
00277     }
00278   }
00279 
00280   public class Bookmark
00281   {
00282     public String Name;
00283     public String URL;
00284     public String ID;
00285     public String Keyword;
00286     public String Description;
00287     public String IconURL;
00288     public BookmarkType Type;
00289   }
00290 
00291   public enum BookmarkType
00292   {
00293     Bookmark,
00294     Folder,
00295     Separator,
00296     IEFavoriteFolder,
00297     IEFavorite,
00298     FileSystemObject,
00299     PersonalToolbarFolder
00300   }
00301 }
00302