summaryrefslogtreecommitdiffstats
path: root/chrome/browser/language_order_table_model.cc
blob: 7f4c42ad381e283f2e503195bd3ee2f0984b16a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) 2010 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 <set>

#include "base/string_split.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/table_model_observer.h"

LanguageOrderTableModel::LanguageOrderTableModel()
    : observer_(NULL) {
}

LanguageOrderTableModel::~LanguageOrderTableModel() {}

void LanguageOrderTableModel::SetAcceptLanguagesString(
    const std::string& language_list) {
  std::vector<std::string> languages_vector;
  base::SplitString(language_list, ',', &languages_vector);
  languages_.clear();
  std::set<std::string> added;
  for (int i = 0; i < static_cast<int>(languages_vector.size()); i++) {
    const std::string& language(languages_vector.at(i));
    if (!language.empty() && added.count(language) == 0) {
      languages_.push_back(language);
      added.insert(language);
    }
  }
  if (observer_)
    observer_->OnModelChanged();
}

void LanguageOrderTableModel::SetObserver(ui::TableModelObserver* observer) {
  observer_ = observer;
}

string16 LanguageOrderTableModel::GetText(int row, int column_id) {
  DCHECK(row >= 0 && row < RowCount());
  const std::string app_locale = g_browser_process->GetApplicationLocale();
  return l10n_util::GetDisplayNameForLocale(languages_.at(row),
                                            app_locale,
                                            true);
}

bool LanguageOrderTableModel::Add(const std::string& language) {
  if (language.empty())
    return false;
  // Check for selecting duplicated language.
  for (std::vector<std::string>::const_iterator cit = languages_.begin();
       cit != languages_.end(); ++cit)
    if (*cit == language)
      return false;
  languages_.push_back(language);
  if (observer_)
    observer_->OnItemsAdded(RowCount() - 1, 1);
  return true;
}

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());
}

std::string LanguageOrderTableModel::GetLanguageList() {
  return JoinString(languages_, ',');
}

int LanguageOrderTableModel::RowCount() {
  return static_cast<int>(languages_.size());
}