gimpenv.c 32.5 KB
Newer Older
1
/* LIBGIMP - The GIMP Library
2
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3 4
 *
 * gimpenv.c
5
 * Copyright (C) 1999 Tor Lillqvist <tml@iki.fi>
6
 *
7
 * This library is free software: you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 3 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
19
 * <https://www.gnu.org/licenses/>.
20 21 22 23
 */

#include "config.h"

24
#include <errno.h>
25
#include <string.h>
26
#include <sys/types.h>
27

28 29 30 31
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

32 33 34 35
#ifdef PLATFORM_OSX
#include <AppKit/AppKit.h>
#endif

36
#include <gio/gio.h>
37 38
#include <glib/gstdio.h>

39
#undef GIMP_DISABLE_DEPRECATED
40 41
#include "gimpbasetypes.h"

42
#define __GIMP_ENV_C__
43
#include "gimpenv.h"
44
#include "gimpversion.h"
45
#include "gimpreloc.h"
46

47
#ifdef G_OS_WIN32
48
#define STRICT
49 50
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
51 52 53 54
#include <io.h>
#ifndef S_IWUSR
# define S_IWUSR _S_IWRITE
#endif
55 56 57 58
#ifndef S_IWGRP
#define S_IWGRP (_S_IWRITE>>3)
#define S_IWOTH (_S_IWRITE>>6)
#endif
59
#ifndef S_ISDIR
60 61
# define __S_ISTYPE(mode, mask) (((mode) & _S_IFMT) == (mask))
# define S_ISDIR(mode)  __S_ISTYPE((mode), _S_IFDIR)
62
#endif
63 64 65 66
#define uid_t gint
#define gid_t gint
#define geteuid() 0
#define getegid() 0
67 68 69 70 71 72 73 74 75 76 77 78 79 80

/* 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. */
static const char* datadir = DATADIR;
#undef DATADIR
#include <shlobj.h>
#define DATADIR datadir
/* Constant available since Shell32.dll 4.72 */
#ifndef CSIDL_APPDATA
#define CSIDL_APPDATA 0x001a
#endif

81 82
#endif

83

84 85 86 87 88 89 90 91 92 93
/**
 * SECTION: gimpenv
 * @title: gimpenv
 * @short_description: Functions to access the GIMP environment.
 *
 * A set of functions to find the locations of GIMP's data directories
 * and configuration files.
 **/


94
static gchar * gimp_env_get_dir   (const gchar *gimp_env_name,
95 96
                                   const gchar *compile_time_dir,
                                   const gchar *relative_subdir);
97 98 99
#ifdef G_OS_WIN32
static gchar * get_special_folder (gint         csidl);
#endif
100

101

102 103 104 105 106
const guint gimp_major_version = GIMP_MAJOR_VERSION;
const guint gimp_minor_version = GIMP_MINOR_VERSION;
const guint gimp_micro_version = GIMP_MICRO_VERSION;


107 108 109 110 111 112 113 114
/**
 * gimp_env_init:
 * @plug_in: must be %TRUE if this function is called from a plug-in
 *
 * You don't need to care about this function. It is being called for
 * you automatically (by means of the MAIN() macro that every plug-in
 * runs). Calling it again will cause a fatal error.
 *
115
 * Since: 2.4
116 117 118 119
 */
void
gimp_env_init (gboolean plug_in)
{
120 121
  static gboolean  gimp_env_initialized = FALSE;
  const gchar     *data_home = g_get_user_data_dir ();
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

  if (gimp_env_initialized)
    g_error ("gimp_env_init() must only be called once!");

  gimp_env_initialized = TRUE;

#ifndef G_OS_WIN32
  if (plug_in)
    {
      _gimp_reloc_init_lib (NULL);
    }
  else if (_gimp_reloc_init (NULL))
    {
      /* Set $LD_LIBRARY_PATH to ensure that plugins can be loaded. */

      const gchar *ldpath = g_getenv ("LD_LIBRARY_PATH");
138 139 140
      gchar       *libdir = g_build_filename (gimp_installation_directory (),
                                              "lib",
                                              NULL);
141 142 143 144 145

      if (ldpath && *ldpath)
        {
          gchar *tmp = g_strconcat (libdir, ":", ldpath, NULL);

146
          g_setenv ("LD_LIBRARY_PATH", tmp, TRUE);
147 148

          g_free (tmp);
149
        }
150 151 152 153 154 155 156 157
      else
        {
          g_setenv ("LD_LIBRARY_PATH", libdir, TRUE);
        }

      g_free (libdir);
    }
#endif
158 159 160 161 162 163 164 165 166 167 168 169 170 171

  /* The user data directory (XDG_DATA_HOME on Unix) is used to store
   * various data, like crash logs (win32) or recently used file history
   * (by GTK+). Yet it may be absent, in particular on non-Linux
   * platforms. Make sure it exists.
   */
  if (! g_file_test (data_home, G_FILE_TEST_IS_DIR))
    {
      if (g_mkdir_with_parents (data_home, S_IRUSR | S_IWUSR | S_IXUSR) != 0)
        {
          g_warning ("Failed to create the data directory '%s': %s",
                     data_home, g_strerror (errno));
        }
    }
172 173
}

