2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
1997-11-25 06:05:25 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
1997-11-25 06:05:25 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
1997-11-25 06:05:25 +08: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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
2000-05-02 04:22:55 +08:00
|
|
|
|
2000-06-08 01:16:45 +08:00
|
|
|
/* Author: Josh MacDonald. */
|
|
|
|
|
2000-05-02 04:22:55 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2004-11-09 08:06:46 +08:00
|
|
|
#include <string.h>
|
2005-02-08 03:40:52 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2004-11-09 08:06:46 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <unistd.h>
|
2004-11-09 08:06:46 +08:00
|
|
|
#endif
|
2000-05-02 04:22:55 +08:00
|
|
|
|
2005-03-05 03:13:21 +08:00
|
|
|
#include <glib/gstdio.h>
|
|
|
|
|
2000-05-02 04:22:55 +08:00
|
|
|
#include <libgimp/gimp.h>
|
2004-07-17 05:24:39 +08:00
|
|
|
#include <libgimp/gimpui.h>
|
2000-05-02 04:22:55 +08:00
|
|
|
|
2005-02-07 00:11:40 +08:00
|
|
|
#include "uri-backend.h"
|
|
|
|
|
2000-05-02 04:22:55 +08:00
|
|
|
#include "libgimp/stdplugins-intl.h"
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-10-28 01:19:58 +08:00
|
|
|
#define LOAD_PROC "file-uri-load"
|
|
|
|
#define SAVE_PROC "file-uri-save"
|
2008-08-11 18:06:13 +08:00
|
|
|
#define PLUG_IN_BINARY "file-uri"
|
2005-08-12 23:46:46 +08:00
|
|
|
|
|
|
|
|
2005-02-08 03:40:52 +08:00
|
|
|
static void query (void);
|
|
|
|
static void run (const gchar *name,
|
|
|
|
gint nparams,
|
|
|
|
const GimpParam *param,
|
|
|
|
gint *nreturn_vals,
|
|
|
|
GimpParam **return_vals);
|
2000-06-08 01:16:45 +08:00
|
|
|
|
2005-02-08 03:40:52 +08:00
|
|
|
static gint32 load_image (const gchar *uri,
|
2008-08-19 05:18:01 +08:00
|
|
|
GimpRunMode run_mode,
|
|
|
|
GError **error);
|
2005-02-08 03:40:52 +08:00
|
|
|
static GimpPDBStatusType save_image (const gchar *uri,
|
|
|
|
gint32 image_ID,
|
|
|
|
gint32 drawable_ID,
|
2008-08-19 05:18:01 +08:00
|
|
|
gint32 run_mode,
|
|
|
|
GError **error);
|
2005-02-08 03:40:52 +08:00
|
|
|
|
|
|
|
static gchar * get_temp_name (const gchar *uri,
|
|
|
|
gboolean *name_image);
|
|
|
|
static gboolean valid_file (const gchar *filename);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
2006-05-16 20:26:20 +08:00
|
|
|
const GimpPlugInInfo PLUG_IN_INFO =
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-01-26 01:46:56 +08:00
|
|
|
NULL, /* init_proc */
|
|
|
|
NULL, /* quit_proc */
|
|
|
|
query, /* query_proc */
|
|
|
|
run, /* run_proc */
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
MAIN ()
|
|
|
|
|
|
|
|
static void
|
2000-01-26 01:46:56 +08:00
|
|
|
query (void)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2006-05-16 20:26:20 +08:00
|
|
|
static const GimpParamDef load_args[] =
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2009-01-20 04:11:36 +08:00
|
|
|
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
|
2004-08-11 08:34:34 +08:00
|
|
|
{ GIMP_PDB_STRING, "filename", "The name of the file to load" },
|
2005-08-12 23:46:46 +08:00
|
|
|
{ GIMP_PDB_STRING, "raw-filename", "The name entered" }
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
2000-05-02 04:22:55 +08:00
|
|
|
|
2006-05-16 20:26:20 +08:00
|
|
|
static const GimpParamDef load_return_vals[] =
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
{ GIMP_PDB_IMAGE, "image", "Output image" }
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
|
|
|
|
2006-05-16 20:26:20 +08:00
|
|
|
static const GimpParamDef save_args[] =
|
2005-02-08 03:40:52 +08:00
|
|
|
{
|
2009-01-20 04:11:36 +08:00
|
|
|
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
|
2005-02-08 03:40:52 +08:00
|
|
|
{ GIMP_PDB_IMAGE, "image", "Input image" },
|
|
|
|
{ GIMP_PDB_DRAWABLE, "drawable", "Drawable to save" },
|
|
|
|
{ GIMP_PDB_STRING, "filename", "The name of the file to save the image in" },
|
2005-08-12 23:46:46 +08:00
|
|
|
{ GIMP_PDB_STRING, "raw-filename", "The name of the file to save the image in" }
|
2005-02-08 03:40:52 +08:00
|
|
|
};
|
|
|
|
|
2005-02-08 01:03:41 +08:00
|
|
|
GError *error = NULL;
|
|
|
|
|
2005-10-28 01:19:58 +08:00
|
|
|
if (! uri_backend_init (PLUG_IN_BINARY, FALSE, 0, &error))
|
2005-02-08 01:03:41 +08:00
|
|
|
{
|
2008-11-04 22:05:24 +08:00
|
|
|
g_message ("%s", error->message);
|
2005-02-08 01:03:41 +08:00
|
|
|
g_clear_error (&error);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uri_backend_get_load_protocols ())
|
|
|
|
{
|
2005-08-12 23:46:46 +08:00
|
|
|
gimp_install_procedure (LOAD_PROC,
|
2005-02-08 01:03:41 +08:00
|
|
|
"loads files given an URI",
|
2007-07-16 21:12:58 +08:00
|
|
|
uri_backend_get_load_help (),
|
2005-02-08 01:03:41 +08:00
|
|
|
"Spencer Kimball & Peter Mattis",
|
|
|
|
"Spencer Kimball & Peter Mattis",
|
2008-03-12 21:55:27 +08:00
|
|
|
"1995-2008",
|
2005-02-08 01:03:41 +08:00
|
|
|
N_("URI"),
|
|
|
|
NULL,
|
|
|
|
GIMP_PLUGIN,
|
|
|
|
G_N_ELEMENTS (load_args),
|
|
|
|
G_N_ELEMENTS (load_return_vals),
|
|
|
|
load_args, load_return_vals);
|
|
|
|
|
2006-06-17 20:21:47 +08:00
|
|
|
gimp_plugin_icon_register (LOAD_PROC, GIMP_ICON_TYPE_STOCK_ID,
|
|
|
|
(const guint8 *) GIMP_STOCK_WEB);
|
2005-08-12 23:46:46 +08:00
|
|
|
gimp_register_load_handler (LOAD_PROC,
|
2005-02-08 03:40:52 +08:00
|
|
|
"", uri_backend_get_load_protocols ());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uri_backend_get_save_protocols ())
|
|
|
|
{
|
2005-08-12 23:46:46 +08:00
|
|
|
gimp_install_procedure (SAVE_PROC,
|
2005-02-08 03:40:52 +08:00
|
|
|
"saves files given an URI",
|
2007-07-16 21:12:58 +08:00
|
|
|
uri_backend_get_save_help (),
|
2008-03-12 21:55:27 +08:00
|
|
|
"Michael Natterer, Sven Neumann",
|
2005-02-08 03:40:52 +08:00
|
|
|
"Michael Natterer",
|
2008-03-12 21:55:27 +08:00
|
|
|
"2005-2008",
|
2005-02-08 03:40:52 +08:00
|
|
|
N_("URI"),
|
|
|
|
"RGB*, GRAY*, INDEXED*",
|
|
|
|
GIMP_PLUGIN,
|
|
|
|
G_N_ELEMENTS (save_args), 0,
|
|
|
|
save_args, NULL);
|
|
|
|
|
2006-06-17 20:21:47 +08:00
|
|
|
gimp_plugin_icon_register (SAVE_PROC, GIMP_ICON_TYPE_STOCK_ID,
|
|
|
|
(const guint8 *) GIMP_STOCK_WEB);
|
2005-08-12 23:46:46 +08:00
|
|
|
gimp_register_save_handler (SAVE_PROC,
|
2005-02-08 03:40:52 +08:00
|
|
|
"", uri_backend_get_save_protocols ());
|
2005-02-08 01:03:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uri_backend_shutdown ();
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-07-02 02:54:28 +08:00
|
|
|
run (const gchar *name,
|
|
|
|
gint nparams,
|
|
|
|
const GimpParam *param,
|
|
|
|
gint *nreturn_vals,
|
|
|
|
GimpParam **return_vals)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2005-02-08 01:03:41 +08:00
|
|
|
static GimpParam values[2];
|
|
|
|
GimpRunMode run_mode;
|
|
|
|
GimpPDBStatusType status = GIMP_PDB_EXECUTION_ERROR;
|
|
|
|
gint32 image_ID;
|
|
|
|
GError *error = NULL;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
run_mode = param[0].data.d_int32;
|
|
|
|
|
|
|
|
*nreturn_vals = 1;
|
2000-01-26 01:46:56 +08:00
|
|
|
*return_vals = values;
|
2005-02-07 00:11:40 +08:00
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
values[0].type = GIMP_PDB_STATUS;
|
2005-02-07 00:11:40 +08:00
|
|
|
values[0].data.d_status = status;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-10-28 01:19:58 +08:00
|
|
|
if (! uri_backend_init (PLUG_IN_BINARY, TRUE, run_mode, &error))
|
2005-02-08 01:03:41 +08:00
|
|
|
{
|
2008-08-19 05:18:01 +08:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
*nreturn_vals = 2;
|
|
|
|
values[1].type = GIMP_PDB_STRING;
|
|
|
|
values[1].data.d_string = error->message;
|
|
|
|
}
|
2005-02-08 01:03:41 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-19 06:54:26 +08:00
|
|
|
/* We handle PDB errors by forwarding them to the caller in
|
|
|
|
* our return values.
|
|
|
|
*/
|
|
|
|
gimp_plugin_set_pdb_error_handler (GIMP_PDB_ERROR_HANDLER_PLUGIN);
|
|
|
|
|
2005-08-12 23:46:46 +08:00
|
|
|
if (! strcmp (name, LOAD_PROC) && uri_backend_get_load_protocols ())
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2008-08-19 05:18:01 +08:00
|
|
|
image_ID = load_image (param[2].data.d_string, run_mode, &error);
|
2005-02-07 00:11:40 +08:00
|
|
|
|
|
|
|
if (image_ID != -1)
|
|
|
|
{
|
|
|
|
status = GIMP_PDB_SUCCESS;
|
2000-01-26 01:46:56 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
*nreturn_vals = 2;
|
2000-08-22 09:26:57 +08:00
|
|
|
values[1].type = GIMP_PDB_IMAGE;
|
1997-11-25 06:05:25 +08:00
|
|
|
values[1].data.d_image = image_ID;
|
|
|
|
}
|
|
|
|
}
|
2005-08-12 23:46:46 +08:00
|
|
|
else if (! strcmp (name, SAVE_PROC) && uri_backend_get_save_protocols ())
|
2005-02-08 03:40:52 +08:00
|
|
|
{
|
|
|
|
status = save_image (param[3].data.d_string,
|
|
|
|
param[1].data.d_int32,
|
|
|
|
param[2].data.d_int32,
|
2008-08-19 05:18:01 +08:00
|
|
|
run_mode, &error);
|
2005-02-08 03:40:52 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
else
|
2000-01-26 01:46:56 +08:00
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
status = GIMP_PDB_CALLING_ERROR;
|
2000-01-26 01:46:56 +08:00
|
|
|
}
|
|
|
|
|
2005-02-08 01:03:41 +08:00
|
|
|
uri_backend_shutdown ();
|
|
|
|
|
2008-08-19 05:18:01 +08:00
|
|
|
if (status != GIMP_PDB_SUCCESS && error)
|
|
|
|
{
|
|
|
|
*nreturn_vals = 2;
|
|
|
|
values[1].type = GIMP_PDB_STRING;
|
|
|
|
values[1].data.d_string = error->message;
|
|
|
|
}
|
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
values[0].data.d_status = status;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static gint32
|
2008-08-19 05:18:01 +08:00
|
|
|
load_image (const gchar *uri,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
GError **error)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2005-02-07 00:11:40 +08:00
|
|
|
gint32 image_ID = -1;
|
2004-08-11 08:34:34 +08:00
|
|
|
gboolean name_image = FALSE;
|
2010-04-02 03:06:09 +08:00
|
|
|
gchar *tmpname;
|
|
|
|
gboolean mapped = FALSE;
|
2004-06-21 08:48:44 +08:00
|
|
|
|
2009-04-21 23:48:17 +08:00
|
|
|
tmpname = uri_backend_map_image (uri, run_mode);
|
2004-08-11 08:34:34 +08:00
|
|
|
|
2009-04-21 23:48:17 +08:00
|
|
|
if (tmpname)
|
2005-02-07 00:11:40 +08:00
|
|
|
{
|
2009-04-21 23:48:17 +08:00
|
|
|
mapped = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmpname = get_temp_name (uri, &name_image);
|
2004-08-11 08:34:34 +08:00
|
|
|
|
2009-04-21 23:48:17 +08:00
|
|
|
if (! uri_backend_load_image (uri, tmpname, run_mode, error))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
image_ID = gimp_file_load (run_mode, tmpname, tmpname);
|
|
|
|
|
|
|
|
if (image_ID != -1)
|
|
|
|
{
|
|
|
|
if (mapped || name_image)
|
|
|
|
gimp_image_set_filename (image_ID, uri);
|
2008-08-19 05:18:01 +08:00
|
|
|
else
|
2009-04-21 23:48:17 +08:00
|
|
|
gimp_image_set_filename (image_ID, "");
|
2005-02-07 00:11:40 +08:00
|
|
|
}
|
2009-04-21 23:48:17 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
"%s", gimp_get_pdb_error ());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! mapped)
|
|
|
|
g_unlink (tmpname);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1999-10-28 06:16:20 +08:00
|
|
|
g_free (tmpname);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-08-25 23:38:38 +08:00
|
|
|
return image_ID;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2005-02-08 03:40:52 +08:00
|
|
|
|
|
|
|
static GimpPDBStatusType
|
2008-08-19 05:18:01 +08:00
|
|
|
save_image (const gchar *uri,
|
|
|
|
gint32 image_ID,
|
|
|
|
gint32 drawable_ID,
|
|
|
|
gint32 run_mode,
|
|
|
|
GError **error)
|
2005-02-08 03:40:52 +08:00
|
|
|
{
|
2008-08-19 05:18:01 +08:00
|
|
|
GimpPDBStatusType status = GIMP_PDB_EXECUTION_ERROR;
|
|
|
|
gchar *tmpname;
|
2010-04-02 03:06:09 +08:00
|
|
|
gboolean mapped = FALSE;
|
2005-02-08 03:40:52 +08:00
|
|
|
|
2009-04-21 23:48:17 +08:00
|
|
|
tmpname = uri_backend_map_image (uri, run_mode);
|
|
|
|
|
|
|
|
if (tmpname)
|
|
|
|
mapped = TRUE;
|
|
|
|
else
|
|
|
|
tmpname = get_temp_name (uri, NULL);
|
2005-02-08 03:40:52 +08:00
|
|
|
|
2008-08-19 05:18:01 +08:00
|
|
|
if (gimp_file_save (run_mode,
|
|
|
|
image_ID,
|
|
|
|
drawable_ID,
|
|
|
|
tmpname,
|
|
|
|
tmpname) && valid_file (tmpname))
|
2005-02-08 03:40:52 +08:00
|
|
|
{
|
2009-04-21 23:48:17 +08:00
|
|
|
if (mapped || uri_backend_save_image (uri, tmpname, run_mode, error))
|
2008-08-19 05:18:01 +08:00
|
|
|
{
|
|
|
|
status = GIMP_PDB_SUCCESS;
|
|
|
|
}
|
2005-02-08 03:40:52 +08:00
|
|
|
}
|
2008-08-19 05:18:01 +08:00
|
|
|
else
|
2005-02-08 03:40:52 +08:00
|
|
|
{
|
2008-08-19 05:18:01 +08:00
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
"%s", gimp_get_pdb_error ());
|
2005-02-08 03:40:52 +08:00
|
|
|
}
|
|
|
|
|
2009-04-21 23:48:17 +08:00
|
|
|
if (! mapped)
|
|
|
|
g_unlink (tmpname);
|
|
|
|
|
2005-02-08 03:40:52 +08:00
|
|
|
g_free (tmpname);
|
|
|
|
|
2008-08-19 05:18:01 +08:00
|
|
|
return status;
|
2005-02-08 03:40:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *
|
|
|
|
get_temp_name (const gchar *uri,
|
|
|
|
gboolean *name_image)
|
|
|
|
{
|
|
|
|
gchar *basename;
|
|
|
|
gchar *tmpname = NULL;
|
|
|
|
|
|
|
|
if (name_image)
|
|
|
|
*name_image = FALSE;
|
|
|
|
|
|
|
|
basename = g_path_get_basename (uri);
|
|
|
|
|
|
|
|
if (basename)
|
|
|
|
{
|
|
|
|
gchar *ext = strchr (basename, '.');
|
|
|
|
|
|
|
|
if (ext && strlen (ext))
|
|
|
|
{
|
|
|
|
tmpname = gimp_temp_name (ext + 1);
|
|
|
|
|
|
|
|
if (name_image)
|
|
|
|
*name_image = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (basename);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! tmpname)
|
|
|
|
tmpname = gimp_temp_name ("xxx");
|
|
|
|
|
|
|
|
return tmpname;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
valid_file (const gchar *filename)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
|
2005-03-05 03:13:21 +08:00
|
|
|
return g_stat (filename, &buf) == 0 && buf.st_size > 0;
|
2005-02-08 03:40:52 +08:00
|
|
|
}
|