[gnome-builder] Add generic show in File Manager helper



commit c46e22384da17185cf4b209525757f8204c67cb9
Author: Garrett Regier <garrettregier gmail com>
Date:   Thu Apr 9 12:56:33 2015 -0700

    Add generic show in File Manager helper
    
    And use it instead the current GAppInfo helper.
    This has not been tested on Windows or OS X.

 src/editor/gb-editor-workspace-actions.c      |    1 -
 src/editor/gb-project-tree-actions.c          |    4 +-
 src/gnome-builder.mk                          |    4 +-
 src/util/gb-file-manager.c                    |  191 +++++++++++++++++++++++++
 src/util/{gb-nautilus.h => gb-file-manager.h} |   14 +-
 src/util/gb-nautilus.c                        |   69 ---------
 6 files changed, 201 insertions(+), 82 deletions(-)
---
diff --git a/src/editor/gb-editor-workspace-actions.c b/src/editor/gb-editor-workspace-actions.c
index 08cae57..8d2c818 100644
--- a/src/editor/gb-editor-workspace-actions.c
+++ b/src/editor/gb-editor-workspace-actions.c
@@ -20,7 +20,6 @@
 
 #include "gb-editor-workspace-actions.h"
 #include "gb-editor-workspace-private.h"
-#include "gb-nautilus.h"
 #include "gb-widget.h"
 #include "gb-workbench.h"
 
diff --git a/src/editor/gb-project-tree-actions.c b/src/editor/gb-project-tree-actions.c
index bd4fd74..8a0c097 100644
--- a/src/editor/gb-project-tree-actions.c
+++ b/src/editor/gb-project-tree-actions.c
@@ -21,7 +21,7 @@
 
 #include "gb-editor-workspace.h"
 #include "gb-editor-workspace-private.h"
-#include "gb-nautilus.h"
+#include "gb-file-manager.h"
 #include "gb-tree.h"
 #include "gb-widget.h"
 #include "gb-workbench.h"
@@ -284,7 +284,7 @@ gb_project_tree_actions_open_containing_folder (GSimpleAction *action,
       if (!file)
         return;
 
-      gb_nautilus_select_file (GTK_WIDGET (self), file, GDK_CURRENT_TIME);
+      gb_file_manager_show (file, NULL);
     }
 }
 
diff --git a/src/gnome-builder.mk b/src/gnome-builder.mk
index 2c65715..71e81e5 100644
--- a/src/gnome-builder.mk
+++ b/src/gnome-builder.mk
@@ -118,12 +118,12 @@ libgnome_builder_la_SOURCES = \
        src/util/gb-cairo.h \
        src/util/gb-dnd.c \
        src/util/gb-dnd.h \
+       src/util/gb-file-manager.c \
+       src/util/gb-file-manager.h \
        src/util/gb-glib.c \
        src/util/gb-glib.h \
        src/util/gb-gtk.c \
        src/util/gb-gtk.h \
-       src/util/gb-nautilus.c \
-       src/util/gb-nautilus.h \
        src/util/gb-pango.c \
        src/util/gb-pango.h \
        src/util/gb-rgba.c \
