2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2001-03-30 21:31:41 +08:00
|
|
|
* Copyright (C) 1995, 1996, 1997 Spencer Kimball and Peter Mattis
|
|
|
|
* Copyright (C) 1997 Josh MacDonald
|
|
|
|
*
|
2004-09-22 23:12:24 +08:00
|
|
|
* file-save.c
|
|
|
|
*
|
2001-03-30 21:31:41 +08:00
|
|
|
* 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"
|
|
|
|
|
2001-10-29 19:47:11 +08:00
|
|
|
#include <errno.h>
|
2001-03-30 21:31:41 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2005-09-26 07:22:05 +08:00
|
|
|
|
2001-10-29 19:47:11 +08:00
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
2005-09-26 07:22:05 +08:00
|
|
|
|
2001-03-30 21:31:41 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2008-10-10 04:24:04 +08:00
|
|
|
#include <gegl.h>
|
2005-09-26 07:22:05 +08:00
|
|
|
#include <glib/gstdio.h>
|
2001-08-17 22:27:31 +08:00
|
|
|
|
2002-02-17 23:55:54 +08:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#define W_OK 2
|
|
|
|
#endif
|
|
|
|
|
2001-05-10 06:34:59 +08:00
|
|
|
#include "core/core-types.h"
|
2001-04-18 05:43:29 +08:00
|
|
|
|
2002-11-19 04:50:31 +08:00
|
|
|
#include "config/gimpcoreconfig.h"
|
|
|
|
|
2001-07-11 20:39:49 +08:00
|
|
|
#include "core/gimp.h"
|
2004-04-15 07:37:34 +08:00
|
|
|
#include "core/gimpcontext.h"
|
2002-05-15 19:05:32 +08:00
|
|
|
#include "core/gimpdocumentlist.h"
|
2001-05-09 10:32:03 +08:00
|
|
|
#include "core/gimpdrawable.h"
|
|
|
|
#include "core/gimpimage.h"
|
2002-04-20 01:15:39 +08:00
|
|
|
#include "core/gimpimagefile.h"
|
2006-04-04 04:54:55 +08:00
|
|
|
#include "core/gimpparamspecs.h"
|
2004-08-11 02:47:21 +08:00
|
|
|
#include "core/gimpprogress.h"
|
2001-05-09 10:32:03 +08:00
|
|
|
|
2006-04-26 17:13:47 +08:00
|
|
|
#include "pdb/gimppdb.h"
|
2006-08-06 05:21:01 +08:00
|
|
|
|
|
|
|
#include "plug-in/gimppluginprocedure.h"
|
2001-12-01 08:14:14 +08:00
|
|
|
|
2001-03-30 21:31:41 +08:00
|
|
|
#include "file-save.h"
|
|
|
|
#include "file-utils.h"
|
2001-10-25 21:30:01 +08:00
|
|
|
|
2003-03-26 00:38:19 +08:00
|
|
|
#include "gimp-intl.h"
|
2001-03-30 21:31:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* public functions */
|
|
|
|
|
2001-05-21 21:58:46 +08:00
|
|
|
GimpPDBStatusType
|
2007-09-13 22:48:32 +08:00
|
|
|
file_save (GimpImage *image,
|
2006-04-05 16:38:33 +08:00
|
|
|
GimpContext *context,
|
|
|
|
GimpProgress *progress,
|
|
|
|
const gchar *uri,
|
|
|
|
GimpPlugInProcedure *file_proc,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
gboolean save_a_copy,
|
|
|
|
GError **error)
|
2001-03-30 21:31:41 +08:00
|
|
|
{
|
2006-04-26 17:13:47 +08:00
|
|
|
GimpDrawable *drawable;
|
2006-04-04 18:30:58 +08:00
|
|
|
GValueArray *return_vals;
|
2001-10-29 19:47:11 +08:00
|
|
|
GimpPDBStatusType status;
|
2006-03-27 19:40:00 +08:00
|
|
|
gchar *filename;
|
2006-04-26 17:13:47 +08:00
|
|
|
gint32 image_ID;
|
|
|
|
gint32 drawable_ID;
|
2001-10-29 19:47:11 +08:00
|
|
|
|
2006-03-29 01:08:36 +08:00
|
|
|
g_return_val_if_fail (GIMP_IS_IMAGE (image), GIMP_PDB_CALLING_ERROR);
|
2004-04-15 07:37:34 +08:00
|
|
|
g_return_val_if_fail (GIMP_IS_CONTEXT (context), GIMP_PDB_CALLING_ERROR);
|
2004-08-11 02:47:21 +08:00
|
|
|
g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress),
|
|
|
|
GIMP_PDB_CALLING_ERROR);
|
2003-03-04 18:32:35 +08:00
|
|
|
g_return_val_if_fail (uri != NULL, GIMP_PDB_CALLING_ERROR);
|
2006-04-05 16:38:33 +08:00
|
|
|
g_return_val_if_fail (GIMP_IS_PLUG_IN_PROCEDURE (file_proc),
|
|
|
|
GIMP_PDB_CALLING_ERROR);
|
2003-03-05 19:25:59 +08:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL,
|
|
|
|
GIMP_PDB_CALLING_ERROR);
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2007-07-19 22:59:51 +08:00
|
|
|
drawable = gimp_image_get_active_drawable (image);
|
2006-04-26 17:13:47 +08:00
|
|
|
|
|
|
|
if (! drawable)
|
2001-05-21 21:58:46 +08:00
|
|
|
return GIMP_PDB_EXECUTION_ERROR;
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2005-02-14 02:05:43 +08:00
|
|
|
filename = file_utils_filename_from_uri (uri);
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2002-04-14 22:38:55 +08:00
|
|
|
if (filename)
|
|
|
|
{
|
|
|
|
/* check if we are saving to a file */
|
2002-04-17 04:25:27 +08:00
|
|
|
if (g_file_test (filename, G_FILE_TEST_EXISTS))
|
2001-03-30 21:31:41 +08:00
|
|
|
{
|
2002-04-17 04:25:27 +08:00
|
|
|
if (! g_file_test (filename, G_FILE_TEST_IS_REGULAR))
|
2002-04-14 22:38:55 +08:00
|
|
|
{
|
2003-03-05 19:25:59 +08:00
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
_("Not a regular file"));
|
2005-02-10 22:32:14 +08:00
|
|
|
status = GIMP_PDB_EXECUTION_ERROR;
|
|
|
|
goto out;
|
2002-04-14 22:38:55 +08:00
|
|
|
}
|
|
|
|
|
2005-09-26 07:22:05 +08:00
|
|
|
if (g_access (filename, W_OK) != 0)
|
2002-04-14 22:38:55 +08:00
|
|
|
{
|
2003-03-05 19:25:59 +08:00
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_ACCES,
|
|
|
|
g_strerror (errno));
|
2005-02-10 22:32:14 +08:00
|
|
|
status = GIMP_PDB_EXECUTION_ERROR;
|
|
|
|
goto out;
|
2002-04-14 22:38:55 +08:00
|
|
|
}
|
2001-03-30 21:31:41 +08:00
|
|
|
}
|
|
|
|
}
|
2006-03-27 19:40:00 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
filename = g_strdup (uri);
|
|
|
|
}
|
2001-03-30 21:31:41 +08:00
|
|
|
|
|
|
|
/* ref the image, so it can't get deleted during save */
|
2006-03-29 01:08:36 +08:00
|
|
|
g_object_ref (image);
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2006-04-26 17:13:47 +08:00
|
|
|
image_ID = gimp_image_get_ID (image);
|
|
|
|
drawable_ID = gimp_item_get_ID (GIMP_ITEM (drawable));
|
|
|
|
|
|
|
|
return_vals =
|
|
|
|
gimp_pdb_execute_procedure_by_name (image->gimp->pdb,
|
2007-12-03 02:05:54 +08:00
|
|
|
context, progress, error,
|
2006-04-26 17:13:47 +08:00
|
|
|
GIMP_OBJECT (file_proc)->name,
|
|
|
|
GIMP_TYPE_INT32, run_mode,
|
|
|
|
GIMP_TYPE_IMAGE_ID, image_ID,
|
|
|
|
GIMP_TYPE_DRAWABLE_ID, drawable_ID,
|
|
|
|
G_TYPE_STRING, filename,
|
|
|
|
G_TYPE_STRING, uri,
|
|
|
|
G_TYPE_NONE);
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2006-04-04 18:30:58 +08:00
|
|
|
status = g_value_get_enum (&return_vals->values[0]);
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2006-04-04 18:30:58 +08:00
|
|
|
g_value_array_free (return_vals);
|
2006-03-27 19:40:00 +08:00
|
|
|
|
2001-05-21 21:58:46 +08:00
|
|
|
if (status == GIMP_PDB_SUCCESS)
|
2001-03-30 21:31:41 +08:00
|
|
|
{
|
2002-05-15 19:05:32 +08:00
|
|
|
GimpDocumentList *documents;
|
|
|
|
GimpImagefile *imagefile;
|
2002-04-20 01:15:39 +08:00
|
|
|
|
2004-11-16 21:37:36 +08:00
|
|
|
if (save_a_copy)
|
2003-04-09 17:52:01 +08:00
|
|
|
{
|
2004-11-29 21:50:02 +08:00
|
|
|
/* remember the "save-a-copy" filename for the next invocation */
|
2006-03-29 01:08:36 +08:00
|
|
|
g_object_set_data_full (G_OBJECT (image), "gimp-image-save-a-copy",
|
2005-02-14 02:05:43 +08:00
|
|
|
g_strdup (uri),
|
2004-11-29 21:50:02 +08:00
|
|
|
(GDestroyNotify) g_free);
|
2003-04-09 17:52:01 +08:00
|
|
|
}
|
2004-11-16 21:37:36 +08:00
|
|
|
else
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
2004-11-29 21:50:02 +08:00
|
|
|
/* reset the "save-a-copy" filename when the image URI changes */
|
2006-03-29 01:08:36 +08:00
|
|
|
if (strcmp (uri, gimp_image_get_uri (image)))
|
|
|
|
g_object_set_data (G_OBJECT (image),
|
2004-11-29 21:50:02 +08:00
|
|
|
"gimp-image-save-a-copy", NULL);
|
|
|
|
|
2006-04-12 20:49:29 +08:00
|
|
|
gimp_image_set_uri (image, uri);
|
2006-03-29 01:08:36 +08:00
|
|
|
gimp_image_set_save_proc (image, file_proc);
|
2007-09-21 05:23:05 +08:00
|
|
|
|
2006-03-29 01:08:36 +08:00
|
|
|
gimp_image_clean_all (image);
|
2006-04-12 20:49:29 +08:00
|
|
|
}
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2006-12-12 04:57:44 +08:00
|
|
|
gimp_image_saved (image, uri);
|
|
|
|
|
2006-03-29 01:08:36 +08:00
|
|
|
documents = GIMP_DOCUMENT_LIST (image->gimp->documents);
|
2004-11-16 21:37:36 +08:00
|
|
|
imagefile = gimp_document_list_add_uri (documents,
|
2005-02-14 02:05:43 +08:00
|
|
|
uri,
|
2005-02-10 22:32:14 +08:00
|
|
|
file_proc->mime_type);
|
2004-11-16 21:37:36 +08:00
|
|
|
|
2006-11-06 16:12:37 +08:00
|
|
|
/* only save a thumbnail if we are saving as XCF, see bug #25272 */
|
|
|
|
if (GIMP_PROCEDURE (file_proc)->proc_type == GIMP_INTERNAL)
|
|
|
|
gimp_imagefile_save_thumbnail (imagefile, file_proc->mime_type, image);
|
2001-03-30 21:31:41 +08:00
|
|
|
}
|
2003-03-05 19:25:59 +08:00
|
|
|
else if (status != GIMP_PDB_CANCEL)
|
|
|
|
{
|
2007-12-03 02:05:54 +08:00
|
|
|
if (error && *error == NULL)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
_("%s plug-in could not save image"),
|
|
|
|
gimp_plug_in_procedure_get_label (file_proc));
|
|
|
|
}
|
2003-03-05 19:25:59 +08:00
|
|
|
}
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2008-08-20 14:30:59 +08:00
|
|
|
gimp_image_flush (image);
|
|
|
|
|
2006-03-29 01:08:36 +08:00
|
|
|
g_object_unref (image);
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2005-02-10 22:32:14 +08:00
|
|
|
out:
|
|
|
|
g_free (filename);
|
|
|
|
|
2001-03-30 21:31:41 +08:00
|
|
|
return status;
|
|
|
|
}
|