PlaylistSync.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "PlaylistSync.h"
  2. #include "api.h"
  3. #include "../nu/AutoUrl.h"
  4. #include "main.h"
  5. #include "../../..\Components\wac_network\wac_network_http_receiver_api.h"
  6. #include <strsafe.h>
  7. /* classes and utility functions to notifying the device of playlist modifications */
  8. class SimpleCallback : public ifc_downloadManagerCallback
  9. {
  10. public:
  11. void OnInit(DownloadToken token)
  12. {
  13. api_httpreceiver *jnet = WAC_API_DOWNLOADMANAGER->GetReceiver(token);
  14. if (jnet)
  15. {
  16. jnet->AddHeaderValue("X-Winamp-ID", winamp_id_str);
  17. jnet->AddHeaderValue("X-Winamp-Name", winamp_name);
  18. }
  19. }
  20. RECVS_DISPATCH;
  21. };
  22. #define CBCLASS SimpleCallback
  23. START_DISPATCH;
  24. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONINIT, OnInit)
  25. END_DISPATCH;
  26. #undef CBCLASS
  27. class NewPlaylistCallback : public ifc_downloadManagerCallback
  28. {
  29. public:
  30. NewPlaylistCallback()
  31. {
  32. event = CreateEvent(NULL, TRUE, FALSE, NULL);
  33. location=0;
  34. }
  35. ~NewPlaylistCallback()
  36. {
  37. CloseHandle(event);
  38. free(location);
  39. }
  40. void OnInit(DownloadToken token)
  41. {
  42. api_httpreceiver *jnet = WAC_API_DOWNLOADMANAGER->GetReceiver(token);
  43. if (jnet)
  44. {
  45. jnet->AddHeaderValue("X-Winamp-ID", winamp_id_str);
  46. jnet->AddHeaderValue("X-Winamp-Name", winamp_name);
  47. }
  48. }
  49. void OnCancel(DownloadToken token)
  50. {
  51. SetEvent(event);
  52. }
  53. void OnError(DownloadToken token, int error)
  54. {
  55. SetEvent(event);
  56. }
  57. void OnFinish(DownloadToken token)
  58. {
  59. api_httpreceiver *jnet = WAC_API_DOWNLOADMANAGER->GetReceiver(token);
  60. if (jnet)
  61. {
  62. const char *jnet_location = jnet->getheader("Location");
  63. if (jnet_location)
  64. location = strdup(jnet_location);
  65. }
  66. SetEvent(event);
  67. }
  68. const char *Wait()
  69. {
  70. WaitForSingleObject(event, INFINITE);
  71. return location;
  72. }
  73. HANDLE event;
  74. char *location;
  75. RECVS_DISPATCH;
  76. };
  77. #define CBCLASS NewPlaylistCallback
  78. START_DISPATCH;
  79. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONINIT, OnInit)
  80. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONCANCEL, OnCancel)
  81. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONERROR, OnInit)
  82. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONFINISH, OnFinish)
  83. END_DISPATCH;
  84. #undef CBCLASS
  85. static SimpleCallback simple_callback;
  86. void Sync_AddToPlaylist(const char *root_url, const wchar_t *playlist_id, const wchar_t *song_id)
  87. {
  88. if ( WAC_API_DOWNLOADMANAGER )
  89. {
  90. char url[1024] = {0};
  91. StringCbPrintfA(url, sizeof(url), "%s/playlist?action=add&id=%s&songid=%s", root_url, AutoUrl(playlist_id), AutoUrl(song_id));
  92. WAC_API_DOWNLOADMANAGER->DownloadEx(url, &simple_callback, api_downloadManager::DOWNLOADEX_BUFFER);
  93. }
  94. }
  95. void Sync_RemoveFromPlaylist(const char *root_url, const wchar_t *playlist_id, const wchar_t *song_id)
  96. {
  97. if ( WAC_API_DOWNLOADMANAGER )
  98. {
  99. char url[1024] = {0};
  100. StringCbPrintfA(url, sizeof(url), "%s/playlist?action=remove&id=%s&songid=%s", root_url, AutoUrl(playlist_id), AutoUrl(song_id));
  101. WAC_API_DOWNLOADMANAGER->DownloadEx(url, &simple_callback, api_downloadManager::DOWNLOADEX_BUFFER);
  102. }
  103. }
  104. void Sync_DeletePlaylist(const char *root_url, const wchar_t *playlist_id)
  105. {
  106. if ( WAC_API_DOWNLOADMANAGER )
  107. {
  108. char url[1024] = {0};
  109. StringCbPrintfA(url, sizeof(url), "%s/playlist?action=delete&id=%s", root_url, AutoUrl(playlist_id));
  110. WAC_API_DOWNLOADMANAGER->DownloadEx(url, &simple_callback, api_downloadManager::DOWNLOADEX_BUFFER);
  111. }
  112. }
  113. WifiPlaylist *Sync_NewPlaylist(const char *root_url, const wchar_t *playlist_name)
  114. {
  115. if ( WAC_API_DOWNLOADMANAGER )
  116. {
  117. NewPlaylistCallback new_playlist_callback;
  118. char url[1024] = {0};
  119. StringCbPrintfA(url, sizeof(url), "%s/playlist?action=new&name=%s", root_url, AutoUrl(playlist_name));
  120. WAC_API_DOWNLOADMANAGER->DownloadEx(url, &new_playlist_callback, api_downloadManager::DOWNLOADEX_BUFFER);
  121. const char *playlist_id = new_playlist_callback.Wait();
  122. if (playlist_id)
  123. {
  124. return new WifiPlaylist(playlist_id, playlist_name);
  125. }
  126. }
  127. return 0;
  128. }
  129. void Sync_RenamePlaylist(const char *root_url, const wchar_t *playlist_id, const wchar_t *playlist_name)
  130. {
  131. if ( WAC_API_DOWNLOADMANAGER )
  132. {
  133. char url[ 1024 ] = { 0 };
  134. StringCbPrintfA( url, sizeof( url ), "%s/playlist?action=rename&id=%s&name=%s", root_url, AutoUrl( playlist_id ), AutoUrl( playlist_name ) );
  135. WAC_API_DOWNLOADMANAGER->DownloadEx( url, &simple_callback, api_downloadManager::DOWNLOADEX_BUFFER );
  136. }
  137. }