pass error messages with the return values instead of calling g_message().

2008-08-18  Sven Neumann  <sven@gimp.org>

	* plug-ins/file-uri/uri.c: pass error messages with the return
	values instead of calling g_message().

	* plug-ins/file-uri/uri-backend-libcurl.c
	* plug-ins/file-uri/uri-backend-gnomevfs.c
	* plug-ins/file-uri/uri-backend-gvfs.c
	* plug-ins/file-uri/uri-backend-wget.c: set errors in the 
	G_FILE_ERROR domain and other minor cleanups.


svn path=/trunk/; revision=26655
This commit is contained in:
Sven Neumann 2008-08-18 21:18:01 +00:00 committed by Sven Neumann
parent 2f0081f492
commit 8b0384a70a
6 changed files with 92 additions and 69 deletions

View File

@ -1,9 +1,20 @@
2008-08-18 Sven Neumann <sven@gimp.org>
* plug-ins/file-uri/uri.c: pass error messages with the return
values instead of calling g_message().
* plug-ins/file-uri/uri-backend-libcurl.c
* plug-ins/file-uri/uri-backend-gnomevfs.c
* plug-ins/file-uri/uri-backend-gvfs.c
* plug-ins/file-uri/uri-backend-wget.c: set errors in the
G_FILE_ERROR domain and other minor cleanups.
2008-08-18 Sven Neumann <sven@gimp.org>
* plug-ins/common/file-compressor.c: open the temporary file
before doing the fork. This allows us to return an error message
if the file can't be opened. Also changed the code to not use
g_message() from the child process.
before forking. This allows us to return an error message if the
file can't be opened. Also changed the code to not use g_message()
from the child process.
2008-08-18 Sven Neumann <sven@gimp.org>

View File

