~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to plug-ins/common/url.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 */
 
18
 
 
19
/* Author: Josh MacDonald. */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <stdlib.h>
 
24
#include <stdio.h>
 
25
#include <string.h>
 
26
#include <errno.h>
 
27
#include <sys/param.h>
 
28
#include <sys/wait.h>
 
29
 
 
30
#ifdef HAVE_UNISTD_H
 
31
#include <unistd.h>
 
32
#endif
 
33
 
 
34
#include <libgimp/gimp.h>
 
35
#include <libgimp/gimpui.h>
 
36
 
 
37
#include "libgimp/stdplugins-intl.h"
 
38
 
 
39
 
 
40
#define TIMEOUT "300"
 
41
#define BUFSIZE 1024
 
42
 
 
43
static void    query (void);
 
44
static void    run   (const gchar      *name,
 
45
                      gint              nparams,
 
46
                      const GimpParam  *param,
 
47
                      gint             *nreturn_vals,
 
48
                      GimpParam       **return_vals);
 
49
 
 
50
static gint32  load_image (const gchar       *filename,
 
51
                           GimpRunMode        run_mode,
 
52
                           GimpPDBStatusType *status /* return value */);
 
53
 
 
54
GimpPlugInInfo PLUG_IN_INFO =
 
55
{
 
56
  NULL,  /* init_proc  */
 
57
  NULL,  /* quit_proc  */
 
58
  query, /* query_proc */
 
59
  run,   /* run_proc   */
 
60
};
 
61
 
 
62
MAIN ()
 
63
 
 
64
static void
 
65
query (void)
 
66
{
 
67
  static GimpParamDef load_args[] =
 
68
  {
 
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" }
 
72
  };
 
73
 
 
74
  static GimpParamDef load_return_vals[] =
 
75
  {
 
76
    { GIMP_PDB_IMAGE, "image", "Output image" }
 
77
  };
 
78
 
 
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",
 
84
                          "1995-1997",
 
85
                          N_("URL"),
 
86
                          NULL,
 
87
                          GIMP_PLUGIN,
 
88
                          G_N_ELEMENTS (load_args),
 
89
                          G_N_ELEMENTS (load_return_vals),
 
90
                          load_args, load_return_vals);
 
91
 
 
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",
 
95
                              "",
 
96
                              "http:,https:,ftp:");
 
97
}
 
98
 
 
99
static void
 
100
run (const gchar      *name,
 
101
     gint              nparams,
 
102
     const GimpParam  *param,
 
103
     gint             *nreturn_vals,
 
104
     GimpParam       **return_vals)
 
105
{
 
106
  static GimpParam  values[2];
 
107
  GimpRunMode       run_mode;
 
108
  GimpPDBStatusType status = GIMP_PDB_SUCCESS;
 
109
  gint32            image_ID;
 
110
 
 
111
  run_mode = param[0].data.d_int32;
 
112
 
 
113
  *nreturn_vals = 1;
 
114
  *return_vals  = values;
 
115
  values[0].type          = GIMP_PDB_STATUS;
 
116
  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
 
117
 
 
118
  if (strcmp (name, "file_url_load") == 0)
 
119
    {
 
120
      image_ID = load_image (param[2].data.d_string, run_mode, &status);
 
121
 
 
122
      if (image_ID != -1 &&
 
123
          status == GIMP_PDB_SUCCESS)
 
124
        {
 
125
          *nreturn_vals = 2;
 
126
          values[1].type         = GIMP_PDB_IMAGE;
 
127
          values[1].data.d_image = image_ID;
 
128
        }
 
129
    }
 
130
  else
 
131
    {
 
132
      status = GIMP_PDB_CALLING_ERROR;
 
133
    }
 
134
 
 
135
  values[0].data.d_status = status;
 
136
}
 
137
 
 
138
static gint32
 
139
load_image (const gchar       *filename,
 
140
            GimpRunMode        run_mode,
 
141
            GimpPDBStatusType *status)
 
142
{
 
143
  gint32    image_ID;
 
144
  gchar    *ext = strrchr (filename, '.');
 
145
  gchar    *tmpname;
 
146
  gint      pid;
 
147
  gint      p[2];
 
148
  gboolean  name_image = FALSE;
 
149
 
 
150
  if (!ext || ext[1] == 0 || strchr (ext, '/'))
 
151
    {
 
152
      tmpname = gimp_temp_name ("xxx");
 
153
    }
 
154
  else
 
155
    {
 
156
      tmpname = gimp_temp_name (ext + 1);
 
157
      name_image = TRUE;
 
158
    }
 
159
 
 
160
  if (pipe (p) != 0)
 
161
    {
 
162
      g_message ("pipe() failed: %s", g_strerror (errno));
 
163
      g_free (tmpname);
 
164
      *status = GIMP_PDB_EXECUTION_ERROR;
 
165
      return -1;
 
166
    }
 
167
 
 
168
  if ((pid = fork()) < 0)
 
169
    {
 
170
      g_message ("fork() failed: %s", g_strerror (errno));
 
171
      g_free (tmpname);
 
172
      *status = GIMP_PDB_EXECUTION_ERROR;
 
173
      return -1;
 
174
    }
 
175
  else if (pid == 0)
 
176
    {
 
177
      close (p[0]);
 
178
      close (2);
 
179
      dup (p[1]);
 
180
      close (p[1]);
 
181
 
 
182
#ifdef HAVE_PUTENV
 
183
      /* produce deterministic output */
 
184
      putenv ("LANGUAGE=C");
 
185
      putenv ("LC_ALL=C");
 
186
      putenv ("LANG=C");
 
187
#endif
 
188
 
 
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));
 
