Back to index

lightning-sunbird  0.9+nobinonly
nsAbLDAPDirFactory.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  *   Csaba Borbola <csaba.borbola@sun.com>
00024  *   Seth Spitzer <sspitzer@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsAbLDAPDirFactory.h"
00041 #include "nsAbUtils.h"
00042 
00043 #include "nsIServiceManager.h"
00044 #include "nsIRDFService.h"
00045 #include "nsIRDFResource.h"
00046 
00047 #include "nsIAbDirectory.h"
00048 #include "nsAbLDAPDirectory.h"
00049 #include "nsAbDirFactoryService.h"
00050 #include "nsIAddressBook.h"
00051 
00052 #include "nsEnumeratorUtils.h"
00053 #include "nsAbBaseCID.h"
00054 
00055 NS_IMPL_ISUPPORTS1(nsAbLDAPDirFactory, nsIAbDirFactory)
00056 
00057 nsAbLDAPDirFactory::nsAbLDAPDirFactory()
00058 {
00059 }
00060 
00061 nsAbLDAPDirFactory::~nsAbLDAPDirFactory()
00062 {
00063 }
00064 
00065 NS_IMETHODIMP nsAbLDAPDirFactory::CreateDirectory(nsIAbDirectoryProperties *aProperties,
00066     nsISimpleEnumerator **aDirectories)
00067 {
00068     NS_ENSURE_ARG_POINTER(aProperties);
00069     NS_ENSURE_ARG_POINTER(aDirectories);
00070 
00071     nsresult rv;
00072 
00073     nsXPIDLCString uri;
00074     nsAutoString description;
00075     nsXPIDLCString prefName;
00076     
00077     rv = aProperties->GetDescription(description);
00078     NS_ENSURE_SUCCESS(rv, rv);
00079 
00080     rv = aProperties->GetURI(getter_Copies(uri));
00081     NS_ENSURE_SUCCESS(rv, rv);
00082     
00083     rv = aProperties->GetPrefName(getter_Copies(prefName));
00084     NS_ENSURE_SUCCESS(rv, rv);
00085     
00086     nsCOMPtr<nsIRDFService> rdf = do_GetService (NS_RDF_CONTRACTID "/rdf-service;1", &rv);
00087     NS_ENSURE_SUCCESS(rv, rv);
00088 
00089     nsCOMPtr<nsIRDFResource> resource;
00090 
00091     if ((strncmp(uri.get(), "ldap:", 5) == 0) ||
00092         (strncmp(uri.get(), "ldaps:", 6) == 0)) {
00093       nsXPIDLCString prefName;
00094       rv = aProperties->GetPrefName(getter_Copies(prefName));
00095       NS_ENSURE_SUCCESS(rv, rv);
00096 
00097       /*
00098        * if the URI starts with ldap: or ldaps:
00099        * then this directory is an LDAP directory.
00100        *
00101        * we don't want to use the ldap:// or ldaps:// URI 
00102        * as the RDF resource URI because the ldap:// or ldaps:// URI 
00103        * will contain the hostname, basedn, port, etc.
00104        * so if those attributes changed, we'll run into the
00105        * the same problem that we hit with changing username / hostname
00106        * for mail servers.  to solve this problem, we add an extra
00107        * level of indirection.  the RDF resource URI that we generate
00108        * (the bridge URI) will be moz-abldapdirectory://<prefName>
00109        * and when we need the hostname, basedn, port, etc,
00110        * we'll use the <prefName> to get the necessary prefs.
00111        * note, <prefName> does not change.
00112        */
00113       nsCAutoString bridgeURI;
00114       bridgeURI = NS_LITERAL_CSTRING(kLDAPDirectoryRoot) + prefName;
00115       rv = rdf->GetResource(bridgeURI, getter_AddRefs(resource));
00116     }
00117     else {
00118       rv = rdf->GetResource(uri, getter_AddRefs(resource));
00119     }
00120     NS_ENSURE_SUCCESS(rv, rv);
00121 
00122     nsCOMPtr<nsIAbDirectory> directory(do_QueryInterface(resource, &rv));
00123     NS_ENSURE_SUCCESS(rv, rv);
00124 
00125     rv = directory->SetDirName(description.get());
00126     NS_ENSURE_SUCCESS(rv,rv);
00127 
00128     rv = directory->SetDirPrefId(prefName);
00129     NS_ENSURE_SUCCESS(rv, rv);
00130 
00131     NS_IF_ADDREF(*aDirectories = new nsSingletonEnumerator(directory));
00132     return *aDirectories ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00133 }
00134 
00135 /* void deleteDirectory (in nsIAbDirectory directory); */
00136 NS_IMETHODIMP nsAbLDAPDirFactory::DeleteDirectory(nsIAbDirectory *directory)
00137 {
00138     // No actual deletion - as the LDAP Address Book is not physically
00139     // created in the corresponding CreateDirectory() unlike the Personal
00140     // Address Books. But we still need to return NS_OK from here.
00141     return NS_OK;
00142 }
00143