Back to index

lightning-sunbird  0.9+nobinonly
nsAbOutlookDirFactory.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  *   Created by Cyrille Moureaux <Cyrille.Moureaux@sun.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 #include "nsAbOutlookDirFactory.h"
00039 #include "nsAbWinHelper.h"
00040 #include "nsIAbDirectory.h"
00041 
00042 #include "nsIRDFService.h"
00043 #include "nsIRDFResource.h"
00044 #include "nsRDFResource.h"
00045 #include "nsEnumeratorUtils.h"
00046 
00047 #include "nsAbBaseCID.h"
00048 
00049 #include "prlog.h"
00050 
00051 #ifdef PR_LOGGING
00052 static PRLogModuleInfo* gAbOutlookDirFactoryLog
00053     = PR_NewLogModule("nsAbOutlookDirFactoryLog");
00054 #endif
00055 
00056 #define PRINTF(args) PR_LOG(nsAbOutlookDirFactoryLog, PR_LOG_DEBUG, args)
00057 
00058 
00059 // In case someone is wondering WHY I have to undefine CreateDirectory,
00060 // it's because the windows files winbase.h and wininet.h define this
00061 // to CreateDirectoryA/W (for reasons best left unknown) and with the
00062 // MAPI stuff, I end up including this, which wreaks havoc on my symbol
00063 // table.
00064 #ifdef CreateDirectory 
00065 #  undef CreateDirectory
00066 #endif // CreateDirectory
00067 
00068 NS_IMPL_ISUPPORTS1(nsAbOutlookDirFactory, nsIAbDirFactory)
00069 
00070 nsAbOutlookDirFactory::nsAbOutlookDirFactory(void)
00071 {
00072 }
00073 
00074 nsAbOutlookDirFactory::~nsAbOutlookDirFactory(void)
00075 {
00076 }
00077 
00078 extern const char *kOutlookDirectoryScheme ;
00079 
00080 static nsresult parseProperties(nsIAbDirectoryProperties *aProperties, nsAbWinType& aWinType)
00081 {
00082     aWinType = nsAbWinType_Unknown ;
00083 
00084     nsXPIDLCString uri;
00085     nsresult rv = aProperties->GetURI(getter_Copies(uri));
00086     NS_ENSURE_SUCCESS(rv,rv);
00087     
00088             nsCString stub ;
00089             nsCString entry ;
00090 
00091             aWinType = getAbWinType(kOutlookDirectoryScheme, uri.get(), stub, entry) ;
00092     return NS_OK;
00093 }
00094 
00095 NS_IMETHODIMP nsAbOutlookDirFactory::CreateDirectory(nsIAbDirectoryProperties *aProperties, 
00096                                                      nsISimpleEnumerator **aDirectories)
00097 {
00098     NS_ENSURE_ARG_POINTER(aProperties);
00099     NS_ENSURE_ARG_POINTER(aDirectories);
00100 
00101     *aDirectories = nsnull ;
00102     nsresult retCode = NS_OK ;
00103     nsAbWinType abType = nsAbWinType_Unknown ;
00104 
00105     retCode = parseProperties(aProperties, abType) ;
00106     NS_ENSURE_SUCCESS(retCode, retCode);
00107 
00108     if (abType == nsAbWinType_Unknown) {
00109         return NS_ERROR_FAILURE ;
00110     }
00111     nsAbWinHelperGuard mapiAddBook (abType) ;
00112     nsMapiEntryArray folders ;
00113     ULONG nbFolders = 0 ;
00114     nsCOMPtr<nsISupportsArray> directories ;
00115     
00116     retCode = NS_NewISupportsArray(getter_AddRefs(directories)) ;
00117     NS_ENSURE_SUCCESS(retCode, retCode) ;
00118     if (!mapiAddBook->IsOK() || !mapiAddBook->GetFolders(folders)) {
00119         return NS_ERROR_FAILURE ;
00120     }
00121     nsCOMPtr<nsIRDFService> rdf = do_GetService (NS_RDF_CONTRACTID "/rdf-service;1", &retCode);
00122     NS_ENSURE_SUCCESS(retCode, retCode) ;
00123     nsCAutoString entryId ;
00124     nsCAutoString uri ;
00125     nsCOMPtr<nsIRDFResource> resource ;
00126 
00127     for (ULONG i = 0 ; i < folders.mNbEntries ; ++ i) {
00128         folders.mEntries [i].ToString(entryId) ;
00129         buildAbWinUri(kOutlookDirectoryScheme, abType, uri) ;
00130         uri.Append(entryId) ;
00131         
00132         retCode = rdf->GetResource(uri, getter_AddRefs(resource)) ;
00133         NS_ENSURE_SUCCESS(retCode, retCode) ;
00134         directories->AppendElement(resource) ;
00135     }
00136     return NS_NewArrayEnumerator(aDirectories, directories) ;
00137 }
00138 
00139 // No actual deletion, since you cannot create the address books from Mozilla.
00140 NS_IMETHODIMP nsAbOutlookDirFactory::DeleteDirectory(nsIAbDirectory *aDirectory)
00141 {
00142     return NS_OK ;
00143 }
00144