Back to index

lightning-sunbird  0.9+nobinonly
MapiApi.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #ifndef MapiApi_h___
00038 #define MapiApi_h___
00039 
00040 #include "nscore.h"
00041 #include "nsString.h"
00042 #include "nsVoidArray.h"
00043 
00044 #include <stdio.h>
00045 
00046 #include <windows.h>
00047 #include <mapi.h>
00048 #include <mapix.h>
00049 #include <mapidefs.h>
00050 #include <mapicode.h>
00051 #include <mapitags.h>
00052 #include <mapiutil.h>
00053 // wabutil.h expects mapiutil to define _MAPIUTIL_H but it actually
00054 // defines _MAPIUTIL_H_
00055 #define _MAPIUTIL_H
00056 
00057 class CMapiFolderList;
00058 class CMsgStore;
00059 class CMapiFolder;
00060 
00061 class CMapiContentIter {
00062 public:
00063        virtual BOOL HandleContentItem( ULONG oType, ULONG cb, LPENTRYID pEntry) = 0;
00064 };
00065 
00066 class CMapiHierarchyIter {
00067 public:
00068        virtual BOOL HandleHierarchyItem( ULONG oType, ULONG cb, LPENTRYID pEntry) = 0;
00069 };
00070 
00071 class CMapiApi {
00072 public:
00073        CMapiApi();
00074        ~CMapiApi();
00075 
00076        static BOOL          LoadMapi( void);
00077        static BOOL          LoadMapiEntryPoints( void);
00078        static void          UnloadMapi( void);
00079 
00080        static HINSTANCE     m_hMapi32;
00081 
00082        static void          MAPIUninitialize( void);
00083        static HRESULT       MAPIInitialize( LPVOID lpInit);
00084        static SCODE  MAPIAllocateBuffer( ULONG cbSize, LPVOID FAR * lppBuffer);
00085        static ULONG  MAPIFreeBuffer( LPVOID lpBuff);
00086        static HRESULT       MAPILogonEx( ULONG ulUIParam, LPTSTR lpszProfileName, LPTSTR lpszPassword, FLAGS flFlags, LPMAPISESSION FAR * lppSession);
00087        static HRESULT       OpenStreamOnFile( LPALLOCATEBUFFER lpAllocateBuffer, LPFREEBUFFER lpFreeBuffer, ULONG ulFlags, LPTSTR lpszFileName, LPTSTR lpszPrefix, LPSTREAM FAR * lppStream);
00088        static void          FreeProws( LPSRowSet prows);
00089 
00090 
00091        BOOL   Initialize( void);
00092        BOOL   LogOn( void);
00093 
00094        void   AddMessageStore( CMsgStore *pStore);
00095        void   SetCurrentMsgStore( LPMDB lpMdb) { m_lpMdb = lpMdb;}
00096 
00097        // Open any given entry from the current Message Store  
00098        BOOL   OpenEntry( ULONG cbEntry, LPENTRYID pEntryId, LPUNKNOWN *ppOpen);
00099        static BOOL OpenMdbEntry( LPMDB lpMdb, ULONG cbEntry, LPENTRYID pEntryId, LPUNKNOWN *ppOpen);
00100 
00101        // Fill in the folders list with the heirarchy from the given
00102        // message store.
00103        BOOL   GetStoreFolders( ULONG cbEid, LPENTRYID lpEid, CMapiFolderList& folders, int startDepth);
00104        BOOL   GetStoreAddressFolders( ULONG cbEid, LPENTRYID lpEid, CMapiFolderList& folders);
00105        BOOL   OpenStore( ULONG cbEid, LPENTRYID lpEid, LPMDB *ppMdb);
00106 
00107        // Iteration
00108        BOOL   IterateStores( CMapiFolderList& list);
00109        BOOL   IterateContents( CMapiContentIter *pIter, LPMAPIFOLDER pFolder, ULONG flags = 0);
00110        BOOL   IterateHierarchy( CMapiHierarchyIter *pIter, LPMAPIFOLDER pFolder, ULONG flags = 0);
00111        
00112        // Properties
00113        static LPSPropValue  GetMapiProperty( LPMAPIPROP pProp, ULONG tag);
00114        static BOOL                 GetEntryIdFromProp( LPSPropValue pVal, ULONG& cbEntryId, LPENTRYID& lpEntryId, BOOL delVal = TRUE);
00115        static BOOL                 GetStringFromProp( LPSPropValue pVal, nsCString& val, BOOL delVal = TRUE);
00116        static BOOL                 GetStringFromProp( LPSPropValue pVal, nsString& val, BOOL delVal = TRUE);
00117        static LONG                 GetLongFromProp( LPSPropValue pVal, BOOL delVal = TRUE);
00118        static BOOL                 GetLargeStringProperty( LPMAPIPROP pProp, ULONG tag, nsCString& val);
00119        static BOOL                 GetLargeStringProperty( LPMAPIPROP pProp, ULONG tag, nsString& val);
00120        static BOOL                 IsLargeProperty( LPSPropValue pVal);
00121        static ULONG    GetEmailPropertyTag(LPMAPIPROP lpProp, LONG nameID);
00122 
00123        // Debugging & reporting stuff
00124        static void                 ListProperties( LPMAPIPROP lpProp, BOOL getValues = TRUE);
00125        static void                 ListPropertyValue( LPSPropValue pVal, nsCString& s);
00126 
00127 protected:
00128        BOOL                 HandleHierarchyItem( ULONG oType, ULONG cb, LPENTRYID pEntry);
00129        BOOL                 HandleContentsItem( ULONG oType, ULONG cb, LPENTRYID pEntry);
00130        void                 GetStoreInfo( CMapiFolder *pFolder, long *pSzContents);
00131 
00132        // array of available message stores, cached so that
00133        // message stores are only opened once, preventing multiple
00134        // logon's by the user if the store requires a logon.
00135        CMsgStore *          FindMessageStore( ULONG cbEid, LPENTRYID lpEid);
00136        void                 ClearMessageStores( void);
00137 
00138        static void                 CStrToUnicode( const char *pStr, nsString& result);
00139 
00140        // Debugging & reporting stuff
00141        static void                 GetPropTagName( ULONG tag, nsCString& s);
00142        static void                 ReportStringProp( const char *pTag, LPSPropValue pVal);
00143        static void                 ReportUIDProp( const char *pTag, LPSPropValue pVal);
00144        static void                 ReportLongProp( const char *pTag, LPSPropValue pVal);
00145 
00146 
00147 private:
00148        static int                         m_clients;
00149        static BOOL                        m_initialized;
00150        static nsVoidArray * m_pStores;
00151        static LPMAPISESSION m_lpSession;
00152        static LPMDB                m_lpMdb;
00153        static HRESULT                     m_lastError;
00154        static PRUnichar *          m_pUniBuff;
00155        static int                         m_uniBuffLen;
00156 };
00157 
00158 
00159 
00160 class CMapiFolder {
00161 public:
00162        CMapiFolder();
00163        CMapiFolder( const CMapiFolder *pCopyFrom);
00164        CMapiFolder( const PRUnichar *pDisplayName, ULONG cbEid, LPENTRYID lpEid, int depth, LONG oType = MAPI_FOLDER);
00165        ~CMapiFolder();
00166 
00167        void   SetDoImport( BOOL doIt) { m_doImport = doIt;}
00168        void   SetObjectType( long oType) { m_objectType = oType;}
00169        void   SetDisplayName( const PRUnichar *pDisplayName) { m_displayName = pDisplayName;}
00170        void   SetEntryID( ULONG cbEid, LPENTRYID lpEid);
00171        void   SetDepth( int depth) { m_depth = depth;}
00172        void   SetFilePath( const PRUnichar *pFilePath) { m_mailFilePath = pFilePath;}
00173        
00174        BOOL   GetDoImport( void) const { return( m_doImport);}
00175        LONG   GetObjectType( void) const { return( m_objectType);}
00176        void   GetDisplayName( nsString& name) const { name = m_displayName;}
00177        void   GetFilePath( nsString& path) const { path = m_mailFilePath;}
00178        BOOL   IsStore( void) const { return( m_objectType == MAPI_STORE);}
00179        BOOL   IsFolder( void) const { return( m_objectType == MAPI_FOLDER);}
00180        int           GetDepth( void) const { return( m_depth);}
00181 
00182        LPENTRYID     GetEntryID( ULONG *pCb = NULL) const { if (pCb) *pCb = m_cbEid; return( (LPENTRYID) m_lpEid);}
00183        ULONG         GetCBEntryID( void) const { return( m_cbEid);}
00184 
00185 private:
00186        LONG          m_objectType;
00187        ULONG         m_cbEid;
00188        BYTE *        m_lpEid;
00189        nsString      m_displayName;
00190        int                  m_depth;
00191        nsString      m_mailFilePath;
00192        BOOL          m_doImport;
00193 
00194 };
00195 
00196 class CMapiFolderList {
00197 public:
00198        CMapiFolderList();
00199        ~CMapiFolderList();
00200 
00201        void                 AddItem( CMapiFolder *pFolder);
00202        CMapiFolder * GetItem( int index) { if ((index >= 0) && (index < m_array.Count())) return( (CMapiFolder *)GetAt( index)); else return( NULL);}
00203        void                 ClearAll( void);
00204 
00205        // Debugging and reporting
00206        void                 DumpList( void);
00207 
00208        void *               GetAt( int index) { return( m_array.ElementAt( index));}
00209        int                         GetSize( void) { return( m_array.Count());}
00210 
00211 protected:
00212        void   EnsureUniqueName( CMapiFolder *pFolder);
00213        void   GenerateFilePath( CMapiFolder *pFolder);
00214        void   ChangeName( nsString& name);
00215 
00216 private:
00217        nsVoidArray          m_array;
00218 };
00219 
00220 
00221 class CMsgStore {
00222 public:
00223        CMsgStore( ULONG cbEid = 0, LPENTRYID lpEid = NULL);
00224        ~CMsgStore();
00225 
00226        void          SetEntryID( ULONG cbEid, LPENTRYID lpEid);
00227        BOOL          Open( LPMAPISESSION pSession, LPMDB *ppMdb);
00228        
00229        ULONG         GetCBEntryID( void) { return( m_cbEid);}
00230        LPENTRYID     GetLPEntryID( void) { return( (LPENTRYID) m_lpEid);}
00231 
00232 private:
00233        ULONG         m_cbEid;
00234        BYTE *        m_lpEid;
00235        LPMDB         m_lpMdb;
00236 };
00237 
00238 
00239 class CMapiFolderContents {
00240 public:
00241        CMapiFolderContents( LPMDB lpMdb, ULONG cbEID, LPENTRYID lpEid);
00242        ~CMapiFolderContents();
00243 
00244        BOOL   GetNext( ULONG *pcbEid, LPENTRYID *ppEid, ULONG *poType, BOOL *pDone);
00245 
00246        ULONG  GetCount( void) { return( m_count);}
00247 
00248 protected:
00249        BOOL SetUpIter( void);
00250 
00251 private:
00252        HRESULT                     m_lastError;
00253        BOOL                 m_failure;
00254        LPMDB                m_lpMdb;
00255        LPMAPIFOLDER  m_lpFolder;
00256        LPMAPITABLE          m_lpTable;
00257        ULONG                m_fCbEid;
00258        BYTE *               m_fLpEid;
00259        ULONG                m_count;
00260        ULONG                m_iterCount;
00261        BYTE *               m_lastLpEid;
00262        ULONG                m_lastCbEid;
00263 };
00264 
00265 
00266 #endif /* MapiApi_h__ */