1
0
Fork 0
mirror of https://gitlab.gnome.org/GNOME/calls.git synced 2024-05-14 09:09:28 +00:00

emergency-call-types: Allow to lookup emergency numbers by country code

We build a hash as cache to speed up lookups. As this is a one time
thing we just do it once and don't bother to free it up.

We currently assume that all numbers can be used for the location based
lookups. Should that change we want to add a flag indicating that.
This commit is contained in:
Guido Günther 2023-07-19 12:41:52 +02:00
parent 6aa356590b
commit aaefc0443a
2 changed files with 56 additions and 2 deletions

View file

@ -26,10 +26,12 @@ typedef struct {
} CallsEmergencyNumber;
typedef struct {
char *country_code;
CallsEmergencyNumber numbers[3];
char *country_code;
CallsEmergencyNumber numbers[3];
} CallsEmergencyNumberTypes;
GHashTable *by_mcc;
CallsEmergencyNumberTypes emergency_number_types[] = {
{ "CH",
{
@ -55,6 +57,22 @@ CallsEmergencyNumberTypes emergency_number_types[] = {
};
static void
init_hash (void)
{
if (g_once_init_enter (&by_mcc)) {
GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal);
for (int i = 0; i < G_N_ELEMENTS (emergency_number_types); i++) {
CallsEmergencyNumberTypes *numbers = &emergency_number_types[i];
g_hash_table_insert (table, numbers->country_code, numbers);
}
g_once_init_leave (&by_mcc, table);
}
}
static char *
flags_to_string (CallsEmergencyCallTypeFlags flags)
{
@ -105,3 +123,36 @@ calls_emergency_call_type_get_name (const char *lookup, const char *country_code
return NULL;
}
/**
* calls_emergency_call_types_get_numbers_by_country_code:
* @mcc: The country code
*
* Get the valid emergency numbers for this country code
*
* Returns:(transfer full): A string array of emergency numbers
*/
GStrv
calls_emergency_call_types_get_numbers_by_country_code (const char *country_code)
{
g_autoptr (GPtrArray) ret = g_ptr_array_new_with_free_func (g_free);
CallsEmergencyNumberTypes *match;
if (country_code == NULL)
return NULL;
init_hash ();
match = g_hash_table_lookup (by_mcc, country_code);
if (!match)
return NULL;
/* Can use g_strv_builder with glib > 2.68 */
for (int i = 0; i < G_N_ELEMENTS (match->numbers); i++) {
char *number = g_strdup (match->numbers[i].number);
g_ptr_array_add (ret, number);
}
g_ptr_array_add (ret, NULL);
return (GStrv) g_ptr_array_steal (ret, NULL);
}

View file

@ -4,6 +4,8 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <glib.h>
# pragma once
typedef enum {
@ -22,3 +24,4 @@ typedef enum {
} CallsEmergencyCallTypeFlags;
char *calls_emergency_call_type_get_name (const char *number, const char *country_code);
GStrv calls_emergency_call_types_get_numbers_by_country_code (const char *country_code);