From 1e848129380a1b3a1267b91f189c93abf3d8fb69 Mon Sep 17 00:00:00 2001 From: Evangelos Ribeiro Tzaras Date: Mon, 26 Apr 2021 12:10:43 +0200 Subject: [PATCH] util: Add simple API to query protocol --- src/util.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 3 +++ 2 files changed, 52 insertions(+) diff --git a/src/util.c b/src/util.c index a58c487..e9de08e 100644 --- a/src/util.c +++ b/src/util.c @@ -198,3 +198,52 @@ calls_find_in_store (GListModel *list, return FALSE; #endif } + +/** + * get_protocol_from_address: + * @target: The target address + * + * simply checks for the the scheme of an address without doing any validation + * + * Returns: The protocol used for address, or NULL if could not determine + */ +const char * +get_protocol_from_address (const char *target) +{ + g_autofree char *lower = NULL; + + g_return_val_if_fail (target, NULL); + + lower = g_ascii_strdown (target, -1); + + if (g_str_has_prefix (lower, "sips:")) + return "sips"; + + if (g_str_has_prefix (lower, "sip:")) + return "sip"; + + if (g_str_has_prefix (lower, "tel:")) + return "tel"; + + /* could not determine the protocol (which most probably means it's a telephone number) */ + return NULL; +} + +/** + * get_protocol_from_address_with_fallback: + * @target: The address to check + * + * simply checks for the the scheme of an address without doing any validation + * + * Returns: The protocol used for address, or "tel" as a fallback + */ +const char * +get_protocol_from_address_with_fallback (const char *target) +{ + const char *protocol = get_protocol_from_address (target); + + if (!protocol) + protocol = "tel"; + + return protocol; +} diff --git a/src/util.h b/src/util.h index b01d910..d94a646 100644 --- a/src/util.h +++ b/src/util.h @@ -138,6 +138,9 @@ gboolean calls_find_in_store (GListModel *list, gpointer item, guint *position); +const char* get_protocol_from_address (const char *target); +const char* get_protocol_from_address_with_fallback (const char *target); + G_END_DECLS #endif /* CALLS__UTIL_H__ */