home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / Netcaster.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  10.5 KB  |  441 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.    Netcaster.cpp -- utilities to open Netcaster
  20.    Created: Chris Toshok <toshok@netscape.com>, 7-Aug-96.
  21.  */
  22.  
  23.  
  24.  
  25.  
  26. #include "mozjava.h"
  27. #include "xfe2_extern.h"
  28. #include "VerReg.h"
  29. #include "libmocha.h"
  30. #include "xpgetstr.h"
  31.  
  32. #include "Netcaster.h"
  33.  
  34. // This is way-less expensive than including allxpstr.h
  35. extern int XP_ALERT_NETCASTER_NO_JS;
  36. extern int XP_ALERT_CANT_RUN_NETCASTER;
  37.  
  38. /*
  39.  * rodt: Defined in ns/cmd/xfe/mozilla.c.  The rest of the world
  40.  * also does this cheesy declaration.
  41.  */
  42. extern "C" void fe_GetProgramDirectory(char * path, int length);
  43.  
  44. static char* netcasterWindowName          = "Netcaster_SelectorTab";
  45. static char* netcasterTabHtmlRegistryNode = "Netcaster/tab.htm";
  46. static char* netcasterTabHtmlPath         = "netcast/tab.htm";
  47.  
  48. /*
  49.  * These statics were moved outside of functions where they are
  50.  * used because of potential problems on HP (rodt, djw, dora).
  51.  * They are for private use within their respective functions!
  52.  */
  53. static char * private_xfe_netcaster_path = 0;
  54. static char * private_xfe_netcaster_url = 0;
  55.  
  56.  
  57.  
  58. /****************************************
  59.  *
  60.  */
  61.  
  62. MWContext *
  63. FE_IsNetcasterRunning(void)
  64.  
  65. /*
  66.  * returns:
  67.  *    A pointer to an MWContext if Netcaster is running.
  68.  *    Null if Netcaster is not running.
  69.  *
  70.  ****************************************/
  71. {
  72.   MWContext * result = 0;
  73.  
  74.   if (fe_IsNetcasterInstalled())
  75.       result = XP_FindNamedContextInList(NULL, netcasterWindowName);
  76.  
  77.   return result;
  78. }
  79.  
  80.  
  81. /****************************************
  82.  */
  83.  
  84. static XP_Bool
  85. xfe_path_exists(const char * path)
  86.  
  87. /*
  88.  * returns:
  89.  *    TRUE if the path exists on disk
  90.  *    FALSE otherwise
  91.  *
  92.  ****************************************/
  93. {
  94.   XP_Bool     result = FALSE;
  95.   int         code;
  96.   struct stat sbuf;
  97.   const       STAT_SUCCESS = 0;
  98.  
  99.   if (path)
  100.     {
  101.       /* rodt: didn't use XP_Stat() because it does a lot of apparently
  102.        * unnecessary work, is undocumented and also is implemented using stat().
  103.        */
  104.       code = stat(path, &sbuf);
  105.       if (code == STAT_SUCCESS)
  106.         result = TRUE;
  107.     }
  108.  
  109.   return result;
  110. }
  111.  
  112.  
  113. /****************************************
  114.  *
  115.  */
  116.  
  117. static int
  118. xfe_last_character(const char * s)
  119.  
  120. /*
  121.  *
  122.  * returns:
  123.  *    The last character in the string or
  124.  *    '\0' on a NULL or empty string
  125.  *
  126.  ****************************************/
  127. {
  128.   int result = '\0';
  129.   if (s)
  130.     {
  131.       int length;
  132.       length = strlen(s);
  133.       if (length > 0)
  134.         result = s[length-1];
  135.     }
  136.   return result;
  137. }
  138.  
  139.  
  140.  
  141. /****************************************
  142.  *
  143.  */
  144.  
  145. static const char *
  146. xfe_netcaster_path(void)
  147.  
  148. /*
  149.  * description:
  150.  *    Determine a path to tab.htm, which is
  151.  *    loaded into a browser window to start
  152.  *    Netcaster by checking
  153.  *    the following locations in order:
  154.  *        $HOME/.netscape/netcast/tab.htm
  155.  *        $MOZILLA_HOME/netcast/tab.htm
  156.  *        Version Registry via VR_GetPath()
  157.  *        fe_GetProgramDirectory()/netcast/tab.htm
  158.  *
  159.  *  The reason for precedence is as follows:
  160.  *        $HOME first. Preferences set in a user's
  161.  *        $HOME override everything else.
  162.  *        This allows users some chance of running in
  163.  *        the case where the system admin won't install
  164.  *        Netcaster in globally accessible location.
  165.  *        This can also be useful for debugging purposes.
  166.  *
  167.  *        $MOZILLA_HOME is now recommended as the standard
  168.  *        for finding Communicator and related components.
  169.  *        It comes next.
  170.  *
  171.  *        The registry should get set correctly during ASD install.
  172.  *        However the registry's role in tracking versions has
  173.  *        been emphasized over its role in tracking location.
  174.  *        (although you can't have the former without the latter).
  175.  *        Rumor also has it that some people are in the habit
  176.  *        of deleting the registry to fix problems, so it
  177.  *        may not always be available.
  178.  *
  179.  *        Last resort is to do our best effort at determining
  180.  *        where Communicator was invoked by getting the program directory
  181.  *        by looking at the invocation path.
  182.  *
  183.  * returns:
  184.  *    On success returns a path to tab.htm
  185.  *    On failure returns NULL
  186.  *
  187.  ****************************************/
  188. {
  189.   const char * result = 0;
  190.   char * home;
  191.   REGERR code;
  192.  
  193.   if (!private_xfe_netcaster_path)
  194.     {
  195.       private_xfe_netcaster_path = (char*)XP_ALLOC(MAXPATHLEN);
  196.       if (private_xfe_netcaster_path)
  197.         private_xfe_netcaster_path[0] = '\0';
  198.       else
  199.         return result;
  200.     }
  201.  
  202.   if (private_xfe_netcaster_path[0])
  203.     {
  204.       result = private_xfe_netcaster_path;
  205. #ifdef DEBUG_rodt
  206.           printf("DEBUG_rodt: Netcaster path a %s\n",result);
  207. #endif
  208.       return result;
  209.     }
  210.  
  211.  
  212.   //
  213.   // CHECK $HOME/.netscape
  214.   //
  215.   home = getenv("HOME");
  216.   if (home)
  217.     {
  218.       XP_STRCPY(private_xfe_netcaster_path, home);
  219.       if (xfe_last_character(private_xfe_netcaster_path) != '/')
  220.         XP_STRCAT(private_xfe_netcaster_path,"/");
  221.       XP_STRCAT(private_xfe_netcaster_path,".netscape/");
  222.       XP_STRCAT(private_xfe_netcaster_path,netcasterTabHtmlPath);
  223.       if (xfe_path_exists(private_xfe_netcaster_path))
  224.         {
  225.           result = private_xfe_netcaster_path;
  226. #ifdef DEBUG_rodt
  227.           printf("DEBUG_rodt: Netcaster path b %s\n",result);
  228. #endif
  229.           return result;
  230.         }
  231.     }
  232.  
  233.  
  234.   //
  235.   // CHECK $MOZILLA_HOME
  236.   //
  237.   home = getenv("MOZILLA_HOME");
  238.   if (home)
  239.     {
  240.       XP_STRCPY(private_xfe_netcaster_path, home);
  241.       if (xfe_last_character(private_xfe_netcaster_path) != '/')
  242.         XP_STRCAT(private_xfe_netcaster_path,"/");
  243.       XP_STRCAT(private_xfe_netcaster_path,netcasterTabHtmlPath);
  244.       if (xfe_path_exists(private_xfe_netcaster_path))
  245.         {
  246.           result = private_xfe_netcaster_path;
  247. #ifdef DEBUG_rodt
  248.           printf("DEBUG_rodt: Netcaster path c %s\n",result);
  249. #endif
  250.           return result;
  251.         }
  252.     }
  253.  
  254.   //
  255.   // CHECK THE REGISTRY
  256.   //
  257.   // Could also call VR_InRegistry("Netcaster") but it would be redundant
  258.   // Note that VR_ValidateComponent also calls VR_GetPath() but it doesn't
  259.   // return any path information
  260.   //
  261.   code = VR_GetPath(netcasterTabHtmlRegistryNode, sizeof(private_xfe_netcaster_path)-1, private_xfe_netcaster_path);
  262.   if (code == REGERR_OK)
  263.     {
  264.       code = VR_ValidateComponent(netcasterTabHtmlRegistryNode);
  265.       if (code == REGERR_OK
  266.           && xfe_path_exists(private_xfe_netcaster_path))  // extra check
  267.         {
  268.           result = private_xfe_netcaster_path;
  269. #ifdef DEBUG_rodt
  270.           printf("DEBUG_rodt: Netcaster path d %s\n",result);
  271. #endif
  272.           return result;
  273.         }
  274.     }
  275.   private_xfe_netcaster_path[0] = '\0';
  276.  
  277.   //
  278.   // CHECK THE PROGRAM DIRECTORY
  279.   //
  280.   fe_GetProgramDirectory(private_xfe_netcaster_path, MAXPATHLEN - 1);
  281.   if (private_xfe_netcaster_path[0])
  282.     {
  283.       if (xfe_last_character(private_xfe_netcaster_path) != '/')
  284.         XP_STRCAT(private_xfe_netcaster_path,"/");
  285.       XP_STRCAT(private_xfe_netcaster_path,netcasterTabHtmlPath);
  286.       if (xfe_path_exists(private_xfe_netcaster_path))
  287.         {
  288.           result = private_xfe_netcaster_path;
  289. #ifdef DEBUG_rodt
  290.           printf("DEBUG_rodt: Netcaster path e %s\n",result);
  291. #endif
  292.           return result;
  293.         }
  294.     }
  295.  
  296.   private_xfe_netcaster_path[0] = '\0';
  297. #ifdef DEBUG_rodt
  298.   printf("DEBUG_rodt: Netcaster path not found\n");
  299. #endif
  300.   return result;
  301. }
  302.  
  303.  
  304.  
  305. /****************************************
  306.  *
  307.  */
  308.  
  309. static const char*
  310. xfe_netcaster_url(void)
  311.  
  312. /*
  313.  * returns:
  314.  *    On success returns a file URL pointing to tab.html
  315.  *    On failure returns NULL
  316.  *
  317.  ****************************************/
  318. {
  319.   const char * result = NULL;
  320.  
  321.   if (!private_xfe_netcaster_url)
  322.     {
  323.       private_xfe_netcaster_url = (char*)XP_ALLOC(MAXPATHLEN);
  324.       if (private_xfe_netcaster_url)
  325.         private_xfe_netcaster_url[0] = '\0';
  326.       else
  327.         return result;
  328.     }
  329.  
  330.   if (private_xfe_netcaster_url[0])
  331.     {
  332.       result = private_xfe_netcaster_url;
  333.     }
  334.   else
  335.     {
  336.       const char * path;
  337.       path = xfe_netcaster_path();
  338.  
  339.       if (path)
  340.         {
  341.           XP_SPRINTF(private_xfe_netcaster_url,"file:%s",path);
  342.           result = private_xfe_netcaster_url;
  343.         }
  344.       else
  345.         private_xfe_netcaster_url[0] = '\0';
  346.     }
  347.  
  348.   return result;
  349. }
  350.  
  351.  
  352.  
  353. /****************************************
  354.  */
  355.  
  356. void
  357. fe_showNetcaster(Widget toplevel)
  358.  
  359. /*
  360.  * description:
  361.  *    This function shows the Netcaster window, starting
  362.  *    Netcaster if necessary.  If Netcaster is not installed,
  363.  *  this function does nothing.
  364.  *
  365.  ****************************************/
  366. {
  367.   MWContext * netcasterContext;
  368.  
  369.   if (!fe_IsNetcasterInstalled())
  370.     return;
  371.  
  372.   if(!(netcasterContext=FE_IsNetcasterRunning()))
  373.     { 
  374.  
  375.       Chrome      netcasterChrome;
  376.       URL_Struct* URL_s;
  377.       const char* netcasterURL = xfe_netcaster_url();
  378.  
  379.       if (netcasterURL)
  380.         {
  381.  
  382.           if (!LM_GetMochaEnabled() || !LJ_GetJavaEnabled())
  383.             {
  384.               fe_Alert_2(toplevel, XP_GetString(XP_ALERT_NETCASTER_NO_JS));
  385.               return;
  386.             }
  387.  
  388.           memset(&netcasterChrome, 0, sizeof(Chrome));
  389.  
  390.           netcasterChrome.w_hint             = 21;
  391.           netcasterChrome.h_hint             = 59;
  392.           netcasterChrome.l_hint             = -1000;
  393.           netcasterChrome.t_hint             = -1000;
  394.           netcasterChrome.topmost            = TRUE;
  395.           netcasterChrome.z_lock             = TRUE;
  396.           netcasterChrome.location_is_chrome = TRUE;
  397.           netcasterChrome.disable_commands   = TRUE;
  398.           netcasterChrome.hide_title_bar     = TRUE;
  399.           netcasterChrome.restricted_target  = TRUE;
  400.       
  401.           URL_s = NET_CreateURLStruct(netcasterURL, NET_DONT_RELOAD);
  402.       
  403.           netcasterContext = xfe2_MakeNewWindow(toplevel,NULL,
  404.                                                 URL_s,
  405.                                                 netcasterWindowName,
  406.                                                 MWContextBrowser,
  407.                                                 False,
  408.                                                 &netcasterChrome);
  409.         }
  410.       else
  411.         {
  412.           fe_Alert_2(toplevel, XP_GetString(XP_ALERT_CANT_RUN_NETCASTER));
  413.         } 
  414.     }
  415.   else
  416.     {
  417.       FE_RaiseWindow(netcasterContext);
  418.     }
  419. }
  420.  
  421.  
  422. /****************************************
  423.  *
  424.  */
  425.  
  426. XP_Bool
  427. fe_IsNetcasterInstalled(void)
  428.  
  429. /*
  430.  * returns:
  431.  *    TRUE or FALSE
  432.  ****************************************/
  433. {
  434.   XP_Bool result = FALSE;
  435.  
  436.   if (xfe_netcaster_path())
  437.     result = TRUE;
  438.  
  439.   return result;
  440. }
  441.