home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / nsshell.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.9 KB  |  199 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. #include "slavewnd.h"
  21.  
  22. #ifndef WIN32
  23. #include "ddeml2.h"
  24. #else
  25. #include <ddeml.h>
  26. #endif // WIN32
  27.  
  28. /*-----------------------------------------------------------------------**
  29. ** Purpose of this file is to provide a DDE server which we use to catch **
  30. **     shell events like shell\open\command, etc.                        **
  31. **-----------------------------------------------------------------------*/
  32.  
  33. class CNSShell {
  34. public:
  35.     CNSShell();
  36.     ~CNSShell();
  37. private:
  38.     UINT m_uServerActive;
  39.     DWORD m_dwID;
  40.     HSZ m_hszServerName;
  41.     HDDEDATA m_hddNameService;
  42.     void *m_pSlaveCookie;
  43. public:
  44.     HDDEDATA shell(UINT type, UINT fmt, HCONV hconv, HSZ hsz1, HSZ hsz2, HDDEDATA hData, DWORD dwData1, DWORD dwData2);
  45.     void ExitInstance();
  46. };
  47.  
  48. //  One global.
  49. CNSShell nsshell;
  50.  
  51. //  Dde callback function.
  52. HDDEDATA CALLBACK 
  53. #ifndef _WIN32
  54. _export 
  55. #endif
  56. _nsshell(UINT type, UINT fmt, HCONV hconv, HSZ hsz1, HSZ hsz2, HDDEDATA hData, DWORD dwData1, DWORD dwData2)   {
  57.     HDDEDATA hRetval = NULL;
  58.     hRetval = nsshell.shell(type, fmt, hconv, hsz1, hsz2, hData, dwData1, dwData2);
  59.     return(hRetval);
  60. }
  61.  
  62. //  slavewnd callback function.
  63. void nsshell_ExitInstance(UINT uMessage, WPARAM wParam, LPARAM lParam) {
  64.     ASSERT(SLAVE_EXITINSTANCE == uMessage);
  65.     if(SLAVE_EXITINSTANCE == uMessage) {
  66.         nsshell.ExitInstance();
  67.     }
  68. }
  69.  
  70. CNSShell::CNSShell() {
  71.     //  Initialize variables.
  72.     m_uServerActive = 0;
  73.     m_dwID = 0;
  74.     m_hszServerName = NULL;
  75.     m_hddNameService = NULL;
  76.     m_pSlaveCookie = NULL;
  77.     
  78.     //  Register to receive the exit instance event, such that we know
  79.     //      to turn off the DDE server.
  80.     m_pSlaveCookie = slavewnd.Register(SLAVE_EXITINSTANCE, nsshell_ExitInstance);
  81.     
  82.     //  Initialize a DDE server.
  83.     m_uServerActive = ::DdeInitialize(&m_dwID, _nsshell, APPCLASS_STANDARD, 0);
  84.     if(DMLERR_NO_ERROR == m_uServerActive) {
  85.         m_hszServerName = ::DdeCreateStringHandle(m_dwID, "nsshell", CP_WINANSI);
  86.         if(m_hszServerName) {
  87.             m_hddNameService = ::DdeNameService(m_dwID, m_hszServerName, NULL, DNS_REGISTER);
  88.         }
  89.     }
  90. }
  91.  
  92. CNSShell::~CNSShell() {
  93.     //  Remove our slave window callback.
  94.     if(m_pSlaveCookie) {
  95.         BOOL bAssert = slavewnd.UnRegister(m_pSlaveCookie);
  96.         ASSERT(bAssert);
  97.         m_pSlaveCookie = NULL;
  98.     }
  99.  
  100.     //  Ensure ExitInstance was called.
  101.     if(m_dwID) {
  102.         ExitInstance();
  103.     }
  104. }
  105.  
  106. void CNSShell::ExitInstance() {
  107.     //  De-initialize DDE server.
  108.     if(DMLERR_NO_ERROR == m_uServerActive) {
  109.         if(m_dwID) {
  110.             if(m_hszServerName) {
  111.                 if(m_hddNameService) {
  112.                     ::DdeNameService(m_dwID, m_hszServerName, NULL, DNS_UNREGISTER);
  113.                     m_hddNameService = NULL;
  114.                 }
  115.                 ::DdeFreeStringHandle(m_dwID, m_hszServerName);
  116.                 m_hszServerName = NULL;
  117.             }
  118.             DdeUninitialize(m_dwID);
  119.             m_dwID = 0;
  120.         }
  121.     }
  122.     m_uServerActive = 0;
  123. }
  124.  
  125. HDDEDATA CNSShell::shell(UINT type, UINT fmt, HCONV hconv, HSZ hsz1, HSZ hsz2, HDDEDATA hData, DWORD dwData1, DWORD dwData2) {
  126.     HDDEDATA hResult = NULL;
  127.  
  128.     switch(type)    {
  129.     //  Only need to handle execute type transactions from shell.
  130.     case XTYP_EXECUTE:  {
  131.         hResult = (HDDEDATA)DDE_FNOTPROCESSED;
  132.         if(hData) {
  133.             DWORD dwSize = ::DdeGetData(hData, NULL, 0, 0);
  134.             if(dwSize) {
  135.                 char *pMemory = new char[dwSize];
  136.                 if(pMemory) {
  137.                     if(::DdeGetData(hData, (BYTE *)pMemory, dwSize, 0)) {
  138.                         //  Don't hand over to netscape if in init instance
  139.                         //      (assume command is on command line).
  140.                         //  Acknowledge the request however.
  141.                         if(theApp.m_bInInitInstance) {
  142.                             hResult = (HDDEDATA)DDE_FACK;
  143.                         }
  144.                         //  Don't handle if we are exiting also.
  145.                         //  No warning is given that nothing will happen,
  146.                         //      but we avoid crashing.
  147.                         else if(theApp.m_bExitStatus) {
  148.                             hResult = (HDDEDATA)DDE_FACK;
  149.                         }
  150.                         //  We receive URLs when there is no leading '['.
  151.                         //  Add [openurl("")] to it if so.
  152.                         else if('[' != pMemory[0]) {
  153.                             char *pArg = new char[dwSize + 13];
  154.                             if(pArg) {
  155.                                 strcpy(pArg, "[openurl(");
  156.                                 if('\"' != pMemory[0]) {
  157.                                     strcat(pArg, "\"");
  158.                                 }
  159.                                 strcat(pArg, pMemory);
  160.                                 if('\"' != pMemory[0]) {
  161.                                     strcat(pArg, "\"");
  162.                                 }
  163.                                 strcat(pArg, ")]");
  164.                                 
  165.                                 //  Have Netscape handle it.
  166.                                 if(theApp.OnDDECommand(pArg)) {
  167.                                     hResult = (HDDEDATA)DDE_FACK;
  168.                                 }
  169.                                 
  170.                                 delete[] pArg;
  171.                                 pArg = NULL;
  172.                             }
  173.                         }
  174.                         //  Have Netscape handle it as is.
  175.                         else {
  176.                             if(theApp.OnDDECommand(pMemory)) {
  177.                                 hResult = (HDDEDATA)DDE_FACK;
  178.                             }
  179.                         }
  180.                     }
  181.                     delete[] pMemory;
  182.                     pMemory = NULL;
  183.                 }
  184.             }
  185.         }
  186.         
  187.         break;
  188.                         }
  189.     //  Accept any connection.
  190.     case XTYP_CONNECT:  {
  191.         hResult = (HDDEDATA)TRUE;
  192.         break;
  193.                         }
  194.     }
  195.  
  196.     return(hResult);
  197. }
  198.  
  199.