home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / oleregis.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  11.4 KB  |  321 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. // oleregis.cpp : implementation file
  20. //
  21.  
  22. #include "stdafx.h"
  23.  
  24. #include "oleregis.h"
  25.  
  26. #include "regproto.h"
  27. #include "olectc.h"
  28. #include "presentm.h"
  29. #include "olestart.h"
  30. #include "oleshut.h"
  31.  
  32. #ifdef _DEBUG
  33. #undef THIS_FILE
  34. static char BASED_CODE THIS_FILE[] = __FILE__;
  35. #endif
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // COleRegistry
  39.  
  40. #ifndef _AFXDLL
  41. #undef new
  42. #endif
  43. IMPLEMENT_DYNCREATE(COleRegistry, CCmdTarget)
  44. #ifndef _AFXDLL
  45. #define new DEBUG_NEW
  46. #endif
  47.  
  48. COleRegistry::COleRegistry()
  49. {
  50.     EnableAutomation();
  51.     
  52.     // To keep the application running as long as an OLE automation 
  53.     //    object is active, the constructor calls AfxOleLockApp.
  54.     
  55.     AfxOleLockApp();
  56. }
  57.  
  58. COleRegistry::~COleRegistry()
  59. {
  60.     // To terminate the application when all objects created with
  61.     //     with OLE automation, the destructor calls AfxOleUnlockApp.
  62.     
  63.     AfxOleUnlockApp();
  64. }
  65.  
  66. void COleRegistry::OnFinalRelease()
  67. {
  68.     // When the last reference for an automation object is released
  69.     //    OnFinalRelease is called.  This implementation deletes the 
  70.     //    object.  Add additional cleanup required for your object before
  71.     //    deleting it from memory.
  72.  
  73.     delete this;
  74. }
  75.  
  76. void COleRegistry::RegisterIniProtocolHandlers()    {
  77. //    Purpose:    Read in the INI entries, and register them as valid automated handlers.
  78. //    Arguments:  void
  79. //    Returns:    void
  80.  
  81.     //  Over 2K is a nightmare.
  82.     char *aBuffer = new char[4096];
  83.     theApp.GetPrivateProfileString("Automation Protocols", NULL, "", aBuffer, 4096, AfxGetApp()->m_pszProfileName);
  84.  
  85.     //  We have a double null terminated list of mime types here.
  86.     //  Go ahead and go through each one, and then obtain the value for each.
  87.     char *pTraverse = aBuffer;
  88.     CString csServerName;
  89.     while(*pTraverse != '\0')   {
  90.         //  Get the entry for this mime type.
  91.         csServerName = theApp.GetProfileString("Automation Protocols", pTraverse);
  92.         if(csServerName.IsEmpty() != TRUE)  {
  93.             //  Have a valid entry.  Cause it to be registered.
  94.             CString csProtocol = pTraverse;
  95.             COLEProtocolItem::OLERegister(csProtocol, csServerName);
  96.         }
  97.  
  98.         //  Go on to the next entry.
  99.         while(*pTraverse != '\0')   {
  100.             pTraverse++;
  101.         }
  102.         pTraverse++;
  103.     }
  104.  
  105.     delete[] aBuffer;
  106. }
  107.  
  108. void COleRegistry::RegisterIniViewers() {
  109. //    Purpose:    Read in the INI entries, and register them as valid automated viewers.
  110. //    Arguments:  void
  111. //    Returns:    void
  112. //    Comments:   This should really only be called after all other normal viewers are registered.
  113.  
  114.     //  Over 2K is a nightmare.
  115.     char *aBuffer = new char[4096];
  116.     theApp.GetPrivateProfileString("Automation Viewers", NULL, "", aBuffer, 4096, AfxGetApp()->m_pszProfileName);
  117.  
  118.     //  We have a double null terminated list of mime types here.
  119.     //  Go ahead and go through each one, and then obtain the value for each.
  120.     char *pTraverse = aBuffer;
  121.     CString csServerName;
  122.     while(*pTraverse != '\0')   {
  123.         //  Get the entry for this mime type.
  124.         csServerName = theApp.GetProfileString("Automation Viewers", pTraverse);
  125.         if(csServerName.IsEmpty() != TRUE)  {
  126.             //  Have a valid entry.  Cause it to be registered.
  127.             COLEStreamData *pOData = new COLEStreamData(csServerName, pTraverse);
  128.             if(FALSE == WPM_RegisterContentTypeConverter(pTraverse, FO_PRESENT, (void *)pOData, ole_stream, TRUE))  {
  129.                 //  Couldn't do it.
  130.                 delete pOData;
  131.             }
  132.         }
  133.  
  134.         //  Go on to the next entry.
  135.         while(*pTraverse != '\0')   {
  136.             pTraverse++;
  137.         }
  138.         pTraverse++;
  139.     }
  140.  
  141.     delete[] aBuffer;
  142. }
  143.  
  144. //  Cause all startup watchers to be fired.
  145. //  Depending on the number, this could decrease startup time significantly.
  146. //  Be sure to always have the splash screen say what's going on, so the user
  147. //      knows it's the fault of another application.
  148. void COleRegistry::Startup()
  149. {
  150.     //  Go through all entries under "Automation Startup"
  151.     char *aBuffer = new char[4096];
  152.     theApp.GetPrivateProfileString("Automation Startup", NULL, "", aBuffer, 4096, AfxGetApp()->m_pszProfileName);
  153.  
  154.     //  We have a double null terminated list of OLE objects here.
  155.     //  Go ahead and go through each one.
  156.     char *pTraverse = aBuffer;
  157.     CString csServerName;
  158.     while(*pTraverse != '\0')   {
  159.         //  Get the entry for this object.
  160.         csServerName = pTraverse;
  161.         if(csServerName.IsEmpty() != TRUE)  {
  162.             //  Update the splash screen if present.
  163.             if(theApp.m_splash.m_hWnd && theApp.m_splash.IsWindowVisible())   {
  164.                 theApp.m_splash.DisplayStatus((LPCSTR)csServerName);
  165.             }
  166.  
  167.             //  start up a conversation with out startup wannabe.
  168.             IStartup isNotify;
  169.             TRY {
  170.                 isNotify.CreateDispatch(csServerName);
  171.                 isNotify.Initialize((long)theApp.m_hInstance);
  172.             }
  173.             CATCH(CException, e)    {
  174.                 //  Couldn't inform/create the object.
  175.                 TRACE("Couldn't notify startup object %s\n", (const char *)csServerName);
  176.             }
  177.             END_CATCH
  178.         }
  179.  
  180.         //  Go on to the next entry.
  181.         while(*pTraverse != '\0')   {
  182.             pTraverse++;
  183.         }
  184.         pTraverse++;
  185.     }
  186.  
  187.     delete[] aBuffer;
  188. }
  189.  
  190. //  Cause all startup watchers to be fired.
  191. //  Depending on the number, this could decrease shutup time significantly.
  192. void COleRegistry::Shutdown()
  193. {
  194.     //  Go through all entries under "Automation Shutdown"
  195.     char *aBuffer = new char[4096];
  196.     theApp.GetPrivateProfileString("Automation Shutdown", NULL, "", aBuffer, 4096, AfxGetApp()->m_pszProfileName);
  197.  
  198.     //  We have a double null terminated list of OLE objects here.
  199.     //  Go ahead and go through each one.
  200.     char *pTraverse = aBuffer;
  201.     CString csServerName;
  202.     while(*pTraverse != '\0')   {
  203.         //  Get the entry for this object.
  204.         csServerName = pTraverse;
  205.         if(csServerName.IsEmpty() != TRUE)  {
  206.             //  start up a conversation with out startup wannabe.
  207.             IShutdown isNotify;
  208.             TRY {
  209.                 isNotify.CreateDispatch(csServerName);
  210.                 isNotify.Initialize((long)theApp.m_hInstance);
  211.             }
  212.             CATCH(CException, e)    {
  213.                 //  Couldn't inform/create the object.
  214.                 TRACE("Couldn't notify shutdown object %s\n", (const char *)csServerName);
  215.             }
  216.             END_CATCH
  217.         }
  218.  
  219.         //  Go on to the next entry.
  220.         while(*pTraverse != '\0')   {
  221.             pTraverse++;
  222.         }
  223.         pTraverse++;
  224.     }
  225.  
  226.     delete[] aBuffer;
  227. }
  228.  
  229. BEGIN_MESSAGE_MAP(COleRegistry, CCmdTarget)
  230.     //{{AFX_MSG_MAP(COleRegistry)
  231.         // NOTE - the ClassWizard will add and remove mapping macros here.
  232.     //}}AFX_MSG_MAP
  233. END_MESSAGE_MAP()
  234.  
  235. BEGIN_DISPATCH_MAP(COleRegistry, CCmdTarget)
  236.     //{{AFX_DISPATCH_MAP(COleRegistry)
  237.     DISP_FUNCTION(COleRegistry, "RegisterViewer", RegisterViewer, VT_BOOL, VTS_BSTR VTS_BSTR)
  238.     DISP_FUNCTION(COleRegistry, "RegisterProtocol", RegisterProtocol, VT_BOOL, VTS_BSTR VTS_BSTR)
  239.     DISP_FUNCTION(COleRegistry, "RegisterStartup", RegisterStartup, VT_BOOL, VTS_BSTR)
  240.     DISP_FUNCTION(COleRegistry, "RegisterShutdown", RegisterShutdown, VT_BOOL, VTS_BSTR)
  241.     //}}AFX_DISPATCH_MAP
  242. END_DISPATCH_MAP()
  243.  
  244. IMPLEMENT_OLECREATE(COleRegistry, "Netscape.Registry.1", 0xe67d6a10, 0x4438, 0x11ce, 0x8c, 0xe4, 0x0, 0x20, 0xaf, 0x18, 0xf9, 0x5)
  245.  
  246. /////////////////////////////////////////////////////////////////////////////
  247. // COleRegistry message handlers
  248.  
  249. BOOL COleRegistry::RegisterViewer(LPCTSTR pMimeType, LPCTSTR pRegistryName) {
  250. //    Purpose:    Register a particular autmation server as an External Viewer.
  251. //    Arguments:  pMimeType   The mime type they'd like to handle.
  252. //              pRegistryName   The name of the registered automation server, as known to the system registry.
  253. //    Returns:    BOOL    TRUE    The registration was a success.
  254. //                      FALSE   The registration failed, another OLE automation server is already registered for said
  255. //                                  MimeType.
  256. //    Comments:   One way registration; is probably wrong, but future revisions will address this problem.
  257.  
  258.     TRACE("RegisterViewer(%s, %s)\n", pMimeType, pRegistryName);
  259.  
  260.     //  If it's not a valid request, reject it right now.
  261.     if(pMimeType == NULL || pRegistryName == NULL)  {
  262.         return(FALSE);
  263.     }
  264.  
  265.     //  Allright, create a new item to attempt to add to the list of registered viewers.
  266.     COLEStreamData *pOData = new COLEStreamData(pRegistryName, pMimeType);
  267.     if(FALSE == WPM_RegisterContentTypeConverter((char *)pMimeType, FO_PRESENT, (void *)pOData, ole_stream, TRUE))  {
  268.         //  Couldn't do it.
  269.         //  Tell them that a converter is already registered, be it DDE or OLE.
  270.         delete pOData;
  271.         return(FALSE);
  272.     }
  273.  
  274.  
  275.     //  Add the string to the INI file for retrieval on startup.
  276.     theApp.WriteProfileString("Automation Viewers", pMimeType, pRegistryName);
  277.  
  278.     return(TRUE);
  279. }
  280.  
  281. BOOL COleRegistry::RegisterProtocol(LPCTSTR pProtocol, LPCTSTR pRegistryName)    {
  282. //    Purpose:    Register a particular autmation server as an External Protocol handler.
  283. //    Arguments:  pProtocol   The protocol they'd like to handle.
  284. //              pRegistryName   The name of the registered automation server, as known to the system registry.
  285. //    Returns:    BOOL    TRUE    The registration was a success.
  286. //                      FALSE   The registration failed, another OLE automation server is already registered for said
  287. //                                  Protocol.
  288. //    Comments:   One way registration; is probably wrong, but future revisions will address this problem.
  289.  
  290.     TRACE("RegisterProtocol(%s, %s)\n", pProtocol, pRegistryName);
  291.  
  292.     //  First, return failure on any stupid cases.
  293.     if(pProtocol == NULL || pRegistryName == NULL)  {
  294.         return(FALSE);
  295.     }
  296.  
  297.     CString csProtocol = pProtocol;
  298.     CString csServiceName = pRegistryName;
  299.     return(COLEProtocolItem::OLERegister(csProtocol, csServiceName));
  300. }
  301.  
  302. //  New startup object being registered.
  303. //  Save to Registry/INI.
  304. BOOL COleRegistry::RegisterStartup(LPCTSTR pRegistryName)
  305. {
  306.     theApp.WriteProfileString("Automation Startup", pRegistryName, "");
  307.  
  308.     //  Always succeed for now.
  309.     return (TRUE);
  310. }
  311.  
  312. //  New shutdown object being registered.
  313. //  Save to Registry/INI.
  314. BOOL COleRegistry::RegisterShutdown(LPCTSTR pRegistryName)
  315. {
  316.     theApp.WriteProfileString("Automation Shutdown", pRegistryName, "");
  317.  
  318.     //  Always succeed for now.
  319.     return (TRUE);
  320. }
  321.