Work in progress for command handling APIs. Not part of the build

This commit is contained in:
sfraser%netscape.com 2001-04-06 01:27:53 +00:00
Родитель 3bbb6a5040
Коммит 8680c7438f
8 изменённых файлов: 1334 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,142 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
*/
#include "nsString.h"
#include "nsReadableUtils.h"
#include "nsVoidArray.h"
#include "nsCommandGroup.h"
/* Implementation file */
NS_IMPL_ISUPPORTS1(nsControllerCommandGroup, nsIControllerCommandGroup)
nsControllerCommandGroup::nsControllerCommandGroup()
{
NS_INIT_ISUPPORTS();
}
nsControllerCommandGroup::~nsControllerCommandGroup()
{
ClearGroupsHash();
}
void
nsControllerCommandGroup::ClearGroupsHash()
{
mGroupsHash.Reset(ClearEnumerator, (void *)this);
}
#if 0
#pragma mark -
#endif
/* void addCommandToGroup (in DOMString aCommand, in DOMString aGroup); */
NS_IMETHODIMP
nsControllerCommandGroup::AddCommandToGroup(const nsAReadableString & aCommand, const nsAReadableString & aGroup)
{
nsStringKey groupKey(aGroup);
nsVoidArray* commandList;
if ((commandList = (nsVoidArray *)mGroupsHash.Get(&groupKey)) == nsnull)
{
// make this list
commandList = new nsVoidArray;
mGroupsHash.Put(&groupKey, (void *)commandList);
}
// add the command to the list. Note that we're not checking for duplicates here
PRUnichar* commandString = ToNewUnicode(aCommand); // we store allocated PRUnichar* in the array
if (!commandString) return NS_ERROR_OUT_OF_MEMORY;
PRBool appended = commandList->AppendElement((void *)commandString);
NS_ASSERTION(appended, "Append failed");
return NS_OK;
}
/* void removeCommandFromGroup (in DOMString aCommand, in DOMString aGroup); */
NS_IMETHODIMP
nsControllerCommandGroup::RemoveCommandFromGroup(const nsAReadableString & aCommand, const nsAReadableString & aGroup)
{
nsStringKey groupKey(aGroup);
nsVoidArray* commandList = (nsVoidArray *)mGroupsHash.Get(&groupKey);
if (!commandList) return NS_OK; // no group
PRInt32 numEntries = commandList->Count();
for (PRInt32 i = 0; i < numEntries; i ++)
{
PRUnichar* commandString = (PRUnichar*)commandList->ElementAt(i);
if (aCommand.Equals(commandString))
{
commandList->RemoveElementAt(i);
nsMemory::Free(commandString);
break;
}
}
return NS_OK;
}
/* boolean isCommandInGroup (in DOMString aCommand, in DOMString aGroup); */
NS_IMETHODIMP
nsControllerCommandGroup::IsCommandInGroup(const nsAReadableString & aCommand, const nsAReadableString & aGroup, PRBool *_retval)
{
NS_ENSURE_ARG_POINTER(_retval);
*_retval = PR_FALSE;
nsStringKey groupKey(aGroup);
nsVoidArray* commandList = (nsVoidArray *)mGroupsHash.Get(&groupKey);
if (!commandList) return NS_OK; // no group
PRInt32 numEntries = commandList->Count();
for (PRInt32 i = 0; i < numEntries; i ++)
{
PRUnichar* commandString = (PRUnichar*)commandList->ElementAt(i);
if (aCommand.Equals(commandString))
{
*_retval = PR_TRUE;
break;
}
}
return NS_OK;
}
#if 0
#pragma mark -
#endif
PRBool nsControllerCommandGroup::ClearEnumerator(nsHashKey *aKey, void *aData, void* closure)
{
nsVoidArray* commandList = (nsVoidArray *)aData;
if (commandList)
{
PRInt32 numEntries = commandList->Count();
for (PRInt32 i = 0; i < numEntries; i ++)
{
PRUnichar* commandString = (PRUnichar*)commandList->ElementAt(i);
nsMemory::Free(commandString);
}
delete commandList;
}
return PR_TRUE;
}

Просмотреть файл

