1
0

SPlaylists.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "SPlaylists.h"
  2. #include "api__playlist.h"
  3. #include "Playlists.h"
  4. #include "SPlaylistsEnumerator.h"
  5. #include "SPlaylist.h"
  6. #include "PlaylistManager.h"
  7. extern Playlists playlists;
  8. extern ScriptObjectController *script_root;
  9. extern PlaylistsScriptController playlistsController;
  10. // {5829EE15-3648-4c6e-B2FE-8736CBBF39DB}
  11. static const GUID makiPlaylistsGUID =
  12. { 0x5829ee15, 0x3648, 0x4c6e, { 0xb2, 0xfe, 0x87, 0x36, 0xcb, 0xbf, 0x39, 0xdb } };
  13. // -- Functions table -------------------------------------
  14. function_descriptor_struct PlaylistsScriptController::exportedFunction[] = {
  15. {L"GetEnumerator", 0, (void*)SPlaylists::script_vcpu_GetEnumerator },
  16. {L"OpenPlaylist", 1, (void*)SPlaylists::script_vcpu_OpenPlaylist },
  17. {L"SavePlaylist", 2, (void*)SPlaylists::script_vcpu_SavePlaylist },
  18. };
  19. // --------------------------------------------------------
  20. const wchar_t *PlaylistsScriptController::getClassName()
  21. {
  22. return L"Playlists";
  23. }
  24. const wchar_t *PlaylistsScriptController::getAncestorClassName()
  25. {
  26. return L"Object";
  27. }
  28. ScriptObjectController *PlaylistsScriptController::getAncestorController()
  29. {
  30. return script_root;
  31. }
  32. int PlaylistsScriptController::getInstantiable()
  33. {
  34. return 0;
  35. }
  36. int PlaylistsScriptController::getReferenceable()
  37. {
  38. return 0;
  39. }
  40. ScriptObject *PlaylistsScriptController::instantiate()
  41. {
  42. SPlaylists *xd = new SPlaylists;
  43. ASSERT(xd != NULL);
  44. return xd->getScriptObject();
  45. }
  46. void PlaylistsScriptController::destroy(ScriptObject *o)
  47. {
  48. SPlaylists *xd = static_cast<SPlaylists *>(o->vcpu_getInterface(makiPlaylistsGUID));
  49. ASSERT(xd != NULL);
  50. delete xd;
  51. }
  52. void *PlaylistsScriptController::encapsulate(ScriptObject *o)
  53. {
  54. return NULL;
  55. }
  56. void PlaylistsScriptController::deencapsulate(void *o)
  57. {}
  58. int PlaylistsScriptController::getNumFunctions()
  59. {
  60. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  61. }
  62. const function_descriptor_struct *PlaylistsScriptController::getExportedFunctions()
  63. {
  64. return exportedFunction;
  65. }
  66. GUID PlaylistsScriptController::getClassGuid()
  67. {
  68. return makiPlaylistsGUID;
  69. }
  70. /* --- */
  71. SPlaylists::SPlaylists()
  72. {
  73. getScriptObject()->vcpu_setInterface(makiPlaylistsGUID, static_cast<SPlaylists *>(this));
  74. getScriptObject()->vcpu_setClassName(L"Playlists");
  75. getScriptObject()->vcpu_setController(&playlistsController);
  76. }
  77. scriptVar SPlaylists::script_vcpu_GetEnumerator(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  78. {
  79. SCRIPT_FUNCTION_INIT;
  80. SPlaylistsEnumerator *enumerator = new SPlaylistsEnumerator();
  81. playlists.Lock();
  82. size_t count = playlists.GetCount();
  83. enumerator->Reserve(count);
  84. for (size_t i=0;i!=count;i++)
  85. {
  86. const PlaylistInfo &info = playlists.GetPlaylistInfo(i);
  87. enumerator->AppendPlaylist(info);
  88. }
  89. playlists.Unlock();
  90. return MAKE_SCRIPT_OBJECT(enumerator->getScriptObject());
  91. }
  92. scriptVar SPlaylists::script_vcpu_OpenPlaylist( SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar scriptPlaylistGUID )
  93. {
  94. SCRIPT_FUNCTION_INIT;
  95. GUID playlist_guid = nsGUID::fromCharW( GET_SCRIPT_STRING( scriptPlaylistGUID ) );
  96. SPlaylist *playlist = 0;
  97. playlists.Lock();
  98. size_t index;
  99. if ( playlists.GetPosition( playlist_guid, &index ) == API_PLAYLISTS_SUCCESS )
  100. {
  101. const wchar_t *filename = playlists.GetFilename( index );
  102. if ( filename )
  103. {
  104. playlist = new SPlaylist;
  105. if ( playlistManager.Load( filename, &playlist->playlist ) != PLAYLISTMANAGER_SUCCESS )
  106. {
  107. delete playlist;
  108. playlist = 0;
  109. }
  110. }
  111. }
  112. playlists.Unlock();
  113. return MAKE_SCRIPT_OBJECT( playlist ? playlist->getScriptObject() : 0 );
  114. }
  115. scriptVar SPlaylists::script_vcpu_SavePlaylist( SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar scriptPlaylistGUID, scriptVar scriptPlaylist )
  116. {
  117. SCRIPT_FUNCTION_INIT;
  118. GUID playlist_guid = nsGUID::fromCharW( GET_SCRIPT_STRING( scriptPlaylistGUID ) );
  119. SPlaylist *playlist = static_cast<SPlaylist *>( GET_SCRIPT_OBJECT_AS( scriptPlaylist, makiPlaylistGUID ) );
  120. if ( playlist )
  121. {
  122. playlists.Lock();
  123. size_t index;
  124. if ( playlists.GetPosition( playlist_guid, &index ) == API_PLAYLISTS_SUCCESS )
  125. {
  126. const wchar_t *filename = playlists.GetFilename( index );
  127. if ( filename )
  128. playlistManager.Save( filename, &playlist->playlist );
  129. }
  130. playlists.Unlock();
  131. }
  132. return MAKE_SCRIPT_OBJECT( playlist ? playlist->getScriptObject() : 0 );
  133. }