summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill/autofill_address_model_mac.mm
blob: 2e70050444ca41902b1c9f083d978262cc19a540 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 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_address_model_mac.h"
#include "app/l10n_util.h"
#include "base/sys_string_conversions.h"
#include "chrome/browser/autofill/autofill_profile.h"
#include "grit/generated_resources.h"

@implementation AutoFillAddressModel

@dynamic summary;
@synthesize label = label_;
@synthesize firstName = firstName_;
@synthesize middleName = middleName_;
@synthesize lastName = lastName_;
@synthesize email = email_;
@synthesize companyName = companyName_;
@synthesize addressLine1 = addressLine1_;
@synthesize addressLine2 = addressLine2_;
@synthesize city = city_;
@synthesize state = state_;
@synthesize zip = zip_;
@synthesize country = country_;
@synthesize phoneCountryCode = phoneCountryCode_;
@synthesize phoneAreaCode = phoneAreaCode_;
@synthesize phoneNumber = phoneNumber_;
@synthesize faxCountryCode = faxCountryCode_;
@synthesize faxAreaCode = faxAreaCode_;
@synthesize faxNumber = faxNumber_;

// Sets up the KVO dependency between "summary" and dependent fields.
+ (NSSet*)keyPathsForValuesAffectingValueForKey:(NSString*)key {
  NSSet* keyPaths = [super keyPathsForValuesAffectingValueForKey:key];

  if ([key isEqualToString:@"summary"]) {
    NSSet* affectingKeys =
        [NSSet setWithObjects:@"firstName", @"lastName", @"addressLine1", nil];
    keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKeys];
  }
  return keyPaths;
}

- (id)initWithProfile:(const AutoFillProfile&)profile {
  if ((self = [super init])) {
    [self setLabel:SysUTF16ToNSString(profile.Label())];
    [self setFirstName:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(NAME_FIRST)))];
    [self setMiddleName:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(NAME_MIDDLE)))];
    [self setLastName:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(NAME_LAST)))];
    [self setEmail:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(EMAIL_ADDRESS)))];
    [self setCompanyName:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(COMPANY_NAME)))];
    [self setAddressLine1:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(ADDRESS_HOME_LINE1)))];
    [self setAddressLine2:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(ADDRESS_HOME_LINE2)))];
    [self setCity:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(ADDRESS_HOME_CITY)))];
    [self setState:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(ADDRESS_HOME_STATE)))];
    [self setZip:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(ADDRESS_HOME_ZIP)))];
    [self setCountry:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(ADDRESS_HOME_COUNTRY)))];
    [self setPhoneCountryCode:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(PHONE_HOME_COUNTRY_CODE)))];
    [self setPhoneAreaCode:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(PHONE_HOME_CITY_CODE)))];
    [self setPhoneNumber:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(PHONE_HOME_NUMBER)))];
    [self setFaxCountryCode:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(PHONE_FAX_COUNTRY_CODE)))];
    [self setFaxAreaCode:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(PHONE_FAX_CITY_CODE)))];
    [self setFaxNumber:SysUTF16ToNSString(
        profile.GetFieldText(AutoFillType(PHONE_FAX_NUMBER)))];
  }
  return self;
}

- (void)dealloc {
  [label_ release];
  [firstName_ release];
  [middleName_ release];
  [lastName_ release];
  [email_ release];
  [companyName_ release];
  [addressLine1_ release];
  [addressLine2_ release];
  [city_ release];
  [state_ release];
  [zip_ release];
  [country_ release];
  [phoneCountryCode_ release];
  [phoneAreaCode_ release];
  [phoneNumber_ release];
  [faxCountryCode_ release];
  [faxAreaCode_ release];
  [faxNumber_ release];
  [super dealloc];
}

- (NSString*)summary {
  // Create a temporary |profile| to generate summary string.
  AutoFillProfile profile(string16(), 0);
  [self copyModelToProfile:&profile];
  return SysUTF16ToNSString(profile.PreviewSummary());
}

- (void)copyModelToProfile:(AutoFillProfile*)profile {
  DCHECK(profile);
  profile->set_label(base::SysNSStringToUTF16([self label]));
  profile->SetInfo(AutoFillType(NAME_FIRST),
      base::SysNSStringToUTF16([self firstName]));
  profile->SetInfo(AutoFillType(NAME_MIDDLE),
      base::SysNSStringToUTF16([self middleName]));
  profile->SetInfo(AutoFillType(NAME_LAST),
      base::SysNSStringToUTF16([self lastName]));
  profile->SetInfo(AutoFillType(EMAIL_ADDRESS),
      base::SysNSStringToUTF16([self email]));
  profile->SetInfo(AutoFillType(COMPANY_NAME),
      base::SysNSStringToUTF16([self companyName]));
  profile->SetInfo(AutoFillType(ADDRESS_HOME_LINE1),
      base::SysNSStringToUTF16([self addressLine1]));
  profile->SetInfo(AutoFillType(ADDRESS_HOME_LINE2),
      base::SysNSStringToUTF16([self addressLine2]));
  profile->SetInfo(AutoFillType(ADDRESS_HOME_CITY),
      base::SysNSStringToUTF16([self city]));
  profile->SetInfo(AutoFillType(ADDRESS_HOME_STATE),
      base::SysNSStringToUTF16([self state]));
  profile->SetInfo(AutoFillType(ADDRESS_HOME_ZIP),
      base::SysNSStringToUTF16([self zip]));
  profile->SetInfo(AutoFillType(ADDRESS_HOME_COUNTRY),
      base::SysNSStringToUTF16([self country]));
  profile->SetInfo(AutoFillType(PHONE_HOME_COUNTRY_CODE),
      base::SysNSStringToUTF16([self phoneCountryCode]));
  profile->SetInfo(AutoFillType(PHONE_HOME_CITY_CODE),
      base::SysNSStringToUTF16([self phoneAreaCode]));
  profile->SetInfo(AutoFillType(PHONE_HOME_NUMBER),
      base::SysNSStringToUTF16([self phoneNumber]));
  profile->SetInfo(AutoFillType(PHONE_FAX_COUNTRY_CODE),
      base::SysNSStringToUTF16([self faxCountryCode]));
  profile->SetInfo(AutoFillType(PHONE_FAX_CITY_CODE),
      base::SysNSStringToUTF16([self faxAreaCode]));
  profile->SetInfo(AutoFillType(PHONE_FAX_NUMBER),
      base::SysNSStringToUTF16([self faxNumber]));
}

@end