diff options
author | jnd@chromium.org <jnd@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-20 04:51:28 +0000 |
---|---|---|
committer | jnd@chromium.org <jnd@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-20 04:51:28 +0000 |
commit | f8fae18a398d4925bd59e80e81890c12195146cc (patch) | |
tree | 3e51f6555f284d7c532d8b8d70e00c483f63675d /chrome/browser/character_encoding.cc | |
parent | 0cf8d378a8e819083dd38b38d4e5d2e2375b2497 (diff) | |
download | chromium_src-f8fae18a398d4925bd59e80e81890c12195146cc.zip chromium_src-f8fae18a398d4925bd59e80e81890c12195146cc.tar.gz chromium_src-f8fae18a398d4925bd59e80e81890c12195146cc.tar.bz2 |
We will sort the encoding menu items according to current used UI language except the UTF-8 encoding and the recently selectd encodings.
For details, see Fix bug http://code.google.com/p/chromium/issues/detail?id=7647
BUG=7647
Review URL: http://codereview.chromium.org/21414
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10077 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/character_encoding.cc')
-rw-r--r-- | chrome/browser/character_encoding.cc | 104 |
1 files changed, 67 insertions, 37 deletions
diff --git a/chrome/browser/character_encoding.cc b/chrome/browser/character_encoding.cc index afcbbcd..2be87ae 100644 --- a/chrome/browser/character_encoding.cc +++ b/chrome/browser/character_encoding.cc @@ -40,8 +40,8 @@ static CanonicalEncodingData canonical_encoding_names[] = { { IDC_ENCODING_BIG5HKSCS, L"Big5-HKSCS", IDS_ENCODING_TRAD_CHINESE }, { IDC_ENCODING_KOREAN, L"windows-949", IDS_ENCODING_KOREAN }, { IDC_ENCODING_SHIFTJIS, L"Shift_JIS", IDS_ENCODING_JAPANESE }, - { IDC_ENCODING_ISO2022JP, L"ISO-2022-JP", IDS_ENCODING_JAPANESE }, { IDC_ENCODING_EUCJP, L"EUC-JP", IDS_ENCODING_JAPANESE }, + { IDC_ENCODING_ISO2022JP, L"ISO-2022-JP", IDS_ENCODING_JAPANESE }, { IDC_ENCODING_THAI, L"windows-874", IDS_ENCODING_THAI }, { IDC_ENCODING_ISO885915, L"ISO-8859-15", IDS_ENCODING_WESTERN }, { IDC_ENCODING_MACINTOSH, L"macintosh", IDS_ENCODING_WESTERN }, @@ -71,7 +71,8 @@ static CanonicalEncodingData canonical_encoding_names[] = { static const int canonical_encoding_names_length = arraysize(canonical_encoding_names); -typedef std::map<int, std::pair<const wchar_t*, int> > IdToCanonicalEncodingNameMapType; +typedef std::map<int, std::pair<const wchar_t*, int> > + IdToCanonicalEncodingNameMapType; typedef std::map<const std::wstring, int> CanonicalEncodingNameToIdMapType; class CanonicalEncodingMap { @@ -85,15 +86,15 @@ class CanonicalEncodingMap { return &locale_dependent_encoding_ids_; } - std::vector<int>* const current_display_encoding_ids() { - return ¤t_display_encoding_ids_; + std::vector<CharacterEncoding::EncodingInfo>* const current_display_encodings() { + return ¤t_display_encodings_; } private: scoped_ptr<IdToCanonicalEncodingNameMapType> id_to_encoding_name_map_; scoped_ptr<CanonicalEncodingNameToIdMapType> encoding_name_to_id_map_; std::vector<int> locale_dependent_encoding_ids_; - std::vector<int> current_display_encoding_ids_; + std::vector<CharacterEncoding::EncodingInfo> current_display_encodings_; DISALLOW_EVIL_CONSTRUCTORS(CanonicalEncodingMap); }; @@ -131,33 +132,25 @@ static CanonicalEncodingMap canonical_encoding_name_map_singleton; const int default_encoding_menus[] = { IDC_ENCODING_UTF16LE, - 0, IDC_ENCODING_ISO88591, IDC_ENCODING_WINDOWS1252, - 0, IDC_ENCODING_GBK, IDC_ENCODING_GB18030, IDC_ENCODING_BIG5, IDC_ENCODING_BIG5HKSCS, - 0, IDC_ENCODING_KOREAN, - 0, IDC_ENCODING_SHIFTJIS, - IDC_ENCODING_ISO2022JP, IDC_ENCODING_EUCJP, - 0, + IDC_ENCODING_ISO2022JP, IDC_ENCODING_THAI, - 0, IDC_ENCODING_ISO885915, IDC_ENCODING_MACINTOSH, IDC_ENCODING_ISO88592, IDC_ENCODING_WINDOWS1250, - 0, IDC_ENCODING_ISO88595, IDC_ENCODING_WINDOWS1251, IDC_ENCODING_KOI8R, IDC_ENCODING_KOI8U, - 0, IDC_ENCODING_ISO88597, IDC_ENCODING_WINDOWS1253, IDC_ENCODING_WINDOWS1254, @@ -166,7 +159,6 @@ const int default_encoding_menus[] = { IDC_ENCODING_ISO88598, IDC_ENCODING_WINDOWS1255, IDC_ENCODING_WINDOWS1258, - IDC_ENCODING_ISO88594, IDC_ENCODING_ISO885913, IDC_ENCODING_WINDOWS1257, @@ -182,7 +174,7 @@ const int default_encoding_menus_length = arraysize(default_encoding_menus); // comma, get available encoding ids and save them to |available_list|. // The parameter |maximum_size| indicates maximum size of encoding items we // want to get from the |encoding_list|. -static void ParseEncodingListSeparatedWithComma( +void ParseEncodingListSeparatedWithComma( const std::wstring& encoding_list, std::vector<int>* const available_list, size_t maximum_size) { WStringTokenizer tokenizer(encoding_list, L","); @@ -211,8 +203,33 @@ std::wstring GetEncodingDisplayName(std::wstring encoding_name, return category_name; } +int GetEncodingCategoryStringIdByCommandId(int id) { + const IdToCanonicalEncodingNameMapType* map = + canonical_encoding_name_map_singleton. + GetIdToCanonicalEncodingNameMapData(); + DCHECK(map); + + IdToCanonicalEncodingNameMapType::const_iterator found_name = map->find(id); + if (found_name != map->end()) + return found_name->second.second; + return 0; +} + +std::wstring GetEncodingCategoryStringByCommandId(int id) { + int category_id = GetEncodingCategoryStringIdByCommandId(id); + if (category_id) + return l10n_util::GetString(category_id); + return std::wstring(); +} + } // namespace +CharacterEncoding::EncodingInfo::EncodingInfo(int id) + : encoding_id(id) { + encoding_category_name = GetEncodingCategoryStringByCommandId(id), + encoding_display_name = GetCanonicalEncodingDisplayNameByCommandId(id); +} + // Static. int CharacterEncoding::GetCommandIdByCanonicalEncodingName( const std::wstring& encoding_name) { @@ -314,13 +331,14 @@ std::wstring CharacterEncoding::GetCanonicalEncodingNameByAliasName( // FireFox, we always put UTF-8 as toppest position, after then put user // recently selected encodings, then put local dependent encoding items. // At last, we put all rest encoding items. -const std::vector<int>* CharacterEncoding::GetCurrentDisplayEncodings( +const std::vector<CharacterEncoding::EncodingInfo>* CharacterEncoding::GetCurrentDisplayEncodings( + const std::wstring& locale, const std::wstring& locale_encodings, const std::wstring& recently_select_encodings) { std::vector<int>* const locale_dependent_encoding_list = canonical_encoding_name_map_singleton.locale_dependent_encoding_ids(); - std::vector<int>* const encoding_list = - canonical_encoding_name_map_singleton.current_display_encoding_ids(); + std::vector<CharacterEncoding::EncodingInfo>* const encoding_list = + canonical_encoding_name_map_singleton.current_display_encodings(); // Initialize locale dependent static encoding list. if (locale_dependent_encoding_list->empty() && !locale_encodings.empty()) @@ -337,7 +355,7 @@ const std::vector<int>* CharacterEncoding::GetCurrentDisplayEncodings( // Clear old encoding list since user recently selected encodings changed. encoding_list->clear(); // Always add UTF-8 to first encoding position. - encoding_list->push_back(IDC_ENCODING_UTF8); + encoding_list->push_back(EncodingInfo(IDC_ENCODING_UTF8)); std::set<int> inserted_encoding; inserted_encoding.insert(IDC_ENCODING_UTF8); @@ -352,8 +370,7 @@ const std::vector<int>* CharacterEncoding::GetCurrentDisplayEncodings( recently_select_encoding_list.insert(recently_select_encoding_list.begin(), locale_dependent_encoding_list->begin(), locale_dependent_encoding_list->end()); - std::vector<int>::const_iterator it; - for (it = recently_select_encoding_list.begin(); + for (std::vector<int>::iterator it = recently_select_encoding_list.begin(); it != recently_select_encoding_list.end(); ++it) { // Test whether we have met this encoding id. bool ok = inserted_encoding.insert(*it).second; @@ -361,27 +378,40 @@ const std::vector<int>* CharacterEncoding::GetCurrentDisplayEncodings( // happened, but just in case some one manually edit preference file. if (!ok) continue; - encoding_list->push_back(*it); + encoding_list->push_back(EncodingInfo(*it)); } // Append a separator; - encoding_list->push_back(0); + encoding_list->push_back(EncodingInfo(0)); + + // We need to keep "Unicode (UTF-16LE)" always at the top (among the rest + // of encodings) instead of being sorted along with other encodings. So if + // "Unicode (UTF-16LE)" is already in previous encodings, sort the rest + // of encodings. Otherwise Put "Unicode (UTF-16LE)" on the first of the + // rest of encodings, skip "Unicode (UTF-16LE)" and sort all left encodings. + int start_sorted_index = encoding_list->size(); + if (inserted_encoding.find(IDC_ENCODING_UTF16LE) == + inserted_encoding.end()) { + encoding_list->push_back(EncodingInfo(IDC_ENCODING_UTF16LE)); + inserted_encoding.insert(IDC_ENCODING_UTF16LE); + start_sorted_index++; + } - // Add those encodings which are in default_encoding_menus and does not - // override with locale-dependent encodings list. - bool previous_is_separator = true; + // Add the rest of encodings that are neither in the static encoding list + // nor in the list of recently selected encodings. + // Build the encoding list sorted in the current locale sorting order. for (int i = 0; i < default_encoding_menus_length; ++i) { int id = default_encoding_menus[i]; - if (id) { - // We have inserted this encoding, skip it. - if (inserted_encoding.find(id) != inserted_encoding.end()) - continue; - encoding_list->push_back(id); - previous_is_separator = false; - } else if (!previous_is_separator) { - encoding_list->push_back(0); - previous_is_separator = true; - } + // We have inserted this encoding, skip it. + if (inserted_encoding.find(id) != inserted_encoding.end()) + continue; + encoding_list->push_back(EncodingInfo(id)); } + // Sort the encoding list. + l10n_util::SortVectorWithStringKey(locale, + encoding_list, + start_sorted_index, + encoding_list->size(), + true); } DCHECK(!encoding_list->empty()); return encoding_list; |