home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libimg / src / external.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.4 KB  |  220 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.  
  20. #include "if.h"
  21. extern PRBool
  22. il_load_image(void *cx, char *image_url, NET_ReloadMethod cache_reload_policy);
  23.  
  24. #include "merrors.h"
  25. /* for XP_GetString() */
  26. #include "xpgetstr.h"
  27.  
  28.  
  29. #include "il_strm.h"            /* Stream converters. */
  30.  
  31. static unsigned int
  32. il_view_write_ready(NET_StreamClass *stream)
  33. {
  34.     il_container *ic = (il_container *)stream->data_object;    
  35.  
  36.     /* For some reason, the imagelib can't deliver the image.
  37.        Trigger il_view_write(), which will abort the stream. */
  38.  
  39.     /* This originally returned (ic!=0) to trigger il_view_write()
  40.     which would set the read_size to something reasonable. But in mkmailbox.c
  41.     ReadMessageSock() uses the return value of one as the read_size and does not
  42.     call il_view_write() until it has filled up the buffer one byte
  43.     at a time. This should be addressed correctly in later versions. */
  44.  
  45.     return (ic != 0) * MAX_WRITE_READY;
  46. }
  47.  
  48. /* Abort the stream if we get this far */
  49. static int
  50. il_view_write(NET_StreamClass *stream, const unsigned char *str, int32 len)
  51. {
  52.     void *dobj=stream->data_object;    
  53.     /* If this assert fires, chances are that the provided URL was malformed.*/
  54.     XP_ASSERT(dobj == (void*)1);
  55.  
  56.      /* Should be MK_DATA_LOADED, but netlib ignores that. */
  57.     return MK_INTERRUPTED;
  58. }
  59.  
  60. static void
  61. il_view_complete(NET_StreamClass *stream)  
  62. {    
  63. }
  64.  
  65. static void 
  66. il_view_abort(NET_StreamClass *stream, int status)
  67. {    
  68. }
  69.  
  70. /* there can be only one, highlander */
  71. static IL_Stream *unconnected_stream = 0;
  72. static IL_URL *unconnected_urls = 0;
  73.  
  74. void
  75. il_reconnect(il_container *ic)
  76. {
  77.     if (unconnected_stream)
  78.     {
  79.  
  80.         unconnected_stream->complete       = il_stream_complete;
  81.         unconnected_stream->abort          = il_abort;
  82.         unconnected_stream->is_write_ready = il_write_ready;
  83.         unconnected_stream->data_object    = (void *)ic;
  84.         unconnected_stream->put_block      = (MKStreamWriteFunc)il_first_write;
  85.  
  86.         ic->type = IL_UNKNOWN;
  87.         ic->stream = unconnected_stream;
  88.         ic->state = IC_STREAM;
  89.  
  90.         unconnected_urls->fe_data = ic;    /* ugh */
  91.         ic->content_length = unconnected_urls->content_length;
  92.  
  93.         unconnected_stream = 0;
  94.         unconnected_urls = 0;
  95.     }
  96. }
  97.  
  98. /* We aren't going to reconnect after all.  Cause the stream to abort */
  99. void
  100. il_abort_reconnect()
  101. {
  102.     if (unconnected_stream) {
  103.         unconnected_stream->data_object = (void *)1;
  104.  
  105.     unconnected_stream = 0;
  106.     unconnected_urls = 0;
  107.     }
  108. }
  109.  
  110. static char fakehtml[] = "<IMG SRC=\"%s\">";
  111.  
  112. NET_StreamClass *
  113. IL_ViewStream(FO_Present_Types format_out, void *newshack, URL_Struct *urls,
  114.               OPAQUE_CONTEXT *cx)
  115. {
  116.     IL_Stream *stream = nil, *viewstream;
  117.     il_container *ic = nil;
  118.     char *org_content_type;
  119.     char *image_url;
  120.  
  121.     /* multi-part reconnect hack */
  122.  
  123.     ic = (il_container*)urls->fe_data;
  124.     if(ic && ic->multi)
  125.     {
  126.         return IL_NewStream(format_out, IL_UNKNOWN, urls, cx);
  127.     }
  128.  
  129.     /* Create stream object */
  130.     if (!(stream = XP_NEW_ZAP(NET_StreamClass))) {
  131.         XP_TRACE(("il: IL_ViewStream memory lossage"));
  132.         return 0;
  133.     }
  134.     
  135.     stream->name           = "image view";
  136.     stream->complete       = il_view_complete;
  137.     stream->abort          = il_view_abort;
  138.     stream->is_write_ready = il_view_write_ready;
  139.     stream->data_object    = NULL;
  140.     stream->window_id      = cx;
  141.     stream->put_block      = (MKStreamWriteFunc)il_view_write;
  142.  
  143.     ILTRACE(0,("il: new view stream, %s", urls->address));
  144.  
  145.     XP_ASSERT(!unconnected_stream);
  146.     unconnected_stream = stream;
  147.     unconnected_urls = urls;
  148.  
  149.     if(!newshack)
  150.     {
  151.         char *buffer;
  152.  
  153.         org_content_type = urls->content_type; 
  154.         urls->content_type = 0;
  155.         StrAllocCopy(urls->content_type, TEXT_HTML);
  156.         urls->is_binary = 1;    /* secret flag for mail-to save as */
  157.  
  158.         /* Force layout to discard the old document and start a new one.
  159.            We do this so that the pre-fetched image request won't be
  160.            destroyed by a layout call to IL_DestroyImageGroup. */
  161.  
  162.         viewstream = NET_StreamBuilder(format_out, urls, cx);
  163.         if (!viewstream) {
  164.             XP_FREE(stream);
  165.             return NULL;
  166.         }
  167.         buffer = XP_STRDUP("<HTML>");
  168.         if (!buffer) {
  169.             XP_FREE(stream);
  170.             XP_FREE(viewstream);
  171.             return NULL;
  172.         }
  173.         (*viewstream->put_block)(viewstream, buffer,
  174.                                  XP_STRLEN(buffer)+1);
  175.         XP_FREE(buffer);
  176.  
  177.     } /* !newshack */
  178.  
  179.     /* Prefetch the image.  We do this so that the image library can
  180.        process image data even if the parser is blocked on the fake IMG
  181.        tag that we send.  Note that this image request will persist until
  182.        the document is destroyed (when IL_DestroyImageGroup will be called.) */
  183.     image_url = (char*) XP_ALLOC(XP_STRLEN(urls->address) + 29);
  184.     if (!image_url) {
  185.         XP_FREE(stream);
  186.         XP_FREE(viewstream);
  187.         return NULL;
  188.     }
  189.     XP_SPRINTF(image_url, "internal-external-reconnect:%s", urls->address);
  190.     if (!il_load_image(cx, image_url, urls->force_reload)) {
  191.         XP_FREE(stream);
  192.         XP_FREE(viewstream);
  193.         return NULL;
  194.     }
  195.     XP_FREE(image_url);
  196.  
  197.     if (!newshack) {
  198.         if (viewstream) {
  199.                 char *buffer = (char*)
  200.                 XP_ALLOC(XP_STRLEN(fakehtml) + XP_STRLEN(urls->address) + 1);
  201.  
  202.             if (buffer)
  203.             {
  204.                 XP_SPRINTF(buffer, fakehtml, urls->address);
  205.                 (*viewstream->put_block)(viewstream,
  206.                                          buffer, XP_STRLEN(buffer));
  207.                 XP_FREE(buffer);
  208.             }
  209.             (*viewstream->complete)(viewstream);
  210.         }
  211.  
  212.         /* this has to be set back for abort to work correctly */
  213.         XP_FREE(urls->content_type);
  214.         urls->content_type = org_content_type;
  215.     } /* !newshack */
  216.  
  217.     return stream;
  218. }
  219.  
  220.