pxcmpmgr.cpp
上传用户:dangjiwu
上传日期:2013-07-19
资源大小:42019k
文件大小:13k
源码类别:

Symbian

开发平台:

Visual C++

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Source last modified: $Id: pxcmpmgr.cpp,v 1.1.26.1 2004/07/09 01:54:47 hubbe Exp $
  3.  * 
  4.  * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
  5.  * 
  6.  * The contents of this file, and the files included with this file,
  7.  * are subject to the current version of the RealNetworks Public
  8.  * Source License (the "RPSL") available at
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10.  * the file under the current version of the RealNetworks Community
  11.  * Source License (the "RCSL") available at
  12.  * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13.  * will apply. You may also obtain the license terms directly from
  14.  * RealNetworks.  You may not use this file except in compliance with
  15.  * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16.  * to this file, the RCSL.  Please see the applicable RPSL or RCSL for
  17.  * the rights, obligations and limitations governing use of the
  18.  * contents of the file.
  19.  * 
  20.  * Alternatively, the contents of this file may be used under the
  21.  * terms of the GNU General Public License Version 2 or later (the
  22.  * "GPL") in which case the provisions of the GPL are applicable
  23.  * instead of those above. If you wish to allow use of your version of
  24.  * this file only under the terms of the GPL, and not to allow others
  25.  * to use your version of this file under the terms of either the RPSL
  26.  * or RCSL, indicate your decision by deleting the provisions above
  27.  * and replace them with the notice and other provisions required by
  28.  * the GPL. If you do not delete the provisions above, a recipient may
  29.  * use your version of this file under the terms of any one of the
  30.  * RPSL, the RCSL or the GPL.
  31.  * 
  32.  * This file is part of the Helix DNA Technology. RealNetworks is the
  33.  * developer of the Original Code and owns the copyrights in the
  34.  * portions it created.
  35.  * 
  36.  * This file, and the files included with this file, is distributed
  37.  * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  38.  * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  39.  * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  40.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  41.  * ENJOYMENT OR NON-INFRINGEMENT.
  42.  * 
  43.  * Technology Compatibility Kit Test Suite(s) Location:
  44.  *    http://www.helixcommunity.org/content/tck
  45.  * 
  46.  * Contributor(s):
  47.  * 
  48.  * ***** END LICENSE BLOCK ***** */
  49. // include
  50. #include "hxtypes.h"
  51. #include "hxcom.h"
  52. #include "hxplugn.h"
  53. #include "hxcomm.h"
  54. // hxcont
  55. #include "hxslist.h"
  56. #include "hxmap.h"
  57. // hxmisc
  58. #include "baseobj.h"
  59. // pxcomlib
  60. #include "pxcmpmgr.h"
  61. // hxdebug
  62. #include "hxheap.h"
  63. #ifdef _DEBUG
  64. #undef HX_THIS_FILE
  65. static char HX_THIS_FILE[] = __FILE__;
  66. #endif
  67. PXComponentManager::PXComponentManager()
  68. {
  69.     m_pComponentList = NULL;
  70.     m_pActiveMap     = NULL;
  71. }
  72. PXComponentManager::~PXComponentManager()
  73. {
  74.     ReleaseAllComponents();
  75.     HX_DELETE(m_pComponentList);
  76.     ClearActiveMap();
  77.     HX_DELETE(m_pActiveMap);
  78. }
  79. STDMETHODIMP PXComponentManager::Init(IUnknown* pContext, REFIID riid)
  80. {
  81.     HX_RESULT                  retVal   = HXR_OK;
  82.     IHXCommonClassFactory*    pFactory = NULL;
  83.     IHXPluginGroupEnumerator* pEnum    = NULL;
  84.     if (pContext)
  85.     {
  86.         // Clear out any current components
  87.         ReleaseAllComponents();
  88.         HX_DELETE(m_pComponentList);
  89.         // Clear any existing active map
  90.         ClearActiveMap();
  91.         HX_DELETE(m_pActiveMap);
  92.         // Allocate a new component list
  93.         m_pComponentList = new CHXSimpleList();
  94.         if (m_pComponentList)
  95.         {
  96.             // Allocate a new active map
  97.             m_pActiveMap = new CHXMapStringToOb();
  98.             if (m_pActiveMap)
  99.             {
  100.                 // First try and QI the context for IHXPluginGroupEnumerator
  101.                 HX_RESULT rv = pContext->QueryInterface(IID_IHXPluginGroupEnumerator, (void**) &pEnum);
  102.                 if (FAILED(rv))
  103.                 {
  104.                     // Get an IHXCommonClassFactory interface
  105.                     retVal = pContext->QueryInterface(IID_IHXCommonClassFactory, (void**) &pFactory);
  106.                     if (SUCCEEDED(retVal))
  107.                     {
  108.                         // Get an IHXPluginGroupEnumerator interface
  109.                         retVal = pFactory->CreateInstance(CLSID_IHXPluginGroupEnumerator, (void**) &pEnum);
  110.                     }
  111.                 }
  112.                 if (SUCCEEDED(retVal))
  113.                 {
  114.                     // Initialize with the IID
  115.                     retVal = pEnum->Init(riid);
  116.                     if (SUCCEEDED(retVal))
  117.                     {
  118.                         // Get the number of codecs
  119.                         UINT32 ulNumComponents = pEnum->GetNumOfPlugins();
  120.                         // Get an interface to each codec
  121.                         for (UINT32 i = 0; i < ulNumComponents; i++)
  122.                         {
  123.                             IUnknown* pComponent = NULL;
  124.                             retVal               = pEnum->GetPlugin(i, pComponent);
  125.                             if (SUCCEEDED(retVal))
  126.                             {
  127.                                 // See if it supports IHXPlugin. If it does, then
  128.                                 // call InitPlugin(). It's OK if it doesn't support IHXPlugin
  129.                                 IHXPlugin* pPlugin = NULL;
  130.                                 HX_RESULT rv        = pComponent->QueryInterface(IID_IHXPlugin,
  131.                                                                                  (void**) &pPlugin);
  132.                                 if (SUCCEEDED(rv))
  133.                                 {
  134.                                     // Call IHXPlugin::InitPlugin()
  135.                                     retVal = pPlugin->InitPlugin(pContext);
  136.                                 }
  137.                                 HX_RELEASE(pPlugin);
  138.                                 if (SUCCEEDED(retVal))
  139.                                 {
  140.                                     // Addref the component before it goes into the list
  141.                                     pComponent->AddRef();
  142.                                     // Add this codec to the list
  143.                                     m_pComponentList->AddTail((void*) pComponent);
  144.                                 }
  145.                             }
  146.                             HX_RELEASE(pComponent);
  147.                             if (FAILED(retVal))
  148.                             {
  149.                                 break;
  150.                             }
  151.                         }
  152.                     }
  153.                 }
  154.             }
  155.             else
  156.             {
  157.                 retVal = HXR_OUTOFMEMORY;
  158.             }
  159.         }
  160.         else
  161.         {
  162.             retVal = HXR_OUTOFMEMORY;
  163.         }
  164.     }
  165.     else
  166.     {
  167.         retVal = HXR_UNEXPECTED;
  168.     }
  169.     HX_RELEASE(pEnum);
  170.     HX_RELEASE(pFactory);
  171.     if (FAILED(retVal))
  172.     {
  173.         ReleaseAllComponents();
  174.         HX_DELETE(m_pComponentList);
  175.         ClearActiveMap();
  176.         HX_DELETE(m_pActiveMap);
  177.     }
  178.     return retVal;
  179. }
  180. STDMETHODIMP PXComponentManager::IsComponentPresent(const char* pszID,  BOOL* pbPresent)
  181. {
  182.     HX_RESULT retVal = HXR_OK;
  183.     if (pszID && pbPresent)
  184.     {
  185.         // Set defaults
  186.         *pbPresent = FALSE;
  187.         if (m_pComponentList)
  188.         {
  189.             CHXString cQueryStr(pszID);
  190.             LISTPOSITION pos = m_pComponentList->GetHeadPosition();
  191.             while (pos)
  192.             {
  193.                 IUnknown* pComponent = (IUnknown*) m_pComponentList->GetNext(pos);
  194.                 if (pComponent)
  195.                 {
  196.                     const char* pszComponentID = NULL;
  197.                     retVal                     = GetID(pComponent, pszComponentID);
  198.                     if (SUCCEEDED(retVal))
  199.                     {
  200.                         CHXString cComponentStr(pszComponentID);
  201.                         if (cQueryStr == cComponentStr)
  202.                         {
  203.                             *pbPresent = TRUE;
  204.                             break;
  205.                         }
  206.                     }
  207.                 }
  208.             }
  209.         }
  210.         else
  211.         {
  212.             retVal = HXR_UNEXPECTED;
  213.         }
  214.     }
  215.     else
  216.     {
  217.         retVal = HXR_INVALID_PARAMETER;
  218.     }
  219.     return retVal;
  220. }
  221. STDMETHODIMP PXComponentManager::GetComponent(const char* pszID,  BOOL* pbPresent, IUnknown** ppComponent)
  222. {
  223.     HX_RESULT retVal = HXR_OK;
  224.     if (pszID && pbPresent && ppComponent)
  225.     {
  226.         // Set defaults
  227.         *pbPresent   = FALSE;
  228.         *ppComponent = NULL;
  229.         if (m_pComponentList)
  230.         {
  231.             CHXString cQueryStr(pszID);
  232.             LISTPOSITION pos = m_pComponentList->GetHeadPosition();
  233.             while (pos)
  234.             {
  235.                 IUnknown* pListComponent = (IUnknown*) m_pComponentList->GetNext(pos);
  236.                 if (pListComponent)
  237.                 {
  238.                     const char* pszComponentID = NULL;
  239.                     retVal                     = GetID(pListComponent, pszComponentID);
  240.                     if (SUCCEEDED(retVal))
  241.                     {
  242.                         CHXString cComponentStr(pszComponentID);
  243.                         if (cQueryStr == cComponentStr)
  244.                         {
  245.                             *pbPresent   = TRUE;
  246.                             *ppComponent = pListComponent;
  247.                             (*ppComponent)->AddRef();
  248.                             break;
  249.                         }
  250.                     }
  251.                 }
  252.             }
  253.         }
  254.         else
  255.         {
  256.             retVal = HXR_UNEXPECTED;
  257.         }
  258.     }
  259.     else
  260.     {
  261.         retVal = HXR_INVALID_PARAMETER;
  262.     }
  263.     return retVal;
  264. }
  265. STDMETHODIMP PXComponentManager::SetActiveComponent(const char* pszID)
  266. {
  267.     HX_RESULT retVal = HXR_OK;
  268.     if (pszID)
  269.     {
  270.         if (m_pActiveMap)
  271.         {
  272.             // Is there already an entry for this string?
  273.             void *pVoid = NULL;
  274.             if (!m_pActiveMap->Lookup(pszID, pVoid))
  275.             {
  276.                 // No entry for this string, so add one - doesn't matter
  277.                 // what we put for the target of the map (the void*) - we'll
  278.                 // never use it - it's just important that a map entry EXISTS.
  279.                 m_pActiveMap->SetAt(pszID, (void*) 1);
  280.             }
  281.         }
  282.         else
  283.         {
  284.             retVal = HXR_UNEXPECTED;
  285.         }
  286.     }
  287.     else
  288.     {
  289.         retVal = HXR_INVALID_PARAMETER;
  290.     }
  291.     return retVal;
  292. }
  293. STDMETHODIMP PXComponentManager::ReleaseInactiveComponents()
  294. {
  295.     HX_RESULT retVal = HXR_OK;
  296.     if (m_pComponentList && m_pActiveMap)
  297.     {
  298.         // Run through the codec list, seeing if the
  299.         // codec is on the active list. If it's not,
  300.         // then get rid of it.
  301.         LISTPOSITION pos = m_pComponentList->GetHeadPosition();
  302.         while (pos)
  303.         {
  304.             IUnknown* pComponent = (IUnknown*) m_pComponentList->GetAt(pos);
  305.             if (pComponent)
  306.             {
  307.                 // Get the id string
  308.                 const char* pszID  = NULL;
  309.                 retVal             = GetID(pComponent, pszID);
  310.                 if (SUCCEEDED(retVal))
  311.                 {
  312.                    // Is the id string in the map?
  313.                    void *pVoid = NULL;
  314.                    if (!m_pActiveMap->Lookup(pszID, pVoid))
  315.                    {
  316.                        // The id string IS NOT in the map, so the codec is
  317.                        // inactive, so we can release it and remove it
  318.                        // from the list
  319.                        HX_RELEASE(pComponent);
  320.                        pos = m_pComponentList->RemoveAt(pos);
  321.                    }
  322.                    else
  323.                    {
  324.                        m_pComponentList->GetNext(pos);
  325.                    }
  326.                 }
  327.             }
  328.             else
  329.             {
  330.                retVal = HXR_FAIL;
  331.             }
  332.             if (FAILED(retVal))
  333.             {
  334.                 break;
  335.             }
  336.         }
  337.     }
  338.     else
  339.     {
  340.         retVal = HXR_UNEXPECTED;
  341.     }
  342.     return retVal;
  343. }
  344. void PXComponentManager::ReleaseAllComponents()
  345. {
  346.     if (m_pComponentList)
  347.     {
  348.         // Release all codecs
  349.         LISTPOSITION pos = m_pComponentList->GetHeadPosition();
  350.         while (pos)
  351.         {
  352.             IUnknown* pComponent = (IUnknown*) m_pComponentList->GetNext(pos);
  353.             HX_RELEASE(pComponent);
  354.         }
  355.         // Clear the list object
  356.         m_pComponentList->RemoveAll();
  357.     }
  358. }
  359. void PXComponentManager::ClearActiveMap()
  360. {
  361.     if (m_pActiveMap)
  362.     {
  363.         // Clear all map entries
  364.         m_pActiveMap->RemoveAll();
  365.     }
  366. }