1
0

JSAPI2_Playlists.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include "JSAPI2_Playlists.h"
  2. #include "../Winamp/JSAPI.h"
  3. #include "api__playlist.h"
  4. #include "../Winamp/JSAPI_ObjectArray.h"
  5. #include "Playlists.h"
  6. #include "JSAPI2_Playlist.h"
  7. #include "PlaylistManager.h"
  8. #include "../Winamp/JSAPI_CallbackParameters.h"
  9. extern Playlists playlists;
  10. JSAPI2::PlaylistsAPI::PlaylistsAPI(const wchar_t *_key, JSAPI::ifc_info *_info)
  11. {
  12. info = _info;
  13. key = _key;
  14. }
  15. #define DISP_TABLE \
  16. CHECK_ID(GetPlaylists)\
  17. CHECK_ID(OpenPlaylist)\
  18. CHECK_ID(SavePlaylist)\
  19. #define CHECK_ID(str) JSAPI_DISP_ENUMIFY(str),
  20. enum {
  21. DISP_TABLE
  22. };
  23. #undef CHECK_ID
  24. #define CHECK_ID(str) if (wcscmp(rgszNames[i], L## #str) == 0) { rgdispid[i] = JSAPI_DISP_ENUMIFY(str); continue; }
  25. HRESULT JSAPI2::PlaylistsAPI::GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgdispid)
  26. {
  27. bool unknowns = false;
  28. for (unsigned int i = 0;i != cNames;i++)
  29. {
  30. DISP_TABLE
  31. rgdispid[i] = DISPID_UNKNOWN;
  32. unknowns = true;
  33. }
  34. if (unknowns)
  35. return DISP_E_UNKNOWNNAME;
  36. else
  37. return S_OK;
  38. }
  39. HRESULT JSAPI2::PlaylistsAPI::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo)
  40. {
  41. return E_NOTIMPL;
  42. }
  43. HRESULT JSAPI2::PlaylistsAPI::GetTypeInfoCount(unsigned int FAR * pctinfo)
  44. {
  45. return E_NOTIMPL;
  46. }
  47. HRESULT JSAPI2::PlaylistsAPI::GetPlaylists(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  48. {
  49. JSAPI_VERIFY_METHOD(wFlags);
  50. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 0);
  51. VariantInit(pvarResult);
  52. V_VT(pvarResult) = VT_DISPATCH;
  53. if (AGAVE_API_JSAPI2_SECURITY->GetActionAuthorization(L"playlists", L"read", key, info, JSAPI2::api_security::ACTION_PROMPT) == JSAPI2::api_security::ACTION_ALLOWED)
  54. {
  55. JSAPI::ObjectArray *objectArray = new JSAPI::ObjectArray;
  56. playlists.Lock();
  57. size_t count = playlists.GetCount();
  58. for (size_t i=0;i!=count;i++)
  59. {
  60. const PlaylistInfo &info = playlists.GetPlaylistInfo(i);
  61. JSAPI::CallbackParameters *playlistParams = new JSAPI::CallbackParameters;
  62. playlistParams->AddString(L"filename", info.filename);
  63. playlistParams->AddString(L"title", info.title);
  64. wchar_t guid_str[40];
  65. nsGUID::toCharW(info.guid, guid_str);
  66. playlistParams->AddString(L"playlistId", guid_str);
  67. playlistParams->AddLong(L"length", info.length);
  68. playlistParams->AddLong(L"numitems", info.numItems);
  69. objectArray->AddObject(playlistParams);
  70. playlistParams->Release();
  71. }
  72. playlists.Unlock();
  73. V_DISPATCH(pvarResult) = objectArray;
  74. return S_OK;
  75. }
  76. else
  77. {
  78. V_DISPATCH(pvarResult) = 0;
  79. return S_OK;
  80. }
  81. return E_FAIL;
  82. }
  83. HRESULT JSAPI2::PlaylistsAPI::OpenPlaylist(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  84. {
  85. JSAPI_VERIFY_METHOD(wFlags);
  86. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 1);
  87. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_BSTR, puArgErr);
  88. VariantInit(pvarResult);
  89. V_VT(pvarResult) = VT_DISPATCH;
  90. V_DISPATCH(pvarResult) = 0;
  91. if (AGAVE_API_JSAPI2_SECURITY->GetActionAuthorization(L"playlists", L"read", key, info, JSAPI2::api_security::ACTION_PROMPT) == JSAPI2::api_security::ACTION_ALLOWED)
  92. {
  93. GUID playlist_guid = nsGUID::fromCharW(JSAPI_PARAM(pdispparams, 1).bstrVal);
  94. playlists.Lock();
  95. size_t index;
  96. if (playlists.GetPosition(playlist_guid, &index) == API_PLAYLISTS_SUCCESS)
  97. {
  98. const wchar_t *filename = playlists.GetFilename(index);
  99. if (filename)
  100. {
  101. PlaylistObject *playlist = new PlaylistObject(key);
  102. if (playlistManager.Load(filename, &playlist->playlist) == PLAYLISTMANAGER_SUCCESS)
  103. {
  104. V_DISPATCH(pvarResult) = playlist;
  105. }
  106. else
  107. {
  108. delete playlist;
  109. }
  110. }
  111. }
  112. playlists.Unlock();
  113. return S_OK;
  114. }
  115. else
  116. {
  117. return S_OK;
  118. }
  119. return E_FAIL;
  120. }
  121. HRESULT JSAPI2::PlaylistsAPI::SavePlaylist(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  122. {
  123. JSAPI_VERIFY_METHOD(wFlags);
  124. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 2);
  125. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_BSTR, puArgErr);
  126. JSAPI_VERIFY_PARAMTYPE(pdispparams, 2, VT_DISPATCH, puArgErr);
  127. VariantInit(pvarResult);
  128. V_VT(pvarResult) = VT_BOOL;
  129. V_BOOL(pvarResult) = FALSE;
  130. if (AGAVE_API_JSAPI2_SECURITY->GetActionAuthorization(L"playlists", L"write", key, info, JSAPI2::api_security::ACTION_PROMPT) == JSAPI2::api_security::ACTION_ALLOWED)
  131. {
  132. GUID playlist_guid = nsGUID::fromCharW(JSAPI_PARAM(pdispparams, 1).bstrVal);
  133. playlists.Lock();
  134. size_t index;
  135. if (playlists.GetPosition(playlist_guid, &index) == API_PLAYLISTS_SUCCESS)
  136. {
  137. const wchar_t *filename = playlists.GetFilename(index);
  138. if (filename)
  139. {
  140. IDispatch *dispPlaylist = JSAPI_PARAM(pdispparams, 2).pdispVal;
  141. PlaylistObject *playlist = 0;
  142. dispPlaylist->QueryInterface(JSAPI2::IID_PlaylistObject, (void **)&playlist);
  143. if (playlistManager.Save(filename, &(playlist->playlist)) == PLAYLISTMANAGER_SUCCESS)
  144. V_BOOL(pvarResult) = TRUE;
  145. }
  146. }
  147. playlists.Unlock();
  148. return S_OK;
  149. }
  150. else
  151. {
  152. return S_OK;
  153. }
  154. return E_FAIL;
  155. }
  156. #undef CHECK_ID
  157. #define CHECK_ID(str) case JSAPI_DISP_ENUMIFY(str): return str(wFlags, pdispparams, pvarResult, puArgErr);
  158. HRESULT JSAPI2::PlaylistsAPI::Invoke(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, EXCEPINFO FAR * pexecinfo, unsigned int FAR *puArgErr)
  159. {
  160. switch (dispid)
  161. {
  162. DISP_TABLE
  163. }
  164. return DISP_E_MEMBERNOTFOUND;
  165. }
  166. STDMETHODIMP JSAPI2::PlaylistsAPI::QueryInterface(REFIID riid, PVOID *ppvObject)
  167. {
  168. if (!ppvObject)
  169. return E_POINTER;
  170. else if (IsEqualIID(riid, IID_IDispatch))
  171. *ppvObject = (IDispatch *)this;
  172. else if (IsEqualIID(riid, IID_IUnknown))
  173. *ppvObject = this;
  174. else
  175. {
  176. *ppvObject = NULL;
  177. return E_NOINTERFACE;
  178. }
  179. AddRef();
  180. return S_OK;
  181. }
  182. ULONG JSAPI2::PlaylistsAPI::AddRef(void)
  183. {
  184. return this->_refCount.fetch_add( 1 );
  185. }
  186. ULONG JSAPI2::PlaylistsAPI::Release( void )
  187. {
  188. std::size_t l_Ref = this->_refCount.fetch_sub( 1 );
  189. if ( l_Ref == 0 )
  190. delete this;
  191. return l_Ref;
  192. }