Home | History | Annotate | Download | only in translate
      1 // Copyright (c) 2011 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/translate/options_menu_model.h"
      6 
      7 #include "base/metrics/histogram.h"
      8 #include "chrome/app/chrome_command_ids.h"
      9 #include "chrome/browser/google/google_util.h"
     10 #include "chrome/browser/profiles/profile.h"
     11 #include "chrome/browser/translate/translate_infobar_delegate.h"
     12 #include "content/browser/tab_contents/tab_contents.h"
     13 #include "grit/generated_resources.h"
     14 #include "grit/locale_settings.h"
     15 #include "ui/base/l10n/l10n_util.h"
     16 
     17 namespace {
     18 
     19 const char kAboutGoogleTranslateUrl[] =
     20 #if defined(OS_CHROMEOS)
     21     "https://www.google.com/support/chromeos/bin/answer.py?answer=173424";
     22 #else
     23     "https://www.google.com/support/chrome/bin/answer.py?answer=173424";
     24 #endif
     25 
     26 }  // namespace
     27 
     28 OptionsMenuModel::OptionsMenuModel(
     29     TranslateInfoBarDelegate* translate_delegate)
     30     : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)),
     31       translate_infobar_delegate_(translate_delegate) {
     32   string16 original_language = translate_delegate->GetLanguageDisplayableNameAt(
     33       translate_delegate->original_language_index());
     34   string16 target_language = translate_delegate->GetLanguageDisplayableNameAt(
     35       translate_delegate->target_language_index());
     36 
     37   // Populate the menu.
     38   // Incognito mode does not get any preferences related items.
     39   if (!translate_delegate->tab_contents()->profile()->IsOffTheRecord()) {
     40     AddCheckItem(IDC_TRANSLATE_OPTIONS_ALWAYS,
     41         l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS_ALWAYS,
     42             original_language, target_language));
     43     AddCheckItem(IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG,
     44         l10n_util::GetStringFUTF16(
     45             IDS_TRANSLATE_INFOBAR_OPTIONS_NEVER_TRANSLATE_LANG,
     46             original_language));
     47     AddCheckItem(IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE,
     48         l10n_util::GetStringUTF16(
     49             IDS_TRANSLATE_INFOBAR_OPTIONS_NEVER_TRANSLATE_SITE));
     50     AddSeparator();
     51   }
     52   AddItem(IDC_TRANSLATE_REPORT_BAD_LANGUAGE_DETECTION,
     53           l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS_REPORT_ERROR,
     54                                      original_language));
     55   AddItemWithStringId(IDC_TRANSLATE_OPTIONS_ABOUT,
     56       IDS_TRANSLATE_INFOBAR_OPTIONS_ABOUT);
     57 }
     58 
     59 OptionsMenuModel::~OptionsMenuModel() {
     60 }
     61 
     62 bool OptionsMenuModel::IsCommandIdChecked(int command_id) const {
     63   switch (command_id) {
     64     case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG:
     65       return translate_infobar_delegate_->IsLanguageBlacklisted();
     66 
     67     case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE:
     68       return translate_infobar_delegate_->IsSiteBlacklisted();
     69 
     70     case IDC_TRANSLATE_OPTIONS_ALWAYS:
     71       return translate_infobar_delegate_->ShouldAlwaysTranslate();
     72 
     73     default:
     74       NOTREACHED() << "Invalid command_id from menu";
     75       break;
     76   }
     77   return false;
     78 }
     79 
     80 bool OptionsMenuModel::IsCommandIdEnabled(int command_id) const {
     81   switch (command_id) {
     82     case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG :
     83     case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE :
     84       return !translate_infobar_delegate_->ShouldAlwaysTranslate();
     85 
     86     case IDC_TRANSLATE_OPTIONS_ALWAYS :
     87       return (!translate_infobar_delegate_->IsLanguageBlacklisted() &&
     88           !translate_infobar_delegate_->IsSiteBlacklisted());
     89 
     90     default:
     91       break;
     92   }
     93   return true;
     94 }
     95 
     96 bool OptionsMenuModel::GetAcceleratorForCommandId(
     97     int command_id, ui::Accelerator* accelerator) {
     98   return false;
     99 }
    100 
    101 void OptionsMenuModel::ExecuteCommand(int command_id) {
    102   switch (command_id) {
    103     case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG:
    104       UMA_HISTOGRAM_COUNTS("Translate.NeverTranslateLang", 1);
    105       translate_infobar_delegate_->ToggleLanguageBlacklist();
    106       break;
    107 
    108     case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE:
    109       UMA_HISTOGRAM_COUNTS("Translate.NeverTranslateSite", 1);
    110       translate_infobar_delegate_->ToggleSiteBlacklist();
    111       break;
    112 
    113     case IDC_TRANSLATE_OPTIONS_ALWAYS:
    114       UMA_HISTOGRAM_COUNTS("Translate.AlwaysTranslateLang", 1);
    115       translate_infobar_delegate_->ToggleAlwaysTranslate();
    116       break;
    117 
    118     case IDC_TRANSLATE_REPORT_BAD_LANGUAGE_DETECTION:
    119       translate_infobar_delegate_->ReportLanguageDetectionError();
    120       break;
    121 
    122     case IDC_TRANSLATE_OPTIONS_ABOUT: {
    123       TabContents* tab_contents = translate_infobar_delegate_->tab_contents();
    124       if (tab_contents) {
    125         GURL about_url = google_util::AppendGoogleLocaleParam(
    126             GURL(kAboutGoogleTranslateUrl));
    127         tab_contents->OpenURL(
    128             about_url, GURL(), NEW_FOREGROUND_TAB, PageTransition::LINK);
    129       }
    130       break;
    131     }
    132 
    133     default:
    134       NOTREACHED() << "Invalid command id from menu.";
    135       break;
    136   }
    137 }
    138