Sven Neumann's avatar
Sven Neumann committed
174 175 176
/**
 * gimp_directory:
 *
177
 * Returns the user-specific GIMP settings directory. If the
178
 * environment variable GIMP3_DIRECTORY exists, it is used. If it is
179 180
 * an absolute path, it is used as is.  If it is a relative path, it
 * is taken to be a subdirectory of the home directory. If it is a
181 182
 * relative path, and no home directory can be determined, it is taken
 * to be a subdirectory of gimp_data_directory().
Sven Neumann's avatar
Sven Neumann committed
183
 *
184
 * The usual case is that no GIMP3_DIRECTORY environment variable
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
 * exists, and then we use the GIMPDIR subdirectory of the local
 * configuration directory:
 *
 * - UNIX: $XDG_CONFIG_HOME (defaults to $HOME/.config/)
 *
 * - Windows: CSIDL_APPDATA
 *
 * - OSX (UNIX exception): the Application Support Directory.
 *
 * If neither the configuration nor home directory exist,
 * g_get_user_config_dir() will return {tmp}/{user_name}/.config/ where
 * the temporary directory {tmp} and the {user_name} are determined
 * according to platform rules.
 *
 * In any case, we always return some non-empty string, whether it
 * corresponds to an existing directory or not.
Sven Neumann's avatar
Sven Neumann committed
201
 *
202 203 204
 * In config files such as gimprc, the string ${gimp_dir} expands to
 * this directory.
 *
205 206
 * The returned string is owned by GIMP and must not be modified or
 * freed. The returned string is in the encoding used for filenames by
207
 * GLib, which isn't necessarily UTF-8 (on Windows it is always
208
 * UTF-8.)
Sven Neumann's avatar
Sven Neumann committed
209 210
 *
 * Returns: The user-specific GIMP settings directory.
211
 **/
212
const gchar *
Sven Neumann's avatar
Sven Neumann committed
213
gimp_directory (void)
214
{
215 216
  static gchar *gimp_dir          = NULL;
  static gchar *last_env_gimp_dir = NULL;
217 218

  const gchar  *env_gimp_dir;
219

220
  env_gimp_dir = g_getenv ("GIMP3_DIRECTORY");
221

222
  if (gimp_dir)
223
    {
224
      gboolean gimp3_directory_changed = FALSE;
225 226 227

      /* We have constructed the gimp_dir already. We can return
       * gimp_dir unless some parameter gimp_dir depends on has
228
       * changed. For now we just check for changes to GIMP3_DIRECTORY
229
       */
230
      gimp3_directory_changed =
231 232 233 234 235 236 237 238
        (env_gimp_dir == NULL &&
         last_env_gimp_dir != NULL) ||
        (env_gimp_dir != NULL &&
         last_env_gimp_dir == NULL) ||
        (env_gimp_dir != NULL &&
         last_env_gimp_dir != NULL &&
         strcmp (env_gimp_dir, last_env_gimp_dir) != 0);

239
      if (! gimp3_directory_changed)
240 241 242 243 244 245 246 247 248 249
        {
          return gimp_dir;
        }
      else
        {
          /* Free the old gimp_dir and go on to update it */
          g_free (gimp_dir);
          gimp_dir = NULL;
        }
    }
250

251
  /* Remember the GIMP3_DIRECTORY to next invocation so we can check
252 253 254 255 256
   * if it changes
   */
  g_free (last_env_gimp_dir);
  last_env_gimp_dir = g_strdup (env_gimp_dir);

257
  if (env_gimp_dir)
258 259
    {
      if (g_path_is_absolute (env_gimp_dir))
260 261 262
        {
          gimp_dir = g_strdup (env_gimp_dir);
        }
263
      else
264
        {
265 266
          const gchar *home_dir = g_get_home_dir ();

267
          if (home_dir)
268
            gimp_dir = g_build_filename (home_dir, env_gimp_dir, NULL);
269
          else
270
            gimp_dir = g_build_filename (gimp_data_directory (), env_gimp_dir, NULL);
271
        }
272
    }
273 274 275 276
  else if (g_path_is_absolute (GIMPDIR))
    {
      gimp_dir = g_strdup (GIMPDIR);
    }
277 278
  else
    {
279 280 281 282 283 284 285 286
#ifdef PLATFORM_OSX

      NSAutoreleasePool *pool;
      NSArray           *path;
      NSString          *library_dir;

      pool = [[NSAutoreleasePool alloc] init];

287
      path = NSSearchPathForDirectoriesInDomains (NSApplicationSupportDirectory,
288 289 290
                                                  NSUserDomainMask, YES);
      library_dir = [path objectAtIndex:0];

291
      gimp_dir = g_build_filename ([library_dir UTF8String],
292
                                   GIMPDIR, GIMP_USER_VERSION, NULL);
293 294 295

      [pool drain];

296
#elif defined G_OS_WIN32
297

298
      gchar *conf_dir = get_special_folder (CSIDL_APPDATA);
299

300 301 302
      gimp_dir = g_build_filename (conf_dir,
                                   GIMPDIR, GIMP_USER_VERSION, NULL);
      g_free(conf_dir);
303

304 305 306 307 308 309 310
#else /* UNIX */

      /* g_get_user_config_dir () always returns a path as a non-null
       * and non-empty string
       */
      gimp_dir = g_build_filename (g_get_user_config_dir (),
                                   GIMPDIR, GIMP_USER_VERSION, NULL);
311 312

#endif /* PLATFORM_OSX */
313 314 315 316 317
    }

  return gimp_dir;
}

318 319
#ifdef G_OS_WIN32

320
/* Taken from glib 2.35 code. */
321 322 323 324 325 326 327 328 329
static gchar *
get_special_folder (int csidl)
{
  wchar_t      path[MAX_PATH+1];
  HRESULT      hr;
  LPITEMIDLIST pidl = NULL;
  BOOL         b;
  gchar       *retval = NULL;

330
  hr = SHGetSpecialFolderLocation (NULL, csidl, &pidl);
331 332 333 334 335 336 337 338 339 340 341
  if (hr == S_OK)
    {
      b = SHGetPathFromIDListW (pidl, path);
      if (b)
        retval = g_utf16_to_utf8 (path, -1, NULL, NULL, NULL);
      CoTaskMemFree (pidl);
    }

  return retval;
}

342 343 344 345
static HMODULE libgimpbase_dll = NULL;

/* Minimal DllMain that just stores the handle to this DLL */

346
BOOL WINAPI /* Avoid silly "no previous prototype" gcc warning */
347
DllMain (HINSTANCE hinstDLL,
Jehan's avatar
Jehan committed
348 349
         DWORD     fdwReason,
         LPVOID    lpvReserved);
350 351 352

BOOL WINAPI
DllMain (HINSTANCE hinstDLL,
Jehan's avatar
Jehan committed
353 354
         DWORD     fdwReason,
         LPVOID    lpvReserved)
355 356 357 358 359 360 361 362 363 364 365 366 367
{
  switch (fdwReason)
    {
    case DLL_PROCESS_ATTACH:
      libgimpbase_dll = hinstDLL;
      break;
    }

  return TRUE;
}

#endif

368 369 370 371 372 373 374 375 376
/**
 * gimp_installation_directory:
 *
 * Returns the top installation directory of GIMP. On Unix the
 * compile-time defined installation prefix is used. On Windows, the
 * installation directory as deduced from the executable's full
 * filename is used. On OSX we ask [NSBundle mainBundle] for the
 * resource path to check if GIMP is part of a relocatable bundle.
 *
377 378 379
 * In config files such as gimprc, the string ${gimp_installation_dir}
 * expands to this directory.
 *
380 381 382 383 384
 * The returned string is owned by GIMP and must not be modified or
 * freed. The returned string is in the encoding used for filenames by
 * GLib, which isn't necessarily UTF-8. (On Windows it always is
 * UTF-8.)
 *
385
 * Since: 2.8
386 387 388 389 390
 *
 * Returns: The toplevel installation directory of GIMP.
 **/
const gchar *
gimp_installation_directory (void)
391 392
{
  static gchar *toplevel = NULL;
393

394
  if (toplevel)
395
    return toplevel;
396

397
#ifdef G_OS_WIN32
398

399 400 401
  toplevel = g_win32_get_package_installation_directory_of_module (libgimpbase_dll);
  if (! toplevel)
    g_error ("g_win32_get_package_installation_directory_of_module() failed");
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456

#elif PLATFORM_OSX

  {
    NSAutoreleasePool *pool;
    NSString          *resource_path;
    gchar             *basename;
    gchar             *dirname;

    pool = [[NSAutoreleasePool alloc] init];

    resource_path = [[NSBundle mainBundle] resourcePath];

    basename = g_path_get_basename ([resource_path UTF8String]);
    dirname  = g_path_get_dirname ([resource_path UTF8String]);

    if (! strcmp (basename, ".libs"))
      {
        /*  we are running from the source dir, do normal unix things  */

        toplevel = _gimp_reloc_find_prefix (PREFIX);
      }
    else if (! strcmp (basename, "bin"))
      {
        /*  we are running the main app, but not from a bundle, the resource
         *  path is the directory which contains the executable
         */

        toplevel = g_strdup (dirname);
      }
    else if (! strcmp (basename, "plug-ins"))
      {
        /*  same for plug-ins, go three levels up from prefix/lib/gimp/x.y  */

        gchar *tmp  = g_path_get_dirname (dirname);
        gchar *tmp2 = g_path_get_dirname (tmp);

        toplevel = g_path_get_dirname (tmp2);

        g_free (tmp);
        g_free (tmp2);
      }
    else
      {
        /*  if none of the above match, we assume that we are really in a bundle  */

        toplevel = g_strdup ([resource_path UTF8String]);
      }

    g_free (basename);
    g_free (dirname);

    [pool drain];
  }

457
#else
458

459
  toplevel = _gimp_reloc_find_prefix (PREFIX);
460

461
#endif
462 463

  return toplevel;
464
}
465

