Commit f1c82a3e authored by Christian Hergert's avatar Christian Hergert
Browse files

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
parent a7d1d9d1
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
*/ */
#define G_LOG_DOMAIN "ide-application-actions" #define G_LOG_DOMAIN "ide-application-actions"
#define DOCS_URI "https://builder.readthedocs.io"
#include "config.h" #include "config.h"
...@@ -149,41 +150,80 @@ ide_application_actions_about (GSimpleAction *action, ...@@ -149,41 +150,80 @@ ide_application_actions_about (GSimpleAction *action,
} }
static void static void
ide_application_actions_help (GSimpleAction *action, ide_application_actions_help_cb (GObject *object,
GVariant *param, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
IdeApplication *self = user_data; GNetworkMonitor *monitor = (GNetworkMonitor *)object;
GtkWindow *focused_window= NULL; g_autoptr(IdeApplication) self = user_data;
const gchar *uri = "https://builder.readthedocs.io"; GtkWindow *focused_window;
g_autoptr(GError) error = NULL;
GNetworkMonitor *monitor; IDE_ENTRY;
g_autofree gchar *real_uri = NULL;
g_assert (IDE_IS_APPLICATION (self)); g_assert (IDE_IS_APPLICATION (self));
g_assert (G_IS_ASYNC_RESULT (result));
focused_window = gtk_application_get_active_window (GTK_APPLICATION (self)); 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 * We failed to reach the online site for some reason (offline, transient error, etc),
* documentation. To do that we might need to translate it to the * so instead try to load the local documentation.
* path for which the host has access.
*/ */
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)) const gchar *uri;
{ g_autofree gchar *real_uri = NULL;
if (ide_is_flatpak ()) g_autoptr(GError) error = NULL;
uri = real_uri = ide_flatpak_get_app_path ("/share/doc/gnome-builder/html/index.html");
else if (ide_is_flatpak ())
uri = "file://"PACKAGE_DOCDIR"/html/index.html"; 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)) IDE_EXIT;
g_message ("Unable to open help: %s\n", error->message); }
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 static void
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment