Back to index

lightning-sunbird  0.9+nobinonly
mozMySpellDirProvider.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 Firefox.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * the Mozilla Foundation <http://www.mozilla.org>.
00018  *
00019  * Portions created by the Initial Developer are Copyright (C) 2006
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  * Benjamin Smedberg <benjamin@smedbergs.us> (Original Code)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * 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 
00039 #include "mozMySpellDirProvider.h"
00040 #include "nsXULAppAPI.h"
00041 #include "nsString.h"
00042 
00043 #include "mozISpellCheckingEngine.h"
00044 #include "nsICategoryManager.h"
00045 
00046 NS_IMPL_ISUPPORTS2(mozMySpellDirProvider,
00047                  nsIDirectoryServiceProvider,
00048                  nsIDirectoryServiceProvider2)
00049 
00050 NS_IMETHODIMP
00051 mozMySpellDirProvider::GetFile(const char *aKey, PRBool *aPersist,
00052                             nsIFile* *aResult)
00053 {
00054   return NS_ERROR_FAILURE;
00055 }
00056 
00057 NS_IMETHODIMP
00058 mozMySpellDirProvider::GetFiles(const char *aKey,
00059                             nsISimpleEnumerator* *aResult)
00060 {
00061   if (strcmp(aKey, DICTIONARY_SEARCH_DIRECTORY_LIST) != 0) {
00062     return NS_ERROR_FAILURE;
00063   }
00064 
00065   nsCOMPtr<nsIProperties> dirSvc =
00066     do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
00067   if (!dirSvc)
00068     return NS_ERROR_FAILURE;
00069 
00070   nsCOMPtr<nsISimpleEnumerator> list;
00071   nsresult rv = dirSvc->Get(XRE_EXTENSIONS_DIR_LIST,
00072                          NS_GET_IID(nsISimpleEnumerator),
00073                          getter_AddRefs(list));
00074   if (NS_FAILED(rv))
00075     return rv;
00076 
00077   nsCOMPtr<nsISimpleEnumerator> e = new AppendingEnumerator(list);
00078   if (!e)
00079     return NS_ERROR_OUT_OF_MEMORY;
00080 
00081   *aResult = nsnull;
00082   e.swap(*aResult);
00083   return NS_SUCCESS_AGGREGATE_RESULT;
00084 }
00085 
00086 NS_IMPL_ISUPPORTS1(mozMySpellDirProvider::AppendingEnumerator,
00087                  nsISimpleEnumerator)
00088 
00089 NS_IMETHODIMP
00090 mozMySpellDirProvider::AppendingEnumerator::HasMoreElements(PRBool *aResult)
00091 {
00092   *aResult = mNext ? PR_TRUE : PR_FALSE;
00093   return NS_OK;
00094 }
00095 
00096 NS_IMETHODIMP
00097 mozMySpellDirProvider::AppendingEnumerator::GetNext(nsISupports* *aResult)
00098 {
00099   if (aResult)
00100     NS_ADDREF(*aResult = mNext);
00101 
00102   mNext = nsnull;
00103 
00104   nsresult rv;
00105 
00106   // Ignore all errors
00107 
00108   PRBool more;
00109   while (NS_SUCCEEDED(mBase->HasMoreElements(&more)) && more) {
00110     nsCOMPtr<nsISupports> nextbasesupp;
00111     mBase->GetNext(getter_AddRefs(nextbasesupp));
00112 
00113     nsCOMPtr<nsIFile> nextbase(do_QueryInterface(nextbasesupp));
00114     if (!nextbase)
00115       continue;
00116 
00117     nextbase->Clone(getter_AddRefs(mNext));
00118     if (!mNext)
00119       continue;
00120 
00121     mNext->AppendNative(NS_LITERAL_CSTRING("dictionaries"));
00122 
00123     PRBool exists;
00124     rv = mNext->Exists(&exists);
00125     if (NS_SUCCEEDED(rv) && exists)
00126       break;
00127 
00128     mNext = nsnull;
00129   }
00130 
00131   return NS_OK;
00132 }
00133 
00134 mozMySpellDirProvider::AppendingEnumerator::AppendingEnumerator
00135     (nsISimpleEnumerator* aBase) :
00136   mBase(aBase)
00137 {
00138   // Initialize mNext to begin
00139   GetNext(nsnull);
00140 }
00141 
00142 NS_METHOD
00143 mozMySpellDirProvider::Register(nsIComponentManager* aCompMgr,
00144                             nsIFile* aPath, const char *aLoaderStr,
00145                             const char *aType,
00146                             const nsModuleComponentInfo *aInfo)
00147 {
00148   nsresult rv;
00149 
00150   nsCOMPtr<nsICategoryManager> catMan =
00151     do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
00152   if (!catMan)
00153     return NS_ERROR_FAILURE;
00154 
00155   rv = catMan->AddCategoryEntry(XPCOM_DIRECTORY_PROVIDER_CATEGORY,
00156                             "spellcheck-directory-provider",
00157                             kContractID, PR_TRUE, PR_TRUE, nsnull);
00158   return rv;
00159 }
00160 
00161 NS_METHOD
00162 mozMySpellDirProvider::Unregister(nsIComponentManager* aCompMgr,
00163                               nsIFile* aPath,
00164                               const char *aLoaderStr,
00165                               const nsModuleComponentInfo *aInfo)
00166 {
00167   nsresult rv;
00168 
00169   nsCOMPtr<nsICategoryManager> catMan =
00170     do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
00171   if (!catMan)
00172     return NS_ERROR_FAILURE;
00173 
00174   rv = catMan->DeleteCategoryEntry(XPCOM_DIRECTORY_PROVIDER_CATEGORY,
00175                                "spellcheck-directory-provider",
00176                                PR_TRUE);
00177   return rv;
00178 }
00179 
00180 char const *const
00181 mozMySpellDirProvider::kContractID = "@mozilla.org/spellcheck/dir-provider;1";