Back to index

lightning-sunbird  0.9+nobinonly
nsMailDirProvider.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 Mail Directory Provider.
00015  *
00016  * The Initial Developer of the Original Code is
00017  *   Scott MacGregor <mscott@mozilla.org>.
00018  * Portions created by the Initial Developer are Copyright (C) 2006
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
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 
00038 #include "nsMailDirProvider.h"
00039 #include "nsMailDirServiceDefs.h"
00040 #ifdef MOZ_XUL_APP
00041 #include "nsXULAppAPI.h"
00042 #endif
00043 #include "nsMsgBaseCID.h"
00044 #include "nsArrayEnumerator.h"
00045 #include "nsCOMArray.h"
00046 #include "nsEnumeratorUtils.h"
00047 #include "nsDirectoryServiceDefs.h"
00048 #include "nsIChromeRegistry.h"
00049 #include "nsICategoryManager.h"
00050 
00051 NS_IMPL_ISUPPORTS2(nsMailDirProvider,
00052                  nsIDirectoryServiceProvider,
00053                  nsIDirectoryServiceProvider2)
00054 
00055 NS_IMETHODIMP
00056 nsMailDirProvider::GetFile(const char *aKey, PRBool *aPersist,
00057                             nsIFile* *aResult)
00058 {
00059   return NS_ERROR_FAILURE;
00060 }
00061 
00062 NS_IMETHODIMP
00063 nsMailDirProvider::GetFiles(const char *aKey,
00064                             nsISimpleEnumerator* *aResult)
00065 {
00066   if (strcmp(aKey, ISP_DIRECTORY_LIST) != 0)
00067     return NS_ERROR_FAILURE;
00068 
00069   // The list of isp directories includes the isp directory
00070   // in the current process dir (i.e. <path to thunderbird.exe>\isp and 
00071   // <path to thunderbird.exe>\isp\locale 
00072   // and isp and isp\locale for each active extension
00073 
00074   nsCOMPtr<nsIProperties> dirSvc =
00075     do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
00076   if (!dirSvc)
00077     return NS_ERROR_FAILURE;
00078 
00079   nsCOMPtr<nsIFile> currentProcessDir;
00080   nsresult rv = dirSvc->Get(NS_XPCOM_CURRENT_PROCESS_DIR,
00081                    NS_GET_IID(nsIFile), getter_AddRefs(currentProcessDir));
00082   NS_ENSURE_SUCCESS(rv, rv);
00083 
00084   nsCOMPtr<nsISimpleEnumerator> directoryEnumerator;
00085   rv = NS_NewSingletonEnumerator(getter_AddRefs(directoryEnumerator), currentProcessDir);
00086   NS_ENSURE_SUCCESS(rv, rv);
00087 
00088   nsCOMPtr<nsISimpleEnumerator> combinedEnumerator;
00089 
00090 #ifdef MOZ_XUL_APP
00091   nsCOMPtr<nsISimpleEnumerator> extensionsEnum;
00092   rv = dirSvc->Get(XRE_EXTENSIONS_DIR_LIST,
00093                          NS_GET_IID(nsISimpleEnumerator),
00094                          getter_AddRefs(extensionsEnum));
00095   NS_ENSURE_SUCCESS(rv, rv);
00096 
00097   rv = NS_NewUnionEnumerator(getter_AddRefs(combinedEnumerator), directoryEnumerator, extensionsEnum);
00098   NS_ENSURE_SUCCESS(rv, rv);
00099 #else
00100   directoryEnumerator.swap(combinedEnumerator);
00101 #endif
00102    
00103   NS_IF_ADDREF(*aResult = new AppendingEnumerator(combinedEnumerator));
00104   return NS_SUCCESS_AGGREGATE_RESULT;
00105 }
00106 
00107 NS_IMPL_ISUPPORTS1(nsMailDirProvider::AppendingEnumerator,
00108                  nsISimpleEnumerator)
00109 
00110 NS_IMETHODIMP
00111 nsMailDirProvider::AppendingEnumerator::HasMoreElements(PRBool *aResult)
00112 {
00113   *aResult = mNext || mNextWithLocale ? PR_TRUE : PR_FALSE;
00114   return NS_OK;
00115 }
00116 
00117 NS_IMETHODIMP
00118 nsMailDirProvider::AppendingEnumerator::GetNext(nsISupports* *aResult)
00119 {
00120   // Set the return value to the next directory we want to enumerate over
00121   if (aResult)
00122     NS_ADDREF(*aResult = mNext);
00123 
00124   if (mNextWithLocale)
00125   {
00126     mNext = mNextWithLocale;
00127     mNextWithLocale = nsnull;
00128     return NS_OK;
00129   }
00130 
00131   mNext = nsnull;
00132 
00133   // Ignore all errors
00134 
00135   PRBool more;
00136   while (NS_SUCCEEDED(mBase->HasMoreElements(&more)) && more) {
00137     nsCOMPtr<nsISupports> nextbasesupp;
00138     mBase->GetNext(getter_AddRefs(nextbasesupp));
00139 
00140     nsCOMPtr<nsIFile> nextbase(do_QueryInterface(nextbasesupp));
00141     if (!nextbase)
00142       continue;
00143 
00144     nextbase->Clone(getter_AddRefs(mNext));
00145     if (!mNext)
00146       continue;
00147 
00148     mNext->AppendNative(NS_LITERAL_CSTRING("isp"));
00149     PRBool exists;
00150     nsresult rv = mNext->Exists(&exists);
00151     if (NS_SUCCEEDED(rv) && exists)
00152     {
00153       if (!mLocale.IsEmpty())
00154       {
00155         mNext->Clone(getter_AddRefs(mNextWithLocale));
00156         mNextWithLocale->AppendNative(mLocale);
00157         rv = mNextWithLocale->Exists(&exists);
00158         if (NS_FAILED(rv) || !exists)
00159           mNextWithLocale = nsnull; // clear out mNextWithLocale, so we don't try to iterate over it
00160       } 
00161       break;
00162     }
00163 
00164     mNext = nsnull;
00165   }
00166 
00167   return NS_OK;
00168 }
00169 
00170 nsMailDirProvider::AppendingEnumerator::AppendingEnumerator
00171     (nsISimpleEnumerator* aBase) :
00172   mBase(aBase)
00173 {
00174   nsCOMPtr<nsIXULChromeRegistry> packageRegistry = do_GetService("@mozilla.org/chrome/chrome-registry;1");
00175   if (packageRegistry)
00176     packageRegistry->GetSelectedLocale(NS_LITERAL_CSTRING("global"), mLocale);
00177   // Initialize mNext to begin
00178   GetNext(nsnull);
00179 }
00180 
00181 NS_METHOD
00182 nsMailDirProvider::Register(nsIComponentManager* aCompMgr,
00183                             nsIFile* aPath, const char *aLoaderStr,
00184                             const char *aType,
00185                             const nsModuleComponentInfo *aInfo)
00186 {
00187   nsresult rv;
00188 
00189   nsCOMPtr<nsICategoryManager> catMan =
00190     do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
00191   if (!catMan)
00192     return NS_ERROR_FAILURE;
00193 
00194   rv = catMan->AddCategoryEntry(XPCOM_DIRECTORY_PROVIDER_CATEGORY,
00195                             "mail-directory-provider",
00196                             NS_MAILDIRPROVIDER_CONTRACTID, PR_TRUE, PR_TRUE, nsnull);
00197   return rv;
00198 }
00199 
00200 NS_METHOD
00201 nsMailDirProvider::Unregister(nsIComponentManager* aCompMgr,
00202                               nsIFile* aPath,
00203                               const char *aLoaderStr,
00204                               const nsModuleComponentInfo *aInfo)
00205 {
00206   nsresult rv;
00207 
00208   nsCOMPtr<nsICategoryManager> catMan =
00209     do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
00210   if (!catMan)
00211     return NS_ERROR_FAILURE;
00212 
00213   rv = catMan->DeleteCategoryEntry(XPCOM_DIRECTORY_PROVIDER_CATEGORY,
00214                                "mail-directory-provider",
00215                                PR_TRUE);
00216   return rv;
00217 }