192
      g_free (tmpname);
 
193
      _exit (127);
 
194
    }
 
195
  else
 
196
    {
 
197
      FILE     *input;
 
198
      gchar     buf[BUFSIZE];
 
199
      gboolean  seen_resolve = FALSE;
 
200
      gboolean  connected = FALSE;
 
201
      gboolean  redirect = FALSE;
 
202
      gboolean  file_found = FALSE;
 
203
      gchar     sizestr[32];
 
204
      gint      size = 0;
 
205
      gchar    *size_msg;
 
206
      gchar    *message;
 
207
      gint      i, j;
 
208
      gchar     dot;
 
209
      gint      kilobytes = 0;
 
210
      gboolean  finished = FALSE;
 
211
 
 
212
      gboolean  debug = FALSE;
 
213
 
 
214
#define DEBUG(x) if (debug) g_printerr (x)
 
215
 
 
216
      close (p[1]);
 
217
 
 
218
      input = fdopen (p[0], "r");
 
219
 
 
220
      /*  hardcoded and not-really-foolproof scanning of wget putput  */
 
221
 
 
222
    wget_begin:
 
223
      /*  Eat any Location lines */
 
224
      if (redirect && fgets (buf, BUFSIZE, input) == NULL)
 
225
        {
 
226
          g_message ("wget exited abnormally on URL '%s'", filename);
 
227
          g_free (tmpname);
 
228
          *status = GIMP_PDB_EXECUTION_ERROR;
 
229
          return -1;
 
230
        }
 
231
 
 
232
      redirect = FALSE;
 
233
 
 
234
      if (fgets (buf, BUFSIZE, input) == NULL)
 
235
        {
 
236
          /*  no message here because failing on the first line means
 
237
           *  that wget was not found
 
238
           */
 
239
          g_free (tmpname);
 
240
          *status = GIMP_PDB_EXECUTION_ERROR;
 
241
          return -1;
 
242
        }
 
243
 
 
244
      DEBUG (buf);
 
245
 
 
246
      /*  The second line is the local copy of the file  */
 
247
      if (fgets (buf, BUFSIZE, input) == NULL)
 
248
        {
 
249
          g_message ("wget exited abnormally on URL '%s'", filename);
 
250
          g_free (tmpname);
 
251
          *status = GIMP_PDB_EXECUTION_ERROR;
 
252
          return -1;
 
253
        }
 
254
 
 
255
      DEBUG (buf);
 
256
 
 
257
      /*  The third line is "Connecting to..."  */
 
258
      gimp_progress_init ("Connecting to server... "
 
259
                          "(timeout is "TIMEOUT" seconds)");
 
260
 
 
261
    read_connect:
 
262
      if (fgets (buf, BUFSIZE, input) == NULL)
 
263
        {
 
264
          g_message ("wget exited abnormally on URL '%s'", filename);
 
265
          g_free (tmpname);
 
266
          *status = GIMP_PDB_EXECUTION_ERROR;
 
267
          return -1;
 
268
        }
 
269
      else if (strstr (buf, "connected"))
 
270
        {
 
271
          connected = TRUE;
 
272
        }
 
273
      /* newer wgets have a "Resolving foo" line, so eat it */
 
274
      else if (!seen_resolve && strstr (buf, "Resolving"))
 
275
        {
 
276
          seen_resolve = TRUE;
 
277
          goto read_connect;
 
278
        }
 
279
 
 
280
      DEBUG (buf);
 
281
 
 
282
      /*  The fourth line is either the network request or an error  */
 
283
      gimp_progress_init ("Opening URL... "
 
284
                          "(timeout is "TIMEOUT" seconds)");
 
285
 
 
286
      if (fgets (buf, BUFSIZE, input) == NULL)
 
287
        {
 
288
          g_message ("wget exited abnormally on URL '%s'", filename);
 
289
          g_free (tmpname);
 
290
          *status = GIMP_PDB_EXECUTION_ERROR;
 
291
          return -1;
 
292
        }
 
293
      else if (! connected)
 
294
        {
 
295
          g_message ("A network error occured: %s", buf);
 
296
 
 
297
          DEBUG (buf);
 
298
 
 
299
          g_free (tmpname);
 
300
          *status = GIMP_PDB_EXECUTION_ERROR;
 
301
          return -1;
 
302
        }
 
303
      else if (strstr (buf, "302 Found"))
 
304
        {
 
305
          DEBUG (buf);
 
306
 
 
307
          connected = FALSE;
 
308
          seen_resolve = FALSE;
 
309
 
 
310
          redirect = TRUE;
 
311
          goto wget_begin;
 
312
        }
 
313
 
 
314
      DEBUG (buf);
 
315
 
 
316
      /*  The fifth line is either the length of the file or an error  */
 
317
      if (fgets (buf, BUFSIZE, input) == NULL)
 
318
        {
 
319
          g_message ("wget exited abnormally on URL '%s'", filename);
 
320
          g_free (tmpname);
 
321
          *status = GIMP_PDB_EXECUTION_ERROR;
 
322
          return -1;
 
323
        }
 
324
      else if (strstr (buf, "Length"))
 
325
        {
 
326
          file_found = TRUE;
 
327
        }
 
328
      else
 
329
        {
 
330
          g_message ("A network error occured: %s", buf);
 
331
 
 
332
          DEBUG (buf);
 
333
 
 
334
          g_free (tmpname);
 
335
          *status = GIMP_PDB_EXECUTION_ERROR;
 
336
          return -1;
 
337
        }
 
338
 
 
339
      DEBUG (buf);
 
340
 
 
341
      if (sscanf (buf, "Length: %31s", sizestr) != 1)
 
342
        {
 
343
          g_message ("Could not parse wget's file length message");
 
344
          g_free (tmpname);
 
345
          *status = GIMP_PDB_EXECUTION_ERROR;
 
346
          return -1;
 
347
        }
 
348
 
 
349
      /*  strip away commas  */
 
350
      for (i = 0, j = 0; i < sizeof (sizestr); i++, j++)
 
351
        {
 
352
          if (sizestr[i] == ',')
 
353
            i++;
 
354
 
 
355
          sizestr[j] = sizestr[i];
 
356
 
 
357
          if (sizestr[j] == '\0')
 
358
            break;
 
359
        }
 
360
 
 
361
      size = atoi (sizestr);
 
362
 
 
363
      if (size > 0)
 
364
        size_msg = g_strdup_printf ("%d bytes", size);
 
365
      else
 
366
        size_msg = g_strdup ("unknown amount");
 
367
 
 
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);
 
