Back to index

lightning-sunbird  0.9+nobinonly
mimeeobj.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsCOMPtr.h"
00039 #include "mimeeobj.h"
00040 #include "prmem.h"
00041 #include "plstr.h"
00042 #include "prlog.h"
00043 #include "nsMimeStringResources.h"
00044 #include "mimemoz2.h"
00045 #include "nsCRT.h"
00046 #include "mimemapl.h"
00047 #include "nsMimeTypes.h"
00048 
00049 
00050 #define MIME_SUPERCLASS mimeLeafClass
00051 MimeDefClass(MimeExternalObject, MimeExternalObjectClass,
00052                       mimeExternalObjectClass, &MIME_SUPERCLASS);
00053 
00054 static int MimeExternalObject_initialize (MimeObject *);
00055 static void MimeExternalObject_finalize (MimeObject *);
00056 static int MimeExternalObject_parse_begin (MimeObject *);
00057 static int MimeExternalObject_parse_buffer (const char *, PRInt32, MimeObject *);
00058 static int MimeExternalObject_parse_line (char *, PRInt32, MimeObject *);
00059 static int MimeExternalObject_parse_decoded_buffer (const char*, PRInt32, MimeObject*);
00060 static PRBool MimeExternalObject_displayable_inline_p (MimeObjectClass *clazz,
00061                                                                                                   MimeHeaders *hdrs);
00062 
00063 static int
00064 MimeExternalObjectClassInitialize(MimeExternalObjectClass *clazz)
00065 {
00066   MimeObjectClass *oclass = (MimeObjectClass *) clazz;
00067   MimeLeafClass   *lclass = (MimeLeafClass *) clazz;
00068 
00069   NS_ASSERTION(!oclass->class_initialized, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
00070   oclass->initialize   = MimeExternalObject_initialize;
00071   oclass->finalize     = MimeExternalObject_finalize;
00072   oclass->parse_begin  = MimeExternalObject_parse_begin;
00073   oclass->parse_buffer = MimeExternalObject_parse_buffer;
00074   oclass->parse_line   = MimeExternalObject_parse_line;
00075   oclass->displayable_inline_p = MimeExternalObject_displayable_inline_p;
00076   lclass->parse_decoded_buffer = MimeExternalObject_parse_decoded_buffer;
00077   return 0;
00078 }
00079 
00080 
00081 static int
00082 MimeExternalObject_initialize (MimeObject *object)
00083 {
00084   return ((MimeObjectClass*)&MIME_SUPERCLASS)->initialize(object);
00085 }
00086 
00087 static void
00088 MimeExternalObject_finalize (MimeObject *object)
00089 {
00090   ((MimeObjectClass*)&MIME_SUPERCLASS)->finalize(object);
00091 }
00092 
00093 
00094 static int
00095 MimeExternalObject_parse_begin (MimeObject *obj)
00096 {
00097   int status;
00098   
00099   status = ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_begin(obj);
00100   if (status < 0) return status;
00101   
00102   // If we're writing this object, and we're doing it in raw form, then
00103   // now is the time to inform the backend what the type of this data is.
00104   //
00105   if (obj->output_p &&
00106     obj->options &&
00107     !obj->options->write_html_p &&
00108     !obj->options->state->first_data_written_p)
00109   {
00110     status = MimeObject_output_init(obj, 0);
00111     if (status < 0) return status;
00112     NS_ASSERTION(obj->options->state->first_data_written_p, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
00113   }
00114     
00115   //
00116   // If we're writing this object as HTML, do all the work now -- just write
00117   // out a table with a link in it.  (Later calls to the `parse_buffer' method
00118   // will simply discard the data of the object itself.)
00119   //
00120   if (obj->options &&
00121       obj->output_p &&
00122       obj->options->write_html_p &&
00123       obj->options->output_fn)
00124   {
00125     MimeDisplayOptions newopt = *obj->options;  // copy it 
00126     char *id = 0;
00127     char *id_url = 0;
00128     char *id_name = 0;
00129     nsXPIDLCString id_imap;
00130     PRBool all_headers_p = obj->options->headers == MimeHeadersAll;
00131     
00132     id = mime_part_address (obj);
00133     if (obj->options->missing_parts)
00134       id_imap.Adopt(mime_imap_part_address (obj));
00135     if (! id) return MIME_OUT_OF_MEMORY;
00136     
00137     if (obj->options && obj->options->url)
00138     {
00139       const char *url = obj->options->url;
00140       if (id_imap && id)
00141       {
00142         // if this is an IMAP part. 
00143         id_url = mime_set_url_imap_part(url, id_imap.get(), id);
00144       }
00145       else
00146       {
00147         // This is just a normal MIME part as usual. 
00148         id_url = mime_set_url_part(url, id, PR_TRUE);
00149       }
00150       if (!id_url)
00151       {
00152         PR_Free(id);
00153         return MIME_OUT_OF_MEMORY;
00154       }
00155     }
00156     if (!nsCRT::strcmp (id, "0"))
00157     {
00158       PR_Free(id);
00159       id = MimeGetStringByID(MIME_MSG_ATTACHMENT);
00160     }
00161     else
00162     {
00163       const char *p = "Part ";  
00164       PRUint32 slen = strlen(p) + strlen(id) + 1;
00165       char *s = (char *)PR_MALLOC(slen);
00166       if (!s)
00167       {
00168         PR_Free(id);
00169         PR_Free(id_url);
00170         return MIME_OUT_OF_MEMORY;
00171       }
00172       // we have a valid id
00173       if (id)
00174         id_name = mime_find_suggested_name_of_part(id, obj);
00175       PL_strncpyz(s, p, slen);
00176       PL_strcatn(s, slen, id);
00177       PR_Free(id);
00178       id = s;
00179     }
00180     
00181     if (all_headers_p &&
00182     // Don't bother showing all headers on this part if it's the only
00183     // part in the message: in that case, we've already shown these
00184     // headers.
00185     obj->options->state &&
00186     obj->options->state->root == obj->parent)
00187     all_headers_p = PR_FALSE;
00188     
00189     newopt.fancy_headers_p = PR_TRUE;
00190     newopt.headers = (all_headers_p ? MimeHeadersAll : MimeHeadersSome);    
00191 
00192 /******    
00193 RICHIE SHERRY
00194 GOTTA STILL DO THIS FOR QUOTING!
00195      status = MimeHeaders_write_attachment_box (obj->headers, &newopt,
00196                                                  obj->content_type,
00197                                                  obj->encoding,
00198                                                  id_name? id_name : id, id_url, 0)
00199 *****/
00200 
00201     // obj->options really owns the storage for this.
00202     newopt.part_to_load = nsnull;
00203     newopt.default_charset = nsnull;
00204     PR_FREEIF(id);
00205     PR_FREEIF(id_url);
00206     PR_FREEIF(id_name);
00207     if (status < 0) return status;
00208   }
00209   
00210   return 0;
00211 }
00212 
00213 static int
00214 MimeExternalObject_parse_buffer (const char *buffer, PRInt32 size, MimeObject *obj)
00215 {
00216   NS_ASSERTION(!obj->closed_p, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
00217   if (obj->closed_p) return -1;
00218 
00219   if (obj->output_p &&
00220          obj->options &&
00221          !obj->options->write_html_p)
00222        {
00223          /* The data will be base64-decoded and passed to
00224                MimeExternalObject_parse_decoded_buffer. */
00225          return ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_buffer(buffer, size,
00226                                                                                                                 obj);
00227        }
00228   else
00229        {
00230          /* Otherwise, simply ignore the data. */
00231          return 0;
00232        }
00233 }
00234 
00235 
00236 static int
00237 MimeExternalObject_parse_decoded_buffer (const char *buf, PRInt32 size,
00238                                                                        MimeObject *obj)
00239 {
00240   /* This is called (by MimeLeafClass->parse_buffer) with blocks of data
00241         that have already been base64-decoded.  This will only be called in
00242         the case where we're not emitting HTML, and want access to the raw
00243         data itself.
00244 
00245         We override the `parse_decoded_buffer' method provided by MimeLeaf
00246         because, unlike most children of MimeLeaf, we do not want to line-
00247         buffer the decoded data -- we want to simply pass it along to the
00248         backend, without going through our `parse_line' method.
00249    */
00250   if (!obj->output_p ||
00251          !obj->options ||
00252          obj->options->write_html_p)
00253        {
00254          NS_ASSERTION(0, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
00255          return -1;
00256        }
00257 
00258   return MimeObject_write(obj, buf, size, PR_TRUE);
00259 }
00260 
00261 
00262 static int
00263 MimeExternalObject_parse_line (char *line, PRInt32 length, MimeObject *obj)
00264 {
00265   /* This method should never be called (externals do no line buffering).
00266    */
00267   NS_ASSERTION(0, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
00268   return -1;
00269 }
00270 
00271 static PRBool
00272 MimeExternalObject_displayable_inline_p (MimeObjectClass *clazz,
00273                                                                        MimeHeaders *hdrs)
00274 {
00275   return PR_FALSE;
00276 }