Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2010 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/language_order_table_model.h"
      6 
      7 #include <set>
      8 
      9 #include "base/string_split.h"
     10 #include "base/utf_string_conversions.h"
     11 #include "chrome/browser/browser_process.h"
     12 #include "ui/base/l10n/l10n_util.h"
     13 #include "ui/base/models/table_model_observer.h"
     14 
     15 LanguageOrderTableModel::LanguageOrderTableModel()
     16     : observer_(NULL) {
     17 }
     18 
     19 LanguageOrderTableModel::~LanguageOrderTableModel() {}
     20 
     21 void LanguageOrderTableModel::SetAcceptLanguagesString(
     22     const std::string& language_list) {
     23   std::vector<std::string> languages_vector;
     24   base::SplitString(language_list, ',', &languages_vector);
     25   languages_.clear();
     26   std::set<std::string> added;
     27   for (int i = 0; i < static_cast<int>(languages_vector.size()); i++) {
     28     const std::string& language(languages_vector.at(i));
     29     if (!language.empty() && added.count(language) == 0) {
     30       languages_.push_back(language);
     31       added.insert(language);
     32     }
     33   }
     34   if (observer_)
     35     observer_->OnModelChanged();
     36 }
     37 
     38 void LanguageOrderTableModel::SetObserver(ui::TableModelObserver* observer) {
     39   observer_ = observer;
     40 }
     41 
     42 string16 LanguageOrderTableModel::GetText(int row, int column_id) {
     43   DCHECK(row >= 0 && row < RowCount());
     44   const std::string app_locale = g_browser_process->GetApplicationLocale();
     45   return l10n_util::GetDisplayNameForLocale(languages_.at(row),
     46                                             app_locale,
     47                                             true);
     48 }
     49 
     50 bool LanguageOrderTableModel::Add(const std::string& language) {
     51   if (language.empty())
     52     return false;
     53   // Check for selecting duplicated language.
     54   for (std::vector<std::string>::const_iterator cit = languages_.begin();
     55        cit != languages_.end(); ++cit)
     56     if (*cit == language)
     57       return false;
     58   languages_.push_back(language);
     59   if (observer_)
     60     observer_->OnItemsAdded(RowCount() - 1, 1);
     61   return true;
     62 }
     63 
     64 void LanguageOrderTableModel::Remove(int index) {
     65   DCHECK(index >= 0 && index < RowCount());
     66   languages_.erase(languages_.begin() + index);
     67   if (observer_)
     68     observer_->OnItemsRemoved(index, 1);
     69 }
     70 
     71 int LanguageOrderTableModel::GetIndex(const std::string& language) {
     72   if (language.empty())
     73     return -1;
     74 
     75   int index = 0;
     76   for (std::vector<std::string>::const_iterator cit = languages_.begin();
     77       cit != languages_.end(); ++cit) {
     78     if (*cit == language)
     79       return index;
     80 
     81     index++;
     82   }
     83 
     84   return -1;
     85 }
     86 
     87 void LanguageOrderTableModel::MoveDown(int index) {
     88   if (index < 0 || index >= RowCount() - 1)
     89     return;
     90   std::string item = languages_.at(index);
     91   languages_.erase(languages_.begin() + index);
     92   if (index == RowCount() - 1)
     93     languages_.push_back(item);
     94   else
     95     languages_.insert(languages_.begin() + index + 1, item);
     96   if (observer_)
     97     observer_->OnItemsChanged(0, RowCount());
     98 }
     99 
    100 void LanguageOrderTableModel::MoveUp(int index) {
    101   if (index <= 0 || index >= static_cast<int>(languages_.size()))
    102     return;
    103   std::string item = languages_.at(index);
    104   languages_.erase(languages_.begin() + index);
    105   languages_.insert(languages_.begin() + index - 1, item);
    106   if (observer_)
    107     observer_->OnItemsChanged(0, RowCount());
    108 }
    109 
    110 std::string LanguageOrderTableModel::GetLanguageList() {
    111   return JoinString(languages_, ',');
    112 }
    113 
    114 int LanguageOrderTableModel::RowCount() {
    115   return static_cast<int>(languages_.size());
    116 }
    117