Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / libaddressinput / src / cpp / src / address_ui.cc
1 // Copyright (C) 2013 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <libaddressinput/address_ui.h>
16
17 #include <libaddressinput/address_field.h>
18 #include <libaddressinput/address_ui_component.h>
19 #include <libaddressinput/localization.h>
20
21 #include <cassert>
22 #include <cstddef>
23 #include <set>
24 #include <string>
25 #include <vector>
26
27 #include "format_element.h"
28 #include "grit.h"
29 #include "language.h"
30 #include "messages.h"
31 #include "region_data_constants.h"
32 #include "rule.h"
33
34 namespace i18n {
35 namespace addressinput {
36
37 namespace {
38
39 std::string GetLabelForField(const Localization& localization,
40                              AddressField field,
41                              int admin_area_name_message_id,
42                              int postal_code_name_message_id) {
43   int messageId;
44   switch (field) {
45     case SORTING_CODE:
46       // This needs no translation as it's used only in one locale.
47       return "CEDEX";
48     case COUNTRY:
49       messageId = IDS_LIBADDRESSINPUT_COUNTRY_OR_REGION_LABEL;
50       break;
51     case ADMIN_AREA:
52       messageId = admin_area_name_message_id;
53       break;
54     case LOCALITY:
55       messageId = IDS_LIBADDRESSINPUT_LOCALITY_LABEL;
56       break;
57     case DEPENDENT_LOCALITY:
58       messageId = IDS_LIBADDRESSINPUT_DISTRICT;
59       break;
60     case POSTAL_CODE:
61       messageId = postal_code_name_message_id;
62       break;
63     case STREET_ADDRESS:
64       messageId = IDS_LIBADDRESSINPUT_ADDRESS_LINE_1_LABEL;
65       break;
66     case ORGANIZATION:
67       messageId = IDS_LIBADDRESSINPUT_ORGANIZATION_LABEL;
68       break;
69     case RECIPIENT:
70       messageId = IDS_LIBADDRESSINPUT_RECIPIENT_LABEL;
71       break;
72     default:
73       messageId = INVALID_MESSAGE_ID;
74   }
75   return localization.GetString(messageId);
76 }
77
78 }  // namespace
79
80 const std::vector<std::string>& GetRegionCodes() {
81   return RegionDataConstants::GetRegionCodes();
82 }
83
84 std::vector<AddressUiComponent> BuildComponents(
85     const std::string& region_code,
86     const Localization& localization,
87     const std::string& ui_language_tag,
88     std::string* best_address_language_tag) {
89   assert(best_address_language_tag != NULL);
90   std::vector<AddressUiComponent> result;
91
92   Rule rule;
93   rule.CopyFrom(Rule::GetDefault());
94   if (!rule.ParseSerializedRule(
95           RegionDataConstants::GetRegionData(region_code))) {
96     return result;
97   }
98
99   const Language& best_address_language =
100       ChooseBestAddressLanguage(rule, Language(ui_language_tag));
101   *best_address_language_tag = best_address_language.tag;
102
103   const std::vector<FormatElement>& format =
104       !rule.GetLatinFormat().empty() && best_address_language.has_latin_script
105           ? rule.GetLatinFormat()
106           : rule.GetFormat();
107
108   // For avoiding showing an input field twice, when the field is displayed
109   // twice on an envelope.
110   std::set<AddressField> fields;
111
112   bool preceded_by_newline = true;
113   bool followed_by_newline = true;
114   for (std::vector<FormatElement>::const_iterator format_it = format.begin();
115        format_it != format.end(); ++format_it) {
116     if (format_it->IsNewline()) {
117       preceded_by_newline = true;
118       continue;
119     } else if (!format_it->IsField() ||
120                !fields.insert(format_it->GetField()).second) {
121       continue;
122     }
123     AddressUiComponent component;
124     std::vector<FormatElement>::const_iterator next_format_it = format_it + 1;
125     followed_by_newline =
126         next_format_it == format.end() || next_format_it->IsNewline();
127     component.length_hint = preceded_by_newline && followed_by_newline
128                                 ? AddressUiComponent::HINT_LONG
129                                 : AddressUiComponent::HINT_SHORT;
130     preceded_by_newline = false;
131     component.field = format_it->GetField();
132     component.name = GetLabelForField(localization,
133                                       format_it->GetField(),
134                                       rule.GetAdminAreaNameMessageId(),
135                                       rule.GetPostalCodeNameMessageId());
136     result.push_back(component);
137   }
138
139   return result;
140 }
141
142 }  // namespace addressinput
143 }  // namespace i18n