@ -0,0 +1,69 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
*/
#ifndef nsCommandGroup_h__
#define nsCommandGroup_h__
#include "nsIController.h"
#include "nsHashtable.h"
// {ecd55a01-2780-11d5-a73c-ca641a6813bc}
#define NS_CONTROLLER_COMMAND_GROUP_CID \
{ 0xecd55a01, 0x2780, 0x11d5, { 0xa7, 0x3c, 0xca, 0x64, 0x1a, 0x68, 0x13, 0xbc } }
#define NS_CONTROLLER_COMMAND_GROUP_CONTRACTID \
"@mozilla.org/embedcomp/controller-command-group;1"
class nsControllerCommandGroup : public nsIControllerCommandGroup
{
public:
nsControllerCommandGroup();
virtual ~nsControllerCommandGroup();
NS_DECL_ISUPPORTS
NS_DECL_NSICONTROLLERCOMMANDGROUP
protected:
void ClearGroupsHash();
protected:
static PRBool ClearEnumerator(nsHashKey *aKey, void *aData, void* closure);
protected:
nsHashtable mGroupsHash; // hash keyed on command group.
// Entries are nsVoidArrays of pointers to PRUnichar*
// This could be made more space-efficient, maybe with atoms
};
#endif // nsCommandGroup_h__

Просмотреть файл

@ -0,0 +1,217 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
*/
#include "nsString.h"
#include "nsIController.h"
#include "nsIObserver.h"
#include "nsIObserverList.h"
#include "nsIComponentManager.h"
#include "nsIDOMWindow.h"
#include "nsPIDOMWindow.h"
#include "nsIFocusController.h"
#include "nsCommandManager.h"
nsCommandManager::nsCommandManager()
: mWindow(nsnull)
{
NS_INIT_ISUPPORTS();
/* member initializers and constructor code */
}
nsCommandManager::~nsCommandManager()
{
/* destructor code */
}
NS_IMPL_ISUPPORTS3(nsCommandManager, nsICommandManager, nsPICommandUpdater, nsISupportsWeakReference)
#if 0
#pragma mark -
#endif
/* void init (in nsIDOMWindow aWindow); */
NS_IMETHODIMP
nsCommandManager::Init(nsIDOMWindow *aWindow)
{
NS_ENSURE_ARG_POINTER(aWindow);
NS_ASSERTION(aWindow, "Need non-null window here");
mWindow = aWindow; // weak ptr
return NS_OK;
}
/* void commandStatusChanged (in DOMString aCommandName, in long aChangeFlags); */
NS_IMETHODIMP
nsCommandManager::CommandStatusChanged(const nsAReadableString & aCommandName, PRInt32 aChangeFlags)
{
nsStringKey hashKey(aCommandName);
nsPromiseFlatString flatCommand = PromiseFlatString(aCommandName);
nsresult rv = NS_OK;
nsCOMPtr<nsISupports> commandSupports = getter_AddRefs(mCommandObserversTable.Get(&hashKey));
nsCOMPtr<nsIObserverList> commandObservers = do_QueryInterface(commandSupports);
if (commandObservers)
{
nsCOMPtr<nsIEnumerator> observers;
rv = commandObservers->EnumerateObserverList(getter_AddRefs(observers));
if (observers)
{
rv = observers->First();
// Continue until error or end of list.
while (observers->IsDone() != NS_OK && NS_SUCCEEDED(rv))
{
nsCOMPtr<nsISupports> itemSupports;
rv = observers->CurrentItem(getter_AddRefs(itemSupports));
nsCOMPtr<nsIObserver> itemObserver = do_QueryInterface(itemSupports);
if (itemObserver)
{
// should we get the command state to pass here? This might be expensive.
itemObserver->Observe((nsICommandManager *)this, flatCommand.get(), NS_LITERAL_STRING("").get());
}
rv = observers->Next();
}
}
}
return NS_OK;
}
#if 0
#pragma mark -
#endif
/* void addCommandObserver (in nsIObserver aCommandObserver, in wstring aCommandToObserve); */
NS_IMETHODIMP
nsCommandManager::AddCommandObserver(nsIObserver *aCommandObserver, const nsAReadableString & aCommandToObserve)
{
NS_ENSURE_ARG(aCommandObserver);
nsresult rv = NS_OK;
// XXX todo: handle special cases of aCommandToObserve being null, or empty
// for each command in the table, we make a list of observers for that command
nsStringKey hashKey(aCommandToObserve);
nsCOMPtr<nsISupports> commandSupports = getter_AddRefs(mCommandObserversTable.Get(&hashKey));
nsCOMPtr<nsIObserverList> commandObservers = do_QueryInterface(commandSupports);
if (!commandObservers)
{
commandObservers = do_CreateInstance("@mozilla.org/xpcom/observer-list;1", &rv);
if (NS_FAILED(rv)) return rv;
rv = mCommandObserversTable.Put(&hashKey, commandObservers);
if (NS_FAILED(rv)) return rv;
}
return commandObservers->AddObserver(aCommandObserver);
}
/* void removeCommandObserver (in nsIObserver aCommandObserver, in wstring aCommandObserved); */
NS_IMETHODIMP
nsCommandManager::RemoveCommandObserver(nsIObserver *aCommandObserver, const nsAReadableString & aCommandObserved)
{
NS_ENSURE_ARG(aCommandObserver);
// XXX todo: handle special cases of aCommandToObserve being null, or empty
nsStringKey hashKey(aCommandObserved);
nsCOMPtr<nsISupports> commandSupports = getter_AddRefs(mCommandObserversTable.Get(&hashKey));
nsCOMPtr<nsIObserverList> commandObservers = do_QueryInterface(commandSupports);
if (!commandObservers)
return NS_ERROR_UNEXPECTED;
return commandObservers->RemoveObserver(aCommandObserver);
}
/* boolean isCommandSupported (in wstring aCommandName); */
NS_IMETHODIMP
nsCommandManager::IsCommandSupported(const nsAReadableString & aCommandName, PRBool *outCommandSupported)
{
NS_ENSURE_ARG_POINTER(outCommandSupported);
nsCOMPtr<nsIController> controller;
nsresult rv = GetControllerForCommand(aCommandName, getter_AddRefs(controller));
*outCommandSupported = (controller.get() != nsnull);
return NS_OK;
}
/* boolean isCommandEnabled (in wstring aCommandName); */
NS_IMETHODIMP
nsCommandManager::IsCommandEnabled(const nsAReadableString & aCommandName, PRBool *outCommandEnabled)
{
NS_ENSURE_ARG_POINTER(outCommandEnabled);
PRBool commandEnabled = PR_FALSE;
nsCOMPtr<nsIController> controller;
nsresult rv = GetControllerForCommand(aCommandName, getter_AddRefs(controller));
if (controller)
{
controller->IsCommandEnabled(aCommandName, &commandEnabled);
}
*outCommandEnabled = commandEnabled;
return NS_OK;
}
/* void doCommand (in wstring aCommandName, in wstring aCommandParams); */
NS_IMETHODIMP
nsCommandManager::DoCommand(const nsAReadableString & aCommandName)
{
nsCOMPtr<nsIController> controller;
nsresult rv = GetControllerForCommand(aCommandName, getter_AddRefs(controller));
if (!controller)
return NS_ERROR_FAILURE;
return controller->DoCommand(aCommandName);
}
#ifdef XP_MAC
#pragma mark -
#endif
NS_IMETHODIMP
nsCommandManager::GetControllerForCommand(const nsAReadableString& aCommand, nsIController** outController)
{
nsresult rv = NS_ERROR_FAILURE;
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(mWindow);
if (!window)
return NS_ERROR_FAILURE;
nsCOMPtr<nsIFocusController> focusController;
rv = window->GetRootFocusController(getter_AddRefs(focusController));
if (!focusController)
return NS_ERROR_FAILURE;
return focusController->GetControllerForCommand(aCommand, getter_AddRefs(outController));
}

