Back to index

lightning-sunbird  0.9+nobinonly
msgMapiSupport.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  *                 Rajiv Dayal <rdayal@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * 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 #include "nsCOMPtr.h"
00038 #include "objbase.h"
00039 #include "nsISupports.h"
00040 
00041 #include "nsIGenericFactory.h"
00042 #include "nsIObserverService.h"
00043 #include "nsIAppStartupNotifier.h"
00044 #include "nsIServiceManager.h"
00045 #include "nsIComponentManager.h"
00046 #include "nsICategoryManager.h"
00047 #include "nsCRT.h"
00048 #include "Registry.h"
00049 #include "msgMapiSupport.h"
00050 
00051 #ifndef MOZ_THUNDERBIRD
00052 #include "nsMapiRegistryUtils.h" 
00053 #include "nsMapiRegistry.h"
00054 #endif
00055 
00056 #include "msgMapiImp.h"
00057 
00062 NS_IMPL_THREADSAFE_ISUPPORTS2(nsMapiSupport, nsIMapiSupport, nsIObserver)
00063 
00064 static NS_METHOD nsMapiRegistrationProc(nsIComponentManager *aCompMgr,
00065                    nsIFile *aPath, const char *registryLocation, const char *componentType,
00066                    const nsModuleComponentInfo *info)
00067 {
00068     
00069   nsresult rv;
00070 
00071   nsCOMPtr<nsICategoryManager> categoryManager(do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv));
00072   if (NS_SUCCEEDED(rv)) 
00073       rv = categoryManager->AddCategoryEntry(APPSTARTUP_CATEGORY, "Mapi Support", 
00074                   "service," NS_IMAPISUPPORT_CONTRACTID, PR_TRUE, PR_TRUE, nsnull);
00075  
00076   return rv ;
00077 
00078 }
00079 
00080 NS_IMETHODIMP
00081 nsMapiSupport::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData)
00082 {
00083     nsresult rv = NS_OK ;
00084 
00085     if (!nsCRT::strcmp(aTopic, "profile-after-change"))
00086         return InitializeMAPISupport();
00087 
00088     if (!nsCRT::strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID))
00089         return ShutdownMAPISupport();
00090 
00091     nsCOMPtr<nsIObserverService> observerService(do_GetService("@mozilla.org/observer-service;1", &rv));
00092     if (NS_FAILED(rv)) return rv;
00093  
00094     rv = observerService->AddObserver(this,"profile-after-change", PR_FALSE);
00095     if (NS_FAILED(rv)) return rv;
00096 
00097     rv = observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_FALSE);
00098     if (NS_FAILED(rv))  return rv;
00099 
00100     return rv;
00101 }
00102 
00103 
00104 nsMapiSupport::nsMapiSupport()
00105 : m_dwRegister(0),
00106   m_nsMapiFactory(nsnull)
00107 {
00108 }
00109 
00110 nsMapiSupport::~nsMapiSupport()
00111 {
00112 }
00113 
00114 NS_IMETHODIMP
00115 nsMapiSupport::InitializeMAPISupport()
00116 {
00117     ::CoInitialize(nsnull) ;
00118 
00119     if (m_nsMapiFactory == nsnull)    // No Registering if already done.  Sanity Check!!
00120     {
00121         m_nsMapiFactory = new CMapiFactory();
00122 
00123         if (m_nsMapiFactory != nsnull)
00124         {
00125             HRESULT hr = ::CoRegisterClassObject(CLSID_CMapiImp, \
00126                                                  m_nsMapiFactory, \
00127                                                  CLSCTX_LOCAL_SERVER, \
00128                                                  REGCLS_MULTIPLEUSE, \
00129                                                  &m_dwRegister);
00130 
00131             if (FAILED(hr))
00132             {
00133                 m_nsMapiFactory->Release() ;
00134                 m_nsMapiFactory = nsnull;
00135                 return NS_ERROR_FAILURE;
00136             }
00137         }
00138     }
00139 
00140     return NS_OK;
00141 }
00142 
00143 NS_IMETHODIMP
00144 nsMapiSupport::ShutdownMAPISupport()
00145 {
00146     if (m_dwRegister != 0)
00147         ::CoRevokeClassObject(m_dwRegister);
00148 
00149     if (m_nsMapiFactory != nsnull)
00150     {
00151         m_nsMapiFactory->Release();
00152         m_nsMapiFactory = nsnull;
00153     }
00154 
00155     ::CoUninitialize();
00156 
00157     return NS_OK ;
00158 }
00159 
00160 NS_IMETHODIMP
00161 nsMapiSupport::RegisterServer()
00162 {
00163   // TODO: Figure out what kind of error propogation to pass back
00164   ::RegisterServer(CLSID_CMapiImp, "Mozilla MAPI", "MozillaMapi", "MozillaMapi.1");
00165   return NS_OK;
00166 }
00167 
00168 NS_IMETHODIMP
00169 nsMapiSupport::UnRegisterServer()
00170 {
00171   // TODO: Figure out what kind of error propogation to pass back
00172   ::UnregisterServer(CLSID_CMapiImp, "MozillaMapi", "MozillaMapi.1");
00173   return NS_OK;
00174 }
00175 
00176 
00177 #ifndef MOZ_THUNDERBIRD
00178 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMapiRegistry)
00179 #endif
00180 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMapiSupport)
00181 
00182 // The list of components we register
00183 static const nsModuleComponentInfo components[] = 
00184 {
00185 #ifndef MOZ_THUNDERBIRD
00186   {
00187     NS_IMAPIREGISTRY_CLASSNAME, 
00188     NS_IMAPIREGISTRY_CID,
00189     NS_IMAPIREGISTRY_CONTRACTID, 
00190     nsMapiRegistryConstructor
00191   },
00192 #endif
00193 
00194 {
00195     NS_IMAPISUPPORT_CLASSNAME,
00196     NS_IMAPISUPPORT_CID,
00197     NS_IMAPISUPPORT_CONTRACTID,
00198     nsMapiSupportConstructor,
00199     nsMapiRegistrationProc,
00200     nsnull
00201 }
00202 };
00203 
00204 NS_IMPL_NSGETMODULE(msgMapiModule, components)
00205