[gimp] plug-ins: add some MacOS support to find RawTherapee.



commit b3eb87606933fdc380c6c370c9eed2f0686c705f
Author: Jehan <jehan girinstud io>
Date:   Sat Jul 29 01:15:28 2017 +0200

    plug-ins: add some MacOS support to find RawTherapee.
    
    Hopefully it works, since that's untested (I have no MacOS access). This
    is mostly code from file-darktable.c, made generic and factorized into
    file-raw-utils.[ch]. At least it still builds and work fine on Linux.
    I used the CFBundleIdentifier from `tools/osx/Info.plist.in` in
    RawTherapee code. I didn't add a Win32 registry key base though, since I
    couldn't find any relevant data for this in RawTherapee code.

 plug-ins/file-raw/Makefile.am        |    4 +
 plug-ins/file-raw/file-darktable.c   |  127 +++++----------------------------
 plug-ins/file-raw/file-raw-utils.c   |  133 ++++++++++++++++++++++++++++++++++
 plug-ins/file-raw/file-raw-utils.h   |   26 +++++++
 plug-ins/file-raw/file-rawtherapee.c |   42 ++++++++---
 5 files changed, 212 insertions(+), 120 deletions(-)
---
diff --git a/plug-ins/file-raw/Makefile.am b/plug-ins/file-raw/Makefile.am
index bf3bcc6..468f3fa 100644
--- a/plug-ins/file-raw/Makefile.am
+++ b/plug-ins/file-raw/Makefile.am
@@ -33,10 +33,14 @@ libexec_PROGRAMS = \
 
 file_darktable_SOURCES = \
        file-darktable.c        \
+       file-raw-utils.c        \
+       file-raw-utils.h        \
        file-formats.h
 
 file_rawtherapee_SOURCES = \
        file-rawtherapee.c      \
+       file-raw-utils.c        \
+       file-raw-utils.h        \
        file-formats.h
 
 file_raw_placeholder_SOURCES = \
diff --git a/plug-ins/file-raw/file-darktable.c b/plug-ins/file-raw/file-darktable.c
index a7feab7..d32cb38 100644
--- a/plug-ins/file-raw/file-darktable.c
+++ b/plug-ins/file-raw/file-darktable.c
@@ -32,19 +32,11 @@
 #include "libgimp/stdplugins-intl.h"
 
 #include "file-formats.h"
+#include "file-raw-utils.h"
 
-#ifdef GDK_WINDOWING_QUARTZ
-#include <CoreServices/CoreServices.h>
-#endif
-
-#ifdef GDK_WINDOWING_WIN32
-#include <Windows.h>
-#endif
 
 #define LOAD_THUMB_PROC "file-darktable-load-thumb"
-
-static gchar   *get_executable_path  (const gchar      *suffix,
-                                      gboolean         *search_path);
+#define REGISTRY_KEY_BASE "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\darktable"
 
 static void     init                 (void);
 static void     query                (void);
@@ -73,103 +65,6 @@ const GimpPlugInInfo PLUG_IN_INFO =
 
 MAIN ()
 