372
      g_free (message);
 
373
      g_free (size_msg);
 
374
 
 
375
      /*  Switch to byte parsing wget's output...  */
 
376
 
 
377
      while (1)
 
378
        {
 
379
          dot = fgetc (input);
 
380
 
 
381
          if (feof (input))
 
382
            break;
 
383
 
 
384
          if (debug)
 
385
            {
 
386
              fputc (dot, stderr);
 
387
              fflush (stderr);
 
388
            }
 
389
 
 
390
          if (dot == '.')  /* one kilobyte */
 
391
            {
 
392
              kilobytes++;
 
393
 
 
394
              if (size > 0)
 
395
                gimp_progress_update ((gdouble) (kilobytes * 1024) /
 
396
                                      (gdouble) size);
 
397
            }
 
398
          else if (dot == ':')  /* the time string contains a ':' */
 
399
            {
 
400
              fgets (buf, BUFSIZE, input);
 
401
 
 
402
              DEBUG (buf);
 
403
 
 
404
              if (! strstr (buf, "error"))
 
405
                {
 
406
                  finished = TRUE;
 
407
                  gimp_progress_update (1.0);
 
408
                }
 
409
 
 
410
              break;
 
411
            }
 
412
        }
 
413
 
 
414
      if (! finished)
 
415
        {
 
416
          g_message ("wget exited before finishing downloading URL\n'%s'",
 
417
                     filename);
 
418
          unlink (tmpname);
 
419
          g_free (tmpname);
 
420
          *status = GIMP_PDB_EXECUTION_ERROR;
 
421
          return -1;
 
422
        }
 
423
    }
 
424
 
 
425
  image_ID = gimp_file_load (run_mode, tmpname, tmpname);
 
426
 
 
427
  unlink (tmpname);
 
428
  g_free (tmpname);
 
429
 
 
430
  if (image_ID != -1)
 
431
    {
 
432
      *status = GIMP_PDB_SUCCESS;
 
433
      if (name_image)
 
434
        gimp_image_set_filename (image_ID, filename);
 
435
      else
 
436
        gimp_image_set_filename (image_ID, "");
 
437
    }
 
438
  else
 
439
    *status = GIMP_PDB_EXECUTION_ERROR;
 
440
 
 
441
  return image_ID;
 
442
}