impl_playlist.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "main.h"
  2. #include "impl_playlist.h"
  3. #include <algorithm>
  4. #include "../nu/MediaLibraryInterface.h"
  5. #include "../nu/AutoChar.h"
  6. #include "../Winamp/strutil.h"
  7. #include <shlwapi.h>
  8. void Playlist::Clear()
  9. {
  10. for ( pl_entry *entry : entries )
  11. delete entry;
  12. entries.clear();
  13. }
  14. void Playlist::OnFile( const wchar_t *filename, const wchar_t *title, int lengthInMS, int sizeInBytes, ifc_plentryinfo *info )
  15. {
  16. entries.push_back( new pl_entry( filename, title, lengthInMS, sizeInBytes, info ) );
  17. }
  18. void Playlist::AppendWithInfo( const wchar_t *filename, const wchar_t *title, int lengthInMS, int sizeInBytes )
  19. {
  20. entries.push_back( new pl_entry( filename, title, lengthInMS, sizeInBytes ) );
  21. }
  22. Playlist::~Playlist()
  23. {
  24. Clear();
  25. }
  26. size_t Playlist::GetNumItems()
  27. {
  28. return entries.size();
  29. }
  30. size_t Playlist::GetItem( size_t item, wchar_t *filename, size_t filenameCch )
  31. {
  32. if ( item >= entries.size() )
  33. return 0;
  34. return entries[ item ]->GetFilename( filename, filenameCch );
  35. }
  36. size_t Playlist::GetItemTitle( size_t item, wchar_t *title, size_t titleCch )
  37. {
  38. if ( item >= entries.size() )
  39. return 0;
  40. return entries[ item ]->GetTitle( title, titleCch );
  41. }
  42. const wchar_t *Playlist::ItemTitle( size_t item )
  43. {
  44. if ( item >= entries.size() )
  45. return 0;
  46. return entries[ item ]->filetitle;
  47. }
  48. const wchar_t *Playlist::ItemName( size_t item )
  49. {
  50. if ( item >= entries.size() )
  51. return 0;
  52. return entries[ item ]->filename;
  53. }
  54. int Playlist::GetItemLengthMilliseconds( size_t item )
  55. {
  56. if ( item >= entries.size() )
  57. return -1;
  58. return entries[ item ]->GetLengthInMilliseconds();
  59. }
  60. int Playlist::GetItemSizeBytes( size_t item )
  61. {
  62. if ( item >= entries.size() )
  63. return -1;
  64. return entries[ item ]->GetSizeInBytes();
  65. }
  66. size_t Playlist::GetItemExtendedInfo( size_t item, const wchar_t *metadata, wchar_t *info, size_t infoCch )
  67. {
  68. if ( item >= entries.size() )
  69. return 0;
  70. return entries[ item ]->GetExtendedInfo( metadata, info, infoCch );
  71. }
  72. uint64_t Playlist::GetPlaylistSizeBytes( void )
  73. {
  74. uint64_t size = 0;
  75. for ( pl_entry *l_entry : entries )
  76. size += l_entry->GetSizeInBytes();
  77. return size;
  78. }
  79. uint64_t Playlist::GetPlaylistLengthMilliseconds( void )
  80. {
  81. uint64_t length = 0;
  82. for ( pl_entry *l_entry : entries )
  83. length += l_entry->GetLengthInMilliseconds();
  84. return length;
  85. }
  86. int Playlist::Reverse()
  87. {
  88. // TODO: keep a bool flag and just do size-item-1 every time a GetItem* function is called
  89. std::reverse( entries.begin(), entries.end() );
  90. return PLAYLIST_SUCCESS;
  91. }
  92. int Playlist::Swap( size_t item1, size_t item2 )
  93. {
  94. std::swap( entries[ item1 ], entries[ item2 ] );
  95. return PLAYLIST_SUCCESS;
  96. }
  97. class RandMod
  98. {
  99. public:
  100. RandMod( int ( *_generator )( ) ) : generator( _generator ) {}
  101. int operator()( int n ) { return generator() % n; }
  102. int ( *generator )( );
  103. };
  104. int Playlist::Randomize( int ( *generator )( ) )
  105. {
  106. RandMod randMod( generator );
  107. std::random_shuffle( entries.begin(), entries.end(), randMod );
  108. return PLAYLIST_SUCCESS;
  109. }
  110. void Playlist::Remove( size_t item )
  111. {
  112. delete entries[ item ];
  113. entries.erase( entries.begin() + item );
  114. }
  115. void Playlist::SetItemFilename( size_t item, const wchar_t *filename )
  116. {
  117. if ( item < entries.size() )
  118. entries[ item ]->SetFilename( filename );
  119. }
  120. void Playlist::SetItemTitle( size_t item, const wchar_t *title )
  121. {
  122. if ( item < entries.size() )
  123. entries[ item ]->SetTitle( title );
  124. }
  125. void Playlist::SetItemLengthMilliseconds( size_t item, int length )
  126. {
  127. if ( item < entries.size() )
  128. entries[ item ]->SetLengthMilliseconds( length );
  129. }
  130. void Playlist::SetItemSizeBytes( size_t item, int size )
  131. {
  132. if ( item < entries.size() )
  133. entries[ item ]->SetSizeBytes( size );
  134. }
  135. void GetTitle( pl_entry *&a )
  136. {
  137. if ( !a->cached )
  138. {
  139. wchar_t title[ FILETITLE_SIZE ] = { 0 };
  140. int length = -1;
  141. mediaLibrary.GetFileInfo( a->filename, title, FILETITLE_SIZE, &length );
  142. a->SetLengthMilliseconds( length * 1000 );
  143. a->SetTitle( title );
  144. }
  145. }
  146. static bool PlayList_sortByTitle( pl_entry *&a, pl_entry *&b )
  147. {
  148. GetTitle( a );
  149. GetTitle( b );
  150. int comp = CompareStringW( LOCALE_USER_DEFAULT, NORM_IGNORECASE /*|NORM_IGNOREKANATYPE*/ | NORM_IGNOREWIDTH, a->filetitle, -1, b->filetitle, -1 );
  151. return comp == CSTR_LESS_THAN;
  152. // TODO: grab this function from winamp - return CompareStringLogical(a.strTitle, b.strTitle)<0;
  153. }
  154. static bool PlayList_sortByFile( pl_entry *&a, pl_entry *&b ) //const void *a, const void *b)
  155. {
  156. const wchar_t *file1 = PathFindFileNameW( a->filename );
  157. const wchar_t *file2 = PathFindFileNameW( b->filename );
  158. int comp = CompareStringW( LOCALE_USER_DEFAULT, NORM_IGNORECASE | /*NORM_IGNOREKANATYPE |*/ NORM_IGNOREWIDTH, file1, -1, file2, -1 );
  159. return comp == CSTR_LESS_THAN;
  160. // TODO: grab this function from winamp - return FileCompareLogical(file1, file2)<0;
  161. }
  162. static bool PlayList_sortByDirectory( pl_entry *&a, pl_entry *&b ) // by dir, then by title
  163. {
  164. const wchar_t *directory1 = a->filename;
  165. const wchar_t *directory2 = b->filename;
  166. const wchar_t *directoryEnd1 = scanstr_backcW( directory1, L"\\", 0 );
  167. const wchar_t *directoryEnd2 = scanstr_backcW( directory2, L"\\", 0 );
  168. size_t dirLen1 = directoryEnd1 - directory1;
  169. size_t dirLen2 = directoryEnd2 - directory2;
  170. if ( !dirLen1 && !dirLen2 ) // both in the current directory?
  171. return PlayList_sortByFile( a, b ); // not optimized, because the function does another scanstr_back, but easy for now :)
  172. if ( !dirLen1 ) // only the first dir is empty?
  173. return true; // sort it first
  174. if ( !dirLen2 ) // only the second dir empty?
  175. return false; // empty dirs go first
  176. #if 0 // TODO: grab this function from winamp
  177. int comp = FileCompareLogicalN( directory1, dirLen1, directory2, dirLen2 );
  178. if ( comp == 0 )
  179. return PlayList_sortByFile( a, b );
  180. else
  181. return comp < 0;
  182. #endif
  183. int comp = CompareStringW( LOCALE_USER_DEFAULT, NORM_IGNORECASE | /*NORM_IGNOREKANATYPE | */NORM_IGNOREWIDTH, directory1, (int)dirLen1, directory2, (int)dirLen2 );
  184. if ( comp == CSTR_EQUAL ) // same dir
  185. return PlayList_sortByFile( a, b ); // do second sort
  186. else // different dirs
  187. return comp == CSTR_LESS_THAN;
  188. }
  189. int Playlist::SortByTitle()
  190. {
  191. std::sort( entries.begin(), entries.end(), PlayList_sortByTitle );
  192. return 1;
  193. }
  194. int Playlist::SortByFilename()
  195. {
  196. std::sort( entries.begin(), entries.end(), PlayList_sortByFile );
  197. return 1;
  198. }
  199. int Playlist::SortByDirectory()
  200. {
  201. std::sort( entries.begin(), entries.end(), PlayList_sortByDirectory );
  202. return 1;
  203. }
  204. /*
  205. int Playlist::Move(size_t itemSrc, size_t itemDest)
  206. {
  207. if (itemSrc < itemDest)
  208. std::rotate(&entries[itemSrc], &entries[itemSrc], &entries[itemDest]);
  209. else
  210. if (itemSrc > itemDest)
  211. std::rotate(&entries[itemDest], &entries[itemSrc], &entries[itemSrc]);
  212. return 1;
  213. }*/
  214. bool Playlist::IsCached( size_t item )
  215. {
  216. return entries[ item ]->cached;
  217. }
  218. void Playlist::ClearCache( size_t item )
  219. {
  220. entries[ item ]->cached = false;
  221. }
  222. void Playlist::InsertPlaylist( Playlist &copy, size_t index )
  223. {
  224. for ( pl_entry *l_entry : copy.entries )
  225. {
  226. entries.insert( entries.begin() + index, l_entry );
  227. ++index;
  228. }
  229. copy.entries.clear();
  230. }
  231. void Playlist::AppendPlaylist( Playlist &copy )
  232. {
  233. entries.insert( entries.end(), std::make_move_iterator( copy.entries.begin() ), std::make_move_iterator( copy.entries.end() ) );
  234. //for ( pl_entry *l_entry : copy.entries )
  235. // entries.push_back( l_entry );
  236. copy.entries.clear();
  237. }
  238. #define CBCLASS Playlist
  239. START_MULTIPATCH;
  240. START_PATCH( patch_playlist )
  241. M_VCB( patch_playlist, ifc_playlist, IFC_PLAYLIST_CLEAR, Clear )
  242. //M_VCB(patch_playlist, ifc_playlist, IFC_PLAYLIST_APPENDWITHINFO, AppendWithInfo)
  243. //M_VCB(patch_playlist, ifc_playlist, IFC_PLAYLIST_APPEND, Append)
  244. M_CB( patch_playlist, ifc_playlist, IFC_PLAYLIST_GETNUMITEMS, GetNumItems )
  245. M_CB( patch_playlist, ifc_playlist, IFC_PLAYLIST_GETITEM, GetItem )
  246. M_CB( patch_playlist, ifc_playlist, IFC_PLAYLIST_GETITEMTITLE, GetItemTitle )
  247. M_CB( patch_playlist, ifc_playlist, IFC_PLAYLIST_GETITEMLENGTHMILLISECONDS, GetItemLengthMilliseconds )
  248. M_CB( patch_playlist, ifc_playlist, IFC_PLAYLIST_GETITEMEXTENDEDINFO, GetItemExtendedInfo )
  249. M_CB( patch_playlist, ifc_playlist, IFC_PLAYLIST_REVERSE, Reverse )
  250. M_CB( patch_playlist, ifc_playlist, IFC_PLAYLIST_SWAP, Swap )
  251. M_CB( patch_playlist, ifc_playlist, IFC_PLAYLIST_RANDOMIZE, Randomize )
  252. M_VCB( patch_playlist, ifc_playlist, IFC_PLAYLIST_REMOVE, Remove )
  253. M_CB( patch_playlist, ifc_playlist, IFC_PLAYLIST_SORTBYTITLE, SortByTitle )
  254. M_CB( patch_playlist, ifc_playlist, IFC_PLAYLIST_SORTBYFILENAME, SortByFilename )
  255. M_CB( patch_playlist, ifc_playlist, IFC_PLAYLIST_SORTBYDIRECTORY, SortByDirectory )
  256. NEXT_PATCH( patch_playlistloadercallback )
  257. M_VCB( patch_playlistloadercallback, ifc_playlistloadercallback, IFC_PLAYLISTLOADERCALLBACK_ONFILE, OnFile );
  258. END_PATCH
  259. END_MULTIPATCH;
  260. #undef CBCLASS