1
0

OMCOM.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. #include "main.h"
  2. #include "./omcom.h"
  3. #include "./resource.h"
  4. #include "./api__ml_online.h"
  5. #include "JnetCOM.h"
  6. #include "./navigation.h"
  7. #include "./preferences.h"
  8. #include "./serviceHelper.h"
  9. #include "./serviceHost.h"
  10. #include "./config.h"
  11. #include "../nu/ConfigCOM.h"
  12. #include "../nu/MediaLibraryInterface.h"
  13. #include "../nu/AutoChar.h"
  14. #include "../nu/AutoWide.h"
  15. #include "../nu/AutoUrl.h"
  16. #include <ifc_omservice.h>
  17. #include <ifc_omserviceeditor.h>
  18. #include <ifc_omfilestorage.h>
  19. #include "../Winamp/JSAPI.h" // IDispatch helper macros
  20. #include <shlwapi.h>
  21. #include <strsafe.h>
  22. extern MediaLibraryInterface mediaLibrary;
  23. extern C_Config *g_config;
  24. #define CONFIG_SERIALNUMBER "serialNumber"
  25. #define SERIALNUMBER_INVALID ((INT)-1)
  26. #define SERIALNUMBER_DEFAULT ((INT)0)
  27. enum
  28. {
  29. DISPATCH_ISSUBSCRIBED = 12312,
  30. DISPATCH_ADDSUBSCRIBED,
  31. DISPATCH_CLEARSUBSCRIBED,
  32. DISPATCH_SERIALNUMBER,
  33. DISPATCH_JNETCREATE,
  34. DISPATCH_GETWID,
  35. DISPATCH_GETSID,
  36. DISPATCH_PLAY,
  37. DISPATCH_CONFIG,
  38. DISPATCH_ENQUEUE,
  39. DISPATCH_PREF,
  40. DISPATCH_SETSIZE,
  41. DISPATCH_GETX,
  42. DISPATCH_GETY,
  43. DISPATCH_NAVDISPLAY,
  44. DISPATCH_FOCUSURL,
  45. DISPATCH_SETCURRENTGUID,
  46. DISPATCH_ADDTITLEHOOK,
  47. DISPATCH_REMOVETITLEHOOK,
  48. DISPATCH_ADDMETADATAHOOK,
  49. DISPATCH_REMOVEMETADATAHOOK,
  50. DISPATCH_SUBSCRIBE,
  51. };
  52. OMCOM::OMCOM()
  53. : config(NULL), serialNumber(SERIALNUMBER_INVALID), publishCookie(0)
  54. {
  55. }
  56. OMCOM::~OMCOM()
  57. {
  58. if (NULL != config)
  59. config->Release();
  60. if (0 != publishCookie)
  61. {
  62. SENDWAIPC(Plugin_GetWinamp(), IPC_REMOVE_DISPATCH_OBJECT, publishCookie);
  63. publishCookie = NULL;
  64. }
  65. }
  66. HRESULT OMCOM::Publish()
  67. {
  68. if (0 != publishCookie)
  69. return E_FAIL;
  70. DispatchInfo dispatchInfo;
  71. ZeroMemory(&dispatchInfo, sizeof(dispatchInfo));
  72. dispatchInfo.name = L"OnMedia";
  73. dispatchInfo.dispatch = this;
  74. if (0 != SENDWAIPC(Plugin_GetWinamp(), IPC_ADD_DISPATCH_OBJECT, (WPARAM)&dispatchInfo))
  75. return E_FAIL;
  76. publishCookie = dispatchInfo.id;
  77. return S_OK;
  78. }
  79. HRESULT OMCOM::GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgdispid)
  80. {
  81. bool unknowns = false;
  82. for (unsigned int i = 0;i != cNames;i++)
  83. {
  84. if (wcscmp(rgszNames[i], L"IsOmSubscribed") == 0)
  85. rgdispid[i] = DISPATCH_ISSUBSCRIBED;
  86. else if (wcscmp(rgszNames[i], L"AddOmSubscribed") == 0)
  87. rgdispid[i] = DISPATCH_ADDSUBSCRIBED;
  88. else if (wcscmp(rgszNames[i], L"ClearOmSubscribed") == 0)
  89. rgdispid[i] = DISPATCH_CLEARSUBSCRIBED;
  90. else if (wcscmp(rgszNames[i], L"OmSerialNumber") == 0)
  91. rgdispid[i] = DISPATCH_SERIALNUMBER;
  92. else if (wcscmp(rgszNames[i], L"JnetCreate") == 0)
  93. rgdispid[i] = DISPATCH_JNETCREATE;
  94. else if (wcscmp(rgszNames[i], L"GetUniqueID") == 0)
  95. rgdispid[i] = DISPATCH_GETWID;
  96. else if (wcscmp(rgszNames[i], L"GetSessionID") == 0)
  97. rgdispid[i] = DISPATCH_GETSID;
  98. else if (wcscmp(rgszNames[i], L"PlayUrl") == 0)
  99. rgdispid[i] = DISPATCH_PLAY;
  100. else if (wcscmp(rgszNames[i], L"Config") == 0)
  101. rgdispid[i] = DISPATCH_CONFIG;
  102. else if (wcscmp(rgszNames[i], L"EnqueueUrl") == 0)
  103. rgdispid[i] = DISPATCH_ENQUEUE;
  104. else if (wcscmp(rgszNames[i], L"ShowPreferences") == 0)
  105. rgdispid[i] = DISPATCH_PREF;
  106. else if (wcscmp(rgszNames[i], L"SetSize") == 0)
  107. rgdispid[i] = DISPATCH_SETSIZE;
  108. else if (wcscmp(rgszNames[i], L"GetX") == 0)
  109. rgdispid[i] = DISPATCH_GETX;
  110. else if (wcscmp(rgszNames[i], L"GetY") == 0)
  111. rgdispid[i] = DISPATCH_GETY;
  112. else if (wcscmp(rgszNames[i], L"DisplayNav") == 0)
  113. rgdispid[i] = DISPATCH_NAVDISPLAY;
  114. else if (wcscmp(rgszNames[i], L"FocusUrl") == 0)
  115. rgdispid[i] = DISPATCH_FOCUSURL;
  116. else if (wcscmp(rgszNames[i], L"SetCurrentGUID") == 0)
  117. rgdispid[i] = DISPATCH_SETCURRENTGUID;
  118. else if (wcscmp(rgszNames[i], L"AddTitleHook") == 0)
  119. rgdispid[i] = DISPATCH_ADDTITLEHOOK;
  120. else if (wcscmp(rgszNames[i], L"RemoveTitleHook") == 0)
  121. rgdispid[i] = DISPATCH_REMOVETITLEHOOK;
  122. else if (wcscmp(rgszNames[i], L"AddMetadataHook") == 0)
  123. rgdispid[i] = DISPATCH_ADDMETADATAHOOK;
  124. else if (wcscmp(rgszNames[i], L"RemoveMetadataHook") == 0)
  125. rgdispid[i] = DISPATCH_REMOVEMETADATAHOOK;
  126. else if (wcscmp(rgszNames[i], L"Subscribe") == 0)
  127. rgdispid[i] = DISPATCH_SUBSCRIBE;
  128. else
  129. {
  130. rgdispid[i] = DISPID_UNKNOWN;
  131. unknowns = true;
  132. }
  133. }
  134. if (unknowns)
  135. return DISP_E_UNKNOWNNAME;
  136. else
  137. return S_OK;
  138. }
  139. HRESULT OMCOM::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo)
  140. {
  141. return E_NOTIMPL;
  142. }
  143. HRESULT OMCOM::GetTypeInfoCount(unsigned int FAR * pctinfo)
  144. {
  145. return E_NOTIMPL;
  146. }
  147. #define VIDEO_GENFF_SIZEREQUEST (WM_USER+2048)
  148. static void RemoveTitleHook(const wchar_t *url)
  149. {
  150. Nullsoft::Utility::AutoLock lock(urlMapGuard);
  151. DISPATCH_REMOVETITLEHOOK_again:
  152. URLMap::iterator itr;
  153. for (itr=urlMap.begin();itr!=urlMap.end();itr++)
  154. {
  155. if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, url, -1, itr->url.c_str(), - 1))
  156. {
  157. urlMap.erase(itr);
  158. goto DISPATCH_REMOVETITLEHOOK_again;
  159. }
  160. }
  161. }
  162. static void RemoveMetadataHook(const wchar_t *url)
  163. {
  164. Nullsoft::Utility::AutoLock lock(urlMapGuard);
  165. DISPATCH_REMOVEMETADATAHOOK_again:
  166. MetadataMap::iterator itr;
  167. for (itr=metadataMap.begin();itr!=metadataMap.end();itr++)
  168. {
  169. if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, url, -1, itr->url.c_str(), - 1))
  170. {
  171. metadataMap.erase(itr);
  172. goto DISPATCH_REMOVEMETADATAHOOK_again;
  173. }
  174. }
  175. }
  176. static void RemoveMetadataHook(const wchar_t *url, const wchar_t *tag)
  177. {
  178. Nullsoft::Utility::AutoLock lock(urlMapGuard);
  179. DISPATCH_REMOVEMETADATAHOOK_again2:
  180. MetadataMap::iterator itr;
  181. for (itr=metadataMap.begin();itr!=metadataMap.end();itr++)
  182. {
  183. if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, url, -1, itr->url.c_str(), - 1) &&
  184. CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, tag, -1, itr->tag.c_str(), - 1))
  185. {
  186. metadataMap.erase(itr);
  187. goto DISPATCH_REMOVEMETADATAHOOK_again2;
  188. }
  189. }
  190. }
  191. HRESULT OMCOM::FindService(VARIANTARG *pArg, ifc_omservice **service)
  192. {
  193. if (NULL == service)
  194. return E_POINTER;
  195. *service = NULL;
  196. if (NULL == pArg)
  197. return E_INVALIDARG;
  198. HRESULT hr = E_INVALIDARG;
  199. UINT serviceId;
  200. if (VT_BSTR == pArg->vt)
  201. {
  202. if (FALSE != StrToIntEx(pArg->bstrVal, STIF_SUPPORT_HEX, (INT*)&serviceId))
  203. hr = S_OK;
  204. }
  205. else if (VT_I4 == pArg->vt)
  206. {
  207. serviceId = pArg->lVal;
  208. hr = S_OK;
  209. }
  210. if (SUCCEEDED(hr))
  211. hr = ServiceHelper_Find(serviceId, service);
  212. return hr;
  213. }
  214. static HRESULT OmCom_AddServiceToNavigation(ifc_omservice *service)
  215. {
  216. Navigation *navigation;
  217. HRESULT hr = Plugin_GetNavigation(&navigation);
  218. if (SUCCEEDED(hr))
  219. {
  220. if (NULL != navigation->CreateItem(service))
  221. {
  222. hr = S_OK;
  223. }
  224. else
  225. {
  226. hr = E_FAIL;
  227. }
  228. navigation->Release();
  229. }
  230. return hr;
  231. }
  232. static HRESULT OmCom_SubscribeToService(UINT serviceId, LPCWSTR pszName, LPCWSTR pszUrl, INT iconId)
  233. {
  234. ifc_omservice *service = NULL;
  235. HRESULT hr = ServiceHelper_Find(serviceId, &service);
  236. if (FAILED(hr)) return hr;
  237. if (S_FALSE == hr)
  238. {
  239. ServiceHost *serviceHost;
  240. if (FAILED(ServiceHost::GetCachedInstance(&serviceHost)))
  241. serviceHost = NULL;
  242. if (NULL != OMSERVICEMNGR)
  243. {
  244. hr = OMSERVICEMNGR->CreateService(serviceId, serviceHost, &service);
  245. }
  246. else
  247. hr = E_FAIL;
  248. }
  249. else if (S_OK == ServiceHelper_IsSubscribed(service))
  250. {
  251. hr = S_FALSE;
  252. }
  253. if (SUCCEEDED(hr))
  254. {
  255. if (S_OK == hr)
  256. {
  257. ifc_omserviceeditor *editor;
  258. hr = service->QueryInterface(IFC_OmServiceEditor, (void**)&editor);
  259. if (SUCCEEDED(hr))
  260. {
  261. if (NULL != pszName && L'\0' != *pszName)
  262. editor->SetName(pszName, FALSE);
  263. if (NULL != pszUrl && L'\0' != *pszUrl)
  264. editor->SetUrl(pszUrl, FALSE);
  265. WCHAR szIcon[256] = {0};
  266. if (SUCCEEDED(StringCchPrintf(szIcon, ARRAYSIZE(szIcon), L"%u", iconId)))
  267. {
  268. editor->SetIcon(szIcon, FALSE);
  269. }
  270. hr = editor->SetFlags(SVCF_SUBSCRIBED, SVCF_SUBSCRIBED);
  271. if (SUCCEEDED(hr))
  272. ServiceHelper_Save(service);
  273. editor->Release();
  274. }
  275. if (SUCCEEDED(hr))
  276. OmCom_AddServiceToNavigation(service);
  277. }
  278. service->Release();
  279. }
  280. return hr;
  281. }
  282. HRESULT OMCOM::Invoke(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, EXCEPINFO FAR * pexecinfo, unsigned int FAR *puArgErr)
  283. {
  284. if (dispid == DISPATCH_ADDTITLEHOOK)
  285. {
  286. if (pdispparams->cArgs == 3)
  287. {
  288. Nullsoft::Utility::AutoLock lock(urlMapGuard);
  289. RemoveTitleHook(pdispparams->rgvarg[2].bstrVal); // ensure no duplicates
  290. url_info info;
  291. info.url = pdispparams->rgvarg[2].bstrVal;
  292. info.url_wcslen = wcslen(info.url.c_str());
  293. info.title=pdispparams->rgvarg[1].bstrVal;
  294. info.length= pdispparams->rgvarg[0].lVal;
  295. urlMap.push_back(info);
  296. return S_OK;
  297. }
  298. }
  299. if (dispid == DISPATCH_REMOVETITLEHOOK)
  300. {
  301. if (pdispparams->cArgs == 1)
  302. {
  303. RemoveTitleHook(pdispparams->rgvarg[0].bstrVal);
  304. return S_OK;
  305. }
  306. else
  307. return DISP_E_BADPARAMCOUNT;
  308. }
  309. if (dispid == DISPATCH_REMOVEMETADATAHOOK)
  310. {
  311. if (pdispparams->cArgs == 1)
  312. {
  313. RemoveMetadataHook(pdispparams->rgvarg[0].bstrVal);
  314. return S_OK;
  315. }
  316. else if (pdispparams->cArgs == 2)
  317. {
  318. RemoveMetadataHook(pdispparams->rgvarg[1].bstrVal, pdispparams->rgvarg[0].bstrVal);
  319. return S_OK;
  320. }
  321. else
  322. return DISP_E_BADPARAMCOUNT;
  323. }
  324. if (dispid == DISPATCH_ADDMETADATAHOOK)
  325. {
  326. if (pdispparams->cArgs == 3)
  327. {
  328. Nullsoft::Utility::AutoLock lock(urlMapGuard);
  329. RemoveMetadataHook(pdispparams->rgvarg[2].bstrVal, pdispparams->rgvarg[1].bstrVal); // ensure no duplicates
  330. metadata_info info;
  331. info.url = pdispparams->rgvarg[2].bstrVal;
  332. info.tag = pdispparams->rgvarg[1].bstrVal;
  333. info.metadata= pdispparams->rgvarg[0].bstrVal;
  334. metadataMap.push_back(info);
  335. return S_OK;
  336. }
  337. }
  338. if (dispid == DISPATCH_SERIALNUMBER)
  339. {
  340. int serial = GetSerialNumber(FALSE);
  341. if (pdispparams->cArgs == 1)
  342. {
  343. SetSerialNumber(pdispparams->rgvarg[0].lVal);
  344. serial = GetSerialNumber(FALSE);
  345. }
  346. JSAPI_INIT_RESULT(pvarResult, VT_I4);
  347. JSAPI_SET_RESULT(pvarResult, lVal, serial);
  348. return S_OK;
  349. }
  350. if (dispid == DISPATCH_ADDSUBSCRIBED)
  351. {
  352. return AddOmSubscribed(wFlags, pdispparams, pvarResult, puArgErr);
  353. }
  354. if (dispid == DISPATCH_SUBSCRIBE)
  355. {
  356. return Subscribe(wFlags, pdispparams, pvarResult, puArgErr);
  357. }
  358. if (dispid == DISPATCH_CLEARSUBSCRIBED && pdispparams->cArgs == 1)
  359. {
  360. JSAPI_VERIFY_METHOD(wFlags);
  361. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 1);
  362. JSAPI_INIT_RESULT(pvarResult, VT_I4);
  363. BOOL result = FALSE;
  364. ifc_omservice *service;
  365. if (S_OK == FindService(&JSAPI_PARAM(pdispparams, 1), &service))
  366. {
  367. if (SUCCEEDED(ServiceHelper_Subscribe(service, FALSE, SHF_SAVE /* | SHF_NOTIFY*/)))
  368. result = TRUE;
  369. service->Release();
  370. }
  371. JSAPI_SET_RESULT(pvarResult, lVal, result);
  372. return S_OK;
  373. }
  374. if (dispid == DISPATCH_ISSUBSCRIBED)
  375. {
  376. return IsOmSubscribed(wFlags, pdispparams, pvarResult, puArgErr);
  377. }
  378. if (dispid == DISPATCH_JNETCREATE)
  379. {
  380. VariantInit(pvarResult);
  381. V_VT(pvarResult) = VT_DISPATCH;
  382. V_DISPATCH(pvarResult) = new JnetCOM();
  383. return S_OK;
  384. }
  385. if (dispid == DISPATCH_GETWID)
  386. {
  387. WCHAR szBuffer[512] = {0};
  388. if (NULL == OMBROWSERMNGR ||
  389. FAILED(OMBROWSERMNGR->GetClientId(szBuffer, ARRAYSIZE(szBuffer))))
  390. {
  391. szBuffer[0] = L'\0';
  392. }
  393. BSTR tag = SysAllocString(szBuffer);
  394. VariantInit(pvarResult);
  395. V_VT(pvarResult) = VT_BSTR;
  396. V_BSTR(pvarResult) = tag;
  397. return S_OK;
  398. }
  399. if (dispid == DISPATCH_GETSID)
  400. {
  401. WCHAR szBuffer[512] = {0};
  402. if (NULL == OMBROWSERMNGR ||
  403. FAILED(OMBROWSERMNGR->GetSessionId(szBuffer, ARRAYSIZE(szBuffer))))
  404. {
  405. szBuffer[0] = L'\0';
  406. }
  407. BSTR tag = SysAllocString(szBuffer);
  408. VariantInit(pvarResult);
  409. V_VT(pvarResult) = VT_BSTR;
  410. V_BSTR(pvarResult) = tag;
  411. return S_OK;
  412. }
  413. if (dispid == DISPATCH_PLAY && pdispparams->cArgs == 1)
  414. {
  415. if (pdispparams->rgvarg[0].bstrVal)
  416. mediaLibrary.PlayStream(pdispparams->rgvarg[0].bstrVal);
  417. return S_OK;
  418. }
  419. if (dispid == DISPATCH_PREF)
  420. {
  421. Preferences_Show();
  422. return S_OK;
  423. }
  424. if (dispid == DISPATCH_CONFIG)
  425. {
  426. VariantInit(pvarResult);
  427. if(NULL == config && FAILED(ConfigCOM::CreateInstanceA("ml_online_config", g_config->GetPath(), &config)))
  428. {
  429. V_VT(pvarResult) = VT_NULL;
  430. }
  431. else
  432. {
  433. V_VT(pvarResult) = VT_DISPATCH;
  434. V_DISPATCH(pvarResult) = config;
  435. config->AddRef();
  436. }
  437. return S_OK;
  438. }
  439. if (dispid == DISPATCH_ENQUEUE && pdispparams->cArgs == 1)
  440. {
  441. if (pdispparams->rgvarg[0].bstrVal)
  442. mediaLibrary.EnqueueStream(pdispparams->rgvarg[0].bstrVal);
  443. return S_OK;
  444. }
  445. if (dispid == DISPATCH_SETSIZE && pdispparams->cArgs == 2)
  446. {
  447. HWND hView = NULL;
  448. Navigation *navigation;
  449. if (SUCCEEDED(Plugin_GetNavigation(&navigation)))
  450. {
  451. hView = navigation->GetActiveView(NULL);
  452. navigation->Release();
  453. }
  454. if (NULL != hView && mediaLibrary.library &&
  455. GetParent(mediaLibrary.library) &&
  456. g_config->ReadInt("AutoSize",1))
  457. {
  458. HWND hWnd;
  459. bool GenFF = false;
  460. if (GetParent(GetParent(mediaLibrary.library)))
  461. {
  462. hWnd = GetParent(GetParent(mediaLibrary.library));
  463. GenFF = true;
  464. }
  465. else
  466. hWnd = GetParent(mediaLibrary.library);
  467. int width = pdispparams->rgvarg[1].lVal;
  468. int height = pdispparams->rgvarg[0].lVal;
  469. RECT rc;
  470. GetWindowRect(hView, &rc); // Our Html page
  471. int WWidth = rc.right - rc.left;
  472. int WHeight = rc.bottom - rc.top;
  473. GetWindowRect(hWnd, &rc); // Gen ML Size
  474. int PWidth = rc.right - rc.left;
  475. int PHeight = rc.bottom - rc.top;
  476. // Subtract the original window size from the parent(base) size
  477. PWidth -= WWidth;
  478. PHeight -= WHeight;
  479. // Add the target size to the parent(base) size
  480. PWidth += width;
  481. PHeight += height;
  482. if (GenFF)
  483. {
  484. SendMessage(hWnd, VIDEO_GENFF_SIZEREQUEST, PWidth, PHeight);
  485. }
  486. else
  487. {
  488. SetWindowPos(hWnd, 0, 0, 0, PWidth, PHeight, SWP_NOMOVE|SWP_ASYNCWINDOWPOS);
  489. // weird? sometimes height isnt set if called once...
  490. SetWindowPos(hWnd, 0, 0, 0, PWidth, PHeight, SWP_NOMOVE|SWP_ASYNCWINDOWPOS);
  491. }
  492. }
  493. return S_OK;
  494. }
  495. if (dispid == DISPATCH_GETX)
  496. {
  497. RECT rc;
  498. HWND hView = NULL;
  499. Navigation *navigation;
  500. if (SUCCEEDED(Plugin_GetNavigation(&navigation)))
  501. {
  502. hView = navigation->GetActiveView(NULL);
  503. navigation->Release();
  504. }
  505. if (NULL != hView)
  506. {
  507. GetWindowRect(hView, &rc); // Our Html page
  508. int WWidth = rc.right - rc.left;
  509. if (pvarResult)
  510. {
  511. VariantInit(pvarResult);
  512. V_VT(pvarResult) = VT_I4;
  513. V_I4(pvarResult) = WWidth;
  514. }
  515. }
  516. return S_OK;
  517. }
  518. if (dispid == DISPATCH_GETY)
  519. {
  520. RECT rc;
  521. HWND hView = NULL;
  522. Navigation *navigation;
  523. if (SUCCEEDED(Plugin_GetNavigation(&navigation)))
  524. {
  525. hView = navigation->GetActiveView(NULL);
  526. navigation->Release();
  527. }
  528. if (NULL != hView)
  529. {
  530. GetWindowRect(hView, &rc); // Our Html page
  531. int WHeight = rc.bottom - rc.top;
  532. if (pvarResult)
  533. {
  534. VariantInit(pvarResult);
  535. V_VT(pvarResult) = VT_I4;
  536. V_I4(pvarResult) = WHeight;
  537. }
  538. }
  539. return S_OK;
  540. }
  541. if (dispid == DISPATCH_NAVDISPLAY && pdispparams->cArgs == 1)
  542. {
  543. //int visible = pdispparams->rgvarg[0].lVal;
  544. return E_NOTIMPL;
  545. }
  546. if (dispid == DISPATCH_FOCUSURL && pdispparams->cArgs == 2)
  547. {
  548. ifc_omservice *service;
  549. if (S_OK == FindService(&pdispparams->rgvarg[1], &service))
  550. {
  551. Navigation *navigation;
  552. if (SUCCEEDED(Plugin_GetNavigation(&navigation)))
  553. {
  554. navigation->ShowService(service->GetId(), pdispparams->rgvarg[0].bstrVal);
  555. navigation->Release();
  556. }
  557. service->Release();
  558. }
  559. return S_OK;
  560. }
  561. return DISP_E_MEMBERNOTFOUND;
  562. }
  563. STDMETHODIMP OMCOM::QueryInterface(REFIID riid, PVOID *ppvObject)
  564. {
  565. if (!ppvObject)
  566. return E_POINTER;
  567. else if (IsEqualIID(riid, IID_IDispatch))
  568. *ppvObject = (IDispatch *)this;
  569. else if (IsEqualIID(riid, IID_IUnknown))
  570. *ppvObject = this;
  571. else
  572. {
  573. *ppvObject = NULL;
  574. return E_NOINTERFACE;
  575. }
  576. AddRef();
  577. return S_OK;
  578. }
  579. ULONG OMCOM::AddRef(void)
  580. {
  581. return 0;
  582. }
  583. ULONG OMCOM::Release(void)
  584. {
  585. return 0;
  586. }
  587. HRESULT OMCOM::IsOmSubscribed(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  588. {
  589. JSAPI_VERIFY_METHOD(wFlags);
  590. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 1);
  591. JSAPI_INIT_RESULT(pvarResult, VT_I4);
  592. // 0 No knowledge , 1 Knowledge and Disabled, 2 Knowledge and Enabled
  593. ifc_omservice *service;
  594. if (S_OK == FindService(&JSAPI_PARAM(pdispparams, 1), &service))
  595. {
  596. JSAPI_SET_RESULT(pvarResult, lVal, (S_OK == ServiceHelper_IsSubscribed(service)) ? 2 : 1);
  597. service->Release();
  598. }
  599. else
  600. {
  601. JSAPI_SET_RESULT(pvarResult, lVal, 0);
  602. }
  603. return S_OK;
  604. }
  605. HRESULT OMCOM::AddOmSubscribed(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  606. {
  607. JSAPI_VERIFY_METHOD(wFlags);
  608. JSAPI_VERIFY_PARAMCOUNT_OPTIONAL(pdispparams, 3, 4);
  609. JSAPI_INIT_RESULT(pvarResult, VT_I4);
  610. LPCWSTR pszUrl, pszName;
  611. UINT baseArg, serviceId, iconId;
  612. baseArg = 0;
  613. if (pdispparams->cArgs >= 4)
  614. {
  615. JSAPI_GETUNSIGNED_AS_NUMBER(iconId, pdispparams, (baseArg + 1), puArgErr);
  616. baseArg++;
  617. }
  618. else
  619. iconId = 0;
  620. JSAPI_GETUNSIGNED_AS_NUMBER(serviceId, pdispparams, (baseArg + 1), puArgErr);
  621. JSAPI_GETSTRING(pszName, pdispparams, (baseArg + 2), puArgErr);
  622. JSAPI_GETSTRING(pszUrl, pdispparams, (baseArg + 3), puArgErr);
  623. INT result = OmCom_SubscribeToService(serviceId, pszName, pszUrl, iconId);
  624. JSAPI_SET_RESULT(pvarResult, lVal, result);
  625. return S_OK;
  626. }
  627. typedef int (*HTTPRETRIEVEFILEW)(HWND hwnd, char *url, wchar_t *file, wchar_t *dlgtitle);
  628. HRESULT OMCOM::Subscribe(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  629. {
  630. // window.external.OnMedia.Subscribe(String name, String url, String id, String icon, String version);
  631. JSAPI_VERIFY_METHOD(wFlags);
  632. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 5);
  633. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  634. WCHAR szBuffer[4096] = {0};
  635. LPCWSTR pszName, pszUrl, pszIcon;
  636. UINT serviceId, version;
  637. JSAPI_GETSTRING(pszName, pdispparams, 1, puArgErr);
  638. JSAPI_GETSTRING(pszUrl, pdispparams, 2, puArgErr);
  639. JSAPI_GETUNSIGNED_AS_NUMBER(serviceId, pdispparams, 3, puArgErr);
  640. JSAPI_GETNUMBER_AS_STRING(pszIcon, szBuffer, pdispparams, 4, puArgErr);
  641. JSAPI_GETUNSIGNED_AS_NUMBER(version, pdispparams, 5, puArgErr);
  642. HRESULT hr;
  643. ifc_omservice *service;
  644. hr = ServiceHelper_Find(serviceId, &service);
  645. if (S_OK != hr)
  646. {
  647. hr = ServiceHelper_Create(serviceId, pszName, pszIcon, pszUrl, SVCF_SUBSCRIBED | SVCF_PREAUTHORIZED, 2, TRUE, &service);
  648. if (SUCCEEDED(hr))
  649. {
  650. OmCom_AddServiceToNavigation(service);
  651. service->Release();
  652. }
  653. }
  654. else
  655. {
  656. hr = ServiceHelper_Subscribe(service, TRUE, SHF_SAVE /*| SHF_NOTIFY*/); // do not call SHF_NOTIFY - or it will adjust stats
  657. if (S_OK == hr)
  658. OmCom_AddServiceToNavigation(service);
  659. service->Release();
  660. }
  661. JSAPI_SET_RESULT(pvarResult, boolVal, (SUCCEEDED(hr) ? VARIANT_TRUE : VARIANT_FALSE));
  662. return S_OK;
  663. }
  664. HRESULT OMCOM::SetSerialNumber(INT sn)
  665. {
  666. if (SERIALNUMBER_INVALID == sn)
  667. return E_INVALIDARG;
  668. if (serialNumber == sn)
  669. return S_FALSE;
  670. serialNumber = sn;
  671. CHAR szBuffer[64] = {0};
  672. HRESULT hr = StringCchPrintfA(szBuffer, ARRAYSIZE(szBuffer), "%d", serialNumber);
  673. if (SUCCEEDED(hr))
  674. {
  675. hr = Config_WriteStr(NULL, CONFIG_SERIALNUMBER, szBuffer);
  676. }
  677. return hr;
  678. }
  679. INT OMCOM::GetSerialNumber(BOOL fForceRead)
  680. {
  681. if (FALSE != fForceRead || SERIALNUMBER_INVALID == serialNumber)
  682. {
  683. serialNumber = Config_ReadInt(NULL, CONFIG_SERIALNUMBER, SERIALNUMBER_DEFAULT);
  684. }
  685. return serialNumber;
  686. }
  687. /*
  688. HRESULT OMCOM::Login(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  689. {
  690. // window.external.OnMedia.Login(String url);
  691. JSAPI_VERIFY_METHOD(wFlags);
  692. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 1);
  693. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  694. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_FALSE);
  695. const wchar_t *url = JSAPI_PARAM(pdispparams, 1).bstrVal;
  696. HWND active_view = OmView_GetActive();
  697. if (active_view)
  698. {
  699. OmService *service = OmView_GetService(active_view);
  700. if (service)
  701. {
  702. OMNAVIGATION->SelectService(service, url);
  703. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  704. }
  705. }
  706. return S_OK;
  707. }
  708. */