@ -78,7 +78,8 @@ uri_backend_init (const gchar *plugin_name,
{
if (! gnome_vfs_init ())
{
g_set_error (error, 0, 0, "Could not initialize GnomeVFS");
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"%s", "Could not initialize GnomeVFS");
return FALSE;
}
@ -267,7 +268,7 @@ copy_uri (const gchar *src_uri,
if (result != GNOME_VFS_OK)
{
g_set_error (error, 0, 0,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Could not open '%s' for reading: %s"),
src_uri, gnome_vfs_result_to_string (result));
return FALSE;
@ -278,7 +279,7 @@ copy_uri (const gchar *src_uri,
if (result != GNOME_VFS_OK)
{
g_set_error (error, 0, 0,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Could not open '%s' for writing: %s"),
dest_uri, gnome_vfs_result_to_string (result));
gnome_vfs_close (read_handle);
@ -307,7 +308,7 @@ copy_uri (const gchar *src_uri,
if (result != GNOME_VFS_ERROR_EOF)
{
memsize = g_format_size_for_display (sizeof (buffer));
g_set_error (error, 0, 0,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Failed to read %s from '%s': %s"),
memsize, src_uri,
gnome_vfs_result_to_string (result));
@ -356,7 +357,7 @@ copy_uri (const gchar *src_uri,
if (chunk_written < chunk_read)
{
memsize = g_format_size_for_display (chunk_read);
g_set_error (error, 0, 0,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Failed to write %s to '%s': %s"),
memsize, dest_uri,
gnome_vfs_result_to_string (result));

View File

@ -286,7 +286,8 @@ copy_uri (const gchar *src_uri,
if (! g_vfs_is_active (vfs))
{
g_set_error (error, 0, 0, "Initialization of GVfs failed");
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"Initialization of GVfs failed");
return FALSE;
}

View File

@ -54,7 +54,8 @@ uri_backend_init (const gchar *plugin_name,
if (curl_global_init (CURL_GLOBAL_ALL))
{
g_set_error (error, 0, 0, _("Could not initialize libcurl"));
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"%s", _("Could not initialize libcurl"));
return FALSE;
}
@ -156,8 +157,9 @@ uri_backend_load_image (const gchar *uri,
if ((out_file = g_fopen (tmpname, "wb")) == NULL)
{
g_set_error (error, 0, 0,
_("Could not open output file for writing"));
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
_("Could not open '%s' for writing: %s"),
gimp_filename_to_utf8 (filename), g_strerror (errno));
return FALSE;
}
@ -182,7 +184,7 @@ uri_backend_load_image (const gchar *uri,
if ((result = curl_easy_perform (curl_handle)) != 0)
{
fclose (out_file);
g_set_error (error, 0, 0,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Could not open '%s' for reading: %s"),
uri, curl_easy_strerror (result));
curl_easy_cleanup (curl_handle);
@ -194,7 +196,7 @@ uri_backend_load_image (const gchar *uri,
if (response_code != 200)
{
fclose (out_file);
g_set_error (error, 0, 0,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Opening '%s' for reading resulted in HTTP "
"response code: %d"),
uri, response_code);
@ -216,8 +218,7 @@ uri_backend_save_image (const gchar *uri,
GimpRunMode run_mode,
GError **error)
{
g_set_error (error, 0, 0,
"EEK! uri_backend_save_image() should not have been called!");
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, "not implemented");
return FALSE;
}

View File

@ -91,7 +91,8 @@ uri_backend_load_image (const gchar *uri,
if (pipe (p) != 0)
{
g_set_error (error, 0, 0, "pipe() failed: %s", g_strerror (errno));
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,,
"pipe() failed: %s", g_strerror (errno));
return FALSE;
}
@ -100,7 +101,8 @@ uri_backend_load_image (const gchar *uri,
if ((pid = fork()) < 0)
{
g_set_error (error, 0, 0, "fork() failed: %s", g_strerror (errno));
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,,
"fork() failed: %s", g_strerror (errno));
return FALSE;
}
else if (pid == 0)
@ -122,7 +124,6 @@ uri_backend_load_image (const gchar *uri,
execlp ("wget",
"wget", "-v", "-e", "server-response=off", "-T", timeout_str,
uri, "-O", tmpname, NULL);
g_set_error (error, 0, 0, "exec() failed: wget: %s", g_strerror (errno));
_exit (127);
}
else
@ -157,7 +158,7 @@ uri_backend_load_image (const gchar *uri,
/* Eat any Location lines */
if (redirect && fgets (buf, sizeof (buf), input) == NULL)
{
g_set_error (error, 0, 0,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("wget exited abnormally on URI '%s'"), uri);
return FALSE;
}
@ -177,7 +178,7 @@ uri_backend_load_image (const gchar *uri,
/* The second line is the local copy of the file */
if (fgets (buf, sizeof (buf), input) == NULL)
{
g_set_error (error, 0, 0,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("wget exited abnormally on URI '%s'"), uri);
return FALSE;
}
@ -196,7 +197,7 @@ uri_backend_load_image (const gchar *uri,
read_connect:
if (fgets (buf, sizeof (buf), input) == NULL)
{
g_set_error (error, 0, 0,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("wget exited abnormally on URI '%s'"), uri);
return FALSE;
}
@ -219,13 +220,13 @@ uri_backend_load_image (const gchar *uri,
if (fgets (buf, sizeof (buf), input) == NULL)
{
g_set_error (error, 0, 0,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("wget exited abnormally on URI '%s'"), uri);
return FALSE;
}
else if (! connected)
{
g_set_error (error, 0, 0,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("A network error occurred: %s"), buf);
DEBUG (buf);
@ -248,7 +249,7 @@ uri_backend_load_image (const gchar *uri,
/* The fifth line is either the length of the file or an error */
if (fgets (buf, sizeof (buf), input) == NULL)
{
g_set_error (error, 0, 0,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("wget exited abnormally on URI '%s'"), uri);
return FALSE;
}
@ -258,7 +259,7 @@ uri_backend_load_image (const gchar *uri,
}
else
{
g_set_error (error, 0, 0,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("A network error occurred: %s"), buf);
DEBUG (buf);
@ -270,7 +271,7 @@ uri_backend_load_image (const gchar *uri,
if (sscanf (buf, "Length: %37s", sizestr) != 1)
{
g_set_error (error, 0, 0,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"Could not parse wget's file length message");
return FALSE;
}
@ -366,7 +367,7 @@ uri_backend_load_image (const gchar *uri,
if (! finished)
{
g_set_error (error, 0, 0,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"wget exited before finishing downloading URI\n'%s'",
uri);
return FALSE;
@ -382,7 +383,7 @@ uri_backend_save_image (const gchar *uri,
GimpRunMode run_mode,
GError **error)
{
g_set_error (error, 0, 0, "EEK");
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, "not implemented");
return FALSE;
}

View File

@ -51,11 +51,13 @@ static void run (const gchar *name,
GimpParam **return_vals);
static gint32 load_image (const gchar *uri,
GimpRunMode run_mode);
GimpRunMode run_mode,
GError **error);
static GimpPDBStatusType save_image (const gchar *uri,
gint32 image_ID,
gint32 drawable_ID,
gint32 run_mode);
gint32 run_mode,
GError **error);
static gchar * get_temp_name (const gchar *uri,
gboolean *name_image);
@ -173,15 +175,19 @@ run (const gchar *name,
if (! uri_backend_init (PLUG_IN_BINARY, TRUE, run_mode, &error))
{
g_message (error->message);
g_clear_error (&error);
if (error)
{
*nreturn_vals = 2;
values[1].type = GIMP_PDB_STRING;
values[1].data.d_string = error->message;
}
return;
}
if (! strcmp (name, LOAD_PROC) && uri_backend_get_load_protocols ())
{
image_ID = load_image (param[2].data.d_string, run_mode);
image_ID = load_image (param[2].data.d_string, run_mode, &error);
if (image_ID != -1)
{
@ -197,7 +203,7 @@ run (const gchar *name,
status = save_image (param[3].data.d_string,
param[1].data.d_int32,
param[2].data.d_int32,
run_mode);
run_mode, &error);
}
else
{
@ -206,21 +212,28 @@ run (const gchar *name,
uri_backend_shutdown ();
if (status != GIMP_PDB_SUCCESS && error)
{
*nreturn_vals = 2;
values[1].type = GIMP_PDB_STRING;
values[1].data.d_string = error->message;
}
values[0].data.d_status = status;
}
static gint32
load_image (const gchar *uri,
GimpRunMode run_mode)
load_image (const gchar *uri,
GimpRunMode run_mode,
GError **error)
{
gchar *tmpname = NULL;
gint32 image_ID = -1;
gboolean name_image = FALSE;
GError *error = NULL;
tmpname = get_temp_name (uri, &name_image);
if (uri_backend_load_image (uri, tmpname, run_mode, &error))
if (uri_backend_load_image (uri, tmpname, run_mode, error))
{
image_ID = gimp_file_load (run_mode, tmpname, tmpname);
@ -231,11 +244,11 @@ load_image (const gchar *uri,
else
gimp_image_set_filename (image_ID, "");
}
}
else if (error)
{
g_message ("%s", error->message);
g_clear_error (&error);
else
{
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"%s", gimp_get_pdb_error ());
}
}
g_unlink (tmpname);
@ -245,43 +258,38 @@ load_image (const gchar *uri,
}
static GimpPDBStatusType
save_image (const gchar *uri,
gint32 image_ID,
gint32 drawable_ID,
gint32 run_mode)
save_image (const gchar *uri,
gint32 image_ID,
gint32 drawable_ID,
gint32 run_mode,
GError **error)
{
gchar *tmpname;
GError *error = NULL;
GimpPDBStatusType status = GIMP_PDB_EXECUTION_ERROR;
gchar *tmpname;
tmpname = get_temp_name (uri, NULL);
if (! (gimp_file_save (run_mode,
image_ID,
drawable_ID,
tmpname,
tmpname) && valid_file (tmpname)))
if (gimp_file_save (run_mode,
image_ID,
drawable_ID,
tmpname,
tmpname) && valid_file (tmpname))
{
g_unlink (tmpname);
g_free (tmpname);
return GIMP_PDB_EXECUTION_ERROR;
if (uri_backend_save_image (uri, tmpname, run_mode, error))
{
status = GIMP_PDB_SUCCESS;
}
}
if (! uri_backend_save_image (uri, tmpname, run_mode, &error))
else
{
g_message ("%s", error->message);
g_clear_error (&error);
g_unlink (tmpname);
g_free (tmpname);
return GIMP_PDB_EXECUTION_ERROR;
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"%s", gimp_get_pdb_error ());
}
g_unlink (tmpname);
g_free (tmpname);
return GIMP_PDB_SUCCESS;
return status;
}
static gchar *