diff --git a/mailnews/addrbook/src/nsAbBSDirectory.cpp b/mailnews/addrbook/src/nsAbBSDirectory.cpp new file mode 100644 index 000000000000..12d528138671 --- /dev/null +++ b/mailnews/addrbook/src/nsAbBSDirectory.cpp @@ -0,0 +1,367 @@ +/* -*- 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): Paul Sandoz + */ + +#include "nsAbBSDirectory.h" + +#include "nsRDFCID.h" +#include "nsIRDFService.h" +#include "nsIRDFResource.h" + +#include "nsDirPrefs.h" +#include "nsAbBaseCID.h" +#include "nsMsgBaseCID.h" +#include "nsIAddressBook.h" +#include "nsAddrDatabase.h" +#include "nsIAddrBookSession.h" +#include "nsIAbMDBDirectory.h" +#include "nsHashtable.h" +#include "nsIAbUpgrader.h" +#include "nsIMessengerMigrator.h" + +#include "prmem.h" +#include "prprf.h" + +extern const char *kDirectoryDataSourceRoot; + +static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID); + +static NS_DEFINE_CID(kAddrBookCID, NS_ADDRESSBOOK_CID); +static NS_DEFINE_CID(kAddressBookDBCID, NS_ADDRDATABASE_CID); +static NS_DEFINE_CID(kAddrBookSessionCID, NS_ADDRBOOKSESSION_CID); +static NS_DEFINE_CID(kMessengerMigratorCID, NS_MESSENGERMIGRATOR_CID); + +nsAbBSDirectory::nsAbBSDirectory() + : nsRDFResource(), + mInitialized(PR_FALSE), + mServers (13) +{ + NS_NewISupportsArray(getter_AddRefs(mSubDirectories)); +} + +nsAbBSDirectory::~nsAbBSDirectory() +{ + if (mSubDirectories) + { + PRUint32 count; + nsresult rv = mSubDirectories->Count(&count); + NS_ASSERTION(NS_SUCCEEDED(rv), "Count failed"); + PRInt32 i; + for (i = count - 1; i >= 0; i--) + mSubDirectories->RemoveElementAt(i); + } +} + +NS_IMPL_ISUPPORTS_INHERITED(nsAbBSDirectory, nsRDFResource, nsIAbDirectory) + + +nsresult nsAbBSDirectory::AddDirectory(const char *uriName, nsIAbDirectory **childDir) +{ + if (!childDir || !uriName) + return NS_ERROR_NULL_POINTER; + + nsresult rv = NS_OK; + NS_WITH_SERVICE(nsIRDFService, rdf, kRDFServiceCID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr res; + rv = rdf->GetResource(uriName, getter_AddRefs(res)); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr directory(do_QueryInterface(res, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + mSubDirectories->AppendElement(directory); + *childDir = directory; + NS_IF_ADDREF(*childDir); + + return rv; +} + +nsresult nsAbBSDirectory::NotifyItemAdded(nsISupports *item) +{ + nsresult rv = NS_OK; + NS_WITH_SERVICE(nsIAddrBookSession, abSession, kAddrBookSessionCID, &rv); + if(NS_SUCCEEDED(rv)) + abSession->NotifyDirectoryItemAdded(this, item); + return NS_OK; +} + +nsresult nsAbBSDirectory::NotifyItemDeleted(nsISupports *item) +{ + nsresult rv = NS_OK; + NS_WITH_SERVICE(nsIAddrBookSession, abSession, kAddrBookSessionCID, &rv); + if(NS_SUCCEEDED(rv)) + abSession->NotifyDirectoryItemDeleted(this, item); + + return NS_OK; +} + + + +NS_IMETHODIMP nsAbBSDirectory::GetChildNodes(nsIEnumerator* *result) +{ + if (!mInitialized) + {/* + nsresult rv; + nsCOMPtr abUpgrader = do_GetService(NS_AB4xUPGRADER_CONTRACTID, &rv); + if (NS_SUCCEEDED(rv) && abUpgrader) + { + nsCOMPtr migrator = do_GetService(kMessengerMigratorCID, &rv); + if (NS_SUCCEEDED(rv) || migrator) + migrator->UpgradePrefs(); + }*/ + + if (!PL_strcmp(mURI, "abdirectory://") && GetDirList()) + { + PRInt32 count = GetDirList()->Count(); + /* check: only show personal address book for now */ + /* not showing 4.x address book unitl we have the converting done */ + PRInt32 i; + for (i = 0; i < count; i++) + { + DIR_Server *server = (DIR_Server *)GetDirList()->ElementAt(i); + + if (server->dirType == PABDirectory) + { + nsString name; name.AssignWithConversion(server->fileName); + PRInt32 pos = name.Find("na2"); + if (pos >= 0) /* check: this is a 4.x file, remove when conversion is done */ + continue; + + char* uriStr = nsnull; + uriStr = PR_smprintf("%s%s", kDirectoryDataSourceRoot, server->fileName); + if (uriStr == nsnull) + return NS_ERROR_OUT_OF_MEMORY; + + nsCOMPtr childDir; + AddDirectory(uriStr, getter_AddRefs(childDir)); + nsCOMPtr dbchildDir(do_QueryInterface(childDir)); + + if (uriStr) + PR_smprintf_free(uriStr); + if (childDir) + { + PRUnichar *unichars = nsnull; + PRInt32 descLength = PL_strlen(server->description); + INTL_ConvertToUnicode((const char *)server->description, + descLength, (void**)&unichars); + childDir->SetDirName(unichars); + PR_FREEIF(unichars); + + nsVoidKey key((void *)childDir); + mServers.Put (&key, (void *)server); + + } + nsresult rv = NS_OK; + nsCOMPtr listDatabase; + + NS_WITH_SERVICE(nsIAddrBookSession, abSession, kAddrBookSessionCID, &rv); + if (NS_SUCCEEDED(rv)) + { + nsFileSpec* dbPath; + abSession->GetUserProfileDirectory(&dbPath); + + nsString file; file.AssignWithConversion(server->fileName); + (*dbPath) += file; + + NS_WITH_SERVICE(nsIAddrDatabase, addrDBFactory, kAddressBookDBCID, &rv); + + if (NS_SUCCEEDED(rv) && addrDBFactory) + rv = addrDBFactory->Open(dbPath, PR_TRUE, getter_AddRefs(listDatabase), PR_TRUE); + if (NS_SUCCEEDED(rv) && listDatabase) + { + listDatabase->GetMailingListsFromDB(childDir); + } + } + } + } + + } + mInitialized = PR_TRUE; + } + return mSubDirectories->Enumerate(result); +} + +NS_IMETHODIMP nsAbBSDirectory::CreateNewDirectory(PRUint32 prefCount, const char **prefName, const PRUnichar **prefValue) +{ + if (!*prefValue || !*prefName) + return NS_ERROR_NULL_POINTER; + + PRUnichar *unicharDescription = nsnull; + char *charFileName = nsnull; + PRBool migrating = PR_FALSE; + char *charMigrate = nsnull; + nsresult rv; + + for (PRUint32 jk=0; jk < prefCount; jk++) + { + switch (tolower(prefName[jk][0])) + { + case 'd': + if (!PL_strcasecmp(prefName[jk], "description")) + { + unicharDescription = (PRUnichar *)prefValue[jk]; + } + break; + case 'f': + if (!PL_strcasecmp(prefName[jk], "fileName")) + { + nsString descString(prefValue[jk]); + PRInt32 unicharLength = descString.Length(); + INTL_ConvertFromUnicode(prefValue[jk], unicharLength, &charFileName); + } + break; + case 'm': + if (!PL_strcasecmp(prefName[jk], "migrating")) + { + nsString descString(prefValue[jk]); + PRInt32 unicharLength = descString.Length(); + INTL_ConvertFromUnicode(prefValue[jk], unicharLength, &charMigrate); + if (!PL_strcasecmp(charMigrate, "true")) + { + migrating = PR_TRUE; + } + } + break; + } + } + + if (unicharDescription == nsnull) + { + rv = NS_ERROR_NULL_POINTER; + } + else + { + char *uri = nsnull; + if (PL_strstr(charFileName, ".mab")) + uri = PR_smprintf("%s%s", kDirectoryDataSourceRoot, charFileName); + rv = CreateDirectoryByURI(unicharDescription, uri, migrating); + if (uri) + PR_smprintf_free(uri); + } + + if (charFileName) + nsMemory::Free(charFileName); + if (charMigrate) + nsMemory::Free(charMigrate); + + return rv; +} + +NS_IMETHODIMP nsAbBSDirectory::CreateDirectoryByURI(const PRUnichar *displayName, const char *uri, PRBool migrating) +{ + if (!displayName || !uri) + return NS_ERROR_NULL_POINTER; + + DIR_Server * server = nsnull; + DirectoryType dirType = PABDirectory; + const char* fileName = nsnull; + if (PL_strstr(uri, kDirectoryDataSourceRoot)) // for abmdbdirectory:// + { + fileName = &(uri[PL_strlen(kDirectoryDataSourceRoot)]); + dirType = PABDirectory; + } + DIR_AddNewAddressBook(displayName, fileName, migrating, dirType, &server); + + nsCOMPtr newDir; + nsresult rv = AddDirectory(uri, getter_AddRefs(newDir)); + + if (NS_SUCCEEDED(rv) && newDir) + { + newDir->SetDirName((PRUnichar *)displayName); + nsVoidKey key((void *)newDir); + mServers.Put (&key, (void *)server); + NotifyItemAdded(newDir); + return NS_OK; + } + else + return NS_ERROR_FAILURE; +} + +NS_IMETHODIMP nsAbBSDirectory::DeleteDirectory(nsIAbDirectory *directory) +{ + nsresult rv = NS_OK; + + if (!directory) + return NS_ERROR_FAILURE; + + nsCOMPtr dbdirectory(do_QueryInterface(directory, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + DIR_Server *server = nsnull; + nsVoidKey key((void *)directory); + server = (DIR_Server* )mServers.Get (&key); + + if (server) + { //it's an address book + + nsISupportsArray* pAddressLists; + directory->GetAddressLists(&pAddressLists); + if (pAddressLists) + { //remove mailing list node + PRUint32 total; + rv = pAddressLists->Count(&total); + if (total) + { + PRInt32 i; + for (i = total - 1; i >= 0; i--) + { + nsCOMPtr pSupport = getter_AddRefs(pAddressLists->ElementAt(i)); + if (pSupport) + { + nsCOMPtr listDir(do_QueryInterface(pSupport, &rv)); + nsCOMPtr dblistDir(do_QueryInterface(pSupport, &rv)); + if (listDir) + { + directory->DeleteDirectory(listDir); + dblistDir->RemoveElementsFromAddressList(); + } + } + pAddressLists->RemoveElement(pSupport); + } + } + } + DIR_DeleteServerFromList(server); + mServers.Remove(&key); + dbdirectory->ClearDatabase(); + + rv = mSubDirectories->RemoveElement(directory); + NotifyItemDeleted(directory); + } + return rv; +} + +NS_IMETHODIMP nsAbBSDirectory::HasDirectory(nsIAbDirectory *dir, PRBool *hasDir) +{ + if (!hasDir) + return NS_ERROR_NULL_POINTER; + + nsresult rv = NS_ERROR_FAILURE; + + DIR_Server* dirServer = nsnull; + nsVoidKey key((void *)dir); + dirServer = (DIR_Server* )mServers.Get (&key); + rv = DIR_ContainsServer(dirServer, hasDir); + + return rv; +} + diff --git a/mailnews/addrbook/src/nsAbBSDirectory.h b/mailnews/addrbook/src/nsAbBSDirectory.h new file mode 100644 index 000000000000..b940cde706ee --- /dev/null +++ b/mailnews/addrbook/src/nsAbBSDirectory.h @@ -0,0 +1,62 @@ +/* -*- 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): Paul Sandoz + */ + + +#ifndef nsAbBSDirectory_h__ +#define nsAbBSDirectory_h__ + +#include "nsRDFResource.h" +#include "nsAbDirProperty.h" +#include "nsHashtable.h" + +#include "nsISupportsArray.h" + +class nsAbBSDirectory : public nsRDFResource, public nsAbDirProperty +{ +public: + NS_DECL_ISUPPORTS_INHERITED + + nsAbBSDirectory(); + virtual ~nsAbBSDirectory(); + + // nsIAbDirectory methods + NS_IMETHOD GetChildNodes(nsIEnumerator* *result); + NS_IMETHOD CreateNewDirectory(PRUint32 prefCount, const char **prefName, const PRUnichar **prefValue); + NS_IMETHOD CreateDirectoryByURI(const PRUnichar *dirName, const char *uri, PRBool migrating); + NS_IMETHOD DeleteDirectory(nsIAbDirectory *directory); + NS_IMETHOD HasDirectory(nsIAbDirectory *dir, PRBool *hasDir); + + +protected: + nsresult AddDirectory(const char *uriName, nsIAbDirectory **childDir); + nsVoidArray* GetDirList(){ return DIR_GetDirectories(); } + + nsresult NotifyItemAdded(nsISupports *item); + nsresult NotifyItemDeleted(nsISupports *item); + +protected: + PRBool mInitialized; + nsCOMPtr mSubDirectories; + nsHashtable mServers; +}; + +#endif diff --git a/mailnews/addrbook/src/nsAbMDBCard.cpp b/mailnews/addrbook/src/nsAbMDBCard.cpp new file mode 100644 index 000000000000..a23ea9b0c02d --- /dev/null +++ b/mailnews/addrbook/src/nsAbMDBCard.cpp @@ -0,0 +1,226 @@ +/* -*- 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): + * Pierre Phaneuf + */ + +#include "nsAbMDBCard.h" +#include "nsIRDFService.h" +#include "nsIServiceManager.h" +#include "nsRDFCID.h" +#include "nsXPIDLString.h" +#include "nsCOMPtr.h" +#include "nsAbBaseCID.h" +#include "prmem.h" +#include "prlog.h" +#include "prprf.h" + +#include "nsAddrDatabase.h" +#include "nsIAddrBookSession.h" +#include "nsIAddressBook.h" + +static NS_DEFINE_CID(kAddrBookSessionCID, NS_ADDRBOOKSESSION_CID); +static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID); + +nsAbMDBCard::nsAbMDBCard(void) + : nsAbMDBRDFResource(), mListeners(nsnull) +{ +} + +nsAbMDBCard::~nsAbMDBCard(void) +{ + if (mCardDatabase) + { + nsIAddrDBListener* listener = this; + mCardDatabase->RemoveListener(listener); + mCardDatabase = null_nsCOMPtr(); + } + + if (mListeners) + { + PRInt32 i; + for (i = mListeners->Count() - 1; i >= 0; --i) + mListeners->RemoveElementAt(i); + delete mListeners; + } +} + +NS_IMPL_ISUPPORTS_INHERITED2(nsAbMDBCard, nsAbMDBRDFResource, nsIAbCard, nsIAbMDBCard) + + +// nsiAddrDBListener methods + +NS_IMETHODIMP nsAbMDBCard::OnCardEntryChange +(PRUint32 abCode, nsIAbCard *card, nsIAddrDBListener *instigator) +{ + if (abCode == AB_NotifyPropertyChanged && card) + { + PRUint32 tableID; + PRUint32 rowID; + PRBool bMailList; + + nsresult err = NS_OK; + nsCOMPtr dbcard(do_QueryInterface(card, &err)); + if (NS_FAILED(err) || !dbcard) + return NS_ERROR_NULL_POINTER; + + dbcard->GetDbTableID(&tableID); + dbcard->GetDbRowID(&rowID); + + card->GetIsMailList(&bMailList); + if (m_dbTableID == tableID && m_dbRowID == rowID && m_bIsMailList == bMailList) + { + nsXPIDLString pDisplayName; + card->GetDisplayName(getter_Copies(pDisplayName)); + if (pDisplayName) + NotifyPropertyChanged("DisplayName", nsnull, + NS_CONST_CAST(PRUnichar*, (const PRUnichar*)pDisplayName)); + + nsXPIDLString pName; + card->GetName(getter_Copies(pName)); + if (pName) + NotifyPropertyChanged("Name", nsnull, + NS_CONST_CAST(PRUnichar*, (const PRUnichar*)pName)); + + nsXPIDLString pNickName; + card->GetNickName(getter_Copies(pNickName)); + if (pNickName) + NotifyPropertyChanged("NickName", nsnull, + NS_CONST_CAST(PRUnichar*, (const PRUnichar*)pNickName)); + + nsXPIDLString pPrimaryEmail; + card->GetPrimaryEmail(getter_Copies(pPrimaryEmail)); + if (pPrimaryEmail) + NotifyPropertyChanged("PrimaryEmail", nsnull, + NS_CONST_CAST(PRUnichar*, (const PRUnichar*)pPrimaryEmail)); + + nsXPIDLString pSecondEmail; + card->GetSecondEmail(getter_Copies(pSecondEmail)); + if (pSecondEmail) + NotifyPropertyChanged("SecondEmail", nsnull, + NS_CONST_CAST(PRUnichar*, (const PRUnichar*)pSecondEmail)); + + nsXPIDLString pWorkPhone; + card->GetWorkPhone(getter_Copies(pWorkPhone)); + if (pWorkPhone) + NotifyPropertyChanged("WorkPhone", nsnull, + NS_CONST_CAST(PRUnichar*, (const PRUnichar*)pWorkPhone)); + + nsXPIDLString pHomePhone; + card->GetHomePhone(getter_Copies(pHomePhone)); + if (pHomePhone) + NotifyPropertyChanged("HomePhone", nsnull, + NS_CONST_CAST(PRUnichar*, (const PRUnichar*)pHomePhone)); + + nsXPIDLString pFaxNumber; + card->GetFaxNumber(getter_Copies(pFaxNumber)); + if (pFaxNumber) + NotifyPropertyChanged("FaxNumber", nsnull, + NS_CONST_CAST(PRUnichar*, (const PRUnichar*)pFaxNumber)); + + nsXPIDLString pPagerNumber; + card->GetPagerNumber(getter_Copies(pPagerNumber)); + if (pPagerNumber) + NotifyPropertyChanged("PagerNumber", nsnull, + NS_CONST_CAST(PRUnichar*, (const PRUnichar*)pPagerNumber)); + + nsXPIDLString pCellularNumber; + card->GetCellularNumber(getter_Copies(pCellularNumber)); + if (pCellularNumber) + NotifyPropertyChanged("CellularNumber", nsnull, + NS_CONST_CAST(PRUnichar*, (const PRUnichar*)pCellularNumber)); + + nsXPIDLString pJobTitle; + card->GetJobTitle(getter_Copies(pJobTitle)); + if (pJobTitle) + NotifyPropertyChanged("JobTitle", nsnull, + NS_CONST_CAST(PRUnichar*, (const PRUnichar*)pJobTitle)); + + nsXPIDLString pDepartment; + card->GetDepartment(getter_Copies(pDepartment)); + if (pDepartment) + NotifyPropertyChanged("Department", nsnull, + NS_CONST_CAST(PRUnichar*, (const PRUnichar*)pDepartment)); + + nsXPIDLString pCompany; + card->GetCompany(getter_Copies(pCompany)); + if (pCompany) + NotifyPropertyChanged("Company", nsnull, + NS_CONST_CAST(PRUnichar*, (const PRUnichar*)pCompany)); + } + } + return NS_OK; +} + + + + + + + + +// protected class methods + +nsresult nsAbMDBCard::NotifyPropertyChanged(char *property, PRUnichar* oldValue, PRUnichar* newValue) +{ + nsCOMPtr supports; + if(NS_SUCCEEDED(QueryInterface(NS_GET_IID(nsISupports), getter_AddRefs(supports)))) + { + //Notify listeners who listen to every folder + nsresult rv; + NS_WITH_SERVICE(nsIAddrBookSession, abSession, kAddrBookSessionCID, &rv); + if(NS_SUCCEEDED(rv)) + abSession->NotifyItemPropertyChanged(supports, property, oldValue, newValue); + } + + return NS_OK; +} + +nsresult nsAbMDBCard::AddSubNode(nsAutoString name, nsIAbCard **childCard) +{ + if(!childCard) + return NS_ERROR_NULL_POINTER; + + nsresult rv = NS_OK; + NS_WITH_SERVICE(nsIRDFService, rdf, kRDFServiceCID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + nsCAutoString uri; + uri.Append(mURI); + uri.Append('/'); + + char *utf8Name = name.ToNewUTF8String(); + if (!utf8Name) + return NS_ERROR_OUT_OF_MEMORY; + uri.Append(utf8Name); + nsMemory::Free(utf8Name); + + nsCOMPtr res; + rv = rdf->GetResource(uri.get(), getter_AddRefs(res)); + NS_ENSURE_SUCCESS(rv, rv); + nsCOMPtr card(do_QueryInterface(res, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + *childCard = card; + NS_IF_ADDREF(*childCard); + + return rv; +} + diff --git a/mailnews/addrbook/src/nsAbMDBCard.h b/mailnews/addrbook/src/nsAbMDBCard.h new file mode 100644 index 000000000000..b5029f06c884 --- /dev/null +++ b/mailnews/addrbook/src/nsAbMDBCard.h @@ -0,0 +1,66 @@ +/* -*- 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): Paul Sandoz + */ + +/******************************************************************************************************** + + Interface for representing Address Book Directory + +*********************************************************************************************************/ + +#ifndef nsAbMDBCard_h__ +#define nsAbMDBCard_h__ + +#include "nsAbMDBCardProperty.h" +#include "nsAbMDBRDFResource.h" +#include "nsISupportsArray.h" +#include "nsVoidArray.h" +#include "nsCOMPtr.h" +#include "nsIAddrDBListener.h" +#include "nsIAddrDatabase.h" + + /* + * Address Book Directory + */ + +class nsAbMDBCard: public nsAbMDBRDFResource, public nsAbMDBCardProperty +{ +public: + + NS_DECL_ISUPPORTS_INHERITED + + nsAbMDBCard(void); + virtual ~nsAbMDBCard(void); + + // nsIAddrDBListener methods: + NS_IMETHOD OnCardEntryChange(PRUint32 abCode, nsIAbCard *card, nsIAddrDBListener *instigator); + +protected: + + nsresult NotifyPropertyChanged(char *property, PRUnichar* oldValue, PRUnichar* newValue); + nsresult AddSubNode(nsAutoString name, nsIAbCard **childDir); + +protected: + + nsVoidArray *mListeners; +}; + +#endif diff --git a/mailnews/addrbook/src/nsAbMDBCardProperty.cpp b/mailnews/addrbook/src/nsAbMDBCardProperty.cpp new file mode 100644 index 000000000000..f6f3e99bd44b --- /dev/null +++ b/mailnews/addrbook/src/nsAbMDBCardProperty.cpp @@ -0,0 +1,622 @@ +/* -*- 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): Paul Sandoz + */ + +#include "nsAbMDBCardProperty.h" +#include "nsIRDFService.h" +#include "nsIServiceManager.h" +#include "nsRDFCID.h" +#include "nsXPIDLString.h" +#include "nsAbBaseCID.h" +#include "prmem.h" +#include "prlog.h" +#include "prprf.h" +#include "rdf.h" +#include "nsCOMPtr.h" + +#include "nsAddrDatabase.h" +#include "nsIAddrBookSession.h" +#include "nsIPref.h" +#include "nsIAddressBook.h" + +static NS_DEFINE_CID(kAddressBookDBCID, NS_ADDRDATABASE_CID); +static NS_DEFINE_CID(kAddrBookSessionCID, NS_ADDRBOOKSESSION_CID); +static NS_DEFINE_CID(kAddrBookCID, NS_ADDRESSBOOK_CID); + +/* The definition is nsAddressBook.cpp */ +extern const char *kDirectoryDataSourceRoot; +extern const char *kCardDataSourceRoot; + +nsAbMDBCardProperty::nsAbMDBCardProperty(void) +{ + m_Key = 0; + m_dbTableID = 0; + m_dbRowID = 0; + + m_pAnonymousStrAttributes = nsnull; + m_pAnonymousStrValues = nsnull; + m_pAnonymousIntAttributes = nsnull; + m_pAnonymousIntValues = nsnull; + m_pAnonymousBoolAttributes = nsnull; + m_pAnonymousBoolValues = nsnull; + +} + +nsAbMDBCardProperty::~nsAbMDBCardProperty(void) +{ + + if (mCardDatabase) + mCardDatabase = null_nsCOMPtr(); + + if (m_pAnonymousStrAttributes) + RemoveAnonymousList(m_pAnonymousStrAttributes); + if (m_pAnonymousIntAttributes) + RemoveAnonymousList(m_pAnonymousIntAttributes); + if (m_pAnonymousBoolAttributes) + RemoveAnonymousList(m_pAnonymousBoolAttributes); + + if (m_pAnonymousStrValues) + RemoveAnonymousList(m_pAnonymousStrValues); + if (m_pAnonymousIntValues) + RemoveAnonymousList(m_pAnonymousIntValues); + if (m_pAnonymousBoolValues) + RemoveAnonymousList(m_pAnonymousBoolValues); + +} + +NS_IMPL_ISUPPORTS_INHERITED(nsAbMDBCardProperty, nsAbCardProperty, nsIAbMDBCard) + + +// nsIAbMDBCard attributes + +NS_IMETHODIMP nsAbMDBCardProperty::GetDbTableID(PRUint32 *aDbTableID) +{ + *aDbTableID = m_dbTableID; + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBCardProperty::SetDbTableID(PRUint32 aDbTableID) +{ + m_dbTableID = aDbTableID; + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBCardProperty::GetDbRowID(PRUint32 *aDbRowID) +{ + *aDbRowID = m_dbRowID; + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBCardProperty::SetDbRowID(PRUint32 aDbRowID) +{ + m_dbRowID = aDbRowID; + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBCardProperty::GetKey(PRUint32 *aKey) +{ + *aKey = m_Key; + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBCardProperty::GetAnonymousStrAttrubutesList(nsVoidArray **attrlist) +{ + if (attrlist && m_pAnonymousStrAttributes) + { + *attrlist = m_pAnonymousStrAttributes; + return NS_OK; + } + else + return NS_ERROR_NULL_POINTER; +} + +NS_IMETHODIMP nsAbMDBCardProperty::GetAnonymousStrValuesList(nsVoidArray **valuelist) +{ + if (valuelist && m_pAnonymousStrValues) + { + *valuelist = m_pAnonymousStrValues; + return NS_OK; + } + else + return NS_ERROR_NULL_POINTER; +} + +NS_IMETHODIMP nsAbMDBCardProperty::GetAnonymousIntAttrubutesList(nsVoidArray **attrlist) +{ + if (attrlist && m_pAnonymousIntAttributes) + { + *attrlist = m_pAnonymousIntAttributes; + return NS_OK; + } + else + return NS_ERROR_NULL_POINTER; +} + +NS_IMETHODIMP nsAbMDBCardProperty::GetAnonymousIntValuesList(nsVoidArray **valuelist) +{ + if (valuelist && m_pAnonymousIntValues) + { + *valuelist = m_pAnonymousIntValues; + return NS_OK; + } + else + return NS_ERROR_NULL_POINTER; +} + +NS_IMETHODIMP nsAbMDBCardProperty::GetAnonymousBoolAttrubutesList(nsVoidArray **attrlist) +{ + if (attrlist && m_pAnonymousBoolAttributes) + { + *attrlist = m_pAnonymousBoolAttributes; + return NS_OK; + } + else + return NS_ERROR_NULL_POINTER; +} + +NS_IMETHODIMP nsAbMDBCardProperty::GetAnonymousBoolValuesList(nsVoidArray **valuelist) +{ + if (valuelist && m_pAnonymousBoolValues) + { + *valuelist = m_pAnonymousBoolValues; + return NS_OK; + } + else + return NS_ERROR_NULL_POINTER; +} + + + + + + + +// nsIAbMDBCard methods + +NS_IMETHODIMP nsAbMDBCardProperty::SetRecordKey(PRUint32 key) +{ + m_Key = key; + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBCardProperty::SetAbDatabase(nsIAddrDatabase* database) +{ + mCardDatabase = database; + return NS_OK; +} + + +NS_IMETHODIMP nsAbMDBCardProperty::SetAnonymousStringAttribute +(const char *attrname, const char *value) +{ + nsresult rv = NS_OK; + + char* pAttribute = PL_strdup(attrname); + char* pValue = PL_strdup(value); + if (pAttribute && pValue) + { + rv = SetAnonymousAttribute(&m_pAnonymousStrAttributes, + &m_pAnonymousStrValues, pAttribute, pValue); + } + else + { + PR_FREEIF(pAttribute); + PR_FREEIF(pValue); + rv = NS_ERROR_NULL_POINTER; + } + return rv; +} + +NS_IMETHODIMP nsAbMDBCardProperty::SetAnonymousIntAttribute +(const char *attrname, PRUint32 value) +{ + nsresult rv = NS_OK; + + char* pAttribute = PL_strdup(attrname); + PRUint32* pValue = (PRUint32 *)PR_Calloc(1, sizeof(PRUint32)); + *pValue = value; + if (pAttribute && pValue) + { + rv = SetAnonymousAttribute(&m_pAnonymousIntAttributes, + &m_pAnonymousIntValues, pAttribute, pValue); + } + else + { + PR_FREEIF(pAttribute); + PR_FREEIF(pValue); + rv = NS_ERROR_NULL_POINTER; + } + return rv; +} + +NS_IMETHODIMP nsAbMDBCardProperty::SetAnonymousBoolAttribute +(const char *attrname, PRBool value) +{ + nsresult rv = NS_OK; + + char* pAttribute = PL_strdup(attrname); + PRBool* pValue = (PRBool *)PR_Calloc(1, sizeof(PRBool)); + *pValue = value; + if (pAttribute && pValue) + { + rv = SetAnonymousAttribute(&m_pAnonymousBoolAttributes, + &m_pAnonymousBoolValues, pAttribute, pValue); + } + else + { + PR_FREEIF(pAttribute); + PR_FREEIF(pValue); + rv = NS_ERROR_NULL_POINTER; + } + return rv; +} + +/* caller need to PR_smprintf_free *uri */ +NS_IMETHODIMP nsAbMDBCardProperty::GetCardURI(char **uri) +{ + char* cardURI = nsnull; + nsFileSpec *filePath = nsnull; + if (mCardDatabase) + { + mCardDatabase->GetDbPath(&filePath); + if (filePath) + { + char* file = nsnull; + file = filePath->GetLeafName(); + if (file && m_dbRowID) + { + if (m_bIsMailList) + cardURI = PR_smprintf("%s%s/ListCard%ld", kCardDataSourceRoot, file, m_dbRowID); + else + cardURI = PR_smprintf("%s%s/Card%ld", kCardDataSourceRoot, file, m_dbRowID); + } + if (file) + nsCRT::free(file); + delete filePath; + } + } + if (cardURI) + { + *uri = cardURI; + return NS_OK; + } + else + return NS_ERROR_NULL_POINTER; +} + +NS_IMETHODIMP nsAbMDBCardProperty::CopyCard(nsIAbMDBCard* srcCardDB) +{ + nsresult err = NS_OK; + nsCOMPtr srcCard(do_QueryInterface(srcCardDB, &err)); + if (NS_FAILED(err)) + return NS_ERROR_NULL_POINTER; + + PRUnichar *str = nsnull; + srcCard->GetFirstName(&str); + SetFirstName(str); + PR_FREEIF(str); + + srcCard->GetLastName(&str); + SetLastName(str); + PR_FREEIF(str); + srcCard->GetDisplayName(&str); + SetDisplayName(str); + PR_FREEIF(str); + srcCard->GetNickName(&str); + SetNickName(str); + PR_FREEIF(str); + srcCard->GetPrimaryEmail(&str); + SetPrimaryEmail(str); + PR_FREEIF(str); + srcCard->GetSecondEmail(&str); + SetSecondEmail(str); + PR_FREEIF(str); + + PRUint32 format = nsIAbPreferMailFormat::unknown; + srcCard->GetPreferMailFormat(&format); + SetPreferMailFormat(format); + + srcCard->GetWorkPhone(&str); + SetWorkPhone(str); + PR_FREEIF(str); + srcCard->GetHomePhone(&str); + SetHomePhone(str); + PR_FREEIF(str); + srcCard->GetFaxNumber(&str); + SetFaxNumber(str); + PR_FREEIF(str); + srcCard->GetPagerNumber(&str); + SetPagerNumber(str); + PR_FREEIF(str); + srcCard->GetCellularNumber(&str); + SetCellularNumber(str); + PR_FREEIF(str); + srcCard->GetHomeAddress(&str); + SetHomeAddress(str); + PR_FREEIF(str); + srcCard->GetHomeAddress2(&str); + SetHomeAddress2(str); + PR_FREEIF(str); + srcCard->GetHomeCity(&str); + SetHomeCity(str); + PR_FREEIF(str); + srcCard->GetHomeState(&str); + SetHomeState(str); + PR_FREEIF(str); + srcCard->GetHomeZipCode(&str); + SetHomeZipCode(str); + PR_FREEIF(str); + srcCard->GetHomeCountry(&str); + SetHomeCountry(str); + PR_FREEIF(str); + srcCard->GetWorkAddress(&str); + SetWorkAddress(str); + PR_FREEIF(str); + srcCard->GetWorkAddress2(&str); + SetWorkAddress2(str); + PR_FREEIF(str); + srcCard->GetWorkCity(&str); + SetWorkCity(str); + PR_FREEIF(str); + srcCard->GetWorkState(&str); + SetWorkState(str); + PR_FREEIF(str); + srcCard->GetWorkZipCode(&str); + SetWorkZipCode(str); + PR_FREEIF(str); + srcCard->GetWorkCountry(&str); + SetWorkCountry(str); + PR_FREEIF(str); + srcCard->GetJobTitle(&str); + SetJobTitle(str); + PR_FREEIF(str); + srcCard->GetDepartment(&str); + SetDepartment(str); + PR_FREEIF(str); + srcCard->GetCompany(&str); + SetCompany(str); + PR_FREEIF(str); + srcCard->GetWebPage1(&str); + SetWebPage1(str); + PR_FREEIF(str); + srcCard->GetWebPage2(&str); + SetWebPage2(str); + PR_FREEIF(str); + srcCard->GetBirthYear(&str); + SetBirthYear(str); + PR_FREEIF(str); + srcCard->GetBirthMonth(&str); + SetBirthMonth(str); + PR_FREEIF(str); + srcCard->GetBirthDay(&str); + SetBirthDay(str); + PR_FREEIF(str); + srcCard->GetCustom1(&str); + SetCustom1(str); + PR_FREEIF(str); + srcCard->GetCustom2(&str); + SetCustom2(str); + PR_FREEIF(str); + srcCard->GetCustom3(&str); + SetCustom3(str); + PR_FREEIF(str); + srcCard->GetCustom4(&str); + SetCustom4(str); + PR_FREEIF(str); + srcCard->GetNotes(&str); + SetNotes(str); + PR_FREEIF(str); + + PRUint32 tableID, rowID; + srcCardDB->GetDbTableID(&tableID); + SetDbTableID(tableID); + srcCardDB->GetDbRowID(&rowID); + SetDbRowID(rowID); + + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBCardProperty::AddAnonymousAttributesToDB() +{ + nsresult rv = NS_OK; + if (mCardDatabase) + mCardDatabase = null_nsCOMPtr(); + rv = GetCardDatabase(kPersonalAddressbookUri); + if (NS_SUCCEEDED(rv) && mCardDatabase) + rv = mCardDatabase->AddAnonymousAttributesFromCard(this); + return rv; +} + +NS_IMETHODIMP nsAbMDBCardProperty::EditAnonymousAttributesInDB() +{ + nsresult rv = NS_OK; + if (mCardDatabase) + mCardDatabase = null_nsCOMPtr(); + rv = GetCardDatabase(kPersonalAddressbookUri); + if (NS_SUCCEEDED(rv) && mCardDatabase) + rv = mCardDatabase->EditAnonymousAttributesFromCard(this); + return rv; +} + + + + + + + + + +// nsIAbCard methods + +NS_IMETHODIMP nsAbMDBCardProperty::GetPrintCardUrl(char * *aPrintCardUrl) +{ +static const char *kAbPrintUrlFormat = "addbook:printone?email=%s&folder=%s"; + + if (!aPrintCardUrl) + return NS_OK; + + PRUnichar *email = nsnull; + GetPrimaryEmail(&email); + nsString emailStr(email); + + if (emailStr.Length() == 0) + { + *aPrintCardUrl = PR_smprintf(""); + return NS_OK; + } + PRUnichar *dirName = nsnull; + if (mCardDatabase) + mCardDatabase->GetDirectoryName(&dirName); + nsString dirNameStr(dirName); + if (dirNameStr.Length() == 0) + { + *aPrintCardUrl = PR_smprintf(""); + return NS_OK; + } + dirNameStr.ReplaceSubstring(NS_ConvertASCIItoUCS2(" "), NS_ConvertASCIItoUCS2("%20")); + + char *emailCharStr = emailStr.ToNewUTF8String(); + char *dirCharStr = dirNameStr.ToNewUTF8String(); + + *aPrintCardUrl = PR_smprintf(kAbPrintUrlFormat, emailCharStr, dirCharStr); + + nsMemory::Free(emailCharStr); + nsMemory::Free(dirCharStr); + + PR_FREEIF(dirName); + PR_FREEIF(email); + + return NS_OK; +} + + + +NS_IMETHODIMP nsAbMDBCardProperty::EditCardToDatabase(const char *uri) +{ + if (!mCardDatabase && uri) + GetCardDatabase(uri); + + if (mCardDatabase) + { + mCardDatabase->EditCard(this, PR_TRUE); + mCardDatabase->Commit(kLargeCommit); + return NS_OK; + } + else + return NS_ERROR_FAILURE; +} + +NS_IMETHODIMP nsAbMDBCardProperty::GetCollationKey(const PRUnichar *str, PRUnichar **key) +{ + nsresult rv = NS_OK; + nsAutoString resultStr; + + if (mCardDatabase) + { + rv = mCardDatabase->CreateCollationKey(str, resultStr); + *key = resultStr.ToNewUnicode(); + } + else + rv = NS_ERROR_FAILURE; + + return rv; +} + + + + + +// protected class methods + +nsresult nsAbMDBCardProperty::GetCardDatabase(const char *uri) +{ + nsresult rv = NS_OK; + + NS_WITH_SERVICE(nsIAddrBookSession, abSession, kAddrBookSessionCID, &rv); + if (NS_SUCCEEDED(rv)) + { + nsFileSpec* dbPath; + abSession->GetUserProfileDirectory(&dbPath); + + const char* file = nsnull; + file = &(uri[PL_strlen(kDirectoryDataSourceRoot)]); + (*dbPath) += file; + + if (dbPath->Exists()) + { + NS_WITH_SERVICE(nsIAddrDatabase, addrDBFactory, kAddressBookDBCID, &rv); + + if (NS_SUCCEEDED(rv) && addrDBFactory) + rv = addrDBFactory->Open(dbPath, PR_TRUE, getter_AddRefs(mCardDatabase), PR_TRUE); + } + else + rv = NS_ERROR_FAILURE; + delete dbPath; + } + return rv; +} + + +nsresult nsAbMDBCardProperty::RemoveAnonymousList(nsVoidArray* pArray) +{ + if (pArray) + { + PRUint32 count = pArray->Count(); + for (int i = count - 1; i >= 0; i--) + { + void* pPtr = pArray->ElementAt(i); + PR_FREEIF(pPtr); + pArray->RemoveElementAt(i); + } + delete pArray; + } + return NS_OK; +} + +nsresult nsAbMDBCardProperty::SetAnonymousAttribute +(nsVoidArray** pAttrAray, nsVoidArray** pValueArray, void *attrname, void *value) +{ + nsresult rv = NS_OK; + nsVoidArray* pAttributes = *pAttrAray; + nsVoidArray* pValues = *pValueArray; + + if (!pAttributes && !pValues) + { + pAttributes = new nsVoidArray(); + pValues = new nsVoidArray(); + *pAttrAray = pAttributes; + *pValueArray = pValues; + } + if (pAttributes && pValues) + { + if (attrname && value) + { + pAttributes->AppendElement(attrname); + pValues->AppendElement(value); + } + } + else + { + rv = NS_ERROR_FAILURE; + } + + return rv; +} diff --git a/mailnews/addrbook/src/nsAbMDBCardProperty.h b/mailnews/addrbook/src/nsAbMDBCardProperty.h new file mode 100644 index 000000000000..55ee34b7444c --- /dev/null +++ b/mailnews/addrbook/src/nsAbMDBCardProperty.h @@ -0,0 +1,68 @@ +/* -*- 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): Paul Sandoz + */ + +#ifndef nsAbMDBCardProperty_h__ +#define nsAbMDBCardProperty_h__ + +#include "nsIAbMDBCard.h" +#include "nsAbCardProperty.h" +#include "nsISupportsArray.h" +#include "nsCOMPtr.h" +#include "nsIAddrDatabase.h" + +class nsAbMDBCardProperty : public nsIAbMDBCard, public nsAbCardProperty +{ +public: + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_NSIABMDBCARD + + nsAbMDBCardProperty(void); + virtual ~nsAbMDBCardProperty(); + + // nsIAbCard methods + NS_IMETHODIMP GetPrintCardUrl(char * *aPrintCardUrl); + NS_IMETHODIMP EditCardToDatabase(const char *uri); + NS_IMETHODIMP GetCollationKey(const PRUnichar *str, PRUnichar **key); + +protected: + nsresult GetCardDatabase(const char *uri); + + PRUint32 m_Key; + PRUint32 m_dbTableID; + PRUint32 m_dbRowID; + + nsCOMPtr mCardDatabase; + + nsresult RemoveAnonymousList(nsVoidArray* pArray); + nsresult SetAnonymousAttribute(nsVoidArray** pAttrAray, + nsVoidArray** pValueArray, void *attrname, void *value); + + nsVoidArray* m_pAnonymousStrAttributes; + nsVoidArray* m_pAnonymousStrValues; + nsVoidArray* m_pAnonymousIntAttributes; + nsVoidArray* m_pAnonymousIntValues; + nsVoidArray* m_pAnonymousBoolAttributes; + nsVoidArray* m_pAnonymousBoolValues; + +}; + +#endif // nsAbMDBCardProperty_h__ diff --git a/mailnews/addrbook/src/nsAbMDBDirProperty.cpp b/mailnews/addrbook/src/nsAbMDBDirProperty.cpp new file mode 100644 index 000000000000..031852eb2f94 --- /dev/null +++ b/mailnews/addrbook/src/nsAbMDBDirProperty.cpp @@ -0,0 +1,215 @@ +/* -*- 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): + * Pierre Phaneuf + */ + +#include "nsAbMDBDirProperty.h" +#include "nsIRDFService.h" +#include "nsIRDFResource.h" +#include "nsIServiceManager.h" +#include "nsRDFCID.h" +#include "nsXPIDLString.h" +#include "nsCOMPtr.h" +#include "nsAbBaseCID.h" +#include "nsAddrDatabase.h" +#include "nsIAbCard.h" +#include "nsIAbListener.h" +#include "nsIAddrBookSession.h" +#include "nsIAddressBook.h" + +#include "mdb.h" +#include "prlog.h" +#include "prprf.h" +#include "prmem.h" + +static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID); +// static NS_DEFINE_CID(kAbCardCID, NS_ABCARD_CID); +static NS_DEFINE_CID(kAddrBookSessionCID, NS_ADDRBOOKSESSION_CID); +static NS_DEFINE_CID(kAddrBookCID, NS_ADDRESSBOOK_CID); + +nsAbMDBDirProperty::nsAbMDBDirProperty(void) +{ +} + +nsAbMDBDirProperty::~nsAbMDBDirProperty(void) +{ +} + + +NS_IMPL_ISUPPORTS_INHERITED(nsAbMDBDirProperty, nsAbDirProperty, nsIAbMDBDirectory) + +//////////////////////////////////////////////////////////////////////////////// + + + +// nsIAbMDBDirectory attributes + +NS_IMETHODIMP nsAbMDBDirProperty::GetDbRowID(PRUint32 *aDbRowID) +{ + *aDbRowID = m_dbRowID; + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBDirProperty::SetDbRowID(PRUint32 aDbRowID) +{ + m_dbRowID = aDbRowID; + return NS_OK; +} + + +// nsIAbMDBDirectory methods + +/* add mailing list to the parent directory */ +NS_IMETHODIMP nsAbMDBDirProperty::AddMailListToDirectory(nsIAbDirectory *mailList) +{ + if (!m_AddressList) + NS_NewISupportsArray(getter_AddRefs(m_AddressList)); + PRUint32 i, count; + m_AddressList->Count(&count); + for (i = 0; i < count; i++) + { + nsresult err; + nsCOMPtr pSupport = + getter_AddRefs(m_AddressList->ElementAt(i)); + nsCOMPtr pList(do_QueryInterface(pSupport, &err)); + if (mailList == pList.get()) + return NS_OK; + } + m_AddressList->AppendElement(mailList); + return NS_OK; +} + +/* add addresses to the mailing list */ +NS_IMETHODIMP nsAbMDBDirProperty::AddAddressToList(nsIAbCard *card) +{ + + if (!m_AddressList) + NS_NewISupportsArray(getter_AddRefs(m_AddressList)); + PRUint32 i, count; + m_AddressList->Count(&count); + for (i = 0; i < count; i++) + { + nsresult err; + nsCOMPtr pSupport = + getter_AddRefs(m_AddressList->ElementAt(i)); + nsCOMPtr pCard(do_QueryInterface(pSupport, &err)); + if (card == pCard.get()) + return NS_OK; + } + m_AddressList->AppendElement(card); + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBDirProperty::CopyDBMailList(nsIAbMDBDirectory* srcListDB) +{ + nsresult err = NS_OK; + nsCOMPtr srcList(do_QueryInterface(srcListDB)); + if (NS_FAILED(err)) + return NS_ERROR_NULL_POINTER; + + CopyMailList (srcList); + + PRUint32 rowID; + srcListDB->GetDbRowID(&rowID); + SetDbRowID(rowID); + + return NS_OK; +} + + + + + + +// nsIAbDirectory methods + +NS_IMETHODIMP nsAbMDBDirProperty::EditMailListToDatabase(const char *uri) +{ + nsresult rv = NS_OK; + + nsCOMPtr listDatabase; + + NS_WITH_SERVICE(nsIAddressBook, addresBook, kAddrBookCID, &rv); + if (NS_SUCCEEDED(rv)) + rv = addresBook->GetAbDatabaseFromURI(uri, getter_AddRefs(listDatabase)); + + if (listDatabase) + { + listDatabase->EditMailList(this, PR_TRUE); + listDatabase->Commit(kLargeCommit); + listDatabase = null_nsCOMPtr(); + + return NS_OK; + + } + else + return NS_ERROR_FAILURE; +} + + + + + + +// nsIAbMDBDirectory NOT IMPLEMENTED methods + +/* nsIAbCard addChildCards (in string uriName); */ +NS_IMETHODIMP nsAbMDBDirProperty::AddChildCards(const char *uriName, nsIAbCard **_retval) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +/* nsIAbDirectory addDirectory (in string uriName); */ +NS_IMETHODIMP nsAbMDBDirProperty::AddDirectory(const char *uriName, nsIAbDirectory **_retval) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +/* string getDirUri (); */ +NS_IMETHODIMP nsAbMDBDirProperty::GetDirUri(char **_retval) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +/* [noscript] void removeElementsFromAddressList (); */ +NS_IMETHODIMP nsAbMDBDirProperty::RemoveElementsFromAddressList() +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +/* void removeEmailAddressAt (in unsigned long aIndex); */ +NS_IMETHODIMP nsAbMDBDirProperty::RemoveEmailAddressAt(PRUint32 aIndex) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +/* [noscript] void notifyDirItemAdded (in nsISupports item); */ +NS_IMETHODIMP nsAbMDBDirProperty::NotifyDirItemAdded(nsISupports *item) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +/* [noscript] void clearDatabase (); */ +NS_IMETHODIMP nsAbMDBDirProperty::ClearDatabase() +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + diff --git a/mailnews/addrbook/src/nsAbMDBDirProperty.h b/mailnews/addrbook/src/nsAbMDBDirProperty.h new file mode 100644 index 000000000000..ac7e6c2b1f02 --- /dev/null +++ b/mailnews/addrbook/src/nsAbMDBDirProperty.h @@ -0,0 +1,60 @@ +/* -*- 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): + */ + +/******************************************************************************************************** + + Interface for representing Address Book Directory + +*********************************************************************************************************/ + +#ifndef nsAbMDBDirProperty_h__ +#define nsAbMDBDirProperty_h__ + +#include "nsIAbMDBDirectory.h" +#include "nsAbDirProperty.h" +#include "nsIAbCard.h" +#include "nsISupportsArray.h" +#include "nsCOMPtr.h" +#include "nsDirPrefs.h" +#include "nsIAddrDatabase.h" + + /* + * Address Book Directory + */ + +class nsAbMDBDirProperty: public nsIAbMDBDirectory, public nsAbDirProperty +{ +public: + nsAbMDBDirProperty(void); + virtual ~nsAbMDBDirProperty(void); + + NS_DECL_ISUPPORTS + NS_DECL_NSIABMDBDIRECTORY + + // nsIAbDirectory methods + NS_IMETHODIMP EditMailListToDatabase(const char *uri); +protected: + + PRUint32 m_dbRowID; +}; + +#endif diff --git a/mailnews/addrbook/src/nsAbMDBDirectory.cpp b/mailnews/addrbook/src/nsAbMDBDirectory.cpp new file mode 100644 index 000000000000..8c68e16992dc --- /dev/null +++ b/mailnews/addrbook/src/nsAbMDBDirectory.cpp @@ -0,0 +1,868 @@ +/* -*- 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): + * Pierre Phaneuf + */ + +#include "nsAbMDBDirectory.h" +#include "nsIRDFService.h" +#include "nsIRDFResource.h" +#include "nsIServiceManager.h" +#include "nsRDFCID.h" +#include "nsXPIDLString.h" +#include "nsCOMPtr.h" +#include "nsAbBaseCID.h" +#include "nsAddrDatabase.h" +#include "nsIAbMDBCard.h" +#include "nsIAbListener.h" +#include "nsIAddrBookSession.h" +#include "nsIAddressBook.h" + +#include "nsAbMDBCardProperty.h" + +#include "mdb.h" +#include "prlog.h" +#include "prprf.h" +#include "prmem.h" + +/* The definition is nsAddressBook.cpp */ +extern const char *kDirectoryDataSourceRoot; + +static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID); +static NS_DEFINE_CID(kAbCardCID, NS_ABMDBCARD_CID); +static NS_DEFINE_CID(kAddressBookDBCID, NS_ADDRDATABASE_CID); +static NS_DEFINE_CID(kAddrBookSessionCID, NS_ADDRBOOKSESSION_CID); +static NS_DEFINE_CID(kAddrBookCID, NS_ADDRESSBOOK_CID); + +nsAbMDBDirectory::nsAbMDBDirectory(void) + : nsAbMDBRDFResource(), + mInitialized(PR_FALSE), + mIsMailingList(-1) +{ + NS_NewISupportsArray(getter_AddRefs(mSubDirectories)); +} + +nsAbMDBDirectory::~nsAbMDBDirectory(void) +{ + if (mURI && PL_strcmp(mURI, kDirectoryDataSourceRoot)) + { + nsresult rv = NS_OK; + + nsCOMPtr database; + NS_WITH_SERVICE(nsIAddressBook, addressBook, kAddrBookCID, &rv); + if (NS_SUCCEEDED(rv) && addressBook) + { + rv = addressBook->GetAbDatabaseFromURI(mURI, getter_AddRefs(database)); + if (NS_SUCCEEDED(rv) && database) + database->RemoveListener(this); + } + } + if (mSubDirectories) + { + PRUint32 count; + nsresult rv = mSubDirectories->Count(&count); + NS_ASSERTION(NS_SUCCEEDED(rv), "Count failed"); + PRInt32 i; + for (i = count - 1; i >= 0; i--) + mSubDirectories->RemoveElementAt(i); + } +} + +NS_IMPL_ISUPPORTS_INHERITED2(nsAbMDBDirectory, nsAbMDBRDFResource, nsIAbDirectory, nsIAbMDBDirectory) + +//////////////////////////////////////////////////////////////////////////////// + + + +// Not called +nsresult nsAbMDBDirectory::AddMailList(const char *uriName) +{ + if (!uriName) + return NS_ERROR_NULL_POINTER; + + nsresult rv = NS_OK; + NS_WITH_SERVICE(nsIRDFService, rdf, kRDFServiceCID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr res; + rv = rdf->GetResource(uriName, getter_AddRefs(res)); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr directory(do_QueryInterface(res, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + mSubDirectories->AppendElement(directory); + + return rv; +} + +nsresult nsAbMDBDirectory::RemoveCardFromAddressList(const nsIAbCard* card) +{ + nsresult rv = NS_OK; + PRUint32 listTotal; + PRInt32 i, j; + rv = m_AddressList->Count(&listTotal); + for (i = listTotal - 1; i >= 0; i--) + { + nsCOMPtr pSupport = getter_AddRefs(m_AddressList->ElementAt(i)); + if (!pSupport) + continue; + + nsCOMPtr listDir(do_QueryInterface(pSupport, &rv)); + if (listDir) + { + nsISupportsArray* pAddressLists; + listDir->GetAddressLists(&pAddressLists); + if (pAddressLists) + { + PRUint32 total; + rv = pAddressLists->Count(&total); + for (j = total - 1; j >= 0; j--) + { + nsCOMPtr pSupport = getter_AddRefs(pAddressLists->ElementAt(j)); + nsCOMPtr cardInList(do_QueryInterface(pSupport, &rv)); + if (card == cardInList.get()) + pAddressLists->RemoveElementAt(j); + } + } + } + } + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBDirectory::DeleteDirectory(nsIAbDirectory *directory) +{ + nsresult rv = NS_OK; + + if (!directory) + return NS_ERROR_FAILURE; + + nsCOMPtr dbdirectory(do_QueryInterface(directory, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + nsXPIDLCString uri; + rv = dbdirectory->GetDirUri(getter_Copies(uri)); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr database; + NS_WITH_SERVICE(nsIAddressBook, addresBook, kAddrBookCID, &rv); + if (NS_SUCCEEDED(rv)) + { + rv = addresBook->GetAbDatabaseFromURI((const char *)uri, getter_AddRefs(database)); + + if (NS_SUCCEEDED(rv)) + rv = database->DeleteMailList(directory, PR_TRUE); + + if (NS_SUCCEEDED(rv)) + database->Commit(kLargeCommit); + + if (m_AddressList) + m_AddressList->RemoveElement(directory); + rv = mSubDirectories->RemoveElement(directory); + + NotifyItemDeleted(directory); + } + + + return rv; +} + +nsresult nsAbMDBDirectory::NotifyPropertyChanged(char *property, PRUnichar* oldValue, PRUnichar* newValue) +{ + nsCOMPtr supports; + if(NS_SUCCEEDED(QueryInterface(NS_GET_IID(nsISupports), getter_AddRefs(supports)))) + { + //Notify listeners who listen to every folder + nsresult rv; + NS_WITH_SERVICE(nsIAddrBookSession, abSession, kAddrBookSessionCID, &rv); + if(NS_SUCCEEDED(rv)) + abSession->NotifyItemPropertyChanged(supports, property, oldValue, newValue); + } + + return NS_OK; +} + +nsresult nsAbMDBDirectory::NotifyItemAdded(nsISupports *item) +{ + nsresult rv = NS_OK; + NS_WITH_SERVICE(nsIAddrBookSession, abSession, kAddrBookSessionCID, &rv); + if(NS_SUCCEEDED(rv)) + abSession->NotifyDirectoryItemAdded(this, item); + return NS_OK; +} + +nsresult nsAbMDBDirectory::NotifyItemDeleted(nsISupports *item) +{ + nsresult rv = NS_OK; + NS_WITH_SERVICE(nsIAddrBookSession, abSession, kAddrBookSessionCID, &rv); + if(NS_SUCCEEDED(rv)) + abSession->NotifyDirectoryItemDeleted(this, item); + + return NS_OK; +} + + + + + + + + + +// nsIAbMDBDirectory methods + +NS_IMETHODIMP nsAbMDBDirectory::ClearDatabase() +{ + if (mDatabase) + { + mDatabase->RemoveListener(this); + mDatabase = null_nsCOMPtr(); + } + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBDirectory::RemoveElementsFromAddressList() +{ + if (m_AddressList) + { + PRUint32 count; + nsresult rv = m_AddressList->Count(&count); + NS_ASSERTION(NS_SUCCEEDED(rv), "Count failed"); + PRInt32 i; + for (i = count - 1; i >= 0; i--) + m_AddressList->RemoveElementAt(i); + } + m_AddressList = null_nsCOMPtr(); + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBDirectory::RemoveEmailAddressAt(PRUint32 aIndex) +{ + if (m_AddressList) + { + return m_AddressList->RemoveElementAt(aIndex); + } + else + return NS_ERROR_FAILURE; +} + +NS_IMETHODIMP nsAbMDBDirectory::AddChildCards(const char *uriName, nsIAbCard **childCard) +{ + if(!childCard) + return NS_ERROR_NULL_POINTER; + + nsresult rv = NS_OK; + NS_WITH_SERVICE(nsIRDFService, rdf, kRDFServiceCID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr res; + rv = rdf->GetResource(uriName, getter_AddRefs(res)); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr personCard(do_QueryInterface(res, &rv)); + if (NS_FAILED(rv)) + { + rv = nsComponentManager::CreateInstance(kAbCardCID, nsnull, NS_GET_IID(nsIAbCard), getter_AddRefs(personCard)); + NS_ENSURE_SUCCESS(rv, rv); + if (!personCard) + return NS_ERROR_NULL_POINTER; + } + + *childCard = personCard; + NS_ADDREF(*childCard); + + return rv; +} + +NS_IMETHODIMP nsAbMDBDirectory::AddDirectory(const char *uriName, nsIAbDirectory **childDir) +{ + if (!childDir || !uriName) + return NS_ERROR_NULL_POINTER; + + nsresult rv = NS_OK; + NS_WITH_SERVICE(nsIRDFService, rdf, kRDFServiceCID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr res; + rv = rdf->GetResource(uriName, getter_AddRefs(res)); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr directory(do_QueryInterface(res, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + mSubDirectories->AppendElement(directory); + *childDir = directory; + NS_IF_ADDREF(*childDir); + + return rv; +} + +NS_IMETHODIMP nsAbMDBDirectory::GetDirUri(char **uri) +{ + if (uri) + { + if (mURI) + *uri = nsCRT::strdup(mURI); + else + *uri = nsCRT::strdup(""); + return NS_OK; + } + else + return NS_RDF_NO_VALUE; +} + +// nsIAbDirectory methods + +NS_IMETHODIMP nsAbMDBDirectory::GetChildNodes(nsIEnumerator* *result) +{ + if (!mInitialized) + { + mInitialized = PR_TRUE; + } + return mSubDirectories->Enumerate(result); +} + +NS_IMETHODIMP nsAbMDBDirectory::GetChildCards(nsIEnumerator* *result) +{ + if (mURI && mIsMailingList == -1) + { + nsString file; file.AssignWithConversion(&(mURI[PL_strlen(kDirectoryDataSourceRoot)])); + PRInt32 pos = file.Find("/"); + if (pos != -1) + mIsMailingList = 1; + else + mIsMailingList = 0; + } + + nsresult rv = GetAbDatabase(); + + if (NS_SUCCEEDED(rv) && mDatabase) + { + if (mIsMailingList == 0) + rv = mDatabase->EnumerateCards(this, result); + else if (mIsMailingList == 1) + rv = mDatabase->EnumerateListAddresses(this, result); + } + + return rv; +} + + +// Not called +nsresult nsAbMDBDirectory::DeleteDirectoryCards(nsIAbDirectory* directory, DIR_Server *server) +{ + if (!server->fileName) // file name does not exist + return NS_OK; + if (PL_strlen(server->fileName) == 0) // file name does not exist + return NS_OK; + + nsresult rv = NS_OK; + nsFileSpec* dbPath = nsnull; + nsCOMPtr database; + + + NS_WITH_SERVICE(nsIAddrBookSession, abSession, kAddrBookSessionCID, &rv); + if(NS_SUCCEEDED(rv)) + abSession->GetUserProfileDirectory(&dbPath); + + if (dbPath) + { + (*dbPath) += server->fileName; + + // close file before delete it + NS_WITH_SERVICE(nsIAddrDatabase, addrDBFactory, kAddressBookDBCID, &rv); + + if (NS_SUCCEEDED(rv) && addrDBFactory) + rv = addrDBFactory->Open(dbPath, PR_FALSE, getter_AddRefs(database), PR_TRUE); + } + + /* delete cards */ + nsCOMPtr cardArray; + nsCOMPtr cardChild; + + NS_NewISupportsArray(getter_AddRefs(cardArray)); + rv = directory->GetChildCards(getter_AddRefs(cardChild)); + + if (NS_SUCCEEDED(rv) && cardChild) + { + nsCOMPtr item; + rv = cardChild->First(); + if (NS_SUCCEEDED(rv)) + { + do + { + cardChild->CurrentItem(getter_AddRefs(item)); + if (item) + { + nsCOMPtr card; + card = do_QueryInterface(item, &rv); + if (card) + { + cardArray->AppendElement(card); + } + } + rv = cardChild->Next(); + } while (NS_SUCCEEDED(rv)); + + if (database) + { + PRUint32 cardCount; + rv = cardArray->Count(&cardCount); + NS_ENSURE_SUCCESS(rv, rv); + for(PRUint32 i = 0; i < cardCount; i++) + { + nsCOMPtr cardSupports = getter_AddRefs(cardArray->ElementAt(i)); + nsCOMPtr card = do_QueryInterface(cardSupports, &rv); + if (card) + { + database->DeleteCard(card, PR_TRUE); + } + } + } + } + } + return rv; +} + +NS_IMETHODIMP nsAbMDBDirectory::DeleteCards(nsISupportsArray *cards) +{ + nsresult rv = NS_OK; + + if (!mDatabase) + rv = GetAbDatabase(); + + if (NS_SUCCEEDED(rv) && mDatabase) + { + PRUint32 cardCount; + PRUint32 i; + rv = cards->Count(&cardCount); + NS_ENSURE_SUCCESS(rv, rv); + for (i = 0; i < cardCount; i++) + { + nsCOMPtr cardSupports; + nsCOMPtr card; + nsCOMPtr dbcard; + cardSupports = getter_AddRefs(cards->ElementAt(i)); + card = do_QueryInterface(cardSupports, &rv); + NS_ENSURE_SUCCESS(rv, rv); + dbcard = do_QueryInterface(card, &rv); + NS_ENSURE_SUCCESS(rv, rv); + if (card) + { + if (IsMailingList()) + { + mDatabase->DeleteCardFromMailList(this, card, PR_TRUE); + + PRUint32 cardTotal; + PRInt32 i; + rv = m_AddressList->Count(&cardTotal); + for (i = cardTotal - 1; i >= 0; i--) + { + nsCOMPtr pSupport = getter_AddRefs(m_AddressList->ElementAt(i)); + if (!pSupport) + continue; + + nsCOMPtr dbarrayCard(do_QueryInterface(pSupport, &rv)); + if (dbarrayCard) + { + PRUint32 tableID, rowID, cardTableID, cardRowID; + dbarrayCard->GetDbTableID(&tableID); + dbarrayCard->GetDbRowID(&rowID); + dbcard->GetDbTableID(&cardTableID); + dbcard->GetDbRowID(&cardRowID); + if (tableID == cardTableID && rowID == cardRowID) + m_AddressList->RemoveElementAt(i); + } + } + } + else + { + mDatabase->DeleteCard(card, PR_TRUE); + PRBool bIsMailList = PR_FALSE; + card->GetIsMailList(&bIsMailList); + if (bIsMailList) + { + //to do, get mailing list dir side uri and notify rdf to remove it + PRUint32 rowID; + dbcard->GetDbRowID(&rowID); + char *listUri = PR_smprintf("%s/MailList%ld", mURI, rowID); + if (listUri) + { + nsresult rv = NS_OK; + NS_WITH_SERVICE(nsIRDFService, rdfService, kRDFServiceCID, &rv); + + if(NS_SUCCEEDED(rv)) + { + nsCOMPtr listResource; + rv = rdfService->GetResource(listUri, getter_AddRefs(listResource)); + nsCOMPtr listDir = do_QueryInterface(listResource, &rv); + if(NS_SUCCEEDED(rv)) + { + if (m_AddressList) + m_AddressList->RemoveElement(listDir); + rv = mSubDirectories->RemoveElement(listDir); + + if (listDir) + NotifyItemDeleted(listDir); + PR_smprintf_free(listUri); + } + else + { + PR_smprintf_free(listUri); + return rv; + } + } + else + { + PR_smprintf_free(listUri); + return rv; + } + } + } + else + { + RemoveCardFromAddressList(card); + } + } + } + } + mDatabase->Commit(kLargeCommit); + } + return rv; +} + +NS_IMETHODIMP nsAbMDBDirectory::HasCard(nsIAbCard *cards, PRBool *hasCard) +{ + if(!hasCard) + return NS_ERROR_NULL_POINTER; + + + nsresult rv = NS_OK; + if (!mDatabase) + rv = GetAbDatabase(); + + if(NS_SUCCEEDED(rv) && mDatabase) + { + if(NS_SUCCEEDED(rv)) + rv = mDatabase->ContainsCard(cards, hasCard); + + } + return rv; +} + +NS_IMETHODIMP nsAbMDBDirectory::HasDirectory(nsIAbDirectory *dir, PRBool *hasDir) +{ + if (!hasDir) + return NS_ERROR_NULL_POINTER; + + nsresult rv = NS_ERROR_FAILURE; + + nsCOMPtr dbdir(do_QueryInterface(dir, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + PRBool bIsMailingList = PR_FALSE; + dir->GetIsMailList(&bIsMailingList); + if (bIsMailingList) + { + nsXPIDLCString uri; + rv = dbdir->GetDirUri(getter_Copies(uri)); + NS_ENSURE_SUCCESS(rv, rv); + nsCOMPtr database; + NS_WITH_SERVICE(nsIAddressBook, addresBook, kAddrBookCID, &rv); + if (NS_SUCCEEDED(rv)) + { + rv = addresBook->GetAbDatabaseFromURI((const char *)uri, getter_AddRefs(database)); + } + if(NS_SUCCEEDED(rv) && database) + { + if(NS_SUCCEEDED(rv)) + rv = database->ContainsMailList(dir, hasDir); + } + } + + return rv; +} + +NS_IMETHODIMP nsAbMDBDirectory::CreateNewDirectory(PRUint32 prefCount, const char **prefName, const PRUnichar **prefValue) +{ + if (!*prefName || !*prefValue) + return NS_ERROR_NULL_POINTER; + + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP nsAbMDBDirectory::CreateDirectoryByURI(const PRUnichar *dirName, const char *uri, PRBool migrating) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP nsAbMDBDirectory::AddMailList(nsIAbDirectory *list) +{ + nsresult rv = NS_OK; + if (!mDatabase) + rv = GetAbDatabase(); + + if (!(NS_SUCCEEDED(rv) || mDatabase)) + return NS_ERROR_FAILURE; + + nsCOMPtr dblist(do_QueryInterface(list, &rv)); + if (NS_FAILED(rv)) + { + nsAbMDBDirProperty* dblistproperty = new nsAbMDBDirProperty (); + NS_ADDREF(dblistproperty); + nsCOMPtr newlist; + newlist = getter_AddRefs(dblistproperty); + newlist->CopyMailList(list); + list = newlist; + dblist = do_QueryInterface(list); + rv = NS_OK; + } + + mDatabase->CreateMailListAndAddToDB(list, PR_TRUE); + mDatabase->Commit(kLargeCommit); + + PRUint32 dbRowID; + dblist->GetDbRowID(&dbRowID); + char *listUri = PR_smprintf("%s/MailList%ld", mURI, dbRowID); + + nsCOMPtr newList; + rv = AddDirectory(listUri, getter_AddRefs(newList)); + nsCOMPtr dbnewList(do_QueryInterface(newList)); + if (NS_SUCCEEDED(rv) && newList) + { + nsCOMPtr listener(do_QueryInterface(newList, &rv)); + NS_ENSURE_SUCCESS(rv, NS_ERROR_NULL_POINTER); + mDatabase->AddListener(listener); + + dbnewList->CopyDBMailList (dblist); + AddMailListToDirectory(newList); + NotifyItemAdded(newList); + } + + return rv; +} + +NS_IMETHODIMP nsAbMDBDirectory::AddCard(nsIAbCard* card, nsIAbCard **_retval) +{ + nsresult rv = NS_OK; + if (!mDatabase) + rv = GetAbDatabase(); + + if (!(NS_SUCCEEDED(rv) || mDatabase)) + return NS_ERROR_FAILURE; + + nsCOMPtr dbcard(do_QueryInterface(card, &rv)); + if (NS_FAILED(rv) || !dbcard) + { + nsAbMDBCardProperty* dbcardproperty = new nsAbMDBCardProperty (); + NS_ADDREF(dbcardproperty); + nsCOMPtr newcard; + newcard = getter_AddRefs(dbcardproperty); + newcard->Copy (card); + card = newcard; + dbcard = do_QueryInterface(card); + rv = NS_OK; + } + + dbcard->SetAbDatabase (mDatabase); + if (mIsMailingList == 1) + mDatabase->CreateNewListCardAndAddToDB(m_dbRowID, card, PR_TRUE); + else + mDatabase->CreateNewCardAndAddToDB(card, PR_TRUE); + mDatabase->Commit(kLargeCommit); + + // Return the RDF resource instance of the card + // which was added to the database + // + NS_WITH_SERVICE(nsIRDFService, rdf, kRDFServiceCID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + nsXPIDLCString uri; + rv = dbcard->GetCardURI(getter_Copies (uri)); + + nsCOMPtr resource; + rv = rdf->GetResource(uri, getter_AddRefs(resource)); + + nsCOMPtr cardInstance (do_QueryInterface (resource, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + *_retval = cardInstance; + NS_ADDREF(*_retval); + + return rv; +} + +NS_IMETHODIMP nsAbMDBDirectory::DropCard(nsIAbCard* card, nsIAbCard **_retval) +{ + nsresult rv = NS_OK; + + if (mURI && mIsMailingList == -1) + { + nsString file; file.AssignWithConversion(&(mURI[PL_strlen(kDirectoryDataSourceRoot)])); + PRInt32 pos = file.Find("/"); + if (pos != -1) + mIsMailingList = 1; + else + mIsMailingList = 0; + } + if (!mDatabase) + rv = GetAbDatabase(); + + if (!(NS_SUCCEEDED(rv) || mDatabase)) + return NS_ERROR_FAILURE; + + nsCOMPtr dbcard(do_QueryInterface(card, &rv)); + if (NS_FAILED(rv)) + { + nsAbMDBCardProperty* dbcardproperty = new nsAbMDBCardProperty (); + NS_ADDREF(dbcardproperty); + nsCOMPtr newcard; + newcard = getter_AddRefs (dbcardproperty); + newcard->Copy (card); + card = newcard; + dbcard = do_QueryInterface (card); + rv = NS_OK; + } + + dbcard->SetAbDatabase (mDatabase); + if (mIsMailingList == 1) + mDatabase->CreateNewListCardAndAddToDB(m_dbRowID, card, PR_TRUE); + else + mDatabase->CreateNewCardAndAddToDB(card, PR_TRUE); + mDatabase->Commit(kLargeCommit); + + + // Return the RDF resource instance of the card + // which was added to the database + // + NS_WITH_SERVICE(nsIRDFService, rdf, kRDFServiceCID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + nsXPIDLCString uri; + rv = dbcard->GetCardURI(getter_Copies (uri)); + + nsCOMPtr resource; + rv = rdf->GetResource(uri, getter_AddRefs(resource)); + + nsCOMPtr cardInstance (do_QueryInterface (resource, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + *_retval = cardInstance; + NS_ADDREF(*_retval); + + return NS_OK; +} + + + + + + + +// nsIAddrDBListener methods + +NS_IMETHODIMP nsAbMDBDirectory::OnCardAttribChange(PRUint32 abCode, nsIAddrDBListener *instigator) +{ + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBDirectory::OnCardEntryChange +(PRUint32 abCode, nsIAbCard *card, nsIAddrDBListener *instigator) +{ + nsresult rv = NS_OK; + if (abCode == AB_NotifyInserted && card) + { + NS_WITH_SERVICE(nsIRDFService, rdf, kRDFServiceCID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + char* cardURI = nsnull; + nsCOMPtr dbcard(do_QueryInterface(card, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + if (!dbcard) + return NS_ERROR_FAILURE; + + rv = dbcard->GetCardURI(&cardURI); + if (!cardURI) + return NS_ERROR_NULL_POINTER; + + nsCOMPtr res; + rv = rdf->GetResource(cardURI, getter_AddRefs(res)); + if(cardURI) + PR_smprintf_free(cardURI); + if (NS_SUCCEEDED(rv)) + { + nsCOMPtr dbpersonCard = do_QueryInterface(res); + if (dbpersonCard) + { + dbpersonCard->CopyCard(dbcard); + if (mDatabase) + { + dbpersonCard->SetAbDatabase(mDatabase); + nsCOMPtr listener(do_QueryInterface(dbpersonCard, &rv)); + NS_ENSURE_SUCCESS(rv, NS_ERROR_NULL_POINTER); + mDatabase->AddListener(listener); + } + nsCOMPtr cardSupports(do_QueryInterface(dbpersonCard)); + if (cardSupports) + { + NotifyItemAdded(cardSupports); + } + } + } + } + else if (abCode == AB_NotifyDeleted && card) + { + nsCOMPtr cardSupports(do_QueryInterface(card, &rv)); + if(NS_SUCCEEDED(rv)) + NotifyItemDeleted(cardSupports); + } + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBDirectory::OnListEntryChange +(PRUint32 abCode, nsIAbDirectory *list, nsIAddrDBListener *instigator) +{ + nsresult rv = NS_OK; + + if (abCode == AB_NotifyPropertyChanged && list) + { + PRBool bIsMailList = PR_FALSE; + list->GetIsMailList(&bIsMailList); + + PRUint32 rowID; + nsCOMPtr dblist(do_QueryInterface(list, &rv)); + if(NS_SUCCEEDED(rv)) + { + dblist->GetDbRowID(&rowID); + + if (bIsMailList && m_dbRowID == rowID) + { + nsXPIDLString pListName; + list->GetListName(getter_Copies(pListName)); + if (pListName) + NotifyPropertyChanged("DirName", nsnull, + NS_CONST_CAST(PRUnichar*, (const PRUnichar*)pListName)); + } + } + + } + return rv; +} + diff --git a/mailnews/addrbook/src/nsAbMDBDirectory.h b/mailnews/addrbook/src/nsAbMDBDirectory.h new file mode 100644 index 000000000000..080a7ac4796d --- /dev/null +++ b/mailnews/addrbook/src/nsAbMDBDirectory.h @@ -0,0 +1,99 @@ +/* -*- 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): + */ + +/******************************************************************************************************** + + Interface for representing Address Book Directory + +*********************************************************************************************************/ + +#ifndef nsAbMDBDirectory_h__ +#define nsAbMDBDirectory_h__ + +#include "nsAbMDBRDFResource.h" +#include "nsAbMDBDirProperty.h" +#include "nsIAbCard.h" +#include "nsISupportsArray.h" +#include "nsCOMPtr.h" +#include "nsDirPrefs.h" + + /* + * Address Book Directory + */ + +class nsAbMDBDirectory: public nsAbMDBRDFResource, public nsAbMDBDirProperty +{ +public: + nsAbMDBDirectory(void); + virtual ~nsAbMDBDirectory(void); + + NS_DECL_ISUPPORTS_INHERITED + + // nsIAbMDBDirectory methods + NS_IMETHOD ClearDatabase(); + NS_IMETHOD NotifyDirItemAdded(nsISupports *item) { return NotifyItemAdded(item);} + NS_IMETHOD RemoveElementsFromAddressList(); + NS_IMETHOD RemoveEmailAddressAt(PRUint32 aIndex); + NS_IMETHOD AddChildCards(const char *uriName, nsIAbCard **childCard); + NS_IMETHOD AddDirectory(const char *uriName, nsIAbDirectory **childDir); + NS_IMETHOD GetDirUri(char **uri); + + // nsIAbDirectory methods: + NS_IMETHOD GetChildNodes(nsIEnumerator* *result); + NS_IMETHOD GetChildCards(nsIEnumerator* *result); + NS_IMETHOD DeleteDirectory(nsIAbDirectory *directory); + NS_IMETHOD DeleteCards(nsISupportsArray *cards); + NS_IMETHOD HasCard(nsIAbCard *cards, PRBool *hasCard); + NS_IMETHOD HasDirectory(nsIAbDirectory *dir, PRBool *hasDir); + NS_IMETHOD CreateNewDirectory(PRUint32 prefCount, const char **prefName, const PRUnichar **prefValue); + NS_IMETHOD CreateDirectoryByURI(const PRUnichar *dirName, const char *uri, PRBool migrating); + NS_IMETHOD AddMailList(nsIAbDirectory *list); + NS_IMETHOD AddCard(nsIAbCard *card, nsIAbCard **_retval); + NS_IMETHOD DropCard(nsIAbCard *card, nsIAbCard **_retval); + + // nsIAddrDBListener methods: + NS_IMETHOD OnCardAttribChange(PRUint32 abCode, nsIAddrDBListener *instigator); + NS_IMETHOD OnCardEntryChange(PRUint32 abCode, nsIAbCard *card, nsIAddrDBListener *instigator); + NS_IMETHOD OnListEntryChange(PRUint32 abCode, nsIAbDirectory *list, nsIAddrDBListener *instigator); + + + PRBool IsMailingList(){ return (mIsMailingList == 1); } + +protected: + nsresult NotifyPropertyChanged(char *property, PRUnichar* oldValue, PRUnichar* newValue); + nsresult NotifyItemAdded(nsISupports *item); + nsresult NotifyItemDeleted(nsISupports *item); + nsresult AddChildCards(nsAutoString name, nsIAbCard **childDir); + nsresult DeleteDirectoryCards(nsIAbDirectory* directory, DIR_Server *server); + nsresult RemoveCardFromAddressList(const nsIAbCard* card); + + nsresult AddMailList(const char *uriName); + + nsVoidArray* GetDirList(){ return DIR_GetDirectories(); } + +protected: + nsCOMPtr mSubDirectories; + PRBool mInitialized; + PRInt16 mIsMailingList; +}; + +#endif diff --git a/mailnews/addrbook/src/nsAbMDBRDFResource.cpp b/mailnews/addrbook/src/nsAbMDBRDFResource.cpp new file mode 100644 index 000000000000..f905a440b1c7 --- /dev/null +++ b/mailnews/addrbook/src/nsAbMDBRDFResource.cpp @@ -0,0 +1,118 @@ +/* -*- 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 "nsAbMDBRDFResource.h" +#include "nsIRDFService.h" +#include "nsIServiceManager.h" +#include "nsRDFCID.h" +#include "nsAbBaseCID.h" +#include "prmem.h" + +#include "nsAddrDatabase.h" +#include "nsIAddrBookSession.h" + +static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID); +static NS_DEFINE_CID(kAddressBookDBCID, NS_ADDRDATABASE_CID); +static NS_DEFINE_CID(kAddrBookSessionCID, NS_ADDRBOOKSESSION_CID); + +/* The definition is nsAddressBook.cpp */ +extern const char *kDirectoryDataSourceRoot; + +/* The definition is nsAddrDatabase.cpp */ +extern const char *kMainPersonalAddressBook; + + +nsAbMDBRDFResource::nsAbMDBRDFResource(void) +{ + NS_INIT_REFCNT(); + + mDatabase = null_nsCOMPtr(); +} + +nsAbMDBRDFResource::~nsAbMDBRDFResource(void) +{ + if (mDatabase) + { + mDatabase->RemoveListener(this); + mDatabase->Close(PR_TRUE); + mDatabase = null_nsCOMPtr(); + } +} + +NS_IMPL_ISUPPORTS_INHERITED(nsAbMDBRDFResource, nsRDFResource, nsIAddrDBListener) + +//////////////////////////////////////////////////////////////////////////////// + +NS_IMETHODIMP nsAbMDBRDFResource::OnCardAttribChange(PRUint32 abCode, nsIAddrDBListener *instigator) +{ + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBRDFResource::OnCardEntryChange +(PRUint32 abCode, nsIAbCard *card, nsIAddrDBListener *instigator) +{ + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBRDFResource::OnListEntryChange +(PRUint32 abCode, nsIAbDirectory *list, nsIAddrDBListener *instigator) +{ + return NS_OK; +} + +NS_IMETHODIMP nsAbMDBRDFResource::OnAnnouncerGoingAway(nsIAddrDBAnnouncer *instigator) +{ + return NS_OK; +} + +nsresult nsAbMDBRDFResource::GetAbDatabase() +{ + nsresult rv = NS_OK; + if (!mDatabase && mURI) + { + nsFileSpec* dbPath = nsnull; + + NS_WITH_SERVICE(nsIAddrBookSession, abSession, kAddrBookSessionCID, &rv); + if(NS_SUCCEEDED(rv)) + abSession->GetUserProfileDirectory(&dbPath); + + nsString file; file.AssignWithConversion(&(mURI[PL_strlen(kDirectoryDataSourceRoot)])); + PRInt32 pos = file.Find("/"); + if (pos != -1) + file.Truncate(pos); + (*dbPath) += file; + + NS_WITH_SERVICE(nsIAddrDatabase, addrDBFactory, kAddressBookDBCID, &rv); + + if (NS_SUCCEEDED(rv) && addrDBFactory) + rv = addrDBFactory->Open(dbPath, PR_TRUE, getter_AddRefs(mDatabase), PR_TRUE); + + if (mDatabase) + mDatabase->AddListener(this); + + return NS_OK; + } + if (!mDatabase) + return NS_ERROR_NULL_POINTER; + return NS_OK; +} + diff --git a/mailnews/addrbook/src/nsAbMDBRDFResource.h b/mailnews/addrbook/src/nsAbMDBRDFResource.h new file mode 100644 index 000000000000..0a2cbd69145b --- /dev/null +++ b/mailnews/addrbook/src/nsAbMDBRDFResource.h @@ -0,0 +1,62 @@ +/* -*- 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): + */ + +/******************************************************************************************************** + + Interface for representing Address Book Directory + +*********************************************************************************************************/ + +#ifndef nsAbMDBRDFResource_h__ +#define nsAbMDBRDFResource_h__ + +#include "nsRDFResource.h" +#include "nsIAbCard.h" +#include "nsIAbDirectory.h" +#include "nsISupportsArray.h" +#include "nsCOMPtr.h" +#include "nsDirPrefs.h" +#include "nsIAddrDatabase.h" +#include "nsIAddrDBListener.h" + + /* + * Address Book RDF Resources and DB listener + */ + +class nsAbMDBRDFResource: public nsRDFResource, public nsIAddrDBListener +{ +public: + nsAbMDBRDFResource(void); + virtual ~nsAbMDBRDFResource(void); + + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_NSIADDRDBLISTENER + +protected: + + nsresult GetAbDatabase(); + + nsCOMPtr mDatabase; + +}; + +#endif