1
0
Fork 0
mirror of https://gitlab.gnome.org/GNOME/calls.git synced 2024-06-16 08:49:31 +00:00

Contacts: replace CallsContacts with CallsContactsProvider

This commit is contained in:
Julian Sparber 2021-01-20 15:20:09 +01:00 committed by Evangelos Ribeiro Tzaras
parent 4ba9feb22f
commit f61ae1cba1
8 changed files with 47 additions and 319 deletions

View file

@ -9,7 +9,6 @@ src/calls-call-record-row.c
src/calls-call-selector-item.c
src/calls-call-window.c
src/calls-contacts-provider.c
src/calls-contacts.c
src/calls-encryption-indicator.c
src/calls-history-box.c
src/calls-in-app-notification.c

View file

@ -32,7 +32,6 @@
#include "calls-ringer.h"
#include "calls-notifier.h"
#include "calls-record-store.h"
#include "calls-contacts.h"
#include "calls-call-window.h"
#include "calls-main-window.h"
#include "calls-manager.h"
@ -61,13 +60,11 @@ struct _CallsApplication
CallsRinger *ringer;
CallsNotifier *notifier;
CallsRecordStore *record_store;
CallsContacts *contacts;
CallsMainWindow *main_window;
CallsCallWindow *call_window;
};
G_DEFINE_TYPE (CallsApplication, calls_application, GTK_TYPE_APPLICATION);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (EPhoneNumber, e_phone_number_free)
static gboolean start_proper (CallsApplication *self);
@ -370,9 +367,6 @@ start_proper (CallsApplication *self)
self->record_store = calls_record_store_new ();
g_assert (self->record_store != NULL);
self->contacts = calls_contacts_get_default ();
g_assert (self->contacts != NULL);
self->notifier = calls_notifier_new ();
g_assert (CALLS_IS_NOTIFIER (self->notifier));
@ -506,7 +500,6 @@ finalize (GObject *object)
g_clear_object (&self->call_window);
g_clear_object (&self->main_window);
g_clear_object (&self->record_store);
g_clear_object (&self->contacts);
g_clear_object (&self->ringer);
g_clear_object (&self->notifier);

View file

