1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "nsReadableUtils.h"
10 #include "nsISimpleEnumerator.h"
12 #include "nsSupportsPrimitives.h"
13 #include "nsIComponentManager.h"
14 #include "nsCommandGroup.h"
15 #include "nsIControllerCommand.h"
18 class nsGroupsEnumerator
: public nsISimpleEnumerator
21 explicit nsGroupsEnumerator(
22 nsControllerCommandGroup::GroupsHashtable
& aInHashTable
);
25 NS_DECL_NSISIMPLEENUMERATOR
28 virtual ~nsGroupsEnumerator();
30 nsresult
Initialize();
33 nsControllerCommandGroup::GroupsHashtable
& mHashTable
;
35 const char** mGroupNames
; // array of pointers to char16_t* in the hash table
39 /* Implementation file */
40 NS_IMPL_ISUPPORTS(nsGroupsEnumerator
, nsISimpleEnumerator
)
42 nsGroupsEnumerator::nsGroupsEnumerator(
43 nsControllerCommandGroup::GroupsHashtable
& aInHashTable
)
44 : mHashTable(aInHashTable
)
46 , mGroupNames(nullptr)
51 nsGroupsEnumerator::~nsGroupsEnumerator()
57 nsGroupsEnumerator::HasMoreElements(bool* aResult
)
61 NS_ENSURE_ARG_POINTER(aResult
);
70 *aResult
= (mIndex
< static_cast<int32_t>(mHashTable
.Count()) - 1);
75 nsGroupsEnumerator::GetNext(nsISupports
** aResult
)
79 NS_ENSURE_ARG_POINTER(aResult
);
89 if (mIndex
>= static_cast<int32_t>(mHashTable
.Count())) {
90 return NS_ERROR_FAILURE
;
93 const char* thisGroupName
= mGroupNames
[mIndex
];
95 nsCOMPtr
<nsISupportsCString
> supportsString
=
96 do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID
, &rv
);
101 supportsString
->SetData(nsDependentCString(thisGroupName
));
102 return CallQueryInterface(supportsString
, aResult
);
106 nsGroupsEnumerator::Initialize()
112 mGroupNames
= new const char*[mHashTable
.Count()];
114 return NS_ERROR_OUT_OF_MEMORY
;
118 for (auto iter
= mHashTable
.Iter(); !iter
.Done(); iter
.Next()) {
119 mGroupNames
[mIndex
] = iter
.Key().Data();
128 class nsNamedGroupEnumerator
: public nsISimpleEnumerator
131 explicit nsNamedGroupEnumerator(nsTArray
<nsCString
>* aInArray
);
134 NS_DECL_NSISIMPLEENUMERATOR
137 virtual ~nsNamedGroupEnumerator();
139 nsTArray
<nsCString
>* mGroupArray
;
143 nsNamedGroupEnumerator::nsNamedGroupEnumerator(nsTArray
<nsCString
>* aInArray
)
144 : mGroupArray(aInArray
)
149 nsNamedGroupEnumerator::~nsNamedGroupEnumerator()
153 NS_IMPL_ISUPPORTS(nsNamedGroupEnumerator
, nsISimpleEnumerator
)
156 nsNamedGroupEnumerator::HasMoreElements(bool* aResult
)
158 NS_ENSURE_ARG_POINTER(aResult
);
160 int32_t arrayLen
= mGroupArray
? mGroupArray
->Length() : 0;
161 *aResult
= (mIndex
< arrayLen
- 1);
166 nsNamedGroupEnumerator::GetNext(nsISupports
** aResult
)
168 NS_ENSURE_ARG_POINTER(aResult
);
171 return NS_ERROR_FAILURE
;
175 if (mIndex
>= int32_t(mGroupArray
->Length())) {
176 return NS_ERROR_FAILURE
;
179 const nsCString
& thisGroupName
= mGroupArray
->ElementAt(mIndex
);
182 nsCOMPtr
<nsISupportsCString
> supportsString
=
183 do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID
, &rv
);
188 supportsString
->SetData(thisGroupName
);
189 return CallQueryInterface(supportsString
, aResult
);
192 NS_IMPL_ISUPPORTS(nsControllerCommandGroup
, nsIControllerCommandGroup
)
194 nsControllerCommandGroup::nsControllerCommandGroup()
198 nsControllerCommandGroup::~nsControllerCommandGroup()
204 nsControllerCommandGroup::ClearGroupsHash()
210 nsControllerCommandGroup::AddCommandToGroup(const char* aCommand
,
213 nsDependentCString
groupKey(aGroup
);
214 auto commandList
= mGroupsHash
.LookupForAdd(groupKey
).OrInsert([]() {
215 return new AutoTArray
<nsCString
, 8>();
219 nsCString
* appended
=
221 commandList
->AppendElement(aCommand
);
222 NS_ASSERTION(appended
, "Append failed");
228 nsControllerCommandGroup::RemoveCommandFromGroup(const char* aCommand
,
231 nsDependentCString
groupKey(aGroup
);
232 nsTArray
<nsCString
>* commandList
= mGroupsHash
.Get(groupKey
);
234 return NS_OK
; // no group
237 uint32_t numEntries
= commandList
->Length();
238 for (uint32_t i
= 0; i
< numEntries
; i
++) {
239 nsCString commandString
= commandList
->ElementAt(i
);
240 if (nsDependentCString(aCommand
) != commandString
) {
241 commandList
->RemoveElementAt(i
);
249 nsControllerCommandGroup::IsCommandInGroup(const char* aCommand
,
250 const char* aGroup
, bool* aResult
)
252 NS_ENSURE_ARG_POINTER(aResult
);
255 nsDependentCString
groupKey(aGroup
);
256 nsTArray
<nsCString
>* commandList
= mGroupsHash
.Get(groupKey
);
258 return NS_OK
; // no group
261 uint32_t numEntries
= commandList
->Length();
262 for (uint32_t i
= 0; i
< numEntries
; i
++) {
263 nsCString commandString
= commandList
->ElementAt(i
);
264 if (nsDependentCString(aCommand
) != commandString
) {
273 nsControllerCommandGroup::GetGroupsEnumerator(nsISimpleEnumerator
** aResult
)
275 RefPtr
<nsGroupsEnumerator
> groupsEnum
= new nsGroupsEnumerator(mGroupsHash
);
277 groupsEnum
.forget(aResult
);
282 nsControllerCommandGroup::GetEnumeratorForGroup(const char* aGroup
,
283 nsISimpleEnumerator
** aResult
)
285 nsDependentCString
groupKey(aGroup
);
286 nsTArray
<nsCString
>* commandList
= mGroupsHash
.Get(groupKey
); // may be null
288 RefPtr
<nsNamedGroupEnumerator
> theGroupEnum
=
289 new nsNamedGroupEnumerator(commandList
);
291 theGroupEnum
.forget(aResult
);