sprivate.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include <precomp.h>
  2. #include "sprivate.h"
  3. #include "main.h"
  4. #include <api/application/api_application.h>
  5. #include "wa2frontend.h"
  6. #include <api.h>
  7. #include "../Agave/Language/api_language.h"
  8. #include "../../../../Components/wac_network/wac_network_http_receiver_api.h"
  9. #include "../nu/AutoWide.h"
  10. #include "../nu/AutoChar.h"
  11. #include "../nu/ns_wc.h"
  12. #include "../Winamp/buildtype.h"
  13. #include "../nu/refcount.h"
  14. #include <shlobj.h>
  15. #include <shlwapi.h>
  16. // {78BD6ED9-0DBC-4fa5-B5CD-5977E3A912F8}
  17. static const GUID SPrivate_script_object_guid =
  18. { 0x78bd6ed9, 0xdbc, 0x4fa5, { 0xb5, 0xcd, 0x59, 0x77, 0xe3, 0xa9, 0x12, 0xf8 } };
  19. static SPrivateScriptObjectController _SPrivateController;
  20. ScriptObjectController *SPrivateController=&_SPrivateController;
  21. // -- Functions table -------------------------------------
  22. function_descriptor_struct SPrivateScriptObjectController::exportedFunction[] =
  23. {
  24. {L"updateLinks", 2, (void*)SPrivate::vcpu_updateLinks },
  25. {L"onLinksUpdated", 1, (void*)SPrivate::vcpu_onLinksUpdated },
  26. };
  27. // --------------------------------------------------------
  28. const wchar_t *SPrivateScriptObjectController::getClassName()
  29. {
  30. return L"Private";
  31. }
  32. const wchar_t *SPrivateScriptObjectController::getAncestorClassName()
  33. {
  34. return L"Object";
  35. }
  36. ScriptObjectController *SPrivateScriptObjectController::getAncestorController()
  37. {
  38. return NULL;
  39. }
  40. ScriptObject *SPrivateScriptObjectController::instantiate()
  41. {
  42. SPrivate *c = new SPrivate;
  43. if (!c) return NULL;
  44. return c->getScriptObject();
  45. }
  46. int SPrivateScriptObjectController::getInstantiable()
  47. {
  48. return 1;
  49. }
  50. void SPrivateScriptObjectController::destroy(ScriptObject *o)
  51. {
  52. SPrivate *obj = static_cast<SPrivate *>(o->vcpu_getInterface(SPrivate_script_object_guid));
  53. ASSERT(obj != NULL);
  54. obj->dlcb = false;
  55. delete obj;
  56. }
  57. void *SPrivateScriptObjectController::encapsulate(ScriptObject *o)
  58. {
  59. return NULL;
  60. }
  61. void SPrivateScriptObjectController::deencapsulate(void *o)
  62. {
  63. }
  64. int SPrivateScriptObjectController::getNumFunctions()
  65. {
  66. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  67. }
  68. const function_descriptor_struct *SPrivateScriptObjectController::getExportedFunctions()
  69. {
  70. return exportedFunction;
  71. }
  72. GUID SPrivateScriptObjectController::getClassGuid()
  73. {
  74. return SPrivate_script_object_guid;
  75. }
  76. /*-----------------------------------*/
  77. static void DoAPC(PAPCFUNC apc, ULONG_PTR data)
  78. {
  79. HANDLE hMainThread = WASABI_API_APP->main_getMainThreadHandle();
  80. if (hMainThread)
  81. {
  82. QueueUserAPC(apc, hMainThread, data);
  83. CloseHandle(hMainThread);
  84. }
  85. }
  86. static void CALLBACK LinksUpdatedAPC(ULONG_PTR data)
  87. {
  88. ScriptObject* scriptCallback = (ScriptObject *)data;
  89. SPrivate::vcpu_onLinksUpdated(SCRIPT_CALL, scriptCallback);
  90. }
  91. class PDownloadCallback : public Countable<ifc_downloadManagerCallback>
  92. {
  93. public:
  94. PDownloadCallback (const char *url, ScriptObject* scriptCallback)
  95. {
  96. this->scriptCallback = scriptCallback;
  97. validSO = true;
  98. }
  99. ~PDownloadCallback ()
  100. {
  101. }
  102. void OnFinish (DownloadToken token)
  103. {
  104. if (!validSO)
  105. {
  106. DeleteFileW(WAC_API_DOWNLOADMANAGER->GetLocation(token));
  107. delete this;
  108. return;
  109. }
  110. api_httpreceiver *http = WAC_API_DOWNLOADMANAGER->GetReceiver(token);
  111. if (http)
  112. {
  113. int replyCode;
  114. replyCode = http->getreplycode();
  115. if (204 != replyCode)
  116. {
  117. BOOL succeeded;
  118. HANDLE hDest;
  119. const wchar_t *downloadDest;
  120. wchar_t finalFileName[MAX_PATH] = {0};
  121. downloadDest = WAC_API_DOWNLOADMANAGER->GetLocation(token);
  122. succeeded = FALSE;
  123. hDest = CreateFileW(downloadDest, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
  124. NULL, OPEN_EXISTING, 0, NULL);
  125. if (INVALID_HANDLE_VALUE != hDest)
  126. {
  127. LARGE_INTEGER fileSize;
  128. if (FALSE != GetFileSizeEx(hDest, &fileSize) &&
  129. 0 != fileSize.QuadPart)
  130. {
  131. succeeded = TRUE;
  132. }
  133. CloseHandle(hDest);
  134. if (FALSE == succeeded)
  135. {
  136. DeleteFileW(downloadDest);
  137. downloadDest = NULL;
  138. }
  139. }
  140. if (FALSE != succeeded &&
  141. NULL != (PathCombineW(finalFileName, WASABI_API_APP->path_getUserSettingsPath(), L"links.xml")))
  142. {
  143. // then move the file there
  144. succeeded = MoveFileW(downloadDest, finalFileName);
  145. if (FALSE == succeeded)
  146. {
  147. succeeded = CopyFileW(downloadDest, finalFileName, FALSE);
  148. DeleteFileW(downloadDest);
  149. }
  150. if (FALSE != succeeded)
  151. {
  152. // hop back on the main thread for the callback
  153. DoAPC(LinksUpdatedAPC, (ULONG_PTR)scriptCallback);
  154. }
  155. }
  156. }
  157. }
  158. Release();
  159. }
  160. void OnError (DownloadToken token, int error)
  161. {
  162. Release();
  163. }
  164. void OnCancel (DownloadToken token)
  165. {
  166. Release();
  167. }
  168. void OnTick (DownloadToken token) {}
  169. ScriptObject* scriptCallback;
  170. bool validSO;
  171. REFERENCE_COUNT_IMPLEMENTATION;
  172. protected:
  173. RECVS_DISPATCH;
  174. };
  175. SPrivate::SPrivate()
  176. {
  177. getScriptObject()->vcpu_setInterface(SPrivate_script_object_guid, static_cast<SPrivate *>(this));
  178. getScriptObject()->vcpu_setClassName(L"Private");
  179. getScriptObject()->vcpu_setController(SPrivateController);
  180. dlcb = false;
  181. }
  182. SPrivate::~SPrivate()
  183. {
  184. }
  185. scriptVar SPrivate::vcpu_updateLinks(SCRIPT_FUNCTION_PARAMS, ScriptObject *object, scriptVar version, scriptVar bversion)
  186. {
  187. SCRIPT_FUNCTION_INIT;
  188. String url;
  189. //if defined(BETA) || defined(INTERNAL)
  190. const wchar_t *langIdentifier = WASABI_API_LNG?(WASABI_API_LNG->GetLanguageIdentifier(LANG_IDENT_STR)):0;
  191. if (!langIdentifier)
  192. langIdentifier = L"en-US";
  193. url = StringPrintf("http://client.winamp.com/data/skins?o=links&sid=bento&version=%s&waversion=%s&build=%i&browserversion=%s&lang=%s", AutoChar(version.data.sdata), WASABI_API_APP->main_getVersionNumString(), WASABI_API_APP->main_getBuildNumber(),AutoChar(bversion.data.sdata), AutoChar(langIdentifier));
  194. //url = StringPrintf("http://martin.skinconsortium.com/links.php?o=links&sid=bento&version=%s&waversion=%s&build=%i&browserversion=%s", AutoChar(version.data.sdata), WASABI_API_APP->main_getVersionNumString(), WASABI_API_APP->main_getBuildNumber(),AutoChar(bversion.data.sdata));
  195. SPrivate *sp = static_cast<SPrivate *>(object->vcpu_getInterface(SPrivate_script_object_guid));
  196. sp->dlcb = new PDownloadCallback(url, object);
  197. WAC_API_DOWNLOADMANAGER->Download(url, sp->dlcb);
  198. RETURN_SCRIPT_VOID;
  199. }
  200. scriptVar SPrivate::vcpu_onLinksUpdated(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  201. {
  202. SCRIPT_FUNCTION_INIT;
  203. PROCESS_HOOKS0(o, SPrivateController);
  204. SCRIPT_FUNCTION_CHECKABORTEVENT(o);
  205. SCRIPT_EXEC_EVENT0(o);
  206. }
  207. #define CBCLASS PDownloadCallback
  208. START_DISPATCH;
  209. REFERENCE_COUNTED;
  210. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONFINISH, OnFinish)
  211. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONTICK, OnTick)
  212. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONERROR, OnError)
  213. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONCANCEL, OnCancel)
  214. END_DISPATCH;
  215. #undef CBCLASS