2001-03-30 21:31:41 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
|
|
|
* 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
|
|
|
|
|
2001-08-17 22:27:31 +08:00
|
|
|
#include <glib-object.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-05 01:47:22 +08:00
|
|
|
#include "pdb/gimp-pdb.h"
|
2006-03-31 17:15:08 +08:00
|
|
|
#include "pdb/gimpprocedure.h"
|
2001-12-01 08:14:14 +08:00
|
|
|
|
|
|
|
#include "plug-in/plug-in.h"
|
2004-09-22 23:12:24 +08:00
|
|
|
#include "plug-in/plug-in-proc-def.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"
|
2004-09-28 20:40:34 +08:00
|
|
|
#include "gimprecentlist.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
|
2006-03-29 01:08:36 +08:00
|
|
|
file_save (GimpImage *image,
|
2005-02-14 02:05:43 +08:00
|
|
|
GimpContext *context,
|
|
|
|
GimpProgress *progress,
|
|
|
|
const gchar *uri,
|
|
|
|
PlugInProcDef *file_proc,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
gboolean save_a_copy,
|
|
|
|
GError **error)
|
2001-03-30 21:31:41 +08:00
|
|
|
{
|
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;
|
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);
|
2005-02-14 02:05:43 +08:00
|
|
|
g_return_val_if_fail (file_proc != NULL, 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
|
|
|
|
2006-03-29 01:08:36 +08:00
|
|
|
if (! gimp_image_active_drawable (image))
|
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-05 01:47:22 +08:00
|
|
|
return_vals = gimp_pdb_run_proc (image->gimp, context, progress,
|
|
|
|
file_proc->procedure->name,
|
|
|
|
GIMP_TYPE_INT32, run_mode,
|
|
|
|
GIMP_TYPE_IMAGE_ID, gimp_image_get_ID (image),
|
|
|
|
GIMP_TYPE_DRAWABLE_ID, gimp_item_get_ID (GIMP_ITEM (gimp_image_active_drawable (image))),
|
|
|
|
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
|
2002-04-20 01:15:39 +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-03-29 01:08:36 +08:00
|
|
|
gimp_image_set_uri (image, uri);
|
|
|
|
gimp_image_set_save_proc (image, file_proc);
|
|
|
|
gimp_image_clean_all (image);
|
2002-04-20 01:15:39 +08:00
|
|
|
}
|
2001-03-30 21:31:41 +08:00
|
|
|
|
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-03-29 01:08:36 +08:00
|
|
|
gimp_imagefile_save_thumbnail (imagefile, file_proc->mime_type, image);
|
2004-09-28 20:40:34 +08:00
|
|
|
|
2006-03-29 01:08:36 +08:00
|
|
|
if (image->gimp->config->save_document_history)
|
2005-11-01 04:57:03 +08:00
|
|
|
gimp_recent_list_add_uri (uri, file_proc->mime_type);
|
2001-03-30 21:31:41 +08:00
|
|
|
}
|
2003-03-05 19:25:59 +08:00
|
|
|
else if (status != GIMP_PDB_CANCEL)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
_("Plug-In could not save image"));
|
|
|
|
}
|
2001-03-30 21:31:41 +08:00
|
|
|
|
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;
|
|
|
|
}
|