InfoDownloader.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "InfoDownloader.h"
  2. #include "api.h"
  3. #include "main.h"
  4. #include "images.h"
  5. #include "InfoDownloader.h"
  6. #include "../../..\Components\wac_network\wac_network_http_receiver_api.h"
  7. #include <api/service/waServiceFactory.h>
  8. #include <strsafe.h>
  9. void OnInfoDownloadDone(InfoDownloader *_info);
  10. void DeviceInfo_Init(DeviceInfo *device_info)
  11. {
  12. device_info->id=0;
  13. device_info->modelInfo=NULL;
  14. device_info->total_space=0;
  15. device_info->used_space=0;
  16. device_info->model[0]=0;
  17. device_info->manufacturer[0]=0;
  18. device_info->name[0]=0;
  19. device_info->product[0]=0;
  20. }
  21. void DeviceInfo_Copy(DeviceInfo *dest, const DeviceInfo *source)
  22. {
  23. dest->id=source->id;
  24. dest->modelInfo=source->modelInfo;
  25. dest->total_space=source->total_space;
  26. dest->used_space=source->used_space;
  27. StringCbCopy(dest->model, sizeof(dest->model), source->model);
  28. StringCbCopy(dest->manufacturer, sizeof(dest->manufacturer), source->manufacturer);
  29. StringCbCopy(dest->name, sizeof(dest->name), source->name);
  30. StringCbCopy(dest->product, sizeof(dest->product), source->product);
  31. }
  32. DeviceXML::DeviceXML()
  33. {
  34. DeviceInfo_Init(&device_info);
  35. }
  36. InfoXML::InfoXML(obj_xml *parser) : parser(parser)
  37. {
  38. parser->xmlreader_registerCallback(L"info\fdevice", this);
  39. parser->xmlreader_registerCallback(L"info\fspace", this);
  40. }
  41. InfoXML::~InfoXML()
  42. {
  43. parser->xmlreader_unregisterCallback(this);
  44. }
  45. void DeviceXML::xmlReaderOnStartElementCallback(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params)
  46. {
  47. if (!wcscmp(xmltag, L"device"))
  48. {
  49. const wchar_t *value = params->getItemValue(L"id");
  50. if (value)
  51. device_info.id = _wcstoui64(value, 0, 16);
  52. value = params->getItemValue(L"manufacturer");
  53. if (value)
  54. StringCbCopyW(device_info.manufacturer, sizeof(device_info.manufacturer), value);
  55. value = params->getItemValue(L"model");
  56. if (value)
  57. StringCbCopyW(device_info.model, sizeof(device_info.model), value);
  58. value = params->getItemValue(L"name");
  59. if (value)
  60. StringCbCopyW(device_info.name, sizeof(device_info.name), value);
  61. value = params->getItemValue(L"product");
  62. if (value)
  63. StringCbCopyW(device_info.product, sizeof(device_info.product), value);
  64. device_info.modelInfo = FindModelInfo(device_info.manufacturer, device_info.model, FALSE);
  65. }
  66. else if (!wcscmp(xmltag, L"space"))
  67. {
  68. const wchar_t *value = params->getItemValue(L"total");
  69. if (value)
  70. {
  71. device_info.total_space = _wtoi64(value);
  72. }
  73. value = params->getItemValue(L"used");
  74. if (value)
  75. {
  76. device_info.used_space = _wtoi64(value);
  77. }
  78. }
  79. }
  80. WifiDevice *InfoXML::CreateDevice(uint64_t device_id_check, const char *root_url)
  81. {
  82. if (device_info.id == device_id_check)
  83. {
  84. return new WifiDevice(root_url, &device_info);
  85. }
  86. return 0;
  87. }
  88. /* ------------------------------------------------------------------------------------------------------------ */
  89. InfoDownloader::InfoDownloader(const char *_root_url, uint64_t id, Wasabi2::nx_string_t usn) : id(id)
  90. {
  91. this->usn = NXStringRetain(usn);
  92. root_url = strdup(_root_url);
  93. done=0;
  94. waServiceFactory *parserFactory = plugin.service->service_getServiceByGuid(obj_xmlGUID);
  95. if (parserFactory)
  96. parser = (obj_xml *)parserFactory->getInterface();
  97. parser->xmlreader_setCaseSensitive();
  98. info = new InfoXML(parser);
  99. parser->xmlreader_open();
  100. }
  101. InfoDownloader::~InfoDownloader()
  102. {
  103. NXStringRelease(usn);
  104. waServiceFactory *parserFactory = plugin.service->service_getServiceByGuid(obj_xmlGUID);
  105. if (parserFactory)
  106. {
  107. delete info;
  108. parser->xmlreader_close();
  109. parserFactory->releaseInterface(parser);
  110. }
  111. free(root_url);
  112. }
  113. void InfoDownloader::OnInit(DownloadToken token)
  114. {
  115. if (done)
  116. {
  117. WAC_API_DOWNLOADMANAGER->CancelDownload(token);
  118. return;
  119. }
  120. api_httpreceiver *jnet = WAC_API_DOWNLOADMANAGER->GetReceiver(token);
  121. if (jnet)
  122. {
  123. jnet->AddHeaderValue("X-Winamp-ID", winamp_id_str);
  124. jnet->AddHeaderValue("X-Winamp-Name", winamp_name);
  125. }
  126. }
  127. void InfoDownloader::OnData(DownloadToken token, void *data, size_t datalen)
  128. {
  129. if (done)
  130. {
  131. WAC_API_DOWNLOADMANAGER->CancelDownload(token);
  132. return;
  133. }
  134. if (parser->xmlreader_feed(data, datalen) != OBJ_XML_SUCCESS)
  135. {
  136. WAC_API_DOWNLOADMANAGER->CancelDownload(token);
  137. }
  138. }
  139. void InfoDownloader::OnCancel(DownloadToken token)
  140. {
  141. done=2;
  142. OnInfoDownloadDone(this);
  143. }
  144. void InfoDownloader::OnError(DownloadToken token, int error)
  145. {
  146. // TODO
  147. done=2;
  148. OnInfoDownloadDone(this);
  149. }
  150. void InfoDownloader::OnFinish(DownloadToken token)
  151. {
  152. if (!done)
  153. {
  154. parser->xmlreader_feed(0, 0);
  155. }
  156. done=1;
  157. OnInfoDownloadDone(this);
  158. }
  159. bool InfoDownloader::Done(WifiDevice **out_device)
  160. {
  161. if (done != 1)
  162. return false;
  163. *out_device = info->CreateDevice(id, root_url);
  164. return true;
  165. }
  166. void InfoDownloader::Cancel()
  167. {
  168. done=2;
  169. }