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/default_encoding_combo_model.h" 6 7 #include "base/utf_string_conversions.h" 8 #include "chrome/browser/browser_process.h" 9 #include "chrome/browser/prefs/pref_member.h" 10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/common/pref_names.h" 12 #include "ui/base/l10n/l10n_util.h" 13 #include "ui/base/l10n/l10n_util_collator.h" 14 DefaultEncodingComboboxModel()15DefaultEncodingComboboxModel::DefaultEncodingComboboxModel() { 16 // Initialize the vector of all sorted encodings according to current 17 // UI locale. 18 std::string locale = g_browser_process->GetApplicationLocale(); 19 int size = CharacterEncoding::GetSupportCanonicalEncodingCount(); 20 for (int i = 0; i < size; ++i) { 21 sorted_encoding_list_.push_back(CharacterEncoding::EncodingInfo( 22 CharacterEncoding::GetEncodingCommandIdByIndex(i))); 23 } 24 l10n_util::SortVectorWithStringKey(locale, &sorted_encoding_list_, true); 25 } 26 ~DefaultEncodingComboboxModel()27DefaultEncodingComboboxModel::~DefaultEncodingComboboxModel() { 28 } 29 GetItemCount()30int DefaultEncodingComboboxModel::GetItemCount() { 31 return static_cast<int>(sorted_encoding_list_.size()); 32 } 33 GetItemAt(int index)34string16 DefaultEncodingComboboxModel::GetItemAt(int index) { 35 DCHECK(index >= 0 && index < GetItemCount()); 36 return sorted_encoding_list_[index].encoding_display_name; 37 } 38 GetEncodingCharsetByIndex(int index)39std::string DefaultEncodingComboboxModel::GetEncodingCharsetByIndex(int index) { 40 DCHECK(index >= 0 && index < GetItemCount()); 41 int encoding_id = sorted_encoding_list_[index].encoding_id; 42 return CharacterEncoding::GetCanonicalEncodingNameByCommandId(encoding_id); 43 } 44 GetSelectedEncodingIndex(Profile * profile)45int DefaultEncodingComboboxModel::GetSelectedEncodingIndex(Profile* profile) { 46 StringPrefMember current_encoding_string; 47 current_encoding_string.Init(prefs::kDefaultCharset, 48 profile->GetPrefs(), 49 NULL); 50 const std::string current_encoding = current_encoding_string.GetValue(); 51 for (int i = 0; i < GetItemCount(); ++i) { 52 if (GetEncodingCharsetByIndex(i) == current_encoding) 53 return i; 54 } 55 56 return 0; 57 } 58