home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / layout / layinfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  14.5 KB  |  628 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 "xp.h"
  21. #include "pa_parse.h"
  22. #include "layout.h"
  23. #include "laylayer.h"
  24. #include "layers.h"
  25.  
  26. /* for XP_GetString() */
  27. #include "xpgetstr.h"
  28. extern int LAY_PAGEINFO_NOINFO;
  29. extern int LAY_PAGEINFO_FRAME;
  30. extern int LAY_PAGEINFO_IMAGE;
  31. extern int LAY_PAGEINFO_EMBED;
  32. extern int LAY_PAGEINFO_APPLET;
  33. extern int LAY_PAGEINFO_BACKGROUND_IMAGE;
  34. extern int LAY_PAGEINFO_ACTIONURL;
  35. extern int LAY_PAGEINFO_ENCODING;
  36. extern int LAY_PAGEINFO_METHOD;
  37. extern int LAY_PAGEINFO_LAYER;
  38.  
  39. #define STREAM_WRITE(Str) \
  40.         (*stream->put_block)(stream, Str, XP_STRLEN(Str))
  41.  
  42. PRIVATE
  43. void
  44. lo_element_info(LO_Element *ele_list, NET_StreamClass *stream)
  45. {
  46.     LO_Element *eptr;
  47.     char buf[1024];
  48.     char *tmp_ptr;
  49.  
  50.     eptr = ele_list;
  51.     while (eptr != NULL)
  52.     {
  53.         switch(eptr->type)
  54.         {
  55.             case LO_IMAGE:
  56.                 {
  57.                     LO_ImageStruct *image;
  58.                     char *str;
  59.                     char *image_src;
  60.  
  61.                     image = (LO_ImageStruct *)eptr;
  62.                     /*
  63.                      * Extract the SRC of this image.
  64.                      */
  65.                     PA_LOCK(str, char *, image->image_url);
  66.                     if (str != NULL)
  67.                     {
  68.                         image_src = XP_STRDUP(str);
  69.                     }
  70.                     else
  71.                     {
  72.                         image_src = NULL;
  73.                     }
  74.  
  75.                     if(image_src 
  76.                         && XP_STRNCMP(image_src, "internal-external-reconnect", 27))
  77.                       {
  78.                         XP_STRCPY(buf,"<li>");
  79.                         XP_STRCAT(buf, XP_GetString(LAY_PAGEINFO_IMAGE));
  80.                         XP_STRCAT(buf," <A TARGET=Internal_URL_Info HREF=\"about:");
  81.                         STREAM_WRITE(buf);
  82.                         STREAM_WRITE(image_src);
  83.                         XP_STRCPY(buf,"\">");
  84.                         STREAM_WRITE(buf);
  85.                         tmp_ptr = NET_EscapeHTML(image_src);
  86.                         STREAM_WRITE(tmp_ptr);
  87.                         XP_FREE(tmp_ptr);
  88.                         XP_STRCPY(buf,"</A>");
  89.                         STREAM_WRITE(buf);
  90.                         XP_FREE(image_src);
  91.                       }
  92.                     PA_UNLOCK(image->image_url);
  93.                 }
  94.                 break;
  95.             case LO_EMBED:
  96.                 {
  97.                     LO_EmbedStruct *embed;
  98.                     char *str;
  99.                     char *embed_src;
  100.  
  101.                     embed = (LO_EmbedStruct *)eptr;
  102.                     /*
  103.                      * Extract the SRC of this embed.
  104.                      */
  105.                     PA_LOCK(str, char *, embed->embed_src);
  106.                     if (str != NULL)
  107.                     {
  108.                         embed_src = XP_STRDUP(str);
  109.                     }
  110.                     else
  111.                     {
  112.                         embed_src = NULL;
  113.                     }
  114.                     PA_UNLOCK(embed->embed_src);
  115.  
  116.                     if(embed_src)
  117.                       {
  118.                         XP_STRCPY(buf,"<li>");
  119.                         XP_STRCAT(buf, XP_GetString(LAY_PAGEINFO_EMBED));
  120.                         XP_STRCAT(buf," <A TARGET=Internal_URL_Info HREF=\"about:");
  121.                         STREAM_WRITE(buf);
  122.                         STREAM_WRITE(embed_src);
  123.                         XP_STRCPY(buf,"\">");
  124.                         STREAM_WRITE(buf);
  125.                         tmp_ptr = NET_EscapeHTML(embed_src);
  126.                         STREAM_WRITE(tmp_ptr);
  127.                         XP_FREE(tmp_ptr);
  128.                         XP_STRCPY(buf,"</A>");
  129.                         STREAM_WRITE(buf);
  130.                         XP_FREE(embed_src);
  131.                       }
  132.                 }
  133.                 break;
  134. #ifdef JAVA
  135.             case LO_JAVA:
  136.                 {
  137.                     LO_JavaAppStruct *java_app;
  138.                     char *str;
  139.                     char *java_code = NULL;
  140.                     char *java_codebase = NULL;
  141.                     char *java_archive = NULL;
  142.                     char *java_name = NULL;
  143.  
  144.                     java_app = (LO_JavaAppStruct *)eptr;
  145.                     /*
  146.                      * Extract the java class of this
  147.                      * java applet.
  148.                      */
  149.                     PA_LOCK(str, char *, java_app->attr_code);
  150.                     if (str != NULL)
  151.                     {
  152.                         java_code = XP_STRDUP(str);
  153.                     }
  154.                     PA_UNLOCK(java_app->attr_code);
  155.  
  156.                     /*
  157.                      * Extract the codebase of this
  158.                      * java applet.
  159.                      */
  160.                     PA_LOCK(str, char *, java_app->attr_codebase);
  161.                     if (str != NULL)
  162.                     {
  163.                         java_codebase = XP_STRDUP(str);
  164.                     }
  165.                     PA_UNLOCK(java_app->attr_codebase);
  166.  
  167.                     /*
  168.                      * Extract the archive of this
  169.                      * java applet.
  170.                      */
  171.                     PA_LOCK(str, char *, java_app->attr_archive);
  172.                     if (str != NULL)
  173.                     {
  174.                         java_archive = XP_STRDUP(str);
  175.                     }
  176.                     PA_UNLOCK(java_app->attr_archive);
  177.  
  178.                     /*
  179.                      * Extract the app name of this
  180.                      * java applet.
  181.                      */
  182.                     PA_LOCK(str, char *, java_app->attr_name);
  183.                     if (str != NULL)
  184.                     {
  185.                         java_name = XP_STRDUP(str);
  186.                     }
  187.                     PA_UNLOCK(java_app->attr_name);
  188.  
  189.                     if(java_code)
  190.                       {
  191.                         XP_STRCPY(buf,"<li>");
  192.                         XP_STRCAT(buf, XP_GetString(LAY_PAGEINFO_APPLET));
  193.                         XP_STRCAT(buf," <A TARGET=Internal_URL_Info HREF=about:");
  194.                         STREAM_WRITE(buf);
  195.                         STREAM_WRITE(java_code);
  196.                         XP_STRCPY(buf,">");
  197.                         STREAM_WRITE(buf);
  198.                         STREAM_WRITE(java_code);
  199.                         XP_STRCPY(buf,"</A>");
  200.                         STREAM_WRITE(buf);
  201.                         XP_FREE(java_code);
  202.                       }
  203.                     if(java_codebase)
  204.                       {
  205.                         XP_STRCPY(buf,"<li>");
  206.                         XP_STRCAT(buf, XP_GetString(LAY_PAGEINFO_APPLET));
  207.                         XP_STRCAT(buf," <A TARGET=Internal_URL_Info HREF=about:");
  208.                         STREAM_WRITE(buf);
  209.                         STREAM_WRITE(java_codebase);
  210.                         XP_STRCPY(buf,">");
  211.                         STREAM_WRITE(buf);
  212.                         STREAM_WRITE(java_codebase);
  213.                         XP_STRCPY(buf,"</A>");
  214.                         STREAM_WRITE(buf);
  215.                         XP_FREE(java_codebase);
  216.                       }
  217.                     if(java_archive)
  218.                       {
  219.                         XP_STRCPY(buf,"<li>");
  220.                         XP_STRCAT(buf, XP_GetString(LAY_PAGEINFO_APPLET));
  221.                         XP_STRCAT(buf," <A TARGET=Internal_URL_Info HREF=about:");
  222.                         STREAM_WRITE(buf);
  223.                         STREAM_WRITE(java_archive);
  224.                         XP_STRCPY(buf,">");
  225.                         STREAM_WRITE(buf);
  226.                         STREAM_WRITE(java_archive);
  227.                         XP_STRCPY(buf,"</A>");
  228.                         STREAM_WRITE(buf);
  229.                         XP_FREE(java_archive);
  230.                       }
  231.                     if(java_name)
  232.                       {
  233.                         XP_STRCPY(buf,"<li>");
  234.                         XP_STRCAT(buf, XP_GetString(LAY_PAGEINFO_APPLET));
  235.                         XP_STRCAT(buf," <A TARGET=Internal_URL_Info HREF=about:");
  236.                         STREAM_WRITE(buf);
  237.                         STREAM_WRITE(java_name);
  238.                         XP_STRCPY(buf,">");
  239.                         STREAM_WRITE(buf);
  240.                         STREAM_WRITE(java_name);
  241.                         XP_STRCPY(buf,"</A>");
  242.                         STREAM_WRITE(buf);
  243.                         XP_FREE(java_name);
  244.                       }
  245.                 }
  246.                 break;
  247. #endif
  248.             /*
  249.              * We must recursively traverse into cells.
  250.              */
  251.             case LO_CELL:
  252.                 lo_element_info(eptr->lo_cell.cell_list, stream);
  253.                 lo_element_info(eptr->lo_cell.cell_float_list, stream);
  254.                 break;
  255.             default:
  256.                 break;
  257.         }
  258.         eptr = eptr->lo_any.next;
  259.     }
  260. }
  261.  
  262. PRIVATE
  263. void
  264. lo_backdrop_info(char *backdrop_image_url, NET_StreamClass *stream)
  265. {
  266.     if (backdrop_image_url)
  267.     {
  268.         char buf[1024];
  269.         char *str_ptr;
  270.         char *copy_of_str;
  271.  
  272.         PA_LOCK(str_ptr, char *, backdrop_image_url);
  273.  
  274.         /* layout mucks with strings so you must make
  275.          * a copy of them before sending them up
  276.          */
  277.         copy_of_str = XP_STRDUP(str_ptr);
  278.         if(copy_of_str)
  279.           {
  280.             XP_STRCPY(buf, XP_GetString(LAY_PAGEINFO_BACKGROUND_IMAGE) );
  281.             STREAM_WRITE(buf);
  282.             XP_STRCPY(buf, "<A TARGET=Internal_URL_Info HREF=about:");
  283.             STREAM_WRITE(buf);
  284.             STREAM_WRITE(copy_of_str);
  285.             XP_STRCPY(buf,">");
  286.             STREAM_WRITE(buf);
  287.             XP_FREE(copy_of_str);
  288.             copy_of_str = XP_STRDUP(str_ptr);
  289.             if(copy_of_str)
  290.               {
  291.                 STREAM_WRITE(copy_of_str);
  292.                 XP_FREE(copy_of_str);
  293.               }
  294.             XP_STRCPY(buf,"</A>");
  295.             STREAM_WRITE(buf);
  296.            }
  297.  
  298.         PA_UNLOCK(backdrop_image_url);
  299.     }
  300. }
  301.  
  302. PRIVATE
  303. void
  304. lo_form_info(lo_FormData *form_list, NET_StreamClass *stream)
  305. {
  306.     int form_num=1;
  307.  
  308.     while (form_list != NULL)
  309.     {
  310.         char *str;
  311.         char *submit_url;
  312.         char *encoding;
  313.         char buf[1024];
  314.  
  315.         XP_SPRINTF(buf, "<b>Form %d:</b><UL>", form_num++);
  316.         STREAM_WRITE(buf);
  317.  
  318.         PA_LOCK(str, char *, form_list->action);
  319.         /*
  320.          * Extract the URL this form will submit to.
  321.          */
  322.         if (str != NULL)
  323.         {
  324.             submit_url = XP_STRDUP(str);
  325.         }
  326.         else
  327.         {
  328.             submit_url = NULL;
  329.         }
  330.         PA_UNLOCK(form_list->action);
  331.  
  332.         XP_STRCPY(buf, XP_GetString(LAY_PAGEINFO_ACTIONURL));
  333.         STREAM_WRITE(buf);
  334.         if(submit_url)
  335.            {
  336.             STREAM_WRITE(submit_url);
  337.             XP_FREE(submit_url);
  338.           }
  339.  
  340.         PA_LOCK(str, char *, form_list->encoding);
  341.         /*
  342.          * Extract the encoding this form will submit in.
  343.          */
  344.         if (str != NULL)
  345.         {
  346.             encoding = XP_STRDUP(str);
  347.         }
  348.         else
  349.         {
  350.             encoding = NULL;
  351.         }
  352.         PA_UNLOCK(form_list->encoding);
  353.  
  354.         XP_STRCPY(buf, XP_GetString(LAY_PAGEINFO_ENCODING));
  355.         STREAM_WRITE(buf);
  356.         if(encoding)
  357.            {
  358.             STREAM_WRITE(encoding);
  359.             XP_FREE(encoding);
  360.           }
  361.         else
  362.           {
  363.             XP_STRCPY(buf, APPLICATION_WWW_FORM_URLENCODED " (default)");
  364.             STREAM_WRITE(buf);
  365.           }
  366.  
  367.         /*
  368.          * Extract the submit method
  369.          * (FORM_METHOD_GET or FORM_METHOD_POST).
  370.          */
  371.         XP_STRCPY(buf, XP_GetString(LAY_PAGEINFO_METHOD));
  372.         STREAM_WRITE(buf);
  373.  
  374.         if(form_list->method == FORM_METHOD_POST)
  375.             XP_STRCPY(buf, "Post");
  376.         else
  377.             XP_STRCPY(buf, "Get");
  378.         STREAM_WRITE(buf);
  379.  
  380.         XP_STRCPY(buf, "</UL>");
  381.         STREAM_WRITE(buf);
  382.  
  383.         /* move to the next one */
  384.         form_list = form_list->next;
  385.     }
  386. }
  387.  
  388.  
  389. /*
  390.  * Prepare a bunch of information about the content of this
  391.  * document and prints the information as HTML down
  392.  * the passed in stream.
  393.  *
  394.  * Returns:
  395.  *    -1    If the context passed does not correspond to any currently
  396.  *        laid out document.
  397.  *    0    If the context passed corresponds to a document that is
  398.  *        in the process of being laid out.
  399.  *    1    If the context passed corresponds to a document that is
  400.  *        completly laid out and info can be found.
  401.  */
  402. PUBLIC intn
  403. LO_DocumentInfo(MWContext *context, NET_StreamClass *stream)
  404. {
  405.     int32 doc_id, i;
  406.     lo_TopState *top_state;
  407.     lo_DocState *state;
  408.     LO_Element **line_array;
  409.     LO_Element *eptr;
  410.     /* stuff we want to return info on */
  411.     char *backdrop_image_url;
  412.     char *url;
  413.     char *base_url;
  414.     lo_FormData *form_list;
  415.  
  416.     if (context == NULL)
  417.     {
  418.         return(-1);
  419.     }
  420.  
  421.     /*
  422.      * Get the unique document ID, and retreive this
  423.      * documents layout state.
  424.      */
  425.     doc_id = XP_DOCID(context);
  426.     top_state = lo_FetchTopState(doc_id);
  427.     if ((top_state == NULL)||(top_state->doc_state == NULL))
  428.     {
  429.         return(-1);
  430.     }
  431.     state = top_state->doc_state;
  432.  
  433.     if (top_state->layout_status != PA_COMPLETE)
  434.     {
  435.         char buf[1024];
  436.  
  437.         XP_STRCPY(buf, XP_GetString(LAY_PAGEINFO_NOINFO));
  438.         STREAM_WRITE(buf);
  439.  
  440.         return(0);
  441.     }
  442.  
  443.     /*
  444.      * Extract the URL this document was accessed by.
  445.      */
  446.     if (top_state->url != NULL)
  447.     {
  448.         char buf[1024];
  449.  
  450.         url = XP_STRDUP(top_state->url);
  451.         XP_STRCPY(buf, "<A TARGET=Internal_URL_Info HREF=about:");
  452.         STREAM_WRITE(buf);
  453.         STREAM_WRITE(url);
  454.         XP_STRCPY(buf,">");
  455.         STREAM_WRITE(buf);
  456.         STREAM_WRITE(url);
  457.         XP_STRCPY(buf,"</A><UL>");
  458.         STREAM_WRITE(buf);
  459.         XP_FREE(url);
  460.     }
  461.     else
  462.     {
  463.         url = NULL;
  464.     }
  465.  
  466.     /*
  467.      * Extract the BASE URL for this document.
  468.      */
  469.     if (top_state->base_url != NULL)
  470.     {
  471.         base_url = XP_STRDUP(top_state->base_url);
  472.     }
  473.     else
  474.     {
  475.         base_url = NULL;
  476.     }
  477.  
  478.     /*
  479.      * Extract the background image, NULL if none.
  480.      */
  481.     backdrop_image_url = (char*)LO_GetLayerBackdropURL(top_state->doc_layer);
  482.     lo_backdrop_info(backdrop_image_url, stream);
  483.  
  484.     /*
  485.      * Is this a grid?  If so there is nothing but grid cells
  486.      * in it.  Return after processing it.
  487.      */
  488.     if (top_state->is_grid != FALSE)
  489.     {
  490.         lo_GridRec *the_grid;
  491.         int32 cell_count;
  492.         lo_GridCellRec *cell_list;
  493.         char buf[1024];
  494.  
  495.         the_grid = top_state->the_grid;
  496.         if (the_grid == NULL)
  497.         {
  498.             XP_STRCPY(buf,"</UL>");
  499.             STREAM_WRITE(buf);
  500.             return(1);
  501.         }
  502.  
  503.         /*
  504.          * The number of cells may be less than
  505.          * the rows * cols because of blank cells.
  506.          */
  507.         cell_count = the_grid->cell_count;
  508.  
  509.         cell_list = the_grid->cell_list;
  510.         while (cell_list != NULL)
  511.         {
  512.             char *cell_url;
  513.             MWContext *cell_context;
  514.  
  515.             /*
  516.              * Extract the cell's URL.
  517.              */
  518.             if (cell_list->url != NULL)
  519.             {
  520.                 cell_url = XP_STRDUP(cell_list->url);
  521.             }
  522.             else
  523.             {
  524.                 cell_url = NULL;
  525.             }
  526.  
  527.             /*
  528.              * Extract the cell's window context.
  529.              */
  530.             cell_context = cell_list->context;
  531.  
  532.             XP_STRCPY(buf, XP_GetString(LAY_PAGEINFO_FRAME));
  533.             STREAM_WRITE(buf);
  534.  
  535.             /* recurse and print if valid context */
  536.             if (cell_context != NULL)
  537.             {
  538.                 LO_DocumentInfo(cell_context, stream);
  539.             }
  540.  
  541.             cell_list = cell_list->next;
  542.         }
  543.         XP_STRCPY(buf,"</UL>");
  544.         STREAM_WRITE(buf);
  545.         return(1);
  546.     }
  547.  
  548.     /*
  549.      * Extract the list of all forms in this doc.
  550.      */
  551.     form_list = top_state->doc_lists.form_list;
  552.     lo_form_info(form_list, stream);
  553.  
  554.     /*
  555.      * Now traverse the element list describing important elements.
  556.      * Right now interesting elements are Images and Embeds.
  557.      */
  558.     /*
  559.      * Make eptr point to the start of the element chain
  560.      * for this document.
  561.      */
  562. #ifdef XP_WIN16
  563.     {
  564.         XP_Block *larray_array;
  565.  
  566.         XP_LOCK_BLOCK(larray_array, XP_Block *, state->larray_array);
  567.         state->line_array = larray_array[0];
  568.         XP_UNLOCK_BLOCK(state->larray_array);
  569.     }
  570. #endif /* XP_WIN16 */
  571.     XP_LOCK_BLOCK(line_array, LO_Element **, state->line_array);
  572.     eptr = line_array[0];
  573.     XP_UNLOCK_BLOCK(state->line_array);
  574.  
  575.     lo_element_info(eptr, stream);
  576.     lo_element_info(state->float_list, stream);
  577.  
  578.     for (i = 0; i <= state->top_state->max_layer_num; i++) {
  579.         lo_LayerDocState *layer_state = state->top_state->layers[i];
  580.         char buf[1024];
  581.         char *name;
  582.  
  583.         if (!layer_state)
  584.             continue;
  585.         if (layer_state->id == LO_DOCUMENT_LAYER_ID)
  586.             continue;
  587.  
  588.         XP_STRCPY(buf, XP_GetString(LAY_PAGEINFO_LAYER));
  589.         STREAM_WRITE(buf);
  590.         if (layer_state->layer) {
  591.             name = CL_GetLayerName(layer_state->layer);
  592.             if (name)
  593.                 STREAM_WRITE(name);
  594.         }
  595.         XP_STRCPY(buf, "<UL>");
  596.         STREAM_WRITE(buf);
  597.  
  598.         /* Extract the backdrop of the layer if it exists */
  599.         backdrop_image_url = (char*)LO_GetLayerBackdropURL(layer_state->layer);
  600.         lo_backdrop_info(backdrop_image_url, stream);
  601.         
  602.         /* Extract form info */
  603.         form_list = layer_state->doc_lists->form_list;
  604.         lo_form_info(form_list, stream);
  605.  
  606.         /* And important element info */
  607.         if (layer_state->cell) {
  608.             LO_CellStruct *cell = layer_state->cell;
  609.  
  610.             lo_element_info(cell->cell_list, stream);
  611.             lo_element_info(cell->cell_float_list, stream);
  612.         }
  613.  
  614.         XP_STRCPY(buf, "</UL>");
  615.         STREAM_WRITE(buf);
  616.     }
  617.     
  618.     if(1)
  619.       {
  620.         char buf[16];
  621.         XP_STRCPY(buf,"</UL>");
  622.         STREAM_WRITE(buf);
  623.       }
  624.  
  625.     return(1);
  626. }
  627.  
  628.