Playlist.cpp 9.0 KB

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