1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsString.h"
39 : #include "nsReadableUtils.h"
40 : #include "nsTArray.h"
41 : #include "nsISimpleEnumerator.h"
42 : #include "nsXPCOM.h"
43 : #include "nsSupportsPrimitives.h"
44 : #include "nsIComponentManager.h"
45 :
46 : #include "nsCommandGroup.h"
47 : #include "nsIControllerCommand.h"
48 : #include "nsCRT.h"
49 :
50 :
51 : class nsGroupsEnumerator : public nsISimpleEnumerator
52 : {
53 : public:
54 : nsGroupsEnumerator(nsHashtable& inHashTable);
55 : virtual ~nsGroupsEnumerator();
56 :
57 : NS_DECL_ISUPPORTS
58 : NS_DECL_NSISIMPLEENUMERATOR
59 :
60 : protected:
61 :
62 : static bool HashEnum(nsHashKey *aKey, void *aData, void* aClosure);
63 :
64 : nsresult Initialize();
65 :
66 : protected:
67 :
68 : nsHashtable& mHashTable;
69 : PRInt32 mIndex;
70 : char ** mGroupNames; // array of pointers to PRUnichar* in the hash table
71 : bool mInitted;
72 :
73 : };
74 :
75 : /* Implementation file */
76 0 : NS_IMPL_ISUPPORTS1(nsGroupsEnumerator, nsISimpleEnumerator)
77 :
78 0 : nsGroupsEnumerator::nsGroupsEnumerator(nsHashtable& inHashTable)
79 : : mHashTable(inHashTable)
80 : , mIndex(-1)
81 : , mGroupNames(nsnull)
82 0 : , mInitted(false)
83 : {
84 : /* member initializers and constructor code */
85 0 : }
86 :
87 0 : nsGroupsEnumerator::~nsGroupsEnumerator()
88 : {
89 0 : delete [] mGroupNames; // ok on null pointer
90 0 : }
91 :
92 : /* boolean hasMoreElements (); */
93 : NS_IMETHODIMP
94 0 : nsGroupsEnumerator::HasMoreElements(bool *_retval)
95 : {
96 0 : nsresult rv = NS_OK;
97 :
98 0 : NS_ENSURE_ARG_POINTER(_retval);
99 :
100 0 : if (!mInitted) {
101 0 : rv = Initialize();
102 0 : if (NS_FAILED(rv)) return rv;
103 : }
104 :
105 0 : *_retval = (mIndex < mHashTable.Count() - 1);
106 0 : return NS_OK;
107 : }
108 :
109 : /* nsISupports getNext (); */
110 : NS_IMETHODIMP
111 0 : nsGroupsEnumerator::GetNext(nsISupports **_retval)
112 : {
113 0 : nsresult rv = NS_OK;
114 :
115 0 : NS_ENSURE_ARG_POINTER(_retval);
116 :
117 0 : if (!mInitted) {
118 0 : rv = Initialize();
119 0 : if (NS_FAILED(rv)) return rv;
120 : }
121 :
122 0 : mIndex ++;
123 0 : if (mIndex >= mHashTable.Count())
124 0 : return NS_ERROR_FAILURE;
125 :
126 0 : char *thisGroupName = mGroupNames[mIndex];
127 :
128 0 : nsCOMPtr<nsISupportsCString> supportsString = do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
129 0 : if (NS_FAILED(rv)) return rv;
130 :
131 0 : supportsString->SetData(nsDependentCString(thisGroupName));
132 0 : return CallQueryInterface(supportsString, _retval);
133 : }
134 :
135 : /* static */
136 : /* return false to stop */
137 : bool
138 0 : nsGroupsEnumerator::HashEnum(nsHashKey *aKey, void *aData, void* aClosure)
139 : {
140 0 : nsGroupsEnumerator* groupsEnum = reinterpret_cast<nsGroupsEnumerator *>(aClosure);
141 0 : nsCStringKey* stringKey = static_cast<nsCStringKey*>(aKey);
142 :
143 0 : groupsEnum->mGroupNames[groupsEnum->mIndex] = (char*)stringKey->GetString();
144 0 : groupsEnum->mIndex ++;
145 0 : return true;
146 : }
147 :
148 : nsresult
149 0 : nsGroupsEnumerator::Initialize()
150 : {
151 0 : if (mInitted) return NS_OK;
152 :
153 0 : mGroupNames = new char*[mHashTable.Count()];
154 0 : if (!mGroupNames) return NS_ERROR_OUT_OF_MEMORY;
155 :
156 0 : mIndex = 0;
157 0 : mHashTable.Enumerate(HashEnum, (void*)this);
158 :
159 0 : mIndex = -1;
160 0 : mInitted = true;
161 0 : return NS_OK;
162 : }
163 :
164 : #if 0
165 : #pragma mark -
166 : #endif
167 :
168 : class nsNamedGroupEnumerator : public nsISimpleEnumerator
169 : {
170 : public:
171 : nsNamedGroupEnumerator(nsTArray<char*>* inArray);
172 : virtual ~nsNamedGroupEnumerator();
173 :
174 : NS_DECL_ISUPPORTS
175 : NS_DECL_NSISIMPLEENUMERATOR
176 :
177 : protected:
178 :
179 : nsTArray<char*>* mGroupArray;
180 : PRInt32 mIndex;
181 :
182 : };
183 :
184 0 : nsNamedGroupEnumerator::nsNamedGroupEnumerator(nsTArray<char*>* inArray)
185 : : mGroupArray(inArray)
186 0 : , mIndex(-1)
187 : {
188 0 : }
189 :
190 0 : nsNamedGroupEnumerator::~nsNamedGroupEnumerator()
191 : {
192 0 : }
193 :
194 0 : NS_IMPL_ISUPPORTS1(nsNamedGroupEnumerator, nsISimpleEnumerator)
195 :
196 : /* boolean hasMoreElements (); */
197 : NS_IMETHODIMP
198 0 : nsNamedGroupEnumerator::HasMoreElements(bool *_retval)
199 : {
200 0 : NS_ENSURE_ARG_POINTER(_retval);
201 :
202 0 : PRInt32 arrayLen = mGroupArray ? mGroupArray->Length() : 0;
203 0 : *_retval = (mIndex < arrayLen - 1);
204 0 : return NS_OK;
205 : }
206 :
207 : /* nsISupports getNext (); */
208 : NS_IMETHODIMP
209 0 : nsNamedGroupEnumerator::GetNext(nsISupports **_retval)
210 : {
211 0 : NS_ENSURE_ARG_POINTER(_retval);
212 :
213 0 : if (!mGroupArray)
214 0 : return NS_ERROR_FAILURE;
215 :
216 0 : mIndex ++;
217 0 : if (mIndex >= PRInt32(mGroupArray->Length()))
218 0 : return NS_ERROR_FAILURE;
219 :
220 0 : PRUnichar *thisGroupName = (PRUnichar*)mGroupArray->ElementAt(mIndex);
221 0 : NS_ASSERTION(thisGroupName, "Bad Element in mGroupArray");
222 :
223 : nsresult rv;
224 0 : nsCOMPtr<nsISupportsString> supportsString = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
225 0 : if (NS_FAILED(rv)) return rv;
226 :
227 0 : supportsString->SetData(nsDependentString(thisGroupName));
228 0 : return CallQueryInterface(supportsString, _retval);
229 : }
230 :
231 : #if 0
232 : #pragma mark -
233 : #endif
234 :
235 :
236 : /* Implementation file */
237 0 : NS_IMPL_ISUPPORTS1(nsControllerCommandGroup, nsIControllerCommandGroup)
238 :
239 0 : nsControllerCommandGroup::nsControllerCommandGroup()
240 : {
241 0 : }
242 :
243 0 : nsControllerCommandGroup::~nsControllerCommandGroup()
244 : {
245 0 : ClearGroupsHash();
246 0 : }
247 :
248 : void
249 0 : nsControllerCommandGroup::ClearGroupsHash()
250 : {
251 0 : mGroupsHash.Reset(ClearEnumerator, (void *)this);
252 0 : }
253 :
254 : #if 0
255 : #pragma mark -
256 : #endif
257 :
258 : /* void addCommandToGroup (in DOMString aCommand, in DOMString aGroup); */
259 : NS_IMETHODIMP
260 0 : nsControllerCommandGroup::AddCommandToGroup(const char * aCommand, const char *aGroup)
261 : {
262 0 : nsCStringKey groupKey(aGroup);
263 : nsTArray<char*>* commandList;
264 0 : if ((commandList = (nsTArray<char*> *)mGroupsHash.Get(&groupKey)) == nsnull)
265 : {
266 : // make this list
267 0 : commandList = new nsAutoTArray<char*, 8>;
268 0 : mGroupsHash.Put(&groupKey, (void *)commandList);
269 : }
270 : // add the command to the list. Note that we're not checking for duplicates here
271 0 : char* commandString = NS_strdup(aCommand); // we store allocated PRUnichar* in the array
272 0 : if (!commandString) return NS_ERROR_OUT_OF_MEMORY;
273 :
274 : #ifdef DEBUG
275 : char** appended =
276 : #endif
277 0 : commandList->AppendElement(commandString);
278 0 : NS_ASSERTION(appended, "Append failed");
279 :
280 0 : return NS_OK;
281 : }
282 :
283 : /* void removeCommandFromGroup (in DOMString aCommand, in DOMString aGroup); */
284 : NS_IMETHODIMP
285 0 : nsControllerCommandGroup::RemoveCommandFromGroup(const char * aCommand, const char * aGroup)
286 : {
287 0 : nsCStringKey groupKey(aGroup);
288 0 : nsTArray<char*>* commandList = (nsTArray<char*> *)mGroupsHash.Get(&groupKey);
289 0 : if (!commandList) return NS_OK; // no group
290 :
291 0 : PRUint32 numEntries = commandList->Length();
292 0 : for (PRUint32 i = 0; i < numEntries; i ++)
293 : {
294 0 : char* commandString = commandList->ElementAt(i);
295 0 : if (!nsCRT::strcmp(aCommand,commandString))
296 : {
297 0 : commandList->RemoveElementAt(i);
298 0 : nsMemory::Free(commandString);
299 0 : break;
300 : }
301 : }
302 :
303 0 : return NS_OK;
304 : }
305 :
306 : /* boolean isCommandInGroup (in DOMString aCommand, in DOMString aGroup); */
307 : NS_IMETHODIMP
308 0 : nsControllerCommandGroup::IsCommandInGroup(const char * aCommand, const char * aGroup, bool *_retval)
309 : {
310 0 : NS_ENSURE_ARG_POINTER(_retval);
311 0 : *_retval = false;
312 :
313 0 : nsCStringKey groupKey(aGroup);
314 0 : nsTArray<char*>* commandList = (nsTArray<char*> *)mGroupsHash.Get(&groupKey);
315 0 : if (!commandList) return NS_OK; // no group
316 :
317 0 : PRUint32 numEntries = commandList->Length();
318 0 : for (PRUint32 i = 0; i < numEntries; i ++)
319 : {
320 0 : char* commandString = commandList->ElementAt(i);
321 0 : if (!nsCRT::strcmp(aCommand,commandString))
322 : {
323 0 : *_retval = true;
324 0 : break;
325 : }
326 : }
327 0 : return NS_OK;
328 : }
329 :
330 : /* nsISimpleEnumerator getGroupsEnumerator (); */
331 : NS_IMETHODIMP
332 0 : nsControllerCommandGroup::GetGroupsEnumerator(nsISimpleEnumerator **_retval)
333 : {
334 0 : nsGroupsEnumerator* groupsEnum = new nsGroupsEnumerator(mGroupsHash);
335 0 : if (!groupsEnum) return NS_ERROR_OUT_OF_MEMORY;
336 :
337 0 : return groupsEnum->QueryInterface(NS_GET_IID(nsISimpleEnumerator), (void **)_retval);
338 : }
339 :
340 : /* nsISimpleEnumerator getEnumeratorForGroup (in DOMString aGroup); */
341 : NS_IMETHODIMP
342 0 : nsControllerCommandGroup::GetEnumeratorForGroup(const char * aGroup, nsISimpleEnumerator **_retval)
343 : {
344 0 : nsCStringKey groupKey(aGroup);
345 0 : nsTArray<char*>* commandList = (nsTArray<char*> *)mGroupsHash.Get(&groupKey); // may be null
346 :
347 0 : nsNamedGroupEnumerator* theGroupEnum = new nsNamedGroupEnumerator(commandList);
348 0 : if (!theGroupEnum) return NS_ERROR_OUT_OF_MEMORY;
349 :
350 0 : return theGroupEnum->QueryInterface(NS_GET_IID(nsISimpleEnumerator), (void **)_retval);
351 : }
352 :
353 : #if 0
354 : #pragma mark -
355 : #endif
356 :
357 0 : bool nsControllerCommandGroup::ClearEnumerator(nsHashKey *aKey, void *aData, void* closure)
358 : {
359 0 : nsTArray<char*>* commandList = (nsTArray<char*> *)aData;
360 0 : if (commandList)
361 : {
362 0 : PRUint32 numEntries = commandList->Length();
363 0 : for (PRUint32 i = 0; i < numEntries; i ++)
364 : {
365 0 : char* commandString = commandList->ElementAt(i);
366 0 : nsMemory::Free(commandString);
367 : }
368 :
369 0 : delete commandList;
370 : }
371 :
372 0 : return true;
373 : }
|