1
0

Playlist.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "main.h"
  2. #include "Playlist.h"
  3. #include <algorithm>
  4. #include "../nu/AutoChar.h"
  5. #include "../Winamp/strutil.h"
  6. void Playlist::Clear()
  7. {
  8. for ( pl_entry *entry : entries )
  9. delete entry;
  10. entries.clear();
  11. }
  12. int Playlist::OnFile( const wchar_t *p_filename, const wchar_t *p_title, int p_lengthInMS, ifc_plentryinfo *p_info )
  13. {
  14. entries.push_back( new pl_entry( p_filename, p_title, p_lengthInMS, p_info ) );
  15. return ifc_playlistloadercallback::LOAD_CONTINUE;
  16. }
  17. void Playlist::AppendWithInfo( const wchar_t *p_filename, const wchar_t *p_title, int p_lengthInMS )
  18. {
  19. entries.push_back( new pl_entry( p_filename, p_title, p_lengthInMS ) );
  20. }
  21. void Playlist::Insert( size_t p_index, const wchar_t *p_filename, const wchar_t *p_title, int p_lengthInMS )
  22. {
  23. entries.insert( entries.begin() + p_index, new pl_entry( p_filename, p_title, p_lengthInMS ) );
  24. }
  25. Playlist::~Playlist()
  26. {
  27. Clear();
  28. }
  29. size_t Playlist::GetNumItems()
  30. {
  31. return entries.size();
  32. }
  33. size_t Playlist::GetItem( size_t item, wchar_t *filename, size_t filenameCch )
  34. {
  35. if ( item >= entries.size() )
  36. return 0;
  37. return entries[ item ]->GetFilename( filename, filenameCch );
  38. }
  39. size_t Playlist::GetItemTitle( size_t item, wchar_t *title, size_t titleCch )
  40. {
  41. if ( item >= entries.size() )
  42. return 0;
  43. return entries[ item ]->GetTitle( title, titleCch );
  44. }
  45. const wchar_t *Playlist::ItemTitle( size_t item )
  46. {
  47. if ( item >= entries.size() )
  48. return 0;
  49. return entries[ item ]->filetitle;
  50. }
  51. const wchar_t *Playlist::ItemName( size_t item )
  52. {
  53. if ( item >= entries.size() )
  54. return 0;
  55. return entries[ item ]->filename;
  56. }
  57. int Playlist::GetItemLengthMilliseconds( size_t item )
  58. {
  59. if ( item >= entries.size() )
  60. return -1;
  61. return entries[ item ]->GetLengthInMilliseconds();
  62. }
  63. size_t Playlist::GetItemExtendedInfo( size_t item, const wchar_t *metadata, wchar_t *info, size_t infoCch )
  64. {
  65. if ( item >= entries.size() )
  66. return 0;
  67. return entries[ item ]->GetExtendedInfo( metadata, info, infoCch );
  68. }
  69. int Playlist::Reverse()
  70. {
  71. // TODO: keep a bool flag and just do size-item-1 every time a GetItem* function is called
  72. std::reverse( entries.begin(), entries.end() );
  73. return PLAYLIST_SUCCESS;
  74. }
  75. int Playlist::Swap( size_t item1, size_t item2 )
  76. {
  77. std::swap( entries[ item1 ], entries[ item2 ] );
  78. return PLAYLIST_SUCCESS;
  79. }
  80. class RandMod
  81. {
  82. public:
  83. RandMod( int ( *_generator )( ) ) : generator( _generator ) {}
  84. int operator ()( int n ) { return generator() % n; }
  85. int ( *generator )( );
  86. };
  87. int Playlist::Randomize( int ( *generator )( ) )
  88. {
  89. RandMod randMod( generator );
  90. std::random_shuffle( entries.begin(), entries.end(), randMod );
  91. return PLAYLIST_SUCCESS;
  92. }
  93. void Playlist::Remove( size_t item )
  94. {
  95. if ( entries.size() > item )
  96. entries.erase( entries.begin() + item );
  97. }
  98. void Playlist::SetItemFilename( size_t item, const wchar_t *filename )
  99. {
  100. if ( item < entries.size() )
  101. entries[ item ]->SetFilename( filename );
  102. }
  103. void Playlist::SetItemTitle( size_t item, const wchar_t *title )
  104. {
  105. if ( item < entries.size() )
  106. entries[ item ]->SetTitle( title );
  107. }
  108. void Playlist::SetItemLengthMilliseconds( size_t item, int length )
  109. {
  110. if ( item < entries.size() )
  111. entries[ item ]->SetLengthMilliseconds( length );
  112. }
  113. static bool PlayList_sortByTitle( pl_entry *&a, pl_entry *&b )
  114. {
  115. int comp = CompareStringW( LOCALE_USER_DEFAULT, NORM_IGNORECASE /*|NORM_IGNOREKANATYPE*/ | NORM_IGNOREWIDTH, a->filetitle, -1, b->filetitle, -1 );
  116. return comp == CSTR_LESS_THAN;
  117. // TODO: grab this function from winamp - return CompareStringLogical(a.strTitle, b.strTitle)<0;
  118. }
  119. static bool PlayList_sortByFile( pl_entry *&a, pl_entry *&b ) //const void *a, const void *b)
  120. {
  121. const wchar_t *file1 = PathFindFileNameW( a->filename );
  122. const wchar_t *file2 = PathFindFileNameW( b->filename );
  123. int comp = CompareStringW( LOCALE_USER_DEFAULT, NORM_IGNORECASE | /*NORM_IGNOREKANATYPE |*/ NORM_IGNOREWIDTH, file1, -1, file2, -1 );
  124. return comp == CSTR_LESS_THAN;
  125. // TODO: grab this function from winamp - return FileCompareLogical(file1, file2)<0;
  126. }
  127. static bool PlayList_sortByDirectory( pl_entry *&a, pl_entry *&b ) // by dir, then by p_title
  128. {
  129. const wchar_t *directory1 = a->filename;
  130. const wchar_t *directory2 = b->filename;
  131. const wchar_t *directoryEnd1 = scanstr_backcW( directory1, L"\\", 0 );
  132. const wchar_t *directoryEnd2 = scanstr_backcW( directory2, L"\\", 0 );
  133. int dirLen1 = (int)( directoryEnd1 - directory1 );
  134. int dirLen2 = (int)( directoryEnd2 - directory2 );
  135. if ( !dirLen1 && !dirLen2 ) // both in the current directory?
  136. return PlayList_sortByFile( a, b ); // not optimized, because the function does another scanstr_back, but easy for now :)
  137. if ( !dirLen1 ) // only the first dir is empty?
  138. return true; // sort it first
  139. if ( !dirLen2 ) // only the second dir empty?
  140. return false; // empty dirs go first
  141. #if 0 // TODO: grab this function from winamp
  142. int comp = FileCompareLogicalN( directory1, dirLen1, directory2, dirLen2 );
  143. if ( comp == 0 )
  144. return PlayList_sortByFile( a, b );
  145. else
  146. return comp < 0;
  147. #endif
  148. int comp = CompareStringW( LOCALE_USER_DEFAULT, NORM_IGNORECASE | /*NORM_IGNOREKANATYPE | */NORM_IGNOREWIDTH, directory1, dirLen1, directory2, dirLen2 );
  149. if ( comp == CSTR_EQUAL ) // same dir
  150. return PlayList_sortByFile( a, b ); // do second sort
  151. else // different dirs
  152. return comp == CSTR_LESS_THAN;
  153. }
  154. int Playlist::SortByTitle()
  155. {
  156. std::sort( entries.begin(), entries.end(), PlayList_sortByTitle );
  157. return 1;
  158. }
  159. int Playlist::SortByFilename()
  160. {
  161. std::sort( entries.begin(), entries.end(), PlayList_sortByFile );
  162. return 1;
  163. }
  164. int Playlist::SortByDirectory()
  165. {
  166. std::sort( entries.begin(), entries.end(), PlayList_sortByDirectory );
  167. return 1;
  168. }
  169. /*
  170. int Playlist::Move(size_t itemSrc, size_t itemDest)
  171. {
  172. if (itemSrc < itemDest)
  173. std::rotate(&entries[itemSrc], &entries[itemSrc], &entries[itemDest]);
  174. else
  175. if (itemSrc > itemDest)
  176. std::rotate(&entries[itemDest], &entries[itemSrc], &entries[itemSrc]);
  177. return 1;
  178. }*/