Extract code handling PrinterProviderAPI from PrintPreviewHandler
[chromium-blink-merge.git] / ios / chrome / browser / translate / after_translate_infobar_controller.mm
blobb325b0f83548fb3f03aff03832dec8d2d82b77b8
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;
26 @end
28 @implementation AfterTranslateInfoBarController
30 #pragma mark -
31 #pragma mark InfoBarControllerProtocol
33 - (void)layoutForDelegate:(infobars::InfoBarDelegate*)delegate
34                     frame:(CGRect)frame {
35   _translateInfoBarDelegate = delegate->AsTranslateInfoBarDelegate();
36   DCHECK(_translateInfoBarDelegate);
37   infobars::InfoBarDelegate* infoBarDelegate =
38       static_cast<infobars::InfoBarDelegate*>(_translateInfoBarDelegate);
39   DCHECK(!infoBarView_);
40   infoBarView_.reset([ios::GetChromeBrowserProvider()->CreateInfoBarView()
41       initWithFrame:frame
42            delegate:delegate_
43           isWarning:infoBarDelegate->GetInfoBarType() ==
44                     infobars::InfoBarDelegate::WARNING_TYPE]);
45   // Icon
46   gfx::Image icon = _translateInfoBarDelegate->GetIcon();
47   if (!icon.IsEmpty())
48     [infoBarView_ addLeftIcon:icon.ToUIImage()];
49   // Main text.
50   const bool autodeterminedSourceLanguage =
51       _translateInfoBarDelegate->original_language_index() ==
52       translate::TranslateInfoBarDelegate::kNoIndex;
53   bool swappedLanguageButtons;
54   std::vector<base::string16> strings;
55   translate::TranslateInfoBarDelegate::GetAfterTranslateStrings(
56       &strings, &swappedLanguageButtons, autodeterminedSourceLanguage);
57   DCHECK_EQ(autodeterminedSourceLanguage ? 2U : 3U, strings.size());
58   NSString* label1 = base::SysUTF16ToNSString(strings[0]);
59   NSString* label2 = base::SysUTF16ToNSString(strings[1]);
60   NSString* label3 = autodeterminedSourceLanguage
61                          ? @""
62                          : base::SysUTF16ToNSString(strings[2]);
63   base::string16 stdOriginal = _translateInfoBarDelegate->language_name_at(
64       _translateInfoBarDelegate->original_language_index());
65   NSString* original = base::SysUTF16ToNSString(stdOriginal);
66   NSString* target =
67       base::SysUTF16ToNSString(_translateInfoBarDelegate->language_name_at(
68           _translateInfoBarDelegate->target_language_index()));
69   base::scoped_nsobject<NSString> label(
70       [[NSString alloc] initWithFormat:@"%@ %@ %@%@ %@.", label1, original,
71                                        label2, label3, target]);
72   [infoBarView_ addLabel:label];
73   // Close button.
74   [infoBarView_ addCloseButtonWithTag:TranslateInfoBarIOSTag::CLOSE
75                                target:self
76                                action:@selector(infoBarButtonDidPress:)];
77   // Other buttons.
78   NSString* buttonRevert = l10n_util::GetNSString(IDS_TRANSLATE_INFOBAR_REVERT);
79   NSString* buttonOptions = base::SysUTF16ToNSString(
80       ios::GetChromeBrowserProvider()->GetStringProvider()->GetDoneString());
81   [infoBarView_ addButton1:buttonOptions
82                       tag1:TranslateInfoBarIOSTag::AFTER_DONE
83                    button2:buttonRevert
84                       tag2:TranslateInfoBarIOSTag::AFTER_REVERT
85                     target:self
86                     action:@selector(infoBarButtonDidPress:)];
87   // Always translate switch.
88   if (_translateInfoBarDelegate->ShouldShowAlwaysTranslateShortcut()) {
89     base::string16 alwaysTranslate = l10n_util::GetStringFUTF16(
90         IDS_TRANSLATE_INFOBAR_ALWAYS_TRANSLATE, stdOriginal);
91     const BOOL switchValue = _translateInfoBarDelegate->ShouldAlwaysTranslate();
92     [infoBarView_
93         addSwitchWithLabel:base::SysUTF16ToNSString(alwaysTranslate)
94                       isOn:switchValue
95                        tag:TranslateInfoBarIOSTag::ALWAYS_TRANSLATE_SWITCH
96                     target:self
97                     action:@selector(infoBarSwitchDidPress:)];
98   }
101 #pragma mark - Handling of User Events
103 - (void)infoBarButtonDidPress:(id)sender {
104   // This press might have occurred after the user has already pressed a button,
105   // in which case the view has been detached from the delegate and this press
106   // should be ignored.
107   if (!delegate_) {
108     return;
109   }
110   if ([sender isKindOfClass:[UIButton class]]) {
111     NSUInteger buttonId = static_cast<UIButton*>(sender).tag;
112     if (buttonId == TranslateInfoBarIOSTag::CLOSE) {
113       delegate_->InfoBarDidCancel();
114     } else {
115       DCHECK(buttonId == TranslateInfoBarIOSTag::AFTER_REVERT ||
116              buttonId == TranslateInfoBarIOSTag::AFTER_DONE);
117       delegate_->InfoBarButtonDidPress(buttonId);
118     }
119   }
122 - (void)infoBarSwitchDidPress:(id)sender {
123   DCHECK_EQ(TranslateInfoBarIOSTag::ALWAYS_TRANSLATE_SWITCH, [sender tag]);
124   DCHECK([sender respondsToSelector:@selector(isOn)]);
125   if (_translateInfoBarDelegate->ShouldAlwaysTranslate() != [sender isOn])
126     _translateInfoBarDelegate->ToggleAlwaysTranslate();
129 @end