michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsString.h" michael@0: #include "nsIControllerCommand.h" michael@0: #include "nsControllerCommandTable.h" michael@0: michael@0: // prototype; michael@0: nsresult michael@0: NS_NewControllerCommandTable(nsIControllerCommandTable** aResult); michael@0: michael@0: michael@0: // this value is used to size the hash table. Just a sensible upper bound michael@0: #define NUM_COMMANDS_BOUNDS 64 michael@0: michael@0: michael@0: nsControllerCommandTable::nsControllerCommandTable() michael@0: : mCommandsTable(NUM_COMMANDS_BOUNDS) michael@0: , mMutable(true) michael@0: { michael@0: } michael@0: michael@0: michael@0: nsControllerCommandTable::~nsControllerCommandTable() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsControllerCommandTable, nsIControllerCommandTable, nsISupportsWeakReference) michael@0: michael@0: NS_IMETHODIMP michael@0: nsControllerCommandTable::MakeImmutable(void) michael@0: { michael@0: mMutable = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsControllerCommandTable::RegisterCommand(const char * aCommandName, nsIControllerCommand *aCommand) michael@0: { michael@0: NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE); michael@0: michael@0: mCommandsTable.Put(nsDependentCString(aCommandName), aCommand); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsControllerCommandTable::UnregisterCommand(const char * aCommandName, nsIControllerCommand *aCommand) michael@0: { michael@0: NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE); michael@0: michael@0: nsDependentCString commandKey(aCommandName); michael@0: michael@0: if (!mCommandsTable.Get(commandKey, nullptr)) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: mCommandsTable.Remove(commandKey); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsControllerCommandTable::FindCommandHandler(const char * aCommandName, nsIControllerCommand **outCommand) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCommand); michael@0: michael@0: *outCommand = nullptr; michael@0: michael@0: nsCOMPtr foundCommand; michael@0: mCommandsTable.Get(nsDependentCString(aCommandName), getter_AddRefs(foundCommand)); michael@0: if (!foundCommand) return NS_ERROR_FAILURE; michael@0: michael@0: foundCommand.forget(outCommand); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: michael@0: /* boolean isCommandEnabled (in wstring command); */ michael@0: NS_IMETHODIMP michael@0: nsControllerCommandTable::IsCommandEnabled(const char * aCommandName, nsISupports *aCommandRefCon, bool *aResult) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aResult); michael@0: michael@0: *aResult = false; michael@0: michael@0: // find the command michael@0: nsCOMPtr commandHandler; michael@0: FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); michael@0: if (!commandHandler) michael@0: { michael@0: #if DEBUG michael@0: NS_WARNING("Controller command table asked about a command that it does not handle -- "); michael@0: #endif michael@0: return NS_OK; // we don't handle this command michael@0: } michael@0: michael@0: return commandHandler->IsCommandEnabled(aCommandName, aCommandRefCon, aResult); michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsControllerCommandTable::UpdateCommandState(const char * aCommandName, nsISupports *aCommandRefCon) michael@0: { michael@0: // find the command michael@0: nsCOMPtr commandHandler; michael@0: FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); michael@0: if (!commandHandler) michael@0: { michael@0: #if DEBUG michael@0: NS_WARNING("Controller command table asked to update the state of a command that it does not handle -- "); michael@0: #endif michael@0: return NS_OK; // we don't handle this command michael@0: } michael@0: michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsControllerCommandTable::SupportsCommand(const char * aCommandName, nsISupports *aCommandRefCon, bool *aResult) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aResult); michael@0: michael@0: // XXX: need to check the readonly and disabled states michael@0: michael@0: *aResult = false; michael@0: michael@0: // find the command michael@0: nsCOMPtr commandHandler; michael@0: FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); michael@0: michael@0: *aResult = (commandHandler.get() != nullptr); michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* void doCommand (in wstring command); */ michael@0: NS_IMETHODIMP michael@0: nsControllerCommandTable::DoCommand(const char * aCommandName, nsISupports *aCommandRefCon) michael@0: { michael@0: // find the command michael@0: nsCOMPtr commandHandler; michael@0: FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); michael@0: if (!commandHandler) michael@0: { michael@0: #if DEBUG michael@0: NS_WARNING("Controller command table asked to do a command that it does not handle -- "); michael@0: #endif michael@0: return NS_OK; // we don't handle this command michael@0: } michael@0: michael@0: return commandHandler->DoCommand(aCommandName, aCommandRefCon); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsControllerCommandTable::DoCommandParams(const char *aCommandName, nsICommandParams *aParams, nsISupports *aCommandRefCon) michael@0: { michael@0: // find the command michael@0: nsCOMPtr commandHandler; michael@0: FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); michael@0: if (!commandHandler) michael@0: { michael@0: #if DEBUG michael@0: NS_WARNING("Controller command table asked to do a command that it does not handle -- "); michael@0: #endif michael@0: return NS_OK; // we don't handle this command michael@0: } michael@0: return commandHandler->DoCommandParams(aCommandName, aParams, aCommandRefCon); michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsControllerCommandTable::GetCommandState(const char *aCommandName, nsICommandParams *aParams, nsISupports *aCommandRefCon) michael@0: { michael@0: // find the command michael@0: nsCOMPtr commandHandler; michael@0: FindCommandHandler(aCommandName, getter_AddRefs(commandHandler)); michael@0: if (!commandHandler) michael@0: { michael@0: #if DEBUG michael@0: NS_WARNING("Controller command table asked to do a command that it does not handle -- "); michael@0: #endif michael@0: return NS_OK; // we don't handle this command michael@0: } michael@0: return commandHandler->GetCommandStateParams(aCommandName, aParams, aCommandRefCon); michael@0: } michael@0: michael@0: michael@0: nsresult michael@0: NS_NewControllerCommandTable(nsIControllerCommandTable** aResult) michael@0: { michael@0: NS_PRECONDITION(aResult != nullptr, "null ptr"); michael@0: if (! aResult) michael@0: return NS_ERROR_NULL_POINTER; michael@0: michael@0: nsControllerCommandTable* newCommandTable = new nsControllerCommandTable(); michael@0: if (! newCommandTable) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: NS_ADDREF(newCommandTable); michael@0: *aResult = newCommandTable; michael@0: return NS_OK; michael@0: }