1
0

PlaylistsXML.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "PlaylistsXML.h"
  2. #include "api__playlist.h"
  3. #include "../nu/AutoLock.h"
  4. #include "Playlists.h"
  5. #include <shlwapi.h>
  6. #include <stdint.h>
  7. using namespace Nullsoft::Utility;
  8. void PlaylistsXML::StartTag( const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params )
  9. {
  10. const wchar_t *filename = params->getItemValue( L"filename" );
  11. const wchar_t *title = params->getItemValue( L"title" );
  12. const wchar_t *countString = params->getItemValue( L"songs" );
  13. size_t numItems = 0;
  14. if ( countString && *countString )
  15. numItems = _wtoi( countString );
  16. const wchar_t *lengthString = params->getItemValue( L"seconds" );
  17. size_t length = 0;
  18. if ( lengthString && *lengthString )
  19. length = _wtoi( lengthString );
  20. const wchar_t *iTunesIDString = params->getItemValue( L"iTunesID" );
  21. uint64_t iTunesID = 0;
  22. if ( iTunesIDString && *iTunesIDString )
  23. iTunesID = _wtoi64( iTunesIDString );
  24. const wchar_t *cloudString = params->getItemValue( L"cloud" );
  25. size_t cloud = 0;
  26. if ( cloudString && *cloudString )
  27. cloud = _wtoi( cloudString );
  28. // parse GUID
  29. GUID guid = INVALID_GUID;
  30. const wchar_t *guidString = params->getItemValue( L"id" );
  31. if ( guidString && *guidString )
  32. {
  33. int Data1, Data2, Data3;
  34. int Data4[ 8 ];
  35. int n = swscanf( guidString, L" { %08x - %04x - %04x - %02x%02x - %02x%02x%02x%02x%02x%02x } ",
  36. &Data1, &Data2, &Data3, Data4 + 0, Data4 + 1,
  37. Data4 + 2, Data4 + 3, Data4 + 4, Data4 + 5, Data4 + 6, Data4 + 7 );
  38. if ( n == 11 ) // GUID was
  39. {
  40. // Cross assign all the values
  41. guid.Data1 = Data1;
  42. guid.Data2 = Data2;
  43. guid.Data3 = Data3;
  44. guid.Data4[ 0 ] = Data4[ 0 ];
  45. guid.Data4[ 1 ] = Data4[ 1 ];
  46. guid.Data4[ 2 ] = Data4[ 2 ];
  47. guid.Data4[ 3 ] = Data4[ 3 ];
  48. guid.Data4[ 4 ] = Data4[ 4 ];
  49. guid.Data4[ 5 ] = Data4[ 5 ];
  50. guid.Data4[ 6 ] = Data4[ 6 ];
  51. guid.Data4[ 7 ] = Data4[ 7 ];
  52. }
  53. }
  54. if ( PathIsFileSpecW( filename ) )
  55. {
  56. wchar_t playlistFilename[ MAX_PATH ] = { 0 };
  57. PathCombineW( playlistFilename, rootPath, filename );
  58. playlists->AddPlaylist_internal( playlistFilename, title, guid, numItems, length, iTunesID, cloud );
  59. }
  60. else
  61. {
  62. playlists->AddPlaylist_internal( filename, title, guid, numItems, length, iTunesID, cloud );
  63. }
  64. }
  65. int PlaylistsXML::LoadFile( const wchar_t *filename )
  66. {
  67. if ( !parser )
  68. return PLAYLISTSXML_NO_PARSER; // no sense in continuing if there's no parser available
  69. HANDLE file = CreateFileW( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL );
  70. if ( file == INVALID_HANDLE_VALUE )
  71. return PLAYLISTSXML_NO_FILE;
  72. while ( true )
  73. {
  74. int8_t data[ 1024 ] = { 0 };
  75. DWORD bytesRead = 0;
  76. if ( ReadFile( file, data, 1024, &bytesRead, NULL ) && bytesRead )
  77. {
  78. if ( parser->xmlreader_feed( data, bytesRead ) != API_XML_SUCCESS )
  79. {
  80. CloseHandle( file );
  81. return PLAYLISTSXML_XML_PARSE_ERROR;
  82. }
  83. }
  84. else
  85. break;
  86. }
  87. CloseHandle( file );
  88. parser->xmlreader_feed( 0, 0 );
  89. return PLAYLISTSXML_SUCCESS;
  90. }
  91. PlaylistsXML::PlaylistsXML( Playlists *_playlists ) : playlists( _playlists )
  92. {
  93. const wchar_t *g_path = WASABI_API_APP->path_getUserSettingsPath();
  94. PathCombineW( rootPath, g_path, L"plugins\\ml\\playlists" );
  95. playlists->AddRef();
  96. parserFactory = WASABI_API_SVC->service_getServiceByGuid( obj_xmlGUID );
  97. if ( parserFactory )
  98. parser = (obj_xml *)parserFactory->getInterface();
  99. if ( parser )
  100. {
  101. parser->xmlreader_setCaseSensitive();
  102. parser->xmlreader_registerCallback( L"Winamp:Playlists\fplaylist", this );
  103. parser->xmlreader_registerCallback( L"playlists\fplaylist", this );
  104. parser->xmlreader_open();
  105. }
  106. }
  107. PlaylistsXML::~PlaylistsXML()
  108. {
  109. playlists->Release();
  110. if ( parser )
  111. {
  112. parser->xmlreader_unregisterCallback( this );
  113. parser->xmlreader_close();
  114. }
  115. if ( parserFactory && parser )
  116. parserFactory->releaseInterface( parser );
  117. parserFactory = 0;
  118. parser = 0;
  119. }
  120. #define CBCLASS PlaylistsXML
  121. START_DISPATCH;
  122. VCB( ONSTARTELEMENT, StartTag )
  123. END_DISPATCH;
  124. #undef CBCLASS