PlaylistInfo.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include <assert.h>
  2. #include <strsafe.h>
  3. #include "main.h"
  4. #include "PlaylistInfo.h"
  5. #include "nu/AutoLock.h"
  6. int uniqueAddress;
  7. using namespace Nullsoft::Utility;
  8. static INT_PTR FindTreeID( GUID playlist_guid )
  9. {
  10. INT_PTR treeId = 0;
  11. //if ( tree_to_guid_map.reverseLookup( playlist_guid, &treeId ) )
  12. // return treeId;
  13. //else
  14. // return 0;
  15. // Look for values and return the index
  16. for (auto& item : tree_to_guid_map)
  17. {
  18. if (0 == memcmp(&item.second, &playlist_guid, sizeof(GUID)))
  19. {
  20. return item.first;
  21. }
  22. }
  23. return 0;
  24. }
  25. PlaylistInfo::PlaylistInfo()
  26. {
  27. Clear();
  28. }
  29. void PlaylistInfo::Clear()
  30. {
  31. length = 0;
  32. numItems = 0;
  33. treeId = 0;
  34. index = 0;
  35. iterator = 0;
  36. cloud = 0;
  37. playlist_guid = INVALID_GUID;
  38. }
  39. bool PlaylistInfo::Associate( INT_PTR _treeId )
  40. {
  41. Clear();
  42. treeId = _treeId;
  43. playlist_guid = tree_to_guid_map[treeId];
  44. if ( AGAVE_API_PLAYLISTS->GetPosition( playlist_guid, &index ) == API_PLAYLISTS_SUCCESS )
  45. {
  46. iterator = AGAVE_API_PLAYLISTS->GetIterator();
  47. AGAVE_API_PLAYLISTS->GetInfo( index, api_playlists_itemCount, &numItems, sizeof( numItems ) );
  48. AGAVE_API_PLAYLISTS->GetInfo( index, api_playlists_totalTime, &length, sizeof( length ) );
  49. AGAVE_API_PLAYLISTS->GetInfo( index, api_playlists_cloud, &cloud, sizeof( cloud ) );
  50. }
  51. else
  52. playlist_guid = INVALID_GUID;
  53. return Valid();
  54. }
  55. // as a pre-condition, AGAVE_API_PLAYLISTS needs to be locked
  56. PlaylistInfo::PlaylistInfo( size_t p_index )
  57. {
  58. Clear();
  59. index = p_index;
  60. iterator = AGAVE_API_PLAYLISTS->GetIterator();
  61. playlist_guid = AGAVE_API_PLAYLISTS->GetGUID( index );
  62. AGAVE_API_PLAYLISTS->GetInfo( index, api_playlists_itemCount, &numItems, sizeof( numItems ) );
  63. AGAVE_API_PLAYLISTS->GetInfo( index, api_playlists_totalTime, &length, sizeof( length ) );
  64. AGAVE_API_PLAYLISTS->GetInfo( index, api_playlists_cloud, &cloud, sizeof( cloud ) );
  65. treeId = FindTreeID( playlist_guid ); // try to find treeId
  66. }
  67. // as a pre-condition, AGAVE_API_PLAYLISTS needs to be locked
  68. PlaylistInfo::PlaylistInfo( GUID p_playlist_guid )
  69. {
  70. Clear();
  71. playlist_guid = p_playlist_guid;
  72. if ( AGAVE_API_PLAYLISTS->GetPosition( playlist_guid, &index ) == API_PLAYLISTS_SUCCESS )
  73. {
  74. iterator = AGAVE_API_PLAYLISTS->GetIterator();
  75. AGAVE_API_PLAYLISTS->GetInfo( index, api_playlists_itemCount, &numItems, sizeof( numItems ) );
  76. AGAVE_API_PLAYLISTS->GetInfo( index, api_playlists_totalTime, &length, sizeof( length ) );
  77. AGAVE_API_PLAYLISTS->GetInfo( index, api_playlists_cloud, &cloud, sizeof( cloud ) );
  78. treeId = FindTreeID( playlist_guid ); // try to find treeId
  79. }
  80. else
  81. playlist_guid = INVALID_GUID;
  82. }
  83. PlaylistInfo::PlaylistInfo( const PlaylistInfo &copy )
  84. {
  85. Clear();
  86. index = copy.index;
  87. iterator = copy.iterator;
  88. playlist_guid = copy.playlist_guid;
  89. length = copy.length;
  90. numItems = copy.numItems;
  91. treeId = copy.treeId;
  92. cloud = copy.cloud;
  93. }
  94. size_t PlaylistInfo::GetIndex()
  95. {
  96. assert( Valid() );
  97. AutoLockT<api_playlists> lock( AGAVE_API_PLAYLISTS );
  98. size_t curIterator = AGAVE_API_PLAYLISTS->GetIterator();
  99. if ( curIterator != iterator )
  100. {
  101. iterator = curIterator;
  102. AGAVE_API_PLAYLISTS->GetPosition( playlist_guid, &index );
  103. }
  104. return index;
  105. }
  106. void PlaylistInfo::IssueSaveCallback()
  107. {
  108. assert( Valid() );
  109. AutoLockT<api_playlists> lock( AGAVE_API_PLAYLISTS );
  110. WASABI_API_SYSCB->syscb_issueCallback( api_playlists::SYSCALLBACK, api_playlists::PLAYLIST_SAVED, GetIndex(), (intptr_t)&uniqueAddress );
  111. }
  112. void PlaylistInfo::Refresh()
  113. {
  114. assert( Valid() );
  115. if ( AGAVE_API_PLAYLISTS->GetPosition( playlist_guid, &index ) == API_PLAYLISTS_SUCCESS )
  116. {
  117. iterator = AGAVE_API_PLAYLISTS->GetIterator();
  118. AGAVE_API_PLAYLISTS->GetInfo( index, api_playlists_itemCount, &numItems, sizeof( numItems ) );
  119. AGAVE_API_PLAYLISTS->GetInfo( index, api_playlists_totalTime, &length, sizeof( length ) );
  120. }
  121. else
  122. playlist_guid = INVALID_GUID;
  123. }
  124. const wchar_t *PlaylistInfo::GetFilename()
  125. {
  126. assert( Valid() );
  127. if ( _filename.empty() )
  128. _filename = AGAVE_API_PLAYLISTS->GetFilename( GetIndex() );
  129. return _filename.c_str();
  130. }
  131. // TODO: we should investigate caching the title
  132. const wchar_t *PlaylistInfo::GetName()
  133. {
  134. assert( Valid() );
  135. if ( _title.empty() )
  136. _title = AGAVE_API_PLAYLISTS->GetName( GetIndex() );
  137. return _title.c_str();
  138. }
  139. size_t PlaylistInfo::GetLength()
  140. {
  141. assert( Valid() );
  142. return length;
  143. }
  144. void PlaylistInfo::SetLength( size_t newLength )
  145. {
  146. assert( Valid() );
  147. length = newLength;
  148. AutoLockT<api_playlists> lock( AGAVE_API_PLAYLISTS );
  149. AGAVE_API_PLAYLISTS->SetInfo( GetIndex(), api_playlists_totalTime, &length, sizeof( length ) );
  150. }
  151. size_t PlaylistInfo::GetSize()
  152. {
  153. assert( Valid() );
  154. return numItems;
  155. }
  156. size_t PlaylistInfo::GetCloud()
  157. {
  158. assert( Valid() );
  159. return cloud;
  160. }
  161. void PlaylistInfo::SetCloud(size_t newCloud)
  162. {
  163. assert( Valid() );
  164. cloud = newCloud;
  165. AutoLockT<api_playlists> lock( AGAVE_API_PLAYLISTS );
  166. AGAVE_API_PLAYLISTS->SetInfo( GetIndex(), api_playlists_cloud, &cloud, sizeof( cloud ) );
  167. }
  168. bool PlaylistInfo::Valid()
  169. {
  170. return !!( playlist_guid != INVALID_GUID );
  171. }
  172. void PlaylistInfo::SetSize( size_t newSize )
  173. {
  174. assert( Valid() );
  175. numItems = newSize;
  176. AutoLockT<api_playlists> lock( AGAVE_API_PLAYLISTS );
  177. AGAVE_API_PLAYLISTS->SetInfo( GetIndex(), api_playlists_itemCount, &numItems, sizeof( numItems ) );
  178. }