2015-05-12 09:30:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2002-01-30 08:15:42 +03:00
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2009-02-25 18:25:08 +03:00
|
|
|
#include "nsTArray.h"
|
2002-01-30 08:15:42 +03:00
|
|
|
#include "nsISimpleEnumerator.h"
|
2002-09-04 03:36:13 +04:00
|
|
|
#include "nsXPCOM.h"
|
2002-01-30 08:15:42 +03:00
|
|
|
#include "nsSupportsPrimitives.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsCommandGroup.h"
|
|
|
|
#include "nsIControllerCommand.h"
|
2002-07-16 02:04:13 +04:00
|
|
|
#include "nsCRT.h"
|
2002-01-30 08:15:42 +03:00
|
|
|
|
|
|
|
class nsGroupsEnumerator : public nsISimpleEnumerator
|
|
|
|
{
|
|
|
|
public:
|
2015-05-11 22:35:14 +03:00
|
|
|
explicit nsGroupsEnumerator(
|
|
|
|
nsControllerCommandGroup::GroupsHashtable& aInHashTable);
|
2002-01-30 08:15:42 +03:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
|
|
|
|
|
|
|
protected:
|
2014-07-01 02:11:52 +04:00
|
|
|
virtual ~nsGroupsEnumerator();
|
|
|
|
|
2014-05-13 20:42:41 +04:00
|
|
|
nsresult Initialize();
|
2002-01-30 08:15:42 +03:00
|
|
|
|
|
|
|
protected:
|
2015-05-11 22:35:14 +03:00
|
|
|
nsControllerCommandGroup::GroupsHashtable& mHashTable;
|
2014-05-13 20:42:41 +04:00
|
|
|
int32_t mIndex;
|
2015-11-02 05:41:40 +03:00
|
|
|
const char** mGroupNames; // array of pointers to char16_t* in the hash table
|
2014-05-13 20:42:41 +04:00
|
|
|
bool mInitted;
|
2002-01-30 08:15:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Implementation file */
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsGroupsEnumerator, nsISimpleEnumerator)
|
2002-01-30 08:15:42 +03:00
|
|
|
|
2015-05-11 22:35:14 +03:00
|
|
|
nsGroupsEnumerator::nsGroupsEnumerator(
|
|
|
|
nsControllerCommandGroup::GroupsHashtable& aInHashTable)
|
|
|
|
: mHashTable(aInHashTable)
|
|
|
|
, mIndex(-1)
|
|
|
|
, mGroupNames(nullptr)
|
|
|
|
, mInitted(false)
|
2002-01-30 08:15:42 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsGroupsEnumerator::~nsGroupsEnumerator()
|
|
|
|
{
|
2015-05-11 22:35:14 +03:00
|
|
|
delete[] mGroupNames;
|
2002-01-30 08:15:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-05-11 22:35:14 +03:00
|
|
|
nsGroupsEnumerator::HasMoreElements(bool* aResult)
|
2002-01-30 08:15:42 +03:00
|
|
|
{
|
2014-05-13 20:42:41 +04:00
|
|
|
nsresult rv = NS_OK;
|
2015-05-11 22:35:14 +03:00
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
2005-03-30 01:52:19 +04:00
|
|
|
|
|
|
|
if (!mInitted) {
|
|
|
|
rv = Initialize();
|
2015-05-11 22:35:14 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2005-03-30 01:52:19 +04:00
|
|
|
}
|
2015-05-11 22:35:14 +03:00
|
|
|
|
|
|
|
*aResult = (mIndex < static_cast<int32_t>(mHashTable.Count()) - 1);
|
2005-03-30 01:52:19 +04:00
|
|
|
return NS_OK;
|
2002-01-30 08:15:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-05-11 22:35:14 +03:00
|
|
|
nsGroupsEnumerator::GetNext(nsISupports** aResult)
|
2002-01-30 08:15:42 +03:00
|
|
|
{
|
2014-05-13 20:42:41 +04:00
|
|
|
nsresult rv = NS_OK;
|
2015-05-11 22:35:14 +03:00
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
2005-03-30 01:52:19 +04:00
|
|
|
|
|
|
|
if (!mInitted) {
|
|
|
|
rv = Initialize();
|
2015-05-11 22:35:14 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2005-03-30 01:52:19 +04:00
|
|
|
}
|
2015-05-11 22:35:14 +03:00
|
|
|
|
|
|
|
mIndex++;
|
|
|
|
if (mIndex >= static_cast<int32_t>(mHashTable.Count())) {
|
2005-03-30 01:52:19 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-05-11 22:35:14 +03:00
|
|
|
}
|
|
|
|
|
2015-11-02 05:41:40 +03:00
|
|
|
const char* thisGroupName = mGroupNames[mIndex];
|
2005-03-30 01:52:19 +04:00
|
|
|
|
2015-05-11 22:35:14 +03:00
|
|
|
nsCOMPtr<nsISupportsCString> supportsString =
|
|
|
|
do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2005-03-30 01:52:19 +04:00
|
|
|
|
|
|
|
supportsString->SetData(nsDependentCString(thisGroupName));
|
2015-05-11 22:35:14 +03:00
|
|
|
return CallQueryInterface(supportsString, aResult);
|
2002-01-30 08:15:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsGroupsEnumerator::Initialize()
|
|
|
|
{
|
2015-05-11 22:35:14 +03:00
|
|
|
if (mInitted) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-02 05:41:40 +03:00
|
|
|
mGroupNames = new const char*[mHashTable.Count()];
|
2015-05-11 22:35:14 +03:00
|
|
|
if (!mGroupNames) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIndex = 0;
|
2015-11-02 05:41:40 +03:00
|
|
|
for (auto iter = mHashTable.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
mGroupNames[mIndex] = iter.Key().Data();
|
|
|
|
mIndex++;
|
|
|
|
}
|
2005-03-30 01:52:19 +04:00
|
|
|
|
|
|
|
mIndex = -1;
|
2011-10-17 18:59:28 +04:00
|
|
|
mInitted = true;
|
2005-03-30 01:52:19 +04:00
|
|
|
return NS_OK;
|
2002-01-30 08:15:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class nsNamedGroupEnumerator : public nsISimpleEnumerator
|
|
|
|
{
|
|
|
|
public:
|
2015-05-11 22:35:14 +03:00
|
|
|
explicit nsNamedGroupEnumerator(nsTArray<nsCString>* aInArray);
|
2002-01-30 08:15:42 +03:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
|
|
|
|
|
|
|
protected:
|
2014-07-01 02:11:52 +04:00
|
|
|
virtual ~nsNamedGroupEnumerator();
|
|
|
|
|
2015-05-11 22:35:14 +03:00
|
|
|
nsTArray<nsCString>* mGroupArray;
|
2014-05-13 20:42:41 +04:00
|
|
|
int32_t mIndex;
|
2002-01-30 08:15:42 +03:00
|
|
|
};
|
|
|
|
|
2015-05-11 22:35:14 +03:00
|
|
|
nsNamedGroupEnumerator::nsNamedGroupEnumerator(nsTArray<nsCString>* aInArray)
|
|
|
|
: mGroupArray(aInArray)
|
|
|
|
, mIndex(-1)
|
2002-01-30 08:15:42 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsNamedGroupEnumerator::~nsNamedGroupEnumerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsNamedGroupEnumerator, nsISimpleEnumerator)
|
2002-01-30 08:15:42 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-05-11 22:35:14 +03:00
|
|
|
nsNamedGroupEnumerator::HasMoreElements(bool* aResult)
|
2002-01-30 08:15:42 +03:00
|
|
|
{
|
2015-05-11 22:35:14 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t arrayLen = mGroupArray ? mGroupArray->Length() : 0;
|
2015-05-11 22:35:14 +03:00
|
|
|
*aResult = (mIndex < arrayLen - 1);
|
2005-03-30 01:52:19 +04:00
|
|
|
return NS_OK;
|
2002-01-30 08:15:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-05-11 22:35:14 +03:00
|
|
|
nsNamedGroupEnumerator::GetNext(nsISupports** aResult)
|
2002-01-30 08:15:42 +03:00
|
|
|
{
|
2015-05-11 22:35:14 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
2005-03-30 01:52:19 +04:00
|
|
|
|
2015-05-11 22:35:14 +03:00
|
|
|
if (!mGroupArray) {
|
2005-03-30 01:52:19 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-05-11 22:35:14 +03:00
|
|
|
}
|
2005-03-30 01:52:19 +04:00
|
|
|
|
2014-05-13 20:42:41 +04:00
|
|
|
mIndex++;
|
2015-05-11 22:35:14 +03:00
|
|
|
if (mIndex >= int32_t(mGroupArray->Length())) {
|
2005-03-30 01:52:19 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-05-11 22:35:14 +03:00
|
|
|
}
|
|
|
|
|
2014-05-13 20:46:37 +04:00
|
|
|
const nsCString& thisGroupName = mGroupArray->ElementAt(mIndex);
|
2015-05-11 22:35:14 +03:00
|
|
|
|
2005-03-30 01:52:19 +04:00
|
|
|
nsresult rv;
|
2015-05-11 22:35:14 +03:00
|
|
|
nsCOMPtr<nsISupportsCString> supportsString =
|
|
|
|
do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2005-03-30 01:52:19 +04:00
|
|
|
|
2014-05-13 20:46:37 +04:00
|
|
|
supportsString->SetData(thisGroupName);
|
2015-05-11 22:35:14 +03:00
|
|
|
return CallQueryInterface(supportsString, aResult);
|
2002-01-30 08:15:42 +03:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsControllerCommandGroup, nsIControllerCommandGroup)
|
2002-01-30 08:15:42 +03:00
|
|
|
|
|
|
|
nsControllerCommandGroup::nsControllerCommandGroup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsControllerCommandGroup::~nsControllerCommandGroup()
|
|
|
|
{
|
|
|
|
ClearGroupsHash();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsControllerCommandGroup::ClearGroupsHash()
|
|
|
|
{
|
2014-05-13 20:42:41 +04:00
|
|
|
mGroupsHash.Clear();
|
2002-01-30 08:15:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-05-11 22:35:14 +03:00
|
|
|
nsControllerCommandGroup::AddCommandToGroup(const char* aCommand,
|
|
|
|
const char* aGroup)
|
2002-01-30 08:15:42 +03:00
|
|
|
{
|
2014-05-13 20:42:41 +04:00
|
|
|
nsDependentCString groupKey(aGroup);
|
2017-05-03 18:25:37 +03:00
|
|
|
auto commandList = mGroupsHash.LookupForAdd(groupKey).OrInsert([]() {
|
|
|
|
return new AutoTArray<nsCString, 8>();
|
|
|
|
});
|
2014-05-13 20:46:37 +04:00
|
|
|
|
2010-07-11 16:35:12 +04:00
|
|
|
#ifdef DEBUG
|
2015-05-11 22:35:14 +03:00
|
|
|
nsCString* appended =
|
2010-07-11 16:35:12 +04:00
|
|
|
#endif
|
2014-05-13 20:46:37 +04:00
|
|
|
commandList->AppendElement(aCommand);
|
2002-01-30 08:15:42 +03:00
|
|
|
NS_ASSERTION(appended, "Append failed");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-05-11 22:35:14 +03:00
|
|
|
nsControllerCommandGroup::RemoveCommandFromGroup(const char* aCommand,
|
|
|
|
const char* aGroup)
|
2002-01-30 08:15:42 +03:00
|
|
|
{
|
2014-05-13 20:42:41 +04:00
|
|
|
nsDependentCString groupKey(aGroup);
|
2015-05-11 22:35:14 +03:00
|
|
|
nsTArray<nsCString>* commandList = mGroupsHash.Get(groupKey);
|
|
|
|
if (!commandList) {
|
|
|
|
return NS_OK; // no group
|
|
|
|
}
|
2002-01-30 08:15:42 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numEntries = commandList->Length();
|
2015-05-11 22:35:14 +03:00
|
|
|
for (uint32_t i = 0; i < numEntries; i++) {
|
2014-05-13 20:46:37 +04:00
|
|
|
nsCString commandString = commandList->ElementAt(i);
|
2015-05-11 22:35:14 +03:00
|
|
|
if (nsDependentCString(aCommand) != commandString) {
|
2002-01-30 08:15:42 +03:00
|
|
|
commandList->RemoveElementAt(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-05-11 22:35:14 +03:00
|
|
|
nsControllerCommandGroup::IsCommandInGroup(const char* aCommand,
|
|
|
|
const char* aGroup, bool* aResult)
|
2002-01-30 08:15:42 +03:00
|
|
|
{
|
2015-05-11 22:35:14 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
|
|
|
*aResult = false;
|
|
|
|
|
2014-05-13 20:42:41 +04:00
|
|
|
nsDependentCString groupKey(aGroup);
|
2015-05-11 22:35:14 +03:00
|
|
|
nsTArray<nsCString>* commandList = mGroupsHash.Get(groupKey);
|
|
|
|
if (!commandList) {
|
|
|
|
return NS_OK; // no group
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numEntries = commandList->Length();
|
2015-05-11 22:35:14 +03:00
|
|
|
for (uint32_t i = 0; i < numEntries; i++) {
|
2014-05-13 20:46:37 +04:00
|
|
|
nsCString commandString = commandList->ElementAt(i);
|
2015-05-11 22:35:14 +03:00
|
|
|
if (nsDependentCString(aCommand) != commandString) {
|
|
|
|
*aResult = true;
|
2002-01-30 08:15:42 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-05-11 22:35:14 +03:00
|
|
|
nsControllerCommandGroup::GetGroupsEnumerator(nsISimpleEnumerator** aResult)
|
2002-01-30 08:15:42 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsGroupsEnumerator> groupsEnum = new nsGroupsEnumerator(mGroupsHash);
|
2002-01-30 08:15:42 +03:00
|
|
|
|
2015-05-11 22:35:14 +03:00
|
|
|
groupsEnum.forget(aResult);
|
2015-03-12 16:43:50 +03:00
|
|
|
return NS_OK;
|
2002-01-30 08:15:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-05-11 22:35:14 +03:00
|
|
|
nsControllerCommandGroup::GetEnumeratorForGroup(const char* aGroup,
|
|
|
|
nsISimpleEnumerator** aResult)
|
2002-01-30 08:15:42 +03:00
|
|
|
{
|
2014-05-13 20:42:41 +04:00
|
|
|
nsDependentCString groupKey(aGroup);
|
2015-05-11 22:35:14 +03:00
|
|
|
nsTArray<nsCString>* commandList = mGroupsHash.Get(groupKey); // may be null
|
2002-01-30 08:15:42 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsNamedGroupEnumerator> theGroupEnum =
|
2015-05-11 22:35:14 +03:00
|
|
|
new nsNamedGroupEnumerator(commandList);
|
2002-01-30 08:15:42 +03:00
|
|
|
|
2015-05-11 22:35:14 +03:00
|
|
|
theGroupEnum.forget(aResult);
|
2015-03-12 16:43:50 +03:00
|
|
|
return NS_OK;
|
2002-01-30 08:15:42 +03:00
|
|
|
}
|