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.org 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) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Simon Fraser <sfraser@netscape.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsString.h"
40 : #include "nsIControllerCommand.h"
41 : #include "nsControllerCommandTable.h"
42 :
43 : // prototype;
44 : nsresult
45 : NS_NewControllerCommandTable(nsIControllerCommandTable** aResult);
46 :
47 :
48 : // this value is used to size the hash table. Just a sensible upper bound
49 : #define NUM_COMMANDS_BOUNDS 64
50 :
51 :
52 0 : nsControllerCommandTable::nsControllerCommandTable()
53 : : mCommandsTable(NUM_COMMANDS_BOUNDS, false)
54 0 : , mMutable(true)
55 : {
56 0 : }
57 :
58 :
59 0 : nsControllerCommandTable::~nsControllerCommandTable()
60 : {
61 0 : }
62 :
63 0 : NS_IMPL_ISUPPORTS2(nsControllerCommandTable, nsIControllerCommandTable, nsISupportsWeakReference)
64 :
65 : NS_IMETHODIMP
66 0 : nsControllerCommandTable::MakeImmutable(void)
67 : {
68 0 : mMutable = false;
69 0 : return NS_OK;
70 : }
71 :
72 : NS_IMETHODIMP
73 0 : nsControllerCommandTable::RegisterCommand(const char * aCommandName, nsIControllerCommand *aCommand)
74 : {
75 0 : NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE);
76 :
77 0 : nsCStringKey commandKey(aCommandName);
78 :
79 0 : if (mCommandsTable.Put(&commandKey, aCommand))
80 : {
81 : #if DEBUG
82 0 : NS_WARNING("Replacing existing command -- ");
83 : #endif
84 : }
85 0 : return NS_OK;
86 : }
87 :
88 :
89 : NS_IMETHODIMP
90 0 : nsControllerCommandTable::UnregisterCommand(const char * aCommandName, nsIControllerCommand *aCommand)
91 : {
92 0 : NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE);
93 :
94 0 : nsCStringKey commandKey(aCommandName);
95 :
96 0 : bool wasRemoved = mCommandsTable.Remove(&commandKey);
97 0 : return wasRemoved ? NS_OK : NS_ERROR_FAILURE;
98 : }
99 :
100 :
101 : NS_IMETHODIMP
102 0 : nsControllerCommandTable::FindCommandHandler(const char * aCommandName, nsIControllerCommand **outCommand)
103 : {
104 0 : NS_ENSURE_ARG_POINTER(outCommand);
105 :
106 0 : *outCommand = NULL;
107 :
108 0 : nsCStringKey commandKey(aCommandName);
109 0 : nsISupports* foundCommand = mCommandsTable.Get(&commandKey);
110 0 : if (!foundCommand) return NS_ERROR_FAILURE;
111 :
112 : // no need to addref since the .Get does it for us
113 0 : *outCommand = reinterpret_cast<nsIControllerCommand*>(foundCommand);
114 0 : return NS_OK;
115 : }
116 :
117 :
118 :
119 : /* boolean isCommandEnabled (in wstring command); */
120 : NS_IMETHODIMP
121 0 : nsControllerCommandTable::IsCommandEnabled(const char * aCommandName, nsISupports *aCommandRefCon, bool *aResult)
122 : {
123 0 : NS_ENSURE_ARG_POINTER(aResult);
124 :
125 0 : *aResult = false;
126 :
127 : // find the command
128 0 : nsCOMPtr<nsIControllerCommand> commandHandler;
129 0 : FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
130 0 : if (!commandHandler)
131 : {
132 : #if DEBUG
133 0 : NS_WARNING("Controller command table asked about a command that it does not handle -- ");
134 : #endif
135 0 : return NS_OK; // we don't handle this command
136 : }
137 :
138 0 : return commandHandler->IsCommandEnabled(aCommandName, aCommandRefCon, aResult);
139 : }
140 :
141 :
142 : NS_IMETHODIMP
143 0 : nsControllerCommandTable::UpdateCommandState(const char * aCommandName, nsISupports *aCommandRefCon)
144 : {
145 : // find the command
146 0 : nsCOMPtr<nsIControllerCommand> commandHandler;
147 0 : FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
148 0 : if (!commandHandler)
149 : {
150 : #if DEBUG
151 0 : NS_WARNING("Controller command table asked to update the state of a command that it does not handle -- ");
152 : #endif
153 0 : return NS_OK; // we don't handle this command
154 : }
155 :
156 0 : return NS_ERROR_NOT_IMPLEMENTED;
157 : }
158 :
159 : NS_IMETHODIMP
160 0 : nsControllerCommandTable::SupportsCommand(const char * aCommandName, nsISupports *aCommandRefCon, bool *aResult)
161 : {
162 0 : NS_ENSURE_ARG_POINTER(aResult);
163 :
164 : // XXX: need to check the readonly and disabled states
165 :
166 0 : *aResult = false;
167 :
168 : // find the command
169 0 : nsCOMPtr<nsIControllerCommand> commandHandler;
170 0 : FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
171 :
172 0 : *aResult = (commandHandler.get() != nsnull);
173 0 : return NS_OK;
174 : }
175 :
176 : /* void doCommand (in wstring command); */
177 : NS_IMETHODIMP
178 0 : nsControllerCommandTable::DoCommand(const char * aCommandName, nsISupports *aCommandRefCon)
179 : {
180 : // find the command
181 0 : nsCOMPtr<nsIControllerCommand> commandHandler;
182 0 : FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
183 0 : if (!commandHandler)
184 : {
185 : #if DEBUG
186 0 : NS_WARNING("Controller command table asked to do a command that it does not handle -- ");
187 : #endif
188 0 : return NS_OK; // we don't handle this command
189 : }
190 :
191 0 : return commandHandler->DoCommand(aCommandName, aCommandRefCon);
192 : }
193 :
194 : NS_IMETHODIMP
195 0 : nsControllerCommandTable::DoCommandParams(const char *aCommandName, nsICommandParams *aParams, nsISupports *aCommandRefCon)
196 : {
197 : // find the command
198 0 : nsCOMPtr<nsIControllerCommand> commandHandler;
199 : nsresult rv;
200 0 : rv = FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
201 0 : if (!commandHandler)
202 : {
203 : #if DEBUG
204 0 : NS_WARNING("Controller command table asked to do a command that it does not handle -- ");
205 : #endif
206 0 : return NS_OK; // we don't handle this command
207 : }
208 0 : return commandHandler->DoCommandParams(aCommandName, aParams, aCommandRefCon);
209 : }
210 :
211 :
212 : NS_IMETHODIMP
213 0 : nsControllerCommandTable::GetCommandState(const char *aCommandName, nsICommandParams *aParams, nsISupports *aCommandRefCon)
214 : {
215 : // find the command
216 0 : nsCOMPtr<nsIControllerCommand> commandHandler;
217 : nsresult rv;
218 0 : rv = FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
219 0 : if (!commandHandler)
220 : {
221 : #if DEBUG
222 0 : NS_WARNING("Controller command table asked to do a command that it does not handle -- ");
223 : #endif
224 0 : return NS_OK; // we don't handle this command
225 : }
226 0 : return commandHandler->GetCommandStateParams(aCommandName, aParams, aCommandRefCon);
227 : }
228 :
229 :
230 : nsresult
231 0 : NS_NewControllerCommandTable(nsIControllerCommandTable** aResult)
232 : {
233 0 : NS_PRECONDITION(aResult != nsnull, "null ptr");
234 0 : if (! aResult)
235 0 : return NS_ERROR_NULL_POINTER;
236 :
237 0 : nsControllerCommandTable* newCommandTable = new nsControllerCommandTable();
238 0 : if (! newCommandTable)
239 0 : return NS_ERROR_OUT_OF_MEMORY;
240 :
241 0 : NS_ADDREF(newCommandTable);
242 0 : *aResult = newCommandTable;
243 0 : return NS_OK;
244 : }
245 :
246 :
247 :
|