1 // Copyright (c) 2012 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.
5 #include "chrome/browser/ui/toolbar/encoding_menu_controller.h"
7 #include "base/i18n/rtl.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/app/chrome_command_ids.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/character_encoding.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/pref_names.h"
15 #include "grit/generated_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
18 const int EncodingMenuController::kValidEncodingIds
[] = {
21 IDC_ENCODING_ISO88591
,
22 IDC_ENCODING_WINDOWS1252
,
26 IDC_ENCODING_BIG5HKSCS
,
28 IDC_ENCODING_SHIFTJIS
,
29 IDC_ENCODING_ISO2022JP
,
32 IDC_ENCODING_ISO885915
,
33 IDC_ENCODING_MACINTOSH
,
34 IDC_ENCODING_ISO88592
,
35 IDC_ENCODING_WINDOWS1250
,
36 IDC_ENCODING_ISO88595
,
37 IDC_ENCODING_WINDOWS1251
,
40 IDC_ENCODING_ISO88597
,
41 IDC_ENCODING_WINDOWS1253
,
42 IDC_ENCODING_ISO88594
,
43 IDC_ENCODING_ISO885913
,
44 IDC_ENCODING_WINDOWS1257
,
45 IDC_ENCODING_ISO88593
,
46 IDC_ENCODING_ISO885910
,
47 IDC_ENCODING_ISO885914
,
48 IDC_ENCODING_ISO885916
,
49 IDC_ENCODING_WINDOWS1254
,
50 IDC_ENCODING_ISO88596
,
51 IDC_ENCODING_WINDOWS1256
,
52 IDC_ENCODING_ISO88598
,
53 IDC_ENCODING_WINDOWS1255
,
54 IDC_ENCODING_WINDOWS1258
,
55 IDC_ENCODING_ISO88598I
,
58 bool EncodingMenuController::DoesCommandBelongToEncodingMenu(int id
) {
59 if (id
== IDC_ENCODING_AUTO_DETECT
) {
63 for (size_t i
= 0; i
< arraysize(kValidEncodingIds
); ++i
) {
64 if (id
== kValidEncodingIds
[i
]) {
72 const int* EncodingMenuController::ValidGUIEncodingIDs() {
73 return kValidEncodingIds
;
76 int EncodingMenuController::NumValidGUIEncodingIDs() {
77 return arraysize(kValidEncodingIds
);
80 bool EncodingMenuController::IsItemChecked(
81 Profile
* browser_profile
,
82 const std::string
& current_tab_encoding
,
84 if (!DoesCommandBelongToEncodingMenu(item_id
))
87 std::string encoding
= current_tab_encoding
;
89 encoding
= browser_profile
->GetPrefs()->GetString(prefs::kDefaultCharset
);
91 if (item_id
== IDC_ENCODING_AUTO_DETECT
) {
92 return browser_profile
->GetPrefs()->GetBoolean(
93 prefs::kWebKitUsesUniversalDetector
);
96 if (!encoding
.empty()) {
98 CharacterEncoding::GetCanonicalEncodingNameByCommandId(item_id
);
104 void EncodingMenuController::GetEncodingMenuItems(Profile
* profile
,
105 EncodingMenuItemList
* menu_items
) {
108 EncodingMenuItem
separator(0, base::string16());
111 menu_items
->push_back(
112 EncodingMenuItem(IDC_ENCODING_AUTO_DETECT
,
113 l10n_util::GetStringUTF16(IDS_ENCODING_AUTO_DETECT
)));
114 menu_items
->push_back(separator
);
116 // Create current display encoding list.
117 const std::vector
<CharacterEncoding::EncodingInfo
>* encodings
;
119 // Build the list of encoding ids : It is made of the
120 // locale-dependent short list, the cache of recently selected
121 // encodings and other encodings.
122 encodings
= CharacterEncoding::GetCurrentDisplayEncodings(
123 g_browser_process
->GetApplicationLocale(),
124 profile
->GetPrefs()->GetString(prefs::kStaticEncodings
),
125 profile
->GetPrefs()->GetString(prefs::kRecentlySelectedEncoding
));
127 DCHECK(!encodings
->empty());
129 // Build up output list for menu.
130 std::vector
<CharacterEncoding::EncodingInfo
>::const_iterator it
;
131 for (it
= encodings
->begin(); it
!= encodings
->end(); ++it
) {
132 if (it
->encoding_id
) {
133 base::string16 encoding
= it
->encoding_display_name
;
134 base::i18n::AdjustStringForLocaleDirection(&encoding
);
135 menu_items
->push_back(EncodingMenuItem(it
->encoding_id
, encoding
));
137 menu_items
->push_back(separator
);