1999-05-18 00:16:22 +04:00
|
|
|
/* -*- 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.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsIFactory.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsAbBaseCID.h"
|
|
|
|
#include "pratom.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "rdf.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Include all of the interfaces our factory can generate components for */
|
|
|
|
|
|
|
|
#include "nsDirectoryDataSource.h"
|
|
|
|
#include "nsCardDataSource.h"
|
|
|
|
#include "nsAbDirectory.h"
|
|
|
|
#include "nsAbCard.h"
|
1999-06-16 04:04:49 +04:00
|
|
|
#include "nsAddrDatabase.h"
|
1999-07-02 00:04:04 +04:00
|
|
|
#include "nsAddressBook.h"
|
1999-07-07 23:06:40 +04:00
|
|
|
#include "nsAddrBookSession.h"
|
1999-05-18 00:16:22 +04:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
|
|
|
|
|
1999-07-02 00:04:04 +04:00
|
|
|
static NS_DEFINE_CID(kAddressBookCID, NS_ADDRESSBOOK_CID);
|
1999-05-18 00:16:22 +04:00
|
|
|
static NS_DEFINE_CID(kAbDirectoryDataSourceCID, NS_ABDIRECTORYDATASOURCE_CID);
|
|
|
|
static NS_DEFINE_CID(kAbDirectoryCID, NS_ABDIRECTORYRESOURCE_CID);
|
|
|
|
static NS_DEFINE_CID(kAbCardDataSourceCID, NS_ABCARDDATASOURCE_CID);
|
|
|
|
static NS_DEFINE_CID(kAbCardCID, NS_ABCARDRESOURCE_CID);
|
1999-06-16 04:04:49 +04:00
|
|
|
static NS_DEFINE_CID(kAddressBookDB, NS_ADDRESSBOOKDB_CID);
|
1999-07-02 00:04:04 +04:00
|
|
|
static NS_DEFINE_CID(kAbCardPropertyCID, NS_ABCARDPROPERTY_CID);
|
1999-07-07 23:06:40 +04:00
|
|
|
static NS_DEFINE_CID(kAddrBookSessionCID, NS_ADDRBOOKSESSION_CID);
|
1999-05-18 00:16:22 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
static PRInt32 g_InstanceCount = 0;
|
|
|
|
static PRInt32 g_LockCount = 0;
|
|
|
|
|
|
|
|
class nsAbFactory : public nsIFactory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// nsISupports methods
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsAbFactory(const nsCID &aClass, const char* aClassName, const char* aProgID);
|
|
|
|
|
|
|
|
// nsIFactory methods
|
|
|
|
NS_IMETHOD CreateInstance(nsISupports *aOuter, const nsIID &aIID, void **aResult);
|
|
|
|
NS_IMETHOD LockFactory(PRBool aLock);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~nsAbFactory();
|
|
|
|
|
|
|
|
nsCID mClassID;
|
|
|
|
char* mClassName;
|
|
|
|
char* mProgID;
|
|
|
|
|
|
|
|
// nsIServiceManager* mServiceManager;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsAbFactory::nsAbFactory(const nsCID &aClass, const char* aClassName, const char* aProgID)
|
|
|
|
: mClassID(aClass), mClassName(nsCRT::strdup(aClassName)), mProgID(nsCRT::strdup(aProgID))
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
// store a copy of the
|
|
|
|
// compMgrSupports->QueryInterface(nsIServiceManager::GetIID(),
|
|
|
|
// (void **)&mServiceManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAbFactory::~nsAbFactory()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mRefCnt == 0, "non-zero refcnt at destruction");
|
|
|
|
// NS_IF_RELEASE(mServiceManager);
|
|
|
|
PL_strfree(mClassName);
|
|
|
|
PL_strfree(mProgID);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsAbFactory::QueryInterface(const nsIID &aIID, void **aResult)
|
|
|
|
{
|
|
|
|
if (aResult == NULL)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// Always NULL result, in case of failure
|
|
|
|
*aResult = NULL;
|
|
|
|
|
|
|
|
// we support two interfaces....nsISupports and nsFactory.....
|
1999-06-30 01:01:31 +04:00
|
|
|
if (aIID.Equals(nsCOMTypeInfo<nsISupports>::GetIID()))
|
1999-05-18 00:16:22 +04:00
|
|
|
*aResult = (void *)(nsISupports*)this;
|
1999-07-01 04:15:20 +04:00
|
|
|
else if (aIID.Equals(nsCOMTypeInfo<nsIFactory>::GetIID()))
|
1999-05-18 00:16:22 +04:00
|
|
|
*aResult = (void *)(nsIFactory*)this;
|
|
|
|
|
|
|
|
if (*aResult == NULL)
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
|
|
|
|
AddRef(); // Increase reference count for caller
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsAbFactory)
|
|
|
|
NS_IMPL_RELEASE(nsAbFactory)
|
|
|
|
|
|
|
|
nsresult nsAbFactory::CreateInstance(nsISupports *aOuter, const nsIID &aIID, void **aResult)
|
|
|
|
{
|
|
|
|
if (aResult == NULL)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
*aResult = NULL;
|
|
|
|
|
|
|
|
// ClassID check happens here
|
|
|
|
// Whenever you add a new class that supports an interface, plug it in here!!!
|
|
|
|
|
1999-07-02 00:04:04 +04:00
|
|
|
if (mClassID.Equals(kAddressBookCID))
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsAddressBook * addressBook = new nsAddressBook();
|
|
|
|
if (addressBook)
|
|
|
|
rv = addressBook->QueryInterface(aIID, aResult);
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) && addressBook)
|
|
|
|
delete addressBook;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
else if (mClassID.Equals(kAbDirectoryDataSourceCID))
|
1999-05-18 00:16:22 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsABDirectoryDataSource * directoryDataSource = new nsABDirectoryDataSource();
|
|
|
|
if (directoryDataSource)
|
|
|
|
rv = directoryDataSource->QueryInterface(aIID, aResult);
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) && directoryDataSource)
|
|
|
|
delete directoryDataSource;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
else if (mClassID.Equals(kAbDirectoryCID))
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsABDirectory * directory = new nsABDirectory();
|
|
|
|
if (directory)
|
|
|
|
rv = directory->QueryInterface(aIID, aResult);
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) && directory)
|
|
|
|
delete directory;
|
|
|
|
return rv;
|
|
|
|
}
|
1999-07-02 00:04:04 +04:00
|
|
|
else if (mClassID.Equals(kAbCardDataSourceCID))
|
1999-05-18 00:16:22 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsABCardDataSource * cardDataSource = new nsABCardDataSource();
|
|
|
|
if (cardDataSource)
|
|
|
|
rv = cardDataSource->QueryInterface(aIID, aResult);
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) && cardDataSource)
|
|
|
|
delete cardDataSource;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
else if (mClassID.Equals(kAbCardCID))
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsABCard * card = new nsABCard();
|
|
|
|
if (card)
|
|
|
|
rv = card->QueryInterface(aIID, aResult);
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) && card)
|
|
|
|
delete card;
|
|
|
|
return rv;
|
|
|
|
}
|
1999-06-16 04:04:49 +04:00
|
|
|
else if (mClassID.Equals(kAddressBookDB))
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsAddrDatabase * abDatabase = new nsAddrDatabase();
|
|
|
|
if (abDatabase)
|
|
|
|
rv = abDatabase->QueryInterface(aIID, aResult);
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) && abDatabase)
|
|
|
|
delete abDatabase;
|
|
|
|
return rv;
|
1999-07-02 00:04:04 +04:00
|
|
|
}
|
|
|
|
else if (mClassID.Equals(kAbCardPropertyCID))
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsAbCardProperty * abCardProperty = new nsAbCardProperty();
|
|
|
|
if (abCardProperty)
|
|
|
|
rv = abCardProperty->QueryInterface(aIID, aResult);
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) && abCardProperty)
|
|
|
|
delete abCardProperty;
|
|
|
|
return rv;
|
|
|
|
}
|
1999-07-07 23:06:40 +04:00
|
|
|
else if (mClassID.Equals(kAddrBookSessionCID))
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsAddrBookSession * abSession = new nsAddrBookSession();
|
|
|
|
if (abSession)
|
|
|
|
rv = abSession->QueryInterface(aIID, aResult);
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) && abSession)
|
|
|
|
delete abSession;
|
|
|
|
return rv;
|
|
|
|
}
|
1999-05-18 00:16:22 +04:00
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsAbFactory::LockFactory(PRBool aLock)
|
|
|
|
{
|
|
|
|
if (aLock)
|
|
|
|
PR_AtomicIncrement(&g_LockCount);
|
|
|
|
else
|
|
|
|
PR_AtomicDecrement(&g_LockCount);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the proper factory to the caller.
|
|
|
|
extern "C" NS_EXPORT nsresult NSGetFactory(nsISupports* aServMgr,
|
|
|
|
const nsCID &aClass,
|
|
|
|
const char *aClassName,
|
|
|
|
const char *aProgID,
|
|
|
|
nsIFactory **aFactory)
|
|
|
|
{
|
|
|
|
if (nsnull == aFactory)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
*aFactory = new nsAbFactory(aClass, aClassName, aProgID);
|
|
|
|
|
|
|
|
if (aFactory)
|
1999-07-01 04:15:20 +04:00
|
|
|
return (*aFactory)->QueryInterface(nsCOMTypeInfo<nsIFactory>::GetIID(), (void**)aFactory); // they want a Factory Interface so give it to them
|
1999-05-18 00:16:22 +04:00
|
|
|
else
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" NS_EXPORT PRBool NSCanUnload(nsISupports* aServMgr)
|
|
|
|
{
|
|
|
|
return PRBool(g_InstanceCount == 0 && g_LockCount == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" NS_EXPORT nsresult
|
|
|
|
NSRegisterSelf(nsISupports* aServMgr, const char* path)
|
|
|
|
{
|
1999-06-05 06:21:20 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsresult finalResult = NS_OK;
|
1999-05-18 00:16:22 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-06-09 10:40:48 +04:00
|
|
|
NS_WITH_SERVICE1(nsIComponentManager, compMgr, aServMgr, kComponentManagerCID, &rv);
|
1999-05-18 00:16:22 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-07-02 00:04:04 +04:00
|
|
|
rv = compMgr->RegisterComponent(kAddressBookCID,
|
|
|
|
"Address Book DOM interaction object",
|
|
|
|
"component://netscape/addressbook",
|
|
|
|
path, PR_TRUE, PR_TRUE);
|
|
|
|
if (NS_FAILED(rv)) finalResult = rv;
|
|
|
|
|
1999-05-18 00:16:22 +04:00
|
|
|
// register our RDF datasources:
|
|
|
|
rv = compMgr->RegisterComponent(kAbDirectoryDataSourceCID,
|
|
|
|
"Mail/News Address Book Directory Data Source",
|
|
|
|
NS_RDF_DATASOURCE_PROGID_PREFIX "addressdirectory",
|
|
|
|
path, PR_TRUE, PR_TRUE);
|
|
|
|
|
1999-06-05 06:21:20 +04:00
|
|
|
if (NS_FAILED(rv)) finalResult = rv;
|
|
|
|
|
1999-05-18 00:16:22 +04:00
|
|
|
rv = compMgr->RegisterComponent(kAbDirectoryCID,
|
|
|
|
"Mail/News Address Book Directory Factory",
|
|
|
|
NS_RDF_RESOURCE_FACTORY_PROGID_PREFIX "abdirectory",
|
|
|
|
path, PR_TRUE, PR_TRUE);
|
|
|
|
|
1999-06-05 06:21:20 +04:00
|
|
|
if (NS_FAILED(rv)) finalResult = rv;
|
|
|
|
|
1999-05-18 00:16:22 +04:00
|
|
|
rv = compMgr->RegisterComponent(kAbCardDataSourceCID,
|
|
|
|
"Mail/News Address Book Card Data Source",
|
|
|
|
NS_RDF_DATASOURCE_PROGID_PREFIX "addresscard",
|
|
|
|
path, PR_TRUE, PR_TRUE);
|
|
|
|
|
1999-06-05 06:21:20 +04:00
|
|
|
if (NS_FAILED(rv)) finalResult = rv;
|
|
|
|
|
1999-05-18 00:16:22 +04:00
|
|
|
rv = compMgr->RegisterComponent(kAbCardCID,
|
|
|
|
"Mail/News Address Book Card Factory",
|
|
|
|
NS_RDF_RESOURCE_FACTORY_PROGID_PREFIX "abcard",
|
|
|
|
path, PR_TRUE, PR_TRUE);
|
1999-06-05 06:21:20 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) finalResult = rv;
|
|
|
|
|
1999-06-16 04:04:49 +04:00
|
|
|
rv = compMgr->RegisterComponent(kAddressBookDB, nsnull, nsnull,
|
|
|
|
path, PR_TRUE, PR_TRUE);
|
|
|
|
if (NS_FAILED(rv))finalResult = rv;
|
|
|
|
|
1999-07-02 00:04:04 +04:00
|
|
|
rv = compMgr->RegisterComponent(kAbCardPropertyCID,
|
|
|
|
"Mail/News Address Book Card Property",
|
|
|
|
"component://netscape/addressbook/cardproperty",
|
|
|
|
path, PR_TRUE, PR_TRUE);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) finalResult = rv;
|
|
|
|
|
1999-07-07 23:06:40 +04:00
|
|
|
rv = compMgr->RegisterComponent(kAddrBookSessionCID,
|
|
|
|
"Address Book Session",
|
|
|
|
"component://netscape/addressbook/services/session",
|
|
|
|
path, PR_TRUE, PR_TRUE);
|
1999-07-02 00:04:04 +04:00
|
|
|
|
1999-07-07 23:06:40 +04:00
|
|
|
if (NS_FAILED(rv)) finalResult = rv;
|
1999-06-05 06:21:20 +04:00
|
|
|
return finalResult;
|
1999-05-18 00:16:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" NS_EXPORT nsresult
|
|
|
|
NSUnregisterSelf(nsISupports* aServMgr, const char* path)
|
|
|
|
{
|
1999-06-05 06:21:20 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsresult finalResult = NS_OK;
|
1999-05-18 00:16:22 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-06-09 10:40:48 +04:00
|
|
|
NS_WITH_SERVICE1(nsIComponentManager, compMgr, aServMgr, kComponentManagerCID, &rv);
|
1999-05-18 00:16:22 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-07-02 00:04:04 +04:00
|
|
|
rv = compMgr->UnregisterComponent(kAddressBookCID, path);
|
|
|
|
if (NS_FAILED(rv)) finalResult = rv;
|
|
|
|
|
1999-05-18 00:16:22 +04:00
|
|
|
rv = compMgr->UnregisterComponent(kAbDirectoryDataSourceCID, path);
|
1999-06-05 06:21:20 +04:00
|
|
|
if (NS_FAILED(rv)) finalResult = rv;
|
1999-05-18 00:16:22 +04:00
|
|
|
|
|
|
|
rv = compMgr->UnregisterComponent(kAbDirectoryCID, path);
|
1999-06-05 06:21:20 +04:00
|
|
|
if (NS_FAILED(rv)) finalResult = rv;
|
1999-05-18 00:16:22 +04:00
|
|
|
|
|
|
|
rv = compMgr->UnregisterComponent(kAbCardDataSourceCID, path);
|
1999-06-05 06:21:20 +04:00
|
|
|
if (NS_FAILED(rv)) finalResult = rv;
|
1999-05-18 00:16:22 +04:00
|
|
|
|
|
|
|
rv = compMgr->UnregisterComponent(kAbCardCID, path);
|
1999-06-05 06:21:20 +04:00
|
|
|
if (NS_FAILED(rv)) finalResult = rv;
|
|
|
|
|
1999-06-16 04:04:49 +04:00
|
|
|
rv = compMgr->UnregisterComponent(kAddressBookDB, path);
|
|
|
|
if (NS_FAILED(rv)) finalResult = rv;
|
|
|
|
|
1999-07-07 23:06:40 +04:00
|
|
|
rv = compMgr->UnregisterComponent(kAbCardPropertyCID, path);
|
|
|
|
if (NS_FAILED(rv)) finalResult = rv;
|
|
|
|
|
|
|
|
rv = compMgr->UnregisterComponent(kAddrBookSessionCID, path);
|
|
|
|
if (NS_FAILED(rv)) finalResult = rv;
|
|
|
|
|
1999-06-05 06:21:20 +04:00
|
|
|
return finalResult;
|
1999-05-18 00:16:22 +04:00
|
|
|
}
|