playlists.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "main.h"
  2. #include "replicant/nu/AutoWide.h"
  3. #include "replicant/nu/AutoLock.h"
  4. #include <algorithm>
  5. #include <strsafe.h>
  6. using namespace Nullsoft::Utility;
  7. TREE_TO_GUID_MAP tree_to_guid_map;
  8. bool FindTreeItem( INT_PTR treeId )
  9. {
  10. TREE_TO_GUID_MAP::iterator itr = tree_to_guid_map.find( treeId );
  11. return itr != tree_to_guid_map.end();
  12. }
  13. void MakeTree( PlaylistInfo &playlist )
  14. {
  15. NAVINSERTSTRUCT nis = { 0 };
  16. nis.item.cbSize = sizeof( NAVITEM );
  17. nis.item.pszText = const_cast<wchar_t *>( AGAVE_API_PLAYLISTS->GetName( playlist.GetIndex() ) );
  18. nis.item.mask = NIMF_TEXT | NIMF_IMAGE | NIMF_IMAGESEL | NIMF_ITEMID;
  19. nis.item.id = playlist.treeId = 3002 + playlist.GetIndex();
  20. nis.hParent = playlistItem;
  21. if ( playlists_CloudInstalled() )
  22. nis.item.iImage = nis.item.iSelectedImage = ( !playlist.GetCloud() ? normalimage : cloudImage );
  23. else
  24. nis.item.iImage = nis.item.iSelectedImage = normalimage;
  25. if ( MLNavCtrl_InsertItem( plugin.hwndLibraryParent, &nis ) )
  26. tree_to_guid_map[ playlist.treeId ] = playlist.playlist_guid;
  27. }
  28. void UpdateTree( PlaylistInfo &playlist, int tree_id )
  29. {
  30. AutoLockT<api_playlists> lock( AGAVE_API_PLAYLISTS );
  31. size_t index = playlist.GetIndex();
  32. MLTREEITEMW updatedItem = { NULL,MLTI_TEXT,NULL };
  33. updatedItem.id = tree_id;
  34. updatedItem.title = const_cast<wchar_t *>( AGAVE_API_PLAYLISTS->GetName( index ) );
  35. updatedItem.imageIndex = ( !playlist.GetCloud() ? imgPL : imgCloudPL );
  36. mediaLibrary.SetTreeItem( updatedItem );
  37. tree_to_guid_map[ tree_id ] = playlist.playlist_guid;
  38. }
  39. void AddPlaylist( int callback, const wchar_t *title, const wchar_t *filename, bool makeTree, int cloud, size_t numItems, uint64_t length )
  40. {
  41. AutoLockT<api_playlists> lock( AGAVE_API_PLAYLISTS );
  42. wchar_t fullFilename[ MAX_PATH ] = { 0 };
  43. if ( PathIsFileSpecW( filename ) )
  44. PathCombineW( fullFilename, g_path, filename );
  45. else
  46. lstrcpynW( fullFilename, filename, MAX_PATH );
  47. size_t newIndex = AGAVE_API_PLAYLISTS->AddPlaylist_NoCallback( fullFilename, title );
  48. // try to get a valid length of the playlist time
  49. // (important for the playlists view otherwise looks silly with just the number shown)
  50. if ( !length )
  51. {
  52. length = AGAVE_API_PLAYLISTMANAGER->GetLongLengthMilliseconds( fullFilename );
  53. if ( length > 0 ) length /= 1000;
  54. else length = 0;
  55. }
  56. if ( cloud )
  57. AGAVE_API_PLAYLISTS->SetInfo( newIndex, api_playlists_cloud, &cloud, sizeof( cloud ) );
  58. if ( numItems > 0 )
  59. AGAVE_API_PLAYLISTS->SetInfo( newIndex, api_playlists_itemCount, &numItems, sizeof( numItems ) );
  60. if ( length > 0 )
  61. AGAVE_API_PLAYLISTS->SetInfo( newIndex, api_playlists_totalTime, &length, sizeof( length ) );
  62. if ( callback )
  63. WASABI_API_SYSCB->syscb_issueCallback( api_playlists::SYSCALLBACK, api_playlists::PLAYLIST_ADDED, newIndex, ( callback - 1 ) );
  64. }
  65. bool LoadOldPlaylists()
  66. {
  67. bool erased = false;
  68. int nb = g_config->ReadInt( L"query_num", 0 );
  69. for ( int i = 0; i < nb; i++ )
  70. {
  71. wchar_t qn[ 128 ] = { 0 }, qv[ 128 ] = { 0 }, qm[ 128 ] = { 0 }, qmet[ 128 ] = { 0 };
  72. StringCchPrintfW( qn, 128, L"query%i_name", i + 1 );
  73. StringCchPrintfW( qv, 128, L"query%i_val", i + 1 );
  74. StringCchPrintfW( qm, 128, L"query%i_mode", i + 1 );
  75. StringCchPrintfW( qmet, 128, L"query%i_meta", i + 1 );
  76. int queryMode = g_config->ReadInt( qm, 0 );
  77. if ( queryMode == 32 )
  78. {
  79. wchar_t *name = g_config->ReadString( qn, NULL );
  80. if ( !name )
  81. continue;
  82. name = _wcsdup( name );
  83. wchar_t *val = g_config->ReadString( qv, NULL );
  84. if ( val )
  85. val = _wcsdup( val );
  86. wchar_t filename[ MAX_PATH ] = { 0 };
  87. PathCombineW( filename, g_path, val );
  88. size_t numItems = AGAVE_API_PLAYLISTMANAGER->CountItems( filename );
  89. AddPlaylist( true, name, filename, ADD_TO_TREE, AddToCloud(), numItems );
  90. g_config->WriteString( qn, NULL );
  91. g_config->WriteString( qv, NULL );
  92. g_config->WriteString( qm, NULL );
  93. g_config->WriteString( qmet, NULL );
  94. erased = true;
  95. free( name );
  96. free( val );
  97. }
  98. }
  99. return erased;
  100. }
  101. void LoadPlaylists()
  102. {
  103. bool loadedOld = LoadOldPlaylists();
  104. AutoLockT<api_playlists> lock( AGAVE_API_PLAYLISTS );
  105. size_t count = AGAVE_API_PLAYLISTS->GetCount();
  106. normalimage = mediaLibrary.AddTreeImageBmp( IDB_TREEITEM_PLAYLIST );
  107. cloudImage = mediaLibrary.AddTreeImageBmp( IDB_TREEITEM_CLOUD_PLAYLIST );
  108. for ( size_t i = 0; i != count; i++ )
  109. {
  110. PlaylistInfo info( i );
  111. if ( info.Valid() )
  112. MakeTree( info );
  113. }
  114. if ( loadedOld )
  115. AGAVE_API_PLAYLISTS->Flush();
  116. }
  117. void UpdatePlaylists()
  118. {
  119. AutoLockT<api_playlists> lock( AGAVE_API_PLAYLISTS );
  120. size_t count = AGAVE_API_PLAYLISTS->GetCount();
  121. normalimage = mediaLibrary.AddTreeImageBmp( IDB_TREEITEM_PLAYLIST );
  122. cloudImage = mediaLibrary.AddTreeImageBmp( IDB_TREEITEM_CLOUD_PLAYLIST );
  123. for ( size_t i = 0; i != count; i++ )
  124. {
  125. PlaylistInfo info( i );
  126. if ( info.Valid() )
  127. UpdateTree( info, info.treeId );
  128. }
  129. if ( IsWindow( currentView ) )
  130. PostMessage( currentView, WM_APP + 101, 0, 0 );
  131. }
  132. void Playlist_importFromWinamp()
  133. {
  134. SendMessage( plugin.hwndWinampParent, WM_WA_IPC, 0, IPC_WRITEPLAYLIST );
  135. const wchar_t *m3udir = (const wchar_t *)SendMessage( plugin.hwndWinampParent, WM_WA_IPC, 0, IPC_GETM3UDIRECTORYW );
  136. wchar_t s[ MAX_PATH ] = { 0 };
  137. PathCombineW( s, m3udir, L"winamp.m3u8" );
  138. wchar_t filename[ 1024 + 256 ] = { 0 };
  139. wchar_t *filenameptr = createPlayListDBFileName( filename );
  140. wchar_t gs[ MAX_PATH ] = { 0 };
  141. PathCombineW( gs, g_path, filenameptr );
  142. size_t numItems = AGAVE_API_PLAYLISTMANAGER->Copy( gs, s );
  143. AddPlaylist( true, WASABI_API_LNGSTRINGW( IDS_IMPORTED_PLAYLIST ), gs, ADD_TO_TREE, ( !( GetAsyncKeyState( VK_SHIFT ) & 0x8000 ) ? AddToCloud() : 0 ), numItems );
  144. AGAVE_API_PLAYLISTS->Flush();
  145. }