OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 #import "chrome/browser/autofill/autofill_credit_card_sheet_controller_mac.h" |
| 6 |
| 7 #include "app/l10n_util.h" |
| 8 #include "base/mac_util.h" |
| 9 #include "base/sys_string_conversions.h" |
| 10 #import "chrome/browser/autofill/autofill_credit_card_model_mac.h" |
| 11 #import "chrome/browser/autofill/autofill_dialog_controller_mac.h" |
| 12 #include "chrome/browser/autofill/credit_card.h" |
| 13 #include "grit/generated_resources.h" |
| 14 |
| 15 // Private methods for the |AutoFillCreditCardSheetController| class. |
| 16 @interface AutoFillCreditCardSheetController (PrivateMethods) |
| 17 - (void)buildBillingAddressContents; |
| 18 - (void)buildExpirationMonthContents; |
| 19 - (void)buildExpirationYearContents; |
| 20 @end |
| 21 |
| 22 @implementation AutoFillCreditCardSheetController |
| 23 |
| 24 @synthesize creditCardModel = creditCardModel_; |
| 25 @synthesize billingAddressContents = billingAddressContents_; |
| 26 @synthesize expirationMonthContents = expirationMonthContents_; |
| 27 @synthesize expirationYearContents = expirationYearContents_; |
| 28 |
| 29 - (id)initWithCreditCard:(const CreditCard&)creditCard |
| 30 mode:(AutoFillCreditCardMode)mode |
| 31 controller:(AutoFillDialogController*)parentController { |
| 32 NSString* nibPath = [mac_util::MainAppBundle() |
| 33 pathForResource:@"AutoFillCreditCardSheet" |
| 34 ofType:@"nib"]; |
| 35 self = [super initWithWindowNibPath:nibPath owner:self]; |
| 36 if (self) { |
| 37 // Create the model. We use setter here for KVO. |
| 38 [self setCreditCardModel:[[[AutoFillCreditCardModel alloc] |
| 39 initWithCreditCard:creditCard] autorelease]]; |
| 40 |
| 41 // We keep track of our parent controller for model-update purposes. |
| 42 parentController_ = parentController; |
| 43 |
| 44 mode_ = mode; |
| 45 } |
| 46 return self; |
| 47 } |
| 48 |
| 49 - (void)dealloc { |
| 50 [creditCardModel_ release]; |
| 51 [billingAddressContents_ release]; |
| 52 [expirationMonthContents_ release]; |
| 53 [expirationYearContents_ release]; |
| 54 [super dealloc]; |
| 55 } |
| 56 |
| 57 - (void)awakeFromNib { |
| 58 // Setup initial state of popups. |
| 59 [self buildBillingAddressContents]; |
| 60 [self buildExpirationMonthContents]; |
| 61 [self buildExpirationYearContents]; |
| 62 |
| 63 // Turn menu autoenable off. We manually govern this. |
| 64 [billingAddressPopup_ setAutoenablesItems:NO]; |
| 65 [expirationMonthPopup_ setAutoenablesItems:NO]; |
| 66 [expirationYearPopup_ setAutoenablesItems:NO]; |
| 67 |
| 68 // Set the caption based on the mode. |
| 69 NSString* caption; |
| 70 if (mode_ == kAutoFillCreditCardAddMode) |
| 71 caption = l10n_util::GetNSString(IDS_AUTOFILL_ADD_CREDITCARD_CAPTION); |
| 72 else if (mode_ == kAutoFillCreditCardEditMode) |
| 73 caption = l10n_util::GetNSString(IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION); |
| 74 else |
| 75 NOTREACHED(); |
| 76 [caption_ setStringValue:caption]; |
| 77 } |
| 78 |
| 79 - (IBAction)save:(id)sender { |
| 80 // Call |makeFirstResponder:| to commit pending text field edits. |
| 81 [[self window] makeFirstResponder:[self window]]; |
| 82 |
| 83 [NSApp endSheet:[self window] returnCode:1]; |
| 84 } |
| 85 |
| 86 - (IBAction)cancel:(id)sender { |
| 87 [NSApp endSheet:[self window] returnCode:0]; |
| 88 } |
| 89 |
| 90 - (void)copyModelToCreditCard:(CreditCard*)creditCard { |
| 91 // The model copies the popup values blindly. We need to clear the strings |
| 92 // in the case that our special menus are in effect. |
| 93 if ([billingAddressPopup_ indexOfSelectedItem] <= 0) |
| 94 [creditCardModel_ setBillingAddress:@""]; |
| 95 if ([expirationMonthPopup_ indexOfSelectedItem] <= 0) |
| 96 [creditCardModel_ setExpirationMonth:@""]; |
| 97 if ([expirationYearPopup_ indexOfSelectedItem] <= 0) |
| 98 [creditCardModel_ setExpirationYear:@""]; |
| 99 |
| 100 [creditCardModel_ copyModelToCreditCard:creditCard]; |
| 101 } |
| 102 |
| 103 // Builds the |billingAddressContents_| array of strings from the list of |
| 104 // addresses returned by the |parentController_| and additional UI string. |
| 105 // Ensures that current selection is valid. If not, reset it. |
| 106 - (void)buildBillingAddressContents { |
| 107 NSString* menuString = l10n_util::GetNSString( |
| 108 IDS_AUTOFILL_DIALOG_CHOOSE_EXISTING_ADDRESS); |
| 109 |
| 110 // Build the menu array and set it. |
| 111 NSArray* addressStrings = [parentController_ addressLabels]; |
| 112 NSArray* newArray = [[NSArray arrayWithObject:menuString] |
| 113 arrayByAddingObjectsFromArray:addressStrings]; |
| 114 [self setBillingAddressContents:newArray]; |
| 115 |
| 116 // If the addresses no longer contain our selected item, reset the selection. |
| 117 if ([addressStrings |
| 118 indexOfObject:[creditCardModel_ billingAddress]] == NSNotFound) { |
| 119 [creditCardModel_ setBillingAddress:menuString]; |
| 120 } |
| 121 |
| 122 // Disable first item in menu. "Choose existing address" is a non-item. |
| 123 [[billingAddressPopup_ itemAtIndex:0] setEnabled:NO]; |
| 124 } |
| 125 |
| 126 // Builds array of valid months. Uses special @" " to indicate no selection. |
| 127 - (void)buildExpirationMonthContents { |
| 128 NSArray* newArray = [NSArray arrayWithObjects:@" ", |
| 129 @"01", @"02", @"03", @"04", @"05", @"06", |
| 130 @"07", @"08", @"09", @"10", @"11", @"12", nil ]; |
| 131 |
| 132 [self setExpirationMonthContents:newArray]; |
| 133 |
| 134 // If the value from the model is not found in the array then set to the empty |
| 135 // item @" ". |
| 136 if ([newArray |
| 137 indexOfObject:[creditCardModel_ expirationMonth]] == NSNotFound) { |
| 138 [creditCardModel_ setExpirationMonth:@" "]; |
| 139 } |
| 140 |
| 141 // Disable first item in menu. @" " is a non-item. |
| 142 [[expirationMonthPopup_ itemAtIndex:0] setEnabled:NO]; |
| 143 } |
| 144 |
| 145 // Builds array of valid years. Uses special @" " to indicate no selection. |
| 146 - (void)buildExpirationYearContents { |
| 147 NSArray* newArray = [NSArray arrayWithObjects:@" ", |
| 148 @"2010", @"2011", @"2012", @"2013", @"2014", @"2015", |
| 149 @"2016", @"2017", @"2018", @"2019", @"2020", @"2021", nil ]; |
| 150 |
| 151 [self setExpirationYearContents:newArray]; |
| 152 |
| 153 // If the value from the model is not found in the array then set to the empty |
| 154 // item @" ". |
| 155 if ([newArray |
| 156 indexOfObject:[creditCardModel_ expirationYear]] == NSNotFound) { |
| 157 [creditCardModel_ setExpirationYear:@" "]; |
| 158 } |
| 159 |
| 160 // Disable first item in menu. @" " is a non-item. |
| 161 [[expirationYearPopup_ itemAtIndex:0] setEnabled:NO]; |
| 162 } |
| 163 |
| 164 @end |
| 165 |
OLD | NEW |