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 "nsIComponentManager.h" michael@0: #include "nsBaseCommandController.h" michael@0: michael@0: #include "nsString.h" michael@0: #include "nsWeakPtr.h" michael@0: michael@0: NS_IMPL_ADDREF(nsBaseCommandController) michael@0: NS_IMPL_RELEASE(nsBaseCommandController) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN(nsBaseCommandController) michael@0: NS_INTERFACE_MAP_ENTRY(nsIController) michael@0: NS_INTERFACE_MAP_ENTRY(nsICommandController) michael@0: NS_INTERFACE_MAP_ENTRY(nsIControllerContext) michael@0: NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor) michael@0: NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIControllerContext) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: nsBaseCommandController::nsBaseCommandController() michael@0: : mCommandContextRawPtr(nullptr) michael@0: { michael@0: } michael@0: michael@0: nsBaseCommandController::~nsBaseCommandController() michael@0: { michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseCommandController::Init(nsIControllerCommandTable *aCommandTable) michael@0: { michael@0: nsresult rv = NS_OK; michael@0: michael@0: if (aCommandTable) michael@0: mCommandTable = aCommandTable; // owning addref michael@0: else michael@0: mCommandTable = do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseCommandController::SetCommandContext(nsISupports *aCommandContext) michael@0: { michael@0: mCommandContextWeakPtr = nullptr; michael@0: mCommandContextRawPtr = nullptr; michael@0: michael@0: if (aCommandContext) { michael@0: nsCOMPtr weak = do_QueryInterface(aCommandContext); michael@0: if (weak) { michael@0: nsresult rv = michael@0: weak->GetWeakReference(getter_AddRefs(mCommandContextWeakPtr)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: else { michael@0: mCommandContextRawPtr = aCommandContext; michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseCommandController::GetInterface(const nsIID & aIID, void * *result) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(result); michael@0: michael@0: if (NS_SUCCEEDED(QueryInterface(aIID, result))) michael@0: return NS_OK; michael@0: michael@0: if (aIID.Equals(NS_GET_IID(nsIControllerCommandTable))) michael@0: { michael@0: if (mCommandTable) michael@0: return mCommandTable->QueryInterface(aIID, result); michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: } michael@0: michael@0: return NS_NOINTERFACE; michael@0: } michael@0: michael@0: michael@0: michael@0: /* ======================================================================= michael@0: * nsIController michael@0: * ======================================================================= */ michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseCommandController::IsCommandEnabled(const char *aCommand, michael@0: bool *aResult) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aCommand); michael@0: NS_ENSURE_ARG_POINTER(aResult); michael@0: NS_ENSURE_STATE(mCommandTable); michael@0: michael@0: nsISupports* context = mCommandContextRawPtr; michael@0: nsCOMPtr weak; michael@0: if (!context) { michael@0: weak = do_QueryReferent(mCommandContextWeakPtr); michael@0: context = weak; michael@0: } michael@0: return mCommandTable->IsCommandEnabled(aCommand, context, aResult); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseCommandController::SupportsCommand(const char *aCommand, bool *aResult) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aCommand); michael@0: NS_ENSURE_ARG_POINTER(aResult); michael@0: NS_ENSURE_STATE(mCommandTable); michael@0: michael@0: nsISupports* context = mCommandContextRawPtr; michael@0: nsCOMPtr weak; michael@0: if (!context) { michael@0: weak = do_QueryReferent(mCommandContextWeakPtr); michael@0: context = weak; michael@0: } michael@0: return mCommandTable->SupportsCommand(aCommand, context, aResult); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseCommandController::DoCommand(const char *aCommand) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aCommand); michael@0: NS_ENSURE_STATE(mCommandTable); michael@0: michael@0: nsISupports* context = mCommandContextRawPtr; michael@0: nsCOMPtr weak; michael@0: if (!context) { michael@0: weak = do_QueryReferent(mCommandContextWeakPtr); michael@0: context = weak; michael@0: } michael@0: return mCommandTable->DoCommand(aCommand, context); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseCommandController::DoCommandWithParams(const char *aCommand, michael@0: nsICommandParams *aParams) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aCommand); michael@0: NS_ENSURE_STATE(mCommandTable); michael@0: michael@0: nsISupports* context = mCommandContextRawPtr; michael@0: nsCOMPtr weak; michael@0: if (!context) { michael@0: weak = do_QueryReferent(mCommandContextWeakPtr); michael@0: context = weak; michael@0: } michael@0: return mCommandTable->DoCommandParams(aCommand, aParams, context); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseCommandController::GetCommandStateWithParams(const char *aCommand, michael@0: nsICommandParams *aParams) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aCommand); michael@0: NS_ENSURE_STATE(mCommandTable); michael@0: michael@0: nsISupports* context = mCommandContextRawPtr; michael@0: nsCOMPtr weak; michael@0: if (!context) { michael@0: weak = do_QueryReferent(mCommandContextWeakPtr); michael@0: context = weak; michael@0: } michael@0: return mCommandTable->GetCommandState(aCommand, aParams, context); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseCommandController::OnEvent(const char * aEventName) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aEventName); michael@0: return NS_OK; michael@0: }