OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ios/chrome/browser/translate/never_translate_infobar_controller.h" |
| 6 |
| 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 #import "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" |
| 18 |
| 19 @interface NeverTranslateInfoBarController () |
| 20 |
| 21 // Action for any of the user defined buttons. |
| 22 - (void)infoBarButtonDidPress:(id)sender; |
| 23 |
| 24 @end |
| 25 |
| 26 @implementation NeverTranslateInfoBarController |
| 27 |
| 28 #pragma mark - |
| 29 #pragma mark InfoBarControllerProtocol |
| 30 |
| 31 - (void)layoutForDelegate:(infobars::InfoBarDelegate*)delegate |
| 32 frame:(CGRect)frame { |
| 33 translate::TranslateInfoBarDelegate* translateInfoBarDelegate = |
| 34 delegate->AsTranslateInfoBarDelegate(); |
| 35 infobars::InfoBarDelegate* infoBarDelegate = |
| 36 static_cast<infobars::InfoBarDelegate*>(translateInfoBarDelegate); |
| 37 DCHECK(!infoBarView_); |
| 38 ios::ChromeBrowserProvider* provider = ios::GetChromeBrowserProvider(); |
| 39 infoBarView_.reset([provider->CreateInfoBarView() |
| 40 initWithFrame:frame |
| 41 delegate:delegate_ |
| 42 isWarning:infoBarDelegate->GetInfoBarType() == |
| 43 infobars::InfoBarDelegate::WARNING_TYPE]); |
| 44 // Icon |
| 45 gfx::Image icon = translateInfoBarDelegate->GetIcon(); |
| 46 if (!icon.IsEmpty()) |
| 47 [infoBarView_ addLeftIcon:icon.ToUIImage()]; |
| 48 // Main text. |
| 49 base::string16 originalLanguage = translateInfoBarDelegate->language_name_at( |
| 50 translateInfoBarDelegate->original_language_index()); |
| 51 [infoBarView_ addLabel:l10n_util::GetNSStringF( |
| 52 IDS_TRANSLATE_INFOBAR_NEVER_MESSAGE_IOS, |
| 53 provider->GetStringProvider()->GetProductName(), |
| 54 originalLanguage)]; |
| 55 // Close button. |
| 56 [infoBarView_ addCloseButtonWithTag:TranslateInfoBarIOSTag::CLOSE |
| 57 target:self |
| 58 action:@selector(infoBarButtonDidPress:)]; |
| 59 // Other buttons. |
| 60 NSString* buttonLanguage = l10n_util::GetNSStringF( |
| 61 IDS_TRANSLATE_INFOBAR_NEVER_TRANSLATE, originalLanguage); |
| 62 NSString* buttonSite = l10n_util::GetNSString( |
| 63 IDS_TRANSLATE_INFOBAR_OPTIONS_NEVER_TRANSLATE_SITE); |
| 64 [infoBarView_ addButton1:buttonLanguage |
| 65 tag1:TranslateInfoBarIOSTag::DENY_LANGUAGE |
| 66 button2:buttonSite |
| 67 tag2:TranslateInfoBarIOSTag::DENY_WEBSITE |
| 68 target:self |
| 69 action:@selector(infoBarButtonDidPress:)]; |
| 70 } |
| 71 |
| 72 #pragma mark - Handling of User Events |
| 73 |
| 74 - (void)infoBarButtonDidPress:(id)sender { |
| 75 // This press might have occurred after the user has already pressed a button, |
| 76 // in which case the view has been detached from the delegate and this press |
| 77 // should be ignored. |
| 78 if (!delegate_) { |
| 79 return; |
| 80 } |
| 81 if ([sender isKindOfClass:[UIButton class]]) { |
| 82 NSUInteger buttonId = static_cast<UIButton*>(sender).tag; |
| 83 if (buttonId == TranslateInfoBarIOSTag::CLOSE) { |
| 84 delegate_->InfoBarDidCancel(); |
| 85 } else { |
| 86 DCHECK(buttonId == TranslateInfoBarIOSTag::DENY_LANGUAGE || |
| 87 buttonId == TranslateInfoBarIOSTag::DENY_WEBSITE); |
| 88 delegate_->InfoBarButtonDidPress(buttonId); |
| 89 } |
| 90 } |
| 91 } |
| 92 |
| 93 @end |
OLD | NEW |