Back to index

lightning-sunbird  0.9+nobinonly
DataStore.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> (Original Author)
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.Core
00041 {
00042   using System;
00043   using System.Collections;
00044 
00045   using Silverstone.Manticore.Bookmarks;
00046   
00047   public interface IDataStore 
00048   {
00049     void GetElements(String aContainerID, out IEnumerator aElements);
00050     
00051     void AddObserver(IDataStoreObserver aObserver);
00052   }
00053 
00057   public interface IDataStoreObserver
00058   {
00059     void OnNodeChanging(Object aOldNode, Object aNewNode);
00060     void OnNodeChanged(Object aOldNode, Object aNewNode);
00061 
00062     void OnNodeAdding(Object aNewNode, Object aParentNode, int aIndex);
00063     void OnNodeAdded(Object aNewNode, Object aParentNode, int aIndex);
00064 
00065     void OnNodeRemoving(Object aNodeRemoving);
00066     void OnNodeRemoved(Object aNodeRemoved);
00067   }
00068 
00069 
00070   public class CommandTarget
00071   {
00072     public CommandTarget()
00073     {
00074     }
00075     
00076     public CommandTarget(String aLabel, String aAccessKey, Object aData, bool aIsContainer)
00077     {
00078       Label = aLabel;
00079       AccessKey = aAccessKey;
00080       Data = aData;
00081       IsContainer = aIsContainer;
00082     }
00083   
00084     private String mLabel = "";
00085     private String mAccesskey = "";
00086     private String mIconURL = "";
00087     private bool mIsContainer = false;
00088     private bool mIsOpen = false;
00089     private Object mData = new Object();
00090 
00091     public String Label 
00092     {
00093       get 
00094       {
00095         return mLabel;
00096       }
00097       set 
00098       {
00099         if (mLabel != value)
00100           mLabel = value;
00101       }
00102     }
00103 
00104     public String AccessKey
00105     {
00106       get 
00107       {
00108         return mAccesskey;
00109       }
00110       set 
00111       {
00112         if (mAccesskey != value) 
00113           mAccesskey = value;
00114       }
00115     }
00116 
00117     public bool IsContainer
00118     {
00119       get 
00120       {
00121         return mIsContainer;
00122       }
00123       set 
00124       {
00125         mIsContainer = value;
00126       }
00127     }
00128 
00129     public bool IsOpen
00130     {
00131       get 
00132       {
00133         return mIsOpen;
00134       }
00135       set 
00136       {
00137         mIsOpen = value;
00138       }
00139     }
00140 
00141     public String IconURL
00142     {
00143       get 
00144       {
00145         return mIconURL;
00146       }
00147       set 
00148       {
00149         mIconURL = value;
00150       }
00151     }
00152 
00153     public Object Data 
00154     {
00155       get 
00156       {
00157         return mData;
00158       }
00159       set 
00160       {
00161         if (!mData.Equals(value))
00162           mData = value;
00163       }
00164     }
00165   }
00166 
00167   public class DataStoreRegistry
00168   {
00169     public static IDataStore GetDataStore(String aDataStore)
00170     {
00171       switch (aDataStore) 
00172       {
00173         case "Bookmarks":
00174           // Need to replace this with a call to svc mgr.
00175           return ServiceManager.Bookmarks as IDataStore;
00176       }
00177       return null;
00178     }
00179   }
00180 
00181   public class SampleDataStore : IDataStore
00182   {
00183     private String mPrefix = "Goats";
00184     private Hashtable mTargets = null;
00185     private Hashtable mObservers = null;
00186   
00187     public SampleDataStore()
00188     {
00189       mTargets = new Hashtable();
00190       SetStuff();
00191     }
00192     
00193     public void AddObserver(IDataStoreObserver aObserver)
00194     {
00195       if (mObservers == null)
00196         mObservers = new Hashtable();
00197       
00198       mObservers.Add(aObserver.GetHashCode(), aObserver);
00199     }
00200 
00201     public void DoCommand(String aCommand, String aResourceID)
00202     {
00203     }
00204     
00205     private void SetStuff()
00206     {
00207       CommandTarget tgt1 = new CommandTarget(mPrefix + "1", "1", "foopy1" as Object, false);
00208       mTargets.Add(tgt1.GetHashCode(), tgt1);
00209       CommandTarget tgt2 = new CommandTarget(mPrefix + "2", "2", "foopy2" as Object, false);
00210       mTargets.Add(tgt2.GetHashCode(), tgt2);
00211       CommandTarget tgt3 = new CommandTarget(mPrefix + "3", "3", "foopy3" as Object, false);
00212       mTargets.Add(tgt3.GetHashCode(), tgt3);
00213       CommandTarget tgt4 = new CommandTarget(mPrefix + "4", "4", "foopy4" as Object, false);
00214       mTargets.Add(tgt4.GetHashCode(), tgt4);
00215     }
00216     
00217     public void ChangePrefix(String aPrefix)
00218     {
00219       /*
00220         CommandTarget[] oldTargets = new CommandTarget[4];
00221         mTargets.Values.CopyTo(oldTargets, 0);
00222         mPrefix = aPrefix;
00223         SetStuff();
00224         IEnumerator targets = mTargets.Values.GetEnumerator();
00225         int i = 0;
00226         while (targets.MoveNext()) {
00227           IEnumerator observers = mObservers.Values.GetEnumerator();
00228           while (observers.MoveNext()) {
00229             IDataStoreObserver idso = observers.Current as IDataStoreObserver;
00230             idso.OnNodeChanged(oldTargets[i++], targets.Current as CommandTarget);
00231           }
00232         }
00233         */
00234     }
00235     
00236     public void RemoveOdd()
00237     {
00238       IEnumerator targets = mTargets.Values.GetEnumerator();
00239       while (targets.MoveNext()) 
00240       {
00241         IEnumerator observers = mObservers.Values.GetEnumerator();
00242         while (observers.MoveNext()) 
00243         {
00244           IDataStoreObserver idso = observers.Current as IDataStoreObserver;
00245           CommandTarget current = targets.Current as CommandTarget;
00246           if (Int32.Parse(current.AccessKey) % 2 != 0) 
00247           {
00248             mTargets.Remove(current.Data.GetHashCode());
00249             idso.OnNodeRemoved(targets.Current as CommandTarget);
00250           }
00251         }
00252       }
00253     }
00254     
00255     public void AddItems()
00256     {
00257       CommandTarget tgt5 = new CommandTarget(mPrefix + "5", "5", "foopy5" as Object, false);
00258       mTargets.Add(tgt5.GetHashCode(), tgt5);
00259       CommandTarget tgt6 = new CommandTarget(mPrefix + "6", "6", "foopy6" as Object, false);
00260       mTargets.Add(tgt6.GetHashCode(), tgt6);
00261     }
00262     
00263     public void GetElements(String aContainerID, out IEnumerator aElements)
00264     {
00265       switch (aContainerID) 
00266       {
00267         case "root":
00268           aElements = mTargets.Values.GetEnumerator();
00269           break;
00270         case "goats":
00271           aElements = mTargets.Values.GetEnumerator();
00272           break;
00273         default:
00274           aElements = mTargets.Values.GetEnumerator();
00275           break;
00276       }
00277     }
00278   }
00279 
00280 }