home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libmime / mimemapl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.6 KB  |  194 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. /* mimemapl.c --- definition of the MimeMultipartAppleDouble class (see mimei.h)
  20.    Created: Jamie Zawinski <jwz@netscape.com>, 15-May-96.
  21.  */
  22.  
  23. #include "mimemapl.h"
  24. #include "xpgetstr.h"
  25.  
  26. #define MIME_SUPERCLASS mimeMultipartClass
  27. MimeDefClass(MimeMultipartAppleDouble, MimeMultipartAppleDoubleClass,
  28.              mimeMultipartAppleDoubleClass, &MIME_SUPERCLASS);
  29.  
  30. extern int MK_MSG_ATTACHMENT;
  31.  
  32. static int MimeMultipartAppleDouble_parse_begin (MimeObject *);
  33. static XP_Bool MimeMultipartAppleDouble_output_child_p(MimeObject *,
  34.                                                        MimeObject *);
  35.  
  36. static int
  37. MimeMultipartAppleDoubleClassInitialize(MimeMultipartAppleDoubleClass *class)
  38. {
  39.   MimeObjectClass    *oclass = (MimeObjectClass *)    class;
  40.   MimeMultipartClass *mclass = (MimeMultipartClass *) class;
  41.  
  42.   XP_ASSERT(!oclass->class_initialized);
  43.   oclass->parse_begin    = MimeMultipartAppleDouble_parse_begin;
  44.   mclass->output_child_p = MimeMultipartAppleDouble_output_child_p;
  45.   return 0;
  46. }
  47.  
  48. static int
  49. MimeMultipartAppleDouble_parse_begin (MimeObject *obj)
  50. {
  51.   /* #### This method is identical to MimeExternalObject_parse_begin
  52.      which kinda s#$%s...
  53.    */
  54.   int status;
  55.  
  56.   status = ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_begin(obj);
  57.   if (status < 0) return status;
  58.  
  59.   /* If we're writing this object, and we're doing it in raw form, then
  60.      now is the time to inform the backend what the type of this data is.
  61.    */
  62.   if (obj->output_p &&
  63.       obj->options &&
  64.       !obj->options->write_html_p &&
  65.       !obj->options->state->first_data_written_p)
  66.     {
  67.       status = MimeObject_output_init(obj, 0);
  68.       if (status < 0) return status;
  69.       XP_ASSERT(obj->options->state->first_data_written_p);
  70.     }
  71.  
  72. #if !defined(XP_MAC) && !defined(MOZILLA_30)
  73.   if (obj->options && obj->options->state) 
  74.   {
  75.     obj->options->state->separator_suppressed_p = TRUE;
  76.     goto done;
  77.   }
  78.   /*
  79.    * It would be nice to not showing the resource fork links
  80.    * if we are displaying inline. But, there is no way we could
  81.    * know ahead of time that we could display the data fork and
  82.    * the data fork is always hidden on MAC platform.
  83.    */
  84. #endif
  85.   /* If we're writing this object as HTML, then emit a link for the
  86.      multipart/appledouble part (both links) that looks just like the
  87.      links that MimeExternalObject emits for external leaf parts.
  88.    */
  89.   if (obj->options &&
  90.       obj->output_p &&
  91.       obj->options->write_html_p &&
  92.       obj->options->output_fn)
  93.     {
  94.       MimeDisplayOptions newopt = *obj->options;  /* copy it */
  95.       char *id = 0;
  96.       char *id_url = 0;
  97.       XP_Bool all_headers_p = obj->options->headers == MimeHeadersAll;
  98.  
  99.       id = mime_part_address (obj);
  100.       if (! id) return MK_OUT_OF_MEMORY;
  101.  
  102.       if (obj->options && obj->options->url)
  103.         {
  104.           const char *url = obj->options->url;
  105.           id_url = mime_set_url_part(url, id, TRUE);
  106.           if (!id_url)
  107.             {
  108.               XP_FREE(id);
  109.               return MK_OUT_OF_MEMORY;
  110.             }
  111.         }
  112.  
  113.       if (!XP_STRCMP (id, "0"))
  114.         {
  115.           XP_FREE(id);
  116.           id = XP_STRDUP(XP_GetString(MK_MSG_ATTACHMENT));
  117.         }
  118.       else
  119.         {
  120.           const char *p = "Part ";  /* #### i18n */
  121.           char *s = (char *)XP_ALLOC(XP_STRLEN(p) + XP_STRLEN(id) + 1);
  122.           if (!s)
  123.             {
  124.               XP_FREE(id);
  125.               XP_FREE(id_url);
  126.               return MK_OUT_OF_MEMORY;
  127.             }
  128.           XP_STRCPY(s, p);
  129.           XP_STRCAT(s, id);
  130.           XP_FREE(id);
  131.           id = s;
  132.         }
  133.  
  134.       if (all_headers_p &&
  135.           /* Don't bother showing all headers on this part if it's the only
  136.              part in the message: in that case, we've already shown these
  137.              headers. */
  138.           obj->options->state &&
  139.           obj->options->state->root == obj->parent)
  140.         all_headers_p = FALSE;
  141.  
  142.       newopt.fancy_headers_p = TRUE;
  143.       newopt.headers = (all_headers_p ? MimeHeadersAll : MimeHeadersSome);
  144.  
  145.       {
  146.         char p[] = "<P>";
  147.         status = MimeObject_write(obj, p, 3, FALSE);
  148.         if (status < 0) goto FAIL;
  149.       }
  150.  
  151.       status = MimeHeaders_write_attachment_box (obj->headers, &newopt,
  152.                                                  obj->content_type,
  153.                                                  obj->encoding,
  154.                                                  id, id_url, 0);
  155.       if (status < 0) goto FAIL;
  156.  
  157.       /* No <P> after the first attachment-box in an AppleDouble, to keep
  158.          them closer together. */
  159.  
  160.     FAIL:
  161.       FREEIF(id);
  162.       FREEIF(id_url);
  163.       if (status < 0) return status;
  164.     }
  165.  
  166. #if !defined(XP_MAC) && !defined(MOZILLA_30)
  167. done:
  168. #endif
  169.  
  170.   return 0;
  171. }
  172.  
  173. static XP_Bool
  174. MimeMultipartAppleDouble_output_child_p(MimeObject *obj, MimeObject *child)
  175. {
  176.   MimeContainer *cont = (MimeContainer *) obj;
  177.  
  178.   /* If this is the first child, and it's an application/applefile, then
  179.      don't emit a link for it.  (There *should* be only two children, and
  180.      the first one should always be an application/applefile.)
  181.    */
  182.  
  183.   if (obj->output_p &&
  184.       obj->options &&
  185.       obj->options->write_html_p &&
  186.       cont->nchildren >= 1 &&
  187.       cont->children[0] == child &&
  188.       child->content_type &&
  189.       !strcasecomp(child->content_type, APPLICATION_APPLEFILE))
  190.     return FALSE;
  191.   else
  192.     return TRUE;
  193. }
  194.