Просмотреть файл

@ -0,0 +1,77 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
*/
#ifndef nsCommandManager_h__
#define nsCommandManager_h__
#include "nsString.h"
#include "nsHashtable.h"
#include "nsWeakReference.h"
#include "nsICommandManager.h"
#include "nsPICommandUpdater.h"
class nsIController;
// {64edb481-0c04-11d5-a73c-e964b968b0bc}
#define NS_COMMAND_MANAGER_CID \
{ 0x64edb481, 0x0c04, 0x11d5, { 0xa7, 0x3c, 0xe9, 0x64, 0xb9, 0x68, 0xb0, 0xbc } }
#define NS_COMMAND_MANAGER_CONTRACTID \
"@mozilla.org/embedcomp/command-manager;1"
class nsCommandManager : public nsICommandManager,
public nsPICommandUpdater,
public nsSupportsWeakReference
{
public:
nsCommandManager();
virtual ~nsCommandManager();
// nsISupports
NS_DECL_ISUPPORTS
// nsICommandManager
NS_DECL_NSICOMMANDMANAGER
// nsPICommandUpdater
NS_DECL_NSPICOMMANDUPDATER
protected:
nsresult GetControllerForCommand(const nsAReadableString& aCommand, nsIController** outController);
protected:
nsSupportsHashtable mCommandObserversTable; // hash table of nsIObservers, keyed by command name
nsIDOMWindow* mWindow; // weak ptr. The window should always outlive us
};
#endif // nsCommandManager_h__