Sven Neumann's avatar
Sven Neumann committed
466 467 468
/**
 * gimp_data_directory:
 *
469
 * Returns the default top directory for GIMP data. If the environment
470
 * variable GIMP3_DATADIR exists, that is used.  It should be an
471
 * absolute pathname.  Otherwise, on Unix the compile-time defined
472 473 474 475 476 477 478 479
 * directory is used. On Windows, the installation directory as
 * deduced from the executable's full filename is used.
 *
 * Note that the actual directories used for GIMP data files can be
 * overridden by the user in the preferences dialog.
 *
 * In config files such as gimprc, the string ${gimp_data_dir} expands
 * to this directory.
Sven Neumann's avatar
Sven Neumann committed
480
 *
481 482
 * The returned string is owned by GIMP and must not be modified or
 * freed. The returned string is in the encoding used for filenames by
483 484
 * GLib, which isn't necessarily UTF-8. (On Windows it always is
 * UTF-8.)
Sven Neumann's avatar
Sven Neumann committed
485 486
 *
 * Returns: The top directory for GIMP data.
487
 **/
488
const gchar *
Sven Neumann's avatar
Sven Neumann committed
489
gimp_data_directory (void)
490
{
Sven Neumann's avatar
Sven Neumann committed
491
  static gchar *gimp_data_dir = NULL;
492

493
  if (! gimp_data_dir)
494
    {
495 496 497 498
      gchar *tmp = g_build_filename ("share",
                                     GIMP_PACKAGE,
                                     GIMP_DATA_VERSION,
                                     NULL);
499

500
      gimp_data_dir = gimp_env_get_dir ("GIMP3_DATADIR", DATADIR, tmp);
501 502
      g_free (tmp);
    }
503 504 505 506

  return gimp_data_dir;
}

507 508 509 510
/**
 * gimp_locale_directory:
 *
 * Returns the top directory for GIMP locale files. If the environment
511
 * variable GIMP3_LOCALEDIR exists, that is used.  It should be an
512
 * absolute pathname.  Otherwise, on Unix the compile-time defined
513 514
 * directory is used. On Windows, the installation directory as deduced
 * from the executable's full filename is used.
515
 *
516 517
 * The returned string is owned by GIMP and must not be modified or
 * freed. The returned string is in the encoding used for filenames by
518 519 520 521 522
 * the C library, which isn't necessarily UTF-8. (On Windows, unlike
 * the other similar functions here, the return value from this
 * function is in the system codepage, never in UTF-8. It can thus be
 * passed directly to the bindtextdomain() function from libintl which
 * does not handle UTF-8.)
523 524 525 526 527 528 529 530
 *
 * Returns: The top directory for GIMP locale files.
 */
const gchar *
gimp_locale_directory (void)
{
  static gchar *gimp_locale_dir = NULL;

531
  if (! gimp_locale_dir)
532
    {
533 534 535
      gchar *tmp = g_build_filename ("share",
                                     "locale",
                                     NULL);
536

537
      gimp_locale_dir = gimp_env_get_dir ("GIMP3_LOCALEDIR", LOCALEDIR, tmp);
538
      g_free (tmp);
539

540
#ifdef G_OS_WIN32
541 542 543 544 545 546
      /* FIXME: g_win32_locale_filename_from_utf8() can actually return
       * NULL (we had actual cases of this). Not sure exactly what
       * gimp_locale_directory() should do when this happens. Anyway
       * that's really broken, and something should be done some day
       * about this!
       */
547
      tmp = g_win32_locale_filename_from_utf8 (gimp_locale_dir);
548 549 550
      g_free (gimp_locale_dir);
      gimp_locale_dir = tmp;
#endif
551
    }
552 553 554 555

  return gimp_locale_dir;
}

556 557 558 559
/**
 * gimp_sysconf_directory:
 *
 * Returns the top directory for GIMP config files. If the environment
560
 * variable GIMP3_SYSCONFDIR exists, that is used.  It should be an
561
 * absolute pathname.  Otherwise, on Unix the compile-time defined
562 563
 * directory is used. On Windows, the installation directory as deduced
 * from the executable's full filename is used.
564
 *
565 566 567
 * In config files such as gimprc, the string ${gimp_sysconf_dir}
 * expands to this directory.
 *
568 569
 * The returned string is owned by GIMP and must not be modified or
 * freed. The returned string is in the encoding used for filenames by
570 571
 * GLib, which isn't necessarily UTF-8. (On Windows it always is
 * UTF-8.).
572
 *
573 574
 * Returns: The top directory for GIMP config files.
 **/
575
const gchar *
576 577 578
gimp_sysconf_directory (void)
{
  static gchar *gimp_sysconf_dir = NULL;
579

580
  if (! gimp_sysconf_dir)
581
    {
582 583 584 585
      gchar *tmp = g_build_filename ("etc",
                                     GIMP_PACKAGE,
                                     GIMP_SYSCONF_VERSION,
                                     NULL);
586

587
      gimp_sysconf_dir = gimp_env_get_dir ("GIMP3_SYSCONFDIR", SYSCONFDIR, tmp);
588 589
      g_free (tmp);
    }
590

591 592
  return gimp_sysconf_dir;
}
593

