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"
|
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
|
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,
|
2004-11-16 21:37:36 +08:00
|
|
|
uri, uri, file_proc, run_mode, FALSE, 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,
|
2004-11-16 21:37:36 +08:00
|
|
|
gboolean save_a_copy,
|
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;
|
2005-02-11 07:47:30 +08:00
|
|
|
gchar *filename = NULL;
|
2005-02-10 22:32:14 +08:00
|
|
|
gchar *raw_filename_with_ext;
|
|
|
|
gchar *uri_with_ext;
|
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
|
|
|
|
2005-02-10 22:32:14 +08:00
|
|
|
if (! gimp_image_active_drawable (gimage))
|
2001-05-21 21:58:46 +08:00
|
|
|
return GIMP_PDB_EXECUTION_ERROR;
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2005-02-10 22:32:14 +08:00
|
|
|
if (strchr (raw_filename, '.'))
|
|
|
|
{
|
|
|
|
raw_filename_with_ext = g_strdup (raw_filename);
|
|
|
|
uri_with_ext = g_strdup (uri);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
raw_filename_with_ext = g_strconcat (raw_filename, ".xcf", NULL);
|
|
|
|
uri_with_ext = g_strconcat (uri, ".xcf", NULL);
|
|
|
|
}
|
|
|
|
|
2003-03-04 18:32:35 +08:00
|
|
|
if (! file_proc)
|
2005-02-10 22:32:14 +08:00
|
|
|
file_proc = file_utils_find_proc (gimage->gimp->save_procs,
|
|
|
|
raw_filename_with_ext);
|
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"));
|
2005-02-10 22:32:14 +08:00
|
|
|
status = GIMP_PDB_CALLING_ERROR;
|
|
|
|
goto out;
|
2001-03-30 21:31:41 +08:00
|
|
|
}
|
|
|
|
|
2005-02-10 22:32:14 +08:00
|
|
|
filename = file_utils_filename_from_uri (uri_with_ext);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2005-02-10 22:32:14 +08:00
|
|
|
args[0].value.pdb_int = run_mode;
|
|
|
|
args[1].value.pdb_int = gimp_image_get_ID (gimage);
|
|
|
|
args[2].value.pdb_int =
|
|
|
|
gimp_item_get_ID (GIMP_ITEM (gimp_image_active_drawable (gimage)));
|
|
|
|
args[3].value.pdb_pointer = (filename ? filename : uri_with_ext);
|
|
|
|
args[4].value.pdb_pointer = raw_filename_with_ext;
|
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
|
|
|
|
|
|
|
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
|
|
|
|
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 */
|
2005-02-10 22:32:14 +08:00
|
|
|
g_object_set_data_full (G_OBJECT (gimage), "gimp-image-save-a-copy",
|
|
|
|
g_strdup (uri_with_ext),
|
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 */
|
2005-02-10 22:32:14 +08:00
|
|
|
if (uri_with_ext &&
|
|
|
|
strcmp (uri_with_ext, gimp_image_get_uri (gimage)))
|
2004-11-29 21:50:02 +08:00
|
|
|
g_object_set_data (G_OBJECT (gimage),
|
|
|
|
"gimp-image-save-a-copy", NULL);
|
|
|
|
|
2005-02-10 22:32:14 +08:00
|
|
|
gimp_image_set_uri (gimage, uri_with_ext);
|
2003-03-04 18:32:35 +08:00
|
|
|
gimp_image_set_save_proc (gimage, file_proc);
|
2004-11-16 21:37:36 +08:00
|
|
|
gimp_image_clean_all (gimage);
|
2002-04-20 01:15:39 +08:00
|
|
|
}
|
2001-03-30 21:31:41 +08:00
|
|
|
|
2004-11-16 21:37:36 +08:00
|
|
|
documents = GIMP_DOCUMENT_LIST (gimage->gimp->documents);
|
|
|
|
imagefile = gimp_document_list_add_uri (documents,
|
2005-02-10 22:32:14 +08:00
|
|
|
uri_with_ext,
|
|
|
|
file_proc->mime_type);
|
2004-11-16 21:37:36 +08:00
|
|
|
|
2004-11-14 01:13:21 +08:00
|
|
|
gimp_imagefile_save_thumbnail (imagefile, file_proc->mime_type, gimage);
|
2004-09-28 20:40:34 +08:00
|
|
|
|
2005-02-10 22:32:14 +08:00
|
|
|
gimp_recent_list_add_uri (uri_with_ext, 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
|
|
|
|
|
|
|
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
|
|
|
|
2005-02-10 22:32:14 +08:00
|
|
|
out:
|
|
|
|
g_free (filename);
|
|
|
|
g_free (raw_filename_with_ext);
|
|
|
|
g_free (uri_with_ext);
|
|
|
|
|
2001-03-30 21:31:41 +08:00
|
|
|
return status;
|
|
|
|
}
|
2005-02-10 22:32:14 +08:00
|
|
|
|