2000-02-05 09:19:49 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2001-09-29 00:14:13 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
2000-02-05 09:19:49 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* 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.
|
2000-02-05 09:19:49 +03:00
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2000
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2000-02-05 09:19:49 +03:00
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Scott Collins <scc@netscape.com>
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2000-02-05 09:19:49 +03:00
|
|
|
|
|
|
|
#include "nsICategoryManager.h"
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsHashtable.h"
|
2000-02-06 05:01:39 +03:00
|
|
|
#include "nsIFactory.h"
|
2000-02-05 09:19:49 +03:00
|
|
|
#include "nsIRegistry.h"
|
2002-06-15 01:33:05 +04:00
|
|
|
#include "nsSupportsPrimitives.h"
|
2001-10-16 07:35:52 +04:00
|
|
|
#include "nsIObserver.h"
|
2000-02-05 09:19:49 +03:00
|
|
|
#include "nsComponentManager.h"
|
2001-10-24 13:06:49 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2000-02-05 09:19:49 +03:00
|
|
|
|
|
|
|
#include "nsHashtableEnumerator.h"
|
|
|
|
#include "nsEnumeratorUtils.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
CategoryDatabase
|
|
|
|
contains 0 or more 1-1 mappings of string to Category
|
|
|
|
each Category contains 0 or more 1-1 mappings of string keys to string values
|
|
|
|
|
|
|
|
In other words, the CategoryDatabase is a tree, whose root is a hashtable.
|
|
|
|
Internal nodes (or Categories) are hashtables.
|
|
|
|
Leaf nodes are strings.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ExtractKeyString( nsHashKey* key, void*, void*, nsISupports** _retval )
|
2000-08-10 10:19:37 +04:00
|
|
|
/*
|
|
|
|
...works with |nsHashtableEnumerator| to make the hash keys enumerable.
|
|
|
|
*/
|
|
|
|
{
|
2002-06-15 07:58:10 +04:00
|
|
|
nsresult status = NS_ERROR_FAILURE;
|
2002-06-15 01:33:05 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsString> obj = new nsSupportsStringImpl();
|
2000-08-10 10:19:37 +04:00
|
|
|
if ( obj ) {
|
|
|
|
nsCStringKey* strkey = NS_STATIC_CAST(nsCStringKey*, key);
|
|
|
|
status = obj->SetDataWithLength(strkey->GetStringLength(), strkey->GetString());
|
2000-02-05 09:19:49 +03:00
|
|
|
}
|
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
*_retval = obj;
|
|
|
|
NS_IF_ADDREF(*_retval);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2000-02-05 09:19:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
typedef nsCString LeafNode;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Our interior nodes are hashtables whose elements are |LeafNode|s,
|
|
|
|
and we need a suitable destruction function to register with a
|
|
|
|
given (interior node) hashtable for destroying its (|LeafNode|) elements.
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
PRBool
|
|
|
|
Destroy_LeafNode( nsHashKey*, void* aElement, void* )
|
|
|
|
{
|
|
|
|
delete NS_STATIC_CAST(LeafNode*, aElement);
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class CategoryNode
|
|
|
|
: public nsObjectHashtable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CategoryNode()
|
2000-05-12 05:09:58 +04:00
|
|
|
: nsObjectHashtable((nsHashtableCloneElementFunc) 0, 0,
|
|
|
|
(nsHashtableEnumFunc) Destroy_LeafNode, 0 )
|
2000-02-05 09:19:49 +03:00
|
|
|
{
|
|
|
|
// Nothing else to do here...
|
|
|
|
}
|
|
|
|
|
|
|
|
LeafNode* find_leaf( const char* );
|
|
|
|
};
|
|
|
|
|
|
|
|
LeafNode*
|
|
|
|
CategoryNode::find_leaf( const char* aLeafName )
|
|
|
|
{
|
2000-08-10 10:19:37 +04:00
|
|
|
nsCStringKey leafNameKey(aLeafName);
|
2000-02-05 09:19:49 +03:00
|
|
|
return NS_STATIC_CAST(LeafNode*, Get(&leafNameKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
We keep a hashtable of hashtables, therefore, we need a suitable
|
|
|
|
destruction function to register with the outer table for destroying
|
|
|
|
elements which are the inner tables.
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
PRBool
|
|
|
|
Destroy_CategoryNode( nsHashKey*, void* aElement, void* )
|
|
|
|
{
|
|
|
|
delete NS_STATIC_CAST(CategoryNode*, aElement);
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class nsCategoryManager
|
|
|
|
: public nsICategoryManager,
|
|
|
|
public nsObjectHashtable
|
|
|
|
{
|
2000-02-06 11:26:13 +03:00
|
|
|
private:
|
|
|
|
friend class nsCategoryManagerFactory;
|
2000-02-05 09:19:49 +03:00
|
|
|
nsCategoryManager();
|
2000-02-06 11:26:13 +03:00
|
|
|
|
|
|
|
public:
|
2000-02-13 20:32:19 +03:00
|
|
|
virtual ~nsCategoryManager();
|
2000-02-05 09:19:49 +03:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSICATEGORYMANAGER
|
|
|
|
|
|
|
|
private:
|
|
|
|
CategoryNode* find_category( const char* );
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsCategoryManager, nsICategoryManager)
|
|
|
|
|
|
|
|
nsCategoryManager::nsCategoryManager()
|
2000-05-12 05:09:58 +04:00
|
|
|
: nsObjectHashtable((nsHashtableCloneElementFunc) 0, 0,
|
|
|
|
(nsHashtableEnumFunc) Destroy_CategoryNode, 0 )
|
|
|
|
|
2000-02-05 09:19:49 +03:00
|
|
|
{
|
2000-02-06 05:01:39 +03:00
|
|
|
NS_INIT_REFCNT();
|
2000-02-05 09:19:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCategoryManager::~nsCategoryManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CategoryNode*
|
|
|
|
nsCategoryManager::find_category( const char* aCategoryName )
|
|
|
|
{
|
2000-08-10 10:19:37 +04:00
|
|
|
nsCStringKey categoryNameKey(aCategoryName);
|
2000-02-05 09:19:49 +03:00
|
|
|
return NS_STATIC_CAST(CategoryNode*, Get(&categoryNameKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCategoryManager::GetCategoryEntry( const char *aCategoryName,
|
|
|
|
const char *aEntryName,
|
|
|
|
char **_retval )
|
|
|
|
{
|
2000-02-05 10:42:30 +03:00
|
|
|
// BULLSHIT ALERT: Category `handler's currently not implemented, so just call through
|
2000-02-05 09:19:49 +03:00
|
|
|
return GetCategoryEntryRaw(aCategoryName, aEntryName, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCategoryManager::GetCategoryEntryRaw( const char *aCategoryName,
|
|
|
|
const char *aEntryName,
|
|
|
|
char **_retval )
|
|
|
|
{
|
2000-02-05 10:42:30 +03:00
|
|
|
NS_ASSERTION(aCategoryName, "aCategoryName is NULL!");
|
|
|
|
NS_ASSERTION(aEntryName, "aEntryName is NULL!");
|
|
|
|
NS_ASSERTION(_retval, "_retval is NULL!");
|
2000-02-05 09:19:49 +03:00
|
|
|
|
|
|
|
nsresult status = NS_ERROR_NOT_AVAILABLE;
|
2000-02-08 06:34:27 +03:00
|
|
|
CategoryNode* category = find_category(aCategoryName);
|
|
|
|
if (category)
|
2000-02-05 09:19:49 +03:00
|
|
|
{
|
2000-08-10 10:19:37 +04:00
|
|
|
nsCStringKey entryKey(aEntryName);
|
2000-02-08 06:34:27 +03:00
|
|
|
LeafNode* entry = NS_STATIC_CAST(LeafNode*, category->Get(&entryKey));
|
|
|
|
if (entry)
|
2001-10-24 13:06:49 +04:00
|
|
|
status = (*_retval = ToNewCString(*entry)) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
2000-02-05 09:19:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCategoryManager::AddCategoryEntry( const char *aCategoryName,
|
|
|
|
const char *aEntryName,
|
|
|
|
const char *aValue,
|
|
|
|
PRBool aPersist,
|
|
|
|
PRBool aReplace,
|
|
|
|
char **_retval )
|
|
|
|
{
|
2000-02-05 10:42:30 +03:00
|
|
|
NS_ASSERTION(aCategoryName, "aCategoryName is NULL!");
|
|
|
|
NS_ASSERTION(aEntryName, "aEntryName is NULL!");
|
|
|
|
NS_ASSERTION(aValue, "aValue is NULL!");
|
|
|
|
|
|
|
|
|
2000-02-05 10:58:56 +03:00
|
|
|
/*
|
|
|
|
Note: if |_retval| is |NULL|, I won't bother returning a copy
|
|
|
|
of the replaced value.
|
|
|
|
*/
|
2000-02-05 10:42:30 +03:00
|
|
|
|
2000-02-05 10:58:56 +03:00
|
|
|
if ( _retval )
|
|
|
|
*_retval = 0;
|
2000-02-05 10:42:30 +03:00
|
|
|
|
|
|
|
|
2000-02-06 11:26:13 +03:00
|
|
|
// Before we can insert a new entry, we'll need to
|
|
|
|
// find the |CategoryNode| to put it in...
|
2000-02-05 09:19:49 +03:00
|
|
|
CategoryNode* category;
|
|
|
|
if ( !(category = find_category(aCategoryName)) )
|
|
|
|
{
|
2000-02-06 11:26:13 +03:00
|
|
|
// That category doesn't exist yet; let's make it.
|
2000-02-05 10:58:56 +03:00
|
|
|
category = new CategoryNode;
|
2000-08-10 10:19:37 +04:00
|
|
|
nsCStringKey categoryNameKey(aCategoryName);
|
2000-02-05 10:58:56 +03:00
|
|
|
Put(&categoryNameKey, category);
|
2000-02-05 09:19:49 +03:00
|
|
|
}
|
|
|
|
|
2000-02-06 11:26:13 +03:00
|
|
|
// See if this entry is already in this category
|
2000-02-05 09:19:49 +03:00
|
|
|
LeafNode* entry = category->find_leaf(aEntryName);
|
|
|
|
|
2000-02-06 11:26:13 +03:00
|
|
|
nsresult status = NS_OK;
|
2000-02-05 10:58:56 +03:00
|
|
|
if ( entry )
|
|
|
|
{
|
2000-02-06 11:26:13 +03:00
|
|
|
// If this entry is in the category already,
|
|
|
|
// then you better have said 'replace'!
|
|
|
|
|
2000-02-05 10:58:56 +03:00
|
|
|
if ( aReplace )
|
|
|
|
{
|
2000-02-06 11:26:13 +03:00
|
|
|
// return the value that we're replacing
|
2000-02-05 10:58:56 +03:00
|
|
|
if ( _retval )
|
2001-10-24 13:06:49 +04:00
|
|
|
*_retval = ToNewCString(*entry);
|
2000-02-05 10:58:56 +03:00
|
|
|
}
|
|
|
|
else
|
2000-08-31 15:42:43 +04:00
|
|
|
status = NS_ERROR_INVALID_ARG; // ...stops us from putting the value in
|
2000-02-05 10:58:56 +03:00
|
|
|
}
|
|
|
|
|
2000-08-31 15:42:43 +04:00
|
|
|
// If you didn't say 'replace', and there was already an entry there,
|
|
|
|
// then we can't put your value in (that's why we set |status|, above),
|
|
|
|
// or make it persistent (see below)
|
|
|
|
|
2000-02-06 11:26:13 +03:00
|
|
|
if ( NS_SUCCEEDED(status) )
|
2000-08-31 15:42:43 +04:00
|
|
|
{ // it's OK to put a value in
|
|
|
|
|
|
|
|
// don't leak the entry we're replacing (if any)
|
|
|
|
delete entry;
|
|
|
|
|
|
|
|
// now put in the new vaentrylue
|
2000-02-06 11:26:13 +03:00
|
|
|
entry = new LeafNode(aValue);
|
2000-08-10 10:19:37 +04:00
|
|
|
nsCStringKey entryNameKey(aEntryName);
|
2000-02-06 11:26:13 +03:00
|
|
|
category->Put(&entryNameKey, entry);
|
|
|
|
|
2002-06-15 01:33:05 +04:00
|
|
|
// this function is not public yet, hence it is externed here.
|
|
|
|
extern nsresult NS_GetComponentLoaderManager(nsIComponentLoaderManager* *result);
|
|
|
|
nsCOMPtr<nsIComponentLoaderManager> mgr;
|
|
|
|
NS_GetComponentLoaderManager(getter_AddRefs(mgr));
|
|
|
|
if (mgr)
|
|
|
|
mgr->FlushPersistentStore(PR_FALSE);
|
2000-02-06 11:26:13 +03:00
|
|
|
}
|
2000-02-05 09:19:49 +03:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCategoryManager::DeleteCategoryEntry( const char *aCategoryName,
|
|
|
|
const char *aEntryName,
|
2001-06-06 01:25:57 +04:00
|
|
|
PRBool aDontPersist)
|
2000-02-05 09:19:49 +03:00
|
|
|
{
|
2000-02-06 08:43:38 +03:00
|
|
|
|
2000-02-05 10:42:30 +03:00
|
|
|
NS_ASSERTION(aCategoryName, "aCategoryName is NULL!");
|
|
|
|
NS_ASSERTION(aEntryName, "aEntryName is NULL!");
|
|
|
|
|
|
|
|
/*
|
|
|
|
Note: no errors are reported since failure to delete
|
|
|
|
probably won't hurt you, and returning errors seriously
|
|
|
|
inconveniences JS clients
|
|
|
|
*/
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2000-02-08 06:34:27 +03:00
|
|
|
CategoryNode* category = find_category(aCategoryName);
|
|
|
|
if (category)
|
2000-02-05 09:19:49 +03:00
|
|
|
{
|
2000-08-10 10:19:37 +04:00
|
|
|
nsCStringKey entryKey(aEntryName);
|
2000-02-05 10:42:30 +03:00
|
|
|
category->RemoveAndDelete(&entryKey);
|
2000-02-05 09:19:49 +03:00
|
|
|
}
|
|
|
|
|
2002-06-15 01:33:05 +04:00
|
|
|
return NS_OK;
|
2000-02-05 09:19:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCategoryManager::DeleteCategory( const char *aCategoryName )
|
|
|
|
{
|
2000-02-05 10:42:30 +03:00
|
|
|
NS_ASSERTION(aCategoryName, "aCategoryName is NULL!");
|
|
|
|
|
|
|
|
// QUESTION: consider whether this should be an error
|
2000-08-10 10:19:37 +04:00
|
|
|
nsCStringKey categoryKey(aCategoryName);
|
2000-02-05 09:19:49 +03:00
|
|
|
return RemoveAndDelete(&categoryKey) ? NS_OK : NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCategoryManager::EnumerateCategory( const char *aCategoryName,
|
|
|
|
nsISimpleEnumerator **_retval )
|
|
|
|
{
|
2000-02-05 10:42:30 +03:00
|
|
|
NS_ASSERTION(aCategoryName, "aCategoryName is NULL!");
|
|
|
|
NS_ASSERTION(_retval, "_retval is NULL!");
|
2000-02-05 09:19:49 +03:00
|
|
|
|
|
|
|
*_retval = 0;
|
|
|
|
|
|
|
|
nsresult status = NS_ERROR_NOT_AVAILABLE;
|
2000-02-08 06:34:27 +03:00
|
|
|
CategoryNode* category = find_category(aCategoryName);
|
|
|
|
if (category)
|
2000-02-05 09:19:49 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEnumerator> innerEnumerator;
|
|
|
|
if ( NS_SUCCEEDED(status = NS_NewHashtableEnumerator(category, ExtractKeyString, 0, getter_AddRefs(innerEnumerator))) )
|
|
|
|
status = NS_NewAdapterEnumerator(_retval, innerEnumerator);
|
|
|
|
}
|
|
|
|
|
2000-02-06 11:26:13 +03:00
|
|
|
// If you couldn't find the category, or had trouble creating an enumerator...
|
2001-11-19 09:45:00 +03:00
|
|
|
if ( NS_FAILED(status) )
|
2000-02-06 11:26:13 +03:00
|
|
|
{
|
|
|
|
NS_IF_RELEASE(*_retval);
|
|
|
|
status = NS_NewEmptyEnumerator(_retval);
|
|
|
|
}
|
2000-02-05 10:42:30 +03:00
|
|
|
|
2000-02-05 09:19:49 +03:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-15 01:33:05 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCategoryManager::EnumerateCategories(nsISimpleEnumerator **_retval)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(_retval, "_retval is NULL!");
|
|
|
|
*_retval = 0;
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2002-06-15 01:33:05 +04:00
|
|
|
nsresult status = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
nsCOMPtr<nsIEnumerator> innerEnumerator;
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2002-06-15 01:33:05 +04:00
|
|
|
if ( NS_SUCCEEDED(status = NS_NewHashtableEnumerator(this, ExtractKeyString, 0, getter_AddRefs(innerEnumerator))))
|
|
|
|
status = NS_NewAdapterEnumerator(_retval, innerEnumerator);
|
|
|
|
|
|
|
|
// If you couldn't find the category, or had trouble creating an enumerator...
|
|
|
|
if ( NS_FAILED(status) )
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(*_retval);
|
|
|
|
status = NS_NewEmptyEnumerator(_retval);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
2000-02-05 09:19:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCategoryManager::RegisterCategoryHandler( const char* /* aCategoryName */,
|
|
|
|
nsICategoryHandler* /* aHandler */,
|
|
|
|
PRInt32 /* aMode */,
|
|
|
|
nsICategoryHandler** /* _retval */ )
|
|
|
|
{
|
2000-02-05 10:42:30 +03:00
|
|
|
// BULLSHIT ALERT: Category `handler's currently not implemented
|
2000-02-05 09:19:49 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCategoryManager::UnregisterCategoryHandler( const char *category,
|
|
|
|
nsICategoryHandler *handler,
|
|
|
|
nsICategoryHandler *previous )
|
|
|
|
{
|
2000-02-05 10:42:30 +03:00
|
|
|
// BULLSHIT ALERT: Category `handler's currently not implemented.
|
2000-02-05 09:19:49 +03:00
|
|
|
// Wasn't implemented in the JS version either.
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2000-02-06 05:01:39 +03:00
|
|
|
|
|
|
|
class nsCategoryManagerFactory : public nsIFactory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsCategoryManagerFactory();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIFACTORY
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsCategoryManagerFactory, nsIFactory)
|
|
|
|
|
|
|
|
nsCategoryManagerFactory::nsCategoryManagerFactory()
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCategoryManagerFactory::CreateInstance( nsISupports* aOuter, const nsIID& aIID, void** aResult )
|
|
|
|
{
|
|
|
|
// assert(aResult);
|
|
|
|
|
|
|
|
*aResult = 0;
|
|
|
|
|
|
|
|
nsresult status = NS_OK;
|
|
|
|
if ( aOuter )
|
|
|
|
status = NS_ERROR_NO_AGGREGATION;
|
|
|
|
else
|
|
|
|
{
|
2000-02-06 11:26:13 +03:00
|
|
|
nsCategoryManager* raw_category_manager;
|
|
|
|
nsCOMPtr<nsICategoryManager> new_category_manager = (raw_category_manager = new nsCategoryManager);
|
|
|
|
if ( new_category_manager )
|
|
|
|
status = new_category_manager->QueryInterface(aIID, aResult);
|
|
|
|
else
|
|
|
|
status = NS_ERROR_OUT_OF_MEMORY;
|
2000-02-06 05:01:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCategoryManagerFactory::LockFactory( PRBool )
|
|
|
|
{
|
|
|
|
// Not implemented...
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
NS_EXPORT
|
|
|
|
nsresult
|
|
|
|
NS_CategoryManagerGetFactory( nsIFactory** aFactory )
|
|
|
|
{
|
|
|
|
// assert(aFactory);
|
|
|
|
|
|
|
|
nsresult status;
|
|
|
|
|
|
|
|
*aFactory = 0;
|
2000-02-08 06:34:27 +03:00
|
|
|
nsIFactory* new_factory = NS_STATIC_CAST(nsIFactory*, new nsCategoryManagerFactory);
|
|
|
|
if (new_factory)
|
2000-02-06 05:01:39 +03:00
|
|
|
{
|
|
|
|
*aFactory = new_factory;
|
|
|
|
NS_ADDREF(*aFactory);
|
|
|
|
status = NS_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2001-10-16 07:35:52 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CreateServicesFromCategory()
|
|
|
|
*
|
|
|
|
* Given a category, this convenience functions enumerates the category and
|
|
|
|
* creates a service of every CID or ContractID registered under the category.
|
|
|
|
* If observerTopic is non null and the service implements nsIObserver,
|
|
|
|
* this will attempt to notify the observer with the origin, observerTopic string
|
|
|
|
* as parameter.
|
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
NS_CreateServicesFromCategory(const char *category,
|
|
|
|
nsISupports *origin,
|
2001-10-20 00:52:59 +04:00
|
|
|
const char *observerTopic)
|
2001-10-16 07:35:52 +04:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
int nFailed = 0;
|
|
|
|
nsCOMPtr<nsICategoryManager> categoryManager =
|
|
|
|
do_GetService("@mozilla.org/categorymanager;1", &rv);
|
|
|
|
if (!categoryManager) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> enumerator;
|
|
|
|
rv = categoryManager->EnumerateCategory(category,
|
|
|
|
getter_AddRefs(enumerator));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> entry;
|
|
|
|
while (NS_SUCCEEDED(enumerator->GetNext(getter_AddRefs(entry)))) {
|
|
|
|
// From here on just skip any error we get.
|
|
|
|
nsCOMPtr<nsISupportsString> catEntry = do_QueryInterface(entry, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
nFailed++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nsXPIDLCString entryString;
|
|
|
|
rv = catEntry->GetData(getter_Copies(entryString));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
nFailed++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nsXPIDLCString contractID;
|
|
|
|
rv = categoryManager->GetCategoryEntry(category,(const char *)entryString, getter_Copies(contractID));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
nFailed++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> instance = do_GetService(contractID, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
nFailed++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (observerTopic) {
|
|
|
|
// try an observer, if it implements it.
|
|
|
|
nsCOMPtr<nsIObserver> observer = do_QueryInterface(instance, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv) && observer)
|
|
|
|
observer->Observe(origin, observerTopic, NS_LITERAL_STRING("").get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (nFailed ? NS_ERROR_FAILURE : NS_OK);
|
|
|
|
}
|