OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/ui/toolbar/encoding_menu_controller.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include "base/i18n/rtl.h" | |
10 #include "base/macros.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "chrome/app/chrome_command_ids.h" | |
13 #include "chrome/browser/browser_process.h" | |
14 #include "chrome/browser/character_encoding.h" | |
15 #include "chrome/browser/profiles/profile.h" | |
16 #include "chrome/common/pref_names.h" | |
17 #include "chrome/grit/generated_resources.h" | |
18 #include "components/prefs/pref_service.h" | |
19 #include "ui/base/l10n/l10n_util.h" | |
20 | |
21 const int EncodingMenuController::kValidEncodingIds[] = { | |
22 IDC_ENCODING_UTF8, | |
23 IDC_ENCODING_UTF16LE, | |
24 IDC_ENCODING_WINDOWS1252, | |
25 IDC_ENCODING_GBK, | |
26 IDC_ENCODING_GB18030, | |
27 IDC_ENCODING_BIG5, | |
28 IDC_ENCODING_KOREAN, | |
29 IDC_ENCODING_SHIFTJIS, | |
30 IDC_ENCODING_ISO2022JP, | |
31 IDC_ENCODING_EUCJP, | |
32 IDC_ENCODING_THAI, | |
33 IDC_ENCODING_ISO885915, | |
34 IDC_ENCODING_MACINTOSH, | |
35 IDC_ENCODING_ISO88592, | |
36 IDC_ENCODING_WINDOWS1250, | |
37 IDC_ENCODING_ISO88595, | |
38 IDC_ENCODING_WINDOWS1251, | |
39 IDC_ENCODING_KOI8R, | |
40 IDC_ENCODING_KOI8U, | |
41 IDC_ENCODING_ISO88597, | |
42 IDC_ENCODING_WINDOWS1253, | |
43 IDC_ENCODING_ISO88594, | |
44 IDC_ENCODING_ISO885913, | |
45 IDC_ENCODING_WINDOWS1257, | |
46 IDC_ENCODING_ISO88593, | |
47 IDC_ENCODING_ISO885910, | |
48 IDC_ENCODING_ISO885914, | |
49 IDC_ENCODING_ISO885916, | |
50 IDC_ENCODING_WINDOWS1254, | |
51 IDC_ENCODING_ISO88596, | |
52 IDC_ENCODING_WINDOWS1256, | |
53 IDC_ENCODING_ISO88598, | |
54 IDC_ENCODING_WINDOWS1255, | |
55 IDC_ENCODING_WINDOWS1258, | |
56 IDC_ENCODING_ISO88598I, | |
57 IDC_ENCODING_IBM866, | |
58 }; | |
59 | |
60 bool EncodingMenuController::DoesCommandBelongToEncodingMenu(int id) { | |
61 if (id == IDC_ENCODING_AUTO_DETECT) { | |
62 return true; | |
63 } | |
64 | |
65 for (size_t i = 0; i < arraysize(kValidEncodingIds); ++i) { | |
66 if (id == kValidEncodingIds[i]) { | |
67 return true; | |
68 } | |
69 } | |
70 | |
71 return false; | |
72 } | |
73 | |
74 const int* EncodingMenuController::ValidGUIEncodingIDs() { | |
75 return kValidEncodingIds; | |
76 } | |
77 | |
78 int EncodingMenuController::NumValidGUIEncodingIDs() { | |
79 return arraysize(kValidEncodingIds); | |
80 } | |
81 | |
82 bool EncodingMenuController::IsItemChecked( | |
83 Profile* browser_profile, | |
84 const std::string& current_tab_encoding, | |
85 int item_id) { | |
86 if (!DoesCommandBelongToEncodingMenu(item_id)) | |
87 return false; | |
88 | |
89 std::string encoding = current_tab_encoding; | |
90 if (encoding.empty()) | |
91 encoding = browser_profile->GetPrefs()->GetString(prefs::kDefaultCharset); | |
92 | |
93 if (item_id == IDC_ENCODING_AUTO_DETECT) { | |
94 return browser_profile->GetPrefs()->GetBoolean( | |
95 prefs::kWebKitUsesUniversalDetector); | |
96 } | |
97 | |
98 if (!encoding.empty()) { | |
99 return encoding == | |
100 CharacterEncoding::GetCanonicalEncodingNameByCommandId(item_id); | |
101 } | |
102 | |
103 return false; | |
104 } | |
105 | |
106 void EncodingMenuController::GetEncodingMenuItems(Profile* profile, | |
107 EncodingMenuItemList* menu_items) { | |
108 | |
109 DCHECK(menu_items); | |
110 EncodingMenuItem separator(0, base::string16()); | |
111 | |
112 menu_items->clear(); | |
113 menu_items->push_back( | |
114 EncodingMenuItem(IDC_ENCODING_AUTO_DETECT, | |
115 l10n_util::GetStringUTF16(IDS_ENCODING_AUTO_DETECT))); | |
116 menu_items->push_back(separator); | |
117 | |
118 // Create current display encoding list. | |
119 const std::vector<CharacterEncoding::EncodingInfo>* encodings; | |
120 | |
121 // Build the list of encoding ids : It is made of the | |
122 // locale-dependent short list, the cache of recently selected | |
123 // encodings and other encodings. | |
124 encodings = CharacterEncoding::GetCurrentDisplayEncodings( | |
125 g_browser_process->GetApplicationLocale(), | |
126 profile->GetPrefs()->GetString(prefs::kStaticEncodings), | |
127 profile->GetPrefs()->GetString(prefs::kRecentlySelectedEncoding)); | |
128 DCHECK(encodings); | |
129 DCHECK(!encodings->empty()); | |
130 | |
131 // Build up output list for menu. | |
132 std::vector<CharacterEncoding::EncodingInfo>::const_iterator it; | |
133 for (it = encodings->begin(); it != encodings->end(); ++it) { | |
134 if (it->encoding_id) { | |
135 base::string16 encoding = it->encoding_display_name; | |
136 base::i18n::AdjustStringForLocaleDirection(&encoding); | |
137 menu_items->push_back(EncodingMenuItem(it->encoding_id, encoding)); | |
138 } else { | |
139 menu_items->push_back(separator); | |
140 } | |
141 } | |
142 } | |
OLD | NEW |