Просмотреть файл

@ -0,0 +1,402 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
*/
#include <new.h> // for placement new
#include "nscore.h"
#include "nsCRT.h"
#include "nsCommandParams.h"
// will come from pldhash.h soon
#define PL_DHASH_ENTRY_IS_LIVE(entry) ((entry)->keyHash >= 2)
PLDHashTableOps nsCommandParams::sHashOps =
{
PL_DHashAllocTable,
PL_DHashFreeTable,
HashGetKey,
HashKey,
HashMatchEntry,
HashMoveEntry,
HashClearEntry,
PL_DHashFinalizeStub
};
NS_IMPL_ISUPPORTS1(nsCommandParams, nsICommandParams)
nsCommandParams::nsCommandParams()
: mCurEntry(0)
, mNumEntries(eNumEntriesUnknown)
{
NS_INIT_ISUPPORTS();
// init the hash table later
}
nsCommandParams::~nsCommandParams()
{
PL_DHashTableFinish(&mValuesHash);
}
nsresult
nsCommandParams::Init()
{
if (!PL_DHashTableInit(&mValuesHash, &sHashOps, (void *)this, sizeof(HashEntry), 4))
return NS_ERROR_FAILURE;
return NS_OK;
}
#if 0
#pragma mark -
#endif
/* short getValueType (in string name); */
NS_IMETHODIMP nsCommandParams::GetValueType(const nsAReadableString & name, PRInt16 *_retval)
{
NS_ENSURE_ARG_POINTER(_retval);
*_retval = eNoType;
HashEntry* foundEntry = GetNamedEntry(name);
if (foundEntry)
{
*_retval = foundEntry->mEntryType;
return NS_OK;
}
return NS_ERROR_FAILURE;
}
/* boolean getBooleanValue (in DOMString name); */
NS_IMETHODIMP nsCommandParams::GetBooleanValue(const nsAReadableString & name, PRBool *_retval)
{
NS_ENSURE_ARG_POINTER(_retval);
*_retval = PR_FALSE;
HashEntry* foundEntry = GetNamedEntry(name);
if (foundEntry && foundEntry->mEntryType == eBooleanType)
{
*_retval = foundEntry->mData.mBoolean;
return NS_OK;
}
return NS_ERROR_FAILURE;
}
/* long getLongValue (in DOMString name); */
NS_IMETHODIMP nsCommandParams::GetLongValue(const nsAReadableString & name, PRInt32 *_retval)
{
NS_ENSURE_ARG_POINTER(_retval);
*_retval = PR_FALSE;
HashEntry* foundEntry = GetNamedEntry(name);
if (foundEntry && foundEntry->mEntryType == eLongType)
{
*_retval = foundEntry->mData.mLong;
return NS_OK;
}
return NS_ERROR_FAILURE;
}
/* double getDoubleValue (in DOMString name); */
NS_IMETHODIMP nsCommandParams::GetDoubleValue(const nsAReadableString & name, double *_retval)
{
NS_ENSURE_ARG_POINTER(_retval);
*_retval = 0.0;
HashEntry* foundEntry = GetNamedEntry(name);
if (foundEntry && foundEntry->mEntryType == eDoubleType)
{
*_retval = foundEntry->mData.mDouble;
return NS_OK;
}
return NS_ERROR_FAILURE;
}
/* DOMString getStringValue (in DOMString name); */
NS_IMETHODIMP nsCommandParams::GetStringValue(const nsAReadableString & name, nsAWritableString & _retval)
{
_retval.Truncate();
HashEntry* foundEntry = GetNamedEntry(name);
if (foundEntry && foundEntry->mEntryType == eWStringType)
{
NS_ASSERTION(foundEntry->mString, "Null string");
_retval.Assign(*foundEntry->mString);
return NS_OK;
}
return NS_ERROR_FAILURE;
}
/* nsISupports getISupportsValue (in DOMString name); */
NS_IMETHODIMP nsCommandParams::GetISupportsValue(const nsAReadableString & name, nsISupports **_retval)
{
NS_ENSURE_ARG_POINTER(_retval);
*_retval = nsnull;
HashEntry* foundEntry = GetNamedEntry(name);
if (foundEntry && foundEntry->mEntryType == eISupportsType)
{
NS_IF_ADDREF(*_retval = foundEntry->mISupports.get());
return NS_OK;
}
return NS_ERROR_FAILURE;
}
#if 0
#pragma mark -
#endif
/* void setBooleanValue (in DOMString name, in boolean value); */
NS_IMETHODIMP nsCommandParams::SetBooleanValue(const nsAReadableString & name, PRBool value)
{
HashEntry* foundEntry;
GetOrMakeEntry(name, eBooleanType, foundEntry);
if (!foundEntry) return NS_ERROR_OUT_OF_MEMORY;
foundEntry->mData.mBoolean = value;
return NS_OK;
}
/* void setLongValue (in DOMString name, in long value); */
NS_IMETHODIMP nsCommandParams::SetLongValue(const nsAReadableString & name, PRInt32 value)
{
HashEntry* foundEntry;
GetOrMakeEntry(name, eLongType, foundEntry);
if (!foundEntry) return NS_ERROR_OUT_OF_MEMORY;
foundEntry->mData.mLong = value;
return NS_OK;
}
/* void setDoubleValue (in DOMString name, in double value); */
NS_IMETHODIMP nsCommandParams::SetDoubleValue(const nsAReadableString & name, double value)
{
HashEntry* foundEntry;
GetOrMakeEntry(name, eDoubleType, foundEntry);
if (!foundEntry) return NS_ERROR_OUT_OF_MEMORY;
foundEntry->mData.mDouble = value;
return NS_OK;
}
/* void setStringValue (in DOMString name, in DOMString value); */
NS_IMETHODIMP nsCommandParams::SetStringValue(const nsAReadableString & name, const nsAReadableString & value)
{
HashEntry* foundEntry;
GetOrMakeEntry(name, eWStringType, foundEntry);
if (!foundEntry) return NS_ERROR_OUT_OF_MEMORY;
foundEntry->mString = new nsString(value);
return NS_OK;
}
/* void setISupportsValue (in DOMString name, in nsISupports value); */
NS_IMETHODIMP nsCommandParams::SetISupportsValue(const nsAReadableString & name, nsISupports *value)
{
HashEntry* foundEntry;
GetOrMakeEntry(name, eISupportsType, foundEntry);
if (!foundEntry) return NS_ERROR_OUT_OF_MEMORY;
foundEntry->mISupports = value; // addrefs
return NS_OK;
}
/* void removeValue (in DOMString name); */
NS_IMETHODIMP
nsCommandParams::RemoveValue(const nsAReadableString & name)
{
nsPromiseFlatString flatName = PromiseFlatString(name);
// PL_DHASH_REMOVE doesn't tell us if the entry was really removed, so we return
// NS_OK unconditionally.
(void)PL_DHashTableOperate(&mValuesHash, (void *)flatName.get(), PL_DHASH_REMOVE);
// inval the number of entries
mNumEntries = eNumEntriesUnknown;
return NS_OK;
}
#if 0
#pragma mark -
#endif
nsCommandParams::HashEntry*
nsCommandParams::GetNamedEntry(const nsAReadableString& name)
{
nsPromiseFlatString flatName = PromiseFlatString(name);
HashEntry *foundEntry = (HashEntry *)PL_DHashTableOperate(&mValuesHash, (void *)flatName.get(), PL_DHASH_LOOKUP);
if (PL_DHASH_ENTRY_IS_BUSY(foundEntry))
return foundEntry;
return nsnull;
}
nsCommandParams::HashEntry*
nsCommandParams::GetIndexedEntry(PRInt32 index)
{
HashEntry* entry = NS_REINTERPRET_CAST(HashEntry*, mValuesHash.entryStore);
HashEntry* limit = entry + PR_BIT(mValuesHash.sizeLog2);
PRUint32 entryCount = 0;
do
{
if (!PL_DHASH_ENTRY_IS_LIVE(entry))
continue;
if (entryCount == index)
return entry;
entryCount ++;
} while (++entry < limit);
return nsnull;
}
PRUint32
nsCommandParams::GetNumEntries()
{
HashEntry* entry = NS_REINTERPRET_CAST(HashEntry*, mValuesHash.entryStore);
HashEntry* limit = entry + PR_BIT(mValuesHash.sizeLog2);
PRUint32 entryCount = 0;
do
{
if (PL_DHASH_ENTRY_IS_LIVE(entry))
entryCount ++;
} while (++entry < limit);
return entryCount;
}
nsresult
nsCommandParams::GetOrMakeEntry(const nsAReadableString& name, PRUint8 entryType, HashEntry*& outEntry)
{
nsPromiseFlatString flatName = PromiseFlatString(name);
HashEntry *foundEntry = (HashEntry *)PL_DHashTableOperate(&mValuesHash, (void *)flatName.get(), PL_DHASH_LOOKUP);
if (PL_DHASH_ENTRY_IS_BUSY(foundEntry)) // reuse existing entry
{
foundEntry->Reset(entryType);
foundEntry->mEntryName.Assign(name);
outEntry = foundEntry;
return NS_OK;
}
foundEntry = (HashEntry *)PL_DHashTableOperate(&mValuesHash, (void *)flatName.get(), PL_DHASH_ADD);
if (!foundEntry) return NS_ERROR_OUT_OF_MEMORY;
// placement new that sucker. Our ctor does not clobber keyHash, which is important.
outEntry = new (foundEntry) HashEntry(entryType, name);
return NS_OK;
}
#if 0
#pragma mark -
#endif
const void *
nsCommandParams::HashGetKey(PLDHashTable *table, PLDHashEntryHdr *entry)
{
HashEntry* thisEntry = NS_STATIC_CAST(HashEntry*, entry);
return (void *)thisEntry->mEntryName.get();
}
PLDHashNumber
nsCommandParams::HashKey(PLDHashTable *table, const void *key)
{
return nsCRT::HashCode((const PRUnichar*)key);
}
PRBool
nsCommandParams::HashMatchEntry(PLDHashTable *table,
const PLDHashEntryHdr *entry, const void *key)
{
const PRUnichar* keyString = (const PRUnichar*)key;
const HashEntry* thisEntry = NS_STATIC_CAST(const HashEntry*, entry);
return thisEntry->mEntryName.Equals(keyString);
}
void
nsCommandParams::HashMoveEntry(PLDHashTable *table, const PLDHashEntryHdr *from,
PLDHashEntryHdr *to)
{
const HashEntry* fromEntry = NS_STATIC_CAST(const HashEntry*, from);
HashEntry* toEntry = NS_STATIC_CAST(HashEntry*, to);
*toEntry = *fromEntry;
// we leave from dirty, but that's OK
}
void
nsCommandParams::HashClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry)
{
HashEntry* thisEntry = NS_STATIC_CAST(HashEntry*, entry);
thisEntry->~HashEntry(); // call dtor explicitly
memset(thisEntry, 0, sizeof(HashEntry)); // and clear out
}
#if 0
#pragma mark -
#endif
/* boolean hasMoreElements (); */
NS_IMETHODIMP
nsCommandParams::HasMoreElements(PRBool *_retval)
{
NS_ENSURE_ARG_POINTER(_retval);
if (mNumEntries == eNumEntriesUnknown)
mNumEntries = GetNumEntries();
*_retval = mCurEntry < mNumEntries;
return NS_OK;
}
/* void first (); */
NS_IMETHODIMP
nsCommandParams::First()
{
mCurEntry = 0;
return NS_OK;
}
/* DOMString getNext (); */
NS_IMETHODIMP
nsCommandParams::GetNext(nsAWritableString & _retval)
{
HashEntry* thisEntry = GetIndexedEntry(mCurEntry);
if (!thisEntry)
return NS_ERROR_FAILURE;
_retval.Assign(thisEntry->mEntryName);
mCurEntry++;
return NS_OK;
}

