SongListDownloader.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "api.h"
  2. #include "main.h"
  3. #include "images.h"
  4. #include "SongListDownloader.h"
  5. #include "../../..\Components\wac_network\wac_network_http_receiver_api.h"
  6. #include <api/service/waServiceFactory.h>
  7. #include <strsafe.h>
  8. WifiXML::WifiXML(obj_xml *parser) : parser(parser)
  9. {
  10. wifi_track = 0;
  11. wifi_playlist = 0;
  12. parser->xmlreader_registerCallback(L"items", this);
  13. parser->xmlreader_registerCallback(L"items\fdevice", &info_xml);
  14. parser->xmlreader_registerCallback(L"items\fspace", &info_xml);
  15. parser->xmlreader_registerCallback(L"items\fitem", this);
  16. parser->xmlreader_registerCallback(L"items\fplaylist", this);
  17. parser->xmlreader_registerCallback(L"items\fplaylist\fitem", this);
  18. parser->xmlreader_registerCallback(L"items\fitem\fartist", &artist);
  19. parser->xmlreader_registerCallback(L"items\fitem\falbum", &album);
  20. parser->xmlreader_registerCallback(L"items\fitem\fcomposer", &composer);
  21. parser->xmlreader_registerCallback(L"items\fitem\fduration", &duration);
  22. parser->xmlreader_registerCallback(L"items\fitem\ftrack", &track);
  23. parser->xmlreader_registerCallback(L"items\fitem\fyear", &year);
  24. parser->xmlreader_registerCallback(L"items\fitem\fsize", &size);
  25. parser->xmlreader_registerCallback(L"items\fitem\ftitle", &title);
  26. parser->xmlreader_registerCallback(L"items\fitem\fmime", &mime_type);
  27. parser->xmlreader_registerCallback(L"items\fplaylist\fitem\fartist", &artist);
  28. parser->xmlreader_registerCallback(L"items\fplaylist\fitem\falbum", &album);
  29. parser->xmlreader_registerCallback(L"items\fplaylist\fitem\fcomposer", &composer);
  30. parser->xmlreader_registerCallback(L"items\fplaylist\fitem\fduration", &duration);
  31. parser->xmlreader_registerCallback(L"items\fplaylist\fitem\ftrack", &track);
  32. parser->xmlreader_registerCallback(L"items\fplaylist\fitem\fyear", &year);
  33. parser->xmlreader_registerCallback(L"items\fplaylist\fitem\fsize", &size);
  34. parser->xmlreader_registerCallback(L"items\fplaylist\fitem\ftitle", &title);
  35. parser->xmlreader_registerCallback(L"items\fplaylist\fitem\fmime", &mime_type);
  36. parser->xmlreader_registerCallback(L"items\fplaylist\fitem\fmodified", &modified);
  37. }
  38. WifiXML::~WifiXML()
  39. {
  40. parser->xmlreader_unregisterCallback(this);
  41. parser->xmlreader_unregisterCallback(&info_xml);
  42. parser->xmlreader_unregisterCallback(&artist);
  43. parser->xmlreader_unregisterCallback(&album);
  44. parser->xmlreader_unregisterCallback(&composer);
  45. parser->xmlreader_unregisterCallback(&duration);
  46. parser->xmlreader_unregisterCallback(&track);
  47. parser->xmlreader_unregisterCallback(&year);
  48. parser->xmlreader_unregisterCallback(&size);
  49. parser->xmlreader_unregisterCallback(&title);
  50. parser->xmlreader_unregisterCallback(&mime_type);
  51. parser->xmlreader_unregisterCallback(&modified);
  52. }
  53. void WifiXML::xmlReaderOnStartElementCallback(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params)
  54. {
  55. if (!wcscmp(xmltag, L"item"))
  56. {
  57. const wchar_t *value = params->getItemValue(L"id");
  58. if (value)
  59. {
  60. wifi_track = new WifiTrack;
  61. wifi_track->id = _wcsdup(value);
  62. }
  63. }
  64. else if (!wcscmp(xmltag, L"playlist"))
  65. {
  66. const wchar_t *value = params->getItemValue(L"id");
  67. const wchar_t *name = params->getItemValue(L"name");
  68. if (value && name)
  69. {
  70. wifi_playlist = new WifiPlaylist;
  71. wifi_playlist->id = _wcsdup(value);
  72. wifi_playlist->name = _wcsdup(name);
  73. }
  74. }
  75. }
  76. void WifiXML::xmlReaderOnEndElementCallback(const wchar_t *xmlpath, const wchar_t *xmltag)
  77. {
  78. if (!wcscmp(xmltag, L"item") && wifi_track)
  79. {
  80. wifi_track->artist = _wcsdup(artist.GetString()); artist.Reset();
  81. wifi_track->album = _wcsdup(album.GetString()); album.Reset();
  82. wifi_track->composer = _wcsdup(composer.GetString()); composer.Reset();
  83. wifi_track->duration = _wtoi(duration.GetString()); duration.Reset();
  84. wifi_track->track = _wtoi(track.GetString()); track.Reset();
  85. wifi_track->year = _wtoi(year.GetString()); year.Reset();
  86. wifi_track->size = _wtoi(size.GetString()); size.Reset();
  87. wifi_track->title = _wcsdup(title.GetString()); title.Reset();
  88. wifi_track->mime_type = _wcsdup(mime_type.GetString()); mime_type.Reset();
  89. wifi_track->last_updated = _wtoi64(modified.GetString()); modified.Reset();
  90. if (wifi_playlist)
  91. wifi_playlist->tracks.push_back(wifi_track);
  92. else
  93. tracks.push_back(wifi_track);
  94. wifi_track=0;
  95. }
  96. else if (!wcscmp(xmltag, L"playlist") && wifi_playlist)
  97. {
  98. playlists.push_back(wifi_playlist);
  99. wifi_playlist = 0;
  100. }
  101. }
  102. /* ------------------------------------------------------------------------------------------------------------ */
  103. SongListDownloader::SongListDownloader(const char *root_url, WifiDevice *wifi_device) : root_url(root_url), wifi_device(wifi_device)
  104. {
  105. device=0;
  106. wifi_device->AddRef();
  107. waServiceFactory *parserFactory = plugin.service->service_getServiceByGuid(obj_xmlGUID);
  108. if (parserFactory)
  109. parser = (obj_xml *)parserFactory->getInterface();
  110. parser->xmlreader_setCaseSensitive();
  111. wifi = new WifiXML(parser);
  112. parser->xmlreader_open();
  113. }
  114. SongListDownloader::~SongListDownloader()
  115. {
  116. waServiceFactory *parserFactory = plugin.service->service_getServiceByGuid(obj_xmlGUID);
  117. if (parserFactory)
  118. {
  119. delete wifi;
  120. parser->xmlreader_close();
  121. parserFactory->releaseInterface(parser);
  122. }
  123. }
  124. void SongListDownloader::OnInit(DownloadToken token)
  125. {
  126. api_httpreceiver *jnet = WAC_API_DOWNLOADMANAGER->GetReceiver(token);
  127. if (jnet)
  128. {
  129. jnet->AddHeaderValue("X-Winamp-ID", winamp_id_str);
  130. jnet->AddHeaderValue("X-Winamp-Name", winamp_name);
  131. }
  132. }
  133. void SongListDownloader::OnData(DownloadToken token, void *data, size_t datalen)
  134. {
  135. if (parser->xmlreader_feed(data, datalen) != OBJ_XML_SUCCESS)
  136. {
  137. WAC_API_DOWNLOADMANAGER->CancelDownload(token);
  138. }
  139. }
  140. void SongListDownloader::OnCancel(DownloadToken token)
  141. {
  142. wifi_device->OnConnectionFailed();
  143. wifi_device->Release();
  144. this->Release();
  145. }
  146. void SongListDownloader::OnError(DownloadToken token, int error)
  147. {
  148. wifi_device->OnConnectionFailed();
  149. wifi_device->Release();
  150. this->Release();
  151. }
  152. void SongListDownloader::OnFinish(DownloadToken token)
  153. {
  154. parser->xmlreader_feed(0, 0);
  155. device = new TemplateDevice(wifi_device, root_url, &wifi->info_xml.device_info, &wifi->tracks, &wifi->playlists);
  156. wifi_device->OnConnected(device);
  157. wifi_device->Release();
  158. PostMessage(plugin.hwndPortablesParent,WM_PMP_IPC,(intptr_t)device,PMP_IPC_DEVICECONNECTED);
  159. this->Release();
  160. }