AddPlaylist.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <shlwapi.h>
  2. #include <strsafe.h> // Include this last
  3. #include <algorithm>
  4. #include "main.h"
  5. #include "resource.h"
  6. #include "../../General/gen_ml/config.h"
  7. #include "../nu/autolock.h"
  8. #include "../nu/autowide.h"
  9. //#include "../playlist/api_playlists.h"
  10. #include "../nu/MediaLibraryInterface.h"
  11. wchar_t *createPlayListDBFileName( wchar_t *filename ) // filename is ignored but used for temp space, make sure it's 1024+256 chars =)
  12. {
  13. wchar_t g_path[ MAX_PATH ] = { 0 };
  14. mediaLibrary.BuildPath( L"Plugins\\ml", g_path, MAX_PATH );
  15. wchar_t *filenameptr;
  16. int x = 32;
  17. for ( ;;)
  18. {
  19. GetTempFileNameW( g_path, L"plf", GetTickCount() + x * 5000, filename );
  20. if ( lstrlenW( filename ) > 4 )
  21. {
  22. int length = lstrlenW( filename ); // Get length
  23. StringCchCopyW( filename + length - 4, length, L".m3u" ); // Add the extension
  24. }
  25. HANDLE h = CreateFileW( filename, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CREATE_NEW, 0, 0 );
  26. if ( h != INVALID_HANDLE_VALUE )
  27. {
  28. filenameptr = filename + lstrlenW( g_path ) + 1;
  29. CloseHandle( h );
  30. break;
  31. }
  32. if ( ++x > 4096 )
  33. {
  34. filenameptr = L"error.m3u";
  35. break;
  36. }
  37. }
  38. // Cleanup
  39. return filenameptr; // return pointer to just the base filename
  40. }
  41. void CreateAndAddPlaylist( const wchar_t *name )
  42. {
  43. wchar_t filename[ MAX_PATH ] = { 0 }, dir[ MAX_PATH ] = { 0 };
  44. GetTempPathW( MAX_PATH, dir );
  45. GetTempFileNameW( dir, L"ml_playlist", 0, filename );
  46. StringCchPrintfW( filename, MAX_PATH, L"%s.m3u8", filename );
  47. int result = -1;
  48. // See if we want to include the seed tracks in our playlist
  49. if ( useSeed == TRUE )
  50. {
  51. seedPlaylist.AppendPlaylist( currentPlaylist );
  52. result = AGAVE_API_PLAYLISTMGR->Save( filename, &seedPlaylist ); // Save the seed playlist contents since we appended the regular playlist to it
  53. }
  54. else
  55. result = AGAVE_API_PLAYLISTMGR->Save( filename, &currentPlaylist ); // Save the current playlist contents to the file so they will be in ML view
  56. if ( result == PLAYLISTMANAGER_SUCCESS )
  57. {
  58. mlAddPlaylist p = { sizeof( p ),name,filename, PL_FLAGS_IMPORT,-1,-1 };
  59. SendMessage( plugin.hwndLibraryParent, WM_ML_IPC, (WPARAM)&p, ML_IPC_PLAYLIST_ADD );
  60. DeleteFileW( filename );
  61. }
  62. }
  63. void TimeStamp( wchar_t *buf, int cch )
  64. {
  65. wchar_t time_str[ 32 ] = { 0 }, date_str[ 32 ] = { 0 };
  66. GetDateFormat( 0, 0, 0, 0, date_str, 32 );
  67. GetTimeFormat( 0, 0, 0, 0, time_str, 32 );
  68. StringCchPrintfW( buf, cch, L"%s - %s", date_str, time_str );
  69. }
  70. INT_PTR CALLBACK AddPlaylistDialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
  71. {
  72. Playlist *seedPlaylist = (Playlist *)lParam;
  73. switch ( uMsg )
  74. {
  75. case WM_INITDIALOG:
  76. {
  77. //Add the default name here:
  78. wchar_t default_pl_name[ 1024 ] = { 0 }, timestamp_t[ 64 ] = { 0 }, title[ 256 ] = { 0 };
  79. TimeStamp( timestamp_t, 64 ); // Get the timestamp
  80. if ( seedPlaylist->entries[ 0 ] ) // Only grab the seed track if it is actually there
  81. seedPlaylist->entries[ 0 ]->GetTitle( title, 256 );
  82. StringCchPrintf( default_pl_name, 1024, L"%s'%s' @ %s", WASABI_API_LNGSTRINGW( IDS_PL_NAME_PREFIX ), title, timestamp_t );
  83. SetDlgItemText( hwndDlg, IDC_EDIT_NAME, default_pl_name );
  84. PostMessage( hwndDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem( hwndDlg, IDC_EDIT_NAME ), TRUE );
  85. break;
  86. }
  87. case WM_COMMAND:
  88. switch ( LOWORD( wParam ) )
  89. {
  90. case IDOK: // On OK create the playlist
  91. {
  92. wchar_t name[ 256 ] = { 0 };
  93. GetDlgItemText( hwndDlg, IDC_EDIT_NAME, name, 255 );
  94. if ( !name[ 0 ] ) // Error if a valid name is not provided
  95. {
  96. wchar_t titleStr[ 32 ] = { 0 };
  97. MessageBox( hwndDlg, WASABI_API_LNGSTRINGW( IDS_ENTER_A_NAME ),
  98. WASABI_API_LNGSTRINGW_BUF( IDS_ERROR, titleStr, 32 ), MB_OK );
  99. break;
  100. }
  101. CreateAndAddPlaylist( name );
  102. AGAVE_API_PLAYLISTS->Flush();
  103. //lParam = IDOK; // Set the code to ok as a return to the calling function
  104. EndDialog( hwndDlg, IDOK );
  105. }
  106. break;
  107. case IDCANCEL:
  108. //lParam = IDCANCEL; // Set the code to cancel as a return to the calling function
  109. EndDialog( hwndDlg, IDCANCEL );
  110. break;
  111. }
  112. break;
  113. }
  114. return FALSE;
  115. };