From b7b0dcbc525ef9eac26a738eaf6f3de716684746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20No=C3=ABl?= Date: Wed, 15 Sep 2021 23:35:57 +0200 Subject: M!77 - alarm-notify: Use GSound instead of libcanberra-gtk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allows to be more future-proof and decrease the dependency on GTK 3 in the alarm-notify. Closes https://gitlab.gnome.org/GNOME/evolution-data-server/-/merge_requests/77 Signed-off-by: Corentin Noël --- CMakeLists.txt | 18 ++-- config.h.in | 4 +- src/services/evolution-alarm-notify/CMakeLists.txt | 6 +- .../evolution-alarm-notify/e-alarm-notify.c | 103 +++++++++++++-------- 4 files changed, 77 insertions(+), 54 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a0ea840c..5776499d2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -123,7 +123,7 @@ set(libaccounts_glib_minimum_version 1.4) set(libsignon_glib_minimum_version 1.8) set(json_glib_minimum_version 1.0.4) set(webkit2gtk_minimum_version 2.28.0) -set(libcanberra_gtk_minimum_version 0.25) +set(gsound_minimum_version 1.0) # Load modules from the source tree set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules) @@ -767,16 +767,16 @@ if(ENABLE_WEATHER) unset(CMAKE_REQUIRED_LIBRARIES) endif(ENABLE_WEATHER) -# ************************************************ -# evolution-alarm-notify : Canberra-GTK for Sound -# ************************************************ +# ******************************************************** +# evolution-alarm-notify : GSound for sound notifications +# ******************************************************** -add_printable_option(ENABLE_CANBERRA "Enable Canberra-GTK for sound in evolution-alarm-notify" ON) +add_printable_option(ENABLE_GSOUND "Enable GSound for sound in evolution-alarm-notify" ON) -if(ENABLE_CANBERRA) - pkg_check_modules_for_option(ENABLE_CANBERRA "Canberra-GTK for sound in evolution-alarm-notify" CANBERRA libcanberra-gtk3>=${libcanberra_gtk_minimum_version}) - set(HAVE_CANBERRA ON) -endif(ENABLE_CANBERRA) +if(ENABLE_GSOUND) + pkg_check_modules_for_option(ENABLE_GSOUND "GSound for sound notifications in evolution-alarm-notify" GSOUND gsound>=${gsound_minimum_version}) + set(HAVE_GSOUND ON) +endif(ENABLE_GSOUND) # ****************************** # File locking diff --git a/config.h.in b/config.h.in index dfe787d19..f589d2c1a 100644 --- a/config.h.in +++ b/config.h.in @@ -197,8 +197,8 @@ /* gweather_info_new() has only one argument */ #cmakedefine HAVE_ONE_ARG_GWEATHER_INFO_NEW 1 -/* evolution-alarm-notify - Define if using Canberra-GTK for sound */ -#cmakedefine HAVE_CANBERRA 1 +/* evolution-alarm-notify - Define if using GSound for sound notification */ +#cmakedefine HAVE_GSOUND 1 /* Whether glib provides GPowerProfileMonitor */ #cmakedefine HAVE_GPOWERPROFILEMONITOR 1 diff --git a/src/services/evolution-alarm-notify/CMakeLists.txt b/src/services/evolution-alarm-notify/CMakeLists.txt index a4c0a41e7..baf765d7c 100644 --- a/src/services/evolution-alarm-notify/CMakeLists.txt +++ b/src/services/evolution-alarm-notify/CMakeLists.txt @@ -23,7 +23,7 @@ target_compile_definitions(evolution-alarm-notify PRIVATE ) target_compile_options(evolution-alarm-notify PUBLIC - ${CANBERRA_CFLAGS} + ${GSOUND_CFLAGS} ${DATA_SERVER_CFLAGS} ${GNOME_PLATFORM_CFLAGS} ${GTK_CFLAGS} @@ -33,7 +33,7 @@ target_include_directories(evolution-alarm-notify PUBLIC ${CMAKE_BINARY_DIR} ${CMAKE_BINARY_DIR}/src ${CMAKE_SOURCE_DIR}/src - ${CANBERRA_INCLUDE_DIRS} + ${GSOUND_INCLUDE_DIRS} ${DATA_SERVER_INCLUDE_DIRS} ${GNOME_PLATFORM_INCLUDE_DIRS} ${GTK_INCLUDE_DIRS} @@ -41,7 +41,7 @@ target_include_directories(evolution-alarm-notify PUBLIC target_link_libraries(evolution-alarm-notify ${DEPENDENCIES} - ${CANBERRA_LDFLAGS} + ${GSOUND_LDFLAGS} ${DATA_SERVER_LDFLAGS} ${GNOME_PLATFORM_LDFLAGS} ${GTK_LDFLAGS} diff --git a/src/services/evolution-alarm-notify/e-alarm-notify.c b/src/services/evolution-alarm-notify/e-alarm-notify.c index 5c9b479f9..d69245acf 100644 --- a/src/services/evolution-alarm-notify/e-alarm-notify.c +++ b/src/services/evolution-alarm-notify/e-alarm-notify.c @@ -19,8 +19,8 @@ #include -#ifdef HAVE_CANBERRA -#include +#ifdef HAVE_GSOUND +#include #endif #include @@ -138,7 +138,11 @@ e_alarm_notify_audio (EAlarmNotify *an, { ICalAttach *attach = NULL; GSList *attachments; - gboolean did_play = FALSE; + GdkDisplay *display; + GError *error = NULL; +#ifdef HAVE_GSOUND + GSoundContext *ctx; +#endif g_return_val_if_fail (an != NULL, FALSE); g_return_val_if_fail (rd != NULL, FALSE); @@ -154,33 +158,43 @@ e_alarm_notify_audio (EAlarmNotify *an, url = i_cal_attach_get_url (attach); if (url && *url) { gchar *filename; - GError *error = NULL; filename = g_filename_from_uri (url, NULL, &error); if (!filename) { ean_debug_print ("Audio notify: Failed to convert URI '%s' to filename: %s\n", url, error ? error->message : "Unknown error"); + g_clear_error (&error); } else if (g_file_test (filename, G_FILE_TEST_EXISTS)) { -#ifdef HAVE_CANBERRA - gint err = ca_context_play (ca_gtk_context_get (), 0, - CA_PROP_APPLICATION_NAME, "evolution-alarm-notify", - CA_PROP_APPLICATION_VERSION, VERSION, - CA_PROP_APPLICATION_ID, "org.gnome.Evolution-alarm-notify", - CA_PROP_MEDIA_FILENAME, filename, - NULL); - - did_play = !err; - - if (err) - ean_debug_print ("Audio notify: Cannot play file '%s': %s\n", filename, ca_strerror (err)); + g_clear_error (&error); +#ifdef HAVE_GSOUND + ctx = gsound_context_new (NULL, &error); + if (error != NULL) { + ean_debug_print ("Failed to create gsound context: %s\n", error->message); + g_clear_error (&error); + } else { + gsound_context_play_simple (ctx, NULL, &error, + GSOUND_ATTR_APPLICATION_NAME, "evolution-alarm-notify", + GSOUND_ATTR_APPLICATION_VERSION, VERSION, + GSOUND_ATTR_APPLICATION_ID, "org.gnome.Evolution-alarm-notify", + GSOUND_ATTR_MEDIA_FILENAME, filename, + NULL); + if (error != NULL) { + ean_debug_print ("Failed to play sound \"%s\": %s\n", filename, error->message); + g_clear_error (&error); + } else { + g_object_unref (ctx); + g_free (filename); + return FALSE; + } + } #else - ean_debug_print ("Audio notify: Cannot play file '%s': Not compiled with libcanberra\n", filename); + g_clear_error (&error); + ean_debug_print ("Audio notify: Cannot play file '%s': Not compiled with gsound\n", filename); #endif } else { ean_debug_print ("Audio notify: File '%s' does not exist\n", filename); } - g_clear_error (&error); g_free (filename); } else { ean_debug_print ("Audio notify: Alarm has stored empty URL, fallback to default sound\n"); @@ -191,35 +205,44 @@ e_alarm_notify_audio (EAlarmNotify *an, ean_debug_print ("Audio notify: Alarm attachment is not a URL to sound file, fallback to default sound\n"); } -#ifdef HAVE_CANBERRA - if (!did_play) { - gint err = ca_context_play (ca_gtk_context_get (), 0, - CA_PROP_APPLICATION_NAME, "evolution-alarm-notify", - CA_PROP_APPLICATION_VERSION, VERSION, - CA_PROP_APPLICATION_ID, "org.gnome.Evolution-alarm-notify", - CA_PROP_EVENT_ID, "alarm-clock-elapsed", - NULL); +#ifdef HAVE_GSOUND + if (!ctx) { + ctx = gsound_context_new (NULL, &error); + if (error != NULL) { + ean_debug_print ("Failed to create gsound context: %s\n", error->message); + g_clear_error (&error); + } + } - did_play = !err; + if (ctx != NULL) { + gsound_context_play_simple (ctx, NULL, &error, + GSOUND_ATTR_APPLICATION_NAME, "evolution-alarm-notify", + GSOUND_ATTR_APPLICATION_VERSION, VERSION, + GSOUND_ATTR_APPLICATION_ID, "org.gnome.Evolution-alarm-notify", + GSOUND_ATTR_EVENT_ID, "alarm-clock-elapsed", + NULL); + if (error != NULL) { + ean_debug_print ("Audio notify: Cannot play event sound: %s\n", error->message); + g_clear_error (&error); + } else { + g_object_unref (ctx); + return FALSE; + } - if (err) - ean_debug_print ("Audio notify: Cannot play event sound: %s\n", ca_strerror (err)); + g_object_unref (ctx); } -#endif - if (!did_play) { - GdkDisplay *display; +#endif - display = an->priv->window ? gtk_widget_get_display (an->priv->window) : NULL; + display = an->priv->window ? gtk_widget_get_display (an->priv->window) : NULL; - if (!display) - display = gdk_display_get_default (); + if (!display) + display = gdk_display_get_default (); - if (display) - gdk_display_beep (display); - else - ean_debug_print ("Audio notify: Cannot beep, no display found\n"); - } + if (display) + gdk_display_beep (display); + else + ean_debug_print ("Audio notify: Cannot beep, no display found\n"); return FALSE; } -- cgit v1.2.1