summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill/autofill_credit_card_sheet_controller_mac.mm
blob: ae72117828c83c677be786ab76195bac183c7465 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/autofill/autofill_credit_card_sheet_controller_mac.h"

#include "app/l10n_util.h"
#include "base/mac/mac_util.h"
#include "base/sys_string_conversions.h"
#import "chrome/browser/autofill/autofill_credit_card_model_mac.h"
#import "chrome/browser/autofill/autofill_dialog_controller_mac.h"
#include "chrome/browser/autofill/credit_card.h"
#include "grit/generated_resources.h"

// Interface exposed for unit testing.
@implementation AutoFillCreditCardSheetController (ExposedForUnitTests)
- (NSTextField*)creditCardNumberField {
  return creditCardNumberField_;
}
@end

// Private methods for the |AutoFillCreditCardSheetController| class.
@interface AutoFillCreditCardSheetController (PrivateMethods)
- (void)buildExpirationMonthContents;
- (void)buildExpirationYearContents;
@end

@implementation AutoFillCreditCardSheetController

@synthesize creditCardModel = creditCardModel_;
@synthesize expirationMonthContents = expirationMonthContents_;
@synthesize expirationYearContents = expirationYearContents_;

- (id)initWithCreditCard:(const CreditCard&)creditCard
                    mode:(AutoFillCreditCardMode)mode {
  NSString* nibPath = [base::mac::MainAppBundle()
                          pathForResource:@"AutoFillCreditCardSheet"
                                   ofType:@"nib"];
  self = [super initWithWindowNibPath:nibPath owner:self];
  if (self) {
    // Create the model.  We use setter here for KVO.
    [self setCreditCardModel:[[[AutoFillCreditCardModel alloc]
          initWithCreditCard:creditCard] autorelease]];

    mode_ = mode;
  }
  return self;
}

- (void)dealloc {
  [creditCardModel_ release];
  [expirationMonthContents_ release];
  [expirationYearContents_ release];
  [super dealloc];
}

- (void)awakeFromNib {
  // Setup initial state of popups.
  [self buildExpirationMonthContents];
  [self buildExpirationYearContents];

  // Turn menu autoenable off.  We manually govern this.
  [expirationMonthPopup_ setAutoenablesItems:NO];
  [expirationYearPopup_ setAutoenablesItems:NO];

  // Set the caption based on the mode.
  NSString* caption = @"";
  if (mode_ == kAutoFillCreditCardAddMode)
    caption = l10n_util::GetNSString(IDS_AUTOFILL_ADD_CREDITCARD_CAPTION);
  else if (mode_ == kAutoFillCreditCardEditMode)
    caption = l10n_util::GetNSString(IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION);
  else
    NOTREACHED();
  [caption_ setStringValue:caption];
}

- (IBAction)save:(id)sender {
  // Call |makeFirstResponder:| to commit pending text field edits.
  [[self window] makeFirstResponder:[self window]];

  [NSApp endSheet:[self window] returnCode:1];
}

- (IBAction)cancel:(id)sender {
  [NSApp endSheet:[self window] returnCode:0];
}

- (void)copyModelToCreditCard:(CreditCard*)creditCard {
  // The model copies the popup values blindly.  We need to clear the strings
  // in the case that our special menus are in effect.
  if ([expirationMonthPopup_ indexOfSelectedItem] <= 0)
    [creditCardModel_ setExpirationMonth:@""];
  if ([expirationYearPopup_ indexOfSelectedItem] <= 0)
    [creditCardModel_ setExpirationYear:@""];

  [creditCardModel_ copyModelToCreditCard:creditCard];
}

// Builds array of valid months.  Uses special @" " to indicate no selection.
- (void)buildExpirationMonthContents {
  NSArray* newArray = [NSArray arrayWithObjects:@" ",
      @"01", @"02", @"03", @"04", @"05", @"06",
      @"07", @"08", @"09", @"10", @"11", @"12", nil ];

  [self setExpirationMonthContents:newArray];

  // If the value from the model is not found in the array then set to the empty
  // item @" ".
  if ([newArray
        indexOfObject:[creditCardModel_ expirationMonth]] == NSNotFound) {
    [creditCardModel_ setExpirationMonth:@" "];
  }

  // Disable first item in menu.  @" " is a non-item.
  [[expirationMonthPopup_ itemAtIndex:0] setEnabled:NO];
}

// Builds array of valid years.  Uses special @" " to indicate no selection.
- (void)buildExpirationYearContents {
  NSArray* newArray = [NSArray arrayWithObjects:@" ",
      @"2010", @"2011", @"2012", @"2013", @"2014", @"2015",
      @"2016", @"2017", @"2018", @"2019", @"2020", nil ];

  [self setExpirationYearContents:newArray];

  // If the value from the model is not found in the array then set to the empty
  // item @" ".
  if ([newArray
        indexOfObject:[creditCardModel_ expirationYear]] == NSNotFound) {
    [creditCardModel_ setExpirationYear:@" "];
  }

  // Disable first item in menu.  @" " is a non-item.
  [[expirationYearPopup_ itemAtIndex:0] setEnabled:NO];
}

@end