summaryrefslogtreecommitdiffstats
path: root/chrome/browser/default_encoding_combo_model.cc
blob: f9007139a6b5ed225c976c7948b6b30e6547ad97 (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
// 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/default_encoding_combo_model.h"

#include "app/l10n_util.h"
#include "app/l10n_util_collator.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/prefs/pref_member.h"
#include "chrome/browser/profile.h"
#include "chrome/common/pref_names.h"

DefaultEncodingComboboxModel::DefaultEncodingComboboxModel() {
  // Initialize the vector of all sorted encodings according to current
  // UI locale.
  std::string locale = g_browser_process->GetApplicationLocale();
  int size = CharacterEncoding::GetSupportCanonicalEncodingCount();
  for (int i = 0; i < size; ++i) {
    sorted_encoding_list_.push_back(CharacterEncoding::EncodingInfo(
        CharacterEncoding::GetEncodingCommandIdByIndex(i)));
  }
  l10n_util::SortVectorWithStringKey(locale, &sorted_encoding_list_, true);
}

DefaultEncodingComboboxModel::~DefaultEncodingComboboxModel() {
}

int DefaultEncodingComboboxModel::GetItemCount() {
  return static_cast<int>(sorted_encoding_list_.size());
}

string16 DefaultEncodingComboboxModel::GetItemAt(int index) {
  DCHECK(index >= 0 && index < GetItemCount());
  return WideToUTF16Hack(sorted_encoding_list_[index].encoding_display_name);
}

std::string DefaultEncodingComboboxModel::GetEncodingCharsetByIndex(int index) {
  DCHECK(index >= 0 && index < GetItemCount());
  int encoding_id = sorted_encoding_list_[index].encoding_id;
  return CharacterEncoding::GetCanonicalEncodingNameByCommandId(encoding_id);
}

int DefaultEncodingComboboxModel::GetSelectedEncodingIndex(Profile* profile) {
  StringPrefMember current_encoding_string;
  current_encoding_string.Init(prefs::kDefaultCharset,
                               profile->GetPrefs(),
                               NULL);
  const std::string current_encoding = current_encoding_string.GetValue();
  for (int i = 0; i < GetItemCount(); ++i) {
    if (GetEncodingCharsetByIndex(i) == current_encoding)
      return i;
  }

  return 0;
}