webinfo_obj.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #include "main.h"
  2. #include "./webinfo_obj.h"
  3. #include "../winamp/buildtype.h"
  4. #include "./resource.h"
  5. #include "api__gen_ml.h"
  6. #include "../Winamp/buildtype.h"
  7. #include <exdisp.h>
  8. #include <mshtmdid.h>
  9. #include <mshtml.h>
  10. #include <strsafe.h>
  11. #define WEBINFO_URL L"http://client.winamp.com/nowplaying/mini"
  12. #define WEBINFO_FUNCTION L"SongInfoCallback"
  13. #define WEBINFO_USERAGENT L"Winamp File Info"
  14. #define WEBINFO_DOWNLOADFLAGS ( DLCTL_DLIMAGES | \
  15. /*DLCTL_NO_SCRIPTS |*/ \
  16. /*DLCTL_NO_JAVA | */ \
  17. DLCTL_NO_DLACTIVEXCTLS | \
  18. /*DLCTL_NO_RUNACTIVEXCTLS |*/ \
  19. /*DLCTL_RESYNCHRONIZE |*/ \
  20. DLCTL_NO_BEHAVIORS | \
  21. 0)
  22. #define WEBINFO_HOSTINFODFLAGS ( DOCHOSTUIFLAG_DIALOG | \
  23. DOCHOSTUIFLAG_DISABLE_HELP_MENU | \
  24. DOCHOSTUIFLAG_NO3DBORDER | \
  25. DOCHOSTUIFLAG_SCROLL_NO | \
  26. DOCHOSTUIFLAG_DISABLE_SCRIPT_INACTIVE | \
  27. DOCHOSTUIFLAG_ACTIVATE_CLIENTHIT_ONLY | \
  28. DOCHOSTUIFLAG_ENABLE_INPLACE_NAVIGATION | \
  29. DOCHOSTUIFLAG_THEME | \
  30. DOCHOSTUIFLAG_NOPICS | \
  31. DOCHOSTUIFLAG_NO3DOUTERBORDER | \
  32. DOCHOSTUIFLAG_DISABLE_UNTRUSTEDPROTOCOL | \
  33. DOCHOSTUIFLAG_ENABLE_REDIRECT_NOTIFICATION | \
  34. DOCHOSTUIFLAG_USE_WINDOWLESS_SELECTCONTROL | \
  35. 0)
  36. #define WEBINFO_CONTAINERSTYLE (CSTYLE_NAVIGATE2_NOCLICKSOUND | CSTYLE_NOCLICKSOUND)
  37. static const wchar_t pszCSSTemplate[] = L"BODY { "
  38. L"background-color: #%06X;"
  39. L"color: #%06X;"
  40. L"scrollbar-face-color: #%06X;"
  41. L"scrollbar-track-color: #%06X;"
  42. L"scrollbar-3dlight-color: #%06X;"
  43. L"scrollbar-shadow-color: #%06X;"
  44. L"scrollbar-darkshadow-color: #%06X;"
  45. L"scrollbar-highlight-color: #%06X;"
  46. L"scrollbar-arrow-color: #%06X"
  47. L" }";
  48. static const wchar_t pszHTMLTemplate[] = L"<HTML><HEAD></HEAD><BODY>"
  49. L"<table height=\"100%%\" width=\"100%%\">"
  50. L"<tr><td align=center valign=middle height=\"96%%\" width=\"96%%\">"
  51. L"<font size=-1 face=\"Arial\">%s</font>"
  52. L"</td></tr></table>"
  53. L"</BODY></HTML>";
  54. WebFileInfo *CreateWebFileInfo(HWND hwndParent, IDispatch *pDispWA)
  55. {
  56. return new WebFileInfo(hwndParent, pDispWA);
  57. }
  58. static COLORREF GetHTMLColor(int nColorIndex);
  59. WebFileInfo::WebFileInfo(HWND hwndParent, IDispatch *pDispWA) :
  60. HTMLContainer2(plugin.hwndParent, hwndParent), nHomePage(HOMEPAGE_NOTLOADED),
  61. bstrMessage(NULL), bstrFileName(NULL), nDragMode(DROPEFFECT_NONE)
  62. {
  63. this->pDispWA = pDispWA;
  64. if (NULL != pDispWA)
  65. pDispWA->AddRef();
  66. }
  67. WebFileInfo::~WebFileInfo(void)
  68. {
  69. if (bstrMessage)
  70. {
  71. SysFreeString(bstrMessage);
  72. bstrMessage = NULL;
  73. }
  74. if (bstrFileName)
  75. {
  76. SysFreeString(bstrFileName);
  77. bstrFileName = NULL;
  78. }
  79. if (NULL != pDispWA)
  80. pDispWA->Release();
  81. }
  82. HRESULT WebFileInfo::QueryInterface(REFIID riid, PVOID *ppvObject)
  83. {
  84. if (IsEqualIID(riid, IID_IDropTarget))
  85. {
  86. *ppvObject = (IDropTarget*)this;
  87. return S_OK;
  88. }
  89. return HTMLContainer2::QueryInterface(riid, ppvObject);
  90. }
  91. ULONG WebFileInfo::AddRef(void)
  92. {
  93. return HTMLContainer2::AddRef();
  94. }
  95. ULONG WebFileInfo::Release(void)
  96. {
  97. return HTMLContainer2::Release();
  98. }
  99. HRESULT WebFileInfo::TranslateAccelerator(LPMSG lpMsg, const GUID __RPC_FAR *pguidCmdGroup, DWORD nCmdID)
  100. {
  101. if ((WM_KEYDOWN == lpMsg->message || WM_KEYUP == lpMsg->message) && lpMsg->wParam >= VK_F1 && lpMsg->wParam <= VK_F24)
  102. {
  103. HWND hHost = GetParentHWND();
  104. if (hHost && IsWindow(hHost)) PostMessageW(hHost, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
  105. return S_OK;
  106. }
  107. return HTMLContainer2::TranslateAccelerator(lpMsg, pguidCmdGroup, nCmdID);
  108. }
  109. HRESULT WebFileInfo::ShowContextMenu(DWORD dwID, POINT __RPC_FAR *ppt, IUnknown __RPC_FAR *pcmdtReserved, IDispatch __RPC_FAR *pdispReserved)
  110. {
  111. #ifdef WINAMP_FINAL_BUILD
  112. return S_OK; // block menu
  113. #else
  114. return E_NOTIMPL;
  115. #endif
  116. }
  117. HRESULT WebFileInfo::ShowMessage(HWND hwnd, LPOLESTR lpstrText, LPOLESTR lpstrCaption, DWORD dwType, LPOLESTR lpstrHelpFile, DWORD dwHelpContext, LRESULT *plResult)
  118. {
  119. wchar_t szBuffer[256] = {0};
  120. lpstrCaption = WASABI_API_LNGSTRINGW_BUF(IDS_WEBINFO_MESSAGEBOX_TITLE, szBuffer,
  121. sizeof(szBuffer)/sizeof(wchar_t));
  122. *plResult = MessageBoxW(hwnd, lpstrText, lpstrCaption, dwType);
  123. return S_OK;
  124. }
  125. COLORREF WebFileInfo::OnGetHostBkColor(void)
  126. {
  127. return WADlg_getColor(WADLG_ITEMBG);
  128. }
  129. DWORD WebFileInfo::OnGetHostInfoFlags(void)
  130. {
  131. return WEBINFO_HOSTINFODFLAGS;
  132. }
  133. OLECHAR *WebFileInfo::OnGetHostCSS(void)
  134. {
  135. LPWSTR pszCSS;
  136. pszCSS = (LPWSTR)CoTaskMemAlloc(sizeof(wchar_t)*4096);
  137. if (pszCSS && S_OK != StringCchPrintfW(pszCSS, 4096, pszCSSTemplate,
  138. GetHTMLColor(WADLG_ITEMBG),
  139. GetHTMLColor(WADLG_ITEMFG),
  140. GetHTMLColor(WADLG_LISTHEADER_BGCOLOR),
  141. GetHTMLColor(WADLG_SCROLLBAR_BGCOLOR),
  142. GetHTMLColor(WADLG_LISTHEADER_FRAME_TOPCOLOR),
  143. GetHTMLColor(WADLG_LISTHEADER_BGCOLOR),
  144. GetHTMLColor(WADLG_LISTHEADER_FRAME_BOTTOMCOLOR),
  145. GetHTMLColor(WADLG_LISTHEADER_BGCOLOR),
  146. GetHTMLColor(WADLG_BUTTONFG)))
  147. {
  148. CoTaskMemFree(pszCSS);
  149. pszCSS = NULL;
  150. }
  151. return pszCSS;
  152. }
  153. DWORD WebFileInfo::OnGetDownlodFlags(void)
  154. {
  155. return WEBINFO_DOWNLOADFLAGS
  156. #ifdef WINAMP_FINAL_BUILD
  157. |DLCTL_SILENT
  158. #endif
  159. ;
  160. }
  161. LPCWSTR WebFileInfo::OnGetUserAgent(void)
  162. {
  163. return WEBINFO_USERAGENT;
  164. }
  165. DWORD WebFileInfo::GetContainerStyle(void)
  166. {
  167. return WEBINFO_CONTAINERSTYLE;
  168. }
  169. HRESULT WebFileInfo::GetExternal(IDispatch __RPC_FAR *__RPC_FAR *ppDispatch)
  170. {
  171. if (NULL == ppDispatch)
  172. return E_POINTER;
  173. if (NULL != pDispWA)
  174. {
  175. *ppDispatch = pDispWA;
  176. pDispWA->AddRef();
  177. return S_OK;
  178. }
  179. return HTMLContainer2::GetExternal(ppDispatch);
  180. }
  181. HRESULT WebFileInfo::GetDropTarget(IDropTarget __RPC_FAR *pDropTarget, IDropTarget __RPC_FAR *__RPC_FAR *ppDropTarget)
  182. {
  183. if (ppDropTarget)
  184. {
  185. *ppDropTarget = (IDropTarget*)this;
  186. AddRef();
  187. return S_OK;
  188. }
  189. return HTMLContainer2::GetDropTarget(pDropTarget, ppDropTarget);
  190. }
  191. void WebFileInfo::OnBeforeNavigate(IDispatch *pDispatch, VARIANT *URL, VARIANT *Flags, VARIANT *TargetFrameName, VARIANT *PostData, VARIANT *Headers, VARIANT_BOOL *Cancel)
  192. {
  193. HTMLContainer2::OnBeforeNavigate(pDispatch, URL, Flags, TargetFrameName, PostData, Headers, Cancel);
  194. if (HOMEPAGE_LOADED == nHomePage) nHomePage = HOMEPAGE_NOTLOADED;
  195. }
  196. void WebFileInfo::OnNewWindow3(IDispatch **ppDisp, VARIANT_BOOL *Cancel, DWORD dwFlags, BSTR bstrUrlContext, BSTR bstrUrl)
  197. {
  198. HTMLContainer2::OnNewWindow3(ppDisp, Cancel, dwFlags, bstrUrlContext, bstrUrl);
  199. if (bstrUrl)
  200. {
  201. HWND hwndHost;
  202. hwndHost = GetHostHWND();
  203. ShellExecuteW(hwndHost, NULL, bstrUrl, NULL, L".", 0); // lets open all annoying popups in default browser
  204. }
  205. *ppDisp = NULL;
  206. *Cancel = VARIANT_TRUE;
  207. }
  208. void WebFileInfo::OnNavigateError(IDispatch *pDispatch, VARIANT *URL, VARIANT *TargetFrameName, VARIANT *StatusCode, VARIANT_BOOL *Cancel)
  209. {
  210. HTMLContainer2::OnNavigateError(pDispatch, URL, TargetFrameName, StatusCode, Cancel);
  211. *Cancel = VARIANT_TRUE;
  212. if (bstrFileName)
  213. {
  214. SysFreeString(bstrFileName);
  215. bstrFileName = NULL;
  216. }
  217. IWebBrowser2 *pWeb2;
  218. if (pDispatch && SUCCEEDED(pDispatch->QueryInterface(IID_IWebBrowser2, (void**)&pWeb2)))
  219. {
  220. pWeb2->Stop();
  221. pWeb2->Release();
  222. }
  223. wchar_t szErrorString[128] = {0};
  224. WASABI_API_LNGSTRINGW_BUF(IDS_WEBINFO_NAVIGATE_ERROR, szErrorString, sizeof(szErrorString)/sizeof(wchar_t));
  225. DisplayMessage(szErrorString, TRUE);
  226. nHomePage = HOMEPAGE_FAILED;
  227. }
  228. void WebFileInfo::OnDocumentReady(IDispatch *pDispatch, VARIANT *URL)
  229. {
  230. HTMLContainer2::OnDocumentReady(pDispatch, URL);
  231. if (bstrMessage)
  232. {
  233. if (URL && VT_BSTR == URL->vt && URL->bstrVal && 0 == lstrcmpW(URL->bstrVal, L"about:blank"))
  234. {
  235. wchar_t szHTML[4096] = {0};
  236. if (S_OK == StringCchPrintfW(szHTML, sizeof(szHTML)/sizeof(wchar_t), pszHTMLTemplate, bstrMessage))
  237. {
  238. WriteHTML(szHTML);
  239. }
  240. }
  241. SysFreeString(bstrMessage);
  242. bstrMessage = NULL;
  243. }
  244. if (HOMEPAGE_LOADING == nHomePage)
  245. {
  246. nHomePage = HOMEPAGE_LOADED;
  247. if (bstrFileName)
  248. {
  249. InvokeFileInfo(bstrFileName);
  250. SysFreeString(bstrFileName);
  251. bstrFileName = NULL;
  252. }
  253. }
  254. }
  255. HRESULT WebFileInfo::InvokeFileInfo(LPCWSTR pszFileName)
  256. {
  257. HRESULT hr;
  258. DISPPARAMS dispParams;
  259. LCID lcid;
  260. if (HOMEPAGE_NOTLOADED == nHomePage)
  261. {
  262. if (bstrFileName)
  263. {
  264. SysFreeString(bstrFileName);
  265. bstrFileName = NULL;
  266. }
  267. bstrFileName = (pszFileName) ? SysAllocString(pszFileName) : NULL;
  268. return NavigateToPage();
  269. }
  270. lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
  271. ZeroMemory(&dispParams, sizeof(DISPPARAMS));
  272. dispParams.cArgs = 1;
  273. dispParams.rgvarg = (VARIANTARG*)calloc(dispParams.cArgs, sizeof(VARIANTARG));
  274. if (!dispParams.rgvarg) hr = E_OUTOFMEMORY;
  275. else
  276. {
  277. VariantInit(&dispParams.rgvarg[0]);
  278. dispParams.rgvarg[0].vt = VT_BSTR;
  279. dispParams.rgvarg[0].bstrVal = SysAllocString(pszFileName);
  280. wTRACE_FMT(L"WebInfo: Requesting song info for '%s'.\n", pszFileName);
  281. hr = InvokeScriptFunction(WEBINFO_FUNCTION, lcid, &dispParams, NULL, NULL, NULL);
  282. if (S_OK != hr) TRACE_FMT(TEXT("Error sending webinfo (0x%08X)\n"), hr);
  283. VariantClear(&dispParams.rgvarg[0]);
  284. free(dispParams.rgvarg);
  285. }
  286. return hr;
  287. }
  288. HRESULT WebFileInfo::NavigateToPage(void)
  289. {
  290. HRESULT hr;
  291. nHomePage = HOMEPAGE_LOADING;
  292. hr = NavigateToName(WEBINFO_URL, navNoHistory);
  293. if (FAILED(hr)) nHomePage = HOMEPAGE_FAILED;
  294. return hr;
  295. }
  296. HRESULT WebFileInfo::UpdateColors(void)
  297. {
  298. HRESULT hr;
  299. IWebBrowser2 *pWeb2;
  300. if (HOMEPAGE_LOADED == nHomePage) return S_OK;
  301. hr = GetIWebBrowser2(&pWeb2);
  302. if (SUCCEEDED(hr))
  303. {
  304. hr = pWeb2->Refresh();
  305. pWeb2->Release();
  306. }
  307. return hr;
  308. }
  309. HRESULT WebFileInfo::DisplayMessage(LPCWSTR pszMessage, BOOL bPostIt)
  310. {
  311. HRESULT hr;
  312. VARIANT Flags, URL;
  313. if (bstrMessage)
  314. {
  315. SysFreeString(bstrMessage);
  316. bstrMessage = NULL;
  317. }
  318. bstrMessage = (pszMessage) ? SysAllocString(pszMessage) : NULL;
  319. VariantInit(&URL);
  320. VariantInit(&Flags);
  321. Flags.vt = VT_I4;
  322. V_I4(&Flags) = navNoHistory | navNoReadFromCache | navNoWriteToCache;
  323. URL.vt = VT_BSTR;
  324. V_BSTR(&URL) = SysAllocString(L"about:blank");
  325. nHomePage = HOMEPAGE_NOTLOADED;
  326. hr = (bPostIt) ? PostNavigate2(&URL, &Flags, NULL, NULL, NULL) : Navigate2(&URL, &Flags, NULL, NULL, NULL);
  327. VariantClear(&Flags);
  328. VariantClear(&URL);
  329. return hr;
  330. }
  331. HRESULT WebFileInfo::DragEnter(IDataObject * pDataObject, DWORD grfKeyState, POINTL pt, DWORD * pdwEffect)
  332. {
  333. HRESULT hr;
  334. FORMATETC format = {CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  335. hr = pDataObject->QueryGetData(&format);
  336. nDragMode = ((hr == S_OK) ? DROPEFFECT_COPY : DROPEFFECT_NONE);
  337. *pdwEffect = nDragMode;
  338. return S_OK;
  339. }
  340. HRESULT WebFileInfo::DragOver(DWORD grfKeyState, POINTL pt, DWORD * pdwEffect)
  341. {
  342. *pdwEffect = nDragMode;
  343. return S_OK;
  344. }
  345. HRESULT WebFileInfo::DragLeave(void)
  346. {
  347. nDragMode = DROPEFFECT_NONE;
  348. return S_OK;
  349. }
  350. HRESULT WebFileInfo::Drop(IDataObject * pDataObject, DWORD grfKeyState, POINTL pt, DWORD * pdwEffect)
  351. {
  352. STGMEDIUM medium;
  353. FORMATETC format = {CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  354. if (nDragMode)
  355. {
  356. HRESULT hr = pDataObject->QueryGetData(&format);
  357. if (S_OK == hr)
  358. {
  359. hr = pDataObject->GetData (&format, &medium);
  360. if (S_OK == hr)
  361. {
  362. wchar_t szFileName[4096] = {0};
  363. HDROP hdrop = (HDROP)medium.hGlobal;
  364. if (hdrop && DragQueryFileW(hdrop, 0, szFileName, sizeof(szFileName)/sizeof(wchar_t))) InvokeFileInfo(szFileName);
  365. }
  366. }
  367. nDragMode = DROPEFFECT_NONE;
  368. }
  369. return S_OK;
  370. }
  371. static COLORREF GetHTMLColor(int nColorIndex)
  372. {
  373. COLORREF rgb = WADlg_getColor(nColorIndex);
  374. return ((rgb >> 16)&0xff|(rgb&0xff00)|((rgb<<16)&0xff0000));
  375. }