Back to index

lightning-sunbird  0.9+nobinonly
nsAbMDBDirFactory.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Sun Microsystems, Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Paul Sandoz <paul.sandoz@sun.com>
00024  *   Csaba Borbola <csaba.borbola@sun.com>
00025  *   Seth Spitzer <sspitzer@netscape.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either of the GNU General Public License Version 2 or later (the "GPL"),
00029  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "nsAbMDBDirFactory.h"
00042 #include "nsAbUtils.h"
00043 
00044 #include "nsIRDFService.h"
00045 #include "nsIRDFResource.h"
00046 #include "nsRDFResource.h"
00047 
00048 #include "nsIAbMDBDirectory.h"
00049 #include "nsAbDirFactoryService.h"
00050 #include "nsAbMDBDirFactory.h"
00051 #include "nsIAddressBook.h"
00052 #include "nsIAddrBookSession.h"
00053 #include "nsIAddrDBListener.h"
00054 
00055 #include "nsEnumeratorUtils.h"
00056 
00057 #include "nsAbBaseCID.h"
00058 
00059 NS_IMPL_ISUPPORTS1(nsAbMDBDirFactory, nsIAbDirFactory)
00060 
00061 nsAbMDBDirFactory::nsAbMDBDirFactory()
00062 {
00063 }
00064 
00065 nsAbMDBDirFactory::~nsAbMDBDirFactory()
00066 {
00067 }
00068 
00069 static nsresult RemoveMailListDBListeners (nsIAddrDatabase* database, nsIAbDirectory* directory)
00070 {
00071     nsresult rv;
00072 
00073     nsCOMPtr<nsISupportsArray> pAddressLists;
00074     rv = directory->GetAddressLists(getter_AddRefs(pAddressLists));
00075     NS_ENSURE_SUCCESS(rv, rv);
00076 
00077     PRUint32 total;
00078     rv = pAddressLists->Count(&total);
00079     NS_ENSURE_SUCCESS(rv, rv);
00080 
00081     for (PRUint32 i = 0; i < total; i++)
00082     {
00083         nsCOMPtr<nsISupports> pSupport;
00084         rv = pAddressLists->GetElementAt(i, getter_AddRefs(pSupport));
00085         if (NS_FAILED(rv))
00086             break;
00087 
00088         nsCOMPtr<nsIAbDirectory> listDir(do_QueryInterface(pSupport, &rv));
00089         if (NS_FAILED(rv))
00090             break;
00091         nsCOMPtr<nsIAddrDBListener> dbListener(do_QueryInterface(pSupport, &rv));
00092         if (NS_FAILED(rv))
00093             break;
00094 
00095         database->RemoveListener(dbListener);
00096     }
00097 
00098     return NS_OK;
00099 }
00100 
00101 NS_IMETHODIMP nsAbMDBDirFactory::CreateDirectory(nsIAbDirectoryProperties *aProperties,
00102     nsISimpleEnumerator **_retval)
00103 {
00104     NS_ENSURE_ARG_POINTER(aProperties);
00105     NS_ENSURE_ARG_POINTER(_retval);
00106 
00107     nsresult rv;
00108 
00109     nsXPIDLCString uri;
00110     nsAutoString description;
00111     nsXPIDLCString prefName;
00112 
00113     rv = aProperties->GetDescription(description);
00114     NS_ENSURE_SUCCESS(rv, rv);
00115 
00116     rv = aProperties->GetURI(getter_Copies(uri));
00117     NS_ENSURE_SUCCESS(rv, rv);
00118     
00119     rv = aProperties->GetPrefName(getter_Copies(prefName));
00120     NS_ENSURE_SUCCESS(rv, rv);
00121     
00122        nsCOMPtr<nsIRDFService> rdf = do_GetService (NS_RDF_CONTRACTID "/rdf-service;1", &rv);
00123     NS_ENSURE_SUCCESS(rv, rv);
00124 
00125     nsCOMPtr<nsIRDFResource> resource;
00126     rv = rdf->GetResource(uri, getter_AddRefs(resource));
00127     NS_ENSURE_SUCCESS(rv, rv);
00128 
00129     nsCOMPtr<nsIAbDirectory> directory(do_QueryInterface(resource, &rv));
00130     NS_ENSURE_SUCCESS(rv, rv);
00131 
00132     rv = directory->SetDirName(description.get());
00133     NS_ENSURE_SUCCESS(rv, rv);
00134 
00135     rv = directory->SetDirPrefId(prefName);
00136     NS_ENSURE_SUCCESS(rv, rv);
00137 
00138     nsCOMPtr<nsIAddrBookSession> abSession = do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv);
00139     NS_ENSURE_SUCCESS(rv, rv);
00140 
00141     nsCOMPtr<nsILocalFile> dbPath;
00142     rv = abSession->GetUserProfileDirectory(getter_AddRefs(dbPath));
00143 
00144     nsCOMPtr<nsIAddrDatabase>  listDatabase;
00145     if (NS_SUCCEEDED(rv))
00146     {
00147       nsCAutoString fileName;
00148       nsDependentCString uriStr(uri);
00149       
00150       if (StringBeginsWith(uriStr, NS_LITERAL_CSTRING(kMDBDirectoryRoot)))
00151           fileName = Substring(uriStr, kMDBDirectoryRootLen, uriStr.Length() - kMDBDirectoryRootLen);
00152 
00153       rv = dbPath->AppendNative(fileName);
00154       NS_ENSURE_SUCCESS(rv, rv);
00155 
00156       nsCOMPtr<nsIAddrDatabase> addrDBFactory = do_GetService(NS_ADDRDATABASE_CONTRACTID, &rv);
00157       NS_ENSURE_SUCCESS(rv, rv);
00158 
00159       rv = addrDBFactory->Open(dbPath, PR_TRUE, PR_TRUE, getter_AddRefs(listDatabase));
00160     }
00161     NS_ENSURE_SUCCESS(rv, rv);
00162 
00163     rv = listDatabase->GetMailingListsFromDB(directory);
00164     NS_ENSURE_SUCCESS(rv, rv);
00165 
00166     /*
00167      * This is a hack because of the way
00168      * MDB databases reference MDB directories
00169      * after the mail lists have been created.
00170      *
00171      * To stop assertions when the listDatabase
00172      * goes out of scope it is necessary to remove
00173      * all mail lists which have been added as
00174      * listeners to the database
00175      */
00176     rv = RemoveMailListDBListeners (listDatabase, directory);
00177     NS_ENSURE_SUCCESS(rv, rv);
00178 
00179     nsSingletonEnumerator* cursor =    new nsSingletonEnumerator(directory);
00180     if(!cursor)
00181         return NS_ERROR_NULL_POINTER;
00182     
00183     NS_IF_ADDREF(*_retval = cursor);
00184     return rv;
00185 }
00186 
00187 
00188 
00189 /* void deleteDirectory (in nsIAbDirectory directory); */
00190 NS_IMETHODIMP nsAbMDBDirFactory::DeleteDirectory(nsIAbDirectory *directory)
00191 {
00192     if (!directory)
00193         return NS_ERROR_NULL_POINTER;
00194     
00195     nsresult rv = NS_OK;
00196 
00197     nsCOMPtr<nsISupportsArray> pAddressLists;
00198     rv = directory->GetAddressLists(getter_AddRefs(pAddressLists));
00199     NS_ENSURE_SUCCESS(rv, rv);
00200 
00201     PRUint32 total;
00202     rv = pAddressLists->Count(&total);
00203     NS_ENSURE_SUCCESS(rv, rv);
00204 
00205     for (PRUint32 i = 0; i < total; i++)
00206     {
00207         nsCOMPtr<nsISupports> pSupport;
00208         rv = pAddressLists->GetElementAt(i, getter_AddRefs(pSupport));
00209         if (NS_FAILED(rv))
00210             break;
00211 
00212         nsCOMPtr<nsIAbDirectory> listDir(do_QueryInterface(pSupport, &rv));
00213         if (NS_FAILED(rv))
00214             break;
00215 
00216         nsCOMPtr<nsIAbMDBDirectory> dblistDir(do_QueryInterface(pSupport, &rv));
00217         if (NS_FAILED(rv))
00218             break;
00219 
00220         rv = directory->DeleteDirectory(listDir);
00221         if (NS_FAILED(rv))
00222             break;
00223 
00224         rv = dblistDir->RemoveElementsFromAddressList();
00225         if (NS_FAILED(rv))
00226             break;
00227 
00228         pAddressLists->RemoveElement(pSupport);
00229     }
00230 
00231     nsCOMPtr<nsIAbMDBDirectory> dbdirectory(do_QueryInterface(directory, &rv));
00232     NS_ENSURE_SUCCESS(rv, rv);
00233 
00234     rv = dbdirectory->ClearDatabase ();
00235     return rv;
00236 }
00237