[gnome-panel/gnome-3-36] menu: handle G_IO_ERROR_NOT_MOUNTED error



commit 9fe1004685c0ed6c449fc18bf664191c64644569
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Sat Jul 11 18:35:16 2020 +0300

    menu: handle G_IO_ERROR_NOT_MOUNTED error
    
    https://gitlab.gnome.org/GNOME/gnome-panel/-/issues/27

 modules/menu/gp-menu-utils.c | 119 ++++++++++++++++++++++++++++++++++++-------
 1 file changed, 101 insertions(+), 18 deletions(-)
---
diff --git a/modules/menu/gp-menu-utils.c b/modules/menu/gp-menu-utils.c
index a83e7f0cb..5477e419a 100644
--- a/modules/menu/gp-menu-utils.c
+++ b/modules/menu/gp-menu-utils.c
@@ -338,6 +338,89 @@ get_app_info_for_uri (const gchar  *uri,
   return app_info;
 }
 
+static gboolean
+launch_uri (const char  *uri,
+            GError     **error)
+{
+  GAppInfo *app_info;
+
+  app_info = get_app_info_for_uri (uri, error);
+
+  if (app_info != NULL)
+    {
+      GList *uris;
+      gboolean success;
+
+      uris = g_list_append (NULL, (gchar *) uri);
+      success = app_info_launch_uris (G_DESKTOP_APP_INFO (app_info),
+                                      uris, error);
+
+      g_object_unref (app_info);
+      g_list_free (uris);
+
+      return success;
+    }
+
+  return FALSE;
+}
+
+static void
+launch_uri_show_error_dialog (const char *uri,
+                              GError     *error)
+{
+  char *message;
+
+  message = g_strdup_printf (_("Could not open location '%s'"), uri);
+
+  gp_menu_utils_show_error_dialog (message, error);
+  g_free (message);
+}
+
+static void
+mount_enclosing_volume_cb (GObject      *source_object,
+                           GAsyncResult *res,
+                           gpointer      user_data)
+{
+  GFile *file;
+  GMountOperation *operation;
+  GError *error;
+
+  file = G_FILE (source_object);
+  operation = G_MOUNT_OPERATION (user_data);
+  error = NULL;
+
+  if (g_file_mount_enclosing_volume_finish (file, res, &error))
+    {
+      char *uri;
+
+      uri = g_file_get_uri (file);
+
+      if (!launch_uri (uri, &error))
+        {
+          launch_uri_show_error_dialog (uri, error);
+          g_clear_error (&error);
+        }
+
+      g_free (uri);
+    }
+  else
+    {
+      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED) &&
+          !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
+        {
+          char *uri;
+
+          uri = g_file_get_uri (file);
+          launch_uri_show_error_dialog (uri, error);
+          g_free (uri);
+        }
+
+      g_clear_error (&error);
+    }
+
+  g_object_unref (operation);
+}
+
 void
 gp_menu_utils_app_info_launch (GDesktopAppInfo *app_info)
 {
@@ -362,34 +445,34 @@ void
 gp_menu_utils_launch_uri (const gchar *uri)
 {
   GError *error;
-  GAppInfo *app_info;
-  gchar *message;
 
   error = NULL;
-  app_info = get_app_info_for_uri (uri, &error);
+  if (launch_uri (uri, &error))
+    return;
 
-  if (app_info != NULL)
+  if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
     {
-      GList *uris;
-      gboolean success;
+      GFile *file;
+      GMountOperation *operation;
 
-      uris = g_list_append (NULL, (gchar *) uri);
-      success = app_info_launch_uris (G_DESKTOP_APP_INFO (app_info),
-                                      uris, &error);
-
-      g_object_unref (app_info);
-      g_list_free (uris);
+      file = g_file_new_for_uri (uri);
+      operation = gtk_mount_operation_new (NULL);
 
-      if (success)
-        return;
-    }
+      g_file_mount_enclosing_volume (file,
+                                     G_MOUNT_MOUNT_NONE,
+                                     operation,
+                                     NULL,
+                                     mount_enclosing_volume_cb,
+                                     operation);
 
-  message = g_strdup_printf (_("Could not open location '%s'"), uri);
+      g_clear_error (&error);
+      g_object_unref (file);
 
-  gp_menu_utils_show_error_dialog (message, error);
+      return;
+    }
 
+  launch_uri_show_error_dialog (uri, error);
   g_clear_error (&error);
-  g_free (message);
 }
 
 GIcon *


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