[evince] cut-n-paste: Add libdazzle utilities to open file manager



commit 9e16eda4d233b1d1a3a5a1ed79cdb7b3f3b70131
Author: Germán Poo-Caamaño <gpoo gnome org>
Date:   Mon Jun 1 23:36:07 2020 -0400

    cut-n-paste: Add libdazzle utilities to open file manager

 cut-n-paste/libdazzle/dzl-file-manager.c   | 196 +++++++++++++++++++++++++++++
 cut-n-paste/libdazzle/dzl-file-manager.h   |  34 +++++
 cut-n-paste/libdazzle/dzl-version-macros.h |  28 +++++
 cut-n-paste/libdazzle/meson.build          |  20 +++
 cut-n-paste/meson.build                    |   1 +
 shell/meson.build                          |   1 +
 6 files changed, 280 insertions(+)
---
diff --git a/cut-n-paste/libdazzle/dzl-file-manager.c b/cut-n-paste/libdazzle/dzl-file-manager.c
new file mode 100644
index 00000000..44912654
--- /dev/null
+++ b/cut-n-paste/libdazzle/dzl-file-manager.c
@@ -0,0 +1,196 @@
+/* dzl-file-manager.c
+ *
+ * Copyright (C) 1995-2017 GIMP Authors
+ * Copyright (C) 2015-2017 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/>.
+ */
+
+#define G_LOG_DOMAIN "dzl-file-manager"
+
+#include "config.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
+
+#include "dzl-file-manager.h"
+
+/* Copied from the GIMP */
+gboolean
+dzl_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/cut-n-paste/libdazzle/dzl-file-manager.h b/cut-n-paste/libdazzle/dzl-file-manager.h
new file mode 100644
index 00000000..c8da6b52
--- /dev/null
+++ b/cut-n-paste/libdazzle/dzl-file-manager.h
@@ -0,0 +1,34 @@
+/* dzl-file-manager.h
+ *
+ * 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/>.
+ */
+
+#ifndef DZL_FILE_MANAGER_H
+#define DZL_FILE_MANAGER_H
+
+#include <gio/gio.h>
+
+#include "dzl-version-macros.h"
+
+G_BEGIN_DECLS
+
+DZL_AVAILABLE_IN_ALL
+gboolean dzl_file_manager_show (GFile   *file,
+                                GError **error);
+
+G_END_DECLS
+
+#endif /* DZL_FILE_MANAGER_H */
diff --git a/cut-n-paste/libdazzle/dzl-version-macros.h b/cut-n-paste/libdazzle/dzl-version-macros.h
new file mode 100644
index 00000000..ea394a8d
--- /dev/null
+++ b/cut-n-paste/libdazzle/dzl-version-macros.h
@@ -0,0 +1,28 @@
+/* dzl-version-macros.h
+ *
+ * Copyright (C) 2017 Christian Hergert <chergert redhat com>
+ *
+ * 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/>.
+ */
+
+#ifndef DZL_VERSION_MACROS_H
+#define DZL_VERSION_MACROS_H
+
+#ifndef _DZL_EXTERN
+#define _DZL_EXTERN extern
+#endif
+
+#define DZL_AVAILABLE_IN_ALL                   _DZL_EXTERN
+
+#endif /* DZL_VERSION_MACROS_H */
diff --git a/cut-n-paste/libdazzle/meson.build b/cut-n-paste/libdazzle/meson.build
new file mode 100644
index 00000000..48a97343
--- /dev/null
+++ b/cut-n-paste/libdazzle/meson.build
@@ -0,0 +1,20 @@
+sources = files(
+  'dzl-file-manager.c',
+)
+
+deps = [
+  glib_dep,
+]
+
+libdazzle = static_library(
+  'dazzle',
+  sources: sources,
+  include_directories: top_inc,
+  dependencies: deps,
+)
+
+libdazzle_dep = declare_dependency(
+  include_directories: include_directories('.'),
+  dependencies: deps,
+  link_with: libdazzle,
+)
diff --git a/cut-n-paste/meson.build b/cut-n-paste/meson.build
index 2a12d40e..0f4dbd30 100644
--- a/cut-n-paste/meson.build
+++ b/cut-n-paste/meson.build
@@ -1,6 +1,7 @@
 cut_n_paste_inc = include_directories('.')
 
 subdir('gimpcellrenderertoggle')
+subdir('libdazzle')
 subdir('libgd')
 subdir('unarr')
 
diff --git a/shell/meson.build b/shell/meson.build
index cc1db2ac..7d76839c 100644
--- a/shell/meson.build
+++ b/shell/meson.build
@@ -67,6 +67,7 @@ deps = [
   gnome_desktop_dep,
   libevmisc_dep,
   libevproperties_dep,
+  libdazzle_dep,
   libgd_dep,
   libgimpcellrenderertoggle_dep,
   m_dep,


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