PlaylistsCOM.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "main.h"
  2. #include "PlaylistsCOM.h"
  3. using namespace Nullsoft::Utility;
  4. enum
  5. {
  6. DISP_PLALISTS_GETXML = 777,
  7. };
  8. #define CHECK_ID(str, id) if (wcscmp(rgszNames[i], L##str) == 0) { rgdispid[i] = id; continue; }
  9. HRESULT PlaylistsCOM::GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgdispid)
  10. {
  11. bool unknowns = false;
  12. for (unsigned int i = 0;i != cNames;i++)
  13. {
  14. CHECK_ID("GetXML", DISP_PLALISTS_GETXML)
  15. rgdispid[i] = DISPID_UNKNOWN;
  16. unknowns = true;
  17. }
  18. if (unknowns)
  19. return DISP_E_UNKNOWNNAME;
  20. else
  21. return S_OK;
  22. }
  23. HRESULT PlaylistsCOM::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo)
  24. {
  25. return E_NOTIMPL;
  26. }
  27. HRESULT PlaylistsCOM::GetTypeInfoCount(unsigned int FAR * pctinfo)
  28. {
  29. return E_NOTIMPL;
  30. }
  31. static void WriteEscaped(FILE *fp, const wchar_t *str)
  32. {
  33. // TODO: for speed optimization,
  34. // we should wait until we hit a special character
  35. // and write out everything else so before it,
  36. // like how ASX loader does it
  37. while (str && *str)
  38. {
  39. switch (*str)
  40. {
  41. case L'&':
  42. fputws(L"&", fp);
  43. break;
  44. case L'>':
  45. fputws(L">", fp);
  46. break;
  47. case L'<':
  48. fputws(L"&lt;", fp);
  49. break;
  50. case L'\'':
  51. fputws(L"&apos;", fp);
  52. break;
  53. case L'\"':
  54. fputws(L"&quot;", fp);
  55. break;
  56. default:
  57. fputwc(*str, fp);
  58. break;
  59. }
  60. // write out the whole UTF-16 character
  61. wchar_t *next = CharNextW(str);
  62. while (++str != next)
  63. fputwc(*str, fp);
  64. }
  65. }
  66. static void SavePlaylistsXML(const wchar_t *destination, size_t limit)
  67. {
  68. AutoLockT<api_playlists> lock (AGAVE_API_PLAYLISTS);
  69. FILE *fp = _wfopen(destination, L"wb");
  70. fputws(L"\xFEFF", fp);
  71. fwprintf(fp, L"<?xml version=\"1.0\" encoding=\"UTF-16\"?>");
  72. size_t numPlaylists = limit ? min(limit, AGAVE_API_PLAYLISTS->GetCount()) : AGAVE_API_PLAYLISTS->GetCount();
  73. fwprintf(fp, L"<playlists playlists=\"%lu\">", numPlaylists);
  74. for (size_t i = 0; i != numPlaylists; i++)
  75. {
  76. fputws(L"<playlist filename=\"", fp);
  77. WriteEscaped(fp, AGAVE_API_PLAYLISTS->GetFilename(i));
  78. fputws(L"\" title=\"", fp);
  79. WriteEscaped(fp, AGAVE_API_PLAYLISTS->GetName(i));
  80. unsigned int numItems = 0, length = 0;
  81. AGAVE_API_PLAYLISTS->GetInfo(i, api_playlists_itemCount, &numItems, sizeof(numItems));
  82. AGAVE_API_PLAYLISTS->GetInfo(i, api_playlists_totalTime, &length, sizeof(length));
  83. fwprintf(fp, L"\" songs=\"%u\" seconds=\"%u\"/>",
  84. numItems,
  85. length);
  86. }
  87. fwprintf(fp, L"</playlists>");
  88. fclose(fp);
  89. }
  90. HRESULT PlaylistsCOM::Invoke(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, EXCEPINFO FAR * pexecinfo, unsigned int FAR *puArgErr)
  91. {
  92. switch (dispid)
  93. {
  94. case DISP_PLALISTS_GETXML:
  95. {
  96. int max = 0;
  97. if (pdispparams->cArgs == 1)
  98. {
  99. max = _wtoi(pdispparams->rgvarg[0].bstrVal);
  100. }
  101. wchar_t tempPath[MAX_PATH] = {0};
  102. GetTempPathW(MAX_PATH, tempPath);
  103. wchar_t tempFile[MAX_PATH] = {0};
  104. GetTempFileNameW(tempPath, L"mpx", 0, tempFile);
  105. SavePlaylistsXML(tempFile, max);
  106. HANDLE plFile = CreateFile(tempFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
  107. size_t flen = SetFilePointer(plFile, 0, NULL, FILE_END);
  108. SetFilePointer(plFile, 0, NULL, FILE_BEGIN);
  109. SAFEARRAY *bufferArray = SafeArrayCreateVector(VT_UI1, 0, flen);
  110. void *data;
  111. SafeArrayAccessData(bufferArray, &data);
  112. DWORD bytesRead = 0;
  113. ReadFile(plFile, data, flen, &bytesRead, 0);
  114. SafeArrayUnaccessData(bufferArray);
  115. CloseHandle(plFile);
  116. DeleteFile(tempFile);
  117. VariantInit(pvarResult);
  118. V_VT(pvarResult) = VT_ARRAY | VT_UI1;
  119. V_ARRAY(pvarResult) = bufferArray;
  120. }
  121. return S_OK;
  122. }
  123. return DISP_E_MEMBERNOTFOUND;
  124. }
  125. STDMETHODIMP PlaylistsCOM::QueryInterface(REFIID riid, PVOID *ppvObject)
  126. {
  127. if (!ppvObject)
  128. return E_POINTER;
  129. else if (IsEqualIID(riid, IID_IDispatch))
  130. *ppvObject = (IDispatch *)this;
  131. else if (IsEqualIID(riid, IID_IUnknown))
  132. *ppvObject = this;
  133. else
  134. {
  135. *ppvObject = NULL;
  136. return E_NOINTERFACE;
  137. }
  138. AddRef();
  139. return S_OK;
  140. }
  141. ULONG PlaylistsCOM::AddRef(void)
  142. {
  143. return 0;
  144. }
  145. ULONG PlaylistsCOM::Release(void)
  146. {
  147. return 0;
  148. }