Back to index

lightning-sunbird  0.9+nobinonly
nsSimpleMimeConverterStub.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 simple MIME converter stubs.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Oracle Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2005
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 
00038 #include "mimecth.h"
00039 #include "mimeobj.h"
00040 #include "mimetext.h"
00041 #include "mimemoz2.h"
00042 #include "mimecom.h"
00043 #include "nsString.h"
00044 #include "nsReadableUtils.h"
00045 #include "nsCategoryManagerUtils.h"
00046 #include "nsCRT.h"
00047 #include "nsCOMPtr.h"
00048 #include "nsAutoPtr.h"
00049 #include "nsISimpleMimeConverter.h"
00050 
00051 typedef struct MimeSimpleStub MimeSimpleStub;
00052 typedef struct MimeSimpleStubClass MimeSimpleStubClass;
00053 
00054 struct MimeSimpleStubClass {
00055     MimeInlineTextClass text;
00056 };
00057 
00058 struct MimeSimpleStub {
00059     MimeInlineText text;
00060     nsCString *buffer;
00061     nsCOMPtr<nsISimpleMimeConverter> innerScriptable;
00062 };
00063 
00064 MimeDefClass(MimeSimpleStub, MimeSimpleStubClass, mimeSimpleStubClass, NULL);
00065 
00066 static int
00067 BeginGather(MimeObject *obj)
00068 {
00069     MimeSimpleStub *ssobj = (MimeSimpleStub *)obj;
00070     int status = ((MimeObjectClass *)XPCOM_GetmimeLeafClass())->parse_begin(obj);
00071 
00072     if (status < 0)
00073         return status;
00074 
00075     if (!obj->output_p ||
00076         !obj->options ||
00077         !obj->options->write_html_p) {
00078         return 0;
00079     }
00080 
00081     ssobj->buffer->Truncate();
00082     return 0;
00083 }
00084 
00085 static int
00086 GatherLine(char *line, PRInt32 length, MimeObject *obj)
00087 {
00088     MimeSimpleStub *ssobj = (MimeSimpleStub *)obj;
00089 
00090     if (!obj->output_p ||
00091         !obj->options ||
00092         !obj->options->output_fn) {
00093         return 0;
00094     }
00095     
00096     if (!obj->options->write_html_p)
00097         return MimeObject_write(obj, line, length, PR_TRUE);
00098 
00099     ssobj->buffer->Append(line);
00100     return 0;
00101 }
00102 
00103 static int
00104 EndGather(MimeObject *obj, PRBool abort_p)
00105 {
00106     MimeSimpleStub *ssobj = (MimeSimpleStub *)obj;
00107 
00108     if (obj->closed_p)
00109         return 0;
00110     
00111     int status = ((MimeObjectClass *)MIME_GetmimeInlineTextClass())->parse_eof(obj, abort_p);
00112     if (status < 0)
00113         return status;
00114 
00115     if (ssobj->buffer->IsEmpty())
00116         return 0;
00117     
00118      mime_stream_data  *msd = (mime_stream_data *) (obj->options->stream_closure);
00119      nsIChannel *channel = msd->channel;  // note the lack of ref counting...
00120      if (channel)
00121      {
00122        nsCOMPtr<nsIURI> uri;
00123        channel->GetURI(getter_AddRefs(uri));
00124        ssobj->innerScriptable->SetUri(uri);
00125      }
00126     nsCString asHTML;
00127     nsresult rv = ssobj->innerScriptable->ConvertToHTML(nsDependentCString(obj->content_type),
00128                                                         *ssobj->buffer,
00129                                                         asHTML);
00130     if (NS_FAILED(rv)) {
00131         NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "converter failure");
00132         return -1;
00133     }
00134 
00135     // MimeObject_write wants a non-const string for some reason, but it doesn't mutate it
00136     status = MimeObject_write(obj, (char *)PromiseFlatCString(asHTML).get(),
00137                               asHTML.Length(), PR_TRUE);
00138     if (status < 0)
00139         return status;
00140     return 0;
00141 }
00142 
00143 static int
00144 Initialize(MimeObject *obj)
00145 {
00146     MimeSimpleStub *ssobj = (MimeSimpleStub *)obj;
00147     ssobj->innerScriptable =
00148         do_CreateInstanceFromCategory(NS_SIMPLEMIMECONVERTERS_CATEGORY, obj->content_type);
00149     if (!ssobj->innerScriptable)
00150         return -1;
00151     ssobj->buffer = new nsCString();
00152     int status = ((MimeObjectClass *)XPCOM_GetmimeLeafClass())->initialize(obj);
00153     return 0;
00154 }
00155 
00156 static void
00157 Finalize(MimeObject *obj)
00158 {
00159     MimeSimpleStub *ssobj = (MimeSimpleStub *)obj;
00160     ssobj->innerScriptable = nsnull;
00161     delete ssobj->buffer;
00162 }
00163 
00164 static int
00165 MimeSimpleStubClassInitialize(MimeSimpleStubClass *clazz)
00166 {
00167     MimeObjectClass *oclass = (MimeObjectClass *)clazz;
00168     oclass->parse_begin = BeginGather;
00169     oclass->parse_line = GatherLine;
00170     oclass->parse_eof = EndGather;
00171     oclass->initialize = Initialize;
00172     oclass->finalize = Finalize;
00173     return 0;
00174 }
00175 
00176 class nsSimpleMimeConverterStub : public nsIMimeContentTypeHandler
00177 {
00178 public:
00179     nsSimpleMimeConverterStub(const char *aContentType) : mContentType(aContentType) { }
00180     virtual ~nsSimpleMimeConverterStub() { }
00181 
00182     NS_DECL_ISUPPORTS
00183 
00184     NS_IMETHOD GetContentType(char **contentType)
00185     {
00186         *contentType = ToNewCString(mContentType);
00187         return *contentType ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00188     }
00189     NS_IMETHOD CreateContentTypeHandlerClass(const char *contentType,
00190                                              contentTypeHandlerInitStruct *initString,
00191                                              MimeObjectClass **objClass);
00192 private:
00193     nsCString mContentType;
00194 };
00195 
00196 NS_IMPL_ISUPPORTS1(nsSimpleMimeConverterStub, nsIMimeContentTypeHandler)
00197 
00198 NS_IMETHODIMP
00199 nsSimpleMimeConverterStub::CreateContentTypeHandlerClass(const char *contentType,
00200                                                      contentTypeHandlerInitStruct *initStruct,
00201                                                          MimeObjectClass **objClass)
00202 {
00203     *objClass = (MimeObjectClass *)&mimeSimpleStubClass;
00204     (*objClass)->superclass = (MimeObjectClass *)XPCOM_GetmimeInlineTextClass();
00205     if (!(*objClass)->superclass)
00206         return NS_ERROR_UNEXPECTED;;
00207 
00208     initStruct->force_inline_display = PR_TRUE;
00209     return NS_OK;;
00210 }
00211 
00212 nsresult
00213 MIME_NewSimpleMimeConverterStub(const char *aContentType,
00214                                 nsIMimeContentTypeHandler **aResult)
00215 {
00216     nsRefPtr<nsSimpleMimeConverterStub> inst = new nsSimpleMimeConverterStub(aContentType);
00217     if (!inst)
00218         return NS_ERROR_OUT_OF_MEMORY;
00219     return CallQueryInterface(inst.get(), aResult);
00220 }