Back to index

lightning-sunbird  0.9+nobinonly
nsBaseCommandController.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 Communicator client 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) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsString.h"
00039 #include "nsIComponentManager.h"
00040 #include "nsBaseCommandController.h"
00041 
00042 #include "nsHashtable.h"
00043 #include "nsString.h"
00044 #include "nsWeakPtr.h"
00045 
00046 NS_IMPL_ADDREF(nsBaseCommandController)
00047 NS_IMPL_RELEASE(nsBaseCommandController)
00048 
00049 NS_INTERFACE_MAP_BEGIN(nsBaseCommandController)
00050   NS_INTERFACE_MAP_ENTRY(nsIController)
00051   NS_INTERFACE_MAP_ENTRY(nsICommandController)
00052   NS_INTERFACE_MAP_ENTRY(nsIControllerContext)
00053   NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
00054   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIControllerContext)
00055 NS_INTERFACE_MAP_END
00056 
00057 nsBaseCommandController::nsBaseCommandController()
00058   : mCommandContextRawPtr(nsnull)
00059 {
00060 }
00061 
00062 nsBaseCommandController::~nsBaseCommandController()
00063 {
00064 }
00065 
00066 NS_IMETHODIMP
00067 nsBaseCommandController::Init(nsIControllerCommandTable *aCommandTable)
00068 {
00069   nsresult rv = NS_OK;
00070 
00071   if (aCommandTable)
00072     mCommandTable = aCommandTable;    // owning addref
00073   else
00074     mCommandTable = do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv);
00075   
00076   return rv;
00077 }
00078 
00079 NS_IMETHODIMP
00080 nsBaseCommandController::SetCommandContext(nsISupports *aCommandContext)
00081 {
00082   mCommandContextWeakPtr = nsnull;
00083   mCommandContextRawPtr = nsnull;
00084 
00085   if (aCommandContext) {
00086     nsCOMPtr<nsISupportsWeakReference> weak = do_QueryInterface(aCommandContext);
00087     if (weak) {
00088       nsresult rv =
00089         weak->GetWeakReference(getter_AddRefs(mCommandContextWeakPtr));
00090       NS_ENSURE_SUCCESS(rv, rv);
00091     }
00092     else {
00093       mCommandContextRawPtr = aCommandContext;
00094     }
00095   }
00096 
00097   return NS_OK;
00098 }
00099 
00100 NS_IMETHODIMP
00101 nsBaseCommandController::GetInterface(const nsIID & aIID, void * *result)
00102 {
00103   NS_ENSURE_ARG_POINTER(result);
00104 
00105   if (NS_SUCCEEDED(QueryInterface(aIID, result)))
00106     return NS_OK;
00107 
00108   if (aIID.Equals(NS_GET_IID(nsIControllerCommandTable)))
00109   {
00110     if (mCommandTable)
00111       return mCommandTable->QueryInterface(aIID, result);
00112     return NS_ERROR_NOT_INITIALIZED;
00113   }
00114     
00115   return NS_NOINTERFACE;
00116 }
00117 
00118 
00119 
00120 /* =======================================================================
00121  * nsIController
00122  * ======================================================================= */
00123 
00124 NS_IMETHODIMP
00125 nsBaseCommandController::IsCommandEnabled(const char *aCommand,
00126                                           PRBool *aResult)
00127 {
00128   NS_ENSURE_ARG_POINTER(aCommand);
00129   NS_ENSURE_ARG_POINTER(aResult);
00130 
00131   nsISupports* context = mCommandContextRawPtr;
00132   nsCOMPtr<nsISupports> weak;
00133   if (!context) {
00134     weak = do_QueryReferent(mCommandContextWeakPtr);
00135     context = weak;
00136   }
00137   NS_ENSURE_STATE(mCommandTable);
00138   return mCommandTable->IsCommandEnabled(aCommand, context, aResult);
00139 }
00140 
00141 NS_IMETHODIMP
00142 nsBaseCommandController::SupportsCommand(const char *aCommand, PRBool *aResult)
00143 {
00144   NS_ENSURE_ARG_POINTER(aCommand);
00145   NS_ENSURE_ARG_POINTER(aResult);
00146 
00147   nsISupports* context = mCommandContextRawPtr;
00148   nsCOMPtr<nsISupports> weak;
00149   if (!context) {
00150     weak = do_QueryReferent(mCommandContextWeakPtr);
00151     context = weak;
00152   }
00153   NS_ENSURE_STATE(mCommandTable);
00154   return mCommandTable->SupportsCommand(aCommand, context, aResult);
00155 }
00156 
00157 NS_IMETHODIMP
00158 nsBaseCommandController::DoCommand(const char *aCommand)
00159 {
00160   NS_ENSURE_ARG_POINTER(aCommand);
00161 
00162   nsISupports* context = mCommandContextRawPtr;
00163   nsCOMPtr<nsISupports> weak;
00164   if (!context) {
00165     weak = do_QueryReferent(mCommandContextWeakPtr);
00166     context = weak;
00167   }
00168   NS_ENSURE_STATE(mCommandTable);
00169   return mCommandTable->DoCommand(aCommand, context);
00170 }
00171 
00172 NS_IMETHODIMP
00173 nsBaseCommandController::DoCommandWithParams(const char *aCommand,
00174                                              nsICommandParams *aParams)
00175 {
00176   NS_ENSURE_ARG_POINTER(aCommand);
00177 
00178   nsISupports* context = mCommandContextRawPtr;
00179   nsCOMPtr<nsISupports> weak;
00180   if (!context) {
00181     weak = do_QueryReferent(mCommandContextWeakPtr);
00182     context = weak;
00183   }
00184   NS_ENSURE_STATE(mCommandTable);
00185   return mCommandTable->DoCommandParams(aCommand, aParams, context);
00186 }
00187 
00188 NS_IMETHODIMP
00189 nsBaseCommandController::GetCommandStateWithParams(const char *aCommand,
00190                                                    nsICommandParams *aParams)
00191 {
00192   NS_ENSURE_ARG_POINTER(aCommand);
00193 
00194   nsISupports* context = mCommandContextRawPtr;
00195   nsCOMPtr<nsISupports> weak;
00196   if (!context) {
00197     weak = do_QueryReferent(mCommandContextWeakPtr);
00198     context = weak;
00199   }
00200   NS_ENSURE_STATE(mCommandTable);
00201   return mCommandTable->GetCommandState(aCommand, aParams, context);
00202 }
00203 
00204 NS_IMETHODIMP
00205 nsBaseCommandController::OnEvent(const char * aEventName)
00206 {
00207   NS_ENSURE_ARG_POINTER(aEventName);
00208   return NS_OK;
00209 }