OLD | NEW |
| (Empty) |
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/translate/options_menu_model.h" | |
6 | |
7 #include "base/metrics/histogram.h" | |
8 #include "chrome/app/chrome_command_ids.h" | |
9 #include "chrome/browser/translate/translate_infobar_delegate.h" | |
10 #include "components/translate/core/browser/translate_driver.h" | |
11 #include "grit/components_strings.h" | |
12 #include "grit/locale_settings.h" | |
13 #include "ui/base/l10n/l10n_util.h" | |
14 | |
15 namespace { | |
16 | |
17 const char kAboutGoogleTranslateURL[] = | |
18 #if defined(OS_CHROMEOS) | |
19 "https://support.google.com/chromeos/?p=ib_translation_bar"; | |
20 #else | |
21 "https://support.google.com/chrome/?p=ib_translation_bar"; | |
22 #endif | |
23 | |
24 } // namespace | |
25 | |
26 OptionsMenuModel::OptionsMenuModel( | |
27 TranslateInfoBarDelegate* translate_delegate) | |
28 : ui::SimpleMenuModel(this), | |
29 translate_infobar_delegate_(translate_delegate) { | |
30 // |translate_delegate| must already be owned. | |
31 DCHECK(translate_infobar_delegate_->GetTranslateDriver()); | |
32 | |
33 base::string16 original_language = translate_delegate->language_name_at( | |
34 translate_delegate->original_language_index()); | |
35 base::string16 target_language = translate_delegate->language_name_at( | |
36 translate_delegate->target_language_index()); | |
37 | |
38 bool autodetermined_source_language = | |
39 translate_delegate->original_language_index() == | |
40 TranslateInfoBarDelegate::kNoIndex; | |
41 | |
42 // Populate the menu. | |
43 // Incognito mode does not get any preferences related items. | |
44 if (!translate_delegate->is_off_the_record()) { | |
45 if (!autodetermined_source_language) { | |
46 AddCheckItem(IDC_TRANSLATE_OPTIONS_ALWAYS, | |
47 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS_ALWAYS, | |
48 original_language, target_language)); | |
49 AddCheckItem(IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG, | |
50 l10n_util::GetStringFUTF16( | |
51 IDS_TRANSLATE_INFOBAR_OPTIONS_NEVER_TRANSLATE_LANG, | |
52 original_language)); | |
53 } | |
54 AddCheckItem(IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE, | |
55 l10n_util::GetStringUTF16( | |
56 IDS_TRANSLATE_INFOBAR_OPTIONS_NEVER_TRANSLATE_SITE)); | |
57 AddSeparator(ui::NORMAL_SEPARATOR); | |
58 } | |
59 if (!autodetermined_source_language) { | |
60 AddItem(IDC_TRANSLATE_REPORT_BAD_LANGUAGE_DETECTION, | |
61 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS_REPORT_ERROR, | |
62 original_language)); | |
63 } | |
64 AddItemWithStringId(IDC_TRANSLATE_OPTIONS_ABOUT, | |
65 IDS_TRANSLATE_INFOBAR_OPTIONS_ABOUT); | |
66 } | |
67 | |
68 OptionsMenuModel::~OptionsMenuModel() { | |
69 } | |
70 | |
71 bool OptionsMenuModel::IsCommandIdChecked(int command_id) const { | |
72 switch (command_id) { | |
73 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG: | |
74 return !translate_infobar_delegate_->IsTranslatableLanguageByPrefs(); | |
75 | |
76 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE: | |
77 return translate_infobar_delegate_->IsSiteBlacklisted(); | |
78 | |
79 case IDC_TRANSLATE_OPTIONS_ALWAYS: | |
80 return translate_infobar_delegate_->ShouldAlwaysTranslate(); | |
81 | |
82 default: | |
83 NOTREACHED() << "Invalid command_id from menu"; | |
84 break; | |
85 } | |
86 return false; | |
87 } | |
88 | |
89 bool OptionsMenuModel::IsCommandIdEnabled(int command_id) const { | |
90 switch (command_id) { | |
91 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG : | |
92 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE : | |
93 return !translate_infobar_delegate_->ShouldAlwaysTranslate(); | |
94 | |
95 case IDC_TRANSLATE_OPTIONS_ALWAYS : | |
96 return (translate_infobar_delegate_->IsTranslatableLanguageByPrefs() && | |
97 !translate_infobar_delegate_->IsSiteBlacklisted()); | |
98 | |
99 default: | |
100 break; | |
101 } | |
102 return true; | |
103 } | |
104 | |
105 bool OptionsMenuModel::GetAcceleratorForCommandId( | |
106 int command_id, ui::Accelerator* accelerator) { | |
107 return false; | |
108 } | |
109 | |
110 void OptionsMenuModel::ExecuteCommand(int command_id, int event_flags) { | |
111 switch (command_id) { | |
112 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG: | |
113 translate_infobar_delegate_->ToggleTranslatableLanguageByPrefs(); | |
114 break; | |
115 | |
116 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE: | |
117 translate_infobar_delegate_->ToggleSiteBlacklist(); | |
118 break; | |
119 | |
120 case IDC_TRANSLATE_OPTIONS_ALWAYS: | |
121 translate_infobar_delegate_->ToggleAlwaysTranslate(); | |
122 break; | |
123 | |
124 case IDC_TRANSLATE_REPORT_BAD_LANGUAGE_DETECTION: | |
125 translate_infobar_delegate_->ReportLanguageDetectionError(); | |
126 break; | |
127 | |
128 case IDC_TRANSLATE_OPTIONS_ABOUT: { | |
129 TranslateDriver* translate_driver = | |
130 translate_infobar_delegate_->GetTranslateDriver(); | |
131 if (translate_driver) | |
132 translate_driver->OpenUrlInNewTab(GURL(kAboutGoogleTranslateURL)); | |
133 break; | |
134 } | |
135 | |
136 default: | |
137 NOTREACHED() << "Invalid command id from menu."; | |
138 break; | |
139 } | |
140 } | |
OLD | NEW |