Просмотреть файл

@ -0,0 +1,172 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
*/
#ifndef nsCommandParams_h__
#define nsCommandParams_h__
#include "nsString.h"
#include "nsICommandParams.h"
#include "nsCOMPtr.h"
#include "nsHashtable.h"
#include "pldhash.h"
// {f7fa4581-238e-11d5-a73c-ab64fb68f2bc}
#define NS_COMMAND_PARAMS_CID \
{ 0xf7fa4581, 0x238e, 0x11d5, { 0xa7, 0x3c, 0xab, 0x64, 0xfb, 0x68, 0xf2, 0xbc } }
#define NS_COMMAND_PARAMS_CONTRACTID \
"@mozilla.org/embedcomp/command-params;1"
class nsCommandParams : public nsICommandParams
{
public:
nsCommandParams();
virtual ~nsCommandParams();
NS_DECL_ISUPPORTS
NS_DECL_NSICOMMANDPARAMS
nsresult Init();
protected:
struct HashEntry : public PLDHashEntryHdr
{
nsString mEntryName;
PRUint8 mEntryType;
union {
PRBool mBoolean;
PRInt32 mLong;
double mDouble;
} mData;
// put these outside the union to avoid clobbering other fields
nsString* mString;
nsCOMPtr<nsISupports> mISupports;
HashEntry(PRUint8 inType, const nsAReadableString& inEntryName)
: mEntryType(inType)
, mEntryName(inEntryName)
, mString(nsnull)
{
mData.mDouble = 0.0;
Reset(mEntryType);
}
HashEntry(const HashEntry& inRHS)
: mEntryType(inRHS.mEntryType)
{
Reset(mEntryType);
switch (mEntryType)
{
case eBooleanType: mData.mBoolean = inRHS.mData.mBoolean; break;
case eLongType: mData.mLong = inRHS.mData.mLong; break;
case eDoubleType: mData.mDouble = inRHS.mData.mDouble; break;
case eWStringType:
NS_ASSERTION(inRHS.mString, "Source entry has no string");
mString = new nsString(*inRHS.mString);
break;
case eISupportsType:
mISupports = inRHS.mISupports.get(); // additional addref
break;
default:
NS_ASSERTION(0, "Unknown type");
}
}
~HashEntry()
{
if (mEntryType == eWStringType)
delete mString;
}
void Reset(PRUint8 inNewType)
{
switch (mEntryType)
{
case eNoType: break;
case eBooleanType: mData.mBoolean = PR_FALSE; break;
case eLongType: mData.mLong = 0; break;
case eDoubleType: mData.mDouble = 0.0; break;
case eWStringType: delete mString; mString = nsnull; break;
case eISupportsType: mISupports = nsnull; break; // clear the nsCOMPtr
default:
NS_ASSERTION(0, "Unknown type");
}
mEntryType = inNewType;
}
};
HashEntry* GetNamedEntry(const nsAReadableString& name);
HashEntry* GetIndexedEntry(PRInt32 index);
PRUint32 GetNumEntries();
nsresult GetOrMakeEntry(const nsAReadableString& name, PRUint8 entryType, HashEntry*& outEntry);
protected:
static const void * HashGetKey(PLDHashTable *table, PLDHashEntryHdr *entry);
static PLDHashNumber HashKey(PLDHashTable *table, const void *key);
static PRBool HashMatchEntry(PLDHashTable *table,
const PLDHashEntryHdr *entry, const void *key);
static void HashMoveEntry(PLDHashTable *table, const PLDHashEntryHdr *from,
PLDHashEntryHdr *to);
static void HashClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry);
protected:
enum {
eNumEntriesUnknown = -1
};
// this is going to have to use a pldhash, because we need to
// be able to iterate through entries, passing names back
// to the caller, which means that we need to store the names
// internally.
PLDHashTable mValuesHash;
// enumerator data
PRInt32 mCurEntry;
PRInt32 mNumEntries; // number of entries at start of enumeration (-1 indicates not known)
static PLDHashTableOps sHashOps;
};
#endif // nsCommandParams_h__

