1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2002
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsString.h"
39 : #include "nsIComponentManager.h"
40 : #include "nsBaseCommandController.h"
41 :
42 : #include "nsHashtable.h"
43 : #include "nsString.h"
44 : #include "nsWeakPtr.h"
45 :
46 0 : NS_IMPL_ADDREF(nsBaseCommandController)
47 0 : NS_IMPL_RELEASE(nsBaseCommandController)
48 :
49 0 : NS_INTERFACE_MAP_BEGIN(nsBaseCommandController)
50 0 : NS_INTERFACE_MAP_ENTRY(nsIController)
51 0 : NS_INTERFACE_MAP_ENTRY(nsICommandController)
52 0 : NS_INTERFACE_MAP_ENTRY(nsIControllerContext)
53 0 : NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
54 0 : NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIControllerContext)
55 0 : NS_INTERFACE_MAP_END
56 :
57 0 : nsBaseCommandController::nsBaseCommandController()
58 0 : : mCommandContextRawPtr(nsnull)
59 : {
60 0 : }
61 :
62 0 : nsBaseCommandController::~nsBaseCommandController()
63 : {
64 0 : }
65 :
66 : NS_IMETHODIMP
67 0 : nsBaseCommandController::Init(nsIControllerCommandTable *aCommandTable)
68 : {
69 0 : nsresult rv = NS_OK;
70 :
71 0 : if (aCommandTable)
72 0 : mCommandTable = aCommandTable; // owning addref
73 : else
74 0 : mCommandTable = do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv);
75 :
76 0 : return rv;
77 : }
78 :
79 : NS_IMETHODIMP
80 0 : nsBaseCommandController::SetCommandContext(nsISupports *aCommandContext)
81 : {
82 0 : mCommandContextWeakPtr = nsnull;
83 0 : mCommandContextRawPtr = nsnull;
84 :
85 0 : if (aCommandContext) {
86 0 : nsCOMPtr<nsISupportsWeakReference> weak = do_QueryInterface(aCommandContext);
87 0 : if (weak) {
88 : nsresult rv =
89 0 : weak->GetWeakReference(getter_AddRefs(mCommandContextWeakPtr));
90 0 : NS_ENSURE_SUCCESS(rv, rv);
91 : }
92 : else {
93 0 : mCommandContextRawPtr = aCommandContext;
94 : }
95 : }
96 :
97 0 : return NS_OK;
98 : }
99 :
100 : NS_IMETHODIMP
101 0 : nsBaseCommandController::GetInterface(const nsIID & aIID, void * *result)
102 : {
103 0 : NS_ENSURE_ARG_POINTER(result);
104 :
105 0 : if (NS_SUCCEEDED(QueryInterface(aIID, result)))
106 0 : return NS_OK;
107 :
108 0 : if (aIID.Equals(NS_GET_IID(nsIControllerCommandTable)))
109 : {
110 0 : if (mCommandTable)
111 0 : return mCommandTable->QueryInterface(aIID, result);
112 0 : return NS_ERROR_NOT_INITIALIZED;
113 : }
114 :
115 0 : return NS_NOINTERFACE;
116 : }
117 :
118 :
119 :
120 : /* =======================================================================
121 : * nsIController
122 : * ======================================================================= */
123 :
124 : NS_IMETHODIMP
125 0 : nsBaseCommandController::IsCommandEnabled(const char *aCommand,
126 : bool *aResult)
127 : {
128 0 : NS_ENSURE_ARG_POINTER(aCommand);
129 0 : NS_ENSURE_ARG_POINTER(aResult);
130 0 : NS_ENSURE_STATE(mCommandTable);
131 :
132 0 : nsISupports* context = mCommandContextRawPtr;
133 0 : nsCOMPtr<nsISupports> weak;
134 0 : if (!context) {
135 0 : weak = do_QueryReferent(mCommandContextWeakPtr);
136 0 : context = weak;
137 : }
138 0 : return mCommandTable->IsCommandEnabled(aCommand, context, aResult);
139 : }
140 :
141 : NS_IMETHODIMP
142 0 : nsBaseCommandController::SupportsCommand(const char *aCommand, bool *aResult)
143 : {
144 0 : NS_ENSURE_ARG_POINTER(aCommand);
145 0 : NS_ENSURE_ARG_POINTER(aResult);
146 0 : NS_ENSURE_STATE(mCommandTable);
147 :
148 0 : nsISupports* context = mCommandContextRawPtr;
149 0 : nsCOMPtr<nsISupports> weak;
150 0 : if (!context) {
151 0 : weak = do_QueryReferent(mCommandContextWeakPtr);
152 0 : context = weak;
153 : }
154 0 : return mCommandTable->SupportsCommand(aCommand, context, aResult);
155 : }
156 :
157 : NS_IMETHODIMP
158 0 : nsBaseCommandController::DoCommand(const char *aCommand)
159 : {
160 0 : NS_ENSURE_ARG_POINTER(aCommand);
161 0 : NS_ENSURE_STATE(mCommandTable);
162 :
163 0 : nsISupports* context = mCommandContextRawPtr;
164 0 : nsCOMPtr<nsISupports> weak;
165 0 : if (!context) {
166 0 : weak = do_QueryReferent(mCommandContextWeakPtr);
167 0 : context = weak;
168 : }
169 0 : return mCommandTable->DoCommand(aCommand, context);
170 : }
171 :
172 : NS_IMETHODIMP
173 0 : nsBaseCommandController::DoCommandWithParams(const char *aCommand,
174 : nsICommandParams *aParams)
175 : {
176 0 : NS_ENSURE_ARG_POINTER(aCommand);
177 0 : NS_ENSURE_STATE(mCommandTable);
178 :
179 0 : nsISupports* context = mCommandContextRawPtr;
180 0 : nsCOMPtr<nsISupports> weak;
181 0 : if (!context) {
182 0 : weak = do_QueryReferent(mCommandContextWeakPtr);
183 0 : context = weak;
184 : }
185 0 : return mCommandTable->DoCommandParams(aCommand, aParams, context);
186 : }
187 :
188 : NS_IMETHODIMP
189 0 : nsBaseCommandController::GetCommandStateWithParams(const char *aCommand,
190 : nsICommandParams *aParams)
191 : {
192 0 : NS_ENSURE_ARG_POINTER(aCommand);
193 0 : NS_ENSURE_STATE(mCommandTable);
194 :
195 0 : nsISupports* context = mCommandContextRawPtr;
196 0 : nsCOMPtr<nsISupports> weak;
197 0 : if (!context) {
198 0 : weak = do_QueryReferent(mCommandContextWeakPtr);
199 0 : context = weak;
200 : }
201 0 : return mCommandTable->GetCommandState(aCommand, aParams, context);
202 : }
203 :
204 : NS_IMETHODIMP
205 0 : nsBaseCommandController::OnEvent(const char * aEventName)
206 : {
207 0 : NS_ENSURE_ARG_POINTER(aEventName);
208 0 : return NS_OK;
209 : }
|