OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "chrome/browser/autofill/contact_info.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <ostream> | |
9 #include <string> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/logging.h" | |
13 #include "base/string_util.h" | |
14 #include "base/utf_string_conversions.h" | |
15 #include "chrome/browser/autofill/autofill_type.h" | |
16 #include "chrome/browser/autofill/field_types.h" | |
17 | |
18 static const AutofillFieldType kAutofillNameInfoTypes[] = { | |
19 NAME_FIRST, | |
20 NAME_MIDDLE, | |
21 NAME_LAST | |
22 }; | |
23 | |
24 static const size_t kAutofillNameInfoLength = | |
25 arraysize(kAutofillNameInfoTypes); | |
26 | |
27 NameInfo::NameInfo() {} | |
28 | |
29 NameInfo::NameInfo(const NameInfo& info) : FormGroup() { | |
30 *this = info; | |
31 } | |
32 | |
33 NameInfo::~NameInfo() {} | |
34 | |
35 NameInfo& NameInfo::operator=(const NameInfo& info) { | |
36 if (this == &info) | |
37 return *this; | |
38 | |
39 first_ = info.first_; | |
40 middle_ = info.middle_; | |
41 last_ = info.last_; | |
42 return *this; | |
43 } | |
44 | |
45 void NameInfo::GetSupportedTypes(FieldTypeSet* supported_types) const { | |
46 supported_types->insert(NAME_FIRST); | |
47 supported_types->insert(NAME_MIDDLE); | |
48 supported_types->insert(NAME_LAST); | |
49 supported_types->insert(NAME_MIDDLE_INITIAL); | |
50 supported_types->insert(NAME_FULL); | |
51 } | |
52 | |
53 string16 NameInfo::GetRawInfo(AutofillFieldType type) const { | |
54 if (type == NAME_FIRST) | |
55 return first(); | |
56 | |
57 if (type == NAME_MIDDLE) | |
58 return middle(); | |
59 | |
60 if (type == NAME_LAST) | |
61 return last(); | |
62 | |
63 if (type == NAME_MIDDLE_INITIAL) | |
64 return MiddleInitial(); | |
65 | |
66 if (type == NAME_FULL) | |
67 return FullName(); | |
68 | |
69 return string16(); | |
70 } | |
71 | |
72 void NameInfo::SetRawInfo(AutofillFieldType type, const string16& value) { | |
73 DCHECK_EQ(AutofillType::NAME, AutofillType(type).group()); | |
74 if (type == NAME_FIRST) | |
75 first_ = value; | |
76 else if (type == NAME_MIDDLE || type == NAME_MIDDLE_INITIAL) | |
77 middle_ = value; | |
78 else if (type == NAME_LAST) | |
79 last_ = value; | |
80 else if (type == NAME_FULL) | |
81 SetFullName(value); | |
82 else | |
83 NOTREACHED(); | |
84 } | |
85 | |
86 string16 NameInfo::FullName() const { | |
87 std::vector<string16> full_name; | |
88 if (!first_.empty()) | |
89 full_name.push_back(first_); | |
90 | |
91 if (!middle_.empty()) | |
92 full_name.push_back(middle_); | |
93 | |
94 if (!last_.empty()) | |
95 full_name.push_back(last_); | |
96 | |
97 return JoinString(full_name, ' '); | |
98 } | |
99 | |
100 string16 NameInfo::MiddleInitial() const { | |
101 if (middle_.empty()) | |
102 return string16(); | |
103 | |
104 string16 middle_name(middle()); | |
105 string16 initial; | |
106 initial.push_back(middle_name[0]); | |
107 return initial; | |
108 } | |
109 | |
110 void NameInfo::SetFullName(const string16& full) { | |
111 // Clear the names. | |
112 first_ = string16(); | |
113 middle_ = string16(); | |
114 last_ = string16(); | |
115 | |
116 std::vector<string16> full_name_tokens; | |
117 Tokenize(full, ASCIIToUTF16(" "), &full_name_tokens); | |
118 | |
119 // There are four possibilities: empty; first name; first and last names; | |
120 // first, middle (possibly multiple strings) and then the last name. | |
121 if (full_name_tokens.size() > 0) { | |
122 first_ = full_name_tokens[0]; | |
123 if (full_name_tokens.size() > 1) { | |
124 last_ = full_name_tokens.back(); | |
125 if (full_name_tokens.size() > 2) { | |
126 full_name_tokens.erase(full_name_tokens.begin()); | |
127 full_name_tokens.pop_back(); | |
128 middle_ = JoinString(full_name_tokens, ' '); | |
129 } | |
130 } | |
131 } | |
132 } | |
133 | |
134 EmailInfo::EmailInfo() {} | |
135 | |
136 EmailInfo::EmailInfo(const EmailInfo& info) : FormGroup() { | |
137 *this = info; | |
138 } | |
139 | |
140 EmailInfo::~EmailInfo() {} | |
141 | |
142 EmailInfo& EmailInfo::operator=(const EmailInfo& info) { | |
143 if (this == &info) | |
144 return *this; | |
145 | |
146 email_ = info.email_; | |
147 return *this; | |
148 } | |
149 | |
150 void EmailInfo::GetSupportedTypes(FieldTypeSet* supported_types) const { | |
151 supported_types->insert(EMAIL_ADDRESS); | |
152 } | |
153 | |
154 string16 EmailInfo::GetRawInfo(AutofillFieldType type) const { | |
155 if (type == EMAIL_ADDRESS) | |
156 return email_; | |
157 | |
158 return string16(); | |
159 } | |
160 | |
161 void EmailInfo::SetRawInfo(AutofillFieldType type, const string16& value) { | |
162 DCHECK_EQ(EMAIL_ADDRESS, type); | |
163 email_ = value; | |
164 } | |
165 | |
166 CompanyInfo::CompanyInfo() {} | |
167 | |
168 CompanyInfo::CompanyInfo(const CompanyInfo& info) : FormGroup() { | |
169 *this = info; | |
170 } | |
171 | |
172 CompanyInfo::~CompanyInfo() {} | |
173 | |
174 CompanyInfo& CompanyInfo::operator=(const CompanyInfo& info) { | |
175 if (this == &info) | |
176 return *this; | |
177 | |
178 company_name_ = info.company_name_; | |
179 return *this; | |
180 } | |
181 | |
182 void CompanyInfo::GetSupportedTypes(FieldTypeSet* supported_types) const { | |
183 supported_types->insert(COMPANY_NAME); | |
184 } | |
185 | |
186 string16 CompanyInfo::GetRawInfo(AutofillFieldType type) const { | |
187 if (type == COMPANY_NAME) | |
188 return company_name_; | |
189 | |
190 return string16(); | |
191 } | |
192 | |
193 void CompanyInfo::SetRawInfo(AutofillFieldType type, const string16& value) { | |
194 DCHECK_EQ(COMPANY_NAME, type); | |
195 company_name_ = value; | |
196 } | |
OLD | NEW |