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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-10-29 19:47:11 +08:00
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
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>
|
|
|
|
|
2002-02-17 23:55:54 +08:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#define W_OK 2
|
|
|
|
#define access(f,p) _access(f,p)
|
|
|
|
#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"
|
2004-08-11 02:47:21 +08:00
|
|
|
#include "core/gimpprogress.h"
|
2001-05-09 10:32:03 +08:00
|
|
|
|
2001-12-01 08:14:14 +08:00
|
|
|
#include "pdb/procedural_db.h"
|
|
|
|
|
|
|
|
#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"
|
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
|
2003-03-05 19:25:59 +08:00
|
|
|
file_save (GimpImage *gimage,
|
2004-04-15 07:37:34 +08:00
|
|
|
GimpContext *context,
|
2004-08-11 02:47:21 +08:00
|
|
|
GimpProgress *progress,
|
2003-03-05 19:25:59 +08:00
|
|
|
GimpRunMode run_mode,
|
|
|
|
GError **error)
|
2003-03-04 18:32:35 +08:00
|
|
|
{
|
|
|
|
const gchar *uri;
|
|
|
|
PlugInProcDef *file_proc;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), 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-05 19:25:59 +08:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL,
|
|
|
|
GIMP_PDB_CALLING_ERROR);
|
2003-03-04 18:32:35 +08:00
|
|
|
|
|
|
|
uri = gimp_object_get_name (GIMP_OBJECT (gimage));
|
|
|
|
|
|
|
|
g_return_val_if_fail (uri != NULL, GIMP_PDB_CALLING_ERROR);
|
|
|
|
|
|
|
|
file_proc = gimp_image_get_save_proc (gimage);
|
|
|
|
|
2004-08-11 02:47:21 +08:00
|
|
|
return file_save_as (gimage, context, progress,
|
|
|
|
uri, uri, file_proc, run_mode,
|
2003-04-09 17:52:01 +08:00
|
|
|
FALSE, TRUE, error);
|
2003-03-04 18:32:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GimpPDBStatusType
|
|
|
|
file_save_as (GimpImage *gimage,
|
2004-04-15 07:37:34 +08:00
|
|
|
GimpContext *context,
|
2004-08-11 02:47:21 +08:00
|
|
|
GimpProgress *progress,
|
2003-03-04 18:32:35 +08:00
|
|
|
const gchar *uri,
|
|
|
|
const gchar *raw_filename,
|
|
|
|
PlugInProcDef *file_proc,
|
|
|
|
GimpRunMode run_mode,
|
2003-03-05 19:25:59 +08:00
|
|
|
gboolean set_uri_and_proc,
|
2003-04-09 17:52:01 +08:00
|
|
|
gboolean set_image_clean,
|
2003-03-05 19:25:59 +08:00
|
|
|
GError **error)
|
2001-03-30 21:31:41 +08:00
|
|
|
{
|
2004-02-03 21:02:18 +08:00
|
|
|
const ProcRecord *proc;
|
2001-10-29 19:47:11 +08:00
|
|
|
Argument *args;
|
|
|
|
Argument *return_vals;
|
|
|
|
GimpPDBStatusType status;
|
|
|
|
gint i;
|
2002-04-14 22:38:55 +08:00
|
|
|
gchar *filename;
|
2001-10-29 19:47:11 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), 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);
|
|
|
|
g_return_val_if_fail (raw_filename != 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
|
|
|
|
|
|
|
if (gimp_image_active_drawable (gimage) == NULL)
|
2001-05-21 21:58:46 +08:00
|
|
|
return GIMP_PDB_EXECUTION_ERROR;
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2003-03-04 18:32:35 +08:00
|
|
|
if (! file_proc)
|
|
|
|
file_proc = file_utils_find_proc (gimage->gimp->save_procs, raw_filename);
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2003-03-04 18:32:35 +08:00
|
|
|
if (! file_proc)
|
|
|
|
{
|
2003-03-05 19:25:59 +08:00
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
_("Unknown file type"));
|
|
|
|
return GIMP_PDB_CALLING_ERROR;
|
2001-03-30 21:31:41 +08:00
|
|
|
}
|
|
|
|
|
2002-04-14 22:38:55 +08:00
|
|
|
filename = g_filename_from_uri (uri, NULL, NULL);
|
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"));
|
2002-04-17 04:25:27 +08:00
|
|
|
g_free (filename);
|
2003-03-05 19:25:59 +08:00
|
|
|
return GIMP_PDB_EXECUTION_ERROR;
|
2002-04-14 22:38:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (access (filename, W_OK) != 0)
|
|
|
|
{
|
2003-03-05 19:25:59 +08:00
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_ACCES,
|
|
|
|
g_strerror (errno));
|
2002-04-17 04:25:27 +08:00
|
|
|
g_free (filename);
|
2003-03-05 19:25:59 +08:00
|
|
|
return GIMP_PDB_EXECUTION_ERROR;
|
2002-04-14 22:38:55 +08:00
|
|
|
}
|
2001-03-30 21:31:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ref the image, so it can't get deleted during save */
|
2003-01-06 06:07:10 +08:00
|
|
|
g_object_ref (gimage);
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2001-12-19 08:13:16 +08:00
|
|
|
proc = plug_in_proc_def_get_proc (file_proc);
|
2001-03-30 21:31:41 +08:00
|
|
|
|
|
|
|
args = g_new0 (Argument, proc->num_args);
|
|
|
|
|
|
|
|
for (i = 0; i < proc->num_args; i++)
|
|
|
|
args[i].arg_type = proc->args[i].arg_type;
|
|
|
|
|
|
|
|
args[0].value.pdb_int = run_mode;
|
2001-04-13 22:50:43 +08:00
|
|
|
args[1].value.pdb_int = gimp_image_get_ID (gimage);
|
2002-02-26 01:58:50 +08:00
|
|
|
args[2].value.pdb_int = gimp_item_get_ID (GIMP_ITEM (gimp_image_active_drawable (gimage)));
|
2002-04-14 22:38:55 +08:00
|
|
|
args[3].value.pdb_pointer = (gpointer) (filename ? filename : uri);
|
2001-07-29 22:09:50 +08:00
|
|
|
args[4].value.pdb_pointer = (gpointer) raw_filename;
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2004-08-11 02:47:21 +08:00
|
|
|
return_vals = procedural_db_execute (gimage->gimp, context, progress,
|
|
|
|
proc->name, args);
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2002-04-14 22:38:55 +08:00
|
|
|
if (filename)
|
|
|
|
g_free (filename);
|
|
|
|
|
2001-03-30 21:31:41 +08:00
|
|
|
status = return_vals[0].value.pdb_int;
|
|
|
|
|
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
|
|
|
|
2003-04-09 17:52:01 +08:00
|
|
|
if (set_image_clean)
|
|
|
|
{
|
|
|
|
/* set this image to clean */
|
|
|
|
gimp_image_clean_all (gimage);
|
|
|
|
}
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2002-05-15 19:05:32 +08:00
|
|
|
documents = GIMP_DOCUMENT_LIST (gimage->gimp->documents);
|
2004-05-14 08:01:11 +08:00
|
|
|
imagefile = gimp_document_list_add_uri (documents,
|
|
|
|
uri, file_proc->mime_type);
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2003-03-04 18:32:35 +08:00
|
|
|
if (set_uri_and_proc)
|
2002-04-20 01:15:39 +08:00
|
|
|
{
|
|
|
|
gimp_image_set_uri (gimage, uri);
|
2003-03-04 18:32:35 +08:00
|
|
|
gimp_image_set_save_proc (gimage, file_proc);
|
2002-04-20 01:15:39 +08:00
|
|
|
}
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2003-03-12 05:04:09 +08:00
|
|
|
/* Write a thumbnail for the saved image */
|
|
|
|
if (set_uri_and_proc)
|
|
|
|
{
|
|
|
|
gimp_imagefile_save_thumbnail (imagefile, gimage);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gchar *saved_uri;
|
2001-12-17 02:31:30 +08:00
|
|
|
|
2003-03-12 05:04:09 +08:00
|
|
|
saved_uri = GIMP_OBJECT (gimage)->name;
|
|
|
|
GIMP_OBJECT (gimage)->name = (gchar *) uri;
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2003-03-12 05:04:09 +08:00
|
|
|
gimp_imagefile_save_thumbnail (imagefile, gimage);
|
2002-04-20 01:15:39 +08:00
|
|
|
|
2003-03-12 05:04:09 +08:00
|
|
|
GIMP_OBJECT (gimage)->name = saved_uri;
|
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
|
|
|
|
|
|
|
g_free (return_vals);
|
|
|
|
g_free (args);
|
|
|
|
|
2003-01-06 06:07:10 +08:00
|
|
|
g_object_unref (gimage);
|
2001-03-30 21:31:41 +08:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|