Import from 1.9a8 tarball
[mozilla-extra.git] / extensions / manticore / core / DataStore.cs
blob8aeca6a795b105629ac5bd71bdfba46c4dd63db7
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> (Original Author)
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.Core
42 using System;
43 using System.Collections;
45 using Silverstone.Manticore.Bookmarks;
47 public interface IDataStore
49 void GetElements(String aContainerID, out IEnumerator aElements);
51 void AddObserver(IDataStoreObserver aObserver);
54 /// <summary>
55 /// Provides a means for a client to observe changes to a data store.
56 /// </summary>
57 public interface IDataStoreObserver
59 void OnNodeChanging(Object aOldNode, Object aNewNode);
60 void OnNodeChanged(Object aOldNode, Object aNewNode);
62 void OnNodeAdding(Object aNewNode, Object aParentNode, int aIndex);
63 void OnNodeAdded(Object aNewNode, Object aParentNode, int aIndex);
65 void OnNodeRemoving(Object aNodeRemoving);
66 void OnNodeRemoved(Object aNodeRemoved);
70 public class CommandTarget
72 public CommandTarget()
76 public CommandTarget(String aLabel, String aAccessKey, Object aData, bool aIsContainer)
78 Label = aLabel;
79 AccessKey = aAccessKey;
80 Data = aData;
81 IsContainer = aIsContainer;
84 private String mLabel = "";
85 private String mAccesskey = "";
86 private String mIconURL = "";
87 private bool mIsContainer = false;
88 private bool mIsOpen = false;
89 private Object mData = new Object();
91 public String Label
93 get
95 return mLabel;
97 set
99 if (mLabel != value)
100 mLabel = value;
104 public String AccessKey
106 get
108 return mAccesskey;
110 set
112 if (mAccesskey != value)
113 mAccesskey = value;
117 public bool IsContainer
119 get
121 return mIsContainer;
123 set
125 mIsContainer = value;
129 public bool IsOpen
131 get
133 return mIsOpen;
135 set
137 mIsOpen = value;
141 public String IconURL
143 get
145 return mIconURL;
147 set
149 mIconURL = value;
153 public Object Data
155 get
157 return mData;
159 set
161 if (!mData.Equals(value))
162 mData = value;
167 public class DataStoreRegistry
169 public static IDataStore GetDataStore(String aDataStore)
171 switch (aDataStore)
173 case "Bookmarks":
174 // Need to replace this with a call to svc mgr.
175 return ServiceManager.Bookmarks as IDataStore;
177 return null;
181 public class SampleDataStore : IDataStore
183 private String mPrefix = "Goats";
184 private Hashtable mTargets = null;
185 private Hashtable mObservers = null;
187 public SampleDataStore()
189 mTargets = new Hashtable();
190 SetStuff();
193 public void AddObserver(IDataStoreObserver aObserver)
195 if (mObservers == null)
196 mObservers = new Hashtable();
198 mObservers.Add(aObserver.GetHashCode(), aObserver);
201 public void DoCommand(String aCommand, String aResourceID)
205 private void SetStuff()
207 CommandTarget tgt1 = new CommandTarget(mPrefix + "1", "1", "foopy1" as Object, false);
208 mTargets.Add(tgt1.GetHashCode(), tgt1);
209 CommandTarget tgt2 = new CommandTarget(mPrefix + "2", "2", "foopy2" as Object, false);
210 mTargets.Add(tgt2.GetHashCode(), tgt2);
211 CommandTarget tgt3 = new CommandTarget(mPrefix + "3", "3", "foopy3" as Object, false);
212 mTargets.Add(tgt3.GetHashCode(), tgt3);
213 CommandTarget tgt4 = new CommandTarget(mPrefix + "4", "4", "foopy4" as Object, false);
214 mTargets.Add(tgt4.GetHashCode(), tgt4);
217 public void ChangePrefix(String aPrefix)
220 CommandTarget[] oldTargets = new CommandTarget[4];
221 mTargets.Values.CopyTo(oldTargets, 0);
222 mPrefix = aPrefix;
223 SetStuff();
224 IEnumerator targets = mTargets.Values.GetEnumerator();
225 int i = 0;
226 while (targets.MoveNext()) {
227 IEnumerator observers = mObservers.Values.GetEnumerator();
228 while (observers.MoveNext()) {
229 IDataStoreObserver idso = observers.Current as IDataStoreObserver;
230 idso.OnNodeChanged(oldTargets[i++], targets.Current as CommandTarget);
236 public void RemoveOdd()
238 IEnumerator targets = mTargets.Values.GetEnumerator();
239 while (targets.MoveNext())
241 IEnumerator observers = mObservers.Values.GetEnumerator();
242 while (observers.MoveNext())
244 IDataStoreObserver idso = observers.Current as IDataStoreObserver;
245 CommandTarget current = targets.Current as CommandTarget;
246 if (Int32.Parse(current.AccessKey) % 2 != 0)
248 mTargets.Remove(current.Data.GetHashCode());
249 idso.OnNodeRemoved(targets.Current as CommandTarget);
255 public void AddItems()
257 CommandTarget tgt5 = new CommandTarget(mPrefix + "5", "5", "foopy5" as Object, false);
258 mTargets.Add(tgt5.GetHashCode(), tgt5);
259 CommandTarget tgt6 = new CommandTarget(mPrefix + "6", "6", "foopy6" as Object, false);
260 mTargets.Add(tgt6.GetHashCode(), tgt6);
263 public void GetElements(String aContainerID, out IEnumerator aElements)
265 switch (aContainerID)
267 case "root":
268 aElements = mTargets.Values.GetEnumerator();
269 break;
270 case "goats":
271 aElements = mTargets.Values.GetEnumerator();
272 break;
273 default:
274 aElements = mTargets.Values.GetEnumerator();
275 break;