added backend methods for saving to URIs.

2005-02-07  Michael Natterer  <mitch@gimp.org>

	* plug-ins/uri/uri-backend.h: added backend methods for saving to
	URIs.

	* plug-ins/uri/uri.c: register a save procecure if the save method
	is available, using some code from compressor.c

	* plug-ins/uri/uri-backend-gnomevfs.c: implement saving.

	* plug-ins/uri/uri-backend-wget.c: added saving stubs which always
	fail.
This commit is contained in:
Michael Natterer 2005-02-07 19:40:52 +00:00 committed by Michael Natterer
parent d4535cc31f
commit 59633d6528
5 changed files with 263 additions and 80 deletions

View file

@ -1,3 +1,16 @@
2005-02-07 Michael Natterer <mitch@gimp.org>
* plug-ins/uri/uri-backend.h: added backend methods for saving to
URIs.
* plug-ins/uri/uri.c: register a save procecure if the save method
is available, using some code from compressor.c
* plug-ins/uri/uri-backend-gnomevfs.c: implement saving.
* plug-ins/uri/uri-backend-wget.c: added saving stubs which always
fail.
2005-02-07 Sven Neumann <sven@gimp.org>
* app/widgets/gimpfiledialog.c (gimp_file_dialog_add_filters): add

View file

