Back to index

lightning-sunbird  0.9+nobinonly
nsSMIMEStub.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 #include "nsSMIMEStub.h"
00038 #include "prlog.h"
00039 #include "prmem.h"
00040 #include "plstr.h"
00041 #include "mimexpcom.h"
00042 #include "mimecth.h"
00043 #include "mimeobj.h"
00044 #include "nsCRT.h"
00045 #include "nsReadableUtils.h"
00046 
00047 // String bundles...
00048 #include "nsIStringBundle.h"
00049 
00050 #define SMIME_PROPERTIES_URL          "chrome://messenger/locale/smime.properties"
00051 #define SMIME_STR_NOT_SUPPORTED_ID    1000
00052 
00053 #ifndef XP_MAC
00054 static nsCOMPtr<nsIStringBundle> stringBundle = nsnull;
00055 #endif
00056 
00057 static char *SMimeGetStringByID(PRInt32 aMsgId)
00058 {
00059   char          *tempString = nsnull;
00060        nsresult res = NS_OK;
00061 
00062 #ifdef XP_MAC
00063 nsCOMPtr<nsIStringBundle> stringBundle = nsnull;
00064 #endif
00065 
00066        if (!stringBundle)
00067        {
00068               static const char propertyURL[] = SMIME_PROPERTIES_URL;
00069 
00070               nsCOMPtr<nsIStringBundleService> sBundleService = 
00071                        do_GetService(NS_STRINGBUNDLE_CONTRACTID, &res); 
00072               if (NS_SUCCEEDED(res) && (nsnull != sBundleService)) 
00073               {
00074                      res = sBundleService->CreateBundle(propertyURL, getter_AddRefs(stringBundle));
00075               }
00076        }
00077 
00078        if (stringBundle)
00079        {
00080               PRUnichar *ptrv = nsnull;
00081               res = stringBundle->GetStringFromID(aMsgId, &ptrv);
00082 
00083               if (NS_FAILED(res)) 
00084       return nsCRT::strdup("???");
00085               else
00086     {
00087       nsAutoString v;
00088       v.Append(ptrv);
00089            PR_FREEIF(ptrv);
00090       tempString = ToNewUTF8String(v);
00091     }
00092        }
00093 
00094   if (!tempString)
00095     return nsCRT::strdup("???");
00096   else
00097     return tempString;
00098 }
00099 
00100 
00101 
00102 
00103 static int MimeInlineTextSMIMEStub_parse_line (char *, PRInt32, MimeObject *);
00104 static int MimeInlineTextSMIMEStub_parse_eof (MimeObject *, PRBool);
00105 static int MimeInlineTextSMIMEStub_parse_begin (MimeObject *obj);
00106 
00107  /* This is the object definition. Note: we will set the superclass 
00108     to NULL and manually set this on the class creation */
00109 
00110 MimeDefClass(MimeInlineTextSMIMEStub, MimeInlineTextSMIMEStubClass, mimeInlineTextSMIMEStubClass, NULL);  
00111 
00112 extern "C" MimeObjectClass *
00113 MIME_SMimeCreateContentTypeHandlerClass(const char *content_type, 
00114                                    contentTypeHandlerInitStruct *initStruct)
00115 {
00116   MimeObjectClass *clazz = (MimeObjectClass *)&mimeInlineTextSMIMEStubClass;
00117   /* 
00118    * Must set the superclass by hand.
00119    */
00120   if (!COM_GetmimeInlineTextClass())
00121     return NULL;
00122 
00123   clazz->superclass = (MimeObjectClass *)COM_GetmimeInlineTextClass();
00124   initStruct->force_inline_display = PR_TRUE;
00125   return clazz;
00126 }
00127 
00128 static int
00129 MimeInlineTextSMIMEStubClassInitialize(MimeInlineTextSMIMEStubClass *clazz)
00130 {
00131   MimeObjectClass *oclass = (MimeObjectClass *) clazz;
00132   NS_ASSERTION(!oclass->class_initialized, "1.1 <rhp@netscape.com> 28 Nov 1999 19:36");
00133   oclass->parse_begin = MimeInlineTextSMIMEStub_parse_begin;
00134   oclass->parse_line  = MimeInlineTextSMIMEStub_parse_line;
00135   oclass->parse_eof   = MimeInlineTextSMIMEStub_parse_eof;
00136 
00137   return 0;
00138 }
00139 
00140 int
00141 GenerateMessage(char** html) 
00142 {
00143   nsCString temp;
00144   temp.Append("<BR><text=\"#000000\" bgcolor=\"#FFFFFF\" link=\"#FF0000\" vlink=\"#800080\" alink=\"#0000FF\">");
00145   temp.Append("<center><table BORDER=1 ><tr><td><CENTER>");
00146 
00147   char *tString = SMimeGetStringByID(SMIME_STR_NOT_SUPPORTED_ID);
00148   temp.Append(tString);
00149   PR_FREEIF(tString);
00150 
00151   temp.Append("</CENTER></td></tr></table></center><BR>");
00152   *html = ToNewCString(temp);
00153   return 0;
00154 }
00155 
00156 static int
00157 MimeInlineTextSMIMEStub_parse_begin(MimeObject *obj)
00158 {
00159   MimeInlineTextSMIMEStubClass *clazz;
00160   int status = ((MimeObjectClass*)COM_GetmimeLeafClass())->parse_begin(obj);
00161   
00162   if (status < 0) 
00163     return status;
00164   
00165   if (!obj->output_p) 
00166     return 0;
00167   if (!obj->options || !obj->options->write_html_p) 
00168     return 0;
00169   
00170   /* This is a fine place to write out any HTML before the real meat begins. */  
00171 
00172   // Initialize the clazz variable...
00173   clazz = ((MimeInlineTextSMIMEStubClass *) obj->clazz);
00174   return 0;
00175 }
00176 
00177 static int
00178 MimeInlineTextSMIMEStub_parse_line(char *line, PRInt32 length, MimeObject *obj)
00179 {
00180  /* 
00181   * This routine gets fed each line of data, one at a time. We just buffer
00182   * it all up, to be dealt with all at once at the end. 
00183   */  
00184   if (!obj->output_p) 
00185     return 0;
00186   if (!obj->options || !obj->options->output_fn) 
00187     return 0;
00188 
00189   if (!obj->options->write_html_p) 
00190   {
00191     return COM_MimeObject_write(obj, line, length, PR_TRUE);
00192   }
00193   
00194   return 0;
00195 }
00196 
00197 static int
00198 MimeInlineTextSMIMEStub_parse_eof (MimeObject *obj, PRBool abort_p)
00199 {
00200   int status = 0;
00201   char* html = NULL;
00202   
00203   if (obj->closed_p) return 0;
00204 
00205   /* Run parent method first, to flush out any buffered data. */
00206   status = ((MimeObjectClass*)COM_GetmimeInlineTextClass())->parse_eof(obj, abort_p);
00207   if (status < 0) 
00208     return status;
00209 
00210   if (  (obj->options) && 
00211         ((obj->options->format_out == nsMimeOutput::nsMimeMessageQuoting) ||
00212          (obj->options->format_out == nsMimeOutput::nsMimeMessageBodyQuoting))
00213      )
00214     return 0;
00215 
00216   status = GenerateMessage(&html);
00217   if (status < 0) 
00218     return status;
00219   
00220   status = COM_MimeObject_write(obj, html, PL_strlen(html), PR_TRUE);
00221   PR_FREEIF(html);
00222   if (status < 0) 
00223     return status;
00224  
00225   return 0;
00226 }