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