594 595 596
/**
 * gimp_plug_in_directory:
 *
597 598
 * Returns the default top directory for GIMP plug-ins and modules. If
 * the environment variable GIMP3_PLUGINDIR exists, that is used.  It
599
 * should be an absolute pathname. Otherwise, on Unix the compile-time
600 601 602 603 604 605 606 607
 * defined directory is used. On Windows, the installation directory
 * as deduced from the executable's full filename is used.
 *
 * Note that the actual directories used for GIMP plug-ins and modules
 * can be overridden by the user in the preferences dialog.
 *
 * In config files such as gimprc, the string ${gimp_plug_in_dir}
 * expands to this directory.
608
 *
609 610
 * The returned string is owned by GIMP and must not be modified or
 * freed. The returned string is in the encoding used for filenames by
611 612
 * GLib, which isn't necessarily UTF-8. (On Windows it always is
 * UTF-8.)
613 614 615 616 617 618 619
 *
 * Returns: The top directory for GIMP plug_ins and modules.
 **/
const gchar *
gimp_plug_in_directory (void)
{
  static gchar *gimp_plug_in_dir = NULL;
620

621
  if (! gimp_plug_in_dir)
622
    {
623 624 625 626
      gchar *tmp = g_build_filename ("lib",
                                     GIMP_PACKAGE,
                                     GIMP_PLUGIN_VERSION,
                                     NULL);
627

628
      gimp_plug_in_dir = gimp_env_get_dir ("GIMP3_PLUGINDIR", PLUGINDIR, tmp);
629 630
      g_free (tmp);
    }
631

632
  return gimp_plug_in_dir;
633 634
}

635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
/**
 * gimp_cache_directory:
 *
 * Returns the default top directory for GIMP cached files. If the
 * environment variable GIMP3_CACHEDIR exists, that is used.  It
 * should be an absolute pathname.  Otherwise, a subdirectory of the
 * directory returned by g_get_user_cache_dir() is used.
 *
 * Note that the actual directories used for GIMP caches files can
 * be overridden by the user in the preferences dialog.
 *
 * In config files such as gimprc, the string ${gimp_cache_dir}
 * expands to this directory.
 *
 * The returned string is owned by GIMP and must not be modified or
 * freed. The returned string is in the encoding used for filenames by
 * GLib, which isn't necessarily UTF-8. (On Windows it always is
 * UTF-8.).
 *
 * Since: 2.10.10
 *
 * Returns: The default top directory for GIMP cached files.
 **/
const gchar *
gimp_cache_directory (void)
{
  static gchar *gimp_cache_dir = NULL;

  if (! gimp_cache_dir)
    {
      gchar *tmp = g_build_filename (g_get_user_cache_dir (),
                                     GIMP_PACKAGE,
                                     GIMP_USER_VERSION,
                                     NULL);

      gimp_cache_dir = gimp_env_get_dir ("GIMP3_CACHEDIR", NULL, tmp);
      g_free (tmp);
    }

  return gimp_cache_dir;
}

/**
 * gimp_temp_directory:
 *
 * Returns the default top directory for GIMP temporary files. If the
 * environment variable GIMP3_TEMPDIR exists, that is used.  It
 * should be an absolute pathname.  Otherwise, a subdirectory of the
 * directory returned by g_get_tmp_dir() is used.
 *
 * In config files such as gimprc, the string ${gimp_temp_dir} expands
 * to this directory.
 *
 * Note that the actual directories used for GIMP temporary files can
 * be overridden by the user in the preferences dialog.
 *
 * The returned string is owned by GIMP and must not be modified or
 * freed. The returned string is in the encoding used for filenames by
 * GLib, which isn't necessarily UTF-8. (On Windows it always is
 * UTF-8.).
 *
 * Since: 2.10.10
 *
 * Returns: The default top directory for GIMP temporary files.
 **/
const gchar *
gimp_temp_directory (void)
{
  static gchar *gimp_temp_dir = NULL;

  if (! gimp_temp_dir)
    {
      gchar *tmp = g_build_filename (g_get_tmp_dir (),
                                     GIMP_PACKAGE,
                                     GIMP_USER_VERSION,
                                     NULL);

      gimp_temp_dir = gimp_env_get_dir ("GIMP3_TEMPDIR", NULL, tmp);
      g_free (tmp);
    }

  return gimp_temp_dir;
}

719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
static GFile *
gimp_child_file (const gchar *parent,
                 const gchar *element,
                 va_list      args)
{
  GFile *file = g_file_new_for_path (parent);

  while (element)
    {
      GFile *child = g_file_get_child (file, element);

      g_object_unref (file);
      file = child;

      element = va_arg (args, const gchar *);
    }

  return file;
}

