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_view_controller_mac.h" | |
6 #include "app/l10n_util.h" | |
7 #include "base/mac_util.h" | |
8 #include "base/sys_string_conversions.h" | |
9 #import "chrome/browser/autofill/autofill_credit_card_model_mac.h" | |
10 #import "chrome/browser/autofill/autofill_dialog_controller_mac.h" | |
11 #include "chrome/browser/autofill/credit_card.h" | |
12 #include "grit/generated_resources.h" | |
13 | |
14 // Private methods for the |AutoFillCreditCardViewController| class. | |
15 @interface AutoFillCreditCardViewController (PrivateMethods) | |
16 - (void)rebuildBillingAddressContents; | |
17 - (void)rebuildShippingAddressContents; | |
18 @end | |
19 | |
20 @implementation AutoFillCreditCardViewController | |
21 | |
22 @synthesize creditCardModel = creditCardModel_; | |
23 @synthesize billingAddressContents = billingAddressContents_; | |
24 @synthesize shippingAddressContents = shippingAddressContents_; | |
25 | |
26 - (id)initWithCreditCard:(const CreditCard&)creditCard | |
27 disclosure:(NSCellStateValue)disclosureState | |
28 controller:(AutoFillDialogController*)parentController { | |
29 self = [super initWithNibName:@"AutoFillCreditCardFormView" | |
30 bundle:mac_util::MainAppBundle() | |
31 disclosure:disclosureState]; | |
32 if (self) { | |
33 // Pull in the view for initialization. | |
34 [self view]; | |
35 | |
36 // Create the model. We use setter here for KVO. | |
37 [self setCreditCardModel:[[[AutoFillCreditCardModel alloc] | |
38 initWithCreditCard:creditCard] autorelease]]; | |
39 | |
40 // We keep track of our parent controller for model-update purposes. | |
41 parentController_ = parentController; | |
42 | |
43 // Setup initial state of popups. | |
44 [self onAddressesChanged:self]; | |
45 } | |
46 return self; | |
47 } | |
48 | |
49 - (void)dealloc { | |
50 [creditCardModel_ release]; | |
51 [billingAddressContents_ release]; | |
52 [shippingAddressContents_ release]; | |
53 [super dealloc]; | |
54 } | |
55 | |
56 - (void)awakeFromNib { | |
57 [super awakeFromNib]; | |
58 | |
59 // Turn menu autoenable off. We manually govern this. | |
60 [billingAddressPopup_ setAutoenablesItems:NO]; | |
61 [shippingAddressPopup_ setAutoenablesItems:NO]; | |
62 } | |
63 | |
64 - (IBAction)deleteCreditCard:(id)sender { | |
65 [parentController_ deleteCreditCard:self]; | |
66 } | |
67 | |
68 - (IBAction)onAddressesChanged:(id)sender { | |
69 [self rebuildBillingAddressContents]; | |
70 [self rebuildShippingAddressContents]; | |
71 } | |
72 | |
73 - (void)copyModelToCreditCard:(CreditCard*)creditCard { | |
74 [creditCardModel_ copyModelToCreditCard:creditCard]; | |
75 | |
76 // The model copies the shipping and billing addresses blindly. We need | |
77 // to clear the strings in the case that our special menus are in effect. | |
78 if ([billingAddressPopup_ indexOfSelectedItem] <= 0) | |
79 creditCard->set_billing_address(string16()); | |
80 if ([shippingAddressPopup_ indexOfSelectedItem] <= 0) | |
81 creditCard->set_shipping_address(string16()); | |
82 } | |
83 | |
84 // Builds the |billingAddressContents_| array of strings from the list of | |
85 // addresses returned by the |parentController_| and additional UI string. | |
86 // Ensures that current selection is valid, if not reset it. | |
87 - (void)rebuildBillingAddressContents { | |
88 NSString* menuString = l10n_util::GetNSString( | |
89 IDS_AUTOFILL_DIALOG_CHOOSE_EXISTING_ADDRESS); | |
90 | |
91 // Build the menu array and set it. | |
92 NSArray* addressStrings = [parentController_ addressLabels]; | |
93 NSArray* newArray = [[NSArray arrayWithObject:menuString] | |
94 arrayByAddingObjectsFromArray:addressStrings]; | |
95 [self setBillingAddressContents:newArray]; | |
96 | |
97 // If the addresses no longer contain our selected item, reset the selection. | |
98 if ([addressStrings | |
99 indexOfObject:[creditCardModel_ billingAddress]] == NSNotFound) { | |
100 [creditCardModel_ setBillingAddress:menuString]; | |
101 } | |
102 | |
103 // Disable first item in menu. "Choose existing address" is a non-item. | |
104 [[billingAddressPopup_ itemAtIndex:0] setEnabled:NO]; | |
105 } | |
106 | |
107 // Builds the |shippingAddressContents_| array of strings from the list of | |
108 // addresses returned by the |parentController_| and additional UI string. | |
109 // Ensures that current selection is valid, if not reset it. | |
110 - (void)rebuildShippingAddressContents { | |
111 NSString* menuString = l10n_util::GetNSString( | |
112 IDS_AUTOFILL_DIALOG_SAME_AS_BILLING); | |
113 | |
114 // Build the menu array and set it. | |
115 NSArray* addressStrings = [parentController_ addressLabels]; | |
116 NSArray* newArray = [[NSArray arrayWithObject:menuString] | |
117 arrayByAddingObjectsFromArray:addressStrings]; | |
118 [self setShippingAddressContents:newArray]; | |
119 | |
120 // If the addresses no longer contain our selected item, reset the selection. | |
121 if ([addressStrings | |
122 indexOfObject:[creditCardModel_ shippingAddress]] == NSNotFound) { | |
123 [creditCardModel_ setShippingAddress:menuString]; | |
124 } | |
125 } | |
126 | |
127 @end | |
128 | |
OLD | NEW |