1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/embedding/components/commandhandler/src/nsBaseCommandController.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,176 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsString.h" 1.10 +#include "nsIComponentManager.h" 1.11 +#include "nsBaseCommandController.h" 1.12 + 1.13 +#include "nsString.h" 1.14 +#include "nsWeakPtr.h" 1.15 + 1.16 +NS_IMPL_ADDREF(nsBaseCommandController) 1.17 +NS_IMPL_RELEASE(nsBaseCommandController) 1.18 + 1.19 +NS_INTERFACE_MAP_BEGIN(nsBaseCommandController) 1.20 + NS_INTERFACE_MAP_ENTRY(nsIController) 1.21 + NS_INTERFACE_MAP_ENTRY(nsICommandController) 1.22 + NS_INTERFACE_MAP_ENTRY(nsIControllerContext) 1.23 + NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor) 1.24 + NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIControllerContext) 1.25 +NS_INTERFACE_MAP_END 1.26 + 1.27 +nsBaseCommandController::nsBaseCommandController() 1.28 + : mCommandContextRawPtr(nullptr) 1.29 +{ 1.30 +} 1.31 + 1.32 +nsBaseCommandController::~nsBaseCommandController() 1.33 +{ 1.34 +} 1.35 + 1.36 +NS_IMETHODIMP 1.37 +nsBaseCommandController::Init(nsIControllerCommandTable *aCommandTable) 1.38 +{ 1.39 + nsresult rv = NS_OK; 1.40 + 1.41 + if (aCommandTable) 1.42 + mCommandTable = aCommandTable; // owning addref 1.43 + else 1.44 + mCommandTable = do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv); 1.45 + 1.46 + return rv; 1.47 +} 1.48 + 1.49 +NS_IMETHODIMP 1.50 +nsBaseCommandController::SetCommandContext(nsISupports *aCommandContext) 1.51 +{ 1.52 + mCommandContextWeakPtr = nullptr; 1.53 + mCommandContextRawPtr = nullptr; 1.54 + 1.55 + if (aCommandContext) { 1.56 + nsCOMPtr<nsISupportsWeakReference> weak = do_QueryInterface(aCommandContext); 1.57 + if (weak) { 1.58 + nsresult rv = 1.59 + weak->GetWeakReference(getter_AddRefs(mCommandContextWeakPtr)); 1.60 + NS_ENSURE_SUCCESS(rv, rv); 1.61 + } 1.62 + else { 1.63 + mCommandContextRawPtr = aCommandContext; 1.64 + } 1.65 + } 1.66 + 1.67 + return NS_OK; 1.68 +} 1.69 + 1.70 +NS_IMETHODIMP 1.71 +nsBaseCommandController::GetInterface(const nsIID & aIID, void * *result) 1.72 +{ 1.73 + NS_ENSURE_ARG_POINTER(result); 1.74 + 1.75 + if (NS_SUCCEEDED(QueryInterface(aIID, result))) 1.76 + return NS_OK; 1.77 + 1.78 + if (aIID.Equals(NS_GET_IID(nsIControllerCommandTable))) 1.79 + { 1.80 + if (mCommandTable) 1.81 + return mCommandTable->QueryInterface(aIID, result); 1.82 + return NS_ERROR_NOT_INITIALIZED; 1.83 + } 1.84 + 1.85 + return NS_NOINTERFACE; 1.86 +} 1.87 + 1.88 + 1.89 + 1.90 +/* ======================================================================= 1.91 + * nsIController 1.92 + * ======================================================================= */ 1.93 + 1.94 +NS_IMETHODIMP 1.95 +nsBaseCommandController::IsCommandEnabled(const char *aCommand, 1.96 + bool *aResult) 1.97 +{ 1.98 + NS_ENSURE_ARG_POINTER(aCommand); 1.99 + NS_ENSURE_ARG_POINTER(aResult); 1.100 + NS_ENSURE_STATE(mCommandTable); 1.101 + 1.102 + nsISupports* context = mCommandContextRawPtr; 1.103 + nsCOMPtr<nsISupports> weak; 1.104 + if (!context) { 1.105 + weak = do_QueryReferent(mCommandContextWeakPtr); 1.106 + context = weak; 1.107 + } 1.108 + return mCommandTable->IsCommandEnabled(aCommand, context, aResult); 1.109 +} 1.110 + 1.111 +NS_IMETHODIMP 1.112 +nsBaseCommandController::SupportsCommand(const char *aCommand, bool *aResult) 1.113 +{ 1.114 + NS_ENSURE_ARG_POINTER(aCommand); 1.115 + NS_ENSURE_ARG_POINTER(aResult); 1.116 + NS_ENSURE_STATE(mCommandTable); 1.117 + 1.118 + nsISupports* context = mCommandContextRawPtr; 1.119 + nsCOMPtr<nsISupports> weak; 1.120 + if (!context) { 1.121 + weak = do_QueryReferent(mCommandContextWeakPtr); 1.122 + context = weak; 1.123 + } 1.124 + return mCommandTable->SupportsCommand(aCommand, context, aResult); 1.125 +} 1.126 + 1.127 +NS_IMETHODIMP 1.128 +nsBaseCommandController::DoCommand(const char *aCommand) 1.129 +{ 1.130 + NS_ENSURE_ARG_POINTER(aCommand); 1.131 + NS_ENSURE_STATE(mCommandTable); 1.132 + 1.133 + nsISupports* context = mCommandContextRawPtr; 1.134 + nsCOMPtr<nsISupports> weak; 1.135 + if (!context) { 1.136 + weak = do_QueryReferent(mCommandContextWeakPtr); 1.137 + context = weak; 1.138 + } 1.139 + return mCommandTable->DoCommand(aCommand, context); 1.140 +} 1.141 + 1.142 +NS_IMETHODIMP 1.143 +nsBaseCommandController::DoCommandWithParams(const char *aCommand, 1.144 + nsICommandParams *aParams) 1.145 +{ 1.146 + NS_ENSURE_ARG_POINTER(aCommand); 1.147 + NS_ENSURE_STATE(mCommandTable); 1.148 + 1.149 + nsISupports* context = mCommandContextRawPtr; 1.150 + nsCOMPtr<nsISupports> weak; 1.151 + if (!context) { 1.152 + weak = do_QueryReferent(mCommandContextWeakPtr); 1.153 + context = weak; 1.154 + } 1.155 + return mCommandTable->DoCommandParams(aCommand, aParams, context); 1.156 +} 1.157 + 1.158 +NS_IMETHODIMP 1.159 +nsBaseCommandController::GetCommandStateWithParams(const char *aCommand, 1.160 + nsICommandParams *aParams) 1.161 +{ 1.162 + NS_ENSURE_ARG_POINTER(aCommand); 1.163 + NS_ENSURE_STATE(mCommandTable); 1.164 + 1.165 + nsISupports* context = mCommandContextRawPtr; 1.166 + nsCOMPtr<nsISupports> weak; 1.167 + if (!context) { 1.168 + weak = do_QueryReferent(mCommandContextWeakPtr); 1.169 + context = weak; 1.170 + } 1.171 + return mCommandTable->GetCommandState(aCommand, aParams, context); 1.172 +} 1.173 + 1.174 +NS_IMETHODIMP 1.175 +nsBaseCommandController::OnEvent(const char * aEventName) 1.176 +{ 1.177 + NS_ENSURE_ARG_POINTER(aEventName); 1.178 + return NS_OK; 1.179 +}