[evolution-data-server] ENetworkMonitor: Implement also the async part of the GNetworkMonitor interface



commit c66ddd940c1e6d00ba2b99f2987ca7a2d149ec46
Author: Milan Crha <mcrha redhat com>
Date:   Mon Jun 13 13:43:36 2016 +0200

    ENetworkMonitor: Implement also the async part of the GNetworkMonitor interface
    
    It seemed like the async part is provided for free and the interface
    lets it override for descendants only when needed, but it turned out
    that it's not the true, thus the async part of the interface is defined
    too.
    
    This exhibited for example when entering the Calendar view, which called
    this async part to figure out whether the calendars are reachable, but
    as the default implementation only calls the synchronous part, then it
    froze the UI for some time, instead of doing things really asynchronously.

 libedataserver/e-network-monitor.c |   55 ++++++++++++++++++++++++++++++++++++
 1 files changed, 55 insertions(+), 0 deletions(-)
---
diff --git a/libedataserver/e-network-monitor.c b/libedataserver/e-network-monitor.c
index c80d352..f2b15d4 100644
--- a/libedataserver/e-network-monitor.c
+++ b/libedataserver/e-network-monitor.c
@@ -376,9 +376,64 @@ e_network_monitor_can_reach (GNetworkMonitor *monitor,
 }
 
 static void
+e_network_monitor_can_reach_async_thread (GTask *task,
+                                         gpointer source_object,
+                                         gpointer task_data,
+                                         GCancellable *cancellable)
+{
+       gboolean success;
+       GError *local_error = NULL;
+
+       success = e_network_monitor_can_reach (source_object, task_data, cancellable, &local_error);
+
+       if (local_error)
+               g_task_return_error (task, local_error);
+       else
+               g_task_return_boolean (task, success);
+}
+
+static void
+e_network_monitor_can_reach_async (GNetworkMonitor *monitor,
+                                  GSocketConnectable *connectable,
+                                  GCancellable *cancellable,
+                                  GAsyncReadyCallback callback,
+                                  gpointer user_data)
+{
+       GTask *task;
+
+       g_return_if_fail (E_IS_NETWORK_MONITOR (monitor));
+       g_return_if_fail (G_IS_SOCKET_CONNECTABLE (connectable));
+
+       task = g_task_new (monitor, cancellable, callback, user_data);
+       g_task_set_source_tag (task, e_network_monitor_can_reach_async);
+       g_task_set_task_data (task, g_object_ref (connectable), g_object_unref);
+
+       g_task_run_in_thread (task, e_network_monitor_can_reach_async_thread);
+
+       g_object_unref (task);
+}
+
+static gboolean
+e_network_monitor_can_reach_finish (GNetworkMonitor *monitor,
+                                   GAsyncResult *result,
+                                   GError **error)
+{
+       g_return_val_if_fail (E_IS_NETWORK_MONITOR (monitor), FALSE);
+       g_return_val_if_fail (g_task_is_valid (result, monitor), FALSE);
+
+       g_return_val_if_fail (
+               g_async_result_is_tagged (
+               result, e_network_monitor_can_reach_async), FALSE);
+
+       return g_task_propagate_boolean (G_TASK (result), error);
+}
+
+static void
 e_network_monitor_gio_iface_init (GNetworkMonitorInterface *iface)
 {
        iface->can_reach = e_network_monitor_can_reach;
+       iface->can_reach_async = e_network_monitor_can_reach_async;
+       iface->can_reach_finish = e_network_monitor_can_reach_finish;
 
        if (!network_changed_signal)
                network_changed_signal = g_signal_lookup ("network-changed", G_TYPE_NETWORK_MONITOR);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]