From 211bdae802a0ba904b306efee41c5ed1dc85287e Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Wed, 23 Jan 2013 06:33:08 +0000 Subject: [PATCH] Add canceling to alerts plugins This helps speed up shutdown if it occurs while loading alert details. It's not _really_ needed because of the new grits_http_abort function, but it's still nice to prevent a bunch of extra fetch attempts. This also fixes some warnings when a download fails. --- src/plugins/alert.c | 27 ++++++++++++++++++++------- src/plugins/alert.h | 1 + 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/plugins/alert.c b/src/plugins/alert.c index 73cb564..cc9f946 100644 --- a/src/plugins/alert.c +++ b/src/plugins/alert.c @@ -321,22 +321,22 @@ GList *msg_load_index(GritsHttp *http, time_t when, time_t *updated, gboolean of return msgs; } -void msg_load_cap(GritsHttp *http, AlertMsg *msg) +gboolean msg_load_cap(GritsHttp *http, AlertMsg *msg) { if (msg->description || msg->instruction || msg->polygon) - return; + return TRUE; g_debug("GritsPlguinAlert: update_cap"); /* Download */ gchar *id = strrchr(msg->link, '='); - if (!id) return; id++; + if (!id) return FALSE; id++; gchar *dir = g_strdelimit(g_strdup(msg->info->abbr), " ", '_'); gchar *tmp = g_strdup_printf("%s/%s.xml", dir, id); gchar *file = grits_http_fetch(http, msg->link, tmp, GRITS_ONCE, NULL, NULL); g_free(tmp); g_free(dir); if (!file) - return; + return FALSE; /* Parse */ gchar *text; gsize len; @@ -344,6 +344,7 @@ void msg_load_cap(GritsHttp *http, AlertMsg *msg) msg_parse_cap(msg, text, len); g_free(file); g_free(text); + return TRUE; } @@ -503,7 +504,10 @@ static gboolean _show_details(GritsPoly *county, GdkEvent *_, GritsPluginAlert * { /* Add details for this messages */ AlertMsg *msg = g_object_get_data(G_OBJECT(county), "msg"); - msg_load_cap(alert->http, msg); + + // TODO: move this to a thread since it blocks on net access + if (!msg_load_cap(alert->http, msg)) + return FALSE; GtkWidget *dialog = alert->details; GtkWidget *content = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); @@ -577,7 +581,11 @@ static GritsPoly *_load_storm_based(GritsPluginAlert *alert, AlertMsg *msg) if (!msg->info->ispoly) return NULL; - msg_load_cap(alert->http, msg); + if (!msg_load_cap(alert->http, msg)) + return NULL; + + if (!msg->polygon) + return NULL; GritsPoly *poly = grits_poly_parse(msg->polygon, "\t", " ", ","); _load_common(alert, msg, poly, 0, 1, 3, "alert/hide_storm_based"); @@ -767,6 +775,8 @@ static void _update_warnings(GritsPluginAlert *alert, GList *old) for (GList *cur = alert->msgs; cur; cur = cur->next) { AlertMsg *msg = cur->data; msg->storm_based = _load_storm_based(alert, msg); + if (alert->aborted) + return; } grits_viewer_queue_draw(alert->viewer); @@ -777,6 +787,8 @@ static void _update_warnings(GritsPluginAlert *alert, GList *old) static void _update(gpointer _, gpointer _alert) { GritsPluginAlert *alert = _alert; + if (alert->aborted) + return; GList *old = alert->msgs; g_debug("GritsPluginAlert: _update"); @@ -956,12 +968,13 @@ static void grits_plugin_alert_dispose(GObject *gobject) { g_debug("GritsPluginAlert: dispose"); GritsPluginAlert *alert = GRITS_PLUGIN_ALERT(gobject); + alert->aborted = TRUE; /* Drop references */ if (alert->viewer) { GritsViewer *viewer = alert->viewer; g_signal_handler_disconnect(viewer, alert->refresh_id); g_signal_handler_disconnect(viewer, alert->time_changed_id); - soup_session_abort(alert->http->soup); + grits_http_abort(alert->http); g_thread_pool_free(alert->threads, TRUE, TRUE); if (alert->update_source) g_source_remove(alert->update_source); diff --git a/src/plugins/alert.h b/src/plugins/alert.h index 299131f..488b726 100644 --- a/src/plugins/alert.h +++ b/src/plugins/alert.h @@ -45,6 +45,7 @@ struct _GritsPluginAlert { guint time_changed_id; guint update_source; GThreadPool *threads; + gboolean aborted; GList *msgs; time_t updated; -- 2.41.0