1
0

navigation.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. #include "main.h"
  2. #include "./navigation.h"
  3. #include "./resource.h"
  4. #include "./wasabi.h"
  5. #include "./local_menu.h"
  6. #include "./commands.h"
  7. #include "./forceUrl.h"
  8. #include "./config.h"
  9. #include "../omBrowser/browserView.h"
  10. #include "../winamp/wa_ipc.h"
  11. #include "./serviceHost.h"
  12. #include "./serviceHelper.h"
  13. #include <ifc_omservice.h>
  14. #include <ifc_omserviceeditor.h>
  15. #include <storageIni.h>
  16. #include <ifc_omserviceenum.h>
  17. #include <ifc_mlnavigationhelper.h>
  18. #include <ifc_omserviceeventmngr.h>
  19. #include <ifc_ombrowserwndmngr.h>
  20. #include <ifc_ombrowserwndenum.h>
  21. #define _ML_HEADER_IMPMLEMENT
  22. #include "../../General/gen_ml/ml_ipc_0313.h"
  23. #undef _ML_HEADER_IMPMLEMENT
  24. #include "../../General/gen_ml/menu.h"
  25. #include <vector>
  26. #include "../nu/sort.h"
  27. #include <shlwapi.h>
  28. #include <strsafe.h>
  29. #include <algorithm>
  30. #define NAVITEM_PREFIX L"webdev_svc_"
  31. #define E_NAVITEM_UNKNOWN E_NOINTERFACE
  32. static BOOL Navigation_CheckInvariantName(LPCWSTR pszInvarian)
  33. {
  34. INT cchInvariant = (NULL != pszInvarian) ? lstrlen(pszInvarian) : 0;
  35. INT cchPrefix = ARRAYSIZE(NAVITEM_PREFIX) - 1;
  36. return (cchInvariant > cchPrefix &&
  37. CSTR_EQUAL == CompareString(CSTR_INVARIANT, 0, NAVITEM_PREFIX, cchPrefix, pszInvarian, cchPrefix));
  38. }
  39. Navigation::Navigation()
  40. : ref(1), cookie(0), hRoot(NULL), hLibrary(NULL)
  41. {
  42. }
  43. Navigation::~Navigation()
  44. {
  45. }
  46. HRESULT Navigation::CreateInstance(Navigation **instance)
  47. {
  48. if (NULL == instance) return E_POINTER;
  49. HRESULT hr;
  50. Navigation *navigation = new Navigation();
  51. if (NULL != navigation)
  52. {
  53. hr = navigation->Initialize();
  54. if (FAILED(hr))
  55. {
  56. navigation->Release();
  57. navigation = NULL;
  58. }
  59. }
  60. else
  61. {
  62. hr = E_OUTOFMEMORY;
  63. }
  64. *instance = navigation;
  65. return hr;
  66. }
  67. size_t Navigation::AddRef()
  68. {
  69. return InterlockedIncrement((LONG*)&ref);
  70. }
  71. size_t Navigation::Release()
  72. {
  73. if (0 == ref)
  74. return ref;
  75. LONG r = InterlockedDecrement((LONG*)&ref);
  76. if (0 == r)
  77. delete(this);
  78. return r;
  79. }
  80. int Navigation::QueryInterface(GUID interface_guid, void **object)
  81. {
  82. if (NULL == object) return E_POINTER;
  83. if (IsEqualIID(interface_guid, IFC_MlNavigationCallback))
  84. *object = static_cast<ifc_mlnavigationcallback*>(this);
  85. else
  86. {
  87. *object = NULL;
  88. return E_NOINTERFACE;
  89. }
  90. if (NULL == *object)
  91. return E_UNEXPECTED;
  92. AddRef();
  93. return S_OK;
  94. }
  95. HRESULT Navigation::Initialize()
  96. {
  97. hLibrary = Plugin_GetLibrary();
  98. if (NULL == hLibrary) return E_UNEXPECTED;
  99. if (0 == cookie)
  100. {
  101. ifc_mlnavigationhelper *navHelper;
  102. if (NULL != OMUTILITY && SUCCEEDED(OMUTILITY->GetMlNavigationHelper(Plugin_GetLibrary(), &navHelper)))
  103. {
  104. navHelper->RegisterCallback(this, &cookie);
  105. navHelper->Release();
  106. }
  107. }
  108. ifc_omservice *service;
  109. MLNavCtrl_BeginUpdate(hLibrary, NUF_LOCK_TOP);
  110. if (SUCCEEDED(ServiceHelper_Create(700, MAKEINTRESOURCE(IDS_ROOTSERVICE_NAME),
  111. NULL, MAKEINTRESOURCE(IDR_HTML_WELCOME),
  112. WDSVCF_ROOT, FALSE, &service)))
  113. {
  114. hRoot = CreateItemInt(NULL, service);
  115. service->Release();
  116. }
  117. if (NULL == hRoot)
  118. {
  119. MLNavCtrl_EndUpdate(hLibrary);
  120. return E_FAIL;
  121. }
  122. if (SUCCEEDED(ServiceHelper_Create(701, MAKEINTRESOURCE(IDS_DOCSERVICE_NAME),
  123. MAKEINTRESOURCE(IDR_HELP_ICON), L"http://dev.winamp.com/online-service-developers",
  124. WDSVCF_SPECIAL, FALSE, &service)))
  125. {
  126. CreateItemInt(hRoot, service);
  127. service->Release();
  128. }
  129. if (SUCCEEDED(ServiceHelper_Create(702, MAKEINTRESOURCE(IDS_TESTSERVICE_NAME),
  130. MAKEINTRESOURCE(IDR_GEAR_ICON), MAKEINTRESOURCE(IDR_HTML_TEST),
  131. WDSVCF_SPECIAL, FALSE, &service)))
  132. {
  133. CreateItemInt(hRoot, service);
  134. service->Release();
  135. }
  136. ifc_omstorage *storage;
  137. if (NULL != OMSERVICEMNGR && SUCCEEDED(OMSERVICEMNGR->QueryStorage(&SUID_OmStorageIni, &storage)))
  138. {
  139. ifc_omserviceenum *enumerator;
  140. WebDevServiceHost *serviceHost;
  141. if (FAILED(WebDevServiceHost::GetCachedInstance(&serviceHost)))
  142. serviceHost = NULL;
  143. if (SUCCEEDED(storage->Load(L"*.ini", serviceHost, &enumerator)))
  144. {
  145. ifc_omservice *service;
  146. std::vector<ifc_omservice*> serviceList;
  147. while (S_OK == enumerator->Next(1, &service, NULL))
  148. {
  149. serviceList.push_back(service);
  150. }
  151. enumerator->Release();
  152. size_t count = serviceList.size();
  153. Order(serviceList);
  154. for(size_t i =0; i < count; i++)
  155. {
  156. service = serviceList[i];
  157. CreateItemInt(hRoot, service);
  158. service->Release();
  159. }
  160. }
  161. storage->Release();
  162. if (NULL != serviceHost)
  163. serviceHost->Release();
  164. }
  165. MLNavCtrl_EndUpdate(hLibrary);
  166. return S_OK;
  167. }
  168. HRESULT Navigation::Finish()
  169. {
  170. ForceUrl_Remove();
  171. if (0 != cookie)
  172. {
  173. ifc_mlnavigationhelper *navHelper;
  174. if (NULL != OMUTILITY && SUCCEEDED(OMUTILITY->GetMlNavigationHelper(Plugin_GetLibrary(), &navHelper)))
  175. {
  176. navHelper->UnregisterCallback(cookie);
  177. navHelper->Release();
  178. }
  179. }
  180. if (NULL != OMBROWSERMNGR)
  181. {
  182. OMBROWSERMNGR->Finish();
  183. }
  184. return S_OK;
  185. }
  186. HRESULT Navigation::SaveOrder()
  187. {
  188. if (NULL == hRoot || NULL == hLibrary)
  189. return E_UNEXPECTED;
  190. LPSTR buffer = NULL;
  191. INT count = MLNavItem_GetChildrenCount(hLibrary, hRoot);
  192. if (count > 0)
  193. {
  194. size_t bufferMax = 11 * count;
  195. buffer = Plugin_MallocAnsiString(bufferMax);
  196. if (NULL == buffer) return E_OUTOFMEMORY;
  197. *buffer = '\0';
  198. LPSTR cursor = buffer;
  199. size_t remaining = bufferMax;
  200. NAVITEM item;
  201. item.cbSize = sizeof(item);
  202. item.mask = NIMF_PARAM;
  203. item.hItem = MLNavItem_GetChild(hLibrary, hRoot);
  204. while (NULL != item.hItem)
  205. {
  206. if (FALSE != MLNavItem_GetInfo(hLibrary, &item))
  207. {
  208. ifc_omservice *service = (ifc_omservice*)item.lParam;
  209. if (NULL != service)
  210. {
  211. UINT serviceFlags;
  212. if (SUCCEEDED(service->GetFlags(&serviceFlags)) &&
  213. 0 == ((WDSVCF_ROOT | WDSVCF_SPECIAL) & serviceFlags))
  214. {
  215. if (cursor == buffer ||
  216. SUCCEEDED(StringCchCopyExA(cursor, remaining, ";", &cursor, &remaining, STRSAFE_NULL_ON_FAILURE)))
  217. {
  218. StringCchPrintfExA(cursor, remaining, &cursor, &remaining, STRSAFE_NULL_ON_FAILURE, "%d", service->GetId());
  219. }
  220. }
  221. }
  222. }
  223. item.hItem = MLNavItem_GetNext(hLibrary, item.hItem);
  224. }
  225. }
  226. Config_WriteStr("Navigation", "order", buffer);
  227. Plugin_FreeAnsiString(buffer);
  228. return S_OK;
  229. }
  230. //static int __fastcall Navigation_OrderComparer(const void *elem1, const void *elem2, const void *context)
  231. //{
  232. // std::vector<UINT> *orderList = (std::vector<UINT>*)context;
  233. //
  234. // UINT serviceId;
  235. // size_t index1, index2;
  236. // size_t count = orderList->size();
  237. //
  238. // serviceId = (*(ifc_omservice**)elem1)->GetId();
  239. // for (index1 = 0; index1 < count && serviceId != orderList->at(index1); index1++);
  240. //
  241. // serviceId = (*(ifc_omservice**)elem2)->GetId();
  242. // for (index2 = 0; index2 < count && serviceId != orderList->at(index2); index2++);
  243. //
  244. // return (INT)(index1 - index2);
  245. //}
  246. class Navigation_OrderComparer
  247. {
  248. public:
  249. Navigation_OrderComparer(const void* ctx)
  250. : context(ctx)
  251. {
  252. }
  253. bool operator()(const void* elem1, const void* elem2)
  254. {
  255. std::vector<UINT>* orderList = (std::vector<UINT>*)context;
  256. UINT serviceId;
  257. size_t index1, index2;
  258. size_t count = orderList->size();
  259. serviceId = ((ifc_omservice*)elem1)->GetId();
  260. for (index1 = 0; index1 < count && serviceId != orderList->at(index1); index1++);
  261. serviceId = ((ifc_omservice*)elem2)->GetId();
  262. for (index2 = 0; index2 < count && serviceId != orderList->at(index2); index2++);
  263. return (INT)(index1 - index2) < 0;
  264. }
  265. private:
  266. const void* context;
  267. };
  268. HRESULT Navigation::Order(std::vector<ifc_omservice*> &list)
  269. {
  270. size_t listSize = list.size();
  271. if (listSize < 2)
  272. return S_FALSE;
  273. //if (NULL == list) return E_INVALIDARG;
  274. size_t bufferMax = 16384;
  275. LPSTR buffer = Plugin_MallocAnsiString(bufferMax);
  276. if (NULL == buffer) return E_OUTOFMEMORY;
  277. UINT len = Config_ReadStr("Navigation", "order", NULL, buffer, (UINT)bufferMax);
  278. std::vector<UINT> orderList;
  279. LPCSTR end = buffer + len;
  280. LPCSTR block = buffer;
  281. LPCSTR cursor = block;
  282. for(;;)
  283. {
  284. if (cursor == end || ';' == *cursor)
  285. {
  286. if (block != cursor)
  287. {
  288. INT serviceId;
  289. if (FALSE != StrToIntExA(block, STIF_DEFAULT, &serviceId))
  290. orderList.push_back(serviceId);
  291. }
  292. if (cursor == end) break;
  293. cursor++;
  294. block = cursor;
  295. }
  296. cursor++;
  297. }
  298. if (0 != orderList.size())
  299. {
  300. //nu::qsort(list, listSize, sizeof(ifc_omservice*), &orderList, Navigation_OrderComparer);
  301. std::sort(list.begin(), list.end(), Navigation_OrderComparer(&orderList));
  302. }
  303. Plugin_FreeAnsiString(buffer);
  304. return S_OK;
  305. }
  306. void Navigation::ImageChanged(LPCWSTR pszName, INT index)
  307. {
  308. if (NULL == hRoot || NULL == hLibrary || NULL == pszName)
  309. return;
  310. WCHAR szBuffer[2048] = {0};
  311. NAVITEM item = {0};
  312. item.cbSize = sizeof(item);
  313. item.mask = NIMF_TEXTINVARIANT | NIMF_PARAM;
  314. item.pszInvariant = szBuffer;
  315. item.cchInvariantMax = ARRAYSIZE(szBuffer);
  316. item.hItem = hRoot;
  317. while (NULL != item.hItem)
  318. {
  319. if (FALSE != MLNavItem_GetInfo(hLibrary, &item) &&
  320. FALSE != Navigation_CheckInvariantName(item.pszInvariant))
  321. {
  322. ifc_omservice *service = (ifc_omservice*)item.lParam;
  323. if (NULL != service &&
  324. SUCCEEDED(service->GetIcon(szBuffer, ARRAYSIZE(szBuffer))) &&
  325. CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, szBuffer, -1, pszName, -1))
  326. {
  327. item.iImage = index;
  328. item.iSelectedImage = index;
  329. item.mask = NIMF_IMAGE | NIMF_IMAGESEL;
  330. MLNavItem_SetInfo(hLibrary, &item);
  331. return;
  332. }
  333. }
  334. item.hItem = (HNAVITEM)SENDMLIPC(hLibrary,
  335. (item.hItem == hRoot) ? ML_IPC_NAVITEM_GETCHILD : ML_IPC_NAVITEM_GETNEXT,
  336. (WPARAM)item.hItem);
  337. }
  338. }
  339. BOOL Navigation::ProcessMessage(INT msg, INT_PTR param1, INT_PTR param2, INT_PTR param3, INT_PTR *result)
  340. {
  341. if (msg == ML_MSG_NO_CONFIG)
  342. {
  343. *result = TRUE;
  344. return TRUE;
  345. }
  346. if (msg < ML_MSG_TREE_BEGIN || msg > ML_MSG_TREE_END)
  347. return FALSE;
  348. HRESULT hr;
  349. switch(msg)
  350. {
  351. case ML_MSG_TREE_ONCREATEVIEW:
  352. {
  353. HWND hView;
  354. hr = OnCreateView(GetMessageItem(msg, param1), (HWND)param2, &hView);
  355. *result = (SUCCEEDED(hr)) ? (INT_PTR)hView : NULL;
  356. }
  357. return TRUE;
  358. case ML_MSG_NAVIGATION_CONTEXTMENU:
  359. hr = OnContextMenu(GetMessageItem(msg, param1), (HWND)param2, MAKEPOINTS(param3));
  360. *result = SUCCEEDED(hr);
  361. return TRUE;
  362. case ML_MSG_NAVIGATION_ONDELETE:
  363. hr = OnDeleteItem(GetMessageItem(msg, param1));
  364. *result = SUCCEEDED(hr);
  365. return TRUE;
  366. case ML_MSG_NAVIGATION_ONENDTITLEEDIT:
  367. hr = OnEndTitleEdit(GetMessageItem(msg, param1), (LPCWSTR)param2);
  368. *result = SUCCEEDED(hr);
  369. return TRUE;
  370. case ML_MSG_TREE_ONKEYDOWN:
  371. hr = OnKeyDown(GetMessageItem(msg, param1), (NMTVKEYDOWN*)param2);
  372. *result = SUCCEEDED(hr);
  373. return TRUE;
  374. case ML_MSG_NAVIGATION_ONDESTROY:
  375. OnControlDestroy();
  376. *result = 0;
  377. return TRUE;
  378. }
  379. return FALSE;
  380. }
  381. HNAVITEM Navigation::GetActive(ifc_omservice **serviceOut)
  382. {
  383. ifc_omservice *service;
  384. HNAVITEM hActive = (NULL != hLibrary) ? MLNavCtrl_GetSelection(hLibrary) : NULL;
  385. if (NULL == hActive || FAILED(GetService(hActive, &service)))
  386. {
  387. hActive = NULL;
  388. service = NULL;
  389. }
  390. if (NULL != serviceOut)
  391. *serviceOut = service;
  392. else if (NULL != service)
  393. service->Release();
  394. return hActive;
  395. }
  396. HWND Navigation::GetActiveView(ifc_omservice **serviceOut)
  397. {
  398. HWND hView = (NULL != hLibrary) ? ((HWND)SENDMLIPC(hLibrary, ML_IPC_GETCURRENTVIEW, 0)) : NULL;
  399. if (NULL != hView)
  400. {
  401. WCHAR szBuffer[128] = {0};
  402. if (!GetClassName(hView, szBuffer, ARRAYSIZE(szBuffer)) ||
  403. CSTR_EQUAL != CompareStringW(CSTR_INVARIANT, NORM_IGNORECASE, szBuffer, -1,
  404. L"Nullsoft_omBrowserView", -1))
  405. {
  406. hView = NULL;
  407. }
  408. }
  409. ifc_omservice *service;
  410. if (NULL == hView || FALSE == BrowserView_GetService(hView, &service))
  411. {
  412. hView = NULL;
  413. service = NULL;
  414. }
  415. if (NULL != serviceOut)
  416. *serviceOut = service;
  417. else if (NULL != service)
  418. service->Release();
  419. return hView;
  420. }
  421. HRESULT Navigation::SelectItem(HNAVITEM hItem, LPCWSTR pszUrl)
  422. {
  423. if (NULL == hItem) return E_INVALIDARG;
  424. ifc_omservice *service;
  425. HRESULT hr = GetService(hItem, &service);
  426. if (FAILED(hr)) return hr;
  427. hr = SelectItemInt(hItem, service->GetId(), pszUrl);
  428. service->Release();
  429. return hr;
  430. }
  431. HRESULT Navigation::DeleteItem(HNAVITEM hItem)
  432. {
  433. if (NULL == hItem) return E_INVALIDARG;
  434. if (NULL == hLibrary) return E_UNEXPECTED;
  435. ifc_omservice *service;
  436. if (FAILED(GetService(hItem, &service)))
  437. return E_FAIL;
  438. UINT serviceFlags;
  439. if (FAILED(service->GetFlags(&serviceFlags))) serviceFlags = 0;
  440. if (0 != ((WDSVCF_ROOT | WDSVCF_SPECIAL) & serviceFlags))
  441. return E_NOTIMPL;
  442. MLNavCtrl_BeginUpdate(hLibrary, 0);
  443. HNAVITEM hSelection = MLNavCtrl_GetSelection(hLibrary);
  444. if (hSelection == hItem)
  445. {
  446. HNAVITEM hNext = MLNavItem_GetNext(hLibrary, hItem);
  447. if (NULL == hNext)
  448. hNext = MLNavItem_GetPrevious(hLibrary, hItem);
  449. if (NULL != hNext)
  450. {
  451. MLNavItem_Select(hLibrary, hNext);
  452. }
  453. }
  454. BOOL result = MLNavCtrl_DeleteItem(hLibrary, hItem);
  455. HRESULT hr = (FALSE != result) ? S_OK : E_FAIL;
  456. MLNavCtrl_EndUpdate(hLibrary);
  457. if (SUCCEEDED(hr))
  458. ServiceHelper_Delete(service);
  459. service->Release();
  460. return hr;
  461. }
  462. HRESULT Navigation::DeleteAll()
  463. {
  464. if (NULL == hRoot || NULL == hLibrary) return E_UNEXPECTED;
  465. std::vector<HNAVITEM> itemList;
  466. HNAVITEM hItem = MLNavItem_GetChild(hLibrary, hRoot);
  467. while (NULL != hItem)
  468. {
  469. itemList.push_back(hItem);
  470. hItem = MLNavItem_GetNext(hLibrary, hItem);
  471. }
  472. MLNavCtrl_BeginUpdate(hLibrary, 0);
  473. NAVITEM item;
  474. item.cbSize = sizeof(item);
  475. item.mask = NIMF_PARAM;
  476. size_t index = itemList.size();
  477. while(index--)
  478. {
  479. item.hItem = itemList[index];
  480. if (FALSE != MLNavItem_GetInfo(hLibrary, &item))
  481. {
  482. ifc_omservice *service = (ifc_omservice*)item.lParam;
  483. if (NULL != service)
  484. {
  485. service->AddRef();
  486. UINT serviceFlags;
  487. if (SUCCEEDED(service->GetFlags(&serviceFlags)) &&
  488. 0 == ((WDSVCF_ROOT | WDSVCF_SPECIAL) & serviceFlags) &&
  489. FALSE != MLNavCtrl_DeleteItem(hLibrary, item.hItem))
  490. {
  491. ServiceHelper_Delete(service);
  492. }
  493. service->Release();
  494. }
  495. }
  496. }
  497. MLNavCtrl_EndUpdate(hLibrary);
  498. return S_OK;
  499. }
  500. HRESULT Navigation::CreatePopup(HNAVITEM hItem, HWND *hwnd)
  501. {
  502. if (NULL == hwnd) return E_POINTER;
  503. *hwnd = NULL;
  504. if (NULL == hLibrary) return E_UNEXPECTED;
  505. if (NULL == hItem) return E_INVALIDARG;
  506. HRESULT hr;
  507. ifc_omservice *service;
  508. hr = GetService(hItem, &service);
  509. if (SUCCEEDED(hr))
  510. {
  511. HWND hWinamp = Plugin_GetWinamp();
  512. if (NULL == OMBROWSERMNGR)
  513. hr = E_UNEXPECTED;
  514. if (SUCCEEDED(hr))
  515. {
  516. hr = OMBROWSERMNGR->Initialize(NULL, hWinamp);
  517. if (SUCCEEDED(hr))
  518. {
  519. RECT rect;
  520. HWND hFrame = (HWND)SENDMLIPC(hLibrary, ML_IPC_GETCURRENTVIEW, 0);
  521. if (NULL == hFrame) hFrame = hLibrary;
  522. if (NULL == hFrame || FALSE == GetWindowRect(hFrame, &rect))
  523. {
  524. hr = E_FAIL;
  525. }
  526. if (SUCCEEDED(hr))
  527. {
  528. rect.left += 16;
  529. rect.top += 16;
  530. hr = OMBROWSERMNGR->CreatePopup(service, rect.left, rect.top,
  531. rect.right - rect.left, rect.bottom - rect.top, hWinamp, NULL, 0, hwnd);
  532. }
  533. }
  534. }
  535. service->Release();
  536. }
  537. return hr;
  538. }
  539. HRESULT Navigation::GetService(HNAVITEM hItem, ifc_omservice **service)
  540. {
  541. WCHAR szBuffer[64] = {0};
  542. if (NULL == service) return E_POINTER;
  543. *service = NULL;
  544. if (NULL == hLibrary || NULL == hItem)
  545. return E_INVALIDARG;
  546. NAVITEM itemInfo;
  547. itemInfo.cbSize = sizeof(NAVITEM);
  548. itemInfo.hItem = hItem;
  549. itemInfo.pszInvariant = szBuffer;
  550. itemInfo.cchInvariantMax = ARRAYSIZE(szBuffer);
  551. itemInfo.mask = NIMF_PARAM | NIMF_TEXTINVARIANT;
  552. if (FALSE == MLNavItem_GetInfo(hLibrary, &itemInfo))
  553. return E_FAIL;
  554. if (FALSE == Navigation_CheckInvariantName(szBuffer))
  555. return E_NAVITEM_UNKNOWN;
  556. *service = (ifc_omservice*)itemInfo.lParam;
  557. (*service)->AddRef();
  558. return S_OK;
  559. }
  560. static void CALLBACK Navigtaion_UpdateServiceApc(Dispatchable *object, ULONG_PTR param1, ULONG_PTR param2)
  561. {
  562. Navigation *navigation = (Navigation*)object;
  563. if (NULL != navigation)
  564. {
  565. ifc_omservice *service = (ifc_omservice*)param1;
  566. navigation->UpdateService(service, (UINT)param2);
  567. if (NULL != service) service->Release();
  568. }
  569. }
  570. HRESULT Navigation::UpdateService(ifc_omservice *service, UINT modifiedFlags)
  571. {
  572. if (NULL == hLibrary) return E_UNEXPECTED;
  573. DWORD libraryTID = GetWindowThreadProcessId(hLibrary, NULL);
  574. DWORD currentTID = GetCurrentThreadId();
  575. if (libraryTID != currentTID)
  576. {
  577. if (NULL != OMUTILITY)
  578. {
  579. service->AddRef();
  580. if (FAILED(OMUTILITY->PostMainThreadCallback2(Navigtaion_UpdateServiceApc, this, (ULONG_PTR)service, (ULONG_PTR)modifiedFlags)))
  581. service->Release();
  582. }
  583. return E_PENDING;
  584. }
  585. HNAVITEM hItem = FindService(service->GetId(), NULL);
  586. if (NULL == hItem)
  587. return E_FAIL;
  588. NAVITEM itemInfo;
  589. itemInfo.cbSize = sizeof(NAVITEM);
  590. itemInfo.hItem = hItem;
  591. itemInfo.mask = NIMF_IMAGE;
  592. if (FALSE == MLNavItem_GetInfo(hLibrary, &itemInfo))
  593. itemInfo.iImage= -1;
  594. itemInfo.mask = 0;
  595. WCHAR szName[512] = {0};
  596. if (0 != (ifc_omserviceeditor::modifiedName & modifiedFlags) &&
  597. SUCCEEDED(service->GetName(szName, ARRAYSIZE(szName))))
  598. {
  599. itemInfo.mask |= NIMF_TEXT;
  600. itemInfo.pszText = szName;
  601. }
  602. if (0 != (ifc_omserviceeditor::modifiedIcon & modifiedFlags))
  603. {
  604. ifc_mlnavigationhelper *navHelper;
  605. if (SUCCEEDED(OMUTILITY->GetMlNavigationHelper(Plugin_GetLibrary(), &navHelper)))
  606. {
  607. INT iImage;
  608. WCHAR szIcon[1024] = {0};
  609. if (FAILED(service->GetIcon(szIcon, ARRAYSIZE(szIcon))) ||
  610. FAILED(navHelper->QueryIndex(szIcon, &iImage, NULL)))
  611. {
  612. iImage = -1;
  613. }
  614. if (itemInfo.iImage != iImage)
  615. {
  616. itemInfo.mask |= NIMF_IMAGE | NIMF_IMAGESEL;
  617. itemInfo.iImage = iImage;
  618. itemInfo.iSelectedImage = iImage;
  619. }
  620. navHelper->Release();
  621. }
  622. }
  623. if (0 != itemInfo.mask)
  624. {
  625. if (FALSE == MLNavItem_SetInfo(hLibrary, &itemInfo))
  626. return E_FAIL;
  627. }
  628. NAVITEMINAVLIDATE invalidate;
  629. invalidate.hItem = hItem;
  630. invalidate.fErase = FALSE;
  631. invalidate.prc = NULL;
  632. MLNavItem_Invalidate(hLibrary, &invalidate);
  633. return S_OK;
  634. }
  635. HNAVITEM Navigation::FindService(UINT serviceId, ifc_omservice **serviceOut)
  636. {
  637. if (NULL == hRoot || NULL == hLibrary)
  638. {
  639. if (NULL != serviceOut) *serviceOut = NULL;
  640. return NULL;
  641. }
  642. WCHAR szBuffer[128] = {0};
  643. NAVITEM item = {0};
  644. item.cbSize = sizeof(item);
  645. item.mask = NIMF_TEXTINVARIANT | NIMF_PARAM;
  646. item.pszInvariant = szBuffer;
  647. item.cchInvariantMax = ARRAYSIZE(szBuffer);
  648. item.hItem = hRoot;
  649. while (NULL != item.hItem)
  650. {
  651. if (FALSE != MLNavItem_GetInfo(hLibrary, &item) &&
  652. FALSE != Navigation_CheckInvariantName(item.pszInvariant))
  653. {
  654. ifc_omservice *service = (ifc_omservice*)item.lParam;
  655. if (NULL != service && serviceId == service->GetId())
  656. {
  657. if (NULL != serviceOut)
  658. {
  659. *serviceOut = service;
  660. service->AddRef();
  661. }
  662. return item.hItem;
  663. }
  664. }
  665. item.hItem = (HNAVITEM)SENDMLIPC(hLibrary,
  666. (item.hItem == hRoot) ? ML_IPC_NAVITEM_GETCHILD : ML_IPC_NAVITEM_GETNEXT,
  667. (WPARAM)item.hItem);
  668. }
  669. if (NULL != serviceOut) *serviceOut = NULL;
  670. return NULL;
  671. }
  672. HRESULT Navigation::ShowService(UINT serviceId, LPCWSTR pszUrl)
  673. {
  674. ifc_omservice *service;
  675. HNAVITEM hItem = FindService(serviceId, &service);
  676. if (NULL == hItem) return E_FAIL;
  677. HRESULT hr = SelectItemInt(hItem, serviceId, pszUrl);
  678. service->Release();
  679. return hr;
  680. }
  681. HNAVITEM Navigation::CreateItem(ifc_omservice *service)
  682. {
  683. if (NULL == hLibrary || NULL == hRoot) return NULL;
  684. return CreateItemInt(hRoot, service);
  685. }
  686. HRESULT Navigation::GenerateServiceName(LPWSTR pszBuffer, INT cchBufferMax)
  687. {
  688. if (NULL == pszBuffer) return E_POINTER;
  689. *pszBuffer = L'\0';
  690. if (NULL == hLibrary || NULL == hRoot) return E_UNEXPECTED;
  691. if (FAILED(Plugin_CopyResString(pszBuffer, cchBufferMax, MAKEINTRESOURCE(IDS_USERSERVICE_NAME))))
  692. return E_UNEXPECTED;
  693. INT cchName = lstrlen(pszBuffer);
  694. LPWSTR pszFormat = pszBuffer + cchName;
  695. INT cchFormatMax = cchBufferMax - cchName;
  696. WCHAR szText[512] = {0};
  697. NAVITEM item = {0};
  698. item.cbSize = sizeof(item);
  699. item.mask = NIMF_TEXT;
  700. item.pszText = szText;
  701. item.cchTextMax = ARRAYSIZE(szText);
  702. BOOL fFound = TRUE;
  703. for(INT index = 1; FALSE != fFound; index++)
  704. {
  705. fFound = FALSE;
  706. if (FAILED(StringCchPrintf(pszFormat, cchFormatMax, L" %d", index)))
  707. {
  708. pszFormat = L'\0';
  709. return E_FAIL;
  710. }
  711. item.hItem = MLNavItem_GetChild(hLibrary, hRoot);
  712. while(NULL != item.hItem)
  713. {
  714. if (FALSE != MLNavItem_GetInfo(hLibrary, &item) &&
  715. CSTR_EQUAL == CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, item.pszText, -1, pszBuffer, -1))
  716. {
  717. fFound = TRUE;
  718. break;
  719. }
  720. item.hItem = MLNavItem_GetNext(hLibrary, item.hItem);
  721. }
  722. }
  723. return S_OK;
  724. }
  725. HRESULT Navigation::CreateUserService(HNAVITEM *itemOut)
  726. {
  727. HRESULT hr;
  728. if (NULL != itemOut)
  729. *itemOut = NULL;
  730. if (NULL == hRoot) return E_FAIL;
  731. INT serviceId = 710;
  732. while(NULL != FindService(serviceId, NULL)) serviceId++;
  733. WCHAR szName[256];
  734. if (FAILED(GenerateServiceName(szName, ARRAYSIZE(szName))))
  735. return E_FAIL;
  736. ifc_omservice *service;
  737. hr = ServiceHelper_Create(serviceId, szName, NULL, L"about:blank", 0, TRUE, &service);
  738. if (SUCCEEDED(hr))
  739. {
  740. HNAVITEM hItem = CreateItem(service);
  741. if (NULL == hItem)
  742. {
  743. hr = E_FAIL;
  744. }
  745. else
  746. {
  747. if (NULL != itemOut)
  748. *itemOut = hItem;
  749. }
  750. service->Release();
  751. }
  752. return hr;
  753. }
  754. HNAVITEM Navigation::CreateItemInt(HNAVITEM hParent, ifc_omservice *service)
  755. {
  756. if (NULL == service) return NULL;
  757. ServiceHelper_RegisterPreAuthorized(service);
  758. WCHAR szName[256] = {0}, szInvariant[64] = {0};
  759. if (FAILED(service->GetName(szName, ARRAYSIZE(szName))))
  760. return NULL;
  761. if (L'\0' == szName[0])
  762. WASABI_API_LNGSTRINGW_BUF(IDS_UNKNOWNNAME, szName, ARRAYSIZE(szName));
  763. if (FAILED(StringCchPrintf(szInvariant, ARRAYSIZE(szInvariant), NAVITEM_PREFIX L"%u", service->GetId())))
  764. return NULL;
  765. NAVINSERTSTRUCT nis = {0};
  766. nis.hInsertAfter = NULL;
  767. nis.hParent = hParent;
  768. INT iIcon = -1;
  769. ifc_mlnavigationhelper *navHelper;
  770. if (NULL != OMUTILITY && SUCCEEDED(OMUTILITY->GetMlNavigationHelper(Plugin_GetLibrary(), &navHelper)))
  771. {
  772. WCHAR szIcon[2048] = {0};
  773. if (FAILED(service->GetIcon(szIcon, ARRAYSIZE(szIcon))) ||
  774. FAILED(navHelper->QueryIndex(szIcon, &iIcon, NULL)))
  775. {
  776. iIcon = -1;
  777. }
  778. navHelper->Release();
  779. }
  780. nis.item.cbSize = sizeof(NAVITEM);
  781. nis.item.mask = NIMF_TEXT | NIMF_STYLE | NIMF_TEXTINVARIANT | NIMF_PARAM | NIMF_IMAGE | NIMF_IMAGESEL;
  782. nis.item.id = 0;
  783. nis.item.pszText = szName;
  784. nis.item.pszInvariant = szInvariant;
  785. nis.item.lParam = (LPARAM)service;
  786. nis.item.style = 0;
  787. UINT serviceFlags;
  788. if (FAILED(service->GetFlags(&serviceFlags)))
  789. serviceFlags = 0;
  790. if (0 != (WDSVCF_ROOT & serviceFlags))
  791. {
  792. nis.item.style |= (NIS_HASCHILDREN | NIS_ALLOWCHILDMOVE);
  793. iIcon = -1;
  794. }
  795. else if (0 != (WDSVCF_SPECIAL & serviceFlags))
  796. nis.item.style |= NIS_ITALIC;
  797. else
  798. nis.item.style |= NIS_ALLOWEDIT;
  799. nis.item.styleMask = nis.item.style;
  800. nis.item.iImage = iIcon;
  801. nis.item.iSelectedImage = iIcon;
  802. HNAVITEM hItem = MLNavCtrl_InsertItem(hLibrary, &nis);
  803. if (NULL != hItem)
  804. {
  805. WebDevServiceHost *serviceHost;
  806. if (SUCCEEDED(WebDevServiceHost::GetCachedInstance(&serviceHost)))
  807. {
  808. ifc_omserviceeventmngr *eventManager;
  809. if (SUCCEEDED(service->QueryInterface(IFC_OmServiceEventMngr, (void**)&eventManager)))
  810. {
  811. eventManager->RegisterHandler(serviceHost);
  812. eventManager->Release();
  813. }
  814. serviceHost->Release();
  815. }
  816. service->AddRef();
  817. }
  818. return hItem;
  819. }
  820. HRESULT Navigation::SelectItemInt(HNAVITEM hItem, UINT serviceId, LPCWSTR pszUrl)
  821. {
  822. if (NULL == hLibrary) return E_UNEXPECTED;
  823. if (NULL != pszUrl && L'\0' != *pszUrl)
  824. {
  825. HRESULT hr = ForceUrl_Set(serviceId, pszUrl);
  826. if (FAILED(hr)) return hr;
  827. }
  828. else
  829. {
  830. ForceUrl_Remove();
  831. }
  832. if (FALSE == MLNavItem_Select(hLibrary, hItem))
  833. {
  834. ForceUrl_Remove();
  835. return E_FAIL;
  836. }
  837. return S_OK;
  838. }
  839. HNAVITEM Navigation::GetMessageItem(INT msg, INT_PTR param1)
  840. {
  841. return (msg < ML_MSG_NAVIGATION_FIRST) ?
  842. MLNavCtrl_FindItemById(hLibrary, param1) :
  843. (HNAVITEM)param1;
  844. }
  845. HRESULT Navigation::OnCreateView(HNAVITEM hItem, HWND hParent, HWND *hView)
  846. {
  847. if (NULL == hView) return E_POINTER;
  848. *hView = NULL;
  849. if (NULL == hLibrary) return E_UNEXPECTED;
  850. if (NULL == hItem || NULL == hParent) return E_INVALIDARG;
  851. HRESULT hr;
  852. ifc_omservice *service;
  853. hr = GetService(hItem, &service);
  854. if (SUCCEEDED(hr))
  855. {
  856. if (NULL == OMBROWSERMNGR)
  857. hr = E_UNEXPECTED;
  858. if (SUCCEEDED(hr))
  859. {
  860. hr = OMBROWSERMNGR->Initialize(NULL, Plugin_GetWinamp());
  861. if (SUCCEEDED(hr))
  862. {
  863. LPCWSTR forceUrl;
  864. if (FAILED(ForceUrl_Get(service->GetId(), &forceUrl)))
  865. forceUrl = NULL;
  866. hr = OMBROWSERMNGR->CreateView(service, hParent, forceUrl, 0, hView);
  867. ForceUrl_Remove();
  868. }
  869. }
  870. service->Release();
  871. }
  872. return hr;
  873. }
  874. HRESULT Navigation::OnContextMenu(HNAVITEM hItem, HWND hHost, POINTS pts)
  875. {
  876. if (NULL == hItem || NULL == hHost)
  877. return E_INVALIDARG;
  878. HWND hLibrary = Plugin_GetLibrary();
  879. if (NULL == hLibrary) return E_UNEXPECTED;
  880. HRESULT hr;
  881. ifc_omservice *service;
  882. hr = GetService(hItem, &service);
  883. if (FAILED(hr)) return hr;
  884. POINT pt;
  885. POINTSTOPOINT(pt, pts);
  886. if (-1 == pt.x || -1 == pt.y)
  887. {
  888. NAVITEMGETRECT itemRect;
  889. itemRect.fItem = FALSE;
  890. itemRect.hItem = hItem;
  891. if (MLNavItem_GetRect(hLibrary, &itemRect))
  892. {
  893. MapWindowPoints(hHost, HWND_DESKTOP, (POINT*)&itemRect.rc, 2);
  894. pt.x = itemRect.rc.left + 2;
  895. pt.y = itemRect.rc.top + 2;
  896. }
  897. }
  898. HMENU hMenu = Menu_GetMenu(MENU_SERVICECONTEXT, service);
  899. if (NULL != hMenu)
  900. {
  901. INT commandId = Menu_TrackPopup(Plugin_GetLibrary(), hMenu,
  902. TPM_LEFTALIGN | TPM_TOPALIGN | TPM_NONOTIFY | TPM_RETURNCMD,
  903. pt.x, pt.y, hHost, NULL);
  904. Menu_ReleaseMenu(hMenu, MENU_SERVICECONTEXT);
  905. CommandManager_Process(hItem, service, commandId);
  906. }
  907. service->Release();
  908. return hr;
  909. }
  910. HRESULT Navigation::OnEndTitleEdit(HNAVITEM hItem, LPCWSTR pszNewTitle)
  911. {
  912. if (NULL == hItem) return E_INVALIDARG;
  913. if (NULL == hLibrary) return E_UNEXPECTED;
  914. HRESULT hr;
  915. ifc_omservice *service;
  916. hr = GetService(hItem, &service);
  917. if (SUCCEEDED(hr))
  918. {
  919. if (NULL != pszNewTitle)
  920. {
  921. ifc_omserviceeditor *editor;
  922. hr = service->QueryInterface(IFC_OmServiceEditor, (void**)&editor);
  923. if (SUCCEEDED(hr))
  924. {
  925. hr = editor->SetName(pszNewTitle, FALSE);
  926. editor->Release();
  927. }
  928. }
  929. if (SUCCEEDED(hr))
  930. ServiceHelper_Save(service);
  931. service->Release();
  932. }
  933. return hr;
  934. }
  935. HRESULT Navigation::OnDeleteItem(HNAVITEM hItem)
  936. {
  937. if (NULL == hItem) return E_INVALIDARG;
  938. if (NULL == hLibrary) return E_UNEXPECTED;
  939. WCHAR szBuffer[2048] = {0};
  940. NAVITEM itemInfo = {0};
  941. itemInfo.cbSize = sizeof(itemInfo);
  942. itemInfo.hItem = hItem;
  943. itemInfo.pszInvariant = szBuffer;
  944. itemInfo.cchInvariantMax = ARRAYSIZE(szBuffer);
  945. itemInfo.mask = NIMF_PARAM | NIMF_TEXTINVARIANT | NIMF_IMAGE;
  946. if (FALSE == MLNavItem_GetInfo(hLibrary, &itemInfo))
  947. return E_FAIL;
  948. if (FALSE == Navigation_CheckInvariantName(szBuffer))
  949. return E_NAVITEM_UNKNOWN;
  950. ifc_omservice *service = (ifc_omservice*)itemInfo.lParam;
  951. if (NULL != service)
  952. {
  953. if (SUCCEEDED(service->GetIcon(szBuffer, ARRAYSIZE(szBuffer))))
  954. {
  955. ifc_mlnavigationhelper *navHelper;
  956. if (SUCCEEDED(OMUTILITY->GetMlNavigationHelper(Plugin_GetLibrary(), &navHelper)))
  957. {
  958. navHelper->ReleaseIndex(szBuffer);
  959. navHelper->Release();
  960. }
  961. }
  962. ifc_ombrowserwndmngr *windowManager;
  963. if (NULL != OMBROWSERMNGR && SUCCEEDED(OMBROWSERMNGR->QueryInterface(IFC_OmBrowserWindowManager, (void**)&windowManager)))
  964. {
  965. UINT serviceId = service->GetId();
  966. ifc_ombrowserwndenum *windowEnum;
  967. if (SUCCEEDED(windowManager->Enumerate(NULL, &serviceId, &windowEnum)))
  968. {
  969. HWND hwnd;
  970. while (S_OK == windowEnum->Next(1, &hwnd, NULL))
  971. {
  972. DestroyWindow(hwnd);
  973. }
  974. windowEnum->Release();
  975. }
  976. windowManager->Release();
  977. }
  978. WebDevServiceHost *serviceHost;
  979. if (SUCCEEDED(WebDevServiceHost::GetCachedInstance(&serviceHost)))
  980. {
  981. ifc_omserviceeventmngr *eventManager;
  982. if (SUCCEEDED(service->QueryInterface(IFC_OmServiceEventMngr, (void**)&eventManager)))
  983. {
  984. eventManager->UnregisterHandler(serviceHost);
  985. eventManager->Release();
  986. }
  987. serviceHost->Release();
  988. }
  989. itemInfo.mask = NIMF_PARAM;
  990. itemInfo.lParam = 0L;
  991. MLNavItem_SetInfo(hLibrary, &itemInfo);
  992. service->Release();
  993. }
  994. return S_OK;
  995. }
  996. HRESULT Navigation::OnKeyDown(HNAVITEM hItem, NMTVKEYDOWN *pnmkd)
  997. {
  998. if (NULL == hItem) return E_INVALIDARG;
  999. if (NULL == hLibrary) return E_UNEXPECTED;
  1000. ifc_omservice *service;
  1001. HRESULT hr = GetService(hItem, &service);
  1002. if (SUCCEEDED(hr))
  1003. {
  1004. switch(pnmkd->wVKey)
  1005. {
  1006. case VK_DELETE:
  1007. Command_DeleteItem(hItem);
  1008. break;
  1009. }
  1010. service->Release();
  1011. }
  1012. return hr;
  1013. }
  1014. HRESULT Navigation::OnControlDestroy()
  1015. {
  1016. SaveOrder();
  1017. return S_OK;
  1018. }
  1019. #define CBCLASS Navigation
  1020. START_DISPATCH;
  1021. CB(ADDREF, AddRef)
  1022. CB(RELEASE, Release)
  1023. CB(QUERYINTERFACE, QueryInterface)
  1024. VCB(API_IMAGECHANGED, ImageChanged)
  1025. END_DISPATCH;
  1026. #undef CBCLASS