From 35063b143b4604df9456a8c3fe7f4c6248ffd7d9 Mon Sep 17 00:00:00 2001 From: Sven Neumann Date: Fri, 11 May 2007 16:19:19 +0000 Subject: [PATCH] app/file/Makefile.am added a slightly modified copy of 2007-05-11 Sven Neumann * app/file/Makefile.am * app/file/xdg-user-dir.[ch]: added a slightly modified copy of xdg-user-dirs-lockup.c from freedesktop.org. svn path=/trunk/; revision=22472 --- ChangeLog | 6 ++ app/file/Makefile.am | 4 +- app/file/xdg-user-dir.c | 161 ++++++++++++++++++++++++++++++++++++++++ app/file/xdg-user-dir.h | 28 +++++++ 4 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 app/file/xdg-user-dir.c create mode 100644 app/file/xdg-user-dir.h diff --git a/ChangeLog b/ChangeLog index 649309e9de..63cfb3f19f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2007-05-11 Sven Neumann + + * app/file/Makefile.am + * app/file/xdg-user-dir.[ch]: added a slightly modified copy of + xdg-user-dirs-lockup.c from freedesktop.org. + 2007-05-11 Sven Neumann * app/main.c (gimp_dbus_open): don't attempt the conversion if the diff --git a/app/file/Makefile.am b/app/file/Makefile.am index 02ba4fc6b5..b8bf44efca 100644 --- a/app/file/Makefile.am +++ b/app/file/Makefile.am @@ -24,6 +24,8 @@ libappfile_a_SOURCES = \ gimprecentitem.c \ gimprecentitem.h \ gimprecentlist.c \ - gimprecentlist.h + gimprecentlist.h \ + xdg-user-dir.c \ + xdg-user-dir.h EXTRA_DIST = makefile.msc diff --git a/app/file/xdg-user-dir.c b/app/file/xdg-user-dir.c new file mode 100644 index 0000000000..4f0bd9880e --- /dev/null +++ b/app/file/xdg-user-dir.c @@ -0,0 +1,161 @@ +/* GIMP - The GNU Image Manipulation Program + * Copyright (C) 1995 Spencer Kimball and Peter Mattis + * + * This code is a slightly modified copy of xdg-user-dir-lockup.c + * as written by Alexander Larsson. + * + * See http://www.freedesktop.org/wiki/Software/xdg-user-dirs + */ + +/* + This file is not licenced under the GPL like the rest of the code. + Its is under the MIT license, to encourage reuse by cut-and-paste. + + Copyright (c) 2007 Red Hat, inc + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + +#include "config.h" + +#include + +#include +#include + +#include "xdg-user-dir.h" + + +/** + * xdg_user_dir_lookup_with_fallback: + * @type: a string specifying the type of directory + * + * Looks up a XDG user directory of the specified type. + * Example of types are "DESKTOP" and "DOWNLOAD". + * + * Return value: a newly allocated absolute pathname or %NULL + **/ +gchar * +xdg_user_dir_lookup (const gchar *type) +{ + FILE *file; + const gchar *home_dir; + const gchar *config_home; + gchar buffer[512]; + gchar *filename; + gchar *user_dir = NULL; + gchar *p, *d; + gint len; + gint relative; + + home_dir = g_get_home_dir (); + + if (! home_dir) + return NULL; + + config_home = g_getenv ("XDG_CONFIG_HOME"); + + if (! config_home || ! *config_home) + { + filename = g_build_filename (home_dir, ".config", "user-dirs.dirs", NULL); + } + else + { + filename = g_build_filename (config_home, "user-dirs.dirs", NULL); + } + + file = g_fopen (filename, "rb"); + + g_free (filename); + + if (! file) + return NULL; + + while (fgets (buffer, sizeof (buffer), file)) + { + /* Remove newline at end */ + len = strlen (buffer); + if (len > 0 && buffer[len-1] == '\n') + buffer[len-1] = 0; + + p = buffer; + while (*p == ' ' || *p == '\t') + p++; + + if (strncmp (p, "XDG_", 4) != 0) + continue; + p += 4; + if (strncmp (p, type, strlen (type)) != 0) + continue; + p += strlen (type); + if (strncmp (p, "_DIR", 4) != 0) + continue; + p += 4; + + while (*p == ' ' || *p == '\t') + p++; + + if (*p != '=') + continue; + p++; + + while (*p == ' ' || *p == '\t') + p++; + + if (*p != '"') + continue; + p++; + + relative = 0; + if (strncmp (p, "$HOME/", 6) == 0) + { + p += 6; + relative = 1; + } + else if (*p != '/') + continue; + + if (relative) + { + user_dir = g_malloc (strlen (home_dir) + 1 + strlen (p) + 1); + strcpy (user_dir, home_dir); + strcat (user_dir, "/"); + } + else + { + user_dir = g_malloc (strlen (p) + 1); + *user_dir = 0; + } + + d = user_dir + strlen (user_dir); + while (*p && *p != '"') + { + if ((*p == '\\') && (*(p+1) != 0)) + p++; + *d++ = *p++; + } + *d = 0; + } + + fclose (file); + + return user_dir; +} diff --git a/app/file/xdg-user-dir.h b/app/file/xdg-user-dir.h new file mode 100644 index 0000000000..d45f7a2528 --- /dev/null +++ b/app/file/xdg-user-dir.h @@ -0,0 +1,28 @@ +/* GIMP - The GNU Image Manipulation Program + * Copyright (C) 1995 Spencer Kimball and Peter Mattis + * + * xdg-user-dir.h + * + * 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. + */ + +#ifndef __XDG_USER_DIR_H__ +#define __XDG_USER_DIR_H__ + + +gchar * xdg_user_dir_lookup (const gchar *type); + + +#endif /* __XDG_USER_DIR_H__ */