home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / regproto.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  12.9 KB  |  384 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. #include "stdafx.h"
  20.  
  21. #include "regproto.h"
  22.  
  23. #include "wfedde.h"
  24. #include "oleprot1.h"
  25. #include "winproto.h"
  26.  
  27. extern "C" XP_Bool FE_UseExternalProtocolModule(MWContext *pContext, FO_Present_Types iFormatOut, URL_Struct *pURL,
  28.     Net_GetUrlExitFunc *pExitFunc)    {
  29. //    Purpose:    See if we're supposed to handle a specific protocol differently.
  30. //    Arguments:    pContext    The context.
  31. //                iFormatOut    The format out (possibly saving).
  32. //                pURL    The URL to load.
  33. //                pExitFunc    The URL exit routine.  If we handle the protocol, we use the function.
  34. //    Returns:    BOOL    TRUE    We want to handle the protocol here.
  35. //                        FALSE    Netlib continues to handle the protocol.
  36. //    Comments:    To work with certain DDE topics which can cause URLs of a specific protocol type to be handled by another
  37. //                    application.
  38. //    Revision History:
  39. //        01-17-95    created GAB
  40. //
  41.  
  42.     //    Extract the protocol.
  43.     if(pURL->address == NULL)    {
  44.         return(FALSE);
  45.     }
  46.     CString csProtocol = pURL->address;
  47.     csProtocol = csProtocol.SpanExcluding(":");
  48.     
  49.     //    See if there's a protocol handler.
  50.     CProtocolItem *pItem = CProtocolItem::Find(csProtocol);
  51.     if(pItem == NULL)    {
  52.         return(FALSE);
  53.     }
  54.     TRACE("External protocol handler being used.\n");
  55.  
  56.     //    Have the handler attempt, respect it's return value.
  57.     //    Call the URL exit routine if it works.
  58.     if((TwoByteBool)TRUE == pItem->OpenURL(pURL, pContext, iFormatOut))    {
  59.         pExitFunc(pURL, MK_DATA_LOADED, pContext);
  60.     }
  61.     else    {
  62.         return(FALSE);
  63.     }
  64.     return(TRUE);
  65. }
  66.  
  67. //    Static variables.
  68. CPtrList CProtocolRegistry::m_Registry;
  69.  
  70. CProtocolRegistry::CProtocolRegistry()  {
  71. //    Purpose:    Construct a registry item.
  72. //    Arguments:  void
  73. //    Returns:    none
  74. //    Comments:   Simply add to this object to the registry list/NOT.
  75. //              Moved to be done in topmost derived classes, since this pointer dynamically changes!
  76. //    Revision History:
  77. //      02-18-95    created
  78. //
  79. }
  80.  
  81. CProtocolRegistry::~CProtocolRegistry() {
  82. //    Purpose:    Destruct a registry item.
  83. //    Arguments:  void
  84. //    Returns:    none
  85. //    Comments:   Simply remove this object from the registry.
  86. //    Revision History:
  87. //      02-18-95    created
  88. //
  89.     TRACE("Removing protocol from the registry\n");
  90.     m_Registry.RemoveAt(m_rIndex);
  91. }
  92.  
  93. CProtocolItem::CProtocolItem(CString& csProtocol, CString& csServiceName, int iType) : CProtocolRegistry()  {
  94. //    Purpose:    Register a new protocol Item, with protocol handling, service name, and service type.
  95. //    Arguments:  csProtocol  The protocol the attemp to handle.
  96. //              csServiceName   The name of the service handler for the protocol.
  97. //              iType   The type of service handler, usually DDE or OLE.
  98. //    Returns:    none
  99. //    Comments:   Uses base class to keep track of itself in the registry.
  100. //    Revision History:
  101. //      02-18-95    created
  102. //
  103.     TRACE("Protocol Item %s:%s:%d adding\n", (const char *)csProtocol, (const char *)csServiceName, iType);
  104.     m_csProtocol = csProtocol;
  105.     NET_AddExternalURLType((char *)(const char *)m_csProtocol);
  106.     m_csServiceName = csServiceName;
  107.     m_iType = iType;
  108. }
  109.  
  110. CProtocolItem::~CProtocolItem() {
  111. //    Purpose:    Destruct a protocol item.
  112. //    Arguments:  void
  113. //    Returns:    none
  114. //    Comments:   nothing special.
  115. //    Revision History:
  116. //      02-18-95    created GAB
  117. //
  118.     //  Do nothing, unless special cleanup required.
  119.     NET_DelExternalURLType((char *)(const char *)m_csProtocol);
  120.     TRACE("Protocol Item %s:%s:%d removing\n", (const char *)m_csProtocol, (const char *)m_csServiceName, m_iType);
  121. }
  122.  
  123. int CProtocolItem::GetType()    {
  124. //    Purpose:    Determine the type of the protocol Item.
  125. //    Arguments:  void
  126. //    Returns:    int The enumerated type.
  127. //    Comments:
  128. //    Revision History:
  129. //      02-18-95    created GAB
  130. //
  131.     TRACE("Protocol Item type is %d\n", m_iType);
  132.     return(m_iType);
  133. }
  134.  
  135. CString CProtocolItem::GetServiceName() {
  136. //    Purpose:    Return the service name for the protocol item.
  137. //    Arguments:  void
  138. //    Returns:    CString The service name.
  139. //    Comments:
  140. //    Revision History:
  141. //      02-18-95    created GAB
  142. //
  143.  
  144.     TRACE("Protocol Item service name is %s\n", (const char *)m_csServiceName);
  145.     return(m_csServiceName);
  146. }
  147.  
  148. CProtocolItem *CProtocolItem::Find(CString& csProtocol)    {
  149. //    Purpose:    Search for the protocol in the registry.
  150. //    Arguments:    csProtocol, the protocol to search for.
  151. //    Returns:    CProtocolItem *    A pointer to the registered handler for that protocol,
  152. //                    or NULL if none found.
  153. //    Comments:    This is a static function.
  154. //    Revision History:
  155. //        01-18-95    created GAB
  156. //
  157.  
  158.     //    Just loop through the registered protocols, and find the one we're looking for.
  159.     //    Consider them case sensitive, need exact match.
  160.     CProtocolItem *pRetval = NULL;
  161.     POSITION rIndex = m_Registry.GetHeadPosition();
  162.     while(rIndex != NULL)    {
  163.         pRetval = (CProtocolItem *)m_Registry.GetNext(rIndex);
  164.         if(csProtocol == pRetval->m_csProtocol)    {
  165.             break;
  166.         }
  167.         else    {
  168.             pRetval = NULL;
  169.         }
  170.     }
  171.     
  172.     return(pRetval);
  173. }
  174.  
  175. COLEProtocolItem::COLEProtocolItem(CString& csProtocol, CString& csServiceName) : CProtocolItem(csProtocol, csServiceName, m_OLE)   {
  176. //    Purpose:    Construct an OLE protocol item.
  177. //    Arguments:  csProtocol  The protocol handler type
  178. //              csServiceName   The service name (automation server) of the protocol
  179. //    Returns:    none
  180. //    Comments:   Used to initialize the type in the base class.
  181. //    Revision History:
  182. //      02-18-95    created GAB
  183. //
  184.     TRACE("Creating an OLE protocol handler\n");
  185.     m_rIndex = m_Registry.AddTail(this);
  186. }
  187.  
  188. BOOL COLEProtocolItem::OLERegister(CString& csProtocol, CString& csServiceName) {
  189. //    Purpose:    Attempt to register an OLE protocol handler.
  190. //    Arguments:  csProtocol  The protocol the handle.
  191. //              csServiceName   The OLE Automation Server to which create the dispatch to when we need to override the protocol.
  192. //    Returns:    BOOL    TRUE    Registered.
  193. //                      FALSE   Not registered.
  194. //    Comments:   Returns FALSE if ther is another registered protocol handler.
  195. //    Revision History:
  196. //      02-17-95    created GAB
  197. //
  198.  
  199.     //  See if anyone's already handling the protocol.
  200.     if(NULL != Find(csProtocol))    {
  201.         return(FALSE);
  202.     }
  203.  
  204.     //  Otherwise, we need to register this service to do it's job.
  205.     COLEProtocolItem *pDontCare = new COLEProtocolItem(csProtocol, csServiceName);
  206.  
  207.     //  Also, we need to save this information into the INI file.
  208.     theApp.WriteProfileString("Automation Protocols", csProtocol, csServiceName);
  209.  
  210.     return(TRUE);
  211. }
  212.  
  213. BOOL COLEProtocolItem::OLEUnRegister(CString& csProtocol, CString& csServiceName)   {
  214. //    Purpose:    Attempt to unregister a protocol handler.
  215. //    Arguments:    csProtocol    The protocol to not handle.
  216. //                csServiceName    Should be the currently registered handler name.
  217. //    Returns:    BOOL    TRUE    could unregister.
  218. //                        FALSE    couldn't unregister, as was never registered.
  219. //    Comments:
  220. //    Revision History:
  221. //        02-17-95    created GAB
  222. //
  223.  
  224.     //    Attempt to find the current handler.
  225.     CProtocolItem *pItem = Find(csProtocol);
  226.     if(pItem == NULL)    {
  227.         return(FALSE);
  228.     }
  229.     
  230.     //    See if it's OLE.
  231.     if(pItem->GetType() != m_OLE)    {
  232.         return(FALSE);
  233.     }
  234.     
  235.     //    See if it's the same name.
  236.     COLEProtocolItem *pOLEItem = (COLEProtocolItem *)pItem;
  237.     if(pOLEItem->GetServiceName() != csServiceName)    {
  238.         return(FALSE);
  239.     }
  240.     
  241.     //    Get rid of it.
  242.     //  Out of the INI file too.
  243.     delete pOLEItem;
  244.     theApp.WriteProfileString("Automation Protocols", csProtocol, NULL);
  245.  
  246.     return(TRUE);
  247. }
  248.  
  249. BOOL COLEProtocolItem::OpenURL(URL_Struct *pURL, MWContext *pContext, FO_Present_Types iFormatOut)    {
  250. //    Purpose:    Have a OLE service name open up a URL.
  251. //    Arguments:    pURL    The url to open.
  252. //                pContext    The context in which it is opening.
  253. //                iFormatOut    The format out.
  254. //    Returns:    BOOL    TRUE    can open.
  255. //                        FALSE    can't open; the server won't talk.
  256. //    Comments:    virtual handler for OpenURL resolves to correct class for protocol handling.
  257. //    Revision History:
  258. //        02-17-95    created GAB
  259. //
  260.  
  261.     //  Only handle, FO_PRESENT types for now.
  262.     if((iFormatOut & FO_PRESENT) != FO_PRESENT)    {
  263.         return(FALSE);
  264.     }
  265.  
  266.     //  Start up a conversation with our protocol handler.
  267.     IIProtocol1 ProtocolHandler;
  268.     TRY {
  269.         ProtocolHandler.CreateDispatch(m_csServiceName);
  270.         if(ProtocolHandler.Initialize(m_csProtocol, pURL->address) == 0)    {
  271.             return(FALSE);
  272.         }
  273.         ProtocolHandler.Open(pURL->address);
  274.     }
  275.     CATCH(CException, e)    {   //  Any exception will do
  276.         
  277.         //  Tell the user that we were unable to complete the operation, ask them if they would like to unregister the handler.
  278.         TRACE("Couldn't connect to the OLE automation server\n");
  279.         CString csMessage = szLoadString(IDS_OLE_CANTUSE_HANDLER);
  280.         csMessage += m_csServiceName;
  281.         csMessage += ".  ";
  282.         csMessage += szLoadString(IDS_OLE_CANTUSE_VIEWER2);
  283.  
  284.         if(IDNO == AfxMessageBox(csMessage, MB_YESNO))   {
  285.             //  They don't want to use the handler in the future.
  286.             //  Unregister it.
  287.             OLEUnRegister(m_csProtocol, m_csServiceName);
  288.         }
  289.         return(FALSE);
  290.     }
  291.     END_CATCH;
  292.  
  293.     return(TRUE);
  294. }
  295.  
  296. CDDEProtocolItem::CDDEProtocolItem(CString& csProtocol, CString& csServiceName) : CProtocolItem(csProtocol, csServiceName, m_DDE)   {
  297. //    Purpose:    Construct an DDE protocol item.
  298. //    Arguments:  csProtocol  The protocol handler type
  299. //              csServiceName   The service name (DDE server) of the protocol
  300. //    Returns:    none
  301. //    Comments:   Used to initialize the type in the base class.
  302. //    Revision History:
  303. //      02-18-95    created GAB
  304. //
  305.     TRACE("Creating a DDE protocol handler\n");
  306.     m_rIndex = m_Registry.AddTail(this);
  307. }
  308.  
  309. BOOL CDDEProtocolItem::DDERegister(CString& csProtocol, CString& csServiceName)    {
  310. //    Purpose:    Attempt to register a DDE protocol handler.
  311. //    Arguments:    csProtocol    The protocol to handle.
  312. //                csServiceName    The DDE service name of the handling applciation.
  313. //    Returns:    BOOL    TRUE    Registered.
  314. //                        FALSE    Not registered.
  315. //    Comments:    Only returns FALSE if there is an already registered protcol handler.
  316. //    Revision History:
  317. //        01-18-95    created GAB
  318. //
  319.  
  320.     //    This is really easy.
  321.     //    If we find a prtocol handler for this protocl, then we can't do it.
  322.     if(NULL != Find(csProtocol))    {
  323.         return(FALSE);
  324.     }
  325.     
  326.     //    Otherwise, simply create a new protocol item.
  327.     CDDEProtocolItem *pDontCare = new CDDEProtocolItem(csProtocol, csServiceName);
  328.     if(pDontCare != NULL)    {
  329.         return(TRUE);
  330.     }
  331.     return(FALSE);
  332. }
  333.  
  334. BOOL CDDEProtocolItem::DDEUnRegister(CString& csProtocol, CString& csServiceName)    {
  335. //    Purpose:    Attempt to unregister a protocol handler.
  336. //    Arguments:    csProtocol    The protocol to not handle.
  337. //                csServiceName    Should be the currently registered handler name.
  338. //    Returns:    BOOL    TRUE    could unregister.
  339. //                        FALSE    couldn't unregister, as was never registered.
  340. //    Comments:
  341. //    Revision History:
  342. //        01-18-95    created GAB
  343. //
  344.  
  345.     //    Attempt to find the current handler.
  346.     CProtocolItem *pItem = Find(csProtocol);
  347.     if(pItem == NULL)    {
  348.         return(FALSE);
  349.     }
  350.     
  351.     //    See if it's DDE.
  352.     if(pItem->GetType() != m_DDE)    {
  353.         return(FALSE);
  354.     }
  355.     
  356.     //    See if it's the same name.
  357.     CDDEProtocolItem *pDDEItem = (CDDEProtocolItem *)pItem;
  358.     if(pDDEItem->GetServiceName() != csServiceName)    {
  359.         return(FALSE);
  360.     }
  361.     
  362.     //    Get rid of it.
  363.     delete pDDEItem;
  364.     return(TRUE);
  365. }
  366.  
  367. BOOL CDDEProtocolItem::OpenURL(URL_Struct *pURL, MWContext *pContext, FO_Present_Types iFormatOut)    {
  368. //    Purpose:    Have a DDE service name open up a URL.
  369. //    Arguments:    pURL    The url to open.
  370. //                pContext    The context in which it is opening.
  371. //                iFormatOut    The format out, wether to save or not basically.
  372. //    Returns:    BOOL    TRUE    can open.
  373. //                        FALSE    can't open; the server won't talk.
  374. //    Comments:    virtual handler for OpenURL resolves to correct class for protocol handling.
  375. //                Calls back into DDE code for actual opening.
  376. //    Revision History:
  377. //        01-18-95    created GAB
  378. //
  379.  
  380.     //    Just call the DDE implementation to open up the URL.
  381.     //    Be sure to let them know the service name.
  382.     return(CDDEWrapper::OpenURL(m_csProtocol, m_csServiceName, pURL, pContext, iFormatOut));
  383. }
  384.