2006-12-09 21:33:38 +00:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2001-03-30 13:31:41 +00:00
|
|
|
* Copyright (C) 1995, 1996, 1997 Spencer Kimball and Peter Mattis
|
|
|
|
* Copyright (C) 1997 Josh MacDonald
|
|
|
|
*
|
2004-09-22 15:12:24 +00:00
|
|
|
* file-utils.c
|
|
|
|
*
|
2009-01-17 22:28:01 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2001-03-30 13:31:41 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-17 22:28:01 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2001-03-30 13:31:41 +00:00
|
|
|
* (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
|
2009-01-17 22:28:01 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2001-03-30 13:31:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
2005-02-07 01:38:18 +00:00
|
|
|
|
2008-10-09 20:24:04 +00:00
|
|
|
#include <gegl.h>
|
2006-04-10 12:59:17 +00:00
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
|
|
|
|
|
|
|
#include "libgimpbase/gimpbase.h"
|
2001-03-30 13:31:41 +00:00
|
|
|
#include "libgimpmath/gimpmath.h"
|
2006-04-10 12:59:17 +00:00
|
|
|
#include "libgimpthumb/gimpthumb.h"
|
2001-03-30 13:31:41 +00:00
|
|
|
|
2001-05-09 22:34:59 +00:00
|
|
|
#include "core/core-types.h"
|
2001-03-30 13:31:41 +00:00
|
|
|
|
2002-04-14 14:38:55 +00:00
|
|
|
#include "core/gimp.h"
|
2001-05-09 02:32:03 +00:00
|
|
|
#include "core/gimpimage.h"
|
2006-04-10 12:59:17 +00:00
|
|
|
#include "core/gimpimagefile.h"
|
2001-05-09 02:32:03 +00:00
|
|
|
|
2007-01-19 11:48:48 +00:00
|
|
|
#include "plug-in/gimppluginmanager.h"
|
2001-10-25 13:30:01 +00:00
|
|
|
|
2007-05-11 18:50:35 +00:00
|
|
|
#include "file-procedure.h"
|
2001-12-01 00:14:14 +00:00
|
|
|
#include "file-utils.h"
|
2001-03-30 13:31:41 +00:00
|
|
|
|
2003-03-25 16:38:19 +00:00
|
|
|
#include "gimp-intl.h"
|
2002-04-16 20:25:27 +00:00
|
|
|
|
2001-03-30 13:31:41 +00:00
|
|
|
|
2014-07-08 10:14:53 +02:00
|
|
|
static gboolean
|
2007-05-13 23:15:34 +00:00
|
|
|
file_utils_filename_is_uri (const gchar *filename,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (filename != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
|
|
|
if (strstr (filename, "://"))
|
|
|
|
{
|
|
|
|
gchar *scheme;
|
|
|
|
gchar *canon;
|
|
|
|
|
|
|
|
scheme = g_strndup (filename, (strstr (filename, "://") - filename));
|
|
|
|
canon = g_strdup (scheme);
|
|
|
|
|
|
|
|
g_strcanon (canon, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "+-.", '-');
|
|
|
|
|
|
|
|
if (strcmp (scheme, canon) || ! g_ascii_isgraph (canon[0]))
|
|
|
|
{
|
|
|
|
g_set_error (error, G_FILE_ERROR, 0,
|
|
|
|
_("'%s:' is not a valid URI scheme"), scheme);
|
|
|
|
|
|
|
|
g_free (scheme);
|
|
|
|
g_free (canon);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (scheme);
|
|
|
|
g_free (canon);
|
|
|
|
|
|
|
|
if (! g_utf8_validate (filename, -1, NULL))
|
|
|
|
{
|
2008-11-04 12:33:09 +00:00
|
|
|
g_set_error_literal (error,
|
|
|
|
G_CONVERT_ERROR,
|
|
|
|
G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid character sequence in URI"));
|
2007-05-13 23:15:34 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2002-04-14 14:38:55 +00:00
|
|
|
gchar *
|
2007-01-19 11:48:48 +00:00
|
|
|
file_utils_filename_to_uri (Gimp *gimp,
|
2002-04-14 14:38:55 +00:00
|
|
|
const gchar *filename,
|
|
|
|
GError **error)
|
|
|
|
{
|
2014-07-07 00:46:25 +02:00
|
|
|
GFile *file;
|
2007-05-14 21:17:10 +00:00
|
|
|
gchar *absolute;
|
|
|
|
gchar *uri;
|
2014-07-07 00:46:25 +02:00
|
|
|
GError *temp_error = NULL;
|
2002-04-14 14:38:55 +00:00
|
|
|
|
2007-01-19 11:48:48 +00:00
|
|
|
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
|
2002-04-14 14:38:55 +00:00
|
|
|
g_return_val_if_fail (filename != NULL, NULL);
|
2007-01-19 11:33:45 +00:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
2002-04-14 14:38:55 +00:00
|
|
|
|
2014-07-07 00:46:25 +02:00
|
|
|
file = g_file_new_for_uri (filename);
|
|
|
|
|
2002-04-14 14:38:55 +00:00
|
|
|
/* check for prefixes like http or ftp */
|
2007-05-11 18:50:35 +00:00
|
|
|
if (file_procedure_find_by_prefix (gimp->plug_in_manager->load_procs,
|
2014-07-07 00:46:25 +02:00
|
|
|
file))
|
2002-04-14 14:38:55 +00:00
|
|
|
{
|
2002-04-16 20:25:27 +00:00
|
|
|
if (g_utf8_validate (filename, -1, NULL))
|
|
|
|
{
|
2014-07-07 00:46:25 +02:00
|
|
|
g_object_unref (file);
|
|
|
|
|
2002-04-16 20:25:27 +00:00
|
|
|
return g_strdup (filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-04 12:33:09 +00:00
|
|
|
g_set_error_literal (error,
|
|
|
|
G_CONVERT_ERROR,
|
|
|
|
G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid character sequence in URI"));
|
2014-07-07 00:46:25 +02:00
|
|
|
g_object_unref (file);
|
2002-04-16 20:25:27 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2007-05-14 21:17:10 +00:00
|
|
|
else if (file_utils_filename_is_uri (filename, &temp_error))
|
2006-10-15 20:04:51 +00:00
|
|
|
{
|
2014-07-07 00:46:25 +02:00
|
|
|
g_object_unref (file);
|
|
|
|
|
2007-05-13 23:15:34 +00:00
|
|
|
return g_strdup (filename);
|
|
|
|
}
|
2007-05-14 21:17:10 +00:00
|
|
|
else if (temp_error)
|
2007-05-13 23:15:34 +00:00
|
|
|
{
|
2007-05-14 21:17:10 +00:00
|
|
|
g_propagate_error (error, temp_error);
|
2014-07-07 00:46:25 +02:00
|
|
|
g_object_unref (file);
|
2007-05-14 21:17:10 +00:00
|
|
|
|
2007-05-13 23:15:34 +00:00
|
|
|
return NULL;
|
2006-10-15 20:04:51 +00:00
|
|
|
}
|
2002-04-14 14:38:55 +00:00
|
|
|
|
2014-07-07 00:46:25 +02:00
|
|
|
g_object_unref (file);
|
|
|
|
|
2002-04-14 14:38:55 +00:00
|
|
|
if (! g_path_is_absolute (filename))
|
|
|
|
{
|
|
|
|
gchar *current;
|
|
|
|
|
|
|
|
current = g_get_current_dir ();
|
|
|
|
absolute = g_build_filename (current, filename, NULL);
|
|
|
|
g_free (current);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
absolute = g_strdup (filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
uri = g_filename_to_uri (absolute, NULL, error);
|
|
|
|
|
|
|
|
g_free (absolute);
|
|
|
|
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
2014-07-07 23:56:56 +02:00
|
|
|
GFile *
|
|
|
|
file_utils_file_with_new_ext (GFile *file,
|
|
|
|
GFile *ext_file)
|
2009-05-05 03:00:53 +02:00
|
|
|
{
|
2014-07-08 02:58:59 +02:00
|
|
|
gchar *uri;
|
|
|
|
gchar *file_ext;
|
|
|
|
gint file_ext_len = 0;
|
|
|
|
gchar *ext_file_ext = NULL;
|
|
|
|
gchar *uri_without_ext;
|
|
|
|
gchar *new_uri;
|
|
|
|
GFile *ret;
|
2014-07-07 23:56:56 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
|
|
|
g_return_val_if_fail (ext_file == NULL || G_IS_FILE (ext_file), NULL);
|
|
|
|
|
2014-07-08 02:58:59 +02:00
|
|
|
uri = g_file_get_uri (file);
|
|
|
|
file_ext = file_utils_file_get_ext (file);
|
2014-07-07 23:56:56 +02:00
|
|
|
|
2014-07-08 02:58:59 +02:00
|
|
|
if (file_ext)
|
2014-07-07 23:56:56 +02:00
|
|
|
{
|
2014-07-08 02:58:59 +02:00
|
|
|
file_ext_len = strlen (file_ext);
|
|
|
|
g_free (file_ext);
|
2014-07-07 23:56:56 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 02:58:59 +02:00
|
|
|
if (ext_file)
|
|
|
|
ext_file_ext = file_utils_file_get_ext (ext_file);
|
|
|
|
|
|
|
|
uri_without_ext = g_strndup (uri, strlen (uri) - file_ext_len);
|
|
|
|
|
|
|
|
g_free (uri);
|
2014-07-07 23:56:56 +02:00
|
|
|
|
2014-07-08 02:58:59 +02:00
|
|
|
new_uri = g_strconcat (uri_without_ext, ext_file_ext, NULL);
|
2014-07-07 23:56:56 +02:00
|
|
|
|
|
|
|
ret = g_file_new_for_uri (new_uri);
|
|
|
|
|
2014-07-08 02:58:59 +02:00
|
|
|
g_free (ext_file_ext);
|
2009-05-05 03:00:53 +02:00
|
|
|
g_free (uri_without_ext);
|
2014-07-07 23:56:56 +02:00
|
|
|
g_free (new_uri);
|
|
|
|
|
2009-05-05 03:00:53 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-08 02:58:59 +02:00
|
|
|
* file_utils_file_get_ext:
|
|
|
|
* @file:
|
2009-05-05 03:00:53 +02:00
|
|
|
*
|
2014-07-08 02:58:59 +02:00
|
|
|
* Returns the position of the extension (including the .), or NULL
|
|
|
|
* if there is no extension.
|
2009-05-05 03:00:53 +02:00
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
**/
|
2014-07-08 02:58:59 +02:00
|
|
|
gchar *
|
|
|
|
file_utils_file_get_ext (GFile *file)
|
2009-05-05 03:00:53 +02:00
|
|
|
{
|
2014-07-08 02:58:59 +02:00
|
|
|
gchar *uri;
|
|
|
|
gint uri_len;
|
|
|
|
gchar *ext = NULL;
|
|
|
|
gint search_len;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
|
|
|
|
|
|
|
uri = g_file_get_uri (file);
|
|
|
|
uri_len = strlen (uri);
|
2009-05-05 03:00:53 +02:00
|
|
|
|
|
|
|
if (g_strrstr (uri, ".gz"))
|
|
|
|
search_len = uri_len - 3;
|
|
|
|
else if (g_strrstr (uri, ".bz2"))
|
|
|
|
search_len = uri_len - 4;
|
2013-05-02 20:05:35 +02:00
|
|
|
else if (g_strrstr (uri, ".xz"))
|
|
|
|
search_len = uri_len - 3;
|
2009-05-05 03:00:53 +02:00
|
|
|
else
|
|
|
|
search_len = uri_len;
|
|
|
|
|
|
|
|
ext = g_strrstr_len (uri, search_len, ".");
|
|
|
|
|
2014-07-08 02:58:59 +02:00
|
|
|
if (ext)
|
|
|
|
ext = g_strdup (ext);
|
|
|
|
|
|
|
|
g_free (uri);
|
2009-05-05 03:00:53 +02:00
|
|
|
|
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
2006-04-10 12:59:17 +00:00
|
|
|
GdkPixbuf *
|
|
|
|
file_utils_load_thumbnail (const gchar *filename)
|
|
|
|
{
|
|
|
|
GimpThumbnail *thumbnail = NULL;
|
|
|
|
GdkPixbuf *pixbuf = NULL;
|
|
|
|
gchar *uri;
|
|
|
|
|
|
|
|
g_return_val_if_fail (filename != NULL, NULL);
|
|
|
|
|
|
|
|
uri = g_filename_to_uri (filename, NULL, NULL);
|
|
|
|
|
|
|
|
if (uri)
|
|
|
|
{
|
|
|
|
thumbnail = gimp_thumbnail_new ();
|
|
|
|
gimp_thumbnail_set_uri (thumbnail, uri);
|
|
|
|
|
|
|
|
pixbuf = gimp_thumbnail_load_thumb (thumbnail,
|
|
|
|
GIMP_THUMBNAIL_SIZE_NORMAL,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (uri);
|
|
|
|
|
|
|
|
if (pixbuf)
|
|
|
|
{
|
|
|
|
gint width = gdk_pixbuf_get_width (pixbuf);
|
|
|
|
gint height = gdk_pixbuf_get_height (pixbuf);
|
|
|
|
|
|
|
|
if (gdk_pixbuf_get_n_channels (pixbuf) != 3)
|
2006-04-12 12:49:29 +00:00
|
|
|
{
|
2006-04-10 12:59:17 +00:00
|
|
|
GdkPixbuf *tmp = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
|
|
|
|
width, height);
|
|
|
|
|
|
|
|
gdk_pixbuf_composite_color (pixbuf, tmp,
|
|
|
|
0, 0, width, height, 0, 0, 1.0, 1.0,
|
|
|
|
GDK_INTERP_NEAREST, 255,
|
|
|
|
0, 0, GIMP_CHECK_SIZE_SM,
|
|
|
|
0x66666666, 0x99999999);
|
|
|
|
|
|
|
|
g_object_unref (pixbuf);
|
|
|
|
pixbuf = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pixbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2006-04-11 00:30:06 +00:00
|
|
|
file_utils_save_thumbnail (GimpImage *image,
|
|
|
|
const gchar *filename)
|
2006-04-10 12:59:17 +00:00
|
|
|
{
|
2014-07-07 23:56:56 +02:00
|
|
|
GFile *file;
|
|
|
|
gboolean success = FALSE;
|
2006-04-10 12:59:17 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
|
|
|
|
g_return_val_if_fail (filename != NULL, FALSE);
|
|
|
|
|
2014-07-07 23:56:56 +02:00
|
|
|
file = gimp_image_get_file (image);
|
2006-04-10 12:59:17 +00:00
|
|
|
|
2014-07-07 23:56:56 +02:00
|
|
|
if (file)
|
2006-04-10 12:59:17 +00:00
|
|
|
{
|
2014-07-07 23:56:56 +02:00
|
|
|
gchar *image_uri = g_file_get_uri (file);
|
|
|
|
gchar *uri = g_filename_to_uri (filename, NULL, NULL);
|
2006-04-10 12:59:17 +00:00
|
|
|
|
2014-07-07 23:56:56 +02:00
|
|
|
if (uri && image_uri && ! strcmp (uri, image_uri))
|
2006-04-10 12:59:17 +00:00
|
|
|
{
|
2014-07-07 23:56:56 +02:00
|
|
|
GimpImagefile *imagefile;
|
2014-07-05 12:51:54 +02:00
|
|
|
|
2014-07-07 23:56:56 +02:00
|
|
|
imagefile = gimp_imagefile_new (image->gimp, file);
|
|
|
|
success = gimp_imagefile_save_thumbnail (imagefile, NULL, image,
|
|
|
|
NULL);
|
|
|
|
g_object_unref (imagefile);
|
2006-04-10 12:59:17 +00:00
|
|
|
}
|
2014-07-07 23:56:56 +02:00
|
|
|
|
|
|
|
g_free (image_uri);
|
|
|
|
g_free (uri);
|
2006-04-10 12:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|