[gnome-photos] local-item: Pre-select the file when opening with Nautilus



commit 67fbd531db1b77c019bfcdacd137976bdc935dd9
Author: Umang Jain <mailumangjain gmail com>
Date:   Tue Jan 16 22:59:08 2018 +0530

    local-item: Pre-select the file when opening with Nautilus
    
    Nautilus can select a specified URI in its parent folder:
      $ nautilus --select file:///uri/of/the/actual/item
    
    Based on GLib's g_app_info_launch_default_for_uri* implementation.
    
    Some changes by Debarshi Ray.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=759413

 src/photos-local-item.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)
---
diff --git a/src/photos-local-item.c b/src/photos-local-item.c
index 0ce6b40a..dbca2296 100644
--- a/src/photos-local-item.c
+++ b/src/photos-local-item.c
@@ -22,6 +22,7 @@
 
 /* Based on code from:
  *   + Documents
+ *   + GLib
  */
 
 
@@ -53,6 +54,94 @@ G_DEFINE_TYPE_WITH_CODE (PhotosLocalItem, photos_local_item, PHOTOS_TYPE_BASE_IT
                                                          0));
 
 
+static gboolean
+photos_local_item_source_widget_activate_link (GtkLinkButton *button, gpointer user_data)
+{
+  g_autoptr (GAppInfo) default_app = NULL;
+  g_autoptr (GAppInfo) nautilus_app = NULL;
+  g_autoptr (GAppLaunchContext) ctx = NULL;
+  PhotosLocalItem *self;
+  gboolean ret_val = GDK_EVENT_PROPAGATE;
+  const gchar *commandline = "nautilus --select";
+  const gchar *default_app_id;
+  const gchar *source_uri;
+  const gchar *uri;
+  g_autofree gchar *command_line = NULL;
+  g_autofree gchar *source_uri_scheme = NULL;
+
+  g_return_val_if_fail (GTK_IS_LINK_BUTTON (button), GDK_EVENT_PROPAGATE);
+  g_return_val_if_fail (PHOTOS_IS_LOCAL_ITEM (user_data), GDK_EVENT_PROPAGATE);
+
+  self = PHOTOS_LOCAL_ITEM (user_data);
+
+  source_uri = gtk_link_button_get_uri (button);
+
+  /* Even though g_file_query_default_handler calls
+   * g_app_info_get_default_for_uri_scheme, we have to do it here in
+   * case GFile can't parse source_uri correctly.
+   *
+   * See glib/gio/gappinfo.c
+   */
+
+  source_uri_scheme = g_uri_parse_scheme (source_uri);
+  if (source_uri_scheme != NULL && source_uri_scheme[0] != '\0')
+    default_app = g_app_info_get_default_for_uri_scheme (source_uri_scheme);
+
+  if (default_app == NULL)
+    {
+      g_autoptr (GFile) source_link = NULL;
+
+      source_link = g_file_new_for_uri (source_uri);
+
+      {
+        g_autoptr (GError) error = NULL;
+
+        default_app = g_file_query_default_handler (source_link, NULL, &error);
+        if (error != NULL)
+          {
+            g_warning ("Unable to query default handler for %s: %s", source_uri, error->message);
+            goto out;
+          }
+      }
+    }
+
+  g_return_val_if_fail (G_IS_APP_INFO (default_app), GDK_EVENT_PROPAGATE);
+
+  default_app_id = g_app_info_get_id (default_app);
+  if (g_strcmp0 (default_app_id, "org.gnome.Nautilus.desktop") != 0)
+    goto out;
+
+  {
+    g_autoptr (GError) error = NULL;
+
+    nautilus_app = g_app_info_create_from_commandline (commandline, NULL, G_APP_INFO_CREATE_NONE, &error);
+    if (error != NULL)
+      {
+        g_warning ("Unable to create GAppInfo from '%s': %s", commandline, error->message);
+        goto out;
+      }
+  }
+
+  uri = photos_base_item_get_uri (PHOTOS_BASE_ITEM (self));
+  ctx = photos_utils_new_app_launch_context_from_widget (GTK_WIDGET (button));
+
+  {
+    g_autoptr (GError) error = NULL;
+
+    if (!photos_glib_app_info_launch_uri (nautilus_app, uri, ctx, &error))
+      {
+        g_warning ("Unable to launch '%s': %s", commandline, error->message);
+        goto out;
+      }
+  }
+
+  ret_val = GDK_EVENT_STOP;
+
+ out:
+  return ret_val;
+}
+
+
 static gchar *
 photos_local_item_create_filename_fallback (PhotosBaseItem *item)
 {
@@ -189,6 +278,11 @@ photos_local_item_get_source_widget (PhotosBaseItem *item)
 
       source_widget = gtk_link_button_new_with_label (source_uri, source_path);
       gtk_widget_set_halign (source_widget, GTK_ALIGN_START);
+      g_signal_connect_object (source_widget,
+                               "activate-link",
+                               G_CALLBACK (photos_local_item_source_widget_activate_link),
+                               item,
+                               0);
 
       label = gtk_bin_get_child (GTK_BIN (source_widget));
       gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);


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