home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / central / earlmgr.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  9.9 KB  |  385 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. // earlmgr.cp, Mac front end
  20.  
  21. #include "earlmgr.h"
  22.     // macfe
  23. #include "ufilemgr.h"
  24. #include "ulaunch.h"
  25. #include "uprefd.h"
  26. #include "uerrmgr.h"
  27. #include "BufferStream.h"
  28. #include "uapp.h"
  29. #include "CNSContext.h"
  30.     // Network
  31. #ifndef NSPR20
  32. #include "CNetwork.h"
  33. #endif
  34.     // Netscape
  35. #include "secrng.h"
  36.     // utilities
  37. #include "PascalString.h"
  38.  
  39. #ifdef JAVA
  40. extern "C" {
  41. #include "nsn.h"
  42. }
  43. #endif
  44.  
  45. #include "resgui.h"
  46.  
  47. #ifdef NSPR20
  48. PR_BEGIN_EXTERN_C
  49. PR_EXTERN(PRThread*) PR_GetPrimaryThread(void);
  50. PR_END_EXTERN_C
  51. #endif
  52.  
  53. /*-----------------------------------------------------------------------------
  54.     Earl Manager
  55. -----------------------------------------------------------------------------*/
  56.  
  57. EarlManager TheEarlManager;
  58.  
  59. int EarlManagerNetTicklerCallback(void);
  60.  
  61. //
  62. // Construction
  63. //
  64.  
  65. EarlManager::EarlManager ()
  66. {
  67.     fInterruptContext = NULL;
  68. #if JAVA
  69.     nsn_InstallTickleHookProc(&EarlManagerNetTicklerCallback);
  70. #endif
  71. }
  72.  
  73. //
  74. // ÑÑ URL loading interface
  75. //
  76.  
  77. void EarlManager::DoCancelLoad (MWContext *context)
  78. {
  79.     // Cancels all loads for a given window
  80.     NET_InterruptWindow (context);
  81. }
  82.  
  83. int EarlManager::StartLoadURL (URL_Struct * request, MWContext *context,
  84.                     FO_Present_Types output_format)
  85. {
  86.     if (request->referer && (strlen(request->referer) == 0))
  87.     {
  88.         XP_FREE(request->referer);
  89.         request->referer = NULL;
  90.     }
  91.     int err = NET_GetURL (request, output_format, context, DispatchFinishLoadURL);    
  92.     return err;
  93. }
  94.  
  95. void EarlManager::SpendTime( const EventRecord& /*inMacEvent*/)
  96. {
  97.     //    Make sure that we only call NET_ProcessNet at an
  98.     //    appropriate time, and make sure that our priority is
  99.     //    low so that other java threads an run.
  100.     
  101. #ifndef NSPR20
  102.     // If we don't have a gNetDriver then we better not call it
  103.     if (!gNetDriver)
  104.         return;
  105.     
  106.     PR_SetThreadPriority(PR_CurrentThread(), 0);
  107.     gNetDriver->SpendTime();
  108. #else
  109.     PR_SetThreadPriority(PR_CurrentThread(), PR_PRIORITY_LOW);
  110. #endif
  111.  
  112.     unsigned char dst;
  113.     RNG_GenerateGlobalRandomBytes(&dst, 1);
  114. #ifdef NSPR20_DISABLED
  115.     CSelectObject socketToCallWith;
  116.     if (gNetDriver->CanCallNetlib(socketToCallWith))
  117.         NET_ProcessNet(MacToStdSocketID(socketToCallWith.fID), SocketSelectToFD_Type(socketToCallWith.fType));
  118. #else
  119.     NET_PollSockets();
  120. #endif
  121.     if ( fInterruptContext )
  122.     {
  123.         NET_InterruptWindow( fInterruptContext );
  124.         fInterruptContext = NULL;
  125.     }
  126. }
  127.  
  128. void EarlManager::InterruptThis(MWContext * context)
  129. {
  130.     fInterruptContext = context;
  131. }
  132.  
  133. void EarlManager::FinishLoadURL(URL_Struct * request, int status, MWContext *context)
  134. {
  135.     CNSContext* nsContext = ExtractNSContext(context);
  136.     if (nsContext)
  137.         nsContext->CompleteLoad(request, status);
  138.     if (status != MK_CHANGING_CONTEXT)
  139.         NET_FreeURLStruct (request);
  140. }
  141.  
  142. void EarlManager::DispatchFinishLoadURL (URL_Struct *request, int status, MWContext *context)
  143. {
  144.     // let the front end know the NetGetUrl is complete. This should go
  145.     // into NetLib at some point! LAM    
  146.     
  147.     TheEarlManager.FinishLoadURL (request, status, context);
  148. }
  149.  
  150.  
  151.  
  152.  
  153. /*------------------------------------------------------------------------------
  154.  FE Misc functions
  155.  - Launching of telnet/tn3270 sessions
  156.  ------------------------------------------------------------------------------*/
  157. // ÑÑ Prototypes
  158.  
  159. LFileBufferStream * CreateTelnetFile(char * hostname, char * port, char * username);
  160. LFileBufferStream * CreateTn3270File(char * hostname, char * port, char * username);
  161.  
  162. // ÑÑ Implementation
  163.  
  164. LFileBufferStream * CreateTelnetFile(char * hostname, char * port, char * username)
  165. {
  166.     FSSpec fileSpec;
  167.  
  168.     // Figure out the mime extensions
  169.     CMimeMapper * mime = CPrefs::sMimeTypes.FindMimeType(CMimeList::Telnet);
  170.     if (mime == NULL)
  171.         return NULL;
  172.  
  173.     // Create the file
  174.     LFileBufferStream * file = NULL;
  175.     CFileMgr::UniqueFileSpec( CPrefs::GetTempFilePrototype(),
  176.         CStr31(::GetCString(NETSCAPE_TELNET)), fileSpec);
  177.  
  178.     Try_    {    // Not dealing with duplicates yet
  179.         file = new LFileBufferStream(fileSpec);
  180.         file->CreateNewDataFile(mime->GetAppSig(), mime->GetDocType(), 0);
  181.         file->OpenDataFork(fsRdWrPerm);
  182.     }
  183.     Catch_(inErr)
  184.     {
  185.         return NULL;
  186.     } EndCatch_
  187.  
  188. // Write the arguments out
  189.  
  190.     WriteCString (file, ::GetCString(NETSCAPE_TELNET_NAME_ARG));    // name="user@hostname"
  191.     if (username != NULL)
  192.     {
  193.         WriteCString (file, username);
  194.         WriteChar (file, '@');
  195.     // ErrorManager::PlainAlert(Login as????
  196.     }
  197.     WriteCString (file, hostname);
  198.     WriteCString (file, "\"\r");
  199.     WriteCString (file, ::GetCString(NETSCAPE_TELNET_HOST_ARG));    // host = "hostname"
  200.     WriteCString (file, hostname);
  201.     WriteCString (file, "\"\r");
  202.     WriteCString (file, ::GetCString(NETSCAPE_TELNET_PORT_ARG));    // port = "port"
  203.     if (port != NULL)
  204.         WriteCString (file, port);
  205.     else
  206.         WriteCString (file, "23\r");
  207.     file->CloseDataFork();
  208.     return file;
  209. }
  210.  
  211. LFileBufferStream * CreateTn3270File(char * hostname, char * /*port*/, char * /*username*/)
  212. {
  213.     FSSpec fileSpec;
  214.  
  215. // Figure out the mime extensions
  216.     CMimeMapper * mime = CPrefs::sMimeTypes.FindMimeType(CMimeList::Tn3270);
  217.     if (mime == NULL)
  218.         return NULL;
  219.  
  220. // Create the file
  221.     LFileBufferStream * file = NULL;
  222.     CFileMgr::UniqueFileSpec( CPrefs::GetTempFilePrototype(),
  223.          CStr31(::GetCString(NETSCAPE_TN3270)), fileSpec);
  224.  
  225.     Try_    {    // Not dealing with duplicates yet
  226.         file = new LFileBufferStream (fileSpec);
  227.         file->CreateNewDataFile(mime->GetAppSig(), mime->GetDocType(), 0);
  228.         file->OpenDataFork(fsRdWrPerm);
  229.     }
  230.     Catch_(inErr)
  231.     {
  232.         return NULL;
  233.     } EndCatch_
  234.  
  235. // Write the arguments out
  236. // I really do not know what the format of the file is, so I am just guessing here
  237.     WriteCString (file, "0 8 80 20 40 10 80 0 ");    // part 1
  238.     WriteCString (file, hostname);
  239.     // part 2 of indecipherable file format
  240.     WriteCString (file, " 2 1 15 1 1 1 23 0 1 ffff ffff ffff fc00 f37d 52f ffff 7a5d 9f8e 0 c42f eaff 1f21 b793 1431 dd6b 8c2 6a2 4eba 6aea dd6b 0 0 0 2a00 2a00 2a00 24 12 0 1 0 0 4eba 6aea dd6b 1 1 0 default 1 English_(U.S.) 12 80 24 80 System_Alert_Sound 2 19f 142 1 74747874 0");                        // part2
  241.     file->CloseDataFork();
  242.     return file;
  243. }
  244.  
  245. void FE_ConnectToRemoteHost(MWContext * ctxt, int url_type, char *
  246. hostname, char * port, char * username)
  247. {
  248.     LFileBufferStream * telnetFile = NULL;
  249.     switch (url_type)    {
  250.         case FE_RLOGIN_URL_TYPE:
  251.         case FE_TELNET_URL_TYPE:
  252.             FE_Progress(ctxt, (char *)GetCString( LAUNCH_TELNET_RESID )); 
  253.             telnetFile = CreateTelnetFile(hostname, port, username);
  254.             break;
  255.         case FE_TN3270_URL_TYPE:
  256.             FE_Progress(ctxt, (char *)GetCString( LAUNCH_TN3720_RESID ));
  257.             telnetFile = CreateTn3270File(hostname, port, username);
  258.     }
  259.     if (telnetFile != NULL)
  260.     {
  261.         LaunchFile(telnetFile);
  262.         CFileMgr::sFileManager.RegisterFile(telnetFile);    // Register file for deletion
  263.     }
  264.     else
  265.         ErrorManager::PlainAlert(TELNET_ERR_RESID);
  266. }
  267.  
  268. /*------------------=----------------------------------------------------------
  269.     Utilities
  270. --------------------=--------------------------------------------------------*/
  271.  
  272. CStr255 gSaveAsPrompt;
  273.  
  274. OSErr GUI_AskForFileSpec (StandardFileReply & reply);
  275. OSErr GUI_AskForFileSpec (StandardFileReply & reply)
  276. {
  277.     ErrorManager::PrepareToInteract();
  278.     
  279.     UDesktop::Deactivate();
  280.     if (gSaveAsPrompt.Length() == 0)
  281.         gSaveAsPrompt = (const char*)GetCString( SAVE_AS_RESID );
  282.     StandardPutFile (gSaveAsPrompt, reply.sfFile.name, &reply);
  283.     UDesktop::Activate();
  284.     
  285.     if (!reply.sfGood)
  286.         return userCanceledErr;
  287.     return noErr;
  288. }
  289.  
  290. int EarlManagerNetTicklerCallback(void)
  291. {
  292.     //    We need to tickle the network and the UI if we
  293.     //    are stuck in java networking code.
  294.     
  295.     if (PR_CurrentThread() == PR_GetPrimaryThread()) {
  296.         (CFrontApp::GetApplication())->ProcessNextEvent();
  297.         return false;
  298.     }
  299.     
  300.     else {
  301.         return true;    
  302.     }
  303. }
  304.  
  305.  
  306. #ifdef NSPR20_DISABLED
  307. int SocketSelectToFD_Type(SocketSelect s)
  308. {
  309.     switch (s)    {
  310.     case eReadSocket:
  311.     case eExceptionSocket:
  312.         return NET_SOCKET_FD;
  313.     case eLocalFileSocket:
  314.         return NET_LOCAL_FILE_FD;
  315.     case eEverytimeSocket:
  316.         return NET_EVERYTIME_TYPE;
  317.     }
  318.     return 0;    // Never reached
  319. }
  320. #endif
  321.  
  322. // FE Select routines
  323. void FE_SetReadSelect (MWContext * /* context */, int sd)
  324. {
  325. #ifndef NSPR20
  326.     gNetDriver->SetReadSelect(sd);
  327. #endif
  328. }
  329.  
  330. void FE_ClearReadSelect (MWContext */* context */, int sd)
  331. {
  332. #ifndef NSPR20
  333.     gNetDriver->ClearReadSelect(sd);
  334. #endif
  335. }
  336.  
  337. void FE_SetConnectSelect (MWContext */* context */, int sd)
  338. {
  339. #ifndef NSPR20
  340.     gNetDriver->SetConnectSelect(sd);
  341. #endif
  342. }
  343.  
  344. void FE_ClearConnectSelect(MWContext */* context */, int sd)
  345. {
  346. #ifndef NSPR20
  347.     gNetDriver->ClearConnectSelect(sd);
  348. #endif
  349. }
  350.  
  351.  
  352. void FE_SetFileReadSelect (MWContext */* context */, int fd)
  353. {
  354. #ifndef NSPR20
  355.     gNetDriver->SetFileReadSelect(fd);
  356. #endif
  357. }
  358.  
  359.         
  360. void FE_ClearFileReadSelect (MWContext */* context */, int fd)
  361. {
  362. #ifndef NSPR20
  363.     gNetDriver->ClearFileReadSelect(fd);
  364. #endif
  365. }
  366.  
  367. int FE_StartAsyncDNSLookup (MWContext */* context */, char * host_port, void ** hoststruct_ptr_ptr, int sock)
  368. {
  369. #ifndef NSPR20
  370.     *hoststruct_ptr_ptr = NULL;
  371.     int err = gNetDriver->StartAsyncDNSLookup(host_port, (struct hostent **)hoststruct_ptr_ptr, sock);
  372.     if (err == EAGAIN)    // Because macsock knows nothing of netlib defines
  373.         return MK_WAITING_FOR_LOOKUP;
  374.     else
  375.         return err;
  376. #endif
  377.     return MK_WAITING_FOR_LOOKUP;
  378. }
  379.  
  380.  
  381. #ifdef PROFILE
  382. #pragma profile off
  383. #endif
  384.  
  385.