/extensions/spellcheck/hunspell/src/mozHunspellDirProvider.cpp

http://github.com/zpao/v8monkey · C++ · 140 lines · 80 code · 24 blank · 36 comment · 12 complexity · b9bc0118f92a9f8f1ce8c7bd1283e979 MD5 · raw file

  1. /******* BEGIN LICENSE BLOCK *******
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Initial Developers of the Original Code are Kevin Hendricks (MySpell)
  15. * and László Németh (Hunspell). Portions created by the Initial Developers
  16. * are Copyright (C) 2002-2005 the Initial Developers. All Rights Reserved.
  17. *
  18. * Contributor(s): Benjamin Smedberg (benjamin@smedbergs.us) (Original Code)
  19. * László Németh (nemethl@gyorsposta.hu)
  20. * Ryan VanderMeulen (ryanvm@gmail.com)
  21. *
  22. * Alternatively, the contents of this file may be used under the terms of
  23. * either the GNU General Public License Version 2 or later (the "GPL"), or
  24. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  25. * in which case the provisions of the GPL or the LGPL are applicable instead
  26. * of those above. If you wish to allow use of your version of this file only
  27. * under the terms of either the GPL or the LGPL, and not to allow others to
  28. * use your version of this file under the terms of the MPL, indicate your
  29. * decision by deleting the provisions above and replace them with the notice
  30. * and other provisions required by the GPL or the LGPL. If you do not delete
  31. * the provisions above, a recipient may use your version of this file under
  32. * the terms of any one of the MPL, the GPL or the LGPL.
  33. *
  34. ******* END LICENSE BLOCK *******/
  35. #include "mozHunspellDirProvider.h"
  36. #include "nsXULAppAPI.h"
  37. #include "nsString.h"
  38. #include "mozISpellCheckingEngine.h"
  39. #include "nsICategoryManager.h"
  40. NS_IMPL_ISUPPORTS2(mozHunspellDirProvider,
  41. nsIDirectoryServiceProvider,
  42. nsIDirectoryServiceProvider2)
  43. NS_IMETHODIMP
  44. mozHunspellDirProvider::GetFile(const char *aKey, bool *aPersist,
  45. nsIFile* *aResult)
  46. {
  47. return NS_ERROR_FAILURE;
  48. }
  49. NS_IMETHODIMP
  50. mozHunspellDirProvider::GetFiles(const char *aKey,
  51. nsISimpleEnumerator* *aResult)
  52. {
  53. if (strcmp(aKey, DICTIONARY_SEARCH_DIRECTORY_LIST) != 0) {
  54. return NS_ERROR_FAILURE;
  55. }
  56. nsCOMPtr<nsIProperties> dirSvc =
  57. do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
  58. if (!dirSvc)
  59. return NS_ERROR_FAILURE;
  60. nsCOMPtr<nsISimpleEnumerator> list;
  61. nsresult rv = dirSvc->Get(XRE_EXTENSIONS_DIR_LIST,
  62. NS_GET_IID(nsISimpleEnumerator),
  63. getter_AddRefs(list));
  64. if (NS_FAILED(rv))
  65. return rv;
  66. nsCOMPtr<nsISimpleEnumerator> e = new AppendingEnumerator(list);
  67. if (!e)
  68. return NS_ERROR_OUT_OF_MEMORY;
  69. *aResult = nsnull;
  70. e.swap(*aResult);
  71. return NS_SUCCESS_AGGREGATE_RESULT;
  72. }
  73. NS_IMPL_ISUPPORTS1(mozHunspellDirProvider::AppendingEnumerator,
  74. nsISimpleEnumerator)
  75. NS_IMETHODIMP
  76. mozHunspellDirProvider::AppendingEnumerator::HasMoreElements(bool *aResult)
  77. {
  78. *aResult = mNext ? true : false;
  79. return NS_OK;
  80. }
  81. NS_IMETHODIMP
  82. mozHunspellDirProvider::AppendingEnumerator::GetNext(nsISupports* *aResult)
  83. {
  84. if (aResult)
  85. NS_ADDREF(*aResult = mNext);
  86. mNext = nsnull;
  87. nsresult rv;
  88. // Ignore all errors
  89. bool more;
  90. while (NS_SUCCEEDED(mBase->HasMoreElements(&more)) && more) {
  91. nsCOMPtr<nsISupports> nextbasesupp;
  92. mBase->GetNext(getter_AddRefs(nextbasesupp));
  93. nsCOMPtr<nsIFile> nextbase(do_QueryInterface(nextbasesupp));
  94. if (!nextbase)
  95. continue;
  96. nextbase->Clone(getter_AddRefs(mNext));
  97. if (!mNext)
  98. continue;
  99. mNext->AppendNative(NS_LITERAL_CSTRING("dictionaries"));
  100. bool exists;
  101. rv = mNext->Exists(&exists);
  102. if (NS_SUCCEEDED(rv) && exists)
  103. break;
  104. mNext = nsnull;
  105. }
  106. return NS_OK;
  107. }
  108. mozHunspellDirProvider::AppendingEnumerator::AppendingEnumerator
  109. (nsISimpleEnumerator* aBase) :
  110. mBase(aBase)
  111. {
  112. // Initialize mNext to begin
  113. GetNext(nsnull);
  114. }
  115. char const *const
  116. mozHunspellDirProvider::kContractID = "@mozilla.org/spellcheck/dir-provider;1";