gimpconfig-path.c 11.8 KB
Newer Older
1 2
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
3
 *
4
 * gimpconfig-path.c
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * Copyright (C) 2001  Sven Neumann <sven@gimp.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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <stdio.h>
#include <string.h>

#include <glib-object.h>

29
#include "libgimpbase/gimpbase.h"
30

31 32
#include "gimpconfig-path.h"

33
#include "libgimp/libgimp-intl.h"
34 35


36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
/**
 * gimp_config_path_get_type:
 *
 * Reveals the object type
 *
 * Returns: the #GType for a GimpConfigPath string property
 *
 * Since: GIMP 2.4
 **/
GType
gimp_config_path_get_type (void)
{
  static GType path_type = 0;

  if (!path_type)
    {
      static const GTypeInfo type_info = { 0, };

      path_type = g_type_register_static (G_TYPE_STRING, "GimpConfigPath",
                                          &type_info, 0);
    }

  return path_type;
}


/*
 * GIMP_TYPE_PARAM_CONFIG_PATH
 */

#define GIMP_PARAM_SPEC_CONFIG_PATH(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), GIMP_TYPE_PARAM_CONFIG_PATH, GimpParamSpecConfigPath))

typedef struct _GimpParamSpecConfigPath GimpParamSpecConfigPath;

struct _GimpParamSpecConfigPath
{
  GParamSpecString    parent_instance;

  GimpConfigPathType  type;
};

static void  gimp_param_config_path_class_init (GParamSpecClass *class);

/**
 * gimp_param_config_path_get_type:
 *
 * Reveals the object type
 *
 * Returns: the #GType for a directory path object
 *
 * Since: GIMP 2.4
 **/
GType
gimp_param_config_path_get_type (void)
{
  static GType spec_type = 0;

  if (!spec_type)
    {
      static const GTypeInfo type_info =
      {
        sizeof (GParamSpecClass),
        NULL, NULL,
        (GClassInitFunc) gimp_param_config_path_class_init,
        NULL, NULL,
        sizeof (GimpParamSpecConfigPath),
        0, NULL, NULL
      };

      spec_type = g_type_register_static (G_TYPE_PARAM_STRING,
                                          "GimpParamConfigPath",
                                          &type_info, 0);
    }

  return spec_type;
}

static void
gimp_param_config_path_class_init (GParamSpecClass *class)
{
  class->value_type = GIMP_TYPE_CONFIG_PATH;
}

/**
 * gimp_param_spec_config_path:
 * @name:          Canonical name of the param
 * @nick:          Nickname of the param
 * @blurb:         Brief desciption of param.
 * @type:          a #GimpParamConfigPathType value.
 * @default_value: Value to use if none is assigned.
 * @flags:         a combination of #GParamFlags
 *
 * Creates a param spec to hold a filename, dir name,
 * or list of file or dir names.
 * See g_param_spec_internal() for more information.
 *
 * Returns: a newly allocated #GParamSpec instance
 *
 * Since: GIMP 2.4
 **/
GParamSpec *
gimp_param_spec_config_path (const gchar        *name,
                             const gchar        *nick,
                             const gchar        *blurb,
                             GimpConfigPathType  type,
141
                             const gchar        *default_value,
142 143 144 145 146 147 148
                             GParamFlags         flags)
{
  GParamSpecString *pspec;

  pspec = g_param_spec_internal (GIMP_TYPE_PARAM_CONFIG_PATH,
                                 name, nick, blurb, flags);

149
  pspec->default_value = g_strdup (default_value);
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

  GIMP_PARAM_SPEC_CONFIG_PATH (pspec)->type = type;

  return G_PARAM_SPEC (pspec);
}

/**
 * gimp_param_spec_config_path_type:
 * @pspec:         A #GParamSpec for a path param
 *
 * Tells whether the path param encodes a filename,
 * dir name, or list of file or dir names.
 *
 * Returns: a #GimpConfigPathType value
 *
 * Since: GIMP 2.4
 **/
GimpConfigPathType
gimp_param_spec_config_path_type (GParamSpec *pspec)
{
  g_return_val_if_fail (GIMP_IS_PARAM_SPEC_CONFIG_PATH (pspec), 0);

  return GIMP_PARAM_SPEC_CONFIG_PATH (pspec)->type;
}
174

175

176 177 178
/*
 * GimpConfig path utilities
 */
179

180 181 182 183
static gchar        * gimp_config_path_expand_only   (const gchar  *path,
                                                      GError      **error);
static inline gchar * gimp_config_path_extract_token (const gchar **str);

Sven Neumann's avatar
Sven Neumann committed
184

