[evolution-data-server/openismus-phonenumber-work] libedataserver: Permit runtime detection of phone number support
- From: Mathias Hasselmann <hasselmm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [evolution-data-server/openismus-phonenumber-work] libedataserver: Permit runtime detection of phone number support
- Date: Wed, 5 Dec 2012 11:05:50 +0000 (UTC)
commit 753303dd5da6dcce0f5ae800b6821fac779c77b8
Author: Mathias Hasselmann <mathias openismus com>
Date: Wed Dec 5 12:02:25 2012 +0100
libedataserver: Permit runtime detection of phone number support
This adds e_phone_number_is_supported().
Also e_phone_number_from_string() now reports
E_PHONE_NUMBER_ERROR_NOT_IMPLEMENTED instead of showing
a critical message.
libedataserver/e-phone-utils.cpp | 103 +++++++++++++++++++---------
libedataserver/e-phone-utils.h | 20 +++++-
tests/libedataserver/Makefile.am | 14 ++--
tests/libedataserver/e-phone-utils-test.c | 56 ++++++++++++----
4 files changed, 138 insertions(+), 55 deletions(-)
---
diff --git a/libedataserver/e-phone-utils.cpp b/libedataserver/e-phone-utils.cpp
index de6ff3e..802739b 100644
--- a/libedataserver/e-phone-utils.cpp
+++ b/libedataserver/e-phone-utils.cpp
@@ -42,11 +42,13 @@
using i18n::phonenumbers::PhoneNumberUtil;
+#endif /* ENABLE_PHONENUMBER */
+
struct _EPhoneNumber {
+#ifdef ENABLE_PHONENUMBER
i18n::phonenumbers::PhoneNumber number;
-};
-
#endif /* ENABLE_PHONENUMBER */
+};
G_DEFINE_BOXED_TYPE (EPhoneNumber,
e_phone_number,
@@ -64,25 +66,6 @@ e_phone_number_error_quark (void)
return q;
}
-static const gchar *
-e_phone_number_error_to_string (EPhoneNumberError code)
-{
- switch (code) {
- case E_PHONE_NUMBER_ERROR_INVALID_COUNTRY_CODE:
- return _("Invalid country code");
- case E_PHONE_NUMBER_ERROR_NOT_A_NUMBER:
- return _("Not a phone number");
- case E_PHONE_NUMBER_ERROR_TOO_SHORT_AFTER_IDD:
- return _("Remaining text after the country code is to short for a phone number");
- case E_PHONE_NUMBER_ERROR_TOO_SHORT:
- return _("Text is too short for a phone number");
- case E_PHONE_NUMBER_ERROR_TOO_LONG:
- return _("Text is too long for a phone number");
- }
-
- return _("Unknown error");
-}
-
#ifdef ENABLE_PHONENUMBER
static PhoneNumberUtil *
@@ -104,8 +87,71 @@ e_phone_number_util_get_instance (void)
return instance;
}
+static EPhoneNumberError
+e_phone_number_error_code (PhoneNumberUtil::ErrorType error)
+{
+ switch (error) {
+ case PhoneNumberUtil::NO_PARSING_ERROR:
+ g_return_val_if_reached (E_PHONE_NUMBER_ERROR_UNKNOWN);
+ case PhoneNumberUtil::INVALID_COUNTRY_CODE_ERROR:
+ return E_PHONE_NUMBER_ERROR_INVALID_COUNTRY_CODE;
+ case PhoneNumberUtil::NOT_A_NUMBER:
+ return E_PHONE_NUMBER_ERROR_NOT_A_NUMBER;
+ case PhoneNumberUtil::TOO_SHORT_AFTER_IDD:
+ return E_PHONE_NUMBER_ERROR_TOO_SHORT_AFTER_IDD;
+ case PhoneNumberUtil::TOO_SHORT_NSN:
+ return E_PHONE_NUMBER_ERROR_TOO_SHORT;
+ case PhoneNumberUtil::TOO_LONG_NSN:
+ return E_PHONE_NUMBER_ERROR_TOO_LONG;
+ }
+
+ /* Please file a bug that we can add a proper error code. */
+ g_return_val_if_reached (E_PHONE_NUMBER_ERROR_UNKNOWN);
+}
+
#endif /* ENABLE_PHONENUMBER */
+static const gchar *
+e_phone_number_error_to_string (EPhoneNumberError code)
+{
+ switch (code) {
+ case E_PHONE_NUMBER_ERROR_NOT_IMPLEMENTED:
+ return _("The library was built without phone number support.");
+ case E_PHONE_NUMBER_ERROR_UNKNOWN:
+ return _("The phone number parser reported an yet unkown error code.");
+ case E_PHONE_NUMBER_ERROR_NOT_A_NUMBER:
+ return _("Not a phone number");
+ case E_PHONE_NUMBER_ERROR_INVALID_COUNTRY_CODE:
+ return _("Invalid country code");
+ case E_PHONE_NUMBER_ERROR_TOO_SHORT_AFTER_IDD:
+ return _("Remaining text after the country code is to short for a phone number");
+ case E_PHONE_NUMBER_ERROR_TOO_SHORT:
+ return _("Text is too short for a phone number");
+ case E_PHONE_NUMBER_ERROR_TOO_LONG:
+ return _("Text is too long for a phone number");
+ }
+
+ return _("Unknown error");
+}
+
+static void
+e_phone_number_set_error (GError **error,
+ EPhoneNumberError code)
+{
+ const gchar *message = e_phone_number_error_to_string (code);
+ g_set_error_literal (error, E_PHONE_NUMBER_ERROR, code, message);
+}
+
+gboolean
+e_phone_number_is_supported (void)
+{
+#ifdef ENABLE_PHONENUMBER
+ return TRUE;
+#else /* ENABLE_PHONENUMBER */
+ return FALSE;
+#endif /* ENABLE_PHONENUMBER */
+}
+
EPhoneNumber *
e_phone_number_from_string (const gchar *phone_number,
const gchar *country_code,
@@ -132,10 +178,7 @@ e_phone_number_from_string (const gchar *phone_number,
&parsed_number->number);
if (err != PhoneNumberUtil::NO_PARSING_ERROR) {
- const EPhoneNumberError code = static_cast<EPhoneNumberError> (err);
-
- g_set_error_literal (error, E_PHONE_NUMBER_ERROR, err,
- e_phone_number_error_to_string (code));
+ e_phone_number_set_error (error, e_phone_number_error_code (err));
delete parsed_number;
parsed_number = NULL;
@@ -143,8 +186,7 @@ e_phone_number_from_string (const gchar *phone_number,
#else /* ENABLE_PHONENUMBER */
- g_critical ("%s: This function is not available because phone number "
- "support was disabled when building %s.", G_STRFUNC, PACKAGE);
+ e_phone_number_set_error (error, E_PHONE_NUMBER_ERROR_NOT_IMPLEMENTED);
#endif /* ENABLE_PHONENUMBER */
@@ -171,8 +213,7 @@ e_phone_number_to_string (const EPhoneNumber *phone_number,
#else /* ENABLE_PHONENUMBER */
- g_critical ("%s: This function is not available because phone number "
- "support was disabled when building %s.", G_STRFUNC, PACKAGE);
+ g_warning ("%s: The library was built without phone number support.", G_STRFUNC);
#endif /* ENABLE_PHONENUMBER */
@@ -182,10 +223,8 @@ e_phone_number_to_string (const EPhoneNumber *phone_number,
EPhoneNumber *
e_phone_number_copy (const EPhoneNumber *phone_number)
{
-#ifdef ENABLE_PHONENUMBER
if (phone_number)
return new EPhoneNumber (*phone_number);
-#endif /* ENABLE_PHONENUMBER */
return NULL;
}
@@ -193,7 +232,5 @@ e_phone_number_copy (const EPhoneNumber *phone_number)
void
e_phone_number_free (EPhoneNumber *phone_number)
{
-#ifdef ENABLE_PHONENUMBER
delete phone_number;
-#endif /* ENABLE_PHONENUMBER */
}
diff --git a/libedataserver/e-phone-utils.h b/libedataserver/e-phone-utils.h
index 3b5fe26..e673d33 100644
--- a/libedataserver/e-phone-utils.h
+++ b/libedataserver/e-phone-utils.h
@@ -66,6 +66,10 @@ typedef enum {
/**
* EPhoneNumberError:
+ * @E_PHONE_NUMBER_ERROR_NOT_IMPLEMENTED: the library was built without phone
+ * number support
+ * @E_PHONE_NUMBER_ERROR_UNKNOWN: the phone number parser reported an yet
+ * unkown error code.
* @E_PHONE_NUMBER_ERROR_INVALID_COUNTRY_CODE: the supplied phone number has an
* invalid country code.
* @E_PHONE_NUMBER_ERROR_NOT_A_NUMBER: the supplied text is not a phone number.
@@ -77,8 +81,10 @@ typedef enum {
* Numeric description of a phone number related error.
**/
typedef enum {
- E_PHONE_NUMBER_ERROR_INVALID_COUNTRY_CODE = 1,
+ E_PHONE_NUMBER_ERROR_NOT_IMPLEMENTED,
+ E_PHONE_NUMBER_ERROR_UNKNOWN,
E_PHONE_NUMBER_ERROR_NOT_A_NUMBER,
+ E_PHONE_NUMBER_ERROR_INVALID_COUNTRY_CODE,
E_PHONE_NUMBER_ERROR_TOO_SHORT_AFTER_IDD,
E_PHONE_NUMBER_ERROR_TOO_SHORT,
E_PHONE_NUMBER_ERROR_TOO_LONG,
@@ -95,6 +101,18 @@ GType e_phone_number_get_type (void) G_GNUC_CONST;
GQuark e_phone_number_error_quark (void) G_GNUC_CONST;
/**
+ * e_phone_number_is_supported:
+ *
+ * Checks if phone number support is available. It is recommended to call this
+ * function before using any of the phone-utils functions to ensure that the
+ * required functionality is available, and to pick alternative mechnisms if
+ * needed.
+ *
+ * Returns: %TRUE if phone number support is available.
+ */
+gboolean e_phone_number_is_supported (void) G_GNUC_CONST;
+
+/**
* e_phone_number_from_string:
* @phone_number: the phone number to parse
* @country_code: (allow-none): a 2-letter country code, or %NULL
diff --git a/tests/libedataserver/Makefile.am b/tests/libedataserver/Makefile.am
index 37bae46..e5f1927 100644
--- a/tests/libedataserver/Makefile.am
+++ b/tests/libedataserver/Makefile.am
@@ -1,4 +1,7 @@
-TESTS = e-source-test
+TESTS = \
+ e-phone-utils-test \
+ e-source-test \
+ $(NULL)
noinst_PROGRAMS = $(TESTS)
@@ -14,13 +17,10 @@ TEST_LDADD = \
$(E_DATA_SERVER_LIBS) \
$(GIO_UNIX_LIBS)
-e_source_test_CPPFLAGS = $(TEST_CPPFLAGS)
-e_source_test_LDADD = $(TEST_LDADD)
-
-if ENABLE_PHONENUMBER
-TESTS += e-phone-utils-test
e_phone_utils_test_CPPFLAGS = $(TEST_CPPFLAGS)
e_phone_utils_test_LDADD = $(TEST_LDADD)
-endif ENABLE_PHONENUMBER
+
+e_source_test_CPPFLAGS = $(TEST_CPPFLAGS)
+e_source_test_LDADD = $(TEST_LDADD)
-include $(top_srcdir)/git.mk
diff --git a/tests/libedataserver/e-phone-utils-test.c b/tests/libedataserver/e-phone-utils-test.c
index fe58a08..c085cba 100644
--- a/tests/libedataserver/e-phone-utils-test.c
+++ b/tests/libedataserver/e-phone-utils-test.c
@@ -19,6 +19,10 @@
* Author: Mathias Hasselmann <mathias openismus com>
*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
#include <libedataserver/libedataserver.h>
static void
@@ -26,27 +30,47 @@ test_parse_and_format (gconstpointer data)
{
GError *error = NULL;
EPhoneNumber *parsed;
- gchar **test_numbers;
gchar **params;
- gchar *formatted;
- gint i;
params = g_strsplit (data, "/", G_MAXINT);
g_assert_cmpint (g_strv_length (params), ==, 6);
- test_numbers = params + 2;
parsed = e_phone_number_from_string (params[0], params[1], &error);
- g_assert (parsed != NULL);
- g_assert (error == NULL);
-
- for (i = 0; test_numbers[i]; ++i) {
- formatted = e_phone_number_to_string (parsed, i);
- g_assert (formatted != NULL);
- g_assert_cmpstr (formatted, ==, test_numbers[i]);
- g_free (formatted);
+
+#ifdef ENABLE_PHONENUMBER
+
+ {
+ gchar **test_numbers;
+ gint i;
+
+ test_numbers = params + 2;
+
+ g_assert (parsed != NULL);
+ g_assert (error == NULL);
+
+ for (i = 0; test_numbers[i]; ++i) {
+ gchar *formatted;
+
+ formatted = e_phone_number_to_string (parsed, i);
+ g_assert (formatted != NULL);
+ g_assert_cmpstr (formatted, ==, test_numbers[i]);
+ g_free (formatted);
+ }
+
+ e_phone_number_free (parsed);
}
- e_phone_number_free (parsed);
+#else /* ENABLE_PHONENUMBER */
+
+ g_assert (parsed == NULL);
+ g_assert (error != NULL);
+ g_assert (error->domain == E_PHONE_NUMBER_ERROR);
+ g_assert (error->code == E_PHONE_NUMBER_ERROR_NOT_IMPLEMENTED);
+ g_assert (error->message != NULL);
+
+#endif /* ENABLE_PHONENUMBER */
+
+ g_clear_error (&error);
g_strfreev (params);
}
@@ -61,10 +85,14 @@ test_parse_bad_number (void)
g_assert (parsed == NULL);
g_assert (error != NULL);
g_assert (error->domain == E_PHONE_NUMBER_ERROR);
+#ifdef ENABLE_PHONENUMBER
g_assert (error->code == E_PHONE_NUMBER_ERROR_NOT_A_NUMBER);
+#else /* ENABLE_PHONENUMBER */
+ g_assert (error->code == E_PHONE_NUMBER_ERROR_NOT_IMPLEMENTED);
+#endif /* ENABLE_PHONENUMBER */
g_assert (error->message != NULL);
- g_error_free (error);
+ g_clear_error (&error);
}
gint
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]