[evolution-data-server/openismus-work] libebook-contacts: Add phone number specific queries



commit 9d25a2148dbcc710b8fd751426bc261dc27c9735
Author: Mathias Hasselmann <mathias openismus com>
Date:   Wed Dec 12 00:09:24 2012 +0100

    libebook-contacts: Add phone number specific queries
    
    This adds new values to the EBookQueryTest enumeration:
    
     * E_BOOK_QUERY_EQUALS_PHONE_NUMBER
     * E_BOOK_QUERY_EQUALS_NATIONAL_PHONE_NUMBER
     * E_BOOK_QUERY_EQUALS_SHORT_PHONE_NUMBER
    
    This values get translated to the binary sexp functions "eqphone",
    "eqphone_national" and "eqphone_short".

 addressbook/libebook-contacts/e-book-query.c       |   66 ++++++++++++++---
 addressbook/libebook-contacts/e-book-query.h       |   18 +++++-
 addressbook/libedata-book/e-book-backend-sexp.c    |   75 +++++++++++++++++++-
 tests/libebook/client/test-client-custom-summary.c |    9 +++
 tests/libebook/data/vcards/custom-1.vcf            |    2 +-
 5 files changed, 154 insertions(+), 16 deletions(-)
---
diff --git a/addressbook/libebook-contacts/e-book-query.c b/addressbook/libebook-contacts/e-book-query.c
index 12e53f4..92cd348 100644
--- a/addressbook/libebook-contacts/e-book-query.c
+++ b/addressbook/libebook-contacts/e-book-query.c
@@ -519,6 +519,33 @@ func_endswith (struct _ESExp *f,
 }
 
 static ESExpResult *
+func_eqphone (struct _ESExp *f,
+              gint argc,
+              struct _ESExpResult **argv,
+              gpointer data)
+{
+	return func_field_test (E_BOOK_QUERY_EQUALS_PHONE_NUMBER, f, argc, argv, data);
+}
+
+static ESExpResult *
+func_eqphone_national (struct _ESExp *f,
+                       gint argc,
+                       struct _ESExpResult **argv,
+                       gpointer data)
+{
+	return func_field_test (E_BOOK_QUERY_EQUALS_NATIONAL_PHONE_NUMBER, f, argc, argv, data);
+}
+
+static ESExpResult *
+func_eqphone_short (struct _ESExp *f,
+                    gint argc,
+                    struct _ESExpResult **argv,
+                    gpointer data)
+{
+	return func_field_test (E_BOOK_QUERY_EQUALS_SHORT_PHONE_NUMBER, f, argc, argv, data);
+}
+
+static ESExpResult *
 func_exists (struct _ESExp *f,
              gint argc,
              struct _ESExpResult **argv,
@@ -558,6 +585,9 @@ static const struct {
 	{ "is", func_is, 0 },
 	{ "beginswith", func_beginswith, 0 },
 	{ "endswith", func_endswith, 0 },
+	{ "eqphone", func_eqphone, 0 },
+	{ "eqphone_national", func_eqphone_national, 0 },
+	{ "eqphone_short", func_eqphone_short, 0 },
 	{ "exists", func_exists, 0 },
 };
 
@@ -615,6 +645,29 @@ e_book_query_from_string (const gchar *query_string)
 	return retval;
 }
 