diff --git a/src/util/gb-file-manager.c b/src/util/gb-file-manager.c
new file mode 100644
index 0000000..c054021
--- /dev/null
+++ b/src/util/gb-file-manager.c
@@ -0,0 +1,191 @@
+/* gb-file-manager.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "gb-file-manager.h"
+
+#include <glib/gi18n.h>
+
+#if defined(G_OS_WIN32)
+/* This is a hack for Windows known directory support.
+ * DATADIR (autotools-generated constant) is a type defined in objidl.h
+ * so we must #undef it before including shlobj.h in order to avoid a
+ * name clash. */
+#undef DATADIR
+#include <windows.h>
+#include <shlobj.h>
+#endif
+
+#ifdef PLATFORM_OSX
+#include <AppKit/AppKit.h>
+#endif
+
+/* Copied from the GIMP */
+gboolean
+gb_file_manager_show (GFile   *file,
+                      GError **error)
+{
+  g_return_val_if_fail (G_IS_FILE (file), FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+#if defined(G_OS_WIN32)
+
+  {
+    gboolean ret;
+    char *filename;
+    int n;
+    LPWSTR w_filename = NULL;
+    ITEMIDLIST *pidl = NULL;
+
+    ret = FALSE;
+
+    /* Calling this function mutiple times should do no harm, but it is
+       easier to put this here as it needs linking against ole32. */
+    CoInitialize (NULL);
+
+    filename = g_file_get_path (file);
+    if (!filename)
+      {
+        g_set_error_literal (error, G_FILE_ERROR, 0,
+                             _("File path is NULL"));
+        goto out;
+      }
+
+    n = MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS,
+                             filename, -1, NULL, 0);
+    if (n == 0)
+      {
+        g_set_error_literal (error, G_FILE_ERROR, 0,
+                             _("Error converting UTF-8 filename to wide char"));
+        goto out;
+      }
+
+    w_filename = g_malloc_n (n + 1, sizeof (wchar_t));
+    n = MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS,
+                             filename, -1,
+                             w_filename, (n + 1) * sizeof (wchar_t));
+    if (n == 0)
+      {
+        g_set_error_literal (error, G_FILE_ERROR, 0,
+                             _("Error converting UTF-8 filename to wide char"));
+        goto out;
+      }
+
+    pidl = ILCreateFromPathW (w_filename);
+    if (!pidl)
+      {
+        g_set_error_literal (error, G_FILE_ERROR, 0,
+                             _("ILCreateFromPath() failed"));
+        goto out;
+      }
+
+    SHOpenFolderAndSelectItems (pidl, 0, NULL, 0);
+    ret = TRUE;
+
+  out:
+    if (pidl)
+      ILFree (pidl);
+    g_free (w_filename);
+    g_free (filename);
+
+    return ret;
+  }
+
+#elif defined(PLATFORM_OSX)
+
+  {
+    gchar    *uri;
+    NSString *filename;
+    NSURL    *url;
+    gboolean  retval = TRUE;
+
+    uri = g_file_get_uri (file);
+    filename = [NSString stringWithUTF8String:uri];
+
+    url = [NSURL URLWithString:filename];
+    if (url)
+      {
+        NSArray *url_array = [NSArray arrayWithObject:url];
+
+        [[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:url_array];
+      }
+    else
+      {
+        g_set_error (error, G_FILE_ERROR, 0,
+                     _("Cannot convert '%s' into a valid NSURL."), uri);
+        retval = FALSE;
+      }
+
+    g_free (uri);
+
+    return retval;
+  }
+
+#else /* UNIX */
+
+  {
+    GDBusProxy      *proxy;
+    GVariant        *retval;
+    GVariantBuilder *builder;
+    gchar           *uri;
+
+    proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+                                           G_DBUS_PROXY_FLAGS_NONE,
+                                           NULL,
+                                           "org.freedesktop.FileManager1",
+                                           "/org/freedesktop/FileManager1",
+                                           "org.freedesktop.FileManager1",
+                                           NULL, error);
+
+    if (!proxy)
+      {
+        g_prefix_error (error,
+                        _("Connecting to org.freedesktop.FileManager1 failed: "));
+        return FALSE;
+      }
+
+    uri = g_file_get_uri (file);
+
+    builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
+    g_variant_builder_add (builder, "s", uri);
+
+    g_free (uri);
+
+    retval = g_dbus_proxy_call_sync (proxy,
+                                     "ShowItems",
+                                     g_variant_new ("(ass)",
+                                                    builder,
+                                                    ""),
+                                     G_DBUS_CALL_FLAGS_NONE,
+                                     -1, NULL, error);
+
+    g_variant_builder_unref (builder);
+    g_object_unref (proxy);
+
+    if (!retval)
+      {
+        g_prefix_error (error, _("Calling ShowItems failed: "));
+        return FALSE;
+      }
+
+    g_variant_unref (retval);
+
+    return TRUE;
+  }
+
+#endif
+}
diff --git a/src/util/gb-nautilus.h b/src/util/gb-file-manager.h
similarity index 74%
rename from src/util/gb-nautilus.h
rename to src/util/gb-file-manager.h
index fcd7ade..1f141ec 100644
--- a/src/util/gb-nautilus.h
+++ b/src/util/gb-file-manager.h
@@ -1,4 +1,4 @@
-/* gb-nautilus.h
+/* gb-file-manager.h
  *
  * Copyright (C) 2015 Christian Hergert <christian hergert me>
  *
@@ -16,18 +16,16 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef GB_NAUTILUS_H
-#define GB_NAUTILUS_H
+#ifndef GB_FILE_MANAGER_H
+#define GB_FILE_MANAGER_H
 
 #include <gio/gio.h>
-#include <gtk/gtk.h>
 
 G_BEGIN_DECLS
 
-gboolean gb_nautilus_select_file (GtkWidget *widget,
-                                  GFile     *file,
-                                  guint32    user_time);
+gboolean gb_file_manager_show (GFile   *file,
+                               GError **error);
 
 G_END_DECLS
 
-#endif /* GB_NAUTILUS_H */
+#endif /* GB_FILE_MANAGER_H */


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