1 // Copyright 2014 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 "components/translate/core/browser/options_menu_model.h"
7 #include "base/metrics/histogram.h"
8 #include "components/translate/core/browser/translate_driver.h"
9 #include "components/translate/core/browser/translate_infobar_delegate.h"
10 #include "grit/components_strings.h"
11 #include "ui/base/l10n/l10n_util.h"
17 const char kAboutGoogleTranslateURL
[] =
18 #if defined(OS_CHROMEOS)
19 "https://support.google.com/chromebook/?p=ib_translation_bar";
21 "https://support.google.com/chrome/?p=ib_translation_bar";
26 OptionsMenuModel::OptionsMenuModel(TranslateInfoBarDelegate
* translate_delegate
)
27 : ui::SimpleMenuModel(this),
28 translate_infobar_delegate_(translate_delegate
) {
29 // |translate_delegate| must already be owned.
30 DCHECK(translate_infobar_delegate_
->GetTranslateDriver());
32 base::string16 original_language
= translate_delegate
->language_name_at(
33 translate_delegate
->original_language_index());
34 base::string16 target_language
= translate_delegate
->language_name_at(
35 translate_delegate
->target_language_index());
37 bool autodetermined_source_language
=
38 translate_delegate
->original_language_index() ==
39 TranslateInfoBarDelegate::kNoIndex
;
42 // Incognito mode does not get any preferences related items.
43 if (!translate_delegate
->is_off_the_record()) {
44 if (!autodetermined_source_language
) {
45 AddCheckItem(ALWAYS_TRANSLATE
,
46 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS_ALWAYS
,
47 original_language
, target_language
));
48 AddCheckItem(NEVER_TRANSLATE_LANGUAGE
,
49 l10n_util::GetStringFUTF16(
50 IDS_TRANSLATE_INFOBAR_OPTIONS_NEVER_TRANSLATE_LANG
,
53 AddCheckItem(NEVER_TRANSLATE_SITE
,
54 l10n_util::GetStringUTF16(
55 IDS_TRANSLATE_INFOBAR_OPTIONS_NEVER_TRANSLATE_SITE
));
56 AddSeparator(ui::NORMAL_SEPARATOR
);
58 if (!autodetermined_source_language
) {
59 AddItem(REPORT_BAD_DETECTION
,
60 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS_REPORT_ERROR
,
63 AddItemWithStringId(ABOUT_TRANSLATE
, IDS_TRANSLATE_INFOBAR_OPTIONS_ABOUT
);
66 OptionsMenuModel::~OptionsMenuModel() {
69 bool OptionsMenuModel::IsCommandIdChecked(int command_id
) const {
71 case NEVER_TRANSLATE_LANGUAGE
:
72 return !translate_infobar_delegate_
->IsTranslatableLanguageByPrefs();
74 case NEVER_TRANSLATE_SITE
:
75 return translate_infobar_delegate_
->IsSiteBlacklisted();
77 case ALWAYS_TRANSLATE
:
78 return translate_infobar_delegate_
->ShouldAlwaysTranslate();
81 NOTREACHED() << "Invalid command_id from menu";
87 bool OptionsMenuModel::IsCommandIdEnabled(int command_id
) const {
89 case NEVER_TRANSLATE_LANGUAGE
:
90 case NEVER_TRANSLATE_SITE
:
91 return !translate_infobar_delegate_
->ShouldAlwaysTranslate();
93 case ALWAYS_TRANSLATE
:
94 return (translate_infobar_delegate_
->IsTranslatableLanguageByPrefs() &&
95 !translate_infobar_delegate_
->IsSiteBlacklisted());
103 bool OptionsMenuModel::GetAcceleratorForCommandId(
105 ui::Accelerator
* accelerator
) {
109 void OptionsMenuModel::ExecuteCommand(int command_id
, int event_flags
) {
110 switch (command_id
) {
111 case NEVER_TRANSLATE_LANGUAGE
:
112 translate_infobar_delegate_
->ToggleTranslatableLanguageByPrefs();
115 case NEVER_TRANSLATE_SITE
:
116 translate_infobar_delegate_
->ToggleSiteBlacklist();
119 case ALWAYS_TRANSLATE
:
120 translate_infobar_delegate_
->ToggleAlwaysTranslate();
123 case REPORT_BAD_DETECTION
:
124 translate_infobar_delegate_
->ReportLanguageDetectionError();
127 case ABOUT_TRANSLATE
: {
128 TranslateDriver
* translate_driver
=
129 translate_infobar_delegate_
->GetTranslateDriver();
130 if (translate_driver
)
131 translate_driver
->OpenUrlInNewTab(GURL(kAboutGoogleTranslateURL
));
136 NOTREACHED() << "Invalid command id from menu.";
141 } // namespace translate