2019-12-09 14:51:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 Purism SPC
|
|
|
|
*
|
|
|
|
* This file is part of Calls.
|
|
|
|
*
|
|
|
|
* Calls is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Calls is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calls. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Bob Ham <bob.ham@puri.sm>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "calls-best-match.h"
|
2021-01-28 13:42:09 +00:00
|
|
|
#include "calls-contacts-provider.h"
|
2021-03-25 19:37:25 +00:00
|
|
|
#include "calls-manager.h"
|
2021-01-28 14:17:30 +00:00
|
|
|
#include "calls-vala.h"
|
2019-12-09 14:51:21 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
|
|
|
|
|
|
|
|
struct _CallsBestMatch
|
|
|
|
{
|
|
|
|
GObject parent_instance;
|
|
|
|
|
2021-01-28 14:17:30 +00:00
|
|
|
FolksSearchView *view;
|
2021-11-18 10:07:38 +00:00
|
|
|
FolksIndividual *matched_individual;
|
2021-06-30 21:23:11 +00:00
|
|
|
char *phone_number;
|
|
|
|
char *country_code;
|
2021-07-01 00:01:32 +00:00
|
|
|
char *name_sip;
|
2021-04-05 02:55:55 +00:00
|
|
|
gboolean had_country_code_last_time;
|
2019-12-09 14:51:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (CallsBestMatch, calls_best_match, G_TYPE_OBJECT);
|
|
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PROP_0,
|
2021-01-28 13:42:09 +00:00
|
|
|
PROP_PHONE_NUMBER,
|
2019-12-09 14:51:21 +00:00
|
|
|
PROP_NAME,
|
2021-01-27 15:14:31 +00:00
|
|
|
PROP_AVATAR,
|
2021-01-28 15:31:38 +00:00
|
|
|
PROP_HAS_INDIVIDUAL,
|
2021-04-05 02:55:55 +00:00
|
|
|
PROP_COUNTRY_CODE,
|
2019-12-09 14:51:21 +00:00
|
|
|
PROP_LAST_PROP,
|
|
|
|
};
|
|
|
|
static GParamSpec *props[PROP_LAST_PROP];
|
|
|
|
|
2021-01-28 13:42:09 +00:00
|
|
|
static void
|
|
|
|
search_view_prepare_cb (FolksSearchView *view,
|
|
|
|
GAsyncResult *res,
|
|
|
|
gpointer *user_data)
|
|
|
|
{
|
|
|
|
g_autoptr (GError) error = NULL;
|
|
|
|
|
|
|
|
folks_search_view_prepare_finish (view, res, &error);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
g_warning ("Failed to prepare Folks search view: %s", error->message);
|
|
|
|
}
|
|
|
|
|
2021-09-24 03:02:45 +00:00
|
|
|
|
2019-12-09 14:51:21 +00:00
|
|
|
static void
|
|
|
|
notify_name (CallsBestMatch *self)
|
|
|
|
{
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self),
|
|
|
|
props[PROP_NAME]);
|
|
|
|
}
|
|
|
|
|
2021-09-24 03:02:45 +00:00
|
|
|
|
2019-12-09 14:51:21 +00:00
|
|
|
static void
|
2021-01-27 15:14:31 +00:00
|
|
|
notify_avatar (CallsBestMatch *self)
|
2019-12-09 14:51:21 +00:00
|
|
|
{
|
2021-01-27 15:14:31 +00:00
|
|
|
g_object_notify_by_pspec (G_OBJECT (self),
|
|
|
|
props[PROP_AVATAR]);
|
2019-12-09 14:51:21 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 03:02:45 +00:00
|
|
|
|
2019-12-09 14:51:21 +00:00
|
|
|
static void
|
2021-01-28 13:58:49 +00:00
|
|
|
update_best_match (CallsBestMatch *self)
|
2019-12-09 14:51:21 +00:00
|
|
|
{
|
2021-09-24 03:00:39 +00:00
|
|
|
GeeSortedSet *individuals = folks_search_view_get_individuals (self->view);
|
2021-11-18 10:07:38 +00:00
|
|
|
FolksIndividual *matched_individual = NULL;
|
2021-01-28 15:31:38 +00:00
|
|
|
gboolean notify_has_individual = FALSE;
|
2021-01-28 14:17:30 +00:00
|
|
|
|
2021-04-05 04:06:59 +00:00
|
|
|
g_return_if_fail (GEE_IS_COLLECTION (individuals));
|
|
|
|
|
2021-01-28 14:17:30 +00:00
|
|
|
if (!gee_collection_get_is_empty (GEE_COLLECTION (individuals)))
|
2021-11-18 10:07:38 +00:00
|
|
|
matched_individual = gee_sorted_set_first (individuals);
|
2021-01-28 13:58:49 +00:00
|
|
|
|
2021-11-18 10:07:38 +00:00
|
|
|
if (matched_individual == self->matched_individual)
|
2021-01-28 13:58:49 +00:00
|
|
|
return;
|
2019-12-09 14:51:21 +00:00
|
|
|
|
2021-11-18 10:07:38 +00:00
|
|
|
if (self->matched_individual) {
|
|
|
|
g_signal_handlers_disconnect_by_data (self->matched_individual, self);
|
|
|
|
g_clear_object (&self->matched_individual);
|
2021-01-28 15:31:38 +00:00
|
|
|
notify_has_individual = TRUE;
|
2021-01-28 13:58:49 +00:00
|
|
|
}
|
2019-12-09 14:51:21 +00:00
|
|
|
|
2021-11-18 10:07:38 +00:00
|
|
|
if (matched_individual) {
|
|
|
|
g_set_object (&self->matched_individual, matched_individual);
|
|
|
|
g_signal_connect_swapped (self->matched_individual,
|
2019-12-09 14:51:21 +00:00
|
|
|
"notify::display-name",
|
|
|
|
G_CALLBACK (notify_name),
|
|
|
|
self);
|
2021-11-18 10:07:38 +00:00
|
|
|
g_signal_connect_swapped (self->matched_individual,
|
2019-12-09 14:51:21 +00:00
|
|
|
"notify::avatar",
|
2021-01-27 15:14:31 +00:00
|
|
|
G_CALLBACK (notify_avatar),
|
2019-12-09 14:51:21 +00:00
|
|
|
self);
|
2021-01-28 15:31:38 +00:00
|
|
|
notify_has_individual = TRUE;
|
2021-01-28 13:58:49 +00:00
|
|
|
}
|
2019-12-09 14:51:21 +00:00
|
|
|
|
|
|
|
notify_name (self);
|
2021-01-27 15:14:31 +00:00
|
|
|
notify_avatar (self);
|
2021-01-28 15:31:38 +00:00
|
|
|
if (notify_has_individual)
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), props[PROP_HAS_INDIVIDUAL]);
|
2019-12-09 14:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
CallsBestMatch *self = CALLS_BEST_MATCH (object);
|
2021-06-30 21:23:11 +00:00
|
|
|
|
|
|
|
switch (property_id) {
|
|
|
|
case PROP_PHONE_NUMBER:
|
|
|
|
calls_best_match_set_phone_number (self, g_value_get_string (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_COUNTRY_CODE:
|
2021-09-23 07:32:37 +00:00
|
|
|
g_free (self->country_code);
|
|
|
|
self->country_code = g_value_dup_string (value);
|
|
|
|
|
|
|
|
if (self->phone_number) {
|
|
|
|
g_autofree char *number = g_strdup (self->phone_number);
|
|
|
|
calls_best_match_set_phone_number (self, number);
|
2019-12-09 14:51:21 +00:00
|
|
|
}
|
2021-06-30 21:23:11 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2019-12-09 14:51:21 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 03:02:45 +00:00
|
|
|
|
2019-12-09 14:51:21 +00:00
|
|
|
static void
|
|
|
|
get_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
CallsBestMatch *self = CALLS_BEST_MATCH (object);
|
|
|
|
|
2021-06-30 21:23:11 +00:00
|
|
|
switch (property_id) {
|
|
|
|
case PROP_HAS_INDIVIDUAL:
|
|
|
|
g_value_set_boolean (value,
|
|
|
|
calls_best_match_has_individual (self));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_PHONE_NUMBER:
|
|
|
|
g_value_set_string (value,
|
|
|
|
calls_best_match_get_phone_number (self));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_COUNTRY_CODE:
|
|
|
|
g_value_set_string (value, self->country_code);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_NAME:
|
|
|
|
g_value_set_string (value,
|
|
|
|
calls_best_match_get_name (self));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_AVATAR:
|
|
|
|
g_value_set_object (value,
|
|
|
|
calls_best_match_get_avatar (self));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2019-12-09 14:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
dispose (GObject *object)
|
|
|
|
{
|
|
|
|
CallsBestMatch *self = CALLS_BEST_MATCH (object);
|
|
|
|
|
|
|
|
g_clear_object (&self->view);
|
2021-01-28 13:42:09 +00:00
|
|
|
g_clear_pointer (&self->phone_number, g_free);
|
2021-04-05 02:55:55 +00:00
|
|
|
g_clear_pointer (&self->country_code, g_free);
|
2021-07-01 00:01:32 +00:00
|
|
|
g_clear_pointer (&self->name_sip, g_free);
|
2019-12-09 14:51:21 +00:00
|
|
|
|
2021-11-18 10:07:38 +00:00
|
|
|
if (self->matched_individual) {
|
|
|
|
g_signal_handlers_disconnect_by_data (self->matched_individual, self);
|
|
|
|
g_clear_object (&self->matched_individual);
|
2021-01-28 13:58:49 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 14:51:21 +00:00
|
|
|
G_OBJECT_CLASS (calls_best_match_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
calls_best_match_class_init (CallsBestMatchClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->set_property = set_property;
|
|
|
|
object_class->get_property = get_property;
|
|
|
|
object_class->dispose = dispose;
|
|
|
|
|
2021-01-28 15:31:38 +00:00
|
|
|
props[PROP_HAS_INDIVIDUAL] =
|
|
|
|
g_param_spec_boolean ("has-individual",
|
|
|
|
"Has individual",
|
|
|
|
"Whether a matching individual was found or not",
|
|
|
|
FALSE,
|
|
|
|
G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);
|
|
|
|
|
2021-01-28 13:42:09 +00:00
|
|
|
props[PROP_PHONE_NUMBER] =
|
|
|
|
g_param_spec_string ("phone_number",
|
|
|
|
"Phone number",
|
|
|
|
"The phone number of the best match",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
|
2019-12-09 14:51:21 +00:00
|
|
|
|
2021-04-05 02:55:55 +00:00
|
|
|
props[PROP_COUNTRY_CODE] =
|
|
|
|
g_param_spec_string ("country-code",
|
|
|
|
"Country code",
|
|
|
|
"The country code used for matching",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
|
|
|
|
|
2019-12-09 14:51:21 +00:00
|
|
|
props[PROP_NAME] =
|
|
|
|
g_param_spec_string ("name",
|
2020-05-29 05:38:11 +00:00
|
|
|
"Name",
|
|
|
|
"The display name of the best match",
|
2019-12-09 14:51:21 +00:00
|
|
|
NULL,
|
|
|
|
G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);
|
|
|
|
|
2021-01-27 15:14:31 +00:00
|
|
|
props[PROP_AVATAR] =
|
|
|
|
g_param_spec_object ("avatar",
|
|
|
|
"Avatar",
|
|
|
|
"The avatar of the best match",
|
|
|
|
G_TYPE_LOADABLE_ICON,
|
|
|
|
G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);
|
2019-12-09 14:51:21 +00:00
|
|
|
|
|
|
|
|
2021-01-27 15:14:31 +00:00
|
|
|
g_object_class_install_properties (object_class, PROP_LAST_PROP, props);
|
2019-12-09 14:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
calls_best_match_init (CallsBestMatch *self)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CallsBestMatch *
|
2021-06-30 21:23:11 +00:00
|
|
|
calls_best_match_new (const char *number)
|
2019-12-09 14:51:21 +00:00
|
|
|
{
|
|
|
|
return g_object_new (CALLS_TYPE_BEST_MATCH,
|
2021-01-28 13:42:09 +00:00
|
|
|
"phone_number", number,
|
2019-12-09 14:51:21 +00:00
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2021-01-28 15:31:38 +00:00
|
|
|
gboolean
|
|
|
|
calls_best_match_has_individual (CallsBestMatch *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (CALLS_IS_BEST_MATCH (self), FALSE);
|
|
|
|
|
2021-11-18 10:07:38 +00:00
|
|
|
return !!self->matched_individual;
|
2021-01-28 15:31:38 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 03:02:45 +00:00
|
|
|
|
2021-06-30 21:23:11 +00:00
|
|
|
const char *
|
2021-01-28 13:42:09 +00:00
|
|
|
calls_best_match_get_phone_number (CallsBestMatch *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (CALLS_IS_BEST_MATCH (self), NULL);
|
|
|
|
|
|
|
|
return self->phone_number;
|
|
|
|
}
|
|
|
|
|
2021-09-24 03:02:45 +00:00
|
|
|
|
2021-01-28 13:42:09 +00:00
|
|
|
void
|
|
|
|
calls_best_match_set_phone_number (CallsBestMatch *self,
|
2021-09-23 07:32:37 +00:00
|
|
|
const char *phone_number)
|
2021-01-28 13:42:09 +00:00
|
|
|
{
|
|
|
|
g_autoptr (CallsPhoneNumberQuery) query = NULL;
|
|
|
|
|
|
|
|
g_return_if_fail (CALLS_IS_BEST_MATCH (self));
|
2021-04-12 11:56:38 +00:00
|
|
|
g_return_if_fail (phone_number);
|
2021-01-28 13:42:09 +00:00
|
|
|
|
|
|
|
g_clear_pointer (&self->phone_number, g_free);
|
|
|
|
|
|
|
|
// Consider empty string phone numbers as NULL
|
|
|
|
if (phone_number[0] != '\0')
|
|
|
|
self->phone_number = g_strdup (phone_number);
|
|
|
|
|
2021-04-05 05:25:31 +00:00
|
|
|
if (self->view)
|
|
|
|
g_signal_handlers_disconnect_by_data (self->view, self);
|
2021-01-28 13:42:09 +00:00
|
|
|
g_clear_object (&self->view);
|
|
|
|
|
|
|
|
if (self->phone_number) {
|
2021-07-01 00:01:32 +00:00
|
|
|
/* This is a SIP address, don' try parsing it as a phone number */
|
|
|
|
if (g_str_has_prefix (self->phone_number, "sip")) {
|
|
|
|
g_auto (GStrv) split = g_strsplit_set (self->phone_number, ":@", -1);
|
|
|
|
|
|
|
|
self->name_sip = g_strdup (split[1]);
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), props[PROP_PHONE_NUMBER]);
|
|
|
|
return;
|
|
|
|
}
|
2021-10-18 08:37:12 +00:00
|
|
|
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);
|
2021-01-28 13:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), props[PROP_PHONE_NUMBER]);
|
|
|
|
}
|
2019-12-09 14:51:21 +00:00
|
|
|
|
2021-06-30 21:23:11 +00:00
|
|
|
const char *
|
2019-12-09 14:51:21 +00:00
|
|
|
calls_best_match_get_name (CallsBestMatch *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (CALLS_IS_BEST_MATCH (self), NULL);
|
|
|
|
|
2021-11-18 10:07:38 +00:00
|
|
|
if (self->matched_individual)
|
|
|
|
return folks_individual_get_display_name (self->matched_individual);
|
2021-06-30 21:23:11 +00:00
|
|
|
else if (self->name_sip)
|
|
|
|
return self->name_sip;
|
2021-01-28 13:43:23 +00:00
|
|
|
else if (self->phone_number)
|
2021-06-30 21:23:11 +00:00
|
|
|
return self->phone_number;
|
2019-12-09 14:51:21 +00:00
|
|
|
else
|
2021-06-30 21:23:11 +00:00
|
|
|
return _("Anonymous caller");
|
2019-12-09 14:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-27 15:14:31 +00:00
|
|
|
GLoadableIcon *
|
|
|
|
calls_best_match_get_avatar (CallsBestMatch *self)
|
2019-12-09 14:51:21 +00:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (CALLS_IS_BEST_MATCH (self), NULL);
|
|
|
|
|
2021-11-18 10:07:38 +00:00
|
|
|
if (self->matched_individual)
|
|
|
|
return folks_avatar_details_get_avatar (FOLKS_AVATAR_DETAILS (self->matched_individual));
|
2021-01-27 15:14:31 +00:00
|
|
|
else
|
2021-06-30 21:23:11 +00:00
|
|
|
return NULL;
|
2019-12-09 14:51:21 +00:00
|
|
|
}
|