| 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/values.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
| 11 #include "chrome/browser/spellchecker/spellcheck_service.h" |
| 12 #include "content/public/browser/web_ui.h" |
| 13 #include "grit/generated_resources.h" |
| 14 #include "ui/base/l10n/l10n_util.h" |
| 15 |
| 16 namespace options { |
| 17 |
| 18 namespace { |
| 19 bool StringCompare(const std::string& a, const std::string& b) { |
| 20 return a.compare(b) < 0; |
| 21 } |
| 22 } // namespace |
| 23 |
| 24 LanguageDictionaryOverlayHandler::LanguageDictionaryOverlayHandler() |
| 25 : overlay_initialized_(false), |
| 26 dictionary_(NULL) { |
| 27 } |
| 28 |
| 29 LanguageDictionaryOverlayHandler::~LanguageDictionaryOverlayHandler() { |
| 30 } |
| 31 |
| 32 void LanguageDictionaryOverlayHandler::GetLocalizedValues( |
| 33 base::DictionaryValue* localized_strings) { |
| 34 RegisterTitle(localized_strings, |
| 35 "languageDictionaryOverlayPage", |
| 36 IDS_LANGUAGE_DICTIONARY_OVERLAY_TITLE); |
| 37 localized_strings->SetString( |
| 38 "languageDictionaryOverlayTitle", |
| 39 l10n_util::GetStringUTF16(IDS_LANGUAGE_DICTIONARY_OVERLAY_TITLE)); |
| 40 localized_strings->SetString( |
| 41 "languageDictionaryOverlayAddWordLabel", |
| 42 l10n_util::GetStringUTF16( |
| 43 IDS_LANGUAGE_DICTIONARY_OVERLAY_ADD_WORD_LABEL)); |
| 44 } |
| 45 |
| 46 void LanguageDictionaryOverlayHandler::InitializeHandler() { |
| 47 dictionary_ = SpellcheckServiceFactory::GetForProfile( |
| 48 Profile::FromWebUI(web_ui()))->GetCustomDictionary(); |
| 49 dictionary_->AddObserver(this); |
| 50 } |
| 51 |
| 52 void LanguageDictionaryOverlayHandler::InitializePage() { |
| 53 } |
| 54 |
| 55 void LanguageDictionaryOverlayHandler::RegisterMessages() { |
| 56 web_ui()->RegisterMessageCallback( |
| 57 "addDictionaryWord", |
| 58 base::Bind(&LanguageDictionaryOverlayHandler::AddWord, |
| 59 base::Unretained(this))); |
| 60 web_ui()->RegisterMessageCallback( |
| 61 "removeDictionaryWord", |
| 62 base::Bind(&LanguageDictionaryOverlayHandler::RemoveWord, |
| 63 base::Unretained(this))); |
| 64 web_ui()->RegisterMessageCallback( |
| 65 "refreshDictionaryWords", |
| 66 base::Bind(&LanguageDictionaryOverlayHandler::RefreshWords, |
| 67 base::Unretained(this))); |
| 68 } |
| 69 |
| 70 void LanguageDictionaryOverlayHandler::Uninitialize() { |
| 71 overlay_initialized_ = false; |
| 72 if (dictionary_) |
| 73 dictionary_->RemoveObserver(this); |
| 74 } |
| 75 |
| 76 void LanguageDictionaryOverlayHandler::OnCustomDictionaryLoaded() { |
| 77 ResetDataModel(); |
| 78 UpdateWordList(); |
| 79 } |
| 80 |
| 81 void LanguageDictionaryOverlayHandler::OnCustomDictionaryWordAdded( |
| 82 const std::string& word) { |
| 83 } |
| 84 |
| 85 void LanguageDictionaryOverlayHandler::OnCustomDictionaryWordRemoved( |
| 86 const std::string& word) { |
| 87 } |
| 88 |
| 89 void LanguageDictionaryOverlayHandler::ResetDataModel() { |
| 90 // TODO(rouslan): Paginate dictionary words. |
| 91 data_model_ = dictionary_->GetWords(); |
| 92 sort(data_model_.begin(), data_model_.end(), StringCompare); |
| 93 } |
| 94 |
| 95 void LanguageDictionaryOverlayHandler::UpdateWordList() { |
| 96 if (!overlay_initialized_) |
| 97 return; |
| 98 ListValue list_value; |
| 99 list_value.AppendStrings(data_model_); |
| 100 web_ui()->CallJavascriptFunction("EditDictionaryOverlay.setWordList", |
| 101 list_value); |
| 102 } |
| 103 |
| 104 void LanguageDictionaryOverlayHandler::RefreshWords(const ListValue* args) { |
| 105 overlay_initialized_ = true; |
| 106 ResetDataModel(); |
| 107 UpdateWordList(); |
| 108 } |
| 109 |
| 110 void LanguageDictionaryOverlayHandler::AddWord(const ListValue* args) { |
| 111 std::string new_word; |
| 112 if (!args->GetString(0, &new_word) || new_word.empty()) { |
| 113 NOTREACHED(); |
| 114 return; |
| 115 } |
| 116 dictionary_->AddWord(new_word); |
| 117 data_model_.push_back(new_word); |
| 118 UpdateWordList(); |
| 119 } |
| 120 |
| 121 void LanguageDictionaryOverlayHandler::RemoveWord(const ListValue* args) { |
| 122 std::string index_string; |
| 123 if (!args->GetString(0, &index_string) || index_string.empty()) { |
| 124 NOTREACHED(); |
| 125 return; |
| 126 } |
| 127 std::stringstream ss(index_string); |
| 128 int i = -1; |
| 129 if ((ss >> i).fail() || i < 0 || i >= (int) data_model_.size()) { |
| 130 NOTREACHED(); |
| 131 return; |
| 132 } |
| 133 dictionary_->RemoveWord(data_model_.at(i)); |
| 134 data_model_.erase(data_model_.begin() + i); |
| 135 UpdateWordList(); |
| 136 } |
| 137 |
| 138 } // namespace options |
| OLD | NEW |