Back to index

lightning-sunbird  0.9+nobinonly
mimecal.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 the GNU General Public License Version 2 or later (the "GPL"), or
00026  * 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 #include "mimecal.h"
00038 #include "prmem.h"
00039 #include "plstr.h"
00040 #include "mimexpcom.h"
00041 #include "mimecth.h"
00042 #include "mimeobj.h"
00043 #include "nsCRT.h"
00044 
00045 static int MimeInlineTextCalendar_parse_line (char *, PRInt32, MimeObject *);
00046 static int MimeInlineTextCalendar_parse_eof (MimeObject *, PRBool);
00047 static int MimeInlineTextCalendar_parse_begin (MimeObject *obj);
00048 
00049 extern "C" int CAL_OUT_OF_MEMORY = -1000;
00050 
00051  /* This is the object definition. Note: we will set the superclass 
00052     to NULL and manually set this on the class creation */
00053 MimeDefClass(MimeInlineTextCalendar, MimeInlineTextCalendarClass,
00054              mimeInlineTextCalendarClass, NULL);  
00055 
00056 extern "C" MimeObjectClass *
00057 MIME_CalendarCreateContentTypeHandlerClass(const char *content_type, 
00058                                            contentTypeHandlerInitStruct *initStruct)
00059 {
00060   MimeObjectClass *clazz = (MimeObjectClass *)&mimeInlineTextCalendarClass;
00061   /*
00062    * Must set the superclass by hand.
00063    */
00064   if (!COM_GetmimeInlineTextClass())
00065     return NULL;
00066 
00067   clazz->superclass = (MimeObjectClass *)COM_GetmimeInlineTextClass();
00068   initStruct->force_inline_display = PR_TRUE;
00069   return clazz;
00070 }
00071 
00072 static int
00073 MimeInlineTextCalendarClassInitialize(MimeInlineTextCalendarClass *clazz)
00074 {
00075   MimeObjectClass *oclass = (MimeObjectClass *) clazz;
00076   NS_ASSERTION(!oclass->class_initialized, "1.1 <rhp@netscape.com> 19 Mar 1999 12:01");
00077   oclass->parse_begin = MimeInlineTextCalendar_parse_begin;
00078   oclass->parse_line  = MimeInlineTextCalendar_parse_line;
00079   oclass->parse_eof   = MimeInlineTextCalendar_parse_eof;
00080 
00081   return 0;
00082 }
00083 
00084 int
00085 mime_TranslateCalendar(char* caldata, char** html) 
00086 {
00087   *html = nsCRT::strdup("\
00088 <text=\"#000000\" bgcolor=\"#FFFFFF\" link=\"#FF0000\" vlink=\"#800080\" alink=\"#0000FF\">\
00089 <center><table BORDER=1 BGCOLOR=\"#CCFFFF\" ><tr>\
00090 <td>This libmime Content Type Handler plugin for the Content-Type <b>text/calendar\
00091 </b> is functional!</td>\
00092 </tr>\
00093 </table></center>");
00094 
00095   return 0;
00096 }
00097 
00098 static int
00099 MimeInlineTextCalendar_parse_begin(MimeObject *obj)
00100 {
00101   MimeInlineTextCalendarClass *clazz;
00102   int status = ((MimeObjectClass*)COM_GetmimeLeafClass())->parse_begin(obj);
00103   
00104   if (status < 0) return status;
00105   
00106   if (!obj->output_p) return 0;
00107   if (!obj->options || !obj->options->write_html_p) return 0;
00108   
00109   /* This is a fine place to write out any HTML before the real meat begins. */  
00110   clazz = ((MimeInlineTextCalendarClass *) obj->clazz);
00111   /* initialize buffered string to empty; */
00112   
00113   clazz->bufferlen = 0;
00114   clazz->buffermax = 512;
00115   clazz->buffer = (char*) PR_MALLOC(clazz->buffermax);
00116   if (clazz->buffer == NULL) return CAL_OUT_OF_MEMORY;
00117   return 0;
00118 }
00119 
00120 static int
00121 MimeInlineTextCalendar_parse_line(char *line, PRInt32 length, MimeObject *obj)
00122 {
00123  /* 
00124   * This routine gets fed each line of data, one at a time. We just buffer
00125   * it all up, to be dealt with all at once at the end. 
00126   */  
00127   MimeInlineTextCalendarClass *clazz = ((MimeInlineTextCalendarClass *) obj->clazz);
00128   
00129   if (!obj->output_p) return 0;
00130   if (!obj->options || !obj->options->output_fn) return 0;
00131   if (!obj->options->write_html_p) {
00132     return COM_MimeObject_write(obj, line, length, PR_TRUE);
00133   }
00134   
00135   if (clazz->bufferlen + length >= clazz->buffermax) {
00136     do {
00137       clazz->buffermax += 512;
00138     } while (clazz->bufferlen + length >= clazz->buffermax);
00139     clazz->buffer = (char *)PR_Realloc(clazz->buffer, clazz->buffermax);
00140     if (clazz->buffer == NULL) return CAL_OUT_OF_MEMORY;
00141   }
00142   memcpy(clazz->buffer + clazz->bufferlen, line, length);
00143   clazz->bufferlen += length;
00144   return 0;
00145 }
00146 
00147 static int
00148 MimeInlineTextCalendar_parse_eof (MimeObject *obj, PRBool abort_p)
00149 {
00150   int status = 0;
00151   MimeInlineTextCalendarClass *clazz = ((MimeInlineTextCalendarClass *) obj->clazz);
00152   char* html = NULL;
00153   
00154   if (obj->closed_p) return 0;
00155   
00156   /* Run parent method first, to flush out any buffered data. */
00157   status = ((MimeObjectClass*)COM_GetmimeInlineTextClass())->parse_eof(obj, abort_p);
00158   if (status < 0) return status;
00159   
00160   if (!clazz->buffer || clazz->bufferlen == 0) return 0;
00161   
00162   clazz->buffer[clazz->bufferlen] = '\0';
00163   
00164   status = mime_TranslateCalendar(clazz->buffer, &html);
00165   PR_Free(clazz->buffer);
00166   clazz->buffer = NULL;
00167   if (status < 0) return status;
00168   
00169   status = COM_MimeObject_write(obj, html, PL_strlen(html), PR_TRUE);
00170   PR_Free(html);
00171   if (status < 0) return status;
00172   
00173   return 0;
00174 }