-static gchar *
-get_executable_path (const gchar *suffix,
-                     gboolean    *search_path)
-{
-  /*
-   * First check for the environment variable DARKTABLE_EXECUTABLE.
-   * Next do platform specific checks (bundle lookup on Mac, registry stuff
-   * on Windows).
-   * Last resort is hoping for darktable to be in PATH.
-   */
-
-  /*
-   * Look for env variable. That can be set directly or via an environ file.
-   * We assume that just appendign the suffix to that value will work.
-   * That means that on Windows there should be no ".exe"!
-   */
-  const gchar *dt_env = g_getenv ("DARKTABLE_EXECUTABLE");
-  if (dt_env)
-    return g_strconcat (dt_env, suffix, NULL);
-
-#if defined (GDK_WINDOWING_QUARTZ)
-  {
-    OSStatus status;
-    CFURLRef bundle_url = NULL;
-
-    /* For macOS, attempt searching for a darktable app bundle first. */
-    status = LSFindApplicationForInfo (kLSUnknownCreator,
-                                       CFSTR ("org.darktable"),
-                                       NULL, NULL, &bundle_url);
-
-    if (status >= 0)
-      {
-        CFBundleRef bundle;
-        CFURLRef exec_url, absolute_url;
-        CFStringRef path;
-        gchar *ret;
-        CFIndex len;
-
-        bundle = CFBundleCreate (kCFAllocatorDefault, bundle_url);
-        CFRelease (bundle_url);
-
-        exec_url = CFBundleCopyExecutableURL (bundle);
-        absolute_url = CFURLCopyAbsoluteURL (exec_url);
-        path = CFURLCopyFileSystemPath (absolute_url, kCFURLPOSIXPathStyle);
-
-        /* This gets us the length in UTF16 characters, we multiply by 2
-        * to make sure we have a buffer big enough to fit the UTF8 string.
-        */
-        len = CFStringGetLength (path);
-        ret = g_malloc0 (len * 2 * sizeof (gchar));
-        if (!CFStringGetCString (path, ret, 2 * len * sizeof (gchar),
-                                 kCFStringEncodingUTF8))
-          ret = NULL;
-
-        CFRelease (path);
-        CFRelease (absolute_url);
-        CFRelease (exec_url);
-        CFRelease (bundle);
-
-        if (ret)
-          return ret;
-      }
-    /* else, app bundle was not found, try path search as last resort. */
-  }
-#elif defined (GDK_WINDOWING_WIN32)
-  {
-    /* Look for darktable in the Windows registry. */
-
-    char *registry_key;
-    const char *registry_key_base = "SOFTWARE\\Microsoft\\Windows\\"
-                                    "CurrentVersion\\App Paths\\darktable";
-    char path[MAX_PATH];
-    DWORD buffer_size = sizeof (path);
-    long status;
-
-    if (suffix)
-      registry_key = g_strconcat (registry_key_base, suffix, ".exe", NULL);
-    else
-      registry_key = g_strconcat (registry_key_base, ".exe", NULL);
-
-    status = RegGetValue (HKEY_LOCAL_MACHINE, registry_key, "", RRF_RT_ANY,
-                          NULL, (PVOID)&path, &buffer_size);
-
-    g_free (registry_key);
-
-    if (status == ERROR_SUCCESS)
-      return g_strdup (path);
-  }
-#endif
-
-  /* Finally, the last resort. */
-  *search_path = TRUE;
-  if (suffix)
-    return g_strconcat ("darktable", suffix, NULL);
-  return g_strdup ("darktable");
-}
-
 static void
 init (void)
 {
@@ -201,7 +96,11 @@ init (void)
   /* check if darktable is installed
    */
   gboolean  search_path      = FALSE;
-  gchar    *exec_path        = get_executable_path (NULL, &search_path);
+  gchar    *exec_path        = file_raw_get_executable_path ("darktable", NULL,
+                                                             "DARKTABLE_EXECUTABLE",
+                                                             "org.darktable",
+                                                             REGISTRY_KEY_BASE,
+                                                             &search_path);
   gchar    *argv[]           = { exec_path, "--version", NULL };
   gchar    *darktable_stdout = NULL;
   gchar    *darktable_stderr = NULL;
@@ -439,7 +338,11 @@ load_image (const gchar  *filename,
 
   /* linear sRGB for now as GIMP uses that internally in many places anyway */
   gboolean  search_path      = FALSE;
-  gchar    *exec_path        = get_executable_path (NULL, &search_path);
+  gchar    *exec_path        = file_raw_get_executable_path ("darktable", NULL,
+                                                             "DARKTABLE_EXECUTABLE",
+                                                             "org.darktable",
+                                                             REGISTRY_KEY_BASE,
+                                                             &search_path);
   gchar    *argv[] =
     {
       exec_path,
@@ -509,7 +412,11 @@ load_thumbnail_image (const gchar   *filename,
   gchar  *darktable_stdout = NULL;
 
   gboolean  search_path      = FALSE;
-  gchar    *exec_path        = get_executable_path ("-cli", &search_path);
+  gchar    *exec_path        = file_raw_get_executable_path ("darktable", "-cli",
+                                                             "DARKTABLE_EXECUTABLE",
+                                                             "org.darktable",
+                                                             REGISTRY_KEY_BASE,
+                                                             &search_path);
   gchar    *argv[] =
     {
       exec_path,
diff --git a/plug-ins/file-raw/file-raw-utils.c b/plug-ins/file-raw/file-raw-utils.c
new file mode 100644
index 0000000..db6dc2a
--- /dev/null
+++ b/plug-ins/file-raw/file-raw-utils.c
@@ -0,0 +1,133 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * file-raw-utils.h -- raw file format plug-in
+ * Copyright (C) 2016 Tobias Ellinghaus <me houz org>
+ *
+ * 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 "config.h"
+
+#include <libgimp/gimp.h>
+#include <file-raw-utils.h>
+
+#ifdef GDK_WINDOWING_QUARTZ
+#include <CoreServices/CoreServices.h>
+#endif
+
+#ifdef GDK_WINDOWING_WIN32
+#include <Windows.h>
+#endif
+
+gchar *
+file_raw_get_executable_path (const gchar *main_executable,
+                              const gchar *suffix,
+                              const gchar *env_variable,
+                              const gchar *mac_bundle_id,
+                              const gchar *win32_registry_key_base,
+                              gboolean    *search_path)
+{
+  /*
+   * First check for the environment variable.
+   * Next do platform specific checks (bundle lookup on Mac, registry stuff
+   * on Windows).
+   * Last resort is hoping for the executable to be in PATH.
+   */
+
+  /*
+   * Look for env variable. That can be set directly or via an environ file.
+   * We assume that just appending the suffix to that value will work.
+   * That means that on Windows there should be no ".exe"!
+   */
+  const gchar *dt_env = env_variable? g_getenv (env_variable) : NULL;
+
+  if (dt_env)
+    return g_strconcat (dt_env, suffix, NULL);
+
+#if defined (GDK_WINDOWING_QUARTZ)
+  if (mac_bundle_id)
+    {
+      OSStatus status;
+      CFURLRef bundle_url = NULL;
+
+      /* For macOS, attempt searching for a darktable app bundle first. */
+      status = LSFindApplicationForInfo (kLSUnknownCreator,
+                                         CFSTR (mac_bundle_id),
+                                         NULL, NULL, &bundle_url);
+
+      if (status >= 0)
+        {
+          CFBundleRef  bundle;
+          CFURLRef     exec_url, absolute_url;
+          CFStringRef  path;
+          gchar       *ret;
+          CFIndex      len;
+
+          bundle = CFBundleCreate (kCFAllocatorDefault, bundle_url);
+          CFRelease (bundle_url);
+
+          exec_url = CFBundleCopyExecutableURL (bundle);
+          absolute_url = CFURLCopyAbsoluteURL (exec_url);
+          path = CFURLCopyFileSystemPath (absolute_url, kCFURLPOSIXPathStyle);
+
+          /* This gets us the length in UTF16 characters, we multiply by 2
+           * to make sure we have a buffer big enough to fit the UTF8 string.
+           */
+          len = CFStringGetLength (path);
+          ret = g_malloc0 (len * 2 * sizeof (gchar));
+          if (!CFStringGetCString (path, ret, 2 * len * sizeof (gchar),
+                                   kCFStringEncodingUTF8))
+            ret = NULL;
+
+          CFRelease (path);
+          CFRelease (absolute_url);
+          CFRelease (exec_url);
+          CFRelease (bundle);
+
+          if (ret)
+            return ret;
+        }
+      /* else, app bundle was not found, try path search as last resort. */
+    }
+#elif defined (GDK_WINDOWING_WIN32)
+  if (registry_key_base)
+    {
+      /* Look for the application in the Windows registry. */
+      char  *registry_key;
+      char   path[MAX_PATH];
+      DWORD  buffer_size = sizeof (path);
+      long   status;
+
+      if (suffix)
+        registry_key = g_strconcat (registry_key_base, suffix, ".exe", NULL);
+      else
+        registry_key = g_strconcat (registry_key_base, ".exe", NULL);
+
+      status = RegGetValue (HKEY_LOCAL_MACHINE, registry_key, "", RRF_RT_ANY,
+                            NULL, (PVOID)&path, &buffer_size);
+
+      g_free (registry_key);
+
+      if (status == ERROR_SUCCESS)
+        return g_strdup (path);
+    }
+#endif
+
+  /* Finally, the last resort. */
+  *search_path = TRUE;
+  if (suffix)
+    return g_strconcat (main_executable, suffix, NULL);
+  return g_strdup (main_executable);
+}
diff --git a/plug-ins/file-raw/file-raw-utils.h b/plug-ins/file-raw/file-raw-utils.h
new file mode 100644
index 0000000..ace1266
--- /dev/null
+++ b/plug-ins/file-raw/file-raw-utils.h
@@ -0,0 +1,26 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * file-raw-utils.h -- raw file format plug-in
+ * Copyright (C) 2016 Tobias Ellinghaus <me houz org>
+ *
+ * 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/>.
+ */
+
+gchar  * file_raw_get_executable_path  (const gchar *main_executable,
+                                        const gchar *suffix,
+                                        const gchar *env_variable,
+                                        const gchar *mac_bundle_id,
+                                        const gchar *win32_registry_key_base,
+                                        gboolean    *search_path);
diff --git a/plug-ins/file-raw/file-rawtherapee.c b/plug-ins/file-raw/file-rawtherapee.c
index a032d72..6909627 100644
--- a/plug-ins/file-raw/file-rawtherapee.c
+++ b/plug-ins/file-raw/file-rawtherapee.c
@@ -32,6 +32,7 @@
 #include "libgimp/stdplugins-intl.h"
 
 #include "file-formats.h"
+#include "file-raw-utils.h"
 
 
 #define LOAD_THUMB_PROC "file-rawtherapee-load-thumb"
@@ -92,7 +93,13 @@ init (void)
   /* check if rawtherapee is installed
    * TODO: allow setting the location of the executable in preferences
    */
-  gchar    *argv[]           = { "rawtherapee", "-v", NULL };
+  gboolean  search_path      = FALSE;
+  gchar    *exec_path        = file_raw_get_executable_path ("rawtherapee", NULL,
+                                                             "RAWTHERAPEE_EXECUTABLE",
+                                                             "com.rawtherapee.rawtherapee",
+                                                             NULL,
+                                                             &search_path);
+  gchar    *argv[]             = { exec_path, "-v", NULL };
   gchar    *rawtherapee_stdout = NULL;
   gboolean  have_rawtherapee   = FALSE;
   gint      i;
@@ -100,8 +107,8 @@ init (void)
   if (g_spawn_sync (NULL,
                     argv,
                     NULL,
-                    G_SPAWN_STDERR_TO_DEV_NULL |
-                    G_SPAWN_SEARCH_PATH,
+                    (search_path ? G_SPAWN_SEARCH_PATH : 0) |
+                    G_SPAWN_STDERR_TO_DEV_NULL,
                     NULL,
                     NULL,
                     &rawtherapee_stdout,
@@ -120,6 +127,7 @@ init (void)
 
       g_free (rawtherapee_stdout);
     }
+  g_free (exec_path);
 
   if (! have_rawtherapee)
     return;
@@ -281,15 +289,21 @@ load_image (const gchar  *filename,
             GimpRunMode   run_mode,
             GError      **error)
 {
-  gint32  image_ID        = -1;
-  gchar  *filename_out    = gimp_temp_name ("tif");
+  gint32    image_ID           = -1;
+  gchar    *filename_out       = gimp_temp_name ("tif");
+  gchar    *rawtherapee_stdout = NULL;
 
-  gchar *rawtherapee_stdout = NULL;
+  gboolean  search_path        = FALSE;
+  gchar    *exec_path          = file_raw_get_executable_path ("rawtherapee", NULL,
+                                                               "RAWTHERAPEE_EXECUTABLE",
+                                                               "com.rawtherapee.rawtherapee",
+                                                               NULL,
+                                                               &search_path);
 
   /* linear sRGB for now as GIMP uses that internally in many places anyway */
   gchar *argv[] =
     {
-      "rawtherapee",
+      exec_path,
       "-gimp",
       (gchar *) filename,
       filename_out,
@@ -304,7 +318,7 @@ load_image (const gchar  *filename,
                     NULL,
                     /*G_SPAWN_STDOUT_TO_DEV_NULL |*/
                     G_SPAWN_STDERR_TO_DEV_NULL |
-                    G_SPAWN_SEARCH_PATH,
+                    (search_path ? G_SPAWN_SEARCH_PATH : 0),
                     NULL,
                     NULL,
                     &rawtherapee_stdout,
@@ -319,6 +333,7 @@ load_image (const gchar  *filename,
 
   /*if (rawtherapee_stdout) printf ("%s\n", rawtherapee_stdout);*/
   g_free (rawtherapee_stdout);
+  g_free (exec_path);
 
   g_unlink (filename_out);
   g_free (filename_out);
@@ -384,9 +399,15 @@ load_thumbnail_image (const gchar   *filename,
     "Method=fast\n";
 
 
+  gboolean  search_path = FALSE;
+  gchar    *exec_path   = file_raw_get_executable_path ("rawtherapee", "-cli",
+                                                        "RAWTHERAPEE_EXECUTABLE",
+                                                        "com.rawtherapee.rawtherapee",
+                                                        NULL,
+                                                        &search_path);
   gchar *argv[] =
     {
-      "rawtherapee-cli",
+      exec_path,
       "-o", filename_out,
       "-d",
       "-s",
@@ -412,7 +433,7 @@ load_thumbnail_image (const gchar   *filename,
                     argv,
                     NULL,
                     G_SPAWN_STDERR_TO_DEV_NULL |
-                    G_SPAWN_SEARCH_PATH,
+                    (search_path ? G_SPAWN_SEARCH_PATH : 0),
                     NULL,
                     NULL,
                     &rawtherapee_stdout,
@@ -441,6 +462,7 @@ load_thumbnail_image (const gchar   *filename,
   g_free (filename_out);
   g_free (thumb_pp3);
   g_free (rawtherapee_stdout);
+  g_free (exec_path);
 
   return image_ID;
 }


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