Home | History | Annotate | Download | only in toolbar
      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 "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 "chrome/grit/generated_resources.h"
     16 #include "ui/base/l10n/l10n_util.h"
     17 
     18 const int EncodingMenuController::kValidEncodingIds[] = {
     19     IDC_ENCODING_UTF8,
     20     IDC_ENCODING_UTF16LE,
     21     IDC_ENCODING_ISO88591,
     22     IDC_ENCODING_WINDOWS1252,
     23     IDC_ENCODING_GBK,
     24     IDC_ENCODING_GB18030,
     25     IDC_ENCODING_BIG5,
     26     IDC_ENCODING_BIG5HKSCS,
     27     IDC_ENCODING_KOREAN,
     28     IDC_ENCODING_SHIFTJIS,
     29     IDC_ENCODING_ISO2022JP,
     30     IDC_ENCODING_EUCJP,
     31     IDC_ENCODING_THAI,
     32     IDC_ENCODING_ISO885915,
     33     IDC_ENCODING_MACINTOSH,
     34     IDC_ENCODING_ISO88592,
     35     IDC_ENCODING_WINDOWS1250,
     36     IDC_ENCODING_ISO88595,
     37     IDC_ENCODING_WINDOWS1251,
     38     IDC_ENCODING_KOI8R,
     39     IDC_ENCODING_KOI8U,
     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,
     56 };
     57 
     58 bool EncodingMenuController::DoesCommandBelongToEncodingMenu(int id) {
     59   if (id == IDC_ENCODING_AUTO_DETECT) {
     60     return true;
     61   }
     62 
     63   for (size_t i = 0; i < arraysize(kValidEncodingIds); ++i) {
     64     if (id == kValidEncodingIds[i]) {
     65       return true;
     66     }
     67   }
     68 
     69   return false;
     70 }
     71 
     72 const int* EncodingMenuController::ValidGUIEncodingIDs() {
     73   return kValidEncodingIds;
     74 }
     75 
     76 int EncodingMenuController::NumValidGUIEncodingIDs() {
     77   return arraysize(kValidEncodingIds);
     78 }
     79 
     80 bool EncodingMenuController::IsItemChecked(
     81     Profile* browser_profile,
     82     const std::string& current_tab_encoding,
     83     int item_id) {
     84   if (!DoesCommandBelongToEncodingMenu(item_id))
     85     return false;
     86 
     87   std::string encoding = current_tab_encoding;
     88   if (encoding.empty())
     89     encoding = browser_profile->GetPrefs()->GetString(prefs::kDefaultCharset);
     90 
     91   if (item_id == IDC_ENCODING_AUTO_DETECT) {
     92     return browser_profile->GetPrefs()->GetBoolean(
     93         prefs::kWebKitUsesUniversalDetector);
     94   }
     95 
     96   if (!encoding.empty()) {
     97     return encoding ==
     98         CharacterEncoding::GetCanonicalEncodingNameByCommandId(item_id);
     99   }
    100 
    101   return false;
    102 }
    103 
    104 void EncodingMenuController::GetEncodingMenuItems(Profile* profile,
    105     EncodingMenuItemList* menu_items) {
    106 
    107   DCHECK(menu_items);
    108   EncodingMenuItem separator(0, base::string16());
    109 
    110   menu_items->clear();
    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);
    115 
    116   // Create current display encoding list.
    117   const std::vector<CharacterEncoding::EncodingInfo>* encodings;
    118 
    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));
    126   DCHECK(encodings);
    127   DCHECK(!encodings->empty());
    128 
    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));
    136     } else {
    137       menu_items->push_back(separator);
    138     }
    139   }
    140 }
    141