external.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "main.h"
  2. #include "./resource.h"
  3. #include "./external.h"
  4. #include "./navigation.h"
  5. #include "./commands.h"
  6. #include "../winamp/jsapi.h"
  7. #include "../winamp/jsapi_CallbackParameters.h"
  8. #include "./serviceHelper.h"
  9. #include <browserView.h>
  10. #include <ifc_omservice.h>
  11. #include <ifc_omserviceeditor.h>
  12. #define DISPTABLE_CLASS ExternalDispatch
  13. DISPTABLE_BEGIN()
  14. DISPENTRY_ADD(DISPATCH_SERVICE_OPEN, L"serviceOpen", OnServiceOpen)
  15. DISPENTRY_ADD(DISPATCH_SERVICE_CREATE, L"serviceCreate", OnServiceCreate)
  16. DISPENTRY_ADD(DISPATCH_SERVICE_GETINFO, L"serviceGetInfo", OnServiceGetInfo)
  17. DISPENTRY_ADD(DISPATCH_SERVICE_SETINFO, L"serviceSetInfo", OnServiceSetInfo)
  18. DISPTABLE_END
  19. #undef DISPTABLE_CLASS
  20. static BOOL DispParam_GetStringOpt(LPCWSTR *str, DISPPARAMS *paramInfo, UINT paramNumber, UINT *argErr)
  21. {
  22. if (paramInfo->cArgs < paramNumber ||
  23. VT_NULL == paramInfo->rgvarg[paramInfo->cArgs - paramNumber].vt)
  24. {
  25. *str = NULL;
  26. return FALSE;
  27. }
  28. JSAPI_GETSTRING((*str), paramInfo, paramNumber, argErr);
  29. return TRUE;
  30. }
  31. ExternalDispatch::ExternalDispatch()
  32. : ref(1)
  33. {
  34. }
  35. ExternalDispatch::~ExternalDispatch()
  36. {
  37. }
  38. HRESULT ExternalDispatch::CreateInstance(ExternalDispatch **instance)
  39. {
  40. if (NULL == instance) return E_POINTER;
  41. *instance = new ExternalDispatch();
  42. if (NULL == *instance) return E_OUTOFMEMORY;
  43. return S_OK;
  44. }
  45. LPCWSTR ExternalDispatch::GetName()
  46. {
  47. return L"WebDev";
  48. }
  49. ULONG ExternalDispatch::AddRef(void)
  50. {
  51. return InterlockedIncrement((LONG*)&ref);
  52. }
  53. ULONG ExternalDispatch::Release(void)
  54. {
  55. if (0 == ref)
  56. return ref;
  57. LONG r = InterlockedDecrement((LONG*)&ref);
  58. if (0 == r)
  59. delete(this);
  60. return r;
  61. }
  62. STDMETHODIMP ExternalDispatch::QueryInterface(REFIID riid, void **ppvObject)
  63. {
  64. if (NULL == ppvObject) return E_POINTER;
  65. if (IsEqualIID(riid, IID_IDispatch))
  66. *ppvObject = static_cast<IDispatch*>(this);
  67. else if (IsEqualIID(riid, IID_IUnknown))
  68. *ppvObject = static_cast<IUnknown*>(this);
  69. else
  70. {
  71. *ppvObject = NULL;
  72. return E_NOINTERFACE;
  73. }
  74. AddRef();
  75. return S_OK;
  76. }
  77. HRESULT ExternalDispatch::OnServiceOpen(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  78. {
  79. JSAPI_VERIFY_METHOD(wFlags);
  80. JSAPI_VERIFY_PARAMCOUNT_OPTIONAL(pdispparams, 1, 2);
  81. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  82. UINT serviceId;
  83. JSAPI_GETUNSIGNED_AS_NUMBER(serviceId, pdispparams, 1, puArgErr);
  84. LPCWSTR forceUrl = NULL;
  85. if (pdispparams->cArgs > 1)
  86. {
  87. switch(pdispparams->rgvarg[0].vt)
  88. {
  89. case VT_BSTR: forceUrl = pdispparams->rgvarg[0].bstrVal; break;
  90. case VT_I4: forceUrl = MAKEINTRESOURCE(pdispparams->rgvarg[0].lVal); break;
  91. }
  92. }
  93. if (FALSE == DispParam_GetStringOpt(&forceUrl, pdispparams,2, puArgErr))
  94. forceUrl = NULL;
  95. HRESULT hr;
  96. Navigation *navigation;
  97. hr = Plugin_GetNavigation(&navigation);
  98. if (SUCCEEDED(hr))
  99. {
  100. ifc_omservice *service;
  101. if (NULL != navigation->FindService(serviceId, &service))
  102. {
  103. hr = Command_NavigateService(service, forceUrl, FALSE);
  104. service->Release();
  105. }
  106. else
  107. {
  108. hr = E_FAIL;
  109. }
  110. navigation->Release();
  111. }
  112. JSAPI_SET_RESULT(pvarResult, boolVal, (SUCCEEDED(hr) ? VARIANT_TRUE : VARIANT_FALSE));
  113. return S_OK;
  114. }
  115. HRESULT ExternalDispatch::OnServiceCreate(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  116. {
  117. JSAPI_VERIFY_METHOD(wFlags);
  118. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 0);
  119. HRESULT hr = Command_CreateService();
  120. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  121. JSAPI_SET_RESULT(pvarResult, boolVal, (SUCCEEDED(hr) ? VARIANT_TRUE : VARIANT_FALSE));
  122. return S_OK;
  123. }
  124. HRESULT ExternalDispatch::OnServiceGetInfo(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  125. {
  126. JSAPI_VERIFY_METHOD(wFlags);
  127. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 1);
  128. JSAPI_INIT_RESULT(pvarResult, VT_DISPATCH);
  129. UINT serviceId;
  130. JSAPI_GETUNSIGNED_AS_NUMBER(serviceId, pdispparams, 1, puArgErr);
  131. Navigation *navigation;
  132. HRESULT hr = Plugin_GetNavigation(&navigation);
  133. if (SUCCEEDED(hr))
  134. {
  135. ifc_omservice *service;
  136. if (NULL != navigation->FindService(serviceId, &service))
  137. {
  138. WCHAR szBuffer[2048];
  139. JSAPI::CallbackParameters *params = new JSAPI::CallbackParameters;
  140. params->AddLong(L"id", service->GetId());
  141. if (FAILED(service->GetName(szBuffer, ARRAYSIZE(szBuffer)))) szBuffer[0] = L'\0';
  142. params->AddString(L"name", szBuffer);
  143. if (FAILED(service->GetUrl(szBuffer, ARRAYSIZE(szBuffer)))) szBuffer[0] = L'\0';
  144. params->AddString(L"url", szBuffer);
  145. if (FAILED(service->GetIcon(szBuffer, ARRAYSIZE(szBuffer)))) szBuffer[0] = L'\0';
  146. params->AddString(L"icon", szBuffer);
  147. params->AddBoolean(L"preauthorized", (S_OK == ServiceHelper_IsPreAuthorized(service)));
  148. service->Release();
  149. V_DISPATCH(pvarResult) = params;
  150. }
  151. else
  152. {
  153. hr = E_FAIL;
  154. }
  155. navigation->Release();
  156. }
  157. if (FAILED(hr))
  158. {
  159. V_DISPATCH(pvarResult) = 0;
  160. }
  161. return S_OK;
  162. }
  163. HRESULT ExternalDispatch::OnServiceSetInfo(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  164. {
  165. JSAPI_VERIFY_METHOD(wFlags);
  166. JSAPI_VERIFY_PARAMCOUNT_OPTIONAL(pdispparams, 2, 5);
  167. UINT serviceId;
  168. JSAPI_GETUNSIGNED_AS_NUMBER(serviceId, pdispparams, 1, puArgErr);
  169. Navigation *navigation;
  170. HRESULT hr = Plugin_GetNavigation(&navigation);
  171. if (SUCCEEDED(hr))
  172. {
  173. ifc_omservice *service;
  174. HNAVITEM hItem = navigation->FindService(serviceId, &service);
  175. if (NULL != hItem)
  176. {
  177. ifc_omserviceeditor *editor;
  178. hr = service->QueryInterface(IFC_OmServiceEditor, (void**)&editor);
  179. if (SUCCEEDED(hr))
  180. {
  181. LPCWSTR value = NULL;
  182. editor->BeginUpdate();
  183. if (FALSE != DispParam_GetStringOpt(&value, pdispparams, 2, puArgErr) && FAILED(editor->SetName(value, FALSE)))
  184. hr = E_FAIL;
  185. if (FALSE != DispParam_GetStringOpt(&value, pdispparams, 3, puArgErr) && FAILED(ServiceHelper_UpdateIcon(editor, value)))
  186. hr = E_FAIL;
  187. if (FALSE != DispParam_GetStringOpt(&value, pdispparams, 4, puArgErr) && FAILED(editor->SetUrl(value, FALSE)))
  188. hr = E_FAIL;
  189. VARIANT_BOOL authorized = JSAPI_PARAM_OPTIONAL(pdispparams, 5, boolVal, VARIANT_FALSE);
  190. if (S_OK == editor->SetFlags((VARIANT_TRUE == authorized) ? WDSVCF_PREAUTHORIZED : 0, WDSVCF_PREAUTHORIZED))
  191. {
  192. if (VARIANT_TRUE == authorized)
  193. Command_ResetPermissions(service);
  194. }
  195. editor->EndUpdate();
  196. editor->Release();
  197. }
  198. if(SUCCEEDED(hr))
  199. hr = ServiceHelper_Save(service);
  200. service->Release();
  201. }
  202. else
  203. {
  204. hr = E_FAIL;
  205. }
  206. navigation->Release();
  207. }
  208. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  209. JSAPI_SET_RESULT(pvarResult, boolVal, (SUCCEEDED(hr) ? VARIANT_TRUE : VARIANT_FALSE));
  210. return S_OK;
  211. }