1
0
Fork 0
mirror of https://gitlab.gnome.org/GNOME/calls.git synced 2024-07-04 16:09:29 +00:00

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

Fixes #345

(cherry picked from commit 4593a82b21)
This commit is contained in:
Evangelos Ribeiro Tzaras 2021-10-18 10:37:12 +02:00
parent 85d02dcfda
commit 53b645cf52
2 changed files with 68 additions and 45 deletions

View file

@ -302,9 +302,7 @@ void
calls_best_match_set_phone_number (CallsBestMatch *self, calls_best_match_set_phone_number (CallsBestMatch *self,
const char *phone_number) const char *phone_number)
{ {
g_autoptr (EPhoneNumber) number = NULL;
g_autoptr (CallsPhoneNumberQuery) query = NULL; g_autoptr (CallsPhoneNumberQuery) query = NULL;
g_autoptr (GError) error = NULL;
g_return_if_fail (CALLS_IS_BEST_MATCH (self)); g_return_if_fail (CALLS_IS_BEST_MATCH (self));
g_return_if_fail (phone_number); g_return_if_fail (phone_number);
@ -328,12 +326,7 @@ calls_best_match_set_phone_number (CallsBestMatch *self,
g_object_notify_by_pspec (G_OBJECT (self), props[PROP_PHONE_NUMBER]); g_object_notify_by_pspec (G_OBJECT (self), props[PROP_PHONE_NUMBER]);
return; return;
} }
number = e_phone_number_from_string (phone_number, self->country_code, &error); query = calls_phone_number_query_new (phone_number, self->country_code);
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)); self->view = folks_search_view_new (folks_individual_aggregator_dup (), FOLKS_QUERY (query));
g_signal_connect_swapped (self->view, g_signal_connect_swapped (self->view,
@ -345,7 +338,6 @@ calls_best_match_set_phone_number (CallsBestMatch *self,
(GAsyncReadyCallback) search_view_prepare_cb, (GAsyncReadyCallback) search_view_prepare_cb,
NULL); NULL);
} }
}
g_object_notify_by_pspec (G_OBJECT (self), props[PROP_PHONE_NUMBER]); g_object_notify_by_pspec (G_OBJECT (self), props[PROP_PHONE_NUMBER]);
} }

View file

@ -25,10 +25,11 @@
public class Calls.PhoneNumberQuery : Folks.Query public class Calls.PhoneNumberQuery : Folks.Query
{ {
private E.PhoneNumber _number; private string _number;
private E.PhoneNumber _ephonenumber;
private string _country_code; private string _country_code;
public PhoneNumberQuery (E.PhoneNumber number, string? country_code) public PhoneNumberQuery (string number, string? country_code)
{ {
string[] match_fields = string[] match_fields =
{ Folks.PersonaStore.detail_key (Folks.PersonaDetail.PHONE_NUMBERS) }; { Folks.PersonaStore.detail_key (Folks.PersonaDetail.PHONE_NUMBERS) };
@ -37,11 +38,25 @@ public class Calls.PhoneNumberQuery : Folks.Query
this._number = number; this._number = number;
this._country_code = country_code; 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) public override uint is_match (Folks.Individual individual)
{ {
const uint MATCH_MAX = 4; const uint MATCH_MAX = 4;
bool use_ephone = this._ephonenumber != null;
// Iterate over the set of phone numbers // Iterate over the set of phone numbers
Gee.Iterator<Folks.PhoneFieldDetails> iter = Gee.Iterator<Folks.PhoneFieldDetails> iter =
@ -52,29 +67,20 @@ public class Calls.PhoneNumberQuery : Folks.Query
// Get the phone number // Get the phone number
Folks.PhoneFieldDetails details = iter.get (); Folks.PhoneFieldDetails details = iter.get ();
string indiv_number = details.value; string indiv_number = details.value;
uint this_match = 0;
if (use_ephone)
{
// Parse it // Parse it
E.PhoneNumber indiv_parsed; E.PhoneNumber indiv_parsed;
try try
{ {
indiv_parsed = indiv_parsed =
E.PhoneNumber.from_string (indiv_number, this._country_code); E.PhoneNumber.from_string (indiv_number, this._country_code);
}
catch (GLib.Error e)
{
warning ("Error parsing Folks phone number `%s'" +
" for Individual `%s': %s",
indiv_number,
individual.display_name,
e.message);
continue;
}
// Compare the Individual's and query's numbers
E.PhoneNumberMatch result = E.PhoneNumberMatch result =
indiv_parsed.compare (this._number); indiv_parsed.compare (this._ephonenumber);
uint this_match;
switch (result) switch (result)
{ {
case E.PhoneNumberMatch.NONE: this_match = 0; break; case E.PhoneNumberMatch.NONE: this_match = 0; break;
@ -84,6 +90,31 @@ public class Calls.PhoneNumberQuery : Folks.Query
default: this_match = 0; break; default: this_match = 0; break;
} }
}
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
{
// Fall back to string comparison
if (this._number == indiv_number)
{
this_match = MATCH_MAX;
}
}
if (this_match > match) if (this_match > match)
{ {
match = this_match; match = this_match;