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.
5 #include "chrome/browser/translate/options_menu_model.h"
7 #include "base/metrics/histogram.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/translate/translate_infobar_delegate.h"
11 #include "chrome/common/url_constants.h"
12 #include "content/public/browser/navigation_controller.h"
13 #include "content/public/browser/navigation_entry.h"
14 #include "content/public/browser/web_contents.h"
15 #include "grit/generated_resources.h"
16 #include "grit/locale_settings.h"
17 #include "ui/base/l10n/l10n_util.h"
19 using content::NavigationEntry
;
20 using content::OpenURLParams
;
21 using content::Referrer
;
22 using content::WebContents
;
24 OptionsMenuModel::OptionsMenuModel(
25 TranslateInfoBarDelegate
* translate_delegate
)
26 : ui::SimpleMenuModel(this),
27 translate_infobar_delegate_(translate_delegate
) {
28 // |translate_delegate| must already be owned.
29 DCHECK(translate_infobar_delegate_
->web_contents());
31 base::string16 original_language
= translate_delegate
->language_name_at(
32 translate_delegate
->original_language_index());
33 base::string16 target_language
= translate_delegate
->language_name_at(
34 translate_delegate
->target_language_index());
36 bool autodetermined_source_language
=
37 translate_delegate
->original_language_index() ==
38 TranslateInfoBarDelegate::kNoIndex
;
41 // Incognito mode does not get any preferences related items.
42 if (!translate_delegate
->web_contents()->GetBrowserContext()->
44 if (!autodetermined_source_language
) {
45 AddCheckItem(IDC_TRANSLATE_OPTIONS_ALWAYS
,
46 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS_ALWAYS
,
47 original_language
, target_language
));
48 AddCheckItem(IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG
,
49 l10n_util::GetStringFUTF16(
50 IDS_TRANSLATE_INFOBAR_OPTIONS_NEVER_TRANSLATE_LANG
,
53 AddCheckItem(IDC_TRANSLATE_OPTIONS_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(IDC_TRANSLATE_REPORT_BAD_LANGUAGE_DETECTION
,
60 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS_REPORT_ERROR
,
63 AddItemWithStringId(IDC_TRANSLATE_OPTIONS_ABOUT
,
64 IDS_TRANSLATE_INFOBAR_OPTIONS_ABOUT
);
67 OptionsMenuModel::~OptionsMenuModel() {
70 bool OptionsMenuModel::IsCommandIdChecked(int command_id
) const {
72 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG
:
73 return !translate_infobar_delegate_
->IsTranslatableLanguageByPrefs();
75 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE
:
76 return translate_infobar_delegate_
->IsSiteBlacklisted();
78 case IDC_TRANSLATE_OPTIONS_ALWAYS
:
79 return translate_infobar_delegate_
->ShouldAlwaysTranslate();
82 NOTREACHED() << "Invalid command_id from menu";
88 bool OptionsMenuModel::IsCommandIdEnabled(int command_id
) const {
90 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG
:
91 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE
:
92 return !translate_infobar_delegate_
->ShouldAlwaysTranslate();
94 case IDC_TRANSLATE_OPTIONS_ALWAYS
:
95 return (translate_infobar_delegate_
->IsTranslatableLanguageByPrefs() &&
96 !translate_infobar_delegate_
->IsSiteBlacklisted());
104 bool OptionsMenuModel::GetAcceleratorForCommandId(
105 int command_id
, ui::Accelerator
* accelerator
) {
109 void OptionsMenuModel::ExecuteCommand(int command_id
, int event_flags
) {
110 switch (command_id
) {
111 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG
:
112 translate_infobar_delegate_
->ToggleTranslatableLanguageByPrefs();
115 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE
:
116 translate_infobar_delegate_
->ToggleSiteBlacklist();
119 case IDC_TRANSLATE_OPTIONS_ALWAYS
:
120 translate_infobar_delegate_
->ToggleAlwaysTranslate();
123 case IDC_TRANSLATE_REPORT_BAD_LANGUAGE_DETECTION
:
124 translate_infobar_delegate_
->ReportLanguageDetectionError();
127 case IDC_TRANSLATE_OPTIONS_ABOUT
: {
128 WebContents
* web_contents
= translate_infobar_delegate_
->web_contents();
130 OpenURLParams
params(
131 GURL(chrome::kAboutGoogleTranslateURL
), Referrer(),
132 NEW_FOREGROUND_TAB
, content::PAGE_TRANSITION_LINK
, false);
133 web_contents
->OpenURL(params
);
139 NOTREACHED() << "Invalid command id from menu.";