nsCommandGroup.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* -*- Mode: C++; tab-width: 8; 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. #include "nsString.h"
  6. #include "nsReadableUtils.h"
  7. #include "nsTArray.h"
  8. #include "nsISimpleEnumerator.h"
  9. #include "nsXPCOM.h"
  10. #include "nsSupportsPrimitives.h"
  11. #include "nsIComponentManager.h"
  12. #include "nsCommandGroup.h"
  13. #include "nsIControllerCommand.h"
  14. #include "nsCRT.h"
  15. class nsGroupsEnumerator : public nsISimpleEnumerator
  16. {
  17. public:
  18. explicit nsGroupsEnumerator(
  19. nsControllerCommandGroup::GroupsHashtable& aInHashTable);
  20. NS_DECL_ISUPPORTS
  21. NS_DECL_NSISIMPLEENUMERATOR
  22. protected:
  23. virtual ~nsGroupsEnumerator();
  24. nsresult Initialize();
  25. protected:
  26. nsControllerCommandGroup::GroupsHashtable& mHashTable;
  27. int32_t mIndex;
  28. const char** mGroupNames; // array of pointers to char16_t* in the hash table
  29. bool mInitted;
  30. };
  31. /* Implementation file */
  32. NS_IMPL_ISUPPORTS(nsGroupsEnumerator, nsISimpleEnumerator)
  33. nsGroupsEnumerator::nsGroupsEnumerator(
  34. nsControllerCommandGroup::GroupsHashtable& aInHashTable)
  35. : mHashTable(aInHashTable)
  36. , mIndex(-1)
  37. , mGroupNames(nullptr)
  38. , mInitted(false)
  39. {
  40. }
  41. nsGroupsEnumerator::~nsGroupsEnumerator()
  42. {
  43. delete[] mGroupNames;
  44. }
  45. NS_IMETHODIMP
  46. nsGroupsEnumerator::HasMoreElements(bool* aResult)
  47. {
  48. nsresult rv = NS_OK;
  49. NS_ENSURE_ARG_POINTER(aResult);
  50. if (!mInitted) {
  51. rv = Initialize();
  52. if (NS_FAILED(rv)) {
  53. return rv;
  54. }
  55. }
  56. *aResult = (mIndex < static_cast<int32_t>(mHashTable.Count()) - 1);
  57. return NS_OK;
  58. }
  59. NS_IMETHODIMP
  60. nsGroupsEnumerator::GetNext(nsISupports** aResult)
  61. {
  62. nsresult rv = NS_OK;
  63. NS_ENSURE_ARG_POINTER(aResult);
  64. if (!mInitted) {
  65. rv = Initialize();
  66. if (NS_FAILED(rv)) {
  67. return rv;
  68. }
  69. }
  70. mIndex++;
  71. if (mIndex >= static_cast<int32_t>(mHashTable.Count())) {
  72. return NS_ERROR_FAILURE;
  73. }
  74. const char* thisGroupName = mGroupNames[mIndex];
  75. nsCOMPtr<nsISupportsCString> supportsString =
  76. do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
  77. if (NS_FAILED(rv)) {
  78. return rv;
  79. }
  80. supportsString->SetData(nsDependentCString(thisGroupName));
  81. return CallQueryInterface(supportsString, aResult);
  82. }
  83. nsresult
  84. nsGroupsEnumerator::Initialize()
  85. {
  86. if (mInitted) {
  87. return NS_OK;
  88. }
  89. mGroupNames = new const char*[mHashTable.Count()];
  90. if (!mGroupNames) {
  91. return NS_ERROR_OUT_OF_MEMORY;
  92. }
  93. mIndex = 0;
  94. for (auto iter = mHashTable.Iter(); !iter.Done(); iter.Next()) {
  95. mGroupNames[mIndex] = iter.Key().Data();
  96. mIndex++;
  97. }
  98. mIndex = -1;
  99. mInitted = true;
  100. return NS_OK;
  101. }
  102. class nsNamedGroupEnumerator : public nsISimpleEnumerator
  103. {
  104. public:
  105. explicit nsNamedGroupEnumerator(nsTArray<nsCString>* aInArray);
  106. NS_DECL_ISUPPORTS
  107. NS_DECL_NSISIMPLEENUMERATOR
  108. protected:
  109. virtual ~nsNamedGroupEnumerator();
  110. nsTArray<nsCString>* mGroupArray;
  111. int32_t mIndex;
  112. };
  113. nsNamedGroupEnumerator::nsNamedGroupEnumerator(nsTArray<nsCString>* aInArray)
  114. : mGroupArray(aInArray)
  115. , mIndex(-1)
  116. {
  117. }
  118. nsNamedGroupEnumerator::~nsNamedGroupEnumerator()
  119. {
  120. }
  121. NS_IMPL_ISUPPORTS(nsNamedGroupEnumerator, nsISimpleEnumerator)
  122. NS_IMETHODIMP
  123. nsNamedGroupEnumerator::HasMoreElements(bool* aResult)
  124. {
  125. NS_ENSURE_ARG_POINTER(aResult);
  126. int32_t arrayLen = mGroupArray ? mGroupArray->Length() : 0;
  127. *aResult = (mIndex < arrayLen - 1);
  128. return NS_OK;
  129. }
  130. NS_IMETHODIMP
  131. nsNamedGroupEnumerator::GetNext(nsISupports** aResult)
  132. {
  133. NS_ENSURE_ARG_POINTER(aResult);
  134. if (!mGroupArray) {
  135. return NS_ERROR_FAILURE;
  136. }
  137. mIndex++;
  138. if (mIndex >= int32_t(mGroupArray->Length())) {
  139. return NS_ERROR_FAILURE;
  140. }
  141. const nsCString& thisGroupName = mGroupArray->ElementAt(mIndex);
  142. nsresult rv;
  143. nsCOMPtr<nsISupportsCString> supportsString =
  144. do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
  145. if (NS_FAILED(rv)) {
  146. return rv;
  147. }
  148. supportsString->SetData(thisGroupName);
  149. return CallQueryInterface(supportsString, aResult);
  150. }
  151. NS_IMPL_ISUPPORTS(nsControllerCommandGroup, nsIControllerCommandGroup)
  152. nsControllerCommandGroup::nsControllerCommandGroup()
  153. {
  154. }
  155. nsControllerCommandGroup::~nsControllerCommandGroup()
  156. {
  157. ClearGroupsHash();
  158. }
  159. void
  160. nsControllerCommandGroup::ClearGroupsHash()
  161. {
  162. mGroupsHash.Clear();
  163. }
  164. NS_IMETHODIMP
  165. nsControllerCommandGroup::AddCommandToGroup(const char* aCommand,
  166. const char* aGroup)
  167. {
  168. nsDependentCString groupKey(aGroup);
  169. nsTArray<nsCString>* commandList = mGroupsHash.Get(groupKey);
  170. if (!commandList) {
  171. // make this list
  172. commandList = new AutoTArray<nsCString, 8>;
  173. mGroupsHash.Put(groupKey, commandList);
  174. }
  175. #ifdef DEBUG
  176. nsCString* appended =
  177. #endif
  178. commandList->AppendElement(aCommand);
  179. NS_ASSERTION(appended, "Append failed");
  180. return NS_OK;
  181. }
  182. NS_IMETHODIMP
  183. nsControllerCommandGroup::RemoveCommandFromGroup(const char* aCommand,
  184. const char* aGroup)
  185. {
  186. nsDependentCString groupKey(aGroup);
  187. nsTArray<nsCString>* commandList = mGroupsHash.Get(groupKey);
  188. if (!commandList) {
  189. return NS_OK; // no group
  190. }
  191. uint32_t numEntries = commandList->Length();
  192. for (uint32_t i = 0; i < numEntries; i++) {
  193. nsCString commandString = commandList->ElementAt(i);
  194. if (nsDependentCString(aCommand) != commandString) {
  195. commandList->RemoveElementAt(i);
  196. break;
  197. }
  198. }
  199. return NS_OK;
  200. }
  201. NS_IMETHODIMP
  202. nsControllerCommandGroup::IsCommandInGroup(const char* aCommand,
  203. const char* aGroup, bool* aResult)
  204. {
  205. NS_ENSURE_ARG_POINTER(aResult);
  206. *aResult = false;
  207. nsDependentCString groupKey(aGroup);
  208. nsTArray<nsCString>* commandList = mGroupsHash.Get(groupKey);
  209. if (!commandList) {
  210. return NS_OK; // no group
  211. }
  212. uint32_t numEntries = commandList->Length();
  213. for (uint32_t i = 0; i < numEntries; i++) {
  214. nsCString commandString = commandList->ElementAt(i);
  215. if (nsDependentCString(aCommand) != commandString) {
  216. *aResult = true;
  217. break;
  218. }
  219. }
  220. return NS_OK;
  221. }
  222. NS_IMETHODIMP
  223. nsControllerCommandGroup::GetGroupsEnumerator(nsISimpleEnumerator** aResult)
  224. {
  225. RefPtr<nsGroupsEnumerator> groupsEnum = new nsGroupsEnumerator(mGroupsHash);
  226. groupsEnum.forget(aResult);
  227. return NS_OK;
  228. }
  229. NS_IMETHODIMP
  230. nsControllerCommandGroup::GetEnumeratorForGroup(const char* aGroup,
  231. nsISimpleEnumerator** aResult)
  232. {
  233. nsDependentCString groupKey(aGroup);
  234. nsTArray<nsCString>* commandList = mGroupsHash.Get(groupKey); // may be null
  235. RefPtr<nsNamedGroupEnumerator> theGroupEnum =
  236. new nsNamedGroupEnumerator(commandList);
  237. theGroupEnum.forget(aResult);
  238. return NS_OK;
  239. }