cddbevnt.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "./cddbevnt.h"
  2. #include "cddbinterface.h"
  3. #include <strsafe.h>
  4. #include <ocidl.h>
  5. #include "cddbcontrolwinamp.tlh"
  6. CDBBEventManager::CDBBEventManager(void) : ref(1), cookie(0), user(0),
  7. fnCmdCompleted(NULL), fnCmdProgress(NULL),
  8. fnLogMessage(NULL), fnServerMessage(NULL)
  9. {
  10. }
  11. CDBBEventManager::~CDBBEventManager(void)
  12. {
  13. }
  14. HRESULT CDBBEventManager::Advise(IUnknown *pCDDBCtrl)
  15. {
  16. HRESULT hr;
  17. IConnectionPoint *pcp;
  18. IConnectionPointContainer *pcpc;
  19. if (cookie) return E_FAIL;
  20. if (!pCDDBCtrl) return E_INVALIDARG;
  21. hr = pCDDBCtrl->QueryInterface(IID_IConnectionPointContainer, (PVOID*)&pcpc);
  22. if (SUCCEEDED (hr))
  23. {
  24. hr = pcpc->FindConnectionPoint(DIID_DCDDBEvents, &pcp);
  25. if (SUCCEEDED(hr))
  26. {
  27. hr = pcp->Advise(static_cast<IDispatch*>(this), &cookie);
  28. if (FAILED(hr)) cookie = 0;
  29. pcp->Release();
  30. }
  31. pcpc->Release();
  32. }
  33. return hr;
  34. }
  35. HRESULT CDBBEventManager::Unadvise(IUnknown *pCDDBCtrl)
  36. {
  37. HRESULT hr;
  38. IConnectionPoint *pcp = nullptr;
  39. IConnectionPointContainer *pcpc = nullptr;
  40. if (!cookie) return S_OK;
  41. if (!pCDDBCtrl) return E_INVALIDARG;
  42. hr = pCDDBCtrl->QueryInterface(IID_IConnectionPointContainer, (PVOID*)&pcpc);
  43. if (SUCCEEDED (hr))
  44. {
  45. hr = pcpc->FindConnectionPoint(DIID_DCDDBEvents, &pcp);
  46. if (SUCCEEDED(hr))
  47. {
  48. hr = pcp->Unadvise(cookie);
  49. pcp->Release();
  50. }
  51. pcpc->Release();
  52. }
  53. return hr;
  54. }
  55. HRESULT CDBBEventManager::RegisterCallback(UINT nType, void *fnCallback)
  56. {
  57. switch(nType)
  58. {
  59. case CDDB_CB_CMDCOMPLETED: fnCmdCompleted = (CDDB_CMDCOMPLETED)fnCallback; break;
  60. case CDDB_CB_CMDPROGRESS: fnCmdProgress = (CDDB_CMDPROGRESS)fnCallback; break;
  61. case CDDB_CB_LOGMSG: fnLogMessage = (CDDB_LOGMSG)fnCallback; break;
  62. case CDDB_CB_SRVMSG: fnServerMessage = (CDDB_SRVMSG)fnCallback; break;
  63. default: return E_INVALIDARG;
  64. }
  65. return S_OK;
  66. }
  67. ULONG_PTR CDBBEventManager::SetUserParam(ULONG_PTR userParam)
  68. {
  69. ULONG_PTR tmp = user;
  70. user = userParam;
  71. return tmp;
  72. }
  73. STDMETHODIMP CDBBEventManager::QueryInterface(REFIID riid, PVOID *ppvObject)
  74. {
  75. if (!ppvObject) return E_POINTER;
  76. if (IsEqualIID(riid, DIID_DCDDBEvents))
  77. *ppvObject = dynamic_cast<CDBBEventManager*>(this);
  78. else if (IsEqualIID(riid, IID_IDispatch))
  79. *ppvObject = dynamic_cast<IDispatch*>(this);
  80. else if (IsEqualIID(riid, IID_IUnknown))
  81. *ppvObject = dynamic_cast<IUnknown*>(this);
  82. else
  83. {
  84. *ppvObject = NULL;
  85. return E_NOINTERFACE;
  86. }
  87. AddRef();
  88. return S_OK;
  89. }
  90. ULONG CDBBEventManager::AddRef(void)
  91. {
  92. return InterlockedIncrement(&ref);
  93. }
  94. ULONG CDBBEventManager::Release(void)
  95. {
  96. if (ref && 0 == InterlockedDecrement(&ref))
  97. {
  98. delete this;
  99. return 0;
  100. }
  101. return ref;
  102. }
  103. STDMETHODIMP CDBBEventManager::GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgdispid)
  104. {
  105. return DISP_E_UNKNOWNNAME;
  106. }
  107. STDMETHODIMP CDBBEventManager::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo)
  108. {
  109. return E_NOTIMPL;
  110. }
  111. STDMETHODIMP CDBBEventManager::GetTypeInfoCount(unsigned int FAR * pctinfo)
  112. {
  113. return E_NOTIMPL;
  114. }
  115. STDMETHODIMP CDBBEventManager::Invoke(DISPID dispId, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR *pDispParams, VARIANT FAR *pvarResult, EXCEPINFO FAR * pexecinfo, unsigned int FAR *puArgErr)
  116. {
  117. switch(dispId)
  118. {
  119. case EVENT_COMMAND_COMPLETED:
  120. if (3 != pDispParams->cArgs) return DISP_E_BADPARAMCOUNT;
  121. OnCommandCompleted(pDispParams->rgvarg[2].lVal, pDispParams->rgvarg[1].lVal, pDispParams->rgvarg[0].pvarVal);
  122. return S_OK;
  123. case EVENT_LOG_MESSAGE:
  124. if (1 != pDispParams->cArgs) return DISP_E_BADPARAMCOUNT;
  125. OnLogMessage(pDispParams->rgvarg[0].bstrVal);
  126. return S_OK;
  127. case EVENT_SERVER_MESSAGE:
  128. if (3 != pDispParams->cArgs) return DISP_E_BADPARAMCOUNT;
  129. OnServerMessage(pDispParams->rgvarg[2].lVal, pDispParams->rgvarg[1].lVal, pDispParams->rgvarg[0].bstrVal);
  130. return S_OK;
  131. case EVENT_COMMAND_PROGRESS:
  132. if (4 != pDispParams->cArgs) return DISP_E_BADPARAMCOUNT;
  133. OnCommandProgress(pDispParams->rgvarg[3].lVal, pDispParams->rgvarg[2].lVal, pDispParams->rgvarg[1].lVal, pDispParams->rgvarg[0].lVal);
  134. return S_OK;
  135. }
  136. return DISP_E_MEMBERNOTFOUND;
  137. }
  138. void CDBBEventManager::OnCommandCompleted(LONG lCommandCode, HRESULT hCommandResult, VARIANT *pCommandData)
  139. {
  140. if(fnCmdCompleted) fnCmdCompleted(lCommandCode, hCommandResult, pCommandData, user);
  141. }
  142. void CDBBEventManager::OnLogMessage(BSTR bstrMessage)
  143. {
  144. if(fnLogMessage) fnLogMessage(bstrMessage, user);
  145. }
  146. void CDBBEventManager::OnServerMessage(LONG lMessageCode, LONG lMessageAction, BSTR bstrMessageData)
  147. {
  148. if(fnServerMessage) fnServerMessage(lMessageCode, lMessageAction,bstrMessageData, user);
  149. }
  150. void CDBBEventManager::OnCommandProgress(LONG lCommandCode, LONG lProgressCode, LONG lBytesDone, LONG lBytesTotal)
  151. {
  152. if(fnCmdProgress) fnCmdProgress(lCommandCode, lProgressCode, lBytesDone, lBytesTotal, user);
  153. }