739
/**
740 741 742 743 744
 * gimp_directory_file:
 * @first_element: the first element of a path to a file in the
 *                 user's GIMP directory, or %NULL.
 * @...: a %NULL terminated list of the remaining elements of the path
 *       to the file.
745
 *
746 747
 * Returns a #GFile in the user's GIMP directory, or the data
 * directory itself if @first_element is %NULL.
748
 *
749
 * See also: gimp_directory().
750
 *
751
 * Since: 2.10
752 753
 *
 * Returns: a new @GFile for the path, Free with g_object_unref().
754
 **/
755 756 757
GFile *
gimp_directory_file (const gchar *first_element,
                     ...)
758
{
759 760 761 762 763 764 765 766
  GFile   *file;
  va_list  args;

  va_start (args, first_element);
  file = gimp_child_file (gimp_directory (), first_element, args);
  va_end (args);

  return file;
767 768
}

769
/**
770 771 772 773 774 775 776 777 778 779 780
 * gimp_data_directory_file:
 * @first_element: the first element of a path to a file in the
 *                 data directory, or %NULL.
 * @...: a %NULL terminated list of the remaining elements of the path
 *       to the file.
 *
 * Returns a #GFile in the data directory, or the data directory
 * itself if @first_element is %NULL.
 *
 * See also: gimp_data_directory().
 *
781
 * Since: 2.10
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
 *
 * Returns: a new @GFile for the path, Free with g_object_unref().
 **/
GFile *
gimp_data_directory_file (const gchar *first_element,
                          ...)
{
  GFile   *file;
  va_list  args;

  va_start (args, first_element);
  file = gimp_child_file (gimp_data_directory (), first_element, args);
  va_end (args);

  return file;
}

/**
 * gimp_locale_directory_file:
 * @first_element: the first element of a path to a file in the
 *                 locale directory, or %NULL.
 * @...: a %NULL terminated list of the remaining elements of the path
 *       to the file.
 *
 * Returns a #GFile in the locale directory, or the locale directory
 * itself if @first_element is %NULL.
 *
 * See also: gimp_locale_directory().
810
 *
811
 * Since: 2.10
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
 *
 * Returns: a new @GFile for the path, Free with g_object_unref().
 **/
GFile *
gimp_locale_directory_file (const gchar *first_element,
                            ...)
{
  GFile   *file;
  va_list  args;

  va_start (args, first_element);
  file = gimp_child_file (gimp_locale_directory (), first_element, args);
  va_end (args);

  return file;
}

/**
 * gimp_sysconf_directory_file:
 * @first_element: the first element of a path to a file in the
 *                 sysconf directory, or %NULL.
 * @...: a %NULL terminated list of the remaining elements of the path
 *       to the file.
835
 *
836 837
 * Returns a #GFile in the sysconf directory, or the sysconf directory
 * itself if @first_element is %NULL.
838
 *
839
 * See also: gimp_sysconf_directory().
840
 *
841
 * Since: 2.10
842
 *
843
 * Returns: a new @GFile for the path, Free with g_object_unref().
844 845
 **/
GFile *
846 847
gimp_sysconf_directory_file (const gchar *first_element,
                             ...)
848
{
849 850
  GFile   *file;
  va_list  args;
851

852 853 854
  va_start (args, first_element);
  file = gimp_child_file (gimp_sysconf_directory (), first_element, args);
  va_end (args);
855 856 857 858

  return file;
}

859 860 861 862 863 864 865 866 867 868 869 870
/**
 * gimp_plug_in_directory_file:
 * @first_element: the first element of a path to a file in the
 *                 plug-in directory, or %NULL.
 * @...: a %NULL terminated list of the remaining elements of the path
 *       to the file.
 *
 * Returns a #GFile in the plug-in directory, or the plug-in directory
 * itself if @first_element is %NULL.
 *
 * See also: gimp_plug_in_directory().
 *
871
 * Since: 2.10
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
 *
 * Returns: a new @GFile for the path, Free with g_object_unref().
 **/
GFile *
gimp_plug_in_directory_file (const gchar *first_element,
                             ...)
{
  GFile   *file;
  va_list  args;

  va_start (args, first_element);
  file = gimp_child_file (gimp_plug_in_directory (), first_element, args);
  va_end (args);

  return file;
}

/**
 * gimp_personal_rc_file:
 * @basename: The basename of a rc_file.
 *
 * Returns the name of a file in the user-specific GIMP settings directory.
 *
 * The returned string is newly allocated and should be freed with
 * g_free() after use. The returned string is in the encoding used for
 * filenames by GLib, which isn't necessarily UTF-8. (On Windows it
 * always is UTF-8.)
 *
 * Returns: The name of a file in the user-specific GIMP settings directory.
 **/
gchar *
gimp_personal_rc_file (const gchar *basename)
{
  return g_build_filename (gimp_directory (), basename, NULL);
}

Sven Neumann's avatar
Sven Neumann committed
908 909 910
/**
 * gimp_gtkrc:
 *
911
 * Returns the name of GIMP's application-specific gtkrc file.
912
 *
913 914
 * The returned string is owned by GIMP and must not be modified or
 * freed. The returned string is in the encoding used for filenames by
915 916
 * GLib, which isn't necessarily UTF-8. (On Windows it always is
 * UTF-8.)
Sven Neumann's avatar
Sven Neumann committed
917
 *
918
 * Returns: The name of GIMP's application-specific gtkrc file.
919
 **/
