diff options
| author | mbkma <[email protected]> | 2026-04-25 17:22:07 +0200 |
|---|---|---|
| committer | mbkma <[email protected]> | 2026-05-05 19:29:26 +0200 |
| commit | 4a48a4d73a83fb2543700f10411850cc43821853 (patch) | |
| tree | b94d11c59e317144007a3b01079acd6b4e63e0cd | |
| parent | 3fc6e65dfccbbb89c378aaa8ac161fd0c7c9807d (diff) | |
| download | mate-calc-fix-currency-conversion.tar.bz2 mate-calc-fix-currency-conversion.tar.xz | |
financial: fix currency conversionfix-currency-conversion
| -rwxr-xr-x | .github/workflows/archlinux.sh | 1 | ||||
| -rwxr-xr-x | .github/workflows/debian.sh | 1 | ||||
| -rwxr-xr-x | .github/workflows/fedora.sh | 1 | ||||
| -rwxr-xr-x | .github/workflows/ubuntu.sh | 1 | ||||
| -rw-r--r-- | configure.ac | 2 | ||||
| -rw-r--r-- | meson.build | 1 | ||||
| -rw-r--r-- | src/currency-manager.c | 92 | ||||
| -rw-r--r-- | src/meson.build | 6 |
8 files changed, 74 insertions, 31 deletions
diff --git a/.github/workflows/archlinux.sh b/.github/workflows/archlinux.sh index e7ba154..f2cdaa5 100755 --- a/.github/workflows/archlinux.sh +++ b/.github/workflows/archlinux.sh @@ -25,6 +25,7 @@ requires+=( gtk3 intltool itstool + libsoup3 libmpc make mate-common diff --git a/.github/workflows/debian.sh b/.github/workflows/debian.sh index 9f5b1bd..56ef544 100755 --- a/.github/workflows/debian.sh +++ b/.github/workflows/debian.sh @@ -23,6 +23,7 @@ requires+=( libglib2.0-dev libgmp-dev libgtk-3-dev + libsoup-3.0-dev libmpc-dev libmpfr-dev libxml2-dev diff --git a/.github/workflows/fedora.sh b/.github/workflows/fedora.sh index 7dd4d6b..1877780 100755 --- a/.github/workflows/fedora.sh +++ b/.github/workflows/fedora.sh @@ -24,6 +24,7 @@ requires+=( git gmp-devel gtk3-devel + libsoup3-devel libmpc-devel libxml2-devel make diff --git a/.github/workflows/ubuntu.sh b/.github/workflows/ubuntu.sh index 3792bd3..feb1ed7 100755 --- a/.github/workflows/ubuntu.sh +++ b/.github/workflows/ubuntu.sh @@ -24,6 +24,7 @@ requires+=( libglib2.0-dev libgmp-dev libgtk-3-dev + libsoup-3.0-dev libmpc-dev libmpfr-dev libxml2-dev diff --git a/configure.ac b/configure.ac index 443dd40..ab62f9f 100644 --- a/configure.ac +++ b/configure.ac @@ -32,6 +32,7 @@ PKG_CHECK_MODULES(MATE_CALC, [ gio-2.0 >= $GIO_REQUIRED mpfr >= $MPFR_REQUIRED libxml-2.0 + libsoup-3.0 gmodule-export-2.0 ]) @@ -40,6 +41,7 @@ PKG_CHECK_MODULES(MATE_CALC_CMD, [ gio-2.0 >= $GIO_REQUIRED mpfr >= $MPFR_REQUIRED libxml-2.0 + libsoup-3.0 ]) GLIB_MKENUMS=`$PKG_CONFIG --variable=glib_mkenums glib-2.0` diff --git a/meson.build b/meson.build index 2051229..ad2ae4d 100644 --- a/meson.build +++ b/meson.build @@ -21,6 +21,7 @@ gio = dependency('gio-2.0', version: '>= ' + glib_min_version) glib = dependency('glib-2.0', version: '>= ' + glib_min_version) gobject = dependency('gobject-2.0', version: '>= ' + glib_min_version) libxml = dependency('libxml-2.0') +libsoup = dependency('libsoup-3.0') gtk = dependency('gtk+-3.0', version : '>=3.22') # Libraries diff --git a/src/currency-manager.c b/src/currency-manager.c index 5445881..9150df4 100644 --- a/src/currency-manager.c +++ b/src/currency-manager.c @@ -13,6 +13,7 @@ #include <glib.h> #include <glib/gstdio.h> #include <gio/gio.h> +#include <libsoup/soup.h> #include <libxml/tree.h> #include <libxml/parser.h> #include <libxml/xpath.h> @@ -219,52 +220,87 @@ file_needs_update(gchar *filename, double max_age) return FALSE; } +typedef struct { + CurrencyManager *manager; + gchar *filename; + gboolean *downloading_flag; + SoupSession *session; + SoupMessage *message; +} DownloadData; + static void -download_imf_cb(GObject *object, GAsyncResult *result, gpointer user_data) +download_done(DownloadData *data) { - CurrencyManager *manager = user_data; - GError *error = NULL; + *data->downloading_flag = FALSE; + load_rates(data->manager); - if (g_file_copy_finish(G_FILE(object), result, &error)) - g_debug("IMF rates updated"); - else - g_warning("Couldn't download IMF currency rate file: %s", error->message); - g_clear_error(&error); - downloading_imf_rates = FALSE; - load_rates(manager); + g_object_unref(data->message); + g_object_unref(data->session); + g_free(data->filename); + g_free(data); } static void -download_ecb_cb(GObject *object, GAsyncResult *result, gpointer user_data) +soup_send_cb(GObject *object, GAsyncResult *result, gpointer user_data) { - CurrencyManager *manager = user_data; + DownloadData *data = user_data; + SoupSession *session = SOUP_SESSION(object); GError *error = NULL; + GBytes *body; + guint status = soup_message_get_status(data->message); + + body = soup_session_send_and_read_finish(session, result, &error); + if (error || !SOUP_STATUS_IS_SUCCESSFUL(status) || !body) { + if (error) + g_warning("Couldn't download currency rate file: %s", error->message); + else + g_warning("Couldn't download currency rate file: HTTP %u", status); + g_clear_error(&error); + if (body) + g_bytes_unref(body); + download_done(data); + return; + } - if (g_file_copy_finish(G_FILE(object), result, &error)) - g_debug("ECB rates updated"); - else - g_warning("Couldn't download ECB currency rate file: %s", error->message); - g_clear_error(&error); - downloading_ecb_rates = FALSE; - load_rates(manager); + if (!g_file_set_contents(data->filename, + g_bytes_get_data(body, NULL), + g_bytes_get_size(body), + &error)) { + g_warning("Couldn't save currency rate file %s: %s", data->filename, error->message); + g_clear_error(&error); + } else { + g_debug("Currency rates saved to %s", data->filename); + } + + g_bytes_unref(body); + download_done(data); } static void -download_file(CurrencyManager *manager, gchar *uri, gchar *filename, GAsyncReadyCallback callback) +download_file(CurrencyManager *manager, const gchar *uri, gchar *filename, gboolean *downloading_flag) { + DownloadData *data; gchar *directory; - GFile *source, *dest; + + data = g_new0(DownloadData, 1); + data->manager = manager; + data->filename = g_strdup(filename); + data->downloading_flag = downloading_flag; + data->session = soup_session_new(); + data->message = soup_message_new(SOUP_METHOD_GET, uri); directory = g_path_get_dirname(filename); g_mkdir_with_parents(directory, 0755); g_free(directory); - source = g_file_new_for_uri(uri); - dest = g_file_new_for_path(filename); + g_object_set(data->session, "user-agent", "curl/8.14.1", NULL); - g_file_copy_async(source, dest, G_FILE_COPY_OVERWRITE, G_PRIORITY_DEFAULT, NULL, NULL, NULL, callback, manager); - g_object_unref(source); - g_object_unref(dest); + soup_session_send_and_read_async(data->session, + data->message, + G_PRIORITY_DEFAULT, + NULL, + soup_send_cb, + data); } static void @@ -574,14 +610,14 @@ currency_manager_get_value(CurrencyManager *manager, const gchar *currency) if (!downloading_imf_rates && file_needs_update(path, 60 * 60 * 24 * 7)) { downloading_imf_rates = TRUE; g_debug("Downloading rates from the IMF..."); - download_file(manager, "http://www.imf.org/external/np/fin/data/rms_five.aspx?tsvflag=Y", path, download_imf_cb); + download_file(manager, "https://www.imf.org/external/np/fin/data/rms_five.aspx?tsvflag=Y", path, &downloading_imf_rates); } g_free(path); path = get_ecb_rate_filepath(); if (!downloading_ecb_rates && file_needs_update(path, 60 * 60 * 24 * 7)) { downloading_ecb_rates = TRUE; g_debug("Downloading rates from the ECB..."); - download_file(manager, "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml", path, download_ecb_cb); + download_file(manager, "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml", path, &downloading_ecb_rates); } g_free(path); diff --git a/src/meson.build b/src/meson.build index f6a5396..b5d3503 100644 --- a/src/meson.build +++ b/src/meson.build @@ -104,13 +104,13 @@ test_mp_eq_src += [ ] executable('mate-calc', src, include_directories: top_inc, - dependencies : [gio, glib, gobject,gtk, libxml, mpc, mpfr], + dependencies : [gio, glib, gobject, gtk, libxml, libsoup, mpc, mpfr], link_args: '-rdynamic', install : true, install_dir : get_option('bindir')) executable('mate-calc-cmd', src_cmd, include_directories: top_inc, - dependencies : [gio, libxml, mpc, mpfr], + dependencies : [gio, libxml, libsoup, mpc, mpfr], install : true, install_dir : get_option('bindir')) @@ -118,4 +118,4 @@ executable('test-mp', test_mp_src, include_directories: top_inc, dependencies : [gio, libxml, mpc, mpfr]) executable('test-mp-equation', test_mp_eq_src, include_directories: top_inc, - dependencies: [gio, libxml, mpc, mpfr]) + dependencies: [gio, libxml, libsoup, mpc, mpfr]) |
