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