1 // Copyright 2013 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 "ios/chrome/browser/translate/after_translate_infobar_controller.h"
7 #include "base/strings/sys_string_conversions.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/translate/core/browser/translate_infobar_delegate.h"
10 #include "grit/components_strings.h"
11 #include "ios/chrome/browser/translate/translate_infobar_tags.h"
12 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
13 #include "ios/public/provider/chrome/browser/string_provider.h"
14 #import "ios/public/provider/chrome/browser/ui/infobar_view_delegate.h"
15 #import "ios/public/provider/chrome/browser/ui/infobar_view_protocol.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/gfx/image/image.h"
19 @interface AfterTranslateInfoBarController () {
20 __weak translate::TranslateInfoBarDelegate* _translateInfoBarDelegate;
23 // Action for any of the user defined buttons.
24 - (void)infoBarButtonDidPress:(id)sender;
28 @implementation AfterTranslateInfoBarController
31 #pragma mark InfoBarControllerProtocol
33 - (base::scoped_nsobject<UIView<InfoBarViewProtocol>>)
34 viewForDelegate:(infobars::InfoBarDelegate*)delegate
36 base::scoped_nsobject<UIView<InfoBarViewProtocol>> infoBarView;
37 _translateInfoBarDelegate = delegate->AsTranslateInfoBarDelegate();
38 DCHECK(_translateInfoBarDelegate);
40 ios::GetChromeBrowserProvider()->CreateInfoBarView(frame, self.delegate));
42 gfx::Image icon = _translateInfoBarDelegate->GetIcon();
44 [infoBarView addLeftIcon:icon.ToUIImage()];
46 const bool autodeterminedSourceLanguage =
47 _translateInfoBarDelegate->original_language_index() ==
48 translate::TranslateInfoBarDelegate::kNoIndex;
49 bool swappedLanguageButtons;
50 std::vector<base::string16> strings;
51 translate::TranslateInfoBarDelegate::GetAfterTranslateStrings(
52 &strings, &swappedLanguageButtons, autodeterminedSourceLanguage);
53 DCHECK_EQ(autodeterminedSourceLanguage ? 2U : 3U, strings.size());
54 NSString* label1 = base::SysUTF16ToNSString(strings[0]);
55 NSString* label2 = base::SysUTF16ToNSString(strings[1]);
56 NSString* label3 = autodeterminedSourceLanguage
58 : base::SysUTF16ToNSString(strings[2]);
59 base::string16 stdOriginal = _translateInfoBarDelegate->language_name_at(
60 _translateInfoBarDelegate->original_language_index());
61 NSString* original = base::SysUTF16ToNSString(stdOriginal);
63 base::SysUTF16ToNSString(_translateInfoBarDelegate->language_name_at(
64 _translateInfoBarDelegate->target_language_index()));
65 base::scoped_nsobject<NSString> label(
66 [[NSString alloc] initWithFormat:@"%@ %@ %@%@ %@.", label1, original,
67 label2, label3, target]);
68 [infoBarView addLabel:label];
70 [infoBarView addCloseButtonWithTag:TranslateInfoBarIOSTag::CLOSE
72 action:@selector(infoBarButtonDidPress:)];
74 NSString* buttonRevert = l10n_util::GetNSString(IDS_TRANSLATE_INFOBAR_REVERT);
75 NSString* buttonOptions = base::SysUTF16ToNSString(
76 ios::GetChromeBrowserProvider()->GetStringProvider()->GetDoneString());
77 [infoBarView addButton1:buttonOptions
78 tag1:TranslateInfoBarIOSTag::AFTER_DONE
80 tag2:TranslateInfoBarIOSTag::AFTER_REVERT
82 action:@selector(infoBarButtonDidPress:)];
83 // Always translate switch.
84 if (_translateInfoBarDelegate->ShouldShowAlwaysTranslateShortcut()) {
85 base::string16 alwaysTranslate = l10n_util::GetStringFUTF16(
86 IDS_TRANSLATE_INFOBAR_ALWAYS_TRANSLATE, stdOriginal);
87 const BOOL switchValue = _translateInfoBarDelegate->ShouldAlwaysTranslate();
89 addSwitchWithLabel:base::SysUTF16ToNSString(alwaysTranslate)
91 tag:TranslateInfoBarIOSTag::ALWAYS_TRANSLATE_SWITCH
93 action:@selector(infoBarSwitchDidPress:)];
98 #pragma mark - Handling of User Events
100 - (void)infoBarButtonDidPress:(id)sender {
101 // This press might have occurred after the user has already pressed a button,
102 // in which case the view has been detached from the delegate and this press
103 // should be ignored.
104 if (!self.delegate) {
107 if ([sender isKindOfClass:[UIButton class]]) {
108 NSUInteger buttonId = static_cast<UIButton*>(sender).tag;
109 if (buttonId == TranslateInfoBarIOSTag::CLOSE) {
110 self.delegate->InfoBarDidCancel();
112 DCHECK(buttonId == TranslateInfoBarIOSTag::AFTER_REVERT ||
113 buttonId == TranslateInfoBarIOSTag::AFTER_DONE);
114 self.delegate->InfoBarButtonDidPress(buttonId);
119 - (void)infoBarSwitchDidPress:(id)sender {
120 DCHECK_EQ(TranslateInfoBarIOSTag::ALWAYS_TRANSLATE_SWITCH, [sender tag]);
121 DCHECK([sender respondsToSelector:@selector(isOn)]);
122 if (_translateInfoBarDelegate->ShouldAlwaysTranslate() != [sender isOn])
123 _translateInfoBarDelegate->ToggleAlwaysTranslate();