| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/webui/options/language_dictionary_overlay_handler.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/utf_string_conversions.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
| 12 #include "chrome/browser/spellchecker/spellcheck_service.h" |
| 13 #include "chrome/common/url_constants.h" |
| 14 #include "content/public/browser/web_ui.h" |
| 15 #include "grit/generated_resources.h" |
| 16 #include "grit/locale_settings.h" |
| 17 #include "ui/base/l10n/l10n_util.h" |
| 18 |
| 19 namespace options { |
| 20 namespace { |
| 21 bool StringCompare(const std::string& a, const std::string& b) { |
| 22 return a.compare(b) < 0; |
| 23 } |
| 24 } // namespace |
| 25 |
| 26 LanguageDictionaryOverlayHandler::LanguageDictionaryOverlayHandler() |
| 27 : overlay_initialized_(false), |
| 28 dictionary_(NULL) { |
| 29 } |
| 30 |
| 31 LanguageDictionaryOverlayHandler::~LanguageDictionaryOverlayHandler() { |
| 32 } |
| 33 |
| 34 void LanguageDictionaryOverlayHandler::GetLocalizedValues( |
| 35 base::DictionaryValue* localized_strings) { |
| 36 RegisterTitle(localized_strings, |
| 37 "languageDictionaryOverlayPage", |
| 38 IDS_LANGUAGE_DICTIONARY_OVERLAY_TITLE); |
| 39 localized_strings->SetString( |
| 40 "languageDictionaryOverlayTitle", |
| 41 l10n_util::GetStringUTF16(IDS_LANGUAGE_DICTIONARY_OVERLAY_TITLE)); |
| 42 localized_strings->SetString( |
| 43 "languageDictionaryOverlayAddWordLabel", |
| 44 l10n_util::GetStringUTF16( |
| 45 IDS_LANGUAGE_DICTIONARY_OVERLAY_ADD_WORD_LABEL)); |
| 46 } |
| 47 |
| 48 void LanguageDictionaryOverlayHandler::InitializeHandler() { |
| 49 dictionary_ = SpellcheckServiceFactory::GetForProfile( |
| 50 Profile::FromWebUI(web_ui()))->GetCustomDictionary(); |
| 51 dictionary_->AddObserver(this); |
| 52 } |
| 53 |
| 54 void LanguageDictionaryOverlayHandler::InitializePage() { |
| 55 } |
| 56 |
| 57 void LanguageDictionaryOverlayHandler::RegisterMessages() { |
| 58 web_ui()->RegisterMessageCallback( |
| 59 "editDictionaryWord", |
| 60 base::Bind(&LanguageDictionaryOverlayHandler::EditWord, |
| 61 base::Unretained(this))); |
| 62 web_ui()->RegisterMessageCallback( |
| 63 "refreshDictionaryWords", |
| 64 base::Bind(&LanguageDictionaryOverlayHandler::RefreshWords, |
| 65 base::Unretained(this))); |
| 66 } |
| 67 |
| 68 void LanguageDictionaryOverlayHandler::Uninitialize() { |
| 69 overlay_initialized_ = false; |
| 70 dictionary_->RemoveObserver(this); |
| 71 } |
| 72 |
| 73 void LanguageDictionaryOverlayHandler::OnCustomDictionaryLoaded() { |
| 74 UpdateWordList(); |
| 75 } |
| 76 |
| 77 void LanguageDictionaryOverlayHandler::OnCustomDictionaryWordAdded( |
| 78 const std::string& word) { |
| 79 UpdateWordList(); |
| 80 } |
| 81 |
| 82 void LanguageDictionaryOverlayHandler::OnCustomDictionaryWordRemoved( |
| 83 const std::string& word) { |
| 84 UpdateWordList(); |
| 85 } |
| 86 |
| 87 void LanguageDictionaryOverlayHandler::UpdateWordList() { |
| 88 if (!overlay_initialized_) |
| 89 return; |
| 90 chrome::spellcheck_common::WordList words = dictionary_->GetWords(); |
| 91 sort(words.begin(), words.end(), StringCompare); |
| 92 ListValue word_list; |
| 93 word_list.AppendStrings(words); |
| 94 web_ui()->CallJavascriptFunction("EditDictionaryOverlay.setWordList", |
| 95 word_list); |
| 96 } |
| 97 |
| 98 void LanguageDictionaryOverlayHandler::RefreshWords(const ListValue* args) { |
| 99 overlay_initialized_ = true; |
| 100 UpdateWordList(); |
| 101 } |
| 102 |
| 103 void LanguageDictionaryOverlayHandler::EditWord(const ListValue* args) { |
| 104 std::string old_word; |
| 105 std::string new_word; |
| 106 if (!args->GetString(0, &old_word) || !args->GetString(1, &new_word)) { |
| 107 NOTREACHED(); |
| 108 return; |
| 109 } |
| 110 if (!old_word.empty()) |
| 111 dictionary_->RemoveWord(old_word); |
| 112 if (!new_word.empty()) |
| 113 dictionary_->AddWord(new_word); |
| 114 } |
| 115 |
| 116 } // namespace options |
| OLD | NEW |