[calls] number-query: Fall back to string comparison if parsing EPhoneNumber fails



commit 4593a82b21cf56d31a27f1969e091e92c143e735
Author: Evangelos Ribeiro Tzaras <devrtz fortysixandtwo eu>
Date:   Mon Oct 18 10:37:12 2021 +0200

    number-query: Fall back to string comparison if parsing EPhoneNumber fails
    
    Fixes #345

 src/calls-best-match.c            | 30 +++++---------
 src/calls-phone-number-query.vala | 85 ++++++++++++++++++++++++++-------------
 2 files changed, 69 insertions(+), 46 deletions(-)
---
diff --git a/src/calls-best-match.c b/src/calls-best-match.c
index b9d79d6f..11f6456f 100644
--- a/src/calls-best-match.c
+++ b/src/calls-best-match.c
@@ -302,9 +302,7 @@ void
 calls_best_match_set_phone_number (CallsBestMatch *self,
                                    const char     *phone_number)
 {
-  g_autoptr (EPhoneNumber) number = NULL;
   g_autoptr (CallsPhoneNumberQuery) query = NULL;
-  g_autoptr (GError) error = NULL;
 
   g_return_if_fail (CALLS_IS_BEST_MATCH (self));
   g_return_if_fail (phone_number);
@@ -328,23 +326,17 @@ calls_best_match_set_phone_number (CallsBestMatch *self,
       g_object_notify_by_pspec (G_OBJECT (self), props[PROP_PHONE_NUMBER]);
       return;
     }
-    number = e_phone_number_from_string (phone_number, self->country_code, &error);
-
-    if (!number) {
-      g_warning ("Failed to convert %s to a phone number: %s", phone_number, error->message);
-    } else {
-      query = calls_phone_number_query_new (number, self->country_code);
-      self->view = folks_search_view_new (folks_individual_aggregator_dup (), FOLKS_QUERY (query));
-
-      g_signal_connect_swapped (self->view,
-                                "individuals-changed-detailed",
-                                G_CALLBACK (update_best_match),
-                                self);
-
-      folks_search_view_prepare (FOLKS_SEARCH_VIEW (self->view),
-                                 (GAsyncReadyCallback) search_view_prepare_cb,
-                                 NULL);
-    }
+    query = calls_phone_number_query_new (phone_number, self->country_code);
+    self->view = folks_search_view_new (folks_individual_aggregator_dup (), FOLKS_QUERY (query));
+
+    g_signal_connect_swapped (self->view,
+                              "individuals-changed-detailed",
+                              G_CALLBACK (update_best_match),
+                              self);
+
+    folks_search_view_prepare (FOLKS_SEARCH_VIEW (self->view),
+                               (GAsyncReadyCallback) search_view_prepare_cb,
+                               NULL);
   }
 
   g_object_notify_by_pspec (G_OBJECT (self), props[PROP_PHONE_NUMBER]);
diff --git a/src/calls-phone-number-query.vala b/src/calls-phone-number-query.vala
index aa708660..543dba27 100644
--- a/src/calls-phone-number-query.vala
+++ b/src/calls-phone-number-query.vala
@@ -25,10 +25,11 @@
 
 public class Calls.PhoneNumberQuery : Folks.Query
 {
-    private E.PhoneNumber _number;
+    private string _number;
+    private E.PhoneNumber _ephonenumber;
     private string _country_code;
 
-    public PhoneNumberQuery (E.PhoneNumber number, string? country_code)
+    public PhoneNumberQuery (string number, string? country_code)
     {
         string[] match_fields =
         { Folks.PersonaStore.detail_key (Folks.PersonaDetail.PHONE_NUMBERS) };
@@ -37,11 +38,25 @@ public class Calls.PhoneNumberQuery : Folks.Query
 
         this._number = number;
         this._country_code = country_code;
+
+        try
+        {
+            this._ephonenumber =
+                E.PhoneNumber.from_string (this._number, this._country_code);
+        }
+        catch (GLib.Error e)
+        {
+        // Fall back to string comparison in this case
+        debug ("Failed to convert `%s' to a phone number: %s",
+               this._number,
+               e.message);
+        }
     }
 
     public override uint is_match (Folks.Individual individual)
     {
         const uint MATCH_MAX = 4;
+        bool use_ephone = this._ephonenumber != null;
 
         // Iterate over the set of phone numbers
         Gee.Iterator<Folks.PhoneFieldDetails> iter =
@@ -52,36 +67,52 @@ public class Calls.PhoneNumberQuery : Folks.Query
             // Get the phone number
             Folks.PhoneFieldDetails details = iter.get ();
             string indiv_number = details.value;
+            uint this_match = 0;
 
-            // Parse it
-            E.PhoneNumber indiv_parsed;
-            try
-            {
-                indiv_parsed =
-                    E.PhoneNumber.from_string (indiv_number, this._country_code);
-            }
-            catch (GLib.Error e)
+            if (use_ephone)
             {
-                warning ("Error parsing Folks phone number `%s'" +
-                         " for Individual `%s': %s",
-                         indiv_number,
-                         individual.display_name,
-                         e.message);
-                continue;
-            }
+                // Parse it
+                E.PhoneNumber indiv_parsed;
+                try
+                {
+                    indiv_parsed =
+                        E.PhoneNumber.from_string (indiv_number, this._country_code);
+
+                    E.PhoneNumberMatch result =
+                    indiv_parsed.compare (this._ephonenumber);
 
-            // Compare the Individual's and query's numbers
-            E.PhoneNumberMatch result =
-                indiv_parsed.compare (this._number);
+                    switch (result)
+                    {
+                    case E.PhoneNumberMatch.NONE:     this_match = 0; break;
+                    case E.PhoneNumberMatch.SHORT:    this_match = 0; break;
+                    case E.PhoneNumberMatch.NATIONAL: this_match = 1; break;
+                    case E.PhoneNumberMatch.EXACT:    this_match = MATCH_MAX; break;
+                    default:                          this_match = 0; break;
+                    }
 
-            uint this_match;
-            switch (result)
+                }
+                catch (GLib.Error e)
+                {
+                    debug ("Error parsing Folks phone number `%s'" +
+                           " for Individual `%s': %s",
+                           indiv_number,
+                           individual.display_name,
+                           e.message);
+
+                    if (this._number == indiv_number)
+                    {
+                        this_match = MATCH_MAX;
+                    }
+
+                }
+            }
+            else
             {
-            case E.PhoneNumberMatch.NONE:     this_match = 0; break;
-            case E.PhoneNumberMatch.SHORT:    this_match = 0; break;
-            case E.PhoneNumberMatch.NATIONAL: this_match = 1; break;
-            case E.PhoneNumberMatch.EXACT:    this_match = MATCH_MAX; break;
-            default:                          this_match = 0; break;
+                // Fall back to string comparison
+                if (this._number == indiv_number)
+                {
+                    this_match = MATCH_MAX;
+                }
             }
 
             if (this_match > match)


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