From 5c0a588da7a0ffa57c8893650432804ff2663800 Mon Sep 17 00:00:00 2001 From: Nazarii Kretovych Date: Sun, 20 Sep 2020 11:53:43 +0300 Subject: [PATCH 1/3] Remove adding country code to tel number while dialing. Fixes #176 Signed-off-by: Nazarii Kretovych --- src/calls-application.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/calls-application.c b/src/calls-application.c index c9a9251..796c8cd 100644 --- a/src/calls-application.c +++ b/src/calls-application.c @@ -393,7 +393,6 @@ open_tel_uri (CallsApplication *self, { EPhoneNumber *number; GError *error = NULL; - gchar *dial_str; g_debug ("Opening tel URI `%s'", uri); @@ -405,14 +404,28 @@ open_tel_uri (CallsApplication *self, g_error_free (error); return; } - - dial_str = e_phone_number_to_string - (number, E_PHONE_NUMBER_FORMAT_E164); e_phone_number_free (number); + // Get dial string. + const size_t uri_len = strlen(uri); + gchar dial_str[uri_len + 1]; + gchar *p_dial_str = dial_str; + for (const gchar *p_uri = uri; *p_uri; p_uri++) { + const gchar ch = *p_uri; + if ((ch >= '0' && ch <= '9') || ch == '+') { + *p_dial_str = ch; + p_dial_str++; + } + } + if (p_dial_str == dial_str) { + // dial_str is empty, so we return. + g_warning("Failed to create dial string for tel URI '%s'", uri); + return; + } + *p_dial_str = '\0'; + calls_main_window_dial (self->main_window, dial_str); - g_free (dial_str); } -- 2.26.2 From dedf8f820753d48575db6ec1774ed19783d66e65 Mon Sep 17 00:00:00 2001 From: Nazarii Kretovych Date: Sat, 26 Sep 2020 17:21:54 +0300 Subject: [PATCH 2/3] Fix: make open_tel_uri generate the proper dial string for tel URIs that have the 'phone-context' param. Signed-off-by: Nazarii Kretovych --- .gitignore | 3 ++- src/calls-application.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 826102b..29e7ed5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ _build .\#* build .buildconfig -.flatpak-builder \ No newline at end of file +.flatpak-builder +/nbproject/ \ No newline at end of file diff --git a/src/calls-application.c b/src/calls-application.c index 796c8cd..591681a 100644 --- a/src/calls-application.c +++ b/src/calls-application.c @@ -410,11 +410,26 @@ open_tel_uri (CallsApplication *self, const size_t uri_len = strlen(uri); gchar dial_str[uri_len + 1]; gchar *p_dial_str = dial_str; + const gchar *p_phone_context = strstr(uri, ";phone-context="); + if (p_phone_context) { + // uri has the 'phone-context' param. Add its value to dial_str. + for (p_phone_context += 15; *p_phone_context; p_phone_context++) { + const gchar ch = *p_phone_context; + if ((ch >= '0' && ch <= '9') || ch == '+') { + *p_dial_str = ch; + p_dial_str++; + } else if (ch == ';') { + break; + } + } + } for (const gchar *p_uri = uri; *p_uri; p_uri++) { const gchar ch = *p_uri; if ((ch >= '0' && ch <= '9') || ch == '+') { *p_dial_str = ch; p_dial_str++; + } else if (ch == ';') { + break; } } if (p_dial_str == dial_str) { -- 2.26.2 From b29dbc9bc4038b8e0ee2e2f8f0d69e46f757297a Mon Sep 17 00:00:00 2001 From: Nazarii Kretovych Date: Sat, 26 Sep 2020 20:22:40 +0300 Subject: [PATCH 3/3] Move code of tel uri parser to separate function and add unit test. Signed-off-by: Nazarii Kretovych --- src/calls-application.c | 81 ++++++++++++++++++++++++++-------------- src/calls-application.h | 1 + tests/meson.build | 14 +++++++ tests/test-application.c | 51 +++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 29 deletions(-) create mode 100644 tests/test-application.c diff --git a/src/calls-application.c b/src/calls-application.c index 591681a..9665a62 100644 --- a/src/calls-application.c +++ b/src/calls-application.c @@ -387,6 +387,53 @@ activate (GApplication *application) } +gchar * +parse_tel_uri_and_get_dial_str (const gchar *uri) +{ + // Get dial string. + const size_t uri_len = strlen(uri); + gchar *dial_str = g_new(gchar, uri_len + 1); + gchar *p_dial_str = dial_str; + const gchar *p_phone_context = strstr(uri, ";phone-context="); + if (p_phone_context) + { + // uri has the 'phone-context' param. Add its value to dial_str. + for (p_phone_context += 15; *p_phone_context; p_phone_context++) + { + const gchar ch = *p_phone_context; + if ((ch >= '0' && ch <= '9') || ch == '+') + { + *p_dial_str = ch; + p_dial_str++; + } + else if (ch == ';') + { + break; + } + } + } + for (const gchar *p_uri = uri; *p_uri; p_uri++) + { + const gchar ch = *p_uri; + if ((ch >= '0' && ch <= '9') || ch == '+') + { + *p_dial_str = ch; + p_dial_str++; + } + else if (ch == ';') + { + break; + } + } + *p_dial_str = '\0'; + if (!*dial_str) { + g_free(dial_str); + dial_str = NULL; + } + + return dial_str; +} + static void open_tel_uri (CallsApplication *self, const gchar *uri) @@ -407,40 +454,16 @@ open_tel_uri (CallsApplication *self, e_phone_number_free (number); // Get dial string. - const size_t uri_len = strlen(uri); - gchar dial_str[uri_len + 1]; - gchar *p_dial_str = dial_str; - const gchar *p_phone_context = strstr(uri, ";phone-context="); - if (p_phone_context) { - // uri has the 'phone-context' param. Add its value to dial_str. - for (p_phone_context += 15; *p_phone_context; p_phone_context++) { - const gchar ch = *p_phone_context; - if ((ch >= '0' && ch <= '9') || ch == '+') { - *p_dial_str = ch; - p_dial_str++; - } else if (ch == ';') { - break; - } - } - } - for (const gchar *p_uri = uri; *p_uri; p_uri++) { - const gchar ch = *p_uri; - if ((ch >= '0' && ch <= '9') || ch == '+') { - *p_dial_str = ch; - p_dial_str++; - } else if (ch == ';') { - break; - } - } - if (p_dial_str == dial_str) { - // dial_str is empty, so we return. - g_warning("Failed to create dial string for tel URI '%s'", uri); + gchar *dial_str = parse_tel_uri_and_get_dial_str (uri); + if (!dial_str) { + g_warning ("Failed to create dial string for tel URI '%s'", uri); return; } - *p_dial_str = '\0'; calls_main_window_dial (self->main_window, dial_str); + + g_free (dial_str); } diff --git a/src/calls-application.h b/src/calls-application.h index 08c3c56..26cf6c8 100644 --- a/src/calls-application.h +++ b/src/calls-application.h @@ -34,5 +34,6 @@ G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (CallsApplication, calls_application, CALLS, APPLICATION, GtkApplication) CallsApplication *calls_application_new (void); +gchar *parse_tel_uri_and_get_dial_str (const gchar *); G_END_DECLS diff --git a/tests/meson.build b/tests/meson.build index 618b1c4..5cf581d 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -71,5 +71,19 @@ t = executable('manager', test_sources, ) test('manager', t, env: test_env) +test_sources = [ 'test-application.c' ] + +t = executable('application', test_sources, + calls_sources, calls_enum_sources, calls_resources, + wl_proto_sources, wayland_sources, + c_args : test_cflags, + link_args: test_link_args, + link_with : calls_vala, + dependencies: calls_deps, + include_directories : [ + calls_includes + ] + ) +test('application', t, env: test_env) endif diff --git a/tests/test-application.c b/tests/test-application.c new file mode 100644 index 0000000..ffd192e --- /dev/null +++ b/tests/test-application.c @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2020 Purism SPC + * + * SPDX-License-Identifier: GPL-3.0+ + */ + +#include "calls-application.h" + +#include +#include + +static void +test_calls_application_parse_tel_uri_and_get_dial_str () +{ + gchar *dial_str; + + // Test 1. + dial_str = parse_tel_uri_and_get_dial_str ("tel:123-456-789;phone-context=+48"); + g_assert_true (g_strcmp0 (dial_str, "+48123456789") == 0); + g_free(dial_str); + + // Test 2. + dial_str = parse_tel_uri_and_get_dial_str ("tel:+38-096-123-45-67"); + g_assert_true (g_strcmp0 (dial_str, "+380961234567") == 0); + g_free(dial_str); + + // Test 3. + dial_str = parse_tel_uri_and_get_dial_str ("tel:0-800-123-456"); + g_assert_true (g_strcmp0 (dial_str, "0800123456") == 0); + g_free(dial_str); + + // Test 4. + dial_str = parse_tel_uri_and_get_dial_str ("tel:447"); + g_assert_true (g_strcmp0 (dial_str, "447") == 0); + g_free(dial_str); +} + +gint +main (gint argc, gchar *argv[]) +{ + gtk_test_init (&argc, &argv, NULL); + + /* Add builddir as search path */ +#ifdef PLUGIN_BUILDDIR + peas_engine_add_search_path (peas_engine_get_default (), PLUGIN_BUILDDIR, NULL); +#endif + + g_test_add_func("/Calls/Application/parse_tel_uri_and_get_dial_str", test_calls_application_parse_tel_uri_and_get_dial_str); + + return g_test_run(); +} -- 2.26.2