@ -30,9 +30,12 @@
/* local function prototypes */
static gboolean copy_uri (const gchar *src_uri,
const gchar *dest_uri,
GError **error);
static gchar * get_protocols (void);
static gboolean copy_uri (const gchar *src_uri,
const gchar *dest_uri,
const gchar *copying_format_str,
const gchar *copied_format_str,
GError **error);
/* private variables */
@ -64,48 +67,18 @@ const gchar *
uri_backend_get_load_protocols (void)
{
if (! supported_protocols)
{
static const gchar *protocols[] =
{
"http:",
"https:",
"ftp:",
"sftp:",
"ssh:",
"smb:",
"dav:",
"davs:"
};
supported_protocols = get_protocols ();
GString *string = g_string_new (NULL);
gint i;
return supported_protocols;
}
for (i = 0; i < G_N_ELEMENTS (protocols); i++)
{
gchar *uri;
GnomeVFSURI *vfs_uri;
const gchar *
uri_backend_get_save_protocols (void)
{
if (! supported_protocols)
supported_protocols = get_protocols ();
uri = g_strdup_printf ("%s//foo/bar.xcf", protocols[i]);
vfs_uri = gnome_vfs_uri_new (uri);
if (vfs_uri)
{
if (string->len > 0)
g_string_append_c (string, ',');
g_string_append (string, protocols[i]);
gnome_vfs_uri_unref (vfs_uri);
}
g_free (uri);
}
supported_protocols = g_string_free (string, FALSE);
}
return "http:,https:,ftp:,sftp:,ssh:,smb:,dav:,davs:";
return supported_protocols;
}
gboolean
@ -118,18 +91,84 @@ uri_backend_load_image (const gchar *uri,
gboolean success;
dest_uri = g_filename_to_uri (tmpname, NULL, NULL);
success = copy_uri (uri, dest_uri, error);
success = copy_uri (uri, dest_uri,
_("Downloading %llu bytes of image data..."),
_("Downloaded %llu bytes of image data"),
error);
g_free (dest_uri);
return success;
}
gboolean
uri_backend_save_image (const gchar *uri,
const gchar *tmpname,
GimpRunMode run_mode,
GError **error)
{
gchar *src_uri;
gboolean success;
src_uri = g_filename_to_uri (tmpname, NULL, NULL);
success = copy_uri (src_uri, uri,
_("Uploading %llu bytes of image data..."),
_("Uploaded %llu bytes of image data"),
error);
g_free (src_uri);
return success;
}
/* private functions */
static gchar *
get_protocols (void)
{
static const gchar *protocols[] =
{
"http:",
"https:",
"ftp:",
"sftp:",
"ssh:",
"smb:",
"dav:",
"davs:"
};
GString *string = g_string_new (NULL);
gint i;
for (i = 0; i < G_N_ELEMENTS (protocols); i++)
{
gchar *uri;
GnomeVFSURI *vfs_uri;
uri = g_strdup_printf ("%s//foo/bar.xcf", protocols[i]);
vfs_uri = gnome_vfs_uri_new (uri);
if (vfs_uri)
{
if (string->len > 0)
g_string_append_c (string, ',');
g_string_append (string, protocols[i]);
gnome_vfs_uri_unref (vfs_uri);
}
g_free (uri);
}
return g_string_free (string, FALSE);
}
static gboolean
copy_uri (const gchar *src_uri,
const gchar *dest_uri,
const gchar *copying_format_str,
const gchar *copied_format_str,
GError **error)
{
GnomeVFSHandle *read_handle;
@ -166,7 +205,7 @@ copy_uri (const gchar *src_uri,
}
result = gnome_vfs_create (&write_handle, dest_uri,
GNOME_VFS_OPEN_WRITE, FALSE, 0777);
GNOME_VFS_OPEN_WRITE, FALSE, 0644);
if (result != GNOME_VFS_OK)
{
@ -178,11 +217,9 @@ copy_uri (const gchar *src_uri,
}
if (file_size > 0)
message = g_strdup_printf (_("Downloading %llu bytes of image data..."),
file_size);
message = g_strdup_printf (copying_format_str, file_size);
else
message = g_strdup_printf (_("Downloaded %llu bytes of image data"),
(GnomeVFSFileSize) 0);
message = g_strdup_printf (copied_format_str, (GnomeVFSFileSize) 0);
gimp_progress_init (message);
g_free (message);
@ -222,8 +259,7 @@ copy_uri (const gchar *src_uri,
}
else
{
message = g_strdup_printf (_("Downloaded %llu bytes of image data"),
bytes_read);
message = g_strdup_printf (copied_format_str, bytes_read);
gimp_progress_init (message);
g_free (message);
}

View file

@ -58,6 +58,12 @@ uri_backend_get_load_protocols (void)
return "http:,https:,ftp:";
}
const gchar *
uri_backend_get_save_protocols (void)
{
return NULL;
}
gboolean
uri_backend_load_image (const gchar *uri,
const gchar *tmpname,
@ -278,3 +284,14 @@ uri_backend_load_image (const gchar *uri,
return TRUE;
}
gboolean
uri_backend_save_image (const gchar *uri,
const gchar *tmpname,
GimpRunMode run_mode,
GError **error)
{
g_set_error (error, 0, 0, "EEK");
return FALSE;
}

View file

@ -24,10 +24,16 @@ gboolean uri_backend_init (GError **error);
void uri_backend_shutdown (void);
const gchar * uri_backend_get_load_protocols (void);
const gchar * uri_backend_get_save_protocols (void);
gboolean uri_backend_load_image (const gchar *uri,
const gchar *tmpname,
GimpRunMode run_mode,
GError **error);
gboolean uri_backend_save_image (const gchar *uri,
const gchar *tmpname,
GimpRunMode run_mode,
GError **error);
#endif /* __URI_BACKEND_H__ */

View file

@ -21,6 +21,8 @@
#include "config.h"
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
@ -34,15 +36,23 @@
#include "libgimp/stdplugins-intl.h"
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static gint32 load_image (const gchar *uri,
GimpRunMode run_mode);
static gint32 load_image (const gchar *uri,
GimpRunMode run_mode);
static GimpPDBStatusType save_image (const gchar *uri,
gint32 image_ID,
gint32 drawable_ID,
gint32 run_mode);
static gchar * get_temp_name (const gchar *uri,
gboolean *name_image);
static gboolean valid_file (const gchar *filename);
GimpPlugInInfo PLUG_IN_INFO =
@ -70,6 +80,15 @@ query (void)
{ GIMP_PDB_IMAGE, "image", "Output image" }
};
static GimpParamDef save_args[] =
{
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
{ 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" },
{ GIMP_PDB_STRING, "raw_filename", "The name of the file to save the image in" }
};
GError *error = NULL;
if (! uri_backend_init (&error))
@ -84,7 +103,7 @@ query (void)
{
gimp_install_procedure ("file_uri_load",
"loads files given an URI",
"You need to have GNU Wget installed.",
"You need to have GNU Wget or GnomeVFS installed.",
"Spencer Kimball & Peter Mattis",
"Spencer Kimball & Peter Mattis",
"1995-1997",
@ -98,8 +117,27 @@ query (void)
gimp_plugin_icon_register ("file_uri_load",
GIMP_ICON_TYPE_STOCK_ID, GIMP_STOCK_WEB);
gimp_register_load_handler ("file_uri_load",
"",
uri_backend_get_load_protocols ());
"", uri_backend_get_load_protocols ());
}
if (uri_backend_get_save_protocols ())
{
gimp_install_procedure ("file_uri_save",
"saves files given an URI",
"You need to have GNU Wget or GnomeVFS installed.",
"Michael Natterer",
"Michael Natterer",
"2005",
N_("URI"),
"RGB*, GRAY*, INDEXED*",
GIMP_PLUGIN,
G_N_ELEMENTS (save_args), 0,
save_args, NULL);
gimp_plugin_icon_register ("file_uri_save",
GIMP_ICON_TYPE_STOCK_ID, GIMP_STOCK_WEB);
gimp_register_save_handler ("file_uri_save",
"", uri_backend_get_save_protocols ());
}
uri_backend_shutdown ();
@ -134,7 +172,8 @@ run (const gchar *name,
return;
}
if (! strcmp (name, "file_uri_load") && uri_backend_get_load_protocols ())
if (! strcmp (name, "file_uri_load") &&
uri_backend_get_load_protocols ())
{
image_ID = load_image (param[2].data.d_string, run_mode);
@ -147,6 +186,14 @@ run (const gchar *name,
values[1].data.d_image = image_ID;
}
}
else if (! strcmp (name, "file_uri_save") &&
uri_backend_get_save_protocols ())
{
status = save_image (param[3].data.d_string,
param[1].data.d_int32,
param[2].data.d_int32,
param[0].data.d_int32);
}
else
{
status = GIMP_PDB_CALLING_ERROR;
@ -161,29 +208,12 @@ static gint32
load_image (const gchar *uri,
GimpRunMode run_mode)
{
gchar *basename;
gchar *tmpname = NULL;
gint32 image_ID = -1;
gboolean name_image = FALSE;
GError *error = NULL;
basename = g_path_get_basename (uri);
if (basename)
{
gchar *ext = strchr (basename, '.');
if (ext && strlen (ext))
{
tmpname = gimp_temp_name (ext + 1);
name_image = TRUE;
}
g_free (basename);
}
if (! tmpname)
tmpname = gimp_temp_name ("xxx");
tmpname = get_temp_name (uri, &name_image);
if (uri_backend_load_image (uri, tmpname, run_mode, &error))
{
@ -208,3 +238,84 @@ load_image (const gchar *uri,
return image_ID;
}
static GimpPDBStatusType
save_image (const gchar *uri,
gint32 image_ID,
gint32 drawable_ID,
gint32 run_mode)
{
gchar *tmpname;
GError *error = NULL;
tmpname = get_temp_name (uri, NULL);
if (! (gimp_file_save (run_mode,
image_ID,
drawable_ID,
tmpname,
tmpname) && valid_file (tmpname)))
{
unlink (tmpname);
g_free (tmpname);
return GIMP_PDB_EXECUTION_ERROR;
}
if (! uri_backend_save_image (uri, tmpname, run_mode, &error))
{
g_message ("%s", error->message);
g_clear_error (&error);
unlink (tmpname);
g_free (tmpname);
return GIMP_PDB_EXECUTION_ERROR;
}
unlink (tmpname);
g_free (tmpname);
return GIMP_PDB_SUCCESS;
}
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;
return stat (filename, &buf) == 0 && buf.st_size > 0;
}