+static const char *
+field_test_name (EBookQueryTest field_test)
+{
+	switch (field_test) {
+	case E_BOOK_QUERY_IS:
+		return "is";
+	case E_BOOK_QUERY_CONTAINS:
+		return "contains";
+	case E_BOOK_QUERY_BEGINS_WITH:
+		return "beginswith";
+	case E_BOOK_QUERY_ENDS_WITH:
+		return "endswith";
+	case E_BOOK_QUERY_EQUALS_PHONE_NUMBER:
+		return "eqphone";
+	case E_BOOK_QUERY_EQUALS_NATIONAL_PHONE_NUMBER:
+		return "eqphone_national";
+	case E_BOOK_QUERY_EQUALS_SHORT_PHONE_NUMBER:
+		return "eqphone_short";
+	}
+
+	g_assert_not_reached ();
+}
+
 /**
  * e_book_query_to_string:
  * @q: an #EBookQuery
@@ -631,7 +684,6 @@ e_book_query_to_string (EBookQuery *q)
 	GString *encoded = g_string_new ("");
 	gint i;
 	gchar *s = NULL;
-	const gchar *cs;
 
 	switch (q->type) {
 	case E_BOOK_QUERY_TYPE_AND:
@@ -665,21 +717,11 @@ e_book_query_to_string (EBookQuery *q)
 		}
 		break;
 	case E_BOOK_QUERY_TYPE_FIELD_TEST:
-		switch (q->query.field_test.test) {
-		case E_BOOK_QUERY_IS: cs = "is"; break;
-		case E_BOOK_QUERY_CONTAINS: cs = "contains"; break;
-		case E_BOOK_QUERY_BEGINS_WITH: cs = "beginswith"; break;
-		case E_BOOK_QUERY_ENDS_WITH: cs = "endswith"; break;
-		default:
-			g_assert_not_reached ();
-			break;
-		}
-
 		e_sexp_encode_string (encoded, q->query.field_test.value);
 
 		g_string_append_printf (
 			str, "%s \"%s\" %s",
-			cs,
+			field_test_name (q->query.field_test.test),
 			q->query.field_test.field_name,
 			encoded->str);
 		break;
diff --git a/addressbook/libebook-contacts/e-book-query.h b/addressbook/libebook-contacts/e-book-query.h
index d5a842f..12a1662 100644
--- a/addressbook/libebook-contacts/e-book-query.h
+++ b/addressbook/libebook-contacts/e-book-query.h
@@ -20,14 +20,30 @@ typedef struct EBookQuery EBookQuery;
  * @E_BOOK_QUERY_CONTAINS: check if a field contains the test value
  * @E_BOOK_QUERY_BEGINS_WITH: check if a field starts with the test value
  * @E_BOOK_QUERY_ENDS_WITH: check if a field ends with the test value
+ * @E_BOOK_QUERY_EQUALS_PHONE_NUMBER: check that a field and the test value
+ * match exactly when interpreted as phone number, that is after stripping
+ * formatting like dashes, dots and spaces. See E_PHONE_NUMBER_MATCH_EXACT.
+ * @E_BOOK_QUERY_EQUALS_NATIONAL_PHONE_NUMBER: check that a field and the
+ * test value match when interpreted as phone number, except for the
+ * (omitted) country code.
+ * @E_BOOK_QUERY_EQUALS_SHORT_PHONE_NUMBER: check that a field and the test
+ * value match is the sense that both values appear to be phone numbers,
+ * and one might be a part (suffix) of the other.
  *
  * The kind of test a query created by e_book_query_field_test() shall perform.
+ *
+ * See also: E_PHONE_NUMBER_MATCH_EXACT, E_PHONE_NUMBER_MATCH_NATIONAL and
+ * E_PHONE_NUMBER_MATCH_SHORT.
  **/
 typedef enum {
   E_BOOK_QUERY_IS,
   E_BOOK_QUERY_CONTAINS,
   E_BOOK_QUERY_BEGINS_WITH,
-  E_BOOK_QUERY_ENDS_WITH
+  E_BOOK_QUERY_ENDS_WITH,
+
+  E_BOOK_QUERY_EQUALS_PHONE_NUMBER,
+  E_BOOK_QUERY_EQUALS_NATIONAL_PHONE_NUMBER,
+  E_BOOK_QUERY_EQUALS_SHORT_PHONE_NUMBER,
 
   /*
     Consider these "coming soon".
diff --git a/addressbook/libedata-book/e-book-backend-sexp.c b/addressbook/libedata-book/e-book-backend-sexp.c
index ef7ae97..3fac3b0 100644
--- a/addressbook/libedata-book/e-book-backend-sexp.c
+++ b/addressbook/libedata-book/e-book-backend-sexp.c
@@ -18,10 +18,12 @@
  * 02110-1301, USA.
  */
 
-#include <string.h>
-
 #include "e-book-backend-sexp.h"
 
+#include <libedataserver/libedataserver.h>
+
+#include <string.h>
+
 #define E_BOOK_BACKEND_SEXP_GET_PRIVATE(obj) \
 	(G_TYPE_INSTANCE_GET_PRIVATE \
 	((obj), E_TYPE_BOOK_BACKEND_SEXP, EBookBackendSExpPrivate))
@@ -744,6 +746,72 @@ func_beginswith (struct _ESExp *f,
 }
 
 static gboolean
+eqphone_helper (const gchar *ps1,
+                const gchar *ps2,
+                EPhoneNumberMatch required_match)
+{
+	const EPhoneNumberMatch actual_match =
+		e_phone_number_compare_strings (ps1, ps2, NULL);
+
+	return actual_match >= E_PHONE_NUMBER_MATCH_EXACT
+		&& actual_match <= required_match;
+}
+
+static gboolean
+eqphone_exact_helper (const gchar *ps1,
+                      const gchar *ps2)
+{
+	return eqphone_helper (ps1, ps2, E_PHONE_NUMBER_MATCH_EXACT);
+}
+
+static gboolean
+eqphone_national_helper (const gchar *ps1,
+                         const gchar *ps2)
+{
+	return eqphone_helper (ps1, ps2, E_PHONE_NUMBER_MATCH_NATIONAL);
+}
+
+static gboolean
+eqphone_short_helper (const gchar *ps1,
+                      const gchar *ps2)
+{
+	return eqphone_helper (ps1, ps2, E_PHONE_NUMBER_MATCH_SHORT);
+}
+
+static ESExpResult *
+func_eqphone (struct _ESExp *f,
+              gint argc,
+              struct _ESExpResult **argv,
+              gpointer data)
+{
+	SearchContext *ctx = data;
+
+	return entry_compare (ctx, f, argc, argv, eqphone_exact_helper);
+}
+
+static ESExpResult *
+func_eqphone_national (struct _ESExp *f,
+                       gint argc,
+                       struct _ESExpResult **argv,
+                       gpointer data)
+{
+	SearchContext *ctx = data;
+
+	return entry_compare (ctx, f, argc, argv, eqphone_national_helper);
+}
+
+static ESExpResult *
+func_eqphone_short (struct _ESExp *f,
+                    gint argc,
+                    struct _ESExpResult **argv,
+                    gpointer data)
+{
+	SearchContext *ctx = data;
+
+	return entry_compare (ctx, f, argc, argv, eqphone_short_helper);
+}
+
+static gboolean
 exists_helper (const gchar *ps1,
                const gchar *ps2)
 {
@@ -894,6 +962,9 @@ static struct {
 	{ "is", func_is, 0 },
 	{ "beginswith", func_beginswith, 0 },
 	{ "endswith", func_endswith, 0 },
+	{ "eqphone", func_eqphone, 0 },
+	{ "eqphone_national", func_eqphone_national, 0 },
+	{ "eqphone_short", func_eqphone_short, 0 },
 	{ "exists", func_exists, 0 },
 	{ "exists_vcard", func_exists_vcard, 0 },
 };
diff --git a/tests/libebook/client/test-client-custom-summary.c b/tests/libebook/client/test-client-custom-summary.c
index 04b139d..553dbfb 100644
--- a/tests/libebook/client/test-client-custom-summary.c
+++ b/tests/libebook/client/test-client-custom-summary.c
@@ -218,6 +218,15 @@ main (gint argc,
 	add_client_test ("/client/search/exact/name", search_test, book_client,
 	                 e_book_query_vcard_field_test(EVC_N, E_BOOK_QUERY_IS, "Janet"),
 	                 1);
+	add_client_test ("/client/search/eqphone/exact/phone", search_test, book_client,
+	                 e_book_query_vcard_field_test(EVC_TEL, E_BOOK_QUERY_EQUALS_PHONE_NUMBER, "+1 221.542.3789"),
+	                 1);
+	add_client_test ("/client/search/eqphone/national/phone", search_test, book_client,
+	                 e_book_query_vcard_field_test(EVC_TEL, E_BOOK_QUERY_EQUALS_NATIONAL_PHONE_NUMBER, "221.542.3789"),
+	                 1);
+	add_client_test ("/client/search/eqphone/short/phone", search_test, book_client,
+	                 e_book_query_vcard_field_test(EVC_TEL, E_BOOK_QUERY_EQUALS_SHORT_PHONE_NUMBER, "5423789"),
+	                 1);
 
 	/* Add search tests that fetch uids */
 	add_client_test ("/client/search-uid/exact/name", uid_test, book_client,
diff --git a/tests/libebook/data/vcards/custom-1.vcf b/tests/libebook/data/vcards/custom-1.vcf
index bc9a23c..6d7241b 100644
--- a/tests/libebook/data/vcards/custom-1.vcf
+++ b/tests/libebook/data/vcards/custom-1.vcf
@@ -1,5 +1,5 @@
 BEGIN:VCARD
 FN:Micheal Jackson
-TEL;HOME:+1234567
+TEL;HOME:+1-221-5423789
 EMAIL;TYPE=home,work:micheal jackson com
 END:VCARD



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]