home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libmime / mimeeobj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.5 KB  |  266 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /* mimeeobj.c --- definition of the MimeExternalObject class (see mimei.h)
  20.    Created: Jamie Zawinski <jwz@netscape.com>, 15-May-96.
  21.  */
  22.  
  23.  
  24. #include "mimeeobj.h"
  25. #include "xpgetstr.h"
  26.  
  27. #define MIME_SUPERCLASS mimeLeafClass
  28. MimeDefClass(MimeExternalObject, MimeExternalObjectClass,
  29.              mimeExternalObjectClass, &MIME_SUPERCLASS);
  30.  
  31. extern int MK_MSG_ATTACHMENT;
  32.  
  33. #if defined(XP_MAC) && !defined(MOZILLA_30)
  34. extern MimeObjectClass mimeMultipartAppleDoubleClass;
  35. #endif
  36.  
  37. static int MimeExternalObject_initialize (MimeObject *);
  38. static void MimeExternalObject_finalize (MimeObject *);
  39. static int MimeExternalObject_parse_begin (MimeObject *);
  40. static int MimeExternalObject_parse_buffer (char *, int32, MimeObject *);
  41. static int MimeExternalObject_parse_line (char *, int32, MimeObject *);
  42. static int MimeExternalObject_parse_decoded_buffer (char*, int32, MimeObject*);
  43. static XP_Bool MimeExternalObject_displayable_inline_p (MimeObjectClass *class,
  44.                                                         MimeHeaders *hdrs);
  45.  
  46. static int
  47. MimeExternalObjectClassInitialize(MimeExternalObjectClass *class)
  48. {
  49.   MimeObjectClass *oclass = (MimeObjectClass *) class;
  50.   MimeLeafClass   *lclass = (MimeLeafClass *) class;
  51.  
  52.   XP_ASSERT(!oclass->class_initialized);
  53.   oclass->initialize   = MimeExternalObject_initialize;
  54.   oclass->finalize     = MimeExternalObject_finalize;
  55.   oclass->parse_begin  = MimeExternalObject_parse_begin;
  56.   oclass->parse_buffer = MimeExternalObject_parse_buffer;
  57.   oclass->parse_line   = MimeExternalObject_parse_line;
  58.   oclass->displayable_inline_p = MimeExternalObject_displayable_inline_p;
  59.   lclass->parse_decoded_buffer = MimeExternalObject_parse_decoded_buffer;
  60.   return 0;
  61. }
  62.  
  63.  
  64. static int
  65. MimeExternalObject_initialize (MimeObject *object)
  66. {
  67.   return ((MimeObjectClass*)&MIME_SUPERCLASS)->initialize(object);
  68. }
  69.  
  70. static void
  71. MimeExternalObject_finalize (MimeObject *object)
  72. {
  73.   ((MimeObjectClass*)&MIME_SUPERCLASS)->finalize(object);
  74. }
  75.  
  76.  
  77. static int
  78. MimeExternalObject_parse_begin (MimeObject *obj)
  79. {
  80.   int status;
  81.  
  82.   status = ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_begin(obj);
  83.   if (status < 0) return status;
  84.  
  85. #if defined (XP_MAC) && !defined(MOZILLA_30)
  86.   if (obj->parent && mime_typep(obj->parent,
  87.       (MimeObjectClass *) &mimeMultipartAppleDoubleClass))
  88.       goto done;
  89. #endif /* defined (XP_MAC) && !defined(MOZILLA_30) */
  90.  
  91.   /* If we're writing this object, and we're doing it in raw form, then
  92.      now is the time to inform the backend what the type of this data is.
  93.    */
  94.   if (obj->output_p &&
  95.       obj->options &&
  96.       !obj->options->write_html_p &&
  97.       !obj->options->state->first_data_written_p)
  98.     {
  99.       status = MimeObject_output_init(obj, 0);
  100.       if (status < 0) return status;
  101.       XP_ASSERT(obj->options->state->first_data_written_p);
  102.     }
  103.  
  104.  
  105.   /* If we're writing this object as HTML, do all the work now -- just write
  106.      out a table with a link in it.  (Later calls to the `parse_buffer' method
  107.      will simply discard the data of the object itself.)
  108.    */
  109.   if (obj->options &&
  110.       obj->output_p &&
  111.       obj->options->write_html_p &&
  112.       obj->options->output_fn)
  113.     {
  114.       MimeDisplayOptions newopt = *obj->options;  /* copy it */
  115.       char *id = 0;
  116.       char *id_url = 0;
  117.       char *id_name = 0;
  118.       XP_Bool all_headers_p = obj->options->headers == MimeHeadersAll;
  119.  
  120.       id = mime_part_address (obj);
  121.       if (! id) return MK_OUT_OF_MEMORY;
  122.  
  123.       if (obj->options && obj->options->url)
  124.         {
  125.           const char *url = obj->options->url;
  126.           id_url = mime_set_url_part(url, id, TRUE);
  127.           if (!id_url)
  128.             {
  129.               XP_FREE(id);
  130.               return MK_OUT_OF_MEMORY;
  131.             }
  132.         }
  133.  
  134.       if (!XP_STRCMP (id, "0"))
  135.         {
  136.           XP_FREE(id);
  137.           id = XP_STRDUP(XP_GetString(MK_MSG_ATTACHMENT));
  138.         }
  139.       else
  140.         {
  141.           const char *p = "Part ";  /* #### i18n */
  142.           char *s = (char *)XP_ALLOC(XP_STRLEN(p) + XP_STRLEN(id) + 1);
  143.           if (!s)
  144.             {
  145.               XP_FREE(id);
  146.               XP_FREE(id_url);
  147.               return MK_OUT_OF_MEMORY;
  148.             }
  149.             /* we have a valid id */
  150.           if (id)
  151.               id_name = mime_find_suggested_name_of_part(id, obj);
  152.           XP_STRCPY(s, p);
  153.           XP_STRCAT(s, id);
  154.           XP_FREE(id);
  155.           id = s;
  156.         }
  157.  
  158.       if (all_headers_p &&
  159.           /* Don't bother showing all headers on this part if it's the only
  160.              part in the message: in that case, we've already shown these
  161.              headers. */
  162.           obj->options->state &&
  163.           obj->options->state->root == obj->parent)
  164.         all_headers_p = FALSE;
  165.  
  166.       newopt.fancy_headers_p = TRUE;
  167.       newopt.headers = (all_headers_p ? MimeHeadersAll : MimeHeadersSome);
  168.  
  169.       {
  170.         char p[] = "<P>";
  171.         status = MimeObject_write(obj, p, 3, FALSE);
  172.         if (status < 0) goto FAIL;
  173.       }
  174.  
  175.       status = MimeHeaders_write_attachment_box (obj->headers, &newopt,
  176.                                                  obj->content_type,
  177.                                                  obj->encoding,
  178.                                                  id_name? id_name : id, id_url, 0);
  179.       FREEIF(id_name);
  180.       if (status < 0) goto FAIL;
  181.  
  182.       {
  183.         char p[] = "<P>";
  184.         status = MimeObject_write(obj, p, 3, FALSE);
  185.         if (status < 0) goto FAIL;
  186.       }
  187.  
  188.     FAIL:
  189.       FREEIF(id);
  190.       FREEIF(id_url);
  191.         FREEIF(id_name);
  192.       if (status < 0) return status;
  193.     }
  194.  
  195. #if defined (XP_MAC) && !defined(MOZILLA_30)
  196. done:
  197. #endif /* defined (XP_MAC) && !defined(MOZILLA_30) */
  198.  
  199.   return 0;
  200. }
  201.  
  202. static int
  203. MimeExternalObject_parse_buffer (char *buffer, int32 size, MimeObject *obj)
  204. {
  205.   XP_ASSERT(!obj->closed_p);
  206.   if (obj->closed_p) return -1;
  207.  
  208.   if (obj->output_p &&
  209.       obj->options &&
  210.       !obj->options->write_html_p)
  211.     {
  212.       /* The data will be base64-decoded and passed to
  213.          MimeExternalObject_parse_decoded_buffer. */
  214.       return ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_buffer(buffer, size,
  215.                                                                 obj);
  216.     }
  217.   else
  218.     {
  219.       /* Otherwise, simply ignore the data. */
  220.       return 0;
  221.     }
  222. }
  223.  
  224.  
  225. static int
  226. MimeExternalObject_parse_decoded_buffer (char *buf, int32 size,
  227.                                          MimeObject *obj)
  228. {
  229.   /* This is called (by MimeLeafClass->parse_buffer) with blocks of data
  230.      that have already been base64-decoded.  This will only be called in
  231.      the case where we're not emitting HTML, and want access to the raw
  232.      data itself.
  233.  
  234.      We override the `parse_decoded_buffer' method provided by MimeLeaf
  235.      because, unlike most children of MimeLeaf, we do not want to line-
  236.      buffer the decoded data -- we want to simply pass it along to the
  237.      backend, without going through our `parse_line' method.
  238.    */
  239.   if (!obj->output_p ||
  240.       !obj->options ||
  241.       obj->options->write_html_p)
  242.     {
  243.       XP_ASSERT(0);
  244.       return -1;
  245.     }
  246.  
  247.   return MimeObject_write(obj, buf, size, TRUE);
  248. }
  249.  
  250.  
  251. static int
  252. MimeExternalObject_parse_line (char *line, int32 length, MimeObject *obj)
  253. {
  254.   /* This method should never be called (externals do no line buffering).
  255.    */
  256.   XP_ASSERT(0);
  257.   return -1;
  258. }
  259.  
  260. static XP_Bool
  261. MimeExternalObject_displayable_inline_p (MimeObjectClass *class,
  262.                                          MimeHeaders *hdrs)
  263. {
  264.   return FALSE;
  265. }
  266.