Commit 6468fa06 authored by Michael Natterer's avatar Michael Natterer 😴

pdb, plug-ins: change gimp_temp_name() to gimp_temp_file()

parent 4118fb6a
Pipeline #114147 passed with stages
in 12 minutes and 51 seconds
......@@ -201,7 +201,7 @@ get_parasite_list_invoker (GimpProcedure *procedure,
}
static GimpValueArray *
temp_name_invoker (GimpProcedure *procedure,
temp_file_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
......@@ -211,24 +211,20 @@ temp_name_invoker (GimpProcedure *procedure,
gboolean success = TRUE;
GimpValueArray *return_vals;
const gchar *extension;
gchar *name = NULL;
GFile *file = NULL;
extension = g_value_get_string (gimp_value_array_index (args, 0));
if (success)
{
GFile *file = gimp_get_temp_file (gimp, extension);
name = g_file_get_path (file);
g_object_unref (file);
file = gimp_get_temp_file (gimp, extension);
}
return_vals = gimp_procedure_get_return_values (procedure, success,
error ? *error : NULL);
if (success)
g_value_take_string (gimp_value_array_index (return_vals, 1), name);
g_value_set_object (gimp_value_array_index (return_vals, 1), file);
return return_vals;
}
......@@ -412,14 +408,14 @@ register_gimp_procs (GimpPDB *pdb)
g_object_unref (procedure);
/*
* gimp-temp-name
* gimp-temp-file
*/
procedure = gimp_procedure_new (temp_name_invoker);
procedure = gimp_procedure_new (temp_file_invoker);
gimp_object_set_static_name (GIMP_OBJECT (procedure),
"gimp-temp-name");
"gimp-temp-file");
gimp_procedure_set_static_help (procedure,
"Generates a unique filename.",
"Generates a unique filename using the temp path supplied in the user's gimprc.",
"Generates a unique temporary file.",
"Generates a unique file using the temp path supplied in the user's gimprc.",
NULL);
gimp_procedure_set_static_attribution (procedure,
"Josh MacDonald",
......@@ -433,12 +429,11 @@ register_gimp_procs (GimpPDB *pdb)
NULL,
GIMP_PARAM_READWRITE));
gimp_procedure_add_return_value (procedure,
gimp_param_spec_string ("name",
"name",
"The new temp filename",
FALSE, FALSE, FALSE,
NULL,
GIMP_PARAM_READWRITE));
g_param_spec_object ("file",
"file",
"The new temp file",
G_TYPE_FILE,
GIMP_PARAM_READWRITE));
gimp_pdb_register_procedure (pdb, procedure);
g_object_unref (procedure);
}
......@@ -385,13 +385,13 @@ EXPORTS
gimp_image_get_component_visible
gimp_image_get_default_new_layer_mode
gimp_image_get_effective_color_profile
gimp_image_get_exported_uri
gimp_image_get_filename
gimp_image_get_exported_file
gimp_image_get_file
gimp_image_get_floating_sel
gimp_image_get_guide_orientation
gimp_image_get_guide_position
gimp_image_get_id
gimp_image_get_imported_uri
gimp_image_get_imported_file
gimp_image_get_item_position
gimp_image_get_layer_by_name
gimp_image_get_layer_by_tattoo
......@@ -409,11 +409,10 @@ EXPORTS
gimp_image_get_thumbnail_data
gimp_image_get_type
gimp_image_get_unit
gimp_image_get_uri
gimp_image_get_vectors
gimp_image_get_vectors_by_name
gimp_image_get_vectors_by_tattoo
gimp_image_get_xcf_uri
gimp_image_get_xcf_file
gimp_image_grid_get_background_color
gimp_image_grid_get_foreground_color
gimp_image_grid_get_offset
......@@ -470,7 +469,7 @@ EXPORTS
gimp_image_set_colormap
gimp_image_set_component_active
gimp_image_set_component_visible
gimp_image_set_filename
gimp_image_set_file
gimp_image_set_metadata
gimp_image_set_resolution
gimp_image_set_tattoo_state
......@@ -746,7 +745,7 @@ EXPORTS
gimp_show_help_button
gimp_smudge
gimp_smudge_default
gimp_temp_name
gimp_temp_file
gimp_text_fontname
gimp_text_get_extents_fontname
gimp_text_layer_get_antialias
......
......@@ -256,37 +256,36 @@ gimp_get_parasite_list (gint *num_parasites)
}
/**
* gimp_temp_name:
* gimp_temp_file:
* @extension: The extension the file will have.
*
* Generates a unique filename.
* Generates a unique temporary file.
*
* Generates a unique filename using the temp path supplied in the
* user's gimprc.
* Generates a unique file using the temp path supplied in the user's
* gimprc.
*
* Returns: (transfer full): The new temp filename.
* The returned value must be freed with g_free().
* Returns: (transfer full): The new temp file.
**/
gchar *
gimp_temp_name (const gchar *extension)
GFile *
gimp_temp_file (const gchar *extension)
{
GimpValueArray *args;
GimpValueArray *return_vals;
gchar *name = NULL;
GFile *file = NULL;
args = gimp_value_array_new_from_types (NULL,
G_TYPE_STRING, extension,
G_TYPE_NONE);
return_vals = gimp_pdb_run_procedure_array (gimp_get_pdb (),
"gimp-temp-name",
"gimp-temp-file",
args);
gimp_value_array_unref (args);
if (GIMP_VALUES_GET_ENUM (return_vals, 0) == GIMP_PDB_SUCCESS)
name = GIMP_VALUES_DUP_STRING (return_vals, 1);
file = GIMP_VALUES_DUP_FILE (return_vals, 1);
gimp_value_array_unref (return_vals);
return name;
return file;
}
......@@ -38,7 +38,7 @@ gboolean gimp_attach_parasite (const GimpParasite *parasite);
gboolean gimp_detach_parasite (const gchar *name);
GimpParasite* gimp_get_parasite (const gchar *name);
gchar** gimp_get_parasite_list (gint *num_parasites);
gchar* gimp_temp_name (const gchar *extension);
GFile* gimp_temp_file (const gchar *extension);
G_END_DECLS
......
......@@ -190,11 +190,11 @@ CODE
);
}
sub temp_name {
$blurb = 'Generates a unique filename.';
sub temp_file {
$blurb = 'Generates a unique temporary file.';
$help = <<'HELP';
Generates a unique filename using the temp path supplied in the user's gimprc.
Generates a unique file using the temp path supplied in the user's gimprc.
HELP
&josh_pdb_misc('1997');
......@@ -206,18 +206,14 @@ HELP
);
@outargs = (
{ name => 'name', type => 'string',
desc => 'The new temp filename' }
{ name => 'file', type => 'file',
desc => 'The new temp file' }
);
%invoke = (
code => <<'CODE'
{
GFile *file = gimp_get_temp_file (gimp, extension);
name = g_file_get_path (file);
g_object_unref (file);
file = gimp_get_temp_file (gimp, extension);
}
CODE
);
......@@ -234,7 +230,7 @@ CODE
detach_parasite
get_parasite
get_parasite_list
temp_name);
temp_file);
%exports = (app => [@procs], lib => [@procs[0..1,3..7]]);
......
......@@ -102,10 +102,10 @@
* that metric, I figure this plug-in is worth about $10,000 USD */
/* But you got it free. Magic of Gnu. */
typedef gboolean (*LoadFn) (const char *infile,
const char *outfile);
typedef gboolean (*SaveFn) (const char *infile,
const char *outfile);
typedef gboolean (* LoadFn) (GFile *infile,
GFile *outfile);
typedef gboolean (* SaveFn) (GFile *infile,
GFile *outfile);
typedef struct _CompressorEntry CompressorEntry;
......@@ -178,25 +178,25 @@ static GimpPDBStatusType save_image (const CompressorEntry *compressor,
gint32 run_mode,
GError **error);
static gboolean valid_file (const gchar *filename);
static gboolean valid_file (GFile *file);
static const gchar * find_extension (const CompressorEntry *compressor,
const gchar *filename);
static gboolean gzip_load (const char *infile,
const char *outfile);
static gboolean gzip_save (const char *infile,
const char *outfile);
static gboolean gzip_load (GFile *infile,
GFile *outfile);
static gboolean gzip_save (GFile *infile,
GFile *outfile);
static gboolean bzip2_load (const char *infile,
const char *outfile);
static gboolean bzip2_save (const char *infile,
const char *outfile);
static gboolean bzip2_load (GFile *infile,
GFile *outfile);
static gboolean bzip2_save (GFile *infile,
GFile *outfile);
static gboolean xz_load (const char *infile,
const char *outfile);
static gboolean xz_save (const char *infile,
const char *outfile);
static goffset get_file_info (const gchar *filename);
static gboolean xz_load (GFile *infile,
GFile *outfile);
static gboolean xz_save (GFile *infile,
GFile *outfile);
static goffset get_file_info (GFile *file);
G_DEFINE_TYPE (Compressor, compressor, GIMP_TYPE_PLUG_IN)
......@@ -424,7 +424,7 @@ save_image (const CompressorEntry *compressor,
{
gchar *filename = g_file_get_path (file);
const gchar *ext;
gchar *tmpname;
GFile *tmp_file;
ext = find_extension (compressor, filename);
......@@ -436,16 +436,16 @@ save_image (const CompressorEntry *compressor,
/* get a temp name with the right extension and save into it. */
tmpname = gimp_temp_name (ext + 1);
tmp_file = gimp_temp_file (ext + 1);
if (! (gimp_file_save (run_mode,
image,
drawable,
g_file_new_for_path (tmpname)) &&
valid_file (tmpname)))
tmp_file) &&
valid_file (tmp_file)))
{
g_unlink (tmpname);
g_free (tmpname);
g_file_delete (tmp_file, NULL, NULL);
g_object_unref (tmp_file);
g_free (filename);
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
......@@ -457,18 +457,19 @@ save_image (const CompressorEntry *compressor,
gimp_progress_init_printf (_("Compressing '%s'"),
gimp_file_get_utf8_name (file));
if (! compressor->save_fn (tmpname, filename))
if (! compressor->save_fn (tmp_file, file))
{
g_unlink (tmpname);
g_free (tmpname);
g_file_delete (tmp_file, NULL, NULL);
g_object_unref (tmp_file);
g_free (filename);
return GIMP_PDB_EXECUTION_ERROR;
}
g_unlink (tmpname);
g_file_delete (tmp_file, NULL, NULL);
g_object_unref (tmp_file);
gimp_progress_update (1.0);
g_free (tmpname);
/* ask the core to save a thumbnail for compressed XCF files */
if (strcmp (ext, ".xcf") == 0)
......@@ -489,7 +490,7 @@ load_image (const CompressorEntry *compressor,
GimpImage *image;
gchar *filename;
const gchar *ext;
gchar *tmpname;
GFile *tmp_file;
filename = g_file_get_path (file);
......@@ -503,11 +504,11 @@ load_image (const CompressorEntry *compressor,
}
/* find a temp name */
tmpname = gimp_temp_name (ext + 1);
tmp_file = gimp_temp_file (ext + 1);
if (! compressor->load_fn (filename, tmpname))
if (! compressor->load_fn (file, tmp_file))
{
g_free (tmpname);
g_object_unref (tmp_file);
g_free (filename);
*status = GIMP_PDB_EXECUTION_ERROR;
return NULL;
......@@ -517,10 +518,10 @@ load_image (const CompressorEntry *compressor,
/* now that we uncompressed it, load the temp file */
image = gimp_file_load (run_mode, g_file_new_for_path (tmpname));
image = gimp_file_load (run_mode, tmp_file);
g_unlink (tmpname);
g_free (tmpname);
g_file_delete (tmp_file, NULL, NULL);
g_object_unref (tmp_file);
if (image)
{
......@@ -543,11 +544,17 @@ load_image (const CompressorEntry *compressor,
}
static gboolean
valid_file (const gchar *filename)
valid_file (GFile *file)
{
struct stat buf;
gchar *filename;
struct stat buf;
gboolean valid;
filename = g_file_get_path (file);
valid = g_stat (filename, &buf) == 0 && buf.st_size > 0;
g_free (filename);
return g_stat (filename, &buf) == 0 && buf.st_size > 0;
return valid;
}
static const gchar *
......@@ -588,33 +595,35 @@ find_extension (const CompressorEntry *compressor,
}
static gboolean
gzip_load (const char *infile,
const char *outfile)
gzip_load (GFile *infile,
GFile *outfile)
{
gboolean ret;
int fd;
gzFile in;
FILE *out;
char buf[16384];
int len;
gchar *in_filename = g_file_get_path (infile);
gchar *out_filename = g_file_get_path (outfile);
gboolean ret;
int fd;
gzFile in;
FILE *out;
char buf[16384];
int len;
ret = FALSE;
in = NULL;
out = NULL;
fd = g_open (infile, O_RDONLY | _O_BINARY, 0);
fd = g_open (in_filename, O_RDONLY | _O_BINARY, 0);
if (fd == -1)
goto out;
in = gzdopen (fd, "rb");
if (!in)
if (! in)
{
close (fd);
goto out;
}
out = g_fopen (outfile, "wb");
if (!out)
out = g_fopen (out_filename, "wb");
if (! out)
goto out;
while (TRUE)
......@@ -642,13 +651,18 @@ gzip_load (const char *infile,
if (out)
fclose (out);
g_free (in_filename);
g_free (out_filename);
return ret;
}
static gboolean
gzip_save (const char *infile,
const char *outfile)
gzip_save (GFile *infile,
GFile *outfile)
{
gchar *in_filename = g_file_get_path (infile);
gchar *out_filename = g_file_get_path (outfile);
gboolean ret;
FILE *in;
int fd;
......@@ -661,16 +675,16 @@ gzip_save (const char *infile,
in = NULL;
out = NULL;
in = g_fopen (infile, "rb");
if (!in)
in = g_fopen (in_filename, "rb");
if (! in)
goto out;
fd = g_open (outfile, O_CREAT | O_WRONLY | O_TRUNC | _O_BINARY, 0664);
fd = g_open (out_filename, O_CREAT | O_WRONLY | O_TRUNC | _O_BINARY, 0664);
if (fd == -1)
goto out;
out = gzdopen (fd, "wb");
if (!out)
if (! out)
{
close (fd);
goto out;
......@@ -710,9 +724,11 @@ gzip_save (const char *infile,
}
static gboolean
bzip2_load (const char *infile,
const char *outfile)
bzip2_load (GFile *infile,
GFile *outfile)
{
gchar *in_filename = g_file_get_path (infile);
gchar *out_filename = g_file_get_path (outfile);
gboolean ret;
int fd;
BZFILE *in;
......@@ -724,7 +740,7 @@ bzip2_load (const char *infile,
in = NULL;
out = NULL;
fd = g_open (infile, O_RDONLY | _O_BINARY, 0);
fd = g_open (in_filename, O_RDONLY | _O_BINARY, 0);
if (fd == -1)
goto out;
......@@ -735,7 +751,7 @@ bzip2_load (const char *infile,
goto out;
}
out = g_fopen (outfile, "wb");
out = g_fopen (out_filename, "wb");
if (!out)
goto out;
......@@ -764,13 +780,18 @@ bzip2_load (const char *infile,
if (out)
fclose (out);
g_free (in_filename);
g_free (out_filename);
return ret;
}
static gboolean
bzip2_save (const char *infile,
const char *outfile)
bzip2_save (GFile *infile,
GFile *outfile)
{
gchar *in_filename = g_file_get_path (infile);
gchar *out_filename = g_file_get_path (outfile);
gboolean ret;
FILE *in;
int fd;
......@@ -783,11 +804,11 @@ bzip2_save (const char *infile,
in = NULL;
out = NULL;
in = g_fopen (infile, "rb");
in = g_fopen (in_filename, "rb");
if (!in)
goto out;
fd = g_open (outfile, O_CREAT | O_WRONLY | O_TRUNC | _O_BINARY, 0664);
fd = g_open (out_filename, O_CREAT | O_WRONLY | O_TRUNC | _O_BINARY, 0664);
if (fd == -1)
goto out;
......@@ -828,13 +849,18 @@ bzip2_save (const char *infile,
if (out)
BZ2_bzclose (out);
g_free (in_filename);
g_free (out_filename);
return ret;
}
static gboolean
xz_load (const char *infile,
const char *outfile)
xz_load (GFile *infile,
GFile *outfile)
{
gchar *in_filename = g_file_get_path (infile);
gchar *out_filename = g_file_get_path (outfile);
gboolean ret;
FILE *in;
FILE *out;
......@@ -848,11 +874,11 @@ xz_load (const char *infile,
in = NULL;
out = NULL;
in = g_fopen (infile, "rb");
in = g_fopen (in_filename, "rb");
if (!in)
goto out;
out = g_fopen (outfile, "wb");
out = g_fopen (out_filename, "wb");
if (!out)
goto out;
......@@ -916,13 +942,18 @@ xz_load (const char *infile,
if (out)
fclose (out);
g_free (in_filename);
g_free (out_filename);
return ret;
}
static gboolean
xz_save (const char *infile,
const char *outfile)
xz_save (GFile *infile,
GFile *outfile)
{
gchar *in_filename = g_file_get_path (infile);
gchar *out_filename = g_file_get_path (outfile);
gboolean ret;
FILE *in;
FILE *out;
......@@ -937,12 +968,12 @@ xz_save (const char *infile,
in = NULL;
out = NULL;
in = g_fopen (infile, "rb");
in = g_fopen (in_filename, "rb");
if (!in)
goto out;
file_size = get_file_info (infile);
out = g_fopen (outfile, "wb");
out = g_fopen (out_filename, "wb");
if (!out)
goto out;
......@@ -1010,14 +1041,16 @@ xz_save (const char *infile,
if (out)
fclose (out);
g_free (in_filename);
g_free (out_filename);
return ret;
}
/* get file size from a filename */
static goffset
get_file_info (const gchar *filename)
get_file_info (GFile *file)
{
GFile *file = g_file_new_for_path (filename);
GFileInfo *info;
goffset size = 1;
......@@ -1033,7 +1066,5 @@ get_file_info (const gchar *filename)
g_object_unref (info);
}
g_object_unref (file);
return size;
}
......@@ -1075,6 +1075,7 @@ mng_save_image (const gchar *filename,
gchar frame_mode;
int frame_delay;
GFile *temp_file;
gchar *temp_file_name;
png_structp pp;
png_infop info;
......@@ -1218,19 +1219,21 @@ mng_save_image (const gchar *filename,
}
}
if ((temp_file_name = gimp_temp_name ("mng")) == NULL)
if ((temp_file = gimp_temp_file ("mng")) == NULL)
{
g_warning ("gimp_temp_name() failed in mng_save_image()");
g_warning ("gimp_temp_file() failed in mng_save_image()");
goto err3;
}
temp_file_name = g_file_get_path (temp_file);
if ((outfile = g_fopen (temp_file_name, "wb")) == NULL)
{
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
_("Could not open '%s' for writing: %s"),
gimp_filename_to_utf8 (temp_file_name),
gimp_file_get_utf8_name (temp_file),
g_strerror (errno));
g_unlink (temp_file_name);
g_file_delete (temp_file, NULL, NULL);
goto err3;
}
......@@ -1240,7 +1243,7 @@ mng_save_image (const gchar *filename,
{
g_warning ("Unable to png_create_write_struct() in mng_save_image()");
fclose (outfile);
g_unlink (temp_file_name);
g_file_delete (temp_file, NULL, NULL);
goto err3;
}
......@@ -1251,7 +1254,7 @@ mng_save_image (const gchar *filename,
("Unable to png_create_info_struct() in mng_save_image()");
png_destroy_write_struct (&pp, NULL);
fclose (outfile);
g_unlink (temp_file_name);
g_file_delete (temp_file, NULL, NULL);
goto err3;
}
......@@ -1260,7 +1263,7 @@ mng_save_image (const gchar *filename,
g_warning ("HRM saving PNG in mng_save_image()");
png_destroy_write_struct (&pp, &info);
fclose (outfile);
g_unlink (temp_file_name);
g_file_delete (temp_file, NULL, NULL);
goto err3;
}
......@@ -1300,7 +1303,7 @@ mng_save_image (const gchar *filename,
g_warning ("This can't be!\n");
png_destroy_write_struct (&pp, &info);
fclose (outfile);
g_unlink (temp_file_name);
g_file_delete (temp_file, NULL, NULL);
goto err3;
}
......@@ -1397,13 +1400,14 @@ mng_save_image (const gchar *filename,
fclose (outfile);
infile = g_fopen (temp_file_name, "rb");
if (NULL == infile)
if (! infile)
{
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
_("Could not open '%s' for reading: %s"),
gimp_filename_to_utf8 (temp_file_name),
gimp_file_get_utf8_name (temp_file),
g_strerror (errno));
g_unlink (temp_file_name);
g_file_delete (temp_file, NULL, NULL);
goto err3;
}
......@@ -1530,7 +1534,7 @@ mng_save_image (const gchar *filename,
}
fclose (infile);
g_unlink (temp_file_name);
g_file_delete (temp_file, NULL, NULL);
}
if (mng_putchunk_mend (handle) != MNG_NOERROR)
......
......@@ -1672,7 +1672,7 @@ get_bbox (GFile *file,
return retval;
}