unique.c 8.35 KB
Newer Older
1 2 3
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17 18 19
 */

#include "config.h"

20
#include <gdk-pixbuf/gdk-pixbuf.h>
21 22 23 24 25 26 27

#ifdef G_OS_WIN32
#include <windows.h>
#endif

#include "core/core-types.h"

28 29 30 31 32
#ifndef GIMP_CONSOLE_COMPILATION
/*  for the DBus service names  */
#include "gui/gimpdbusservice.h"
#endif

33 34 35 36
#include "unique.h"


static gboolean  gimp_unique_dbus_open  (const gchar **filenames,
Jehan's avatar
Jehan committed
37
                                         gboolean      as_new);
38 39
#ifdef G_OS_WIN32
static gboolean  gimp_unique_win32_open (const gchar **filenames,
Jehan's avatar
Jehan committed
40
                                         gboolean      as_new);
41
#endif
42 43
static gboolean  gimp_unique_dbus_batch_run (const gchar  *batch_interpreter,
                                             const gchar **batch_commands);
44 45 46

gboolean
gimp_unique_open (const gchar **filenames,
Jehan's avatar
Jehan committed
47
                  gboolean      as_new)
48 49
{
#ifdef G_OS_WIN32
50
  return gimp_unique_win32_open (filenames, as_new);
51
#else
52
  return gimp_unique_dbus_open (filenames, as_new);
53 54 55
#endif
}

56 57 58 59 60 61 62 63 64 65 66 67 68
gboolean
gimp_unique_batch_run (const gchar  *batch_interpreter,
                       const gchar **batch_commands)
{
#ifdef G_OS_WIN32
  g_printerr ("Batch commands cannot be run in existing instance in Win32.\n");
  return FALSE;
#else
  return gimp_unique_dbus_batch_run (batch_interpreter,
                                     batch_commands);
#endif
}

69 70
static gboolean
gimp_unique_dbus_open (const gchar **filenames,
Jehan's avatar
Jehan committed
71
                       gboolean      as_new)
72 73 74
{
#ifndef GIMP_CONSOLE_COMPILATION

75 76 77 78
  GDBusConnection *connection;
  GError          *error = NULL;

  connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
79 80 81

  if (connection)
    {
82
      gboolean success = TRUE;
83 84 85 86

      if (filenames)
        {
          const gchar *method = as_new ? "OpenAsNew" : "Open";
87
          gchar       *cwd    = g_get_current_dir ();
88 89
          gint         i;

90
          for (i = 0; filenames[i] && success; i++)
91
            {
92 93 94
              GFile *file;

              file = g_file_new_for_commandline_arg_and_cwd (filenames[i], cwd);
95

96
              if (file)
97
                {
98
                  GVariant *result;
99
                  gchar    *uri = g_file_get_uri (file);
100 101 102 103

                  result = g_dbus_connection_call_sync (connection,
                                                        GIMP_DBUS_SERVICE_NAME,
                                                        GIMP_DBUS_SERVICE_PATH,
104
                                                        GIMP_DBUS_INTERFACE_NAME,
105 106 107 108 109 110 111
                                                        method,
                                                        g_variant_new ("(s)",
                                                                       uri),
                                                        NULL,
                                                        G_DBUS_CALL_FLAGS_NO_AUTO_START,
                                                        -1,
                                                        NULL, NULL);
112 113 114

                  g_free (uri);

115 116 117 118
                  if (result)
                    g_variant_unref (result);
                  else
                    success = FALSE;
119

120
                  g_object_unref (file);
121 122 123
                }
              else
                {
124 125
                  g_printerr ("conversion to uri failed for '%s'\n",
                              filenames[i]);
126 127 128 129 130 131 132
                }
            }

          g_free (cwd);
        }
      else
        {
133 134 135 136 137
          GVariant *result;

          result = g_dbus_connection_call_sync (connection,
                                                GIMP_DBUS_SERVICE_NAME,
                                                GIMP_DBUS_SERVICE_PATH,
138
                                                GIMP_DBUS_INTERFACE_NAME,
139 140 141 142 143 144 145 146 147 148
                                                "Activate",
                                                NULL,
                                                NULL,
                                                G_DBUS_CALL_FLAGS_NO_AUTO_START,
                                                -1,
                                                NULL, NULL);
          if (result)
            g_variant_unref (result);
          else
            success = FALSE;
149 150
        }

151
      g_object_unref (connection);
152

153 154 155 156 157
      return success;
    }
  else
    {
      g_printerr ("%s\n", error->message);
158 159 160 161 162 163 164 165 166 167 168
      g_clear_error (&error);
    }
#endif

  return FALSE;
}

