JSAPI2_Playlist.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "JSAPI2_Playlist.h"
  2. #include "../Winamp/JSAPI.h"
  3. #include "api__playlist.h"
  4. #include "PlaylistManager.h"
  5. JSAPI2::PlaylistObject::PlaylistObject(const wchar_t *_key)
  6. {
  7. key = _key;
  8. }
  9. #define DISP_TABLE \
  10. CHECK_ID(Clear)\
  11. CHECK_ID(AppendURL)\
  12. CHECK_ID(GetItemFilename)\
  13. CHECK_ID(GetItemTitle)\
  14. CHECK_ID(GetItemLength)\
  15. CHECK_ID(GetItemExtendedInfo)\
  16. CHECK_ID(Reverse)\
  17. CHECK_ID(SwapItems)\
  18. CHECK_ID(Randomize)\
  19. CHECK_ID(RemoveItem)\
  20. CHECK_ID(SortByTitle)\
  21. CHECK_ID(SortByFilename)\
  22. CHECK_ID(SetItemFilename)\
  23. CHECK_ID(SetItemTitle)\
  24. CHECK_ID(SetItemLength)\
  25. CHECK_ID(InsertURL)\
  26. CHECK_ID(numitems)\
  27. #define CHECK_ID(str) JSAPI_DISP_ENUMIFY(str),
  28. enum {
  29. DISP_TABLE
  30. };
  31. #undef CHECK_ID
  32. #define CHECK_ID(str) if (wcscmp(rgszNames[i], L## #str) == 0) { rgdispid[i] = JSAPI_DISP_ENUMIFY(str); continue; }
  33. HRESULT JSAPI2::PlaylistObject::GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgdispid)
  34. {
  35. bool unknowns = false;
  36. for (unsigned int i = 0;i != cNames;i++)
  37. {
  38. DISP_TABLE
  39. rgdispid[i] = DISPID_UNKNOWN;
  40. unknowns = true;
  41. }
  42. if (unknowns)
  43. return DISP_E_UNKNOWNNAME;
  44. else
  45. return S_OK;
  46. }
  47. HRESULT JSAPI2::PlaylistObject::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo)
  48. {
  49. return E_NOTIMPL;
  50. }
  51. HRESULT JSAPI2::PlaylistObject::GetTypeInfoCount(unsigned int FAR * pctinfo)
  52. {
  53. return E_NOTIMPL;
  54. }
  55. HRESULT JSAPI2::PlaylistObject::Clear(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  56. {
  57. JSAPI_VERIFY_METHOD(wFlags);
  58. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 0);
  59. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  60. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  61. playlist.Clear();
  62. return S_OK;
  63. }
  64. HRESULT JSAPI2::PlaylistObject::AppendURL(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  65. {
  66. JSAPI_VERIFY_METHOD(wFlags);
  67. JSAPI_VERIFY_PARAMCOUNT_OPTIONAL(pdispparams, 1, 3);
  68. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_BSTR, puArgErr);
  69. JSAPI_VERIFY_PARAMTYPE_OPTIONAL(pdispparams, 2, VT_BSTR, puArgErr);
  70. JSAPI_VERIFY_PARAMTYPE_OPTIONAL(pdispparams, 3, VT_I4, puArgErr);
  71. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  72. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  73. const wchar_t *filename = JSAPI_PARAM(pdispparams, 1).bstrVal;
  74. const wchar_t *title = JSAPI_PARAM_OPTIONAL(pdispparams, 2, bstrVal, 0);
  75. int length = JSAPI_PARAM_OPTIONAL(pdispparams, 3, lVal, -1);
  76. playlist.AppendWithInfo(filename, title, length);
  77. return S_OK;
  78. }
  79. HRESULT JSAPI2::PlaylistObject::InsertURL(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  80. {
  81. JSAPI_VERIFY_METHOD(wFlags);
  82. JSAPI_VERIFY_PARAMCOUNT_OPTIONAL(pdispparams, 2, 4);
  83. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_I4, puArgErr);
  84. JSAPI_VERIFY_PARAMTYPE(pdispparams, 2, VT_BSTR, puArgErr);
  85. JSAPI_VERIFY_PARAMTYPE_OPTIONAL(pdispparams, 3, VT_BSTR, puArgErr);
  86. JSAPI_VERIFY_PARAMTYPE_OPTIONAL(pdispparams, 4, VT_I4, puArgErr);
  87. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  88. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  89. const wchar_t *filename = JSAPI_PARAM(pdispparams, 2).bstrVal;
  90. const wchar_t *title = JSAPI_PARAM_OPTIONAL(pdispparams, 3, bstrVal, 0);
  91. int length = JSAPI_PARAM_OPTIONAL(pdispparams, 4, lVal, -1);
  92. playlist.Insert(JSAPI_PARAM(pdispparams, 1).lVal, filename, title, length);
  93. return S_OK;
  94. }
  95. HRESULT JSAPI2::PlaylistObject::GetItemFilename(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  96. {
  97. JSAPI_VERIFY_METHOD(wFlags);
  98. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 1);
  99. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_I4, puArgErr);
  100. JSAPI_INIT_RESULT(pvarResult, VT_BSTR);
  101. JSAPI_SET_RESULT(pvarResult, bstrVal, SysAllocString(playlist.ItemName(JSAPI_PARAM(pdispparams, 1).lVal)));
  102. return S_OK;
  103. }
  104. HRESULT JSAPI2::PlaylistObject::GetItemTitle(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  105. {
  106. JSAPI_VERIFY_METHOD(wFlags);
  107. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 1);
  108. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_I4, puArgErr);
  109. JSAPI_INIT_RESULT(pvarResult, VT_BSTR);
  110. JSAPI_SET_RESULT(pvarResult, bstrVal, SysAllocString(playlist.ItemTitle(JSAPI_PARAM(pdispparams, 1).lVal)));
  111. return S_OK;
  112. }
  113. HRESULT JSAPI2::PlaylistObject::GetItemLength(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  114. {
  115. JSAPI_VERIFY_METHOD(wFlags);
  116. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 1);
  117. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_I4, puArgErr);
  118. JSAPI_INIT_RESULT(pvarResult, VT_I4);
  119. JSAPI_SET_RESULT(pvarResult, lVal, playlist.GetItemLengthMilliseconds(JSAPI_PARAM(pdispparams, 1).lVal));
  120. return S_OK;
  121. }
  122. HRESULT JSAPI2::PlaylistObject::GetItemExtendedInfo(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  123. {
  124. JSAPI_VERIFY_METHOD(wFlags);
  125. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 2);
  126. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_I4, puArgErr);
  127. JSAPI_VERIFY_PARAMTYPE(pdispparams, 2, VT_BSTR, puArgErr);
  128. JSAPI_INIT_RESULT(pvarResult, VT_BSTR);
  129. wchar_t metadata[1024]=L"";
  130. playlist.GetItemExtendedInfo(JSAPI_PARAM(pdispparams, 1).lVal, JSAPI_PARAM(pdispparams, 2).bstrVal, metadata, sizeof(metadata)/sizeof(*metadata));
  131. JSAPI_SET_RESULT(pvarResult, bstrVal, SysAllocString(metadata));
  132. return S_OK;
  133. }
  134. HRESULT JSAPI2::PlaylistObject::Reverse(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  135. {
  136. JSAPI_VERIFY_METHOD(wFlags);
  137. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 0);
  138. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  139. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  140. playlist.Reverse();
  141. return S_OK;
  142. }
  143. HRESULT JSAPI2::PlaylistObject::SwapItems(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  144. {
  145. JSAPI_VERIFY_METHOD(wFlags);
  146. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 2);
  147. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_I4, puArgErr);
  148. JSAPI_VERIFY_PARAMTYPE(pdispparams, 2, VT_I4, puArgErr);
  149. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  150. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  151. playlist.Swap(JSAPI_PARAM(pdispparams, 1).lVal, JSAPI_PARAM(pdispparams, 2).lVal);
  152. return S_OK;
  153. }
  154. HRESULT JSAPI2::PlaylistObject::Randomize(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  155. {
  156. JSAPI_VERIFY_METHOD(wFlags);
  157. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 0);
  158. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  159. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  160. playlistManager.Randomize(&playlist);
  161. return S_OK;
  162. }
  163. HRESULT JSAPI2::PlaylistObject::RemoveItem(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  164. {
  165. JSAPI_VERIFY_METHOD(wFlags);
  166. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 1);
  167. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_I4, puArgErr);
  168. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  169. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  170. playlist.Remove(JSAPI_PARAM(pdispparams, 1).lVal);
  171. return S_OK;
  172. }
  173. HRESULT JSAPI2::PlaylistObject::SortByTitle(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  174. {
  175. JSAPI_VERIFY_METHOD(wFlags);
  176. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 0);
  177. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  178. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  179. playlist.SortByTitle();
  180. return S_OK;
  181. }
  182. HRESULT JSAPI2::PlaylistObject::SortByFilename(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  183. {
  184. JSAPI_VERIFY_METHOD(wFlags);
  185. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 0);
  186. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  187. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  188. playlist.SortByFilename();
  189. return S_OK;
  190. }
  191. HRESULT JSAPI2::PlaylistObject::SetItemFilename(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  192. {
  193. JSAPI_VERIFY_METHOD(wFlags);
  194. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 2);
  195. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_I4, puArgErr);
  196. JSAPI_VERIFY_PARAMTYPE(pdispparams, 2, VT_BSTR, puArgErr);
  197. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  198. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  199. playlist.SetItemFilename(JSAPI_PARAM(pdispparams, 1).lVal, JSAPI_PARAM(pdispparams, 2).bstrVal);
  200. return S_OK;
  201. }
  202. HRESULT JSAPI2::PlaylistObject::SetItemTitle(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  203. {
  204. JSAPI_VERIFY_METHOD(wFlags);
  205. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 2);
  206. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_I4, puArgErr);
  207. JSAPI_VERIFY_PARAMTYPE(pdispparams, 2, VT_BSTR, puArgErr);
  208. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  209. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  210. playlist.SetItemTitle(JSAPI_PARAM(pdispparams, 1).lVal, JSAPI_PARAM(pdispparams, 2).bstrVal);
  211. return S_OK;
  212. }
  213. HRESULT JSAPI2::PlaylistObject::SetItemLength(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  214. {
  215. JSAPI_VERIFY_METHOD(wFlags);
  216. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 2);
  217. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_I4, puArgErr);
  218. JSAPI_VERIFY_PARAMTYPE(pdispparams, 2, VT_I4, puArgErr);
  219. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  220. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  221. playlist.SetItemLengthMilliseconds(JSAPI_PARAM(pdispparams, 1).lVal, JSAPI_PARAM(pdispparams, 2).lVal);
  222. return S_OK;
  223. }
  224. HRESULT JSAPI2::PlaylistObject::numitems(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  225. {
  226. // JSAPI_VERIFY_METHOD(wFlags);
  227. // JSAPI_VERIFY_PARAMCOUNT(pdispparams, 0);
  228. if (wFlags & DISPATCH_PROPERTYGET)
  229. {
  230. JSAPI_INIT_RESULT(pvarResult, VT_I4);
  231. JSAPI_SET_RESULT(pvarResult, lVal, (LONG)playlist.GetNumItems());
  232. return S_OK;
  233. }
  234. else
  235. return DISP_E_MEMBERNOTFOUND;
  236. }
  237. #undef CHECK_ID
  238. #define CHECK_ID(str) case JSAPI_DISP_ENUMIFY(str): return str(wFlags, pdispparams, pvarResult, puArgErr);
  239. HRESULT JSAPI2::PlaylistObject::Invoke(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, EXCEPINFO FAR * pexecinfo, unsigned int FAR *puArgErr)
  240. {
  241. switch (dispid)
  242. {
  243. DISP_TABLE
  244. }
  245. return DISP_E_MEMBERNOTFOUND;
  246. }
  247. STDMETHODIMP JSAPI2::PlaylistObject::QueryInterface(REFIID riid, PVOID *ppvObject)
  248. {
  249. if (!ppvObject)
  250. return E_POINTER;
  251. else if (IsEqualIID(riid, IID_IDispatch))
  252. *ppvObject = (IDispatch *)this;
  253. else if (IsEqualIID(riid, IID_IUnknown))
  254. *ppvObject = this;
  255. else if (IsEqualIID(riid, IID_PlaylistObject))
  256. *ppvObject = (PlaylistObject *)this;
  257. else
  258. {
  259. *ppvObject = NULL;
  260. return E_NOINTERFACE;
  261. }
  262. AddRef();
  263. return S_OK;
  264. }
  265. ULONG JSAPI2::PlaylistObject::AddRef(void)
  266. {
  267. return this->_refCount.fetch_add( 1 );
  268. }
  269. ULONG JSAPI2::PlaylistObject::Release( void )
  270. {
  271. std::size_t l_Ref = this->_refCount.fetch_sub( 1 );
  272. if ( l_Ref == 0 )
  273. delete this;
  274. return l_Ref;
  275. }