Back to index

lightning-sunbird  0.9+nobinonly
nsMapiAddressBook.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 "nsMapiAddressBook.h"
00039 #include "nsAutoLock.h"
00040 
00041 #include "prlog.h"
00042 
00043 #ifdef PR_LOGGING
00044 static PRLogModuleInfo* gMapiAddressBookLog
00045     = PR_NewLogModule("nsMapiAddressBookLog");
00046 #endif
00047 
00048 #define PRINTF(args) PR_LOG(gMapiAddressBookLog, PR_LOG_DEBUG, args)
00049 
00050 
00051 HMODULE nsMapiAddressBook::mLibrary = NULL ;
00052 PRInt32 nsMapiAddressBook::mLibUsage = 0 ;
00053 LPMAPIINITIALIZE nsMapiAddressBook::mMAPIInitialize = NULL ;
00054 LPMAPIUNINITIALIZE nsMapiAddressBook::mMAPIUninitialize = NULL ;
00055 LPMAPIALLOCATEBUFFER nsMapiAddressBook::mMAPIAllocateBuffer = NULL ;
00056 LPMAPIFREEBUFFER nsMapiAddressBook::mMAPIFreeBuffer = NULL ;
00057 LPMAPILOGONEX nsMapiAddressBook::mMAPILogonEx = NULL ;
00058 
00059 BOOL nsMapiAddressBook::mInitialized = FALSE ;
00060 BOOL nsMapiAddressBook::mLogonDone = FALSE ;
00061 LPMAPISESSION nsMapiAddressBook::mRootSession = NULL ;
00062 LPADRBOOK nsMapiAddressBook::mRootBook = NULL ;
00063 
00064 BOOL nsMapiAddressBook::LoadMapiLibrary(void)
00065 {
00066     if (mLibrary) { ++ mLibUsage ; return TRUE ; }
00067     HMODULE libraryHandle = LoadLibrary("MAPI32.DLL") ;
00068 
00069     if (!libraryHandle) { return FALSE ; }
00070     FARPROC entryPoint = GetProcAddress(libraryHandle, "MAPIGetNetscapeVersion") ;
00071 
00072     if (entryPoint) {
00073         FreeLibrary(libraryHandle) ;
00074         libraryHandle = LoadLibrary("MAPI32BAK.DLL") ;
00075         if (!libraryHandle) { return FALSE ; }
00076     }
00077     mLibrary = libraryHandle ;
00078     ++ mLibUsage ;
00079     mMAPIInitialize = NS_REINTERPRET_CAST(LPMAPIINITIALIZE, 
00080         GetProcAddress(mLibrary, "MAPIInitialize")) ;
00081     if (!mMAPIInitialize) { return FALSE ; }
00082     mMAPIUninitialize = NS_REINTERPRET_CAST(LPMAPIUNINITIALIZE, 
00083         GetProcAddress(mLibrary, "MAPIUninitialize")) ;
00084     if (!mMAPIUninitialize) { return FALSE ; }
00085     mMAPIAllocateBuffer = NS_REINTERPRET_CAST(LPMAPIALLOCATEBUFFER, 
00086         GetProcAddress(mLibrary, "MAPIAllocateBuffer")) ;
00087     if (!mMAPIAllocateBuffer) { return FALSE ; }
00088     mMAPIFreeBuffer = NS_REINTERPRET_CAST(LPMAPIFREEBUFFER, 
00089         GetProcAddress(mLibrary, "MAPIFreeBuffer")) ;
00090     if (!mMAPIFreeBuffer) { return FALSE ; }
00091     mMAPILogonEx = NS_REINTERPRET_CAST(LPMAPILOGONEX, 
00092         GetProcAddress(mLibrary, "MAPILogonEx")) ;
00093     if (!mMAPILogonEx) { return FALSE ; }
00094     MAPIINIT_0 mapiInit = { MAPI_INIT_VERSION, MAPI_MULTITHREAD_NOTIFICATIONS } ;
00095     HRESULT retCode = mMAPIInitialize(&mapiInit) ;
00096 
00097     if (HR_FAILED(retCode)) { 
00098         PRINTF(("Cannot initialize MAPI %08x.\n", retCode)) ; return FALSE ;
00099     }
00100     mInitialized = TRUE ;
00101     retCode = mMAPILogonEx(0, NULL, NULL,
00102                            MAPI_NO_MAIL | 
00103                            MAPI_USE_DEFAULT | 
00104                            MAPI_EXTENDED | 
00105                            MAPI_NEW_SESSION,
00106                            &mRootSession) ;
00107     if (HR_FAILED(retCode)) { 
00108         PRINTF(("Cannot logon to MAPI %08x.\n", retCode)) ; return FALSE ;
00109     }
00110     mLogonDone = TRUE ;
00111     retCode = mRootSession->OpenAddressBook(0, NULL, 0, &mRootBook) ;
00112     if (HR_FAILED(retCode)) { 
00113         PRINTF(("Cannot open MAPI address book %08x.\n", retCode)) ;
00114     }
00115     return HR_SUCCEEDED(retCode) ;
00116 }
00117 
00118 void nsMapiAddressBook::FreeMapiLibrary(void)
00119 {
00120     if (mLibrary) {
00121         if (-- mLibUsage == 0) {
00122             {
00123                 if (mRootBook) { mRootBook->Release() ; }
00124                 if (mRootSession) {
00125                     if (mLogonDone) { 
00126                         mRootSession->Logoff(NULL, 0, 0) ; 
00127                         mLogonDone = FALSE ;
00128                     }
00129                     mRootSession->Release() ;
00130                 }
00131                 if (mInitialized) { 
00132                     mMAPIUninitialize() ; 
00133                     mInitialized = FALSE ;
00134                 }
00135             }  
00136             FreeLibrary(mLibrary) ;
00137             mLibrary = NULL ; 
00138         }
00139     }
00140 }
00141 
00142 MOZ_DECL_CTOR_COUNTER(nsMapiAddressBook)
00143 
00144 nsMapiAddressBook::nsMapiAddressBook(void)
00145 : nsAbWinHelper()
00146 {
00147     BOOL result = Initialize() ;
00148 
00149     NS_ASSERTION(result == TRUE, "Couldn't initialize Mapi Helper") ;
00150     MOZ_COUNT_CTOR(nsMapiAddressBook) ;
00151 }
00152 
00153 nsMapiAddressBook::~nsMapiAddressBook(void)
00154 {
00155     nsAutoLock guard(mMutex) ;
00156 
00157     FreeMapiLibrary() ;
00158     MOZ_COUNT_DTOR(nsMapiAddressBook) ;
00159 }
00160 
00161 BOOL nsMapiAddressBook::Initialize(void)
00162 {
00163     if (mAddressBook) { return TRUE ; }
00164     nsAutoLock guard(mMutex) ;
00165 
00166     if (!LoadMapiLibrary()) {
00167         PRINTF(("Cannot load library.\n")) ;
00168         return FALSE ;
00169     }
00170     mAddressBook = mRootBook ; 
00171     return TRUE ;
00172 }
00173 
00174 void nsMapiAddressBook::AllocateBuffer(ULONG aByteCount, LPVOID *aBuffer)
00175 {
00176     mMAPIAllocateBuffer(aByteCount, aBuffer) ;
00177 }
00178 
00179 void nsMapiAddressBook::FreeBuffer(LPVOID aBuffer)
00180 {
00181     mMAPIFreeBuffer(aBuffer) ;
00182 }
00183 
00184 
00185 
00186 
00187