WPLLoader.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "main.h"
  2. #include "WPLLoader.h"
  3. #include <stdio.h>
  4. #include "../nu/AutoWide.h"
  5. #include "../xml/ifc_xmlreadercallback.h"
  6. #include "../xml/obj_xml.h"
  7. #include "api.h"
  8. #include <api/service/waservicefactory.h>
  9. #include <shlwapi.h>
  10. #include <strsafe.h>
  11. class WPLXML : public ifc_xmlreadercallback
  12. {
  13. public:
  14. WPLXML(ifc_playlistloadercallback *_playlist, const wchar_t *wplFilename) : playlist(_playlist)
  15. {
  16. lstrcpynW(rootPath, wplFilename, MAX_PATH);
  17. PathRemoveFileSpecW(rootPath);
  18. }
  19. void StartTag(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params)
  20. {
  21. //not necessary YET, it will be if we register for more things: if (!_wcsicmp(xmlpath, L"smil\fbody\fseq\fmedia"))
  22. {
  23. const wchar_t *track = params->getItemValue(L"src");
  24. if (track)
  25. {
  26. if (PathIsRootW(track) || PathIsURLW(track))
  27. {
  28. playlist->OnFile(track, 0, -1, 0); // TODO: more info!!!
  29. }
  30. else
  31. {
  32. wchar_t fullPath[MAX_PATH] = {0}, canonicalizedPath[MAX_PATH] = {0};
  33. PathCombineW(fullPath, rootPath, track);
  34. PathCanonicalizeW(canonicalizedPath, fullPath);
  35. playlist->OnFile(canonicalizedPath, 0, -1, 0); // TODO: more info!!!
  36. }
  37. }
  38. }
  39. }
  40. ifc_playlistloadercallback *playlist;
  41. wchar_t rootPath[MAX_PATH];
  42. protected:
  43. RECVS_DISPATCH;
  44. };
  45. #ifdef CBCLASS
  46. #undef CBCLASS
  47. #endif
  48. #define CBCLASS WPLXML
  49. START_DISPATCH;
  50. VCB(ONSTARTELEMENT, StartTag)
  51. END_DISPATCH;
  52. WPLLoader::WPLLoader()
  53. {
  54. }
  55. WPLLoader::~WPLLoader(void)
  56. {
  57. //Close();
  58. }
  59. int WPLLoader::Load(const wchar_t *filename, ifc_playlistloadercallback *playlist)
  60. {
  61. HANDLE file = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
  62. if (file == INVALID_HANDLE_VALUE)
  63. return IFC_PLAYLISTLOADER_FAILED;
  64. obj_xml *parser=0;
  65. waServiceFactory *parserFactory=0;
  66. parserFactory = plugin.service->service_getServiceByGuid(obj_xmlGUID);
  67. if (parserFactory)
  68. parser = (obj_xml *)parserFactory->getInterface();
  69. if (parser)
  70. {
  71. WPLXML wplXml(playlist, filename);
  72. parser->xmlreader_registerCallback(L"smil\fbody\fseq\fmedia", &wplXml);
  73. parser->xmlreader_open();
  74. parser->xmlreader_setEncoding(L"UTF-8"); // WPL is always UTF-8, but doesn't explicitly have it
  75. while (true)
  76. {
  77. char data[1024] = {0};
  78. DWORD bytesRead = 0;
  79. if (ReadFile(file, data, 1024, &bytesRead, NULL) && bytesRead)
  80. {
  81. if (parser->xmlreader_feed(data, bytesRead) != API_XML_SUCCESS)
  82. {
  83. CloseHandle(file);
  84. parser->xmlreader_unregisterCallback(&wplXml);
  85. parser->xmlreader_close();
  86. parserFactory->releaseInterface(parser);
  87. return IFC_PLAYLISTLOADER_FAILED;
  88. }
  89. }
  90. else
  91. break;
  92. }
  93. CloseHandle(file);
  94. parser->xmlreader_feed(0, 0);
  95. parser->xmlreader_unregisterCallback(&wplXml);
  96. parser->xmlreader_close();
  97. parserFactory->releaseInterface(parser);
  98. return IFC_PLAYLISTLOADER_SUCCESS;
  99. }
  100. CloseHandle(file);
  101. return IFC_PLAYLISTLOADER_FAILED;
  102. }
  103. #ifdef CBCLASS
  104. #undef CBCLASS
  105. #endif
  106. #define CBCLASS WPLLoader
  107. START_DISPATCH;
  108. CB(IFC_PLAYLISTLOADER_LOAD, Load)
  109. END_DISPATCH;