| Index: chrome/browser/ui/webui/options/language_dictionary_overlay_handler.cc
|
| diff --git a/chrome/browser/ui/webui/options/language_dictionary_overlay_handler.cc b/chrome/browser/ui/webui/options/language_dictionary_overlay_handler.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..b0cab23e9aff941040219b0b8a909da21a83db52
|
| --- /dev/null
|
| +++ b/chrome/browser/ui/webui/options/language_dictionary_overlay_handler.cc
|
| @@ -0,0 +1,116 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/ui/webui/options/language_dictionary_overlay_handler.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/utf_string_conversions.h"
|
| +#include "base/values.h"
|
| +#include "chrome/browser/profiles/profile.h"
|
| +#include "chrome/browser/spellchecker/spellcheck_factory.h"
|
| +#include "chrome/browser/spellchecker/spellcheck_service.h"
|
| +#include "chrome/common/url_constants.h"
|
| +#include "content/public/browser/web_ui.h"
|
| +#include "grit/generated_resources.h"
|
| +#include "grit/locale_settings.h"
|
| +#include "ui/base/l10n/l10n_util.h"
|
| +
|
| +namespace options {
|
| +namespace {
|
| +bool StringCompare(const std::string& a, const std::string& b) {
|
| + return a.compare(b) < 0;
|
| +}
|
| +} // namespace
|
| +
|
| +LanguageDictionaryOverlayHandler::LanguageDictionaryOverlayHandler()
|
| + : overlay_initialized_(false),
|
| + dictionary_(NULL) {
|
| +}
|
| +
|
| +LanguageDictionaryOverlayHandler::~LanguageDictionaryOverlayHandler() {
|
| +}
|
| +
|
| +void LanguageDictionaryOverlayHandler::GetLocalizedValues(
|
| + base::DictionaryValue* localized_strings) {
|
| + RegisterTitle(localized_strings,
|
| + "languageDictionaryOverlayPage",
|
| + IDS_LANGUAGE_DICTIONARY_OVERLAY_TITLE);
|
| + localized_strings->SetString(
|
| + "languageDictionaryOverlayTitle",
|
| + l10n_util::GetStringUTF16(IDS_LANGUAGE_DICTIONARY_OVERLAY_TITLE));
|
| + localized_strings->SetString(
|
| + "languageDictionaryOverlayAddWordLabel",
|
| + l10n_util::GetStringUTF16(
|
| + IDS_LANGUAGE_DICTIONARY_OVERLAY_ADD_WORD_LABEL));
|
| +}
|
| +
|
| +void LanguageDictionaryOverlayHandler::InitializeHandler() {
|
| + dictionary_ = SpellcheckServiceFactory::GetForProfile(
|
| + Profile::FromWebUI(web_ui()))->GetCustomDictionary();
|
| + dictionary_->AddObserver(this);
|
| +}
|
| +
|
| +void LanguageDictionaryOverlayHandler::InitializePage() {
|
| +}
|
| +
|
| +void LanguageDictionaryOverlayHandler::RegisterMessages() {
|
| + web_ui()->RegisterMessageCallback(
|
| + "editDictionaryWord",
|
| + base::Bind(&LanguageDictionaryOverlayHandler::EditWord,
|
| + base::Unretained(this)));
|
| + web_ui()->RegisterMessageCallback(
|
| + "refreshDictionaryWords",
|
| + base::Bind(&LanguageDictionaryOverlayHandler::RefreshWords,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void LanguageDictionaryOverlayHandler::Uninitialize() {
|
| + overlay_initialized_ = false;
|
| + dictionary_->RemoveObserver(this);
|
| +}
|
| +
|
| +void LanguageDictionaryOverlayHandler::OnCustomDictionaryLoaded() {
|
| + UpdateWordList();
|
| +}
|
| +
|
| +void LanguageDictionaryOverlayHandler::OnCustomDictionaryWordAdded(
|
| + const std::string& word) {
|
| + UpdateWordList();
|
| +}
|
| +
|
| +void LanguageDictionaryOverlayHandler::OnCustomDictionaryWordRemoved(
|
| + const std::string& word) {
|
| + UpdateWordList();
|
| +}
|
| +
|
| +void LanguageDictionaryOverlayHandler::UpdateWordList() {
|
| + if (!overlay_initialized_)
|
| + return;
|
| + chrome::spellcheck_common::WordList words = dictionary_->GetWords();
|
| + sort(words.begin(), words.end(), StringCompare);
|
| + ListValue word_list;
|
| + word_list.AppendStrings(words);
|
| + web_ui()->CallJavascriptFunction("EditDictionaryOverlay.setWordList",
|
| + word_list);
|
| +}
|
| +
|
| +void LanguageDictionaryOverlayHandler::RefreshWords(const ListValue* args) {
|
| + overlay_initialized_ = true;
|
| + UpdateWordList();
|
| +}
|
| +
|
| +void LanguageDictionaryOverlayHandler::EditWord(const ListValue* args) {
|
| + std::string old_word;
|
| + std::string new_word;
|
| + if (!args->GetString(0, &old_word) || !args->GetString(1, &new_word)) {
|
| + NOTREACHED();
|
| + return;
|
| + }
|
| + if (!old_word.empty())
|
| + dictionary_->RemoveWord(old_word);
|
| + if (!new_word.empty())
|
| + dictionary_->AddWord(new_word);
|
| +}
|
| +
|
| +} // namespace options
|
|
|