1
0

SkinCOM.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /** (c) Nullsoft, Inc. C O N F I D E N T I A L
  2. ** Filename:
  3. ** Project:
  4. ** Description:
  5. ** Author: Ben Allison [email protected]
  6. ** Created:
  7. **/
  8. #include "main.h"
  9. #include "../nu/ns_wc.h"
  10. #include "SkinCOM.h"
  11. #include "resource.h"
  12. #include "../nu/AutoWide.h"
  13. #include "../nu/AutoChar.h"
  14. #include "wa_dlg.h"
  15. #include "Browser.h"
  16. void WriteEscaped(FILE *fp, const char *str);
  17. void WriteSkinsAsXML(const wchar_t *filename, int limit)
  18. {
  19. FILE *fp = _wfopen(filename, L"wb");
  20. fputs("<?xml version=\"1.0\" encoding=\"utf-8\"?>", fp);
  21. fputs("<skins>\n", fp);
  22. fputs("<skin filename=\"", fp);
  23. WriteEscaped(fp,"Winamp Classic");
  24. fputs("\"/>\n", fp);
  25. HANDLE h;
  26. WIN32_FIND_DATAW d;
  27. wchar_t dirmask[MAX_PATH] = {0};
  28. PathCombineW(dirmask, SKINDIR, L"*");
  29. h = FindFirstFileW(dirmask, &d);
  30. if (h != INVALID_HANDLE_VALUE )
  31. {
  32. int count=1;
  33. do
  34. {
  35. if ( limit && count >= limit ) break;
  36. if (d.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  37. {
  38. if (wcscmp(d.cFileName, L".") && wcscmp(d.cFileName, L".."))
  39. {
  40. fputs("<skin filename=\"", fp);
  41. WriteEscaped(fp, AutoChar(d.cFileName, CP_UTF8));
  42. fputs("\"/>\n", fp);
  43. count++;
  44. }
  45. }
  46. else if (!_wcsicmp(extensionW(d.cFileName), L"zip") || !_wcsicmp(extensionW(d.cFileName), L"wsz") || !_wcsicmp(extensionW(d.cFileName), L"wal"))
  47. {
  48. fputs("<skin filename=\"", fp);
  49. WriteEscaped(fp, AutoChar(d.cFileName, CP_UTF8));
  50. fputs("\"/>\n", fp);
  51. count++;
  52. }
  53. }
  54. while (FindNextFileW(h, &d));
  55. FindClose(h);
  56. }
  57. fputs("</skins>", fp);
  58. fclose(fp);
  59. }
  60. enum
  61. {
  62. DISP_CURRENTSKIN_GETCOLOR = 777,
  63. DISP_CURRENTSKIN_GETNAME,
  64. DISP_CURRENTSKIN_REGISTERSKINCHANGECALLBACK,
  65. DISP_CURRENTSKIN_UNREGISTERSKINCHANGECALLBACK,
  66. DISP_CURRENTSKIN_GETFONTNAME,
  67. DISP_CURRENTSKIN_GETFONTSIZE,
  68. DISP_CURRENTSKIN_GETPLAYLISTCOLOR,
  69. DISP_CURRENTSKIN_SETSKIN,
  70. DISP_CURRENTSKIN_GETXMLSKINLIST,
  71. };
  72. #define CHECK_ID(str, id)\
  73. if (CSTR_EQUAL == CompareStringW(lcid, NORM_IGNORECASE, rgszNames[i], -1, L##str, -1))\
  74. { rgdispid[i] = id; continue; }
  75. HRESULT SkinCOM::GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgdispid)
  76. {
  77. UNREFERENCED_PARAMETER(riid);
  78. bool unknowns = false;
  79. for (unsigned int i = 0;i != cNames;i++)
  80. {
  81. CHECK_ID("GetColor", DISP_CURRENTSKIN_GETCOLOR)
  82. CHECK_ID("GetName", DISP_CURRENTSKIN_GETNAME)
  83. CHECK_ID("RegisterSkinChangeCallback", DISP_CURRENTSKIN_REGISTERSKINCHANGECALLBACK)
  84. CHECK_ID("UnregisterSkinChangeCallback", DISP_CURRENTSKIN_UNREGISTERSKINCHANGECALLBACK)
  85. CHECK_ID("GetFontName", DISP_CURRENTSKIN_GETFONTNAME)
  86. CHECK_ID("GetFontSize", DISP_CURRENTSKIN_GETFONTSIZE);
  87. CHECK_ID("GetPlaylistColor", DISP_CURRENTSKIN_GETPLAYLISTCOLOR);
  88. CHECK_ID("SetSkin", DISP_CURRENTSKIN_SETSKIN);
  89. CHECK_ID("GetXMLSkinList", DISP_CURRENTSKIN_GETXMLSKINLIST);
  90. rgdispid[i] = DISPID_UNKNOWN;
  91. unknowns = true;
  92. }
  93. if (unknowns)
  94. return DISP_E_UNKNOWNNAME;
  95. else
  96. return S_OK;
  97. }
  98. HRESULT SkinCOM::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo)
  99. {
  100. UNREFERENCED_PARAMETER(itinfo);
  101. UNREFERENCED_PARAMETER(lcid);
  102. UNREFERENCED_PARAMETER(pptinfo);
  103. return E_NOTIMPL;
  104. }
  105. HRESULT SkinCOM::GetTypeInfoCount(unsigned int FAR * pctinfo)
  106. {
  107. UNREFERENCED_PARAMETER(pctinfo);
  108. return E_NOTIMPL;
  109. }
  110. HRESULT SkinCOM::Invoke(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, EXCEPINFO FAR * pexecinfo, unsigned int FAR *puArgErr)
  111. {
  112. UNREFERENCED_PARAMETER(riid);
  113. UNREFERENCED_PARAMETER(lcid);
  114. UNREFERENCED_PARAMETER(pexecinfo);
  115. UNREFERENCED_PARAMETER(wFlags);
  116. switch (dispid)
  117. {
  118. case DISP_CURRENTSKIN_SETSKIN:
  119. if (pdispparams->cArgs == 1)
  120. {
  121. const wchar_t *newSkin = pdispparams->rgvarg[0].bstrVal;
  122. if (newSkin && *newSkin)
  123. {
  124. if (_wcsicmp(config_skin, newSkin))
  125. {
  126. StringCchCopyW(config_skin, MAX_PATH, newSkin);
  127. PostMessageW(hMainWindow, WM_COMMAND, WINAMP_REFRESHSKIN, 0);
  128. }
  129. }
  130. return S_OK;
  131. }
  132. else
  133. return DISP_E_BADPARAMCOUNT;
  134. case DISP_CURRENTSKIN_REGISTERSKINCHANGECALLBACK:
  135. return callbacks.RegisterFromDispParam(pdispparams, 0, puArgErr);
  136. case DISP_CURRENTSKIN_UNREGISTERSKINCHANGECALLBACK:
  137. return callbacks.UnregisterFromDispParam(pdispparams, 0, puArgErr);
  138. case DISP_CURRENTSKIN_GETCOLOR:
  139. if (pdispparams->cArgs == 1)
  140. {
  141. COLORREF color = WADlg_getColor(pdispparams->rgvarg[0].lVal);
  142. color = ((color >> 16) & 0xff | (color & 0xff00) | ((color << 16) & 0xff0000));
  143. char colorString[8] = {0};
  144. StringCchPrintfA(colorString, 8, "#%06X", color);
  145. AutoWide answer(colorString);
  146. BSTR tag = SysAllocString(answer);
  147. VariantInit(pvarResult);
  148. V_VT(pvarResult) = VT_BSTR;
  149. V_BSTR(pvarResult) = tag;
  150. return S_OK;
  151. }
  152. else
  153. return DISP_E_BADPARAMCOUNT;
  154. case DISP_CURRENTSKIN_GETNAME:
  155. {
  156. BSTR tag = SysAllocString(config_skin);
  157. VariantInit(pvarResult);
  158. V_VT(pvarResult) = VT_BSTR;
  159. V_BSTR(pvarResult) = tag;
  160. return S_OK;
  161. }
  162. break;
  163. case DISP_CURRENTSKIN_GETFONTNAME:
  164. {
  165. BSTR tag = SysAllocString(GetFontNameW());
  166. VariantInit(pvarResult);
  167. V_VT(pvarResult) = VT_BSTR;
  168. V_BSTR(pvarResult) = tag;
  169. return S_OK;
  170. }
  171. break;
  172. case DISP_CURRENTSKIN_GETFONTSIZE:
  173. {
  174. VariantInit(pvarResult);
  175. V_VT(pvarResult) = VT_I4;
  176. V_I4(pvarResult) = GetFontSize();
  177. return S_OK;
  178. }
  179. case DISP_CURRENTSKIN_GETPLAYLISTCOLOR:
  180. if (pdispparams->cArgs == 1)
  181. {
  182. COLORREF color = Skin_PLColors[pdispparams->rgvarg[0].lVal];
  183. color = ((color >> 16) & 0xff | (color & 0xff00) | ((color << 16) & 0xff0000));
  184. char colorString[8] = {0};
  185. StringCchPrintfA(colorString, 8, "#%06X", color);
  186. AutoWide answer(colorString);
  187. BSTR tag = SysAllocString(answer);
  188. VariantInit(pvarResult);
  189. V_VT(pvarResult) = VT_BSTR;
  190. V_BSTR(pvarResult) = tag;
  191. return S_OK;
  192. }
  193. else
  194. return DISP_E_BADPARAMCOUNT;
  195. case DISP_CURRENTSKIN_GETXMLSKINLIST:
  196. {
  197. int max=0;
  198. if (pdispparams->cArgs == 1)
  199. {
  200. max = _wtoi(pdispparams->rgvarg[0].bstrVal);
  201. }
  202. wchar_t tempPath[MAX_PATH] = {0};
  203. GetTempPathW(MAX_PATH, tempPath);
  204. wchar_t tempFile[MAX_PATH] = {0};
  205. GetTempFileNameW(tempPath, L"slx", 0, tempFile);
  206. WriteSkinsAsXML(tempFile, max);
  207. HANDLE plFile = CreateFileW(tempFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
  208. size_t flen = SetFilePointer(plFile, 0, NULL, FILE_END);
  209. SetFilePointer(plFile, 0, NULL, FILE_BEGIN);
  210. SAFEARRAY *bufferArray=SafeArrayCreateVector(VT_UI1, 0, (ULONG)flen);
  211. void *data;
  212. SafeArrayAccessData(bufferArray, &data);
  213. DWORD bytesRead = 0;
  214. ReadFile(plFile, data, (DWORD)flen, &bytesRead, 0);
  215. SafeArrayUnaccessData(bufferArray);
  216. CloseHandle(plFile);
  217. VariantInit(pvarResult);
  218. V_VT(pvarResult) = VT_ARRAY|VT_UI1;
  219. V_ARRAY(pvarResult) = bufferArray;
  220. DeleteFileW(tempFile);
  221. }
  222. return S_OK;
  223. }
  224. return DISP_E_MEMBERNOTFOUND;
  225. }
  226. STDMETHODIMP SkinCOM::QueryInterface(REFIID riid, PVOID *ppvObject)
  227. {
  228. if (!ppvObject)
  229. return E_POINTER;
  230. else if (IsEqualIID(riid, IID_IDispatch))
  231. *ppvObject = (IDispatch *)this;
  232. else if (IsEqualIID(riid, IID_IUnknown))
  233. *ppvObject = this;
  234. else
  235. {
  236. *ppvObject = NULL;
  237. return E_NOINTERFACE;
  238. }
  239. AddRef();
  240. return S_OK;
  241. }
  242. ULONG SkinCOM::AddRef(void)
  243. {
  244. return 0;
  245. }
  246. ULONG SkinCOM::Release(void)
  247. {
  248. return 0;
  249. }
  250. static void SkinChangedNotifyCb(IDispatch *dispatch, void *param)
  251. {
  252. UNREFERENCED_PARAMETER(param);
  253. DISPPARAMS params;
  254. params.cArgs = 0;
  255. params.cNamedArgs = 0;
  256. params.rgdispidNamedArgs = 0;
  257. params.rgvarg = 0;
  258. unsigned int ret;
  259. if (!(config_no_visseh&8))
  260. {
  261. try
  262. {
  263. dispatch->Invoke(0, GUID_NULL, 0, DISPATCH_METHOD, &params, 0, 0, &ret);
  264. }
  265. catch(...)
  266. {
  267. }
  268. }
  269. else
  270. dispatch->Invoke(0, GUID_NULL, 0, DISPATCH_METHOD, &params, 0, 0, &ret);
  271. }
  272. void SkinCOM::SkinChanged()
  273. {
  274. callbacks.Notify(SkinChangedNotifyCb, NULL, NULL);
  275. }