diff options
author | mhm@chromium.org <mhm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-28 01:31:55 +0000 |
---|---|---|
committer | mhm@chromium.org <mhm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-28 01:31:55 +0000 |
commit | 736388d4ce0e8b9fee9ae9ac58d50027d290539b (patch) | |
tree | d84002565730958c62d71f69cd11b2efda2a79f1 /chrome/browser/language_order_table_model.cc | |
parent | 1eaabcb4a8bf38a1ed8b260efe019d91f33dbeae (diff) | |
download | chromium_src-736388d4ce0e8b9fee9ae9ac58d50027d290539b.zip chromium_src-736388d4ce0e8b9fee9ae9ac58d50027d290539b.tar.gz chromium_src-736388d4ce0e8b9fee9ae9ac58d50027d290539b.tar.bz2 |
Refactor out language models for linux gtk views preparation
There are models and an language array that are needed for the linux gtk views afaik. So extracting the classes from the windows specific code into separate files would allow code reusabiliy.
BUG=13524
TEST=Windows Language Dialog functions properly.
Review URL: http://codereview.chromium.org/151138
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21787 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/language_order_table_model.cc')
-rw-r--r-- | chrome/browser/language_order_table_model.cc | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/chrome/browser/language_order_table_model.cc b/chrome/browser/language_order_table_model.cc new file mode 100644 index 0000000..50799d2 --- /dev/null +++ b/chrome/browser/language_order_table_model.cc @@ -0,0 +1,112 @@ +// Copyright (c) 2009 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/language_order_table_model.h" + +#include "app/l10n_util.h" +#include "chrome/browser/browser_process.h" + +LanguageOrderTableModel::LanguageOrderTableModel() + : observer_(NULL) { +} + +void LanguageOrderTableModel::SetAcceptLanguagesString( + const std::string& language_list) { + std::vector<std::string> languages_vector; + ListToVector(language_list, &languages_vector); + for (int i = 0; i < static_cast<int>(languages_vector.size()); i++) { + Add(languages_vector.at(i)); + } +} + +void LanguageOrderTableModel::SetObserver(TableModelObserver* observer) { + observer_ = observer; +} + +std::wstring LanguageOrderTableModel::GetText(int row, int column_id) { + DCHECK(row >= 0 && row < RowCount()); + const std::string app_locale = g_browser_process->GetApplicationLocale(); + return UTF16ToWide(l10n_util::GetDisplayNameForLocale(languages_.at(row), + app_locale, + true)); +} + +void LanguageOrderTableModel::Add(const std::string& language) { + if (language.empty()) + return; + // Check for selecting duplicated language. + for (std::vector<std::string>::const_iterator cit = languages_.begin(); + cit != languages_.end(); ++cit) + if (*cit == language) + return; + languages_.push_back(language); + if (observer_) + observer_->OnItemsAdded(RowCount() - 1, 1); +} + +void LanguageOrderTableModel::Remove(int index) { + DCHECK(index >= 0 && index < RowCount()); + languages_.erase(languages_.begin() + index); + if (observer_) + observer_->OnItemsRemoved(index, 1); +} + +int LanguageOrderTableModel::GetIndex(const std::string& language) { + if (language.empty()) + return -1; + + int index = 0; + for (std::vector<std::string>::const_iterator cit = languages_.begin(); + cit != languages_.end(); ++cit) { + if (*cit == language) + return index; + + index++; + } + + return -1; +} + +void LanguageOrderTableModel::MoveDown(int index) { + if (index < 0 || index >= RowCount() - 1) + return; + std::string item = languages_.at(index); + languages_.erase(languages_.begin() + index); + if (index == RowCount() - 1) + languages_.push_back(item); + else + languages_.insert(languages_.begin() + index + 1, item); + if (observer_) + observer_->OnItemsChanged(0, RowCount()); +} + +void LanguageOrderTableModel::MoveUp(int index) { + if (index <= 0 || index >= static_cast<int>(languages_.size())) + return; + std::string item = languages_.at(index); + languages_.erase(languages_.begin() + index); + languages_.insert(languages_.begin() + index - 1, item); + if (observer_) + observer_->OnItemsChanged(0, RowCount()); +} + +int LanguageOrderTableModel::RowCount() { + return static_cast<int>(languages_.size()); +} + +void LanguageOrderTableModel::ListToVector(const std::string& list, + std::vector<std::string>* vector) { + SplitString(list, ',', vector); +} + +std::string LanguageOrderTableModel::VectorToList( + const std::vector<std::string>& vector) { + std::string list; + for (int i = 0 ; i < static_cast<int>(vector.size()) ; i++) { + list += vector.at(i); + if (i != static_cast<int>(vector.size()) - 1) + list += ','; + } + return list; +} |