Back to index

lightning-sunbird  0.9+nobinonly
nsWabAddressBook.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 "nsWabAddressBook.h"
00039 #include "nsAutoLock.h"
00040 #include "prlog.h"
00041 
00042 #ifdef PR_LOGGING
00043 static PRLogModuleInfo* gWabAddressBookLog
00044     = PR_NewLogModule("nsWabAddressBookLog");
00045 #endif
00046 
00047 #define PRINTF(args) PR_LOG(gWabAddressBookLog, PR_LOG_DEBUG, args)
00048 
00049 HMODULE nsWabAddressBook::mLibrary = NULL ;
00050 PRInt32 nsWabAddressBook::mLibUsage = 0 ;
00051 LPWABOPEN nsWabAddressBook::mWABOpen = NULL ;
00052 LPWABOBJECT nsWabAddressBook::mRootSession = NULL ;
00053 LPADRBOOK nsWabAddressBook::mRootBook = NULL ;
00054 
00055 BOOL nsWabAddressBook::LoadWabLibrary(void)
00056 {
00057     if (mLibrary) { ++ mLibUsage ; return TRUE ; }
00058     // We try to fetch the location of the WAB DLL from the registry
00059     TCHAR wabDLLPath [MAX_PATH] ;
00060     DWORD keyType = 0 ;
00061     ULONG byteCount = sizeof(wabDLLPath) ;
00062     HKEY keyHandle = NULL ;
00063     
00064     wabDLLPath [MAX_PATH - 1] = 0 ;
00065     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WAB_DLL_PATH_KEY, 0, KEY_READ, &keyHandle) == ERROR_SUCCESS) {
00066         RegQueryValueEx(keyHandle, "", NULL, &keyType, (LPBYTE) wabDLLPath, &byteCount) ;
00067     }
00068     if (keyHandle) { RegCloseKey(keyHandle) ; }
00069     mLibrary = LoadLibrary( (lstrlen(wabDLLPath)) ? wabDLLPath : WAB_DLL_NAME );
00070     if (!mLibrary) { return FALSE ; }
00071     ++ mLibUsage ;
00072     mWABOpen = NS_REINTERPRET_CAST(LPWABOPEN, GetProcAddress(mLibrary, "WABOpen")) ;
00073     if (!mWABOpen) { return FALSE ; }
00074     HRESULT retCode = mWABOpen(&mRootBook, &mRootSession, NULL, 0) ;
00075 
00076     if (HR_FAILED(retCode)) {
00077         PRINTF(("Cannot initialize WAB %08x.\n", retCode)) ; return FALSE ;
00078     }
00079     return TRUE ;
00080 }
00081 
00082 void nsWabAddressBook::FreeWabLibrary(void)
00083 {
00084     if (mLibrary) {
00085         if (-- mLibUsage == 0) {
00086             if (mRootBook) { mRootBook->Release() ; }
00087             if (mRootSession) { mRootSession->Release() ; }
00088             FreeLibrary(mLibrary) ;
00089             mLibrary = NULL ;
00090         }
00091     }
00092 }
00093 
00094 MOZ_DECL_CTOR_COUNTER(nsWabAddressBook)
00095 
00096 nsWabAddressBook::nsWabAddressBook(void)
00097 : nsAbWinHelper()
00098 {
00099     BOOL result = Initialize() ;
00100 
00101     NS_ASSERTION(result == TRUE, "Couldn't initialize Wab Helper") ;
00102     MOZ_COUNT_CTOR(nsWabAddressBook) ;
00103 }
00104 
00105 nsWabAddressBook::~nsWabAddressBook(void)
00106 {
00107     nsAutoLock guard(mMutex) ;
00108     FreeWabLibrary() ;
00109     MOZ_COUNT_DTOR(nsWabAddressBook) ;
00110 }
00111 
00112 BOOL nsWabAddressBook::Initialize(void)
00113 {
00114     if (mAddressBook) { return TRUE ; }
00115     nsAutoLock guard(mMutex) ;
00116 
00117     if (!LoadWabLibrary()) {
00118         PRINTF(("Cannot load library.\n")) ;
00119         return FALSE ;
00120     }
00121     mAddressBook = mRootBook ;
00122     return TRUE ;
00123 }
00124 
00125 void nsWabAddressBook::AllocateBuffer(ULONG aByteCount, LPVOID *aBuffer)
00126 {
00127     mRootSession->AllocateBuffer(aByteCount, aBuffer) ;
00128 }
00129 
00130 void nsWabAddressBook::FreeBuffer(LPVOID aBuffer)
00131 {
00132     mRootSession->FreeBuffer(aBuffer) ;
00133 }
00134 
00135 
00136 
00137 
00138 
00139