Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "nsString.h"
8 : #include "nsIComponentManager.h"
9 : #include "nsBaseCommandController.h"
10 :
11 : #include "nsString.h"
12 : #include "nsWeakPtr.h"
13 :
14 118 : NS_IMPL_ADDREF(nsBaseCommandController)
15 110 : NS_IMPL_RELEASE(nsBaseCommandController)
16 :
17 94 : NS_INTERFACE_MAP_BEGIN(nsBaseCommandController)
18 94 : NS_INTERFACE_MAP_ENTRY(nsIController)
19 36 : NS_INTERFACE_MAP_ENTRY(nsICommandController)
20 32 : NS_INTERFACE_MAP_ENTRY(nsIControllerContext)
21 16 : NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
22 16 : NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIControllerContext)
23 12 : NS_INTERFACE_MAP_END
24 :
25 6 : nsBaseCommandController::nsBaseCommandController()
26 6 : : mCommandContextRawPtr(nullptr)
27 : {
28 6 : }
29 :
30 0 : nsBaseCommandController::~nsBaseCommandController()
31 : {
32 0 : }
33 :
34 : NS_IMETHODIMP
35 6 : nsBaseCommandController::Init(nsIControllerCommandTable* aCommandTable)
36 : {
37 6 : nsresult rv = NS_OK;
38 :
39 6 : if (aCommandTable) {
40 6 : mCommandTable = aCommandTable;
41 : } else {
42 : mCommandTable =
43 0 : do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv);
44 : }
45 :
46 6 : return rv;
47 : }
48 :
49 : NS_IMETHODIMP
50 10 : nsBaseCommandController::SetCommandContext(nsISupports* aCommandContext)
51 : {
52 10 : mCommandContextWeakPtr = nullptr;
53 10 : mCommandContextRawPtr = nullptr;
54 :
55 10 : if (aCommandContext) {
56 12 : nsCOMPtr<nsISupportsWeakReference> weak = do_QueryInterface(aCommandContext);
57 6 : if (weak) {
58 : nsresult rv =
59 6 : weak->GetWeakReference(getter_AddRefs(mCommandContextWeakPtr));
60 6 : NS_ENSURE_SUCCESS(rv, rv);
61 : } else {
62 0 : mCommandContextRawPtr = aCommandContext;
63 : }
64 : }
65 :
66 10 : return NS_OK;
67 : }
68 :
69 : NS_IMETHODIMP
70 0 : nsBaseCommandController::GetInterface(const nsIID& aIID, void** aResult)
71 : {
72 0 : NS_ENSURE_ARG_POINTER(aResult);
73 :
74 0 : if (NS_SUCCEEDED(QueryInterface(aIID, aResult))) {
75 0 : return NS_OK;
76 : }
77 :
78 0 : if (aIID.Equals(NS_GET_IID(nsIControllerCommandTable))) {
79 0 : if (mCommandTable) {
80 0 : return mCommandTable->QueryInterface(aIID, aResult);
81 : }
82 0 : return NS_ERROR_NOT_INITIALIZED;
83 : }
84 :
85 0 : return NS_NOINTERFACE;
86 : }
87 :
88 : /* =======================================================================
89 : * nsIController
90 : * ======================================================================= */
91 :
92 : NS_IMETHODIMP
93 126 : nsBaseCommandController::IsCommandEnabled(const char* aCommand, bool* aResult)
94 : {
95 126 : NS_ENSURE_ARG_POINTER(aCommand);
96 126 : NS_ENSURE_ARG_POINTER(aResult);
97 126 : NS_ENSURE_STATE(mCommandTable);
98 :
99 126 : nsISupports* context = mCommandContextRawPtr;
100 252 : nsCOMPtr<nsISupports> weak;
101 126 : if (!context) {
102 126 : weak = do_QueryReferent(mCommandContextWeakPtr);
103 126 : context = weak;
104 : }
105 126 : return mCommandTable->IsCommandEnabled(aCommand, context, aResult);
106 : }
107 :
108 : NS_IMETHODIMP
109 11 : nsBaseCommandController::SupportsCommand(const char* aCommand, bool* aResult)
110 : {
111 11 : NS_ENSURE_ARG_POINTER(aCommand);
112 11 : NS_ENSURE_ARG_POINTER(aResult);
113 11 : NS_ENSURE_STATE(mCommandTable);
114 :
115 11 : nsISupports* context = mCommandContextRawPtr;
116 22 : nsCOMPtr<nsISupports> weak;
117 11 : if (!context) {
118 11 : weak = do_QueryReferent(mCommandContextWeakPtr);
119 11 : context = weak;
120 : }
121 11 : return mCommandTable->SupportsCommand(aCommand, context, aResult);
122 : }
123 :
124 : NS_IMETHODIMP
125 0 : nsBaseCommandController::DoCommand(const char* aCommand)
126 : {
127 0 : NS_ENSURE_ARG_POINTER(aCommand);
128 0 : NS_ENSURE_STATE(mCommandTable);
129 :
130 0 : nsISupports* context = mCommandContextRawPtr;
131 0 : nsCOMPtr<nsISupports> weak;
132 0 : if (!context) {
133 0 : weak = do_QueryReferent(mCommandContextWeakPtr);
134 0 : context = weak;
135 : }
136 0 : return mCommandTable->DoCommand(aCommand, context);
137 : }
138 :
139 : NS_IMETHODIMP
140 0 : nsBaseCommandController::DoCommandWithParams(const char* aCommand,
141 : nsICommandParams* aParams)
142 : {
143 0 : NS_ENSURE_ARG_POINTER(aCommand);
144 0 : NS_ENSURE_STATE(mCommandTable);
145 :
146 0 : nsISupports* context = mCommandContextRawPtr;
147 0 : nsCOMPtr<nsISupports> weak;
148 0 : if (!context) {
149 0 : weak = do_QueryReferent(mCommandContextWeakPtr);
150 0 : context = weak;
151 : }
152 0 : return mCommandTable->DoCommandParams(aCommand, aParams, context);
153 : }
154 :
155 : NS_IMETHODIMP
156 0 : nsBaseCommandController::GetCommandStateWithParams(const char* aCommand,
157 : nsICommandParams* aParams)
158 : {
159 0 : NS_ENSURE_ARG_POINTER(aCommand);
160 0 : NS_ENSURE_STATE(mCommandTable);
161 :
162 0 : nsISupports* context = mCommandContextRawPtr;
163 0 : nsCOMPtr<nsISupports> weak;
164 0 : if (!context) {
165 0 : weak = do_QueryReferent(mCommandContextWeakPtr);
166 0 : context = weak;
167 : }
168 0 : return mCommandTable->GetCommandState(aCommand, aParams, context);
169 : }
170 :
171 : NS_IMETHODIMP
172 0 : nsBaseCommandController::OnEvent(const char* aEventName)
173 : {
174 0 : NS_ENSURE_ARG_POINTER(aEventName);
175 0 : return NS_OK;
176 : }
177 :
178 : NS_IMETHODIMP
179 4 : nsBaseCommandController::GetSupportedCommands(uint32_t* aCount,
180 : char*** aCommands)
181 : {
182 4 : NS_ENSURE_STATE(mCommandTable);
183 4 : return mCommandTable->GetSupportedCommands(aCount, aCommands);
184 : }
|