| OLD | NEW |
| (Empty) | |
| 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 "country_rules_retriever.h" |
| 16 |
| 17 #include <libaddressinput/address_field.h> |
| 18 #include <libaddressinput/callback.h> |
| 19 #include <libaddressinput/util/basictypes.h> |
| 20 #include <libaddressinput/util/scoped_ptr.h> |
| 21 |
| 22 #include <cassert> |
| 23 #include <cstddef> |
| 24 #include <string> |
| 25 |
| 26 #include "rule.h" |
| 27 #include "lookup_key.h" |
| 28 #include "rule_retriever.h" |
| 29 |
| 30 namespace i18n { |
| 31 namespace addressinput { |
| 32 |
| 33 namespace { |
| 34 |
| 35 class Helper { |
| 36 public: |
| 37 Helper(const std::string& country_code, |
| 38 const CountryRulesRetriever::Callback& rules_ready, |
| 39 const RuleRetriever& rule_retriever, |
| 40 LookupKey* root) |
| 41 : country_code_(country_code), |
| 42 rules_ready_(rules_ready), |
| 43 rule_retriever_(rule_retriever), |
| 44 root_(root), |
| 45 rule_ready_(BuildCallback(this, &Helper::OnRuleReady)), |
| 46 num_requested_(1), |
| 47 num_received_(0), |
| 48 success_(true), |
| 49 default_language_(), |
| 50 languages_() { |
| 51 assert(root_ != NULL); |
| 52 rule_retriever_.RetrieveRule(*root_, *rule_ready_); |
| 53 } |
| 54 |
| 55 private: |
| 56 ~Helper() {} |
| 57 |
| 58 void OnRuleReady(bool success, const LookupKey& key, const Rule& rule) { |
| 59 success_ &= success; |
| 60 ++num_received_; |
| 61 if (key.GetLevel() == COUNTRY && default_language_.empty()) { |
| 62 default_language_ = rule.GetLanguage(); |
| 63 languages_ = rule.GetLanguages(); |
| 64 } |
| 65 |
| 66 // TODO(rouslan): This rule copy can get expensive. If we decide to stick |
| 67 // with this design, then we should change the RuleRetriever to return a |
| 68 // pointer to the Rule object instead of a const-ref. |
| 69 Rule* rule_copy = new Rule(); |
| 70 rule_copy->CopyFrom(rule); |
| 71 |
| 72 // Takes ownership of |rule_copy|. |
| 73 // |
| 74 // TODO(rouslan): These const_casts seem unnecessary. Perhaps we should |
| 75 // change the callbacks to return non-constant references to keys. |
| 76 const_cast<LookupKey&>(key).SetRule(rule_copy); |
| 77 |
| 78 if (default_language_.empty() || rule.GetLanguage() == default_language_) { |
| 79 for (std::vector<std::string>::const_iterator\ |
| 80 lang_it = languages_.begin(); |
| 81 lang_it != languages_.end(); |
| 82 ++lang_it) { |
| 83 if (*lang_it != default_language_) { |
| 84 ++num_requested_; |
| 85 rule_retriever_.RetrieveRule( |
| 86 *const_cast<LookupKey&>(key).AddLanguageCodeKey(*lang_it), |
| 87 *rule_ready_); |
| 88 } |
| 89 } |
| 90 |
| 91 if (key.GetLevel() < DEPENDENT_LOCALITY) { |
| 92 for (std::vector<std::string>::const_iterator |
| 93 subkey_it = rule.GetSubKeys().begin(); |
| 94 subkey_it != rule.GetSubKeys().end(); |
| 95 ++subkey_it) { |
| 96 ++num_requested_; |
| 97 rule_retriever_.RetrieveRule( |
| 98 *const_cast<LookupKey&>(key).AddSubKey(*subkey_it), |
| 99 *rule_ready_); |
| 100 } |
| 101 } |
| 102 } |
| 103 |
| 104 if (num_received_ == num_requested_) { |
| 105 rules_ready_(success_, country_code_, *root_); |
| 106 delete this; |
| 107 } |
| 108 } |
| 109 |
| 110 const std::string country_code_; |
| 111 const CountryRulesRetriever::Callback& rules_ready_; |
| 112 const RuleRetriever& rule_retriever_; |
| 113 LookupKey* root_; |
| 114 scoped_ptr<RuleRetriever::Callback> rule_ready_; |
| 115 int num_requested_; |
| 116 int num_received_; |
| 117 bool success_; |
| 118 std::string default_language_; |
| 119 std::vector<std::string> languages_; |
| 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(Helper); |
| 122 }; |
| 123 |
| 124 } // namespace |
| 125 |
| 126 CountryRulesRetriever::CountryRulesRetriever( |
| 127 const RuleRetriever* rule_retriever) |
| 128 : rule_retriever_(rule_retriever) { |
| 129 assert(rule_retriever_ != NULL); |
| 130 } |
| 131 |
| 132 CountryRulesRetriever::~CountryRulesRetriever() {} |
| 133 |
| 134 void CountryRulesRetriever::RetrieveRules(const std::string& country_code, |
| 135 const Callback& rules_ready, |
| 136 LookupKey* lookup_key) const { |
| 137 new Helper(country_code, rules_ready, *rule_retriever_, lookup_key); |
| 138 } |
| 139 |
| 140 } // namespace addressinput |
| 141 } // namespace i18n |
| OLD | NEW |