@ -24,7 +24,8 @@
#include "calls-call-record-row.h"
#include "calls-best-match.h"
#include "calls-contacts.h"
#include "calls-contacts-provider.h"
#include "calls-manager.h"
#include "util.h"
#include <glib/gi18n.h>
@ -385,7 +386,7 @@ setup_contact (CallsCallRecordRow *self)
{
g_autofree gchar *target = NULL;
g_autoptr(GError) error = NULL;
EPhoneNumber *phone_number;
CallsContactsProvider *contacts_provider;
// Get the target number
g_object_get (G_OBJECT (self->record),
@ -396,22 +397,12 @@ setup_contact (CallsCallRecordRow *self)
if (!target[0])
return;
// Parse it
phone_number = e_phone_number_from_string
(target, NULL, &error);
if (!phone_number)
{
g_warning ("Error parsing phone number `%s': %s",
target, error->message);
return;
}
// Look up the best match object
self->contact = calls_contacts_lookup_phone_number
(calls_contacts_get_default (), phone_number);
g_assert (self->contact != NULL);
g_object_ref (self->contact);
e_phone_number_free (phone_number);
contacts_provider = calls_manager_get_contacts_provider (calls_manager_get_default ());
self->contact = calls_contacts_provider_lookup_phone_number (contacts_provider, target);
if (self->contact == NULL)
return;
g_signal_connect_swapped (self->contact,
"notify::name",

View file

@ -1,216 +0,0 @@
/*
* 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-contacts.h"
#include <glib/gi18n.h>
struct _CallsContacts
{
GObject parent_instance;
FolksIndividualAggregator *big_pile_of_contacts;
/** Map of call target (EPhoneNumber) to CallsBestMatch */
GHashTable *phone_number_best_matches;
};
G_DEFINE_TYPE (CallsContacts, calls_contacts, G_TYPE_OBJECT);
static guint
phone_number_hash (const EPhoneNumber *number)
{
g_autofree gchar *str = NULL;
str = e_phone_number_to_string
(number, E_PHONE_NUMBER_FORMAT_E164);
g_assert (str != NULL);
return g_str_hash (str);
}
static gboolean
phone_number_equal (const EPhoneNumber *a,
const EPhoneNumber *b)
{
EPhoneNumberMatch match = e_phone_number_compare (a, b);
return
match == E_PHONE_NUMBER_MATCH_EXACT
||
match == E_PHONE_NUMBER_MATCH_NATIONAL;
}
static void
prepare_cb (FolksIndividualAggregator *aggregator,
GAsyncResult *res,
CallsContacts *self)
{
GError *error = NULL;
folks_individual_aggregator_prepare_finish (aggregator,
res,
&error);
if (error)
{
g_warning ("Error preparing Folks individual aggregator: %s",
error->message);
g_error_free (error);
}
else
{
g_debug ("Folks individual aggregator prepared");
}
}
static void
constructed (GObject *object)
{
CallsContacts *self = CALLS_CONTACTS (object);
self->big_pile_of_contacts = folks_individual_aggregator_dup ();
g_assert (self->big_pile_of_contacts != NULL);
g_object_ref (self->big_pile_of_contacts);
folks_individual_aggregator_prepare (self->big_pile_of_contacts,
(GAsyncReadyCallback)prepare_cb,
self);
self->phone_number_best_matches = g_hash_table_new_full
((GHashFunc)phone_number_hash,
(GEqualFunc)phone_number_equal,
(GDestroyNotify)e_phone_number_free,
g_object_unref);
G_OBJECT_CLASS (calls_contacts_parent_class)->constructed (object);
}
static void
dispose (GObject *object)
{
CallsContacts *self = CALLS_CONTACTS (object);
g_clear_pointer (&self->phone_number_best_matches,
g_hash_table_unref);
g_clear_object (&self->big_pile_of_contacts);
G_OBJECT_CLASS (calls_contacts_parent_class)->dispose (object);
}
static void
calls_contacts_class_init (CallsContactsClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->constructed = constructed;
object_class->dispose = dispose;
}
static void
calls_contacts_init (CallsContacts *self)
{
}
CallsContacts *
calls_contacts_get_default ()
{
static CallsContacts *instance;
if (instance == NULL)
{
instance = g_object_new (CALLS_TYPE_CONTACTS, NULL);
g_object_add_weak_pointer (G_OBJECT (instance), (gpointer *) &instance);
}
return instance;
}
static void
search_view_prepare_cb (FolksSearchView *view,
GAsyncResult *res,
CallsContacts *self)
{
GError *error = NULL;
folks_search_view_prepare_finish (view, res, &error);
if (error)
{
g_warning ("Error preparing Folks search view: %s",
error->message);
g_error_free (error);
}
}
/*
* calls_contacts_lookup_phone_number:
* @self: The #CallsContact
* @number: The phonenumber
*
* Get a best contact match for a phone number
*
* Returns: (transfer none): The best match as #CallsBestMatch
*/
CallsBestMatch *
calls_contacts_lookup_phone_number (CallsContacts *self,
EPhoneNumber *number)
{
CallsBestMatch *best_match;
CallsPhoneNumberQuery *query;
CallsBestMatchView *view;
best_match = g_hash_table_lookup (self->phone_number_best_matches, number);
if (best_match)
{
return best_match;
}
query = calls_phone_number_query_new (number);
view = calls_best_match_view_new
(self->big_pile_of_contacts, FOLKS_QUERY (query));
g_object_unref (query);
folks_search_view_prepare
(FOLKS_SEARCH_VIEW (view),
(GAsyncReadyCallback)search_view_prepare_cb,
self);
best_match = calls_best_match_new (view);
g_assert (best_match != NULL);
g_object_unref (view);
g_hash_table_insert (self->phone_number_best_matches,
e_phone_number_copy (number),
best_match);
return best_match;
}

View file

@ -1,46 +0,0 @@
/*
* Copyright (C) 2018, 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
*
*/
#ifndef CALLS_CONTACTS_H__
#define CALLS_CONTACTS_H__
#include "calls-best-match.h"
#include <glib-object.h>
#include <libebook-contacts/libebook-contacts.h>
G_BEGIN_DECLS
#define CALLS_TYPE_CONTACTS (calls_contacts_get_type ())
G_DECLARE_FINAL_TYPE (CallsContacts, calls_contacts, CALLS, CONTACTS, GObject);
CallsContacts * calls_contacts_get_default ();
CallsBestMatch * calls_contacts_lookup_phone_number (CallsContacts *self,
EPhoneNumber *number);
G_END_DECLS
#endif /* CALLS_CONTACTS_H__ */

View file

@ -25,7 +25,7 @@
#include "config.h"
#include "calls-ussd.h"
#include "calls-manager.h"
#include "calls-contacts.h"
#include "calls-contacts-provider.h"
#include "enum-types.h"
#include <glib/gi18n.h>
@ -36,13 +36,13 @@ struct _CallsManager
GObject parent_instance;
CallsProvider *provider;
CallsContactsProvider *contacts_provider;
gchar *provider_name;
CallsOrigin *default_origin;
CallsManagerState state;
};
G_DEFINE_TYPE (CallsManager, calls_manager, G_TYPE_OBJECT);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (EPhoneNumber, e_phone_number_free)
enum {
PROP_0,
@ -351,6 +351,7 @@ calls_manager_finalize (GObject *object)
g_clear_object (&self->provider);
g_clear_pointer (&self->provider_name, g_free);
g_clear_object (&self->contacts_provider);
G_OBJECT_CLASS (calls_manager_parent_class)->finalize (object);
}
@ -476,6 +477,9 @@ calls_manager_init (CallsManager *self)
{
self->state = CALLS_MANAGER_STATE_NO_PROVIDER;
self->provider_name = NULL;
// Load the contacts provider
self->contacts_provider = calls_contacts_provider_new ();
}
@ -497,6 +501,14 @@ calls_manager_get_default (void)
return instance;
}
CallsContactsProvider *
calls_manager_get_contacts_provider (CallsManager *self)
{
g_return_val_if_fail (CALLS_IS_MANAGER (self), NULL);
return self->contacts_provider;
}
const gchar *
calls_manager_get_provider (CallsManager *self)
{
@ -661,24 +673,18 @@ calls_manager_set_default_origin (CallsManager *self,
const gchar *
calls_manager_get_contact_name (CallsCall *call)
{
g_autoptr (EPhoneNumber) phone_number = NULL;
g_autoptr (GError) err = NULL;
const gchar *number;
CallsBestMatch *match;
g_autoptr (CallsBestMatch) match = NULL;
CallsContactsProvider *contacts_provider;
number = calls_call_get_number (call);
if (!number || g_strcmp0 (number, "") == 0)
return _("Anonymous caller");
phone_number = e_phone_number_from_string (number, NULL, &err);
if (!phone_number)
{
g_warning ("Failed to convert %s to a phone number: %s", number, err->message);
return NULL;
}
contacts_provider = calls_manager_get_contacts_provider (calls_manager_get_default ());
match = calls_contacts_provider_lookup_phone_number (contacts_provider,
number);
match = calls_contacts_lookup_phone_number (calls_contacts_get_default (),
phone_number);
if (!match)
return NULL;

View file

@ -24,6 +24,7 @@
#pragma once
#include "calls-contacts-provider.h"
#include "calls-provider.h"
#include "calls-origin.h"
@ -45,23 +46,24 @@ typedef enum
} CallsManagerState;
CallsManager *calls_manager_new (void);
CallsManager *calls_manager_get_default (void);
const gchar *calls_manager_get_provider (CallsManager *self);
void calls_manager_set_provider (CallsManager *self,
const gchar *name);
CallsProvider *calls_manager_get_real_provider (CallsManager *self);
CallsManagerState calls_manager_get_state (CallsManager *self);
GList *calls_manager_get_origins (CallsManager *self);
GList *calls_manager_get_calls (CallsManager *self);
void calls_manager_dial (CallsManager *self,
CallsOrigin *origin,
const gchar *target);
CallsOrigin *calls_manager_get_default_origin (CallsManager *self);
void calls_manager_set_default_origin (CallsManager *self,
CallsOrigin *origin);
const gchar *calls_manager_get_contact_name (CallsCall *call);
gboolean calls_manager_has_active_call (CallsManager *self);
void calls_manager_hang_up_all_calls (CallsManager *self);
CallsManager *calls_manager_new (void);
CallsManager *calls_manager_get_default (void);
CallsContactsProvider *calls_manager_get_contacts_provider (CallsManager *self);
const gchar *calls_manager_get_provider (CallsManager *self);
void calls_manager_set_provider (CallsManager *self,
const gchar *name);
CallsProvider *calls_manager_get_real_provider (CallsManager *self);
CallsManagerState calls_manager_get_state (CallsManager *self);
GList *calls_manager_get_origins (CallsManager *self);
GList *calls_manager_get_calls (CallsManager *self);
void calls_manager_dial (CallsManager *self,
CallsOrigin *origin,
const gchar *target);
CallsOrigin *calls_manager_get_default_origin (CallsManager *self);
void calls_manager_set_default_origin (CallsManager *self,
CallsOrigin *origin);
const gchar *calls_manager_get_contact_name (CallsCall *call);
gboolean calls_manager_has_active_call (CallsManager *self);
void calls_manager_hang_up_all_calls (CallsManager *self);
G_END_DECLS

View file

@ -84,7 +84,6 @@ calls_sources = files(['calls-message-source.c', 'calls-message-source.h',
'calls-call-record.c', 'calls-call-record.h',
'calls-record-store.c', 'calls-record-store.h',
'calls-call-record-row.c', 'calls-call-record-row.h',
'calls-contacts.c', 'calls-contacts.h',
'calls-contacts-provider.c', 'calls-contacts-provider.h',
'calls-best-match.c', 'calls-best-match.h',
'calls-in-app-notification.c', 'calls-in-app-notification.h',