#ifdef G_OS_WIN32

static gboolean
gimp_unique_win32_open (const gchar **filenames,
Jehan's avatar
Jehan committed
169
                        gboolean      as_new)
170 171 172
{
#ifndef GIMP_CONSOLE_COMPILATION

173 174 175 176
/*  for the proxy window names  */
#include "gui/gui-unique.h"

  HWND  window_handle = FindWindowW (GIMP_UNIQUE_WIN32_WINDOW_CLASS,
Jehan's avatar
Jehan committed
177
                                     GIMP_UNIQUE_WIN32_WINDOW_NAME);
178 179 180

  if (window_handle)
    {
181
      COPYDATASTRUCT  copydata = { 0, };
182

183 184 185 186 187 188 189
      if (filenames)
        {
          gchar  *cwd   = g_get_current_dir ();
          gint    i;

          for (i = 0; filenames[i]; i++)
            {
190 191
              GFile *file;
              file = g_file_new_for_commandline_arg_and_cwd (filenames[i], cwd);
192

193
              if (file)
194
                {
195 196
                  gchar *uri = g_file_get_uri (file);

197 198 199 200 201
                  copydata.lpData = uri;
                  copydata.cbData = strlen (uri) + 1;  /* size in bytes   */
                  copydata.dwData = (long) as_new;

                  SendMessage (window_handle,
202
                               WM_COPYDATA, (WPARAM) window_handle, (LPARAM) &copydata);
203 204 205

                  g_free (uri);
                  g_object_unref (file);
206 207 208
                }
              else
                {
209 210
                  g_printerr ("conversion to uri failed for '%s'\n",
                              filenames[i]);
211 212 213 214 215 216 217 218
                }
            }

          g_free (cwd);
        }
      else
        {
          SendMessage (window_handle,
219
                       WM_COPYDATA, (WPARAM) window_handle, (LPARAM) &copydata);
220
        }
221 222 223 224

      return TRUE;
    }

225 226
#endif

227
  return FALSE;
228 229 230
}

#endif  /* G_OS_WIN32 */
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

static gboolean
gimp_unique_dbus_batch_run (const gchar  *batch_interpreter,
                            const gchar **batch_commands)
{
#ifndef GIMP_CONSOLE_COMPILATION

  GDBusConnection *connection;
  GError          *error = NULL;

  connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);

  if (connection)
    {
      const gchar *method = "BatchRun";
      gboolean     success = TRUE;
      gint         i;

      for (i = 0; batch_commands[i] && success; i++)
        {
          GVariant    *result;
          const gchar *interpreter;

          /* NULL is not a valid string GVariant. */
          interpreter = batch_interpreter ? batch_interpreter : "";

          result = g_dbus_connection_call_sync (connection,
                                                GIMP_DBUS_SERVICE_NAME,
                                                GIMP_DBUS_SERVICE_PATH,
                                                GIMP_DBUS_INTERFACE_NAME,
                                                method,
                                                g_variant_new ("(ss)",
                                                               interpreter,
                                                               batch_commands[i]),
                                                NULL,
                                                G_DBUS_CALL_FLAGS_NO_AUTO_START,
                                                -1,
                                                NULL, NULL);


          if (result)
            g_variant_unref (result);
          else
            success = FALSE;
        }

      g_object_unref (connection);

      return success;
    }
  else
    {
      g_printerr ("%s\n", error->message);
      g_clear_error (&error);
    }
#endif

  return FALSE;
}