1
/* The GIMP -- an image manipulation program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
/* Author: Josh MacDonald. */
27
#include <sys/param.h>
34
#include <libgimp/gimp.h>
35
#include <libgimp/gimpui.h>
37
#include "libgimp/stdplugins-intl.h"
43
static void query (void);
44
static void run (const gchar *name,
46
const GimpParam *param,
48
GimpParam **return_vals);
50
static gint32 load_image (const gchar *filename,
52
GimpPDBStatusType *status /* return value */);
54
GimpPlugInInfo PLUG_IN_INFO =
58
query, /* query_proc */
67
static GimpParamDef load_args[] =
69
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
70
{ GIMP_PDB_STRING, "filename", "The name of the file to load" },
71
{ GIMP_PDB_STRING, "raw_filename", "The name entered" }
74
static GimpParamDef load_return_vals[] =
76
{ GIMP_PDB_IMAGE, "image", "Output image" }
79
gimp_install_procedure ("file_url_load",
80
"loads files given a URL",
81
"You need to have GNU Wget installed.",
82
"Spencer Kimball & Peter Mattis",
83
"Spencer Kimball & Peter Mattis",
88
G_N_ELEMENTS (load_args),
89
G_N_ELEMENTS (load_return_vals),
90
load_args, load_return_vals);
92
gimp_plugin_icon_register ("file_url_load",
93
GIMP_ICON_TYPE_STOCK_ID, GIMP_STOCK_WEB);
94
gimp_register_load_handler ("file_url_load",
100
run (const gchar *name,
102
const GimpParam *param,
104
GimpParam **return_vals)
106
static GimpParam values[2];
107
GimpRunMode run_mode;
108
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
111
run_mode = param[0].data.d_int32;
114
*return_vals = values;
115
values[0].type = GIMP_PDB_STATUS;
116
values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
118
if (strcmp (name, "file_url_load") == 0)
120
image_ID = load_image (param[2].data.d_string, run_mode, &status);
122
if (image_ID != -1 &&
123
status == GIMP_PDB_SUCCESS)
126
values[1].type = GIMP_PDB_IMAGE;
127
values[1].data.d_image = image_ID;
132
status = GIMP_PDB_CALLING_ERROR;
135
values[0].data.d_status = status;
139
load_image (const gchar *filename,
140
GimpRunMode run_mode,
141
GimpPDBStatusType *status)
144
gchar *ext = strrchr (filename, '.');
148
gboolean name_image = FALSE;
150
if (!ext || ext[1] == 0 || strchr (ext, '/'))
152
tmpname = gimp_temp_name ("xxx");
156
tmpname = gimp_temp_name (ext + 1);
162
g_message ("pipe() failed: %s", g_strerror (errno));
164
*status = GIMP_PDB_EXECUTION_ERROR;
168
if ((pid = fork()) < 0)
170
g_message ("fork() failed: %s", g_strerror (errno));
172
*status = GIMP_PDB_EXECUTION_ERROR;
183
/* produce deterministic output */
184
putenv ("LANGUAGE=C");
189
execlp ("wget", "wget", "-e", "server-response=off", "-T", TIMEOUT,
190
filename, "-O", tmpname, NULL);
191
g_message ("exec() failed: wget: %s", g_strerror (errno));
199
gboolean seen_resolve = FALSE;
200
gboolean connected = FALSE;
201
gboolean redirect = FALSE;
202
gboolean file_found = FALSE;
210
gboolean finished = FALSE;
212
gboolean debug = FALSE;
214
#define DEBUG(x) if (debug) g_printerr (x)
218
input = fdopen (p[0], "r");
220
/* hardcoded and not-really-foolproof scanning of wget putput */
223
/* Eat any Location lines */
224
if (redirect && fgets (buf, BUFSIZE, input) == NULL)
226
g_message ("wget exited abnormally on URL '%s'", filename);
228
*status = GIMP_PDB_EXECUTION_ERROR;
234
if (fgets (buf, BUFSIZE, input) == NULL)
236
/* no message here because failing on the first line means
237
* that wget was not found
240
*status = GIMP_PDB_EXECUTION_ERROR;
246
/* The second line is the local copy of the file */
247
if (fgets (buf, BUFSIZE, input) == NULL)
249
g_message ("wget exited abnormally on URL '%s'", filename);
251
*status = GIMP_PDB_EXECUTION_ERROR;
257
/* The third line is "Connecting to..." */
258
gimp_progress_init ("Connecting to server... "
259
"(timeout is "TIMEOUT" seconds)");
262
if (fgets (buf, BUFSIZE, input) == NULL)
264
g_message ("wget exited abnormally on URL '%s'", filename);
266
*status = GIMP_PDB_EXECUTION_ERROR;
269
else if (strstr (buf, "connected"))
273
/* newer wgets have a "Resolving foo" line, so eat it */
274
else if (!seen_resolve && strstr (buf, "Resolving"))
282
/* The fourth line is either the network request or an error */
283
gimp_progress_init ("Opening URL... "
284
"(timeout is "TIMEOUT" seconds)");
286
if (fgets (buf, BUFSIZE, input) == NULL)
288
g_message ("wget exited abnormally on URL '%s'", filename);
290
*status = GIMP_PDB_EXECUTION_ERROR;
293
else if (! connected)
295
g_message ("A network error occured: %s", buf);
300
*status = GIMP_PDB_EXECUTION_ERROR;
303
else if (strstr (buf, "302 Found"))
308
seen_resolve = FALSE;
316
/* The fifth line is either the length of the file or an error */
317
if (fgets (buf, BUFSIZE, input) == NULL)
319
g_message ("wget exited abnormally on URL '%s'", filename);
321
*status = GIMP_PDB_EXECUTION_ERROR;
324
else if (strstr (buf, "Length"))
330
g_message ("A network error occured: %s", buf);
335
*status = GIMP_PDB_EXECUTION_ERROR;
341
if (sscanf (buf, "Length: %31s", sizestr) != 1)
343
g_message ("Could not parse wget's file length message");
345
*status = GIMP_PDB_EXECUTION_ERROR;
349
/* strip away commas */
350
for (i = 0, j = 0; i < sizeof (sizestr); i++, j++)
352
if (sizestr[i] == ',')
355
sizestr[j] = sizestr[i];
357
if (sizestr[j] == '\0')
361
size = atoi (sizestr);
364
size_msg = g_strdup_printf ("%d bytes", size);
366
size_msg = g_strdup ("unknown amount");
368
/* Start the actual download... */
369
message = g_strdup_printf ("Downloading %s of image data... "
370
"(timeout is "TIMEOUT" seconds)", size_msg);
371
gimp_progress_init (message);
375
/* Switch to byte parsing wget's output... */
390
if (dot == '.') /* one kilobyte */
395
gimp_progress_update ((gdouble) (kilobytes * 1024) /
398
else if (dot == ':') /* the time string contains a ':' */
400
fgets (buf, BUFSIZE, input);
404
if (! strstr (buf, "error"))
407
gimp_progress_update (1.0);
416
g_message ("wget exited before finishing downloading URL\n'%s'",
420
*status = GIMP_PDB_EXECUTION_ERROR;
425
image_ID = gimp_file_load (run_mode, tmpname, tmpname);
432
*status = GIMP_PDB_SUCCESS;
434
gimp_image_set_filename (image_ID, filename);
436
gimp_image_set_filename (image_ID, "");
439
*status = GIMP_PDB_EXECUTION_ERROR;