g_return_val_if_reached (E_PHONE_NUMBER_ERROR_UNKNOWN);
}
-EPhoneNumber *
-_e_phone_number_cxx_from_string (const gchar *phone_number,
- const gchar *region_code,
- GError **error)
+static std::string
+e_phone_number_make_region_code (const gchar *region_code)
{
- g_return_val_if_fail (NULL != phone_number, NULL);
-
- std::string valid_region;
-
/* Get country code from current locale's address facet if supported */
#if HAVE__NL_ADDRESS_COUNTRY_AB2
if (region_code == NULL || region_code[0] == '\0')
if (region_code == NULL || region_code[0] == '\0') {
/* From outside this is a C library, so we better consult the
* C infrastructure instead of std::locale, which might divert. */
- valid_region = setlocale (LC_ADDRESS, NULL);
+ std::string current_region = setlocale (LC_ADDRESS, NULL);
- const std::string::size_type underscore = valid_region.find ('_');
+ const std::string::size_type underscore = current_region.find ('_');
if (underscore != std::string::npos)
- valid_region.resize (underscore);
- } else {
- valid_region = region_code;
+ current_region.resize (underscore);
+
+ return current_region;
}
- /* Now finally parse the phone number */
+ return region_code;
+}
+
+gint
+_e_phone_number_cxx_get_country_code_for_region (const gchar *region_code)
+{
+ return e_phone_number_util_get_instance ()->GetCountryCodeForRegion (
+ e_phone_number_make_region_code (region_code));
+}
+
+gchar *
+_e_phone_number_cxx_get_default_region ()
+{
+ return g_strdup (e_phone_number_make_region_code (NULL).c_str ());
+}
+
+EPhoneNumber *
+_e_phone_number_cxx_from_string (const gchar *phone_number,
+ const gchar *region_code,
+ GError **error)
+{
+ g_return_val_if_fail (NULL != phone_number, NULL);
+
+ const std::string valid_region = e_phone_number_make_region_code (region_code);
std::auto_ptr<EPhoneNumber> parsed_number(new EPhoneNumber);
const PhoneNumberUtil::ErrorType err =
/* defined in e-phone-number-private.cpp, and used by by e-phone-number.c */
+E_PHONE_NUMBER_LOCAL gint _e_phone_number_cxx_get_country_code_for_region
+ (const gchar *region_code);
+E_PHONE_NUMBER_LOCAL gchar * _e_phone_number_cxx_get_default_region (void);
+
E_PHONE_NUMBER_LOCAL EPhoneNumber * _e_phone_number_cxx_from_string (const gchar *phone_number,
const gchar *region_code,
GError **error);
}
/**
+ * e_phone_number_get_country_code_for_region:
+ * @region_code: (allow-none): a 2-letter country code, or %NULL
+ *
+ * Returns the preferred country code for @region_code, e.g. 358 for "fi".
+ *
+ * If %NULL is passed for @region_code the default region as returned by
+ * e_phone_number_get_default_region() is used.
+ *
+ * Returns: a valid country code, or zero if an unknown region code was passed.
+ *
+ * Since: 3.8
+ */
+gint
+e_phone_number_get_country_code_for_region (const gchar *region_code)
+{
+#ifdef ENABLE_PHONENUMBER
+
+ return _e_phone_number_cxx_get_country_code_for_region (region_code);
+
+#else /* ENABLE_PHONENUMBER */
+
+ g_warning ("%s: The library was built without phone number support.", G_STRFUNC);
+ return 0;
+
+#endif /* ENABLE_PHONENUMBER */
+}
+
+/**
+ * e_phone_number_get_default_region:
+ *
+ * Retrieves the current 2-letter region code that's used by default for
+ * parsing phone numbers in e_phone_number_from_string(). It can be useful
+ * to store this number before parsing a bigger number of phone numbers.
+ *
+ * The result of this functions depends on the current setup of the
+ * %LC_ADDRESS category: If that category provides a reasonable value
+ * for %_NL_ADDRESS_COUNTRY_AB2 this value is returned. Otherwise the
+ * locale name configured for %LC_ADDRESS is parsed.
+ *
+ * Returns: (transfer full): a newly allocated string containing the
+ * current locale's 2-letter code for phone number parsing.
+ *
+ * Since: 3.8
+ */
+gchar *
+e_phone_number_get_default_region (void)
+{
+#ifdef ENABLE_PHONENUMBER
+
+ return _e_phone_number_cxx_get_default_region ();
+
+#else /* ENABLE_PHONENUMBER */
+
+ g_warning ("%s: The library was built without phone number support.", G_STRFUNC);
+ return NULL;
+
+#endif /* ENABLE_PHONENUMBER */
+}
+
+/**
* e_phone_number_from_string:
* @phone_number: the phone number to parse
* @region_code: (allow-none): a 2-letter country code, or %NULL
* region.
*
* The 2-letter country code passed in @region_code only is used if the
- * @phone_number is not written in international format. The applications's
- * currently locale is consulted if %NULL gets passed for @region_code.
+ * @phone_number is not written in international format. The application's
+ * default region as returned by e_phone_number_get_default_region() is used
+ * if @region_code is %NULL.
+ *
* If the number is guaranteed to start with a '+' followed by the country
- * calling code, then "ZZ" can be passed here.
+ * calling code, then "ZZ" can be passed for @region_code.
*
* Returns: (transfer full): a new EPhoneNumber instance on success,
* or %NULL on error. Call e_phone_number_free() to release this instance.
GQuark e_phone_number_error_quark (void);
gboolean e_phone_number_is_supported (void) G_GNUC_CONST;
+gint e_phone_number_get_country_code_for_region
+ (const gchar *region_code);
+gchar * e_phone_number_get_default_region
+ (void);
EPhoneNumber * e_phone_number_from_string (const gchar *phone_number,
const gchar *region_code,
EPhoneNumberFormat
EPhoneNumberMatch
e_phone_number_is_supported
+e_phone_number_get_country_code_for_region
+e_phone_number_get_default_region
e_phone_number_from_string
e_phone_number_to_string
e_phone_number_get_country_code
#endif /* ENABLE_PHONENUMBER */
}
+static void
+test_country_code_for_region (void)
+{
+ g_assert_cmpstr (setlocale (LC_ADDRESS, NULL), ==, "en_US.UTF-8");
+ g_assert_cmpint (e_phone_number_get_country_code_for_region ("CH"), ==, 41);
+ g_assert_cmpint (e_phone_number_get_country_code_for_region (NULL), ==, 1);
+ g_assert_cmpint (e_phone_number_get_country_code_for_region ("C"), ==, 0);
+ g_assert_cmpint (e_phone_number_get_country_code_for_region (""), ==, 1);
+}
+
gint
main (gint argc,
gchar **argv)
"+3587144556677", "+358 71 44556677", "071 44556677", "tel:+358-71-44556677"),
test_parse_and_format, parse_and_format_data_free);
- g_test_add_func
- ("/ebook-phone-number/parse-and-format/bad-number",
- test_parse_bad_number);
+ g_test_add_func (
+ "/ebook-phone-number/parse-and-format/bad-number",
+ test_parse_bad_number);
- g_test_add_func
- ("/ebook-phone-number/parse-and-format/auto-region",
- test_parse_auto_region);
+ g_test_add_func (
+ "/ebook-phone-number/parse-and-format/auto-region",
+ test_parse_auto_region);
g_assert_cmpint (G_N_ELEMENTS (match_candidates) * G_N_ELEMENTS (match_candidates),
==, G_N_ELEMENTS (expected_matches));
}
}
+ g_test_add_func (
+ "/ebook-phone-number/country-code/for-region",
+ test_country_code_for_region);
+
return g_test_run ();
}