/extensions/spellcheck/src/mozPersonalDictionary.cpp

http://github.com/zpao/v8monkey · C++ · 299 lines · 182 code · 50 blank · 67 comment · 46 complexity · a29252b65ad45b35e798b622cfcdf360 MD5 · raw file

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is Mozilla Spellchecker Component.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * David Einstein.
  19. * Portions created by the Initial Developer are Copyright (C) 2001
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s): David Einstein <Deinst@world.std.com>
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. #include "mozPersonalDictionary.h"
  38. #include "nsIUnicharInputStream.h"
  39. #include "nsReadableUtils.h"
  40. #include "nsIFile.h"
  41. #include "nsAppDirectoryServiceDefs.h"
  42. #include "nsICharsetConverterManager.h"
  43. #include "nsICharsetAlias.h"
  44. #include "nsIObserverService.h"
  45. #include "nsIPrefService.h"
  46. #include "nsIPrefBranch.h"
  47. #include "nsIWeakReference.h"
  48. #include "nsCRT.h"
  49. #include "nsNetUtil.h"
  50. #include "nsStringEnumerator.h"
  51. #include "nsUnicharInputStream.h"
  52. #define MOZ_PERSONAL_DICT_NAME "persdict.dat"
  53. const int kMaxWordLen=256;
  54. /**
  55. * This is the most braindead implementation of a personal dictionary possible.
  56. * There is not much complexity needed, though. It could be made much faster,
  57. * and probably should, but I don't see much need for more in terms of interface.
  58. *
  59. * Allowing personal words to be associated with only certain dictionaries maybe.
  60. *
  61. * TODO:
  62. * Implement the suggestion record.
  63. */
  64. NS_IMPL_CYCLE_COLLECTING_ADDREF(mozPersonalDictionary)
  65. NS_IMPL_CYCLE_COLLECTING_RELEASE(mozPersonalDictionary)
  66. NS_INTERFACE_MAP_BEGIN(mozPersonalDictionary)
  67. NS_INTERFACE_MAP_ENTRY(mozIPersonalDictionary)
  68. NS_INTERFACE_MAP_ENTRY(nsIObserver)
  69. NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
  70. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIPersonalDictionary)
  71. NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(mozPersonalDictionary)
  72. NS_INTERFACE_MAP_END
  73. NS_IMPL_CYCLE_COLLECTION_1(mozPersonalDictionary, mEncoder)
  74. mozPersonalDictionary::mozPersonalDictionary()
  75. : mDirty(false)
  76. {
  77. }
  78. mozPersonalDictionary::~mozPersonalDictionary()
  79. {
  80. }
  81. nsresult mozPersonalDictionary::Init()
  82. {
  83. if (!mDictionaryTable.Init() || !mIgnoreTable.Init())
  84. return NS_ERROR_OUT_OF_MEMORY;
  85. nsresult rv;
  86. nsCOMPtr<nsIObserverService> svc =
  87. do_GetService("@mozilla.org/observer-service;1", &rv);
  88. if (NS_SUCCEEDED(rv) && svc)
  89. rv = svc->AddObserver(this, "profile-do-change", true); // we want to reload the dictionary if the profile switches
  90. if (NS_FAILED(rv)) return rv;
  91. Load();
  92. return NS_OK;
  93. }
  94. /* void Load (); */
  95. NS_IMETHODIMP mozPersonalDictionary::Load()
  96. {
  97. //FIXME Deinst -- get dictionary name from prefs;
  98. nsresult res;
  99. nsCOMPtr<nsIFile> theFile;
  100. bool dictExists;
  101. res = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(theFile));
  102. if(NS_FAILED(res)) return res;
  103. if(!theFile)return NS_ERROR_FAILURE;
  104. res = theFile->Append(NS_LITERAL_STRING(MOZ_PERSONAL_DICT_NAME));
  105. if(NS_FAILED(res)) return res;
  106. res = theFile->Exists(&dictExists);
  107. if(NS_FAILED(res)) return res;
  108. if (!dictExists) {
  109. // Nothing is really wrong...
  110. return NS_OK;
  111. }
  112. nsCOMPtr<nsIInputStream> inStream;
  113. NS_NewLocalFileInputStream(getter_AddRefs(inStream), theFile);
  114. nsCOMPtr<nsIUnicharInputStream> convStream;
  115. res = nsSimpleUnicharStreamFactory::GetInstance()->
  116. CreateInstanceFromUTF8Stream(inStream, getter_AddRefs(convStream));
  117. if(NS_FAILED(res)) return res;
  118. // we're rereading to get rid of the old data -- we shouldn't have any, but...
  119. mDictionaryTable.Clear();
  120. PRUnichar c;
  121. PRUint32 nRead;
  122. bool done = false;
  123. do{ // read each line of text into the string array.
  124. if( (NS_OK != convStream->Read(&c, 1, &nRead)) || (nRead != 1)) break;
  125. while(!done && ((c == '\n') || (c == '\r'))){
  126. if( (NS_OK != convStream->Read(&c, 1, &nRead)) || (nRead != 1)) done = true;
  127. }
  128. if (!done){
  129. nsAutoString word;
  130. while((c != '\n') && (c != '\r') && !done){
  131. word.Append(c);
  132. if( (NS_OK != convStream->Read(&c, 1, &nRead)) || (nRead != 1)) done = true;
  133. }
  134. mDictionaryTable.PutEntry(word.get());
  135. }
  136. } while(!done);
  137. mDirty = false;
  138. return res;
  139. }
  140. // A little helper function to add the key to the list.
  141. // This is not threadsafe, and only safe if the consumer does not
  142. // modify the list.
  143. static PLDHashOperator
  144. AddHostToStringArray(nsUnicharPtrHashKey *aEntry, void *aArg)
  145. {
  146. static_cast<nsTArray<nsString>*>(aArg)->AppendElement(nsDependentString(aEntry->GetKey()));
  147. return PL_DHASH_NEXT;
  148. }
  149. /* void Save (); */
  150. NS_IMETHODIMP mozPersonalDictionary::Save()
  151. {
  152. nsCOMPtr<nsIFile> theFile;
  153. nsresult res;
  154. if(!mDirty) return NS_OK;
  155. //FIXME Deinst -- get dictionary name from prefs;
  156. res = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(theFile));
  157. if(NS_FAILED(res)) return res;
  158. if(!theFile)return NS_ERROR_FAILURE;
  159. res = theFile->Append(NS_LITERAL_STRING(MOZ_PERSONAL_DICT_NAME));
  160. if(NS_FAILED(res)) return res;
  161. nsCOMPtr<nsIOutputStream> outStream;
  162. NS_NewLocalFileOutputStream(getter_AddRefs(outStream), theFile, PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE ,0664);
  163. // get a buffered output stream 4096 bytes big, to optimize writes
  164. nsCOMPtr<nsIOutputStream> bufferedOutputStream;
  165. res = NS_NewBufferedOutputStream(getter_AddRefs(bufferedOutputStream), outStream, 4096);
  166. if (NS_FAILED(res)) return res;
  167. nsTArray<nsString> array(mDictionaryTable.Count());
  168. mDictionaryTable.EnumerateEntries(AddHostToStringArray, &array);
  169. PRUint32 bytesWritten;
  170. nsCAutoString utf8Key;
  171. for (PRUint32 i = 0; i < array.Length(); ++i ) {
  172. CopyUTF16toUTF8(array[i], utf8Key);
  173. bufferedOutputStream->Write(utf8Key.get(), utf8Key.Length(), &bytesWritten);
  174. bufferedOutputStream->Write("\n", 1, &bytesWritten);
  175. }
  176. return res;
  177. }
  178. /* readonly attribute nsIStringEnumerator GetWordList() */
  179. NS_IMETHODIMP mozPersonalDictionary::GetWordList(nsIStringEnumerator **aWords)
  180. {
  181. NS_ENSURE_ARG_POINTER(aWords);
  182. *aWords = nsnull;
  183. nsTArray<nsString> *array = new nsTArray<nsString>(mDictionaryTable.Count());
  184. if (!array)
  185. return NS_ERROR_OUT_OF_MEMORY;
  186. mDictionaryTable.EnumerateEntries(AddHostToStringArray, array);
  187. array->Sort();
  188. return NS_NewAdoptingStringEnumerator(aWords, array);
  189. }
  190. /* boolean Check (in wstring word, in wstring language); */
  191. NS_IMETHODIMP mozPersonalDictionary::Check(const PRUnichar *aWord, const PRUnichar *aLanguage, bool *aResult)
  192. {
  193. NS_ENSURE_ARG_POINTER(aWord);
  194. NS_ENSURE_ARG_POINTER(aResult);
  195. *aResult = (mDictionaryTable.GetEntry(aWord) || mIgnoreTable.GetEntry(aWord));
  196. return NS_OK;
  197. }
  198. /* void AddWord (in wstring word); */
  199. NS_IMETHODIMP mozPersonalDictionary::AddWord(const PRUnichar *aWord, const PRUnichar *aLang)
  200. {
  201. mDictionaryTable.PutEntry(aWord);
  202. mDirty = true;
  203. return NS_OK;
  204. }
  205. /* void RemoveWord (in wstring word); */
  206. NS_IMETHODIMP mozPersonalDictionary::RemoveWord(const PRUnichar *aWord, const PRUnichar *aLang)
  207. {
  208. mDictionaryTable.RemoveEntry(aWord);
  209. mDirty = true;
  210. return NS_OK;
  211. }
  212. /* void IgnoreWord (in wstring word); */
  213. NS_IMETHODIMP mozPersonalDictionary::IgnoreWord(const PRUnichar *aWord)
  214. {
  215. // avoid adding duplicate words to the ignore list
  216. if (aWord && !mIgnoreTable.GetEntry(aWord))
  217. mIgnoreTable.PutEntry(aWord);
  218. return NS_OK;
  219. }
  220. /* void EndSession (); */
  221. NS_IMETHODIMP mozPersonalDictionary::EndSession()
  222. {
  223. Save(); // save any custom words at the end of a spell check session
  224. mIgnoreTable.Clear();
  225. return NS_OK;
  226. }
  227. /* void AddCorrection (in wstring word, in wstring correction); */
  228. NS_IMETHODIMP mozPersonalDictionary::AddCorrection(const PRUnichar *word, const PRUnichar *correction, const PRUnichar *lang)
  229. {
  230. return NS_ERROR_NOT_IMPLEMENTED;
  231. }
  232. /* void RemoveCorrection (in wstring word, in wstring correction); */
  233. NS_IMETHODIMP mozPersonalDictionary::RemoveCorrection(const PRUnichar *word, const PRUnichar *correction, const PRUnichar *lang)
  234. {
  235. return NS_ERROR_NOT_IMPLEMENTED;
  236. }
  237. /* void GetCorrection (in wstring word, [array, size_is (count)] out wstring words, out PRUint32 count); */
  238. NS_IMETHODIMP mozPersonalDictionary::GetCorrection(const PRUnichar *word, PRUnichar ***words, PRUint32 *count)
  239. {
  240. return NS_ERROR_NOT_IMPLEMENTED;
  241. }
  242. /* void observe (in nsISupports aSubject, in string aTopic, in wstring aData); */
  243. NS_IMETHODIMP mozPersonalDictionary::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData)
  244. {
  245. if (!nsCRT::strcmp(aTopic, "profile-do-change")) {
  246. Load(); // load automatically clears out the existing dictionary table
  247. }
  248. return NS_OK;
  249. }