920
const gchar *
Sven Neumann's avatar
Sven Neumann committed
921
gimp_gtkrc (void)
922
{
Sven Neumann's avatar
Sven Neumann committed
923
  static gchar *gimp_gtkrc_filename = NULL;
924

925
  if (! gimp_gtkrc_filename)
926
    gimp_gtkrc_filename = g_build_filename (gimp_data_directory (),
927
                                            "themes", "System", "gtkrc",
928
                                            NULL);
929

930 931
  return gimp_gtkrc_filename;
}
Sven Neumann's avatar
Sven Neumann committed
932

933 934
/**
 * gimp_path_runtime_fix:
935 936
 * @path: A pointer to a string (allocated with g_malloc) that is
 *        (or could be) a pathname.
937 938 939 940 941 942 943 944 945
 *
 * On Windows, this function checks if the string pointed to by @path
 * starts with the compile-time prefix, and in that case, replaces the
 * prefix with the run-time one.  @path should be a pointer to a
 * dynamically allocated (with g_malloc, g_strconcat, etc) string. If
 * the replacement takes place, the original string is deallocated,
 * and *@path is replaced with a pointer to a new string with the
 * run-time prefix spliced in.
 *
946 947 948
 * On Linux, it does the same thing, but only if BinReloc support is enabled.
 * On other Unices, it does nothing because those platforms don't have a
 * way to find out where our binary is.
949
 */
950
static void
951 952 953 954 955 956 957 958 959 960 961 962
gimp_path_runtime_fix (gchar **path)
{
#if defined (G_OS_WIN32) && defined (PREFIX)
  gchar *p;

  /* Yes, I do mean forward slashes below */
  if (strncmp (*path, PREFIX "/", strlen (PREFIX "/")) == 0)
    {
      /* This is a compile-time entry. Replace the path with the
       * real one on this machine.
       */
      p = *path;
963
      *path = g_strconcat (gimp_installation_directory (),
964 965 966
                           "\\",
                           *path + strlen (PREFIX "/"),
                           NULL);
967 968 969 970 971 972 973 974 975 976
      g_free (p);
    }
  /* Replace forward slashes with backslashes, just for
   * completeness */
  p = *path;
  while ((p = strchr (p, '/')) != NULL)
    {
      *p = '\\';
      p++;
    }
977 978 979 980 981
#elif defined (G_OS_WIN32)
  /* without defineing PREFIX do something useful too */
  gchar *p = *path;
  if (!g_path_is_absolute (p))
    {
982
      *path = g_build_filename (gimp_installation_directory (), *path, NULL);
983 984
      g_free (p);
    }
985 986 987
#else
  gchar *p;

988 989
  if (strncmp (*path, PREFIX G_DIR_SEPARATOR_S,
               strlen (PREFIX G_DIR_SEPARATOR_S)) == 0)
990 991 992 993 994
    {
      /* This is a compile-time entry. Replace the path with the
       * real one on this machine.
       */
      p = *path;
995
      *path = g_build_filename (gimp_installation_directory (),
996 997
                                *path + strlen (PREFIX G_DIR_SEPARATOR_S),
                                NULL);
998 999
      g_free (p);
    }
1000 1001 1002
#endif
}

1003 1004
/**
 * gimp_path_parse:
1005 1006
 * @path:         A list of directories separated by #G_SEARCHPATH_SEPARATOR.
 * @max_paths:    The maximum number of directories to return.
1007
 * @check:        %TRUE if you want the directories to be checked.
1008
 * @check_failed: Returns a #GList of path elements for which the
1009
 *                check failed.
1010
 *
1011
 * Returns: A #GList of all directories in @path.
1012
 **/
1013
GList *
1014
gimp_path_parse (const gchar  *path,
1015 1016 1017
                 gint          max_paths,
                 gboolean      check,
                 GList       **check_failed)
1018
{
1019 1020 1021 1022 1023
  gchar    **patharray;
  GList     *list      = NULL;
  GList     *fail_list = NULL;
  gint       i;
  gboolean   exists    = TRUE;
1024 1025 1026

  if (!path || !*path || max_paths < 1 || max_paths > 256)
    return NULL;
Sven Neumann's avatar
Sven Neumann committed
1027

1028 1029 1030 1031 1032
  patharray = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, max_paths);

  for (i = 0; i < max_paths; i++)
    {
      GString *dir;
Sven Neumann's avatar
Sven Neumann committed
1033

1034
      if (! patharray[i])
1035
        break;
1036

1037
#ifndef G_OS_WIN32
1038
      if (*patharray[i] == '~')
1039
        {
1040
          dir = g_string_new (g_get_home_dir ());
1041 1042
          g_string_append (dir, patharray[i] + 1);
        }
1043
      else
1044
#endif
1045 1046 1047 1048
        {
          gimp_path_runtime_fix (&patharray[i]);
          dir = g_string_new (patharray[i]);
        }
1049 1050

      if (check)
1051
        exists = g_file_test (dir->str, G_FILE_TEST_IS_DIR);
1052

1053
      if (exists)
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
        {
          GList *dup;

          /*  check for duplicate entries, see bug #784502  */
          for (dup = list; dup; dup = g_list_next (dup))
            {
              if (! strcmp (dir->str, dup->data))
                break;
            }

          /*  only add to the list if it's not a duplicate  */
          if (! dup)
            list = g_list_prepend (list, g_strdup (dir->str));
        }
1068
      else if (check_failed)
1069 1070 1071
        {
          fail_list = g_list_prepend (fail_list, g_strdup (dir->str));
        }
1072

Michael Natterer's avatar
Michael Natterer committed
1073
      g_string_free (dir, TRUE);
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
    }

  g_strfreev (patharray);

  list = g_list_reverse (list);

  if (check && check_failed)
    {
      fail_list = g_list_reverse (fail_list);
      *check_failed = fail_list;
    }

  return list;
}

/**
 * gimp_path_to_str:
 * @path: A list of directories as returned by gimp_path_parse().
 *
 * Returns: A searchpath string separated by #G_SEARCHPATH_SEPARATOR.
1094
 **/
1095 1096 1097
gchar *
gimp_path_to_str (GList *path)
{
1098
  GString *str    = NULL;
1099 1100 1101 1102 1103
  GList   *list;
  gchar   *retval = NULL;

  for (list = path; list; list = g_list_next (list))
    {
1104
      gchar *dir = list->data;
1105

1106
      if (str)
1107 1108 1109 1110
        {
          g_string_append_c (str, G_SEARCHPATH_SEPARATOR);
          g_string_append (str, dir);
        }
1111
      else
1112 1113 1114
        {
          str = g_string_new (dir);
        }
1115 1116 1117
    }

  if (str)
1118
    retval = g_string_free (str, FALSE);
1119 1120 1121 1122 1123 1124 1125 1126

  return retval;
}

/**
 * gimp_path_free:
 * @path: A list of directories as returned by gimp_path_parse().
 *
1127 1128
 * This function frees the memory allocated for the list and the strings
 * it contains.
1129
 **/
1130 1131 1132
void
gimp_path_free (GList *path)
{
1133
  g_list_free_full (path, (GDestroyNotify) g_free);
1134 1135 1136 1137 1138 1139
}

/**
 * gimp_path_get_user_writable_dir:
 * @path: A list of directories as returned by gimp_path_parse().
 *
1140 1141
 * Note that you have to g_free() the returned string.
 *
1142
 * Returns: The first directory in @path where the user has write permission.
1143
 **/
1144 1145 1146
gchar *
gimp_path_get_user_writable_dir (GList *path)
{
1147 1148 1149 1150 1151
  GList       *list;
  uid_t        euid;
  gid_t        egid;
  struct stat  filestat;
  gint         err;
1152

1153 1154
  g_return_val_if_fail (path != NULL, NULL);

1155 1156 1157 1158 1159
  euid = geteuid ();
  egid = getegid ();

  for (list = path; list; list = g_list_next (list))
    {
1160
      gchar *dir = list->data;
1161

1162
      /*  check if directory exists  */
1163
      err = g_stat (dir, &filestat);
1164 1165 1166 1167 1168 1169 1170 1171

      /*  this is tricky:
       *  if a file is e.g. owned by the current user but not user-writable,
       *  the user has no permission to write to the file regardless
       *  of his group's or other's write permissions
       */
      if (!err && S_ISDIR (filestat.st_mode) &&

1172
          ((filestat.st_mode & S_IWUSR) ||
1173

1174 1175
           ((filestat.st_mode & S_IWGRP) &&
            (euid != filestat.st_uid)) ||
1176

1177 1178 1179 1180 1181 1182
           ((filestat.st_mode & S_IWOTH) &&
            (euid != filestat.st_uid) &&
            (egid != filestat.st_gid))))
        {
          return g_strdup (dir);
        }
1183 1184 1185 1186
    }

  return NULL;
}
1187 1188 1189

static gchar *
gimp_env_get_dir (const gchar *gimp_env_name,
1190 1191
                  const gchar *compile_time_dir,
                  const gchar *relative_subdir)
1192
{
1193
  const gchar *env = g_getenv (gimp_env_name);
1194 1195 1196 1197

  if (env)
    {
      if (! g_path_is_absolute (env))
1198
        g_error ("%s environment variable should be an absolute path.",
1199
                 gimp_env_name);
1200

1201 1202
      return g_strdup (env);
    }
1203
  else if (compile_time_dir)
1204
    {
1205
      gchar *retval = g_strdup (compile_time_dir);
1206

1207
      gimp_path_runtime_fix (&retval);
1208

1209
      return retval;
1210
    }
1211 1212 1213 1214 1215 1216
  else if (! g_path_is_absolute (relative_subdir))
    {
      return g_build_filename (gimp_installation_directory (),
                               relative_subdir,
                               NULL);
    }
1217

1218
  return g_strdup (relative_subdir);
1219
}