Просмотреть файл

@ -0,0 +1,204 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
#include "nsString.h"
#include "nsControllerCommandManager.h"
// prototype;
nsresult
NS_NewControllerCommandManager(nsIControllerCommandManager** aResult);
// this value is used to size the hash table. Just a sensible upper bound
#define NUM_COMMANDS_BOUNDS 64
nsControllerCommandManager::nsControllerCommandManager()
: mCommandsTable(NUM_COMMANDS_BOUNDS, PR_FALSE)
{
NS_INIT_REFCNT();
}
nsControllerCommandManager::~nsControllerCommandManager()
{
}
NS_IMPL_ISUPPORTS2(nsControllerCommandManager, nsIControllerCommandManager, nsISupportsWeakReference);
NS_IMETHODIMP
nsControllerCommandManager::RegisterCommand(const nsAReadableString & aCommandName, nsIControllerCommand *aCommand)
{
nsStringKey commandKey(aCommandName);
if (mCommandsTable.Put (&commandKey, aCommand))
{
#if DEBUG
nsCAutoString msg("Replacing existing command -- ");
msg.AppendWithConversion(aCommandName);
NS_WARNING(msg);
#endif
}
return NS_OK;
}
NS_IMETHODIMP
nsControllerCommandManager::UnregisterCommand(const nsAReadableString & aCommandName, nsIControllerCommand *aCommand)
{
nsStringKey commandKey(aCommandName);
PRBool wasRemoved = mCommandsTable.Remove (&commandKey);
return wasRemoved ? NS_OK : NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsControllerCommandManager::FindCommandHandler(const nsAReadableString & aCommandName, nsIControllerCommand **outCommand)
{
NS_ENSURE_ARG_POINTER(outCommand);
*outCommand = NULL;
nsStringKey commandKey(aCommandName);
nsISupports* foundCommand = mCommandsTable.Get(&commandKey); // this does the addref
if (!foundCommand) return NS_ERROR_FAILURE;
*outCommand = NS_REINTERPRET_CAST(nsIControllerCommand*, foundCommand);
return NS_OK;
}
/* boolean isCommandEnabled (in wstring command); */
NS_IMETHODIMP
nsControllerCommandManager::IsCommandEnabled(const nsAReadableString & aCommandName, nsISupports *aCommandRefCon, PRBool *aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
*aResult = PR_FALSE;
// find the command
nsCOMPtr<nsIControllerCommand> commandHandler;
FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
if (!commandHandler)
{
#if DEBUG
nsCAutoString msg("Controller command manager asked about a command that it does not handle -- ");
msg.AppendWithConversion(aCommandName);
NS_WARNING(msg);
#endif
return NS_OK; // we don't handle this command
}
return commandHandler->IsCommandEnabled(aCommandName, aCommandRefCon, aResult);
}
NS_IMETHODIMP
nsControllerCommandManager::UpdateCommandState(const nsAReadableString & aCommandName, nsISupports *aCommandRefCon)
{
// find the command
nsCOMPtr<nsIControllerCommand> commandHandler;
FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
if (!commandHandler)
{
#if DEBUG
nsCAutoString msg("Controller command manager asked to update the state of a command that it does not handle -- ");
msg.AppendWithConversion(aCommandName);
NS_WARNING(msg);
#endif
return NS_OK; // we don't handle this command
}
nsCOMPtr<nsIStateUpdatingControllerCommand> stateCommand = do_QueryInterface(commandHandler);
if (!stateCommand)
{
#if DEBUG
nsCAutoString msg("Controller command manager asked to update the state of a command that doesn't do state updating -- ");
msg.AppendWithConversion(aCommandName);
NS_WARNING(msg);
#endif
return NS_ERROR_NO_INTERFACE;
}
return stateCommand->UpdateCommandState(aCommandName, aCommandRefCon);
}
NS_IMETHODIMP
nsControllerCommandManager::SupportsCommand(const nsAReadableString & aCommandName, nsISupports *aCommandRefCon, PRBool *aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
// XXX: need to check the readonly and disabled states
*aResult = PR_FALSE;
// find the command
nsCOMPtr<nsIControllerCommand> commandHandler;
FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
*aResult = (commandHandler.get() != nsnull);
return NS_OK;
}
/* void doCommand (in wstring command); */
NS_IMETHODIMP
nsControllerCommandManager::DoCommand(const nsAReadableString & aCommandName, nsISupports *aCommandRefCon)
{
// find the command
nsCOMPtr<nsIControllerCommand> commandHandler;
FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
if (!commandHandler)
{
#if DEBUG
nsCAutoString msg("Controller command manager asked to do a command that it does not handle -- ");
msg.AppendWithConversion(aCommandName);
NS_WARNING(msg);
#endif
return NS_OK; // we don't handle this command
}
return commandHandler->DoCommand(aCommandName, aCommandRefCon);
}
nsresult
NS_NewControllerCommandManager(nsIControllerCommandManager** aResult)
{
NS_PRECONDITION(aResult != nsnull, "null ptr");
if (! aResult)
return NS_ERROR_NULL_POINTER;
nsControllerCommandManager* newCommandManager = new nsControllerCommandManager();
if (! newCommandManager)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(newCommandManager);
*aResult = newCommandManager;
return NS_OK;
}

Просмотреть файл

@ -0,0 +1,51 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
#ifndef nsControllerCommandManager_h_
#define nsControllerCommandManager_h_
#include "nsIControllerCommand.h"
#include "nsWeakReference.h"
#include "nsHashtable.h"
class nsControllerCommandManager : public nsIControllerCommandManager,
public nsSupportsWeakReference
{
public:
nsControllerCommandManager();
virtual ~nsControllerCommandManager();
NS_DECL_ISUPPORTS
NS_DECL_NSICONTROLLERCOMMANDMANAGER
protected:
nsSupportsHashtable mCommandsTable; // hash table of nsIControllerCommands, keyed by command name
};
#endif // nsControllerCommandManager_h_