[gnome-builder] help: use g_network_monitor_can_reach_async()



commit f1c82a3e00390f36445054911b39108e71d40388
Author: Christian Hergert <chergert redhat com>
Date:   Tue Mar 14 14:52:46 2017 -0700

    help: use g_network_monitor_can_reach_async()
    
    We cannot rely on g_network_monitor_get_available() being specific enough
    to tell us if we can reach builder.readthedocs.io, so use the can-reach
    async API instead.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=780035

 libide/application/ide-application-actions.c |   86 +++++++++++++++++++-------
 1 files changed, 63 insertions(+), 23 deletions(-)
---
diff --git a/libide/application/ide-application-actions.c b/libide/application/ide-application-actions.c
index 2249e17..acccd32 100644
--- a/libide/application/ide-application-actions.c
+++ b/libide/application/ide-application-actions.c
@@ -17,6 +17,7 @@
  */
 
 #define G_LOG_DOMAIN "ide-application-actions"
+#define DOCS_URI "https://builder.readthedocs.io";
 
 #include "config.h"
 
@@ -149,41 +150,80 @@ ide_application_actions_about (GSimpleAction *action,
 }
 
 static void
-ide_application_actions_help (GSimpleAction *action,
-                              GVariant      *param,
-                              gpointer       user_data)
+ide_application_actions_help_cb (GObject      *object,
+                                 GAsyncResult *result,
+                                 gpointer      user_data)
 {
-  IdeApplication *self = user_data;
-  GtkWindow *focused_window= NULL;
-  const gchar *uri = "https://builder.readthedocs.io";;
-  g_autoptr(GError) error = NULL;
-  GNetworkMonitor *monitor;
-  g_autofree gchar *real_uri = NULL;
+  GNetworkMonitor *monitor = (GNetworkMonitor *)object;
+  g_autoptr(IdeApplication) self = user_data;
+  GtkWindow *focused_window;
+
+  IDE_ENTRY;
 
   g_assert (IDE_IS_APPLICATION (self));
+  g_assert (G_IS_ASYNC_RESULT (result));
 
   focused_window = gtk_application_get_active_window (GTK_APPLICATION (self));
 
-  monitor = g_network_monitor_get_default ();
+  /*
+   * If we can reach the documentation website, prefer showing up-to-date
+   * documentation from the website.
+   */
+  if (g_network_monitor_can_reach_finish (monitor, result, NULL))
+    {
+      if (gtk_show_uri_on_window (focused_window, DOCS_URI, gtk_get_current_event_time (), NULL))
+        IDE_EXIT;
+    }
 
   /*
-   * If we don't have network access, we should try to use the local
-   * documentation. To do that we might need to translate it to the
-   * path for which the host has access.
+   * We failed to reach the online site for some reason (offline, transient error, etc),
+   * so instead try to load the local documentation.
    */
-  if (!g_network_monitor_get_network_available (monitor))
+  if (g_file_test (PACKAGE_DOCDIR"/html/index.html", G_FILE_TEST_IS_REGULAR))
     {
-      if (g_file_test (PACKAGE_DOCDIR"/html/index.html", G_FILE_TEST_IS_REGULAR))
-        {
-          if (ide_is_flatpak ())
-            uri = real_uri = ide_flatpak_get_app_path ("/share/doc/gnome-builder/html/index.html");
-          else
-            uri = "file://"PACKAGE_DOCDIR"/html/index.html";
-        }
+      const gchar *uri;
+      g_autofree gchar *real_uri = NULL;
+      g_autoptr(GError) error = NULL;
+
+      if (ide_is_flatpak ())
+        uri = real_uri = ide_flatpak_get_app_path ("/share/doc/gnome-builder/html/index.html");
+      else
+        uri = "file://"PACKAGE_DOCDIR"/html/index.html";
+
+      if (!gtk_show_uri_on_window (focused_window, uri, gtk_get_current_event_time (), &error))
+        g_warning ("Failed to load documentation: %s", error->message);
     }
 
-  if (!gtk_show_uri_on_window (focused_window, uri, gtk_get_current_event_time (), &error))
-    g_message ("Unable to open help: %s\n", error->message);
+  IDE_EXIT;
+}
+
+static void
+ide_application_actions_help (GSimpleAction *action,
+                              GVariant      *param,
+                              gpointer       user_data)
+{
+  IdeApplication *self = user_data;
+  g_autoptr(GSocketConnectable) network_address = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (G_IS_SIMPLE_ACTION (action));
+  g_assert (IDE_IS_APPLICATION (self));
+
+  /*
+   * Check for access to the internet. Sadly, we cannot use
+   * g_network_monitor_get_network_available() because that does not seem to
+   * act correctly on some systems (Ubuntu appears to be one example). So
+   * instead, we can asynchronously check if we can reach the peer first.
+   */
+  network_address = g_network_address_parse_uri (DOCS_URI, 443, NULL);
+  g_network_monitor_can_reach_async (g_network_monitor_get_default (),
+                                     network_address,
+                                     NULL,
+                                     ide_application_actions_help_cb,
+                                     g_object_ref (self));
+
+  IDE_EXIT;
 }
 
 static void


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