Back to index

lightning-sunbird  0.9+nobinonly
msgMapiMain.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corp.
00018  * Portions created by the Initial Developer are Copyright (C) 2001
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s): Krishna Mohan Khandrika (kkhandrika@netscape.com)
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include <mapidefs.h>
00038 #include <mapi.h>
00039 
00040 #include "msgCore.h"
00041 #include "nsMsgComposeStringBundle.h"
00042 #include "msgMapiMain.h"
00043 #include "nsIServiceManager.h"
00044 #include "nsCOMPtr.h"
00045 
00046 // move to xpcom bug 81956.
00047 class nsPRUintKey : public nsHashKey {
00048 protected:
00049     PRUint32 mKey;
00050 public:
00051     nsPRUintKey(PRUint32 key) : mKey(key) {}
00052 
00053     PRUint32 HashCode(void) const {
00054         return mKey;
00055     }
00056 
00057     PRBool Equals(const nsHashKey *aKey) const {
00058         return mKey == ((const nsPRUintKey *) aKey)->mKey;
00059     }
00060     nsHashKey *Clone() const {
00061         return new nsPRUintKey(mKey);
00062     }
00063     PRUint32 GetValue() { return mKey; }
00064 };
00065 //
00066 
00067 
00068 nsMAPIConfiguration *nsMAPIConfiguration::m_pSelfRef = nsnull;
00069 PRUint32 nsMAPIConfiguration::session_generator = 0;
00070 PRUint32 nsMAPIConfiguration::sessionCount = 0;
00071 
00072 nsMAPIConfiguration *nsMAPIConfiguration::GetMAPIConfiguration()
00073 {
00074     if (m_pSelfRef == nsnull)
00075         m_pSelfRef = new nsMAPIConfiguration();
00076 
00077     return m_pSelfRef;
00078 }
00079 
00080 nsMAPIConfiguration::nsMAPIConfiguration()
00081 : m_nMaxSessions(MAX_SESSIONS)
00082 {
00083     m_Lock = PR_NewLock();
00084 }
00085 
00086 static PRBool
00087 FreeSessionMapEntries(nsHashKey *aKey, void *aData, void* aClosure)
00088 {
00089     delete (nsMAPISession*) aData;
00090     return PR_TRUE;
00091 }
00092 
00093 static PRBool
00094 FreeProfileMapEntries(nsHashKey *aKey, void *aData, void* aClosure)
00095 {
00096     return PR_TRUE;
00097 }
00098 
00099 nsMAPIConfiguration::~nsMAPIConfiguration()
00100 {
00101     if (m_Lock)
00102         PR_DestroyLock(m_Lock);
00103 
00104     m_SessionMap.Reset(FreeSessionMapEntries);
00105     m_ProfileMap.Reset(FreeProfileMapEntries);
00106 }
00107 
00108 void nsMAPIConfiguration::OpenConfiguration()
00109 {
00110     // No. of max. sessions is set to MAX_SESSIONS.  In future
00111     // if it is decided to have configuration (registry)
00112     // parameter, this function can be used to set the
00113     // max sessions;
00114 
00115     return;
00116 }
00117 
00118 PRInt16 nsMAPIConfiguration::RegisterSession(PRUint32 aHwnd,
00119                 const PRUnichar *aUserName, const PRUnichar *aPassword,
00120                 PRBool aForceDownLoad, PRBool aNewSession,
00121                 PRUint32 *aSession, char *aIdKey)
00122 {
00123     PRInt16 nResult = 0;
00124     PRUint32 n_SessionId = 0;
00125 
00126     PR_Lock(m_Lock);
00127 
00128     // Check whether max sessions is exceeded
00129 
00130     if (sessionCount >= m_nMaxSessions)
00131     {
00132         PR_Unlock(m_Lock);
00133         return -1;
00134     }
00135 
00136     if (aUserName != nsnull && aUserName[0] != '\0')
00137     {
00138       nsStringKey usernameKey(aUserName);
00139       n_SessionId = (PRUint32) m_ProfileMap.Get(&usernameKey);
00140     }
00141 
00142     // try to share a session; if not create a session
00143 
00144     if (n_SessionId > 0)
00145     {
00146         nsPRUintKey sessionKey(n_SessionId);
00147         nsMAPISession *pTemp = (nsMAPISession *)m_SessionMap.Get(&sessionKey);
00148         if (pTemp != nsnull)
00149         {
00150             pTemp->IncrementSession();
00151             *aSession = n_SessionId;
00152             nResult = 1;
00153         }
00154     }
00155     else if (aNewSession || n_SessionId == 0) // checking for n_SessionId is a concession
00156     {
00157         // create a new session ; if new session is specified OR there is no session
00158         nsMAPISession *pTemp = nsnull;
00159         pTemp = new nsMAPISession(aHwnd, aUserName,
00160                                aPassword, aForceDownLoad, aIdKey);
00161 
00162         if (pTemp != nsnull)
00163         {
00164             session_generator++;
00165 
00166             // I don't think there will be (2 power 32) sessions alive
00167             // in a cycle.  This is an assumption
00168 
00169             if (session_generator == 0)
00170                 session_generator++;
00171 
00172             nsPRUintKey sessionKey(session_generator);
00173             m_SessionMap.Put(&sessionKey, pTemp);
00174             if (aUserName != nsnull && aUserName[0] != '\0')
00175             {
00176                 nsStringKey usernameKey(aUserName);
00177                 m_ProfileMap.Put(&usernameKey, (void*)session_generator);
00178             }
00179 
00180             *aSession = session_generator;
00181             sessionCount++;
00182             nResult = 1;
00183         }
00184     }
00185 
00186     PR_Unlock(m_Lock);
00187     return nResult;
00188 }
00189 
00190 PRBool nsMAPIConfiguration::UnRegisterSession(PRUint32 aSessionID)
00191 {
00192     PRBool bResult = PR_FALSE;
00193 
00194     PR_Lock(m_Lock);
00195 
00196     if (aSessionID != 0)
00197     {
00198         nsPRUintKey sessionKey(aSessionID);
00199         nsMAPISession *pTemp = (nsMAPISession *)m_SessionMap.Get(&sessionKey);
00200 
00201         if (pTemp != nsnull)
00202         {
00203             if (pTemp->DecrementSession() == 0)
00204             {
00205                 if (pTemp->m_pProfileName.get() != nsnull)
00206                 {
00207                   nsStringKey stringKey(pTemp->m_pProfileName.get());
00208                   m_ProfileMap.Remove(&stringKey);
00209                 }
00210                 m_SessionMap.Remove(&sessionKey);
00211                 sessionCount--;
00212                 bResult = PR_TRUE;
00213             }
00214         }
00215     }
00216 
00217     PR_Unlock(m_Lock);
00218     return bResult;
00219 }
00220 
00221 PRBool nsMAPIConfiguration::IsSessionValid(PRUint32 aSessionID)
00222 {
00223     if (aSessionID == 0)
00224         return PR_FALSE;
00225 
00226     PRBool retValue = PR_FALSE;
00227     nsPRUintKey sessionKey(aSessionID);
00228 
00229     PR_Lock(m_Lock);
00230 
00231     retValue = m_SessionMap.Exists(&sessionKey);
00232         
00233     PR_Unlock(m_Lock);
00234 
00235     return retValue;
00236 }
00237 
00238 
00239 PRUnichar *nsMAPIConfiguration::GetPassword(PRUint32 aSessionID)
00240 {
00241     PRUnichar *pResult = nsnull;
00242 
00243     PR_Lock(m_Lock);
00244 
00245     if (aSessionID != 0)
00246     {
00247         nsPRUintKey sessionKey(aSessionID);
00248         nsMAPISession *pTemp = (nsMAPISession *)m_SessionMap.Get(&sessionKey);
00249 
00250         if (pTemp)
00251             pResult = pTemp->GetPassword();
00252     }
00253 
00254     PR_Unlock(m_Lock);
00255 
00256     return pResult;
00257 }
00258 
00259 void *nsMAPIConfiguration::GetMapiListContext(PRUint32 aSessionID)
00260 {
00261     void *pResult = nsnull;
00262 
00263     PR_Lock(m_Lock);
00264 
00265     if (aSessionID != 0)
00266     {
00267         nsPRUintKey sessionKey(aSessionID);
00268         nsMAPISession *pTemp = (nsMAPISession *)m_SessionMap.Get(&sessionKey);
00269         if (pTemp)
00270            pResult = pTemp->GetMapiListContext();
00271     }
00272 
00273     PR_Unlock(m_Lock);
00274     return pResult;
00275 }
00276 
00277 void nsMAPIConfiguration::SetMapiListContext(PRUint32 aSessionID, void *mapiListContext)
00278 {
00279     PR_Lock(m_Lock);
00280 
00281     if (aSessionID != 0)
00282     {
00283         nsPRUintKey sessionKey(aSessionID);
00284         nsMAPISession *pTemp = (nsMAPISession *)m_SessionMap.Get(&sessionKey);
00285         if (pTemp)
00286            pTemp->SetMapiListContext(mapiListContext);
00287     }
00288 
00289     PR_Unlock(m_Lock);
00290 }
00291 
00292 char *nsMAPIConfiguration::GetIdKey(PRUint32 aSessionID)
00293 {
00294     char *pResult = nsnull;
00295 
00296     PR_Lock(m_Lock);
00297 
00298     if (aSessionID != 0)
00299     {
00300         nsPRUintKey sessionKey(aSessionID);
00301         nsMAPISession *pTemp = (nsMAPISession *)m_SessionMap.Get(&sessionKey);
00302         if (pTemp)
00303            pResult = pTemp->GetIdKey();
00304     }
00305 
00306     PR_Unlock(m_Lock);
00307     return pResult;
00308 }
00309 
00310 // util func
00311 HRESULT nsMAPIConfiguration::GetMAPIErrorFromNSError (nsresult res)
00312 {
00313     HRESULT hr = SUCCESS_SUCCESS ;
00314 
00315     if (NS_SUCCEEDED (res)) return hr ;
00316 
00317     // if failure return the related MAPI failure code
00318     switch (res)
00319     {
00320         case NS_MSG_NO_RECIPIENTS :
00321             hr = MAPI_E_BAD_RECIPTYPE ;
00322             break ;
00323         case NS_ERROR_COULD_NOT_GET_USERS_MAIL_ADDRESS :
00324             hr = MAPI_E_INVALID_RECIPS ; 
00325             break ;
00326         case NS_ERROR_COULD_NOT_LOGIN_TO_SMTP_SERVER :
00327             hr = MAPI_E_LOGIN_FAILURE ;
00328             break ;
00329         case NS_MSG_UNABLE_TO_OPEN_FILE :                 
00330         case NS_MSG_UNABLE_TO_OPEN_TMP_FILE :
00331         case NS_MSG_COULDNT_OPEN_FCC_FOLDER :
00332         case NS_ERROR_FILE_INVALID_PATH :
00333             hr = MAPI_E_ATTACHMENT_OPEN_FAILURE ;
00334             break ;
00335         case NS_ERROR_FILE_TARGET_DOES_NOT_EXIST :
00336             hr = MAPI_E_ATTACHMENT_NOT_FOUND ;
00337             break ;
00338         case NS_MSG_CANCELLING :
00339             hr = MAPI_E_USER_ABORT ;
00340             break ;
00341         case NS_MSG_ERROR_WRITING_FILE :
00342         case NS_MSG_UNABLE_TO_SAVE_TEMPLATE :
00343         case NS_MSG_UNABLE_TO_SAVE_DRAFT :
00344             hr = MAPI_E_ATTACHMENT_WRITE_FAILURE ;
00345             break ;
00346         default :
00347             hr = MAPI_E_FAILURE ;
00348             break ;
00349     }
00350 
00351     return hr ;
00352 }
00353 
00354 
00355 nsMAPISession::nsMAPISession(PRUint32 aHwnd, const PRUnichar *aUserName,\
00356                              const PRUnichar *aPassword, \
00357                              PRBool aForceDownLoad, char *aKey)
00358 : m_bIsForcedDownLoad(aForceDownLoad),
00359   m_hAppHandle(aHwnd),
00360   m_nShared(1),
00361   m_pIdKey(aKey)
00362 {
00363     m_listContext = NULL;
00364     m_pProfileName.Assign(aUserName);
00365     m_pPassword.Assign(aPassword);
00366 }
00367 
00368 nsMAPISession::~nsMAPISession()
00369 {
00370     if (m_pIdKey != nsnull)
00371     {
00372         delete [] m_pIdKey;
00373         m_pIdKey = nsnull;
00374     }
00375 }
00376 
00377 PRUint32 nsMAPISession::IncrementSession()
00378 {
00379     return ++m_nShared;
00380 }
00381 
00382 PRUint32 nsMAPISession::DecrementSession()
00383 {
00384     return --m_nShared;
00385 }
00386 
00387 PRUint32 nsMAPISession::GetSessionCount()
00388 {
00389     return m_nShared;
00390 }
00391 
00392 PRUnichar *nsMAPISession::GetPassword()
00393 {
00394     return (PRUnichar *)m_pPassword.get();
00395 }
00396 
00397 char *nsMAPISession::GetIdKey()
00398 {
00399     return m_pIdKey;
00400 }
00401