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
|
2018-07-11 23:27:07 +02:00
|
|
|
* along with this program. If not, see <https://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
|
|
|
|
2016-01-03 20:05:27 +01:00
|
|
|
#include "plug-in/gimppluginmanager-file.h"
|
2001-10-25 13:30:01 +00:00
|
|
|
|
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,
|
2016-12-21 04:05:32 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-08 10:25:25 +02:00
|
|
|
GFile *
|
|
|
|
file_utils_filename_to_file (Gimp *gimp,
|
|
|
|
const gchar *filename,
|
|
|
|
GError **error)
|
2002-04-14 14:38:55 +00:00
|
|
|
{
|
2014-07-07 00:46:25 +02:00
|
|
|
GFile *file;
|
2007-05-14 21:17:10 +00:00
|
|
|
gchar *absolute;
|
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);
|
|
|
|
|
2019-07-02 17:19:35 +02:00
|
|
|
if (! file)
|
|
|
|
{
|
|
|
|
/* Despite the docs says it never fails, it actually can on Windows.
|
|
|
|
* See issue #3093 (and glib#1819).
|
|
|
|
*/
|
|
|
|
g_set_error_literal (error,
|
|
|
|
G_CONVERT_ERROR,
|
|
|
|
G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid character sequence in URI"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-04-14 14:38:55 +00:00
|
|
|
/* check for prefixes like http or ftp */
|
2016-01-03 20:05:27 +01:00
|
|
|
if (gimp_plug_in_manager_file_procedure_find_by_prefix (gimp->plug_in_manager,
|
|
|
|
GIMP_FILE_PROCEDURE_GROUP_OPEN,
|
|
|
|
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-08 10:25:25 +02:00
|
|
|
return file;
|
2002-04-16 20:25:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-04 12:33:09 +00:00
|
|
|
g_set_error_literal (error,
|
2016-12-21 04:05:32 +01:00
|
|
|
G_CONVERT_ERROR,
|
|
|
|
G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid character sequence in URI"));
|
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-08 10:25:25 +02:00
|
|
|
return file;
|
2007-05-13 23:15:34 +00:00
|
|
|
}
|
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-08 20:23:10 +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);
|
|
|
|
}
|
|
|
|
|
2014-07-08 10:25:25 +02:00
|
|
|
file = g_file_new_for_path (absolute);
|
2002-04-14 14:38:55 +00:00
|
|
|
|
|
|
|
g_free (absolute);
|
|
|
|
|
2014-07-08 10:25:25 +02:00
|
|
|
return file;
|
2002-04-14 14:38:55 +00:00
|
|
|
}
|
|
|
|
|
2006-04-10 12:59:17 +00:00
|
|
|
GdkPixbuf *
|
2019-09-11 21:48:34 +02:00
|
|
|
file_utils_load_thumbnail (GFile *file)
|
2006-04-10 12:59:17 +00:00
|
|
|
{
|
|
|
|
GimpThumbnail *thumbnail = NULL;
|
|
|
|
GdkPixbuf *pixbuf = NULL;
|
|
|
|
gchar *uri;
|
|
|
|
|
2019-09-11 21:48:34 +02:00
|
|
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
2006-04-10 12:59:17 +00:00
|
|
|
|
2019-09-11 21:48:34 +02:00
|
|
|
uri = g_file_get_uri (file);
|
2006-04-10 12:59:17 +00:00
|
|
|
|
2019-09-11 21:48:34 +02:00
|
|
|
thumbnail = gimp_thumbnail_new ();
|
|
|
|
gimp_thumbnail_set_uri (thumbnail, uri);
|
2006-04-10 12:59:17 +00:00
|
|
|
|
2019-09-11 21:48:34 +02:00
|
|
|
pixbuf = gimp_thumbnail_load_thumb (thumbnail,
|
|
|
|
(GimpThumbSize) GIMP_THUMBNAIL_SIZE_NORMAL,
|
|
|
|
NULL);
|
2006-04-10 12:59:17 +00:00
|
|
|
|
|
|
|
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
|
2019-09-11 21:48:34 +02:00
|
|
|
file_utils_save_thumbnail (GimpImage *image,
|
|
|
|
GFile *file)
|
2006-04-10 12:59:17 +00:00
|
|
|
{
|
2019-09-11 21:48:34 +02:00
|
|
|
GFile *image_file;
|
2014-07-07 23:56:56 +02:00
|
|
|
gboolean success = FALSE;
|
2006-04-10 12:59:17 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
|
2019-09-11 21:48:34 +02:00
|
|
|
g_return_val_if_fail (G_IS_FILE (file), FALSE);
|
2006-04-10 12:59:17 +00:00
|
|
|
|
2019-09-11 21:48:34 +02:00
|
|
|
image_file = gimp_image_get_file (image);
|
2006-04-10 12:59:17 +00:00
|
|
|
|
2019-09-11 21:48:34 +02:00
|
|
|
if (image_file)
|
2006-04-10 12:59:17 +00:00
|
|
|
{
|
2019-09-11 21:48:34 +02:00
|
|
|
gchar *image_uri = g_file_get_uri (image_file);
|
|
|
|
gchar *uri = g_file_get_uri (file);
|
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;
|
|
|
|
}
|