Back to index

lightning-sunbird  0.9+nobinonly
nsControllerCommandTable.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Simon Fraser <sfraser@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsString.h"
00040 #include "nsIControllerCommand.h"
00041 #include "nsControllerCommandTable.h"
00042 
00043 // prototype;
00044 nsresult
00045 NS_NewControllerCommandTable(nsIControllerCommandTable** aResult);
00046 
00047 
00048 // this value is used to size the hash table. Just a sensible upper bound
00049 #define NUM_COMMANDS_BOUNDS       64
00050 
00051 
00052 nsControllerCommandTable::nsControllerCommandTable()
00053 : mCommandsTable(NUM_COMMANDS_BOUNDS, PR_FALSE)
00054 , mMutable(PR_TRUE)
00055 {
00056 }
00057 
00058 
00059 nsControllerCommandTable::~nsControllerCommandTable()
00060 {
00061 }
00062 
00063 NS_IMPL_ISUPPORTS2(nsControllerCommandTable, nsIControllerCommandTable, nsISupportsWeakReference)
00064 
00065 NS_IMETHODIMP
00066 nsControllerCommandTable::MakeImmutable(void)
00067 {
00068   mMutable = PR_FALSE;
00069   return NS_OK;
00070 }
00071 
00072 NS_IMETHODIMP
00073 nsControllerCommandTable::RegisterCommand(const char * aCommandName, nsIControllerCommand *aCommand)
00074 {
00075   NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE);
00076   
00077   nsCStringKey commandKey(aCommandName);
00078   
00079   if (mCommandsTable.Put(&commandKey, aCommand))
00080   {
00081 #if DEBUG
00082     NS_WARNING("Replacing existing command -- ");
00083 #endif
00084   }  
00085   return NS_OK;
00086 }
00087 
00088 
00089 NS_IMETHODIMP
00090 nsControllerCommandTable::UnregisterCommand(const char * aCommandName, nsIControllerCommand *aCommand)
00091 {
00092   NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE);
00093 
00094   nsCStringKey commandKey(aCommandName);
00095 
00096   PRBool wasRemoved = mCommandsTable.Remove(&commandKey);
00097   return wasRemoved ? NS_OK : NS_ERROR_FAILURE;
00098 }
00099 
00100 
00101 NS_IMETHODIMP
00102 nsControllerCommandTable::FindCommandHandler(const char * aCommandName, nsIControllerCommand **outCommand)
00103 {
00104   NS_ENSURE_ARG_POINTER(outCommand);
00105   
00106   *outCommand = NULL;
00107   
00108   nsCStringKey commandKey(aCommandName);
00109   nsISupports* foundCommand = mCommandsTable.Get(&commandKey);
00110   if (!foundCommand) return NS_ERROR_FAILURE;
00111   
00112   // no need to addref since the .Get does it for us
00113   *outCommand = NS_REINTERPRET_CAST(nsIControllerCommand*, foundCommand);
00114   return NS_OK;
00115 }
00116 
00117 
00118 
00119 /* boolean isCommandEnabled (in wstring command); */
00120 NS_IMETHODIMP
00121 nsControllerCommandTable::IsCommandEnabled(const char * aCommandName, nsISupports *aCommandRefCon, PRBool *aResult)
00122 {
00123   NS_ENSURE_ARG_POINTER(aResult);
00124 
00125   *aResult = PR_FALSE;
00126       
00127   // find the command  
00128   nsCOMPtr<nsIControllerCommand> commandHandler;
00129   FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));  
00130   if (!commandHandler) 
00131   {
00132 #if DEBUG
00133     NS_WARNING("Controller command table asked about a command that it does not handle -- ");
00134 #endif
00135     return NS_OK;    // we don't handle this command
00136   }
00137   
00138   return commandHandler->IsCommandEnabled(aCommandName, aCommandRefCon, aResult);
00139 }
00140 
00141 
00142 NS_IMETHODIMP
00143 nsControllerCommandTable::UpdateCommandState(const char * aCommandName, nsISupports *aCommandRefCon)
00144 {
00145   // find the command  
00146   nsCOMPtr<nsIControllerCommand> commandHandler;
00147   FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));  
00148   if (!commandHandler)
00149   {
00150 #if DEBUG
00151     NS_WARNING("Controller command table asked to update the state of a command that it does not handle -- ");
00152 #endif
00153     return NS_OK;    // we don't handle this command
00154   }
00155   
00156   return NS_ERROR_NOT_IMPLEMENTED;
00157 }
00158 
00159 NS_IMETHODIMP
00160 nsControllerCommandTable::SupportsCommand(const char * aCommandName, nsISupports *aCommandRefCon, PRBool *aResult)
00161 {
00162   NS_ENSURE_ARG_POINTER(aResult);
00163 
00164   // XXX: need to check the readonly and disabled states
00165 
00166   *aResult = PR_FALSE;
00167   
00168   // find the command  
00169   nsCOMPtr<nsIControllerCommand> commandHandler;
00170   FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
00171 
00172   *aResult = (commandHandler.get() != nsnull);
00173   return NS_OK;
00174 }
00175 
00176 /* void doCommand (in wstring command); */
00177 NS_IMETHODIMP
00178 nsControllerCommandTable::DoCommand(const char * aCommandName, nsISupports *aCommandRefCon)
00179 {
00180   // find the command  
00181   nsCOMPtr<nsIControllerCommand> commandHandler;
00182   FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
00183   if (!commandHandler)
00184   {
00185 #if DEBUG
00186     NS_WARNING("Controller command table asked to do a command that it does not handle -- ");
00187 #endif
00188     return NS_OK;    // we don't handle this command
00189   }
00190   
00191   return commandHandler->DoCommand(aCommandName, aCommandRefCon);
00192 }
00193 
00194 NS_IMETHODIMP
00195 nsControllerCommandTable::DoCommandParams(const char *aCommandName, nsICommandParams *aParams, nsISupports *aCommandRefCon)
00196 {
00197   // find the command  
00198   nsCOMPtr<nsIControllerCommand> commandHandler;
00199   nsresult rv;
00200   rv = FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
00201   if (!commandHandler)
00202   {
00203 #if DEBUG
00204     NS_WARNING("Controller command table asked to do a command that it does not handle -- ");
00205 #endif
00206     return NS_OK;    // we don't handle this command
00207   }
00208   return commandHandler->DoCommandParams(aCommandName, aParams, aCommandRefCon);
00209 }
00210 
00211 
00212 NS_IMETHODIMP
00213 nsControllerCommandTable::GetCommandState(const char *aCommandName, nsICommandParams *aParams, nsISupports *aCommandRefCon)
00214 {
00215   // find the command  
00216   nsCOMPtr<nsIControllerCommand> commandHandler;
00217   nsresult rv;
00218   rv = FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
00219   if (!commandHandler)
00220   {
00221 #if DEBUG
00222     NS_WARNING("Controller command table asked to do a command that it does not handle -- ");
00223 #endif
00224     return NS_OK;    // we don't handle this command
00225   }
00226   return commandHandler->GetCommandStateParams(aCommandName, aParams, aCommandRefCon);
00227 }
00228 
00229 
00230 nsresult
00231 NS_NewControllerCommandTable(nsIControllerCommandTable** aResult)
00232 {
00233   NS_PRECONDITION(aResult != nsnull, "null ptr");
00234   if (! aResult)
00235     return NS_ERROR_NULL_POINTER;
00236 
00237   nsControllerCommandTable* newCommandTable = new nsControllerCommandTable();
00238   if (! newCommandTable)
00239     return NS_ERROR_OUT_OF_MEMORY;
00240 
00241   NS_ADDREF(newCommandTable);
00242   *aResult = newCommandTable;
00243   return NS_OK;
00244 }
00245 
00246 
00247