185 186
/**
 * gimp_config_build_data_path:
Sven Neumann's avatar
Sven Neumann committed
187 188 189 190 191 192
 * @name: directory name (in UTF-8 encoding)
 *
 * Creates a search path as it is used in the gimprc file.  The path
 * returned by gimp_config_build_data_path() includes a directory
 * below the user's gimp directory and one in the system-wide data
 * directory.
193
 *
Sven Neumann's avatar
Sven Neumann committed
194 195 196 197 198
 * Note that you cannot use this path directly with gimp_path_parse().
 * As it is in the gimprc notation, you first need to expand and
 * recode it using gimp_config_path_expand().
 *
 * Returns: a newly allocated string
199 200 201
 *
 * Since: GIMP 2.4
 **/
202 203 204 205 206 207 208 209
gchar *
gimp_config_build_data_path (const gchar *name)
{
  return g_strconcat ("${gimp_dir}", G_DIR_SEPARATOR_S, name,
                      G_SEARCHPATH_SEPARATOR_S,
                      "${gimp_data_dir}", G_DIR_SEPARATOR_S, name,
                      NULL);
}
210

211 212
/**
 * gimp_config_build_plug_in_path:
Sven Neumann's avatar
Sven Neumann committed
213 214 215 216 217 218
 * @name: directory name (in UTF-8 encoding)
 *
 * Creates a search path as it is used in the gimprc file.  The path
 * returned by gimp_config_build_plug_in_path() includes a directory
 * below the user's gimp directory and one in the system-wide plug-in
 * directory.
219
 *
Sven Neumann's avatar
Sven Neumann committed
220 221 222 223 224
 * Note that you cannot use this path directly with gimp_path_parse().
 * As it is in the gimprc notation, you first need to expand and
 * recode it using gimp_config_path_expand().
 *
 * Returns: a newly allocated string
225 226 227
 *
 * Since: GIMP 2.4
 **/
228 229 230 231 232 233 234
gchar *
gimp_config_build_plug_in_path (const gchar *name)
{
  return g_strconcat ("${gimp_dir}", G_DIR_SEPARATOR_S, name,
                      G_SEARCHPATH_SEPARATOR_S,
                      "${gimp_plug_in_dir}", G_DIR_SEPARATOR_S, name,
                      NULL);
235
}
236

237 238
/**
 * gimp_config_build_writable_path:
Sven Neumann's avatar
Sven Neumann committed
239 240 241 242 243 244 245 246 247
 * @name: directory name (in UTF-8 encoding)
 *
 * Creates a search path as it is used in the gimprc file.  The path
 * returned by gimp_config_build_writable_path() is just the writable
 * parts of the search path constructed by gimp_config_build_data_path().
 *
 * Note that you cannot use this path directly with gimp_path_parse().
 * As it is in the gimprc notation, you first need to expand and
 * recode it using gimp_config_path_expand().
248
 *
Sven Neumann's avatar
Sven Neumann committed
249
 * Returns: a newly allocated string
250 251 252 253 254 255 256 257 258
 *
 * Since: GIMP 2.4
 **/
gchar *
gimp_config_build_writable_path (const gchar *name)
{
  return g_strconcat ("${gimp_dir}", G_DIR_SEPARATOR_S, name, NULL);
}

259

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
/**
 * gimp_config_path_expand:
 * @path: a %NUL-terminated string in UTF-8 encoding
 * @recode: whether to convert to the filesystem's encoding
 * @error: return location for errors
 *
 * Paths as stored in the gimprc have to be treated special.  The
 * string may contain special identifiers such as for example
 * ${gimp_dir} that have to be substituted before use. Also the user's
 * filesystem may be in a different encoding than UTF-8 (which is what
 * is used for the gimprc). This function does the variable
 * substitution for you and can also attempt to convert to the
 * filesystem encoding.
 *
 * Return value: a newly allocated %NUL-terminated string
275 276
 *
 * Since: GIMP 2.4
277
 **/
278
gchar *
279 280 281
gimp_config_path_expand (const gchar  *path,
                         gboolean      recode,
                         GError      **error)
282 283 284 285 286 287 288 289 290
{
  g_return_val_if_fail (path != NULL, NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  if (recode)
    {
      gchar *retval;
      gchar *expanded = gimp_config_path_expand_only (path, error);

291 292 293
      if (! expanded)
        return NULL;

294 295 296 297 298 299
      retval = g_filename_from_utf8 (expanded, -1, NULL, NULL, error);

      g_free (expanded);

      return retval;
    }
300 301

  return gimp_config_path_expand_only (path, error);
302 303
}

304 305 306

#define SUBSTS_ALLOC 4

307 308 309
static gchar *
gimp_config_path_expand_only (const gchar  *path,
                              GError      **error)
310
{
311
  const gchar *home;
Sven Neumann's avatar
Sven Neumann committed
312
  const gchar *p;
313
  const gchar *s;
Sven Neumann's avatar
Sven Neumann committed
314
  gchar       *n;
315
  gchar       *token;
316
  gchar       *filename = NULL;
317
  gchar       *expanded = NULL;
318 319 320
  gchar      **substs   = NULL;
  guint        n_substs = 0;
  gint         length   = 0;
321
  gint         i;
322

323 324 325 326
  home = g_get_home_dir ();
  if (home)
    home = gimp_filename_to_utf8 (home);

327
  p = path;
328

329
  while (*p)
330
    {
331
      if (*p == '~' && home)
332
	{
333 334
          length += strlen (home);
          p += 1;
335
	}
336
      else if ((token = gimp_config_path_extract_token (&p)) != NULL)
337
        {
338 339 340
          for (i = 0; i < n_substs; i++)
            if (strcmp (substs[2*i], token) == 0)
              break;
341

342 343 344 345 346 347
          if (i < n_substs)
            {
              s = substs[2*i+1];
            }
          else
            {
348
              s = NULL;
349

350
              if (strcmp (token, "gimp_dir") == 0)
351
                s = gimp_directory ();
352
              else if (strcmp (token, "gimp_data_dir") == 0)
353
                s = gimp_data_directory ();
354 355
              else if (strcmp (token, "gimp_plug_in_dir") == 0 ||
		       strcmp (token, "gimp_plugin_dir") == 0)
356
                s = gimp_plug_in_directory ();
357
              else if (strcmp (token, "gimp_sysconf_dir") == 0)
358
                s = gimp_sysconf_directory ();
359

360 361
              if (!s)
                s = g_getenv (token);
362

363
#ifdef G_OS_WIN32
364 365 366 367 368 369 370 371 372
              /* The default user gimprc on Windows references
               * ${TEMP}, but not all Windows installations have that
               * environment variable, even if it should be kinda
               * standard. So special-case it.
               */
              if (!s && strcmp (token, "TEMP") == 0)
                s = g_get_tmp_dir ();
#endif  /* G_OS_WIN32 */
            }
373

374 375
          if (!s)
            {
376
              g_set_error (error, 0, 0, _("Cannot expand ${%s}"), token);
377 378
              g_free (token);
              goto cleanup;
379
            }
380

381 382
          if (n_substs % SUBSTS_ALLOC == 0)
            substs = g_renew (gchar *, substs, 2 * (n_substs + SUBSTS_ALLOC));
383

384
          substs[2*n_substs]     = token;
385
          substs[2*n_substs + 1] = (gchar *) gimp_filename_to_utf8 (s);
386

387 388 389
          length += strlen (substs[2*n_substs + 1]);

          n_substs++;
390 391 392
        }
      else
	{
393
          length += g_utf8_skip[(const guchar) *p];
394
          p = g_utf8_next_char (p);
395 396 397
	}
    }

398
  if (n_substs == 0)
399
    return g_strdup (path);
400

401
  expanded = g_new (gchar, length + 1);
402

403
  p = path;
404
  n = expanded;
405

Sven Neumann's avatar
Sven Neumann committed
406
  while (*p)
407
    {
408
      if (*p == '~' && home)
409
	{
Sven Neumann's avatar
Sven Neumann committed
410
	  *n = '\0';
411 412
	  strcat (n, home);
	  n += strlen (home);
Sven Neumann's avatar
Sven Neumann committed
413
	  p += 1;
414
	}
415
      else if ((token = gimp_config_path_extract_token (&p)) != NULL)
416
        {
417
          for (i = 0; i < n_substs; i++)
418
            {
419
              if (strcmp (substs[2*i], token) == 0)
420
                {
421
                  s = substs[2*i+1];
422

Sven Neumann's avatar
Sven Neumann committed
423
                  *n = '\0';
424 425
                  strcat (n, s);
                  n += strlen (s);
426 427 428 429 430 431 432 433 434

                  break;
                }
            }

          g_free (token);
	}
      else
	{
Sven Neumann's avatar
Sven Neumann committed
435
	  *n++ = *p++;
436 437 438
	}
    }

Sven Neumann's avatar
Sven Neumann committed
439
  *n = '\0';
440 441

 cleanup:
442 443
  for (i = 0; i < n_substs; i++)
    g_free (substs[2*i]);
444

445
  g_free (substs);
446
  g_free (filename);
447

448
  return expanded;
449
}
450 451

static inline gchar *
452
gimp_config_path_extract_token (const gchar **str)
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
{
  const gchar *p;
  gchar       *token;

  if (strncmp (*str, "${", 2))
    return NULL;

  p = *str + 2;

  while (*p && (*p != '}'))
    p = g_utf8_next_char (p);

  if (!p)
    return NULL;

  token = g_strndup (*str + 2, g_utf8_pointer_to_offset (*str + 2, p));

  *str = p + 1; /* after the closing bracket */

  return token;
}