navigation.cpp 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. #include "main.h"
  2. #include "./navigation.h"
  3. #include <strsafe.h>
  4. #define DEVICES_NAVITEM_PREFIX L"ml_devices_"
  5. static HNAVITEM navigationRoot = NULL;
  6. static size_t deviceHandler = 0;
  7. static HNAVITEM
  8. NavigationItem_Insert(HNAVITEM parent,
  9. HNAVITEM insertAfter,
  10. unsigned int mask,
  11. int itemId,
  12. const wchar_t *name,
  13. const wchar_t *displayName,
  14. unsigned int style,
  15. int image,
  16. LPARAM param)
  17. {
  18. NAVITEM *item;
  19. NAVINSERTSTRUCT nis = {0};
  20. nis.hInsertAfter = insertAfter;
  21. nis.hParent = parent;
  22. item = &nis.item;
  23. item->cbSize = sizeof(NAVITEM);
  24. item->id = itemId;
  25. if (0 != (NIMF_IMAGE & mask))
  26. mask |= NIMF_IMAGESEL;
  27. item->mask = mask;
  28. item->pszText = (wchar_t*)displayName;
  29. item->pszInvariant = (wchar_t*)name;
  30. item->style = style;
  31. item->styleMask = style;
  32. item->iImage = image;
  33. item->iSelectedImage = image;
  34. item->lParam = param;
  35. if (!wcsnicmp(L"ml_devices_all_sources", item->pszInvariant, 22))
  36. {
  37. // and this will allow us to make the cloud library
  38. // root do a web page or a sources view as needed...
  39. return NULL;
  40. }
  41. return MLNavCtrl_InsertItem(Plugin_GetLibraryWindow(), &nis);
  42. }
  43. static BOOL
  44. NavigationItem_Delete(HNAVITEM item)
  45. {
  46. return MLNavCtrl_DeleteItem(Plugin_GetLibraryWindow(), item);
  47. }
  48. static HNAVITEM
  49. NavigationItem_GetFromMessage(INT msg, INT_PTR param)
  50. {
  51. return (msg < ML_MSG_NAVIGATION_FIRST) ?
  52. MLNavCtrl_FindItemById(Plugin_GetLibraryWindow(), param) :
  53. (HNAVITEM)param;
  54. }
  55. static HNAVITEM
  56. NavigationItem_Find(HNAVITEM root, const wchar_t *name, BOOL allow_root = 0)
  57. {
  58. NAVCTRLFINDPARAMS find = {0};
  59. HNAVITEM item;
  60. HWND libraryWindow;
  61. if (NULL == name)
  62. return NULL;
  63. libraryWindow = Plugin_GetLibraryWindow();
  64. if (NULL == libraryWindow)
  65. return NULL;
  66. find.pszName = (wchar_t*)name;
  67. find.cchLength = -1;
  68. find.compFlags = NICF_INVARIANT;
  69. find.fFullNameSearch = FALSE;
  70. item = MLNavCtrl_FindItemByName(libraryWindow, &find);
  71. if (NULL == item)
  72. return NULL;
  73. if (!allow_root)
  74. {
  75. // if allowed then we can look for root level items which
  76. // is really for getting 'cloud' devices to another group
  77. if (NULL != root &&
  78. root != MLNavItem_GetParent(libraryWindow, item))
  79. {
  80. item = NULL;
  81. }
  82. }
  83. return item;
  84. }
  85. static wchar_t *
  86. NavigationItem_GetNameFromDeviceName(wchar_t *buffer, size_t bufferMax, const char *name)
  87. {
  88. wchar_t *cursor;
  89. size_t length;
  90. BOOL allocated;
  91. if (NULL == name || '\0' == *name)
  92. return NULL;
  93. length = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
  94. if (ARRAYSIZE(DEVICES_NAVITEM_PREFIX) > bufferMax ||
  95. length > (bufferMax - (ARRAYSIZE(DEVICES_NAVITEM_PREFIX) - 1)))
  96. {
  97. bufferMax = length + ARRAYSIZE(DEVICES_NAVITEM_PREFIX) - 1;
  98. buffer = String_Malloc(bufferMax);
  99. if (NULL == buffer)
  100. return NULL;
  101. allocated = TRUE;
  102. }
  103. else
  104. allocated = FALSE;
  105. if (FAILED(StringCchCopyEx(buffer, bufferMax, DEVICES_NAVITEM_PREFIX, &cursor, &length, 0)) ||
  106. 0 == MultiByteToWideChar(CP_UTF8, 0, name, -1, cursor, (int)length))
  107. {
  108. if (FALSE != allocated)
  109. String_Free(buffer);
  110. return NULL;
  111. }
  112. return buffer;
  113. }
  114. static HNAVITEM
  115. NavigationItem_FindFromDeviceName(HNAVITEM root, const char *name)
  116. {
  117. wchar_t buffer[1], *itemName;
  118. HNAVITEM item;
  119. if (NULL == root)
  120. return NULL;
  121. itemName = NavigationItem_GetNameFromDeviceName(buffer, ARRAYSIZE(buffer), name);
  122. if (NULL == itemName)
  123. return NULL;
  124. item = NavigationItem_Find(root, itemName);
  125. if (itemName != buffer)
  126. String_Free(itemName);
  127. return item;
  128. }
  129. static HNAVITEM
  130. NavigationItem_FindFromDevice(HNAVITEM root, ifc_device *device)
  131. {
  132. if (NULL == device)
  133. return NULL;
  134. return NavigationItem_FindFromDeviceName(root, device->GetName());
  135. }
  136. static BOOL
  137. NavigationItem_DisplayDeviceMenu(HNAVITEM item, const char *deviceName, HWND hostWindow, POINT pt)
  138. {
  139. HMENU menu;
  140. ifc_device *device;
  141. unsigned int commandId;
  142. BOOL succeeded;
  143. HWND libraryWindow;
  144. libraryWindow = Plugin_GetLibraryWindow();
  145. if (NULL == item|| NULL == deviceName)
  146. return FALSE;
  147. if (NULL == WASABI_API_DEVICES ||
  148. S_OK != WASABI_API_DEVICES->DeviceFind(deviceName, &device))
  149. {
  150. return FALSE;
  151. }
  152. menu = CreatePopupMenu();
  153. if (NULL != menu)
  154. {
  155. if (0 == Menu_InsertDeviceItems(menu, 0, 100, device, DeviceCommandContext_NavigationMenu))
  156. {
  157. DestroyMenu(menu);
  158. menu = NULL;
  159. }
  160. }
  161. device->Release();
  162. if (NULL == menu)
  163. return FALSE;
  164. succeeded = FALSE;
  165. if (item == MLNavCtrl_GetSelection(libraryWindow))
  166. {
  167. commandId = Menu_FindItemByAnsiStringData(menu, "view_open");
  168. if ((unsigned int)-1 != commandId)
  169. EnableMenuItem(menu, commandId, MF_BYCOMMAND | MF_DISABLED);
  170. }
  171. commandId = Menu_TrackPopup(Plugin_GetLibraryWindow(), menu,
  172. TPM_LEFTALIGN | TPM_TOPALIGN | TPM_VERPOSANIMATION | TPM_VERTICAL | TPM_RETURNCMD,
  173. pt.x, pt.y, hostWindow, NULL);
  174. if (0 != commandId)
  175. {
  176. const char *command;
  177. command = (const char*)Menu_GetItemData(menu, commandId, FALSE);
  178. if (NULL != command)
  179. {
  180. if (NULL != WASABI_API_DEVICES &&
  181. S_OK == WASABI_API_DEVICES->DeviceFind(deviceName, &device))
  182. {
  183. BOOL commandProcessed;
  184. commandProcessed = FALSE;
  185. if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, 0, command, -1, "view_open", -1))
  186. {
  187. succeeded = MLNavItem_Select(libraryWindow, item);
  188. commandProcessed = succeeded;
  189. }
  190. else if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, 0, command, -1, "rename", -1))
  191. {
  192. succeeded = MLNavItem_EditTitle(libraryWindow, item);
  193. commandProcessed = succeeded;
  194. }
  195. if (FALSE == commandProcessed &&
  196. SUCCEEDED(device->SendCommand(command, hostWindow, 0)))
  197. {
  198. succeeded = TRUE;
  199. }
  200. device->Release();
  201. }
  202. }
  203. }
  204. else
  205. {
  206. if (ERROR_SUCCESS == GetLastError())
  207. succeeded = TRUE;
  208. }
  209. Menu_FreeItemData(menu, 0, -1);
  210. return succeeded;
  211. }
  212. static BOOL
  213. NavigationItem_DisplayRootMenu(HNAVITEM item, HWND hostWindow, POINT pt)
  214. {
  215. HMENU pluginMenu, menu;
  216. BOOL succeeded;
  217. if (NULL == item)
  218. return FALSE;
  219. pluginMenu = Plugin_LoadMenu();
  220. if (NULL == pluginMenu)
  221. return FALSE;
  222. succeeded = FALSE;
  223. menu = GetSubMenu(pluginMenu, 0);
  224. if (NULL != menu)
  225. {
  226. HWND libraryWindow;
  227. HNAVITEM selectedItem;
  228. libraryWindow = Plugin_GetLibraryWindow();
  229. selectedItem = MLNavCtrl_GetSelection(libraryWindow);
  230. EnableMenuItem(menu, ID_VIEW_OPEN, MF_BYCOMMAND | ((item != selectedItem) ? MF_ENABLED : MF_DISABLED));
  231. SetMenuDefaultItem(menu, ID_VIEW_OPEN, FALSE);
  232. unsigned int commandId = Menu_TrackPopup(Plugin_GetLibraryWindow(), menu,
  233. TPM_LEFTALIGN | TPM_TOPALIGN | TPM_VERPOSANIMATION | TPM_VERTICAL | TPM_RETURNCMD,
  234. pt.x, pt.y, hostWindow, NULL);
  235. if (0 != commandId)
  236. {
  237. switch(commandId)
  238. {
  239. case ID_VIEW_OPEN:
  240. MLNavItem_Select(libraryWindow, item);
  241. break;
  242. case ID_DISCOVERY_BEGIN:
  243. Plugin_BeginDiscovery();
  244. break;
  245. case ID_PLUGIN_HELP:
  246. Plugin_ShowHelp();
  247. break;
  248. }
  249. }
  250. else
  251. {
  252. if (ERROR_SUCCESS == GetLastError())
  253. succeeded = TRUE;
  254. }
  255. }
  256. DestroyMenu(pluginMenu);
  257. return succeeded;
  258. }
  259. static BOOL
  260. Navigation_GetDeviceTitle(ifc_device *device, wchar_t *buffer, size_t bufferMax)
  261. {
  262. size_t read, write;
  263. if (NULL == device ||
  264. FAILED(device->GetDisplayName(buffer, bufferMax)))
  265. {
  266. return FALSE;
  267. }
  268. for(read = 0, write = 0;; read++)
  269. {
  270. if (read == bufferMax)
  271. return FALSE;
  272. if (L'\r' == buffer[read])
  273. continue;
  274. if (L'\n' == buffer[read] ||
  275. L'\t' == buffer[read] ||
  276. L'\b' == buffer[read])
  277. {
  278. buffer[write] = L' ';
  279. }
  280. buffer[write] = buffer[read];
  281. if (L'\0' == buffer[read])
  282. break;
  283. write++;
  284. }
  285. return TRUE;
  286. }
  287. static BOOL
  288. Navigation_DeviceAdd(HNAVITEM root, ifc_device *device)
  289. {
  290. HNAVITEM item, insertAfter = NCI_LAST;
  291. wchar_t nameBuffer[256] = {0}, *itemName;
  292. wchar_t title[512] = {0};
  293. int iconIndex;
  294. if (NULL == device)
  295. return FALSE;
  296. itemName = NavigationItem_GetNameFromDeviceName(nameBuffer, ARRAYSIZE(nameBuffer), device->GetName());
  297. if (NULL == itemName)
  298. return FALSE;
  299. if (NULL != NavigationItem_Find(root, itemName))
  300. {
  301. if (itemName != nameBuffer)
  302. String_Free(itemName);
  303. return FALSE;
  304. }
  305. if (FALSE == Navigation_GetDeviceTitle(device, title, ARRAYSIZE(title)))
  306. title[0] = L'\0';
  307. iconIndex = NavigationIcons_GetDeviceIconIndex(device);
  308. // filter the cloud devices to their own group
  309. if (!lstrcmpiA(device->GetConnection(), "cloud"))
  310. {
  311. HNAVITEM cloud = NavigationItem_Find(navigationRoot, L"cloud_sources", TRUE);
  312. if (cloud != NULL)
  313. {
  314. root = cloud;
  315. HNAVITEM transfers = NavigationItem_Find(0, L"cloud_transfers", TRUE);
  316. // to maintain some specific orders, we need to alter the insert position
  317. if (!wcsnicmp(L"ml_devices_hss", itemName, 14))
  318. {
  319. insertAfter = transfers;
  320. if (!insertAfter) insertAfter = NCI_LAST;
  321. }
  322. else if (!wcsnicmp(L"ml_devices_local_desktop", itemName, 24))
  323. {
  324. insertAfter = NavigationItem_Find(0, L"ml_devices_hss", TRUE);
  325. if (!insertAfter) insertAfter = NCI_LAST;
  326. }
  327. // when adding children, change from the cloud source to the open/closed arrow
  328. HWND libraryWindow = Plugin_GetLibraryWindow();
  329. if (NULL != libraryWindow && transfers)
  330. {
  331. NAVITEM itemInfo = {0};
  332. itemInfo.hItem = cloud;
  333. itemInfo.cbSize = sizeof(itemInfo);
  334. itemInfo.mask = NIMF_IMAGE | NIMF_IMAGESEL;
  335. itemInfo.iImage = itemInfo.iSelectedImage = -1;
  336. MLNavItem_SetInfo(libraryWindow, &itemInfo);
  337. }
  338. }
  339. }
  340. item = NavigationItem_Insert(root, insertAfter,
  341. NIMF_TEXT | NIMF_TEXTINVARIANT | NIMF_IMAGE | NIMF_IMAGESEL | NIMF_STYLE,
  342. 0, itemName, title, NIS_ALLOWEDIT, iconIndex, 0L);
  343. if (NULL == item)
  344. NavigationIcons_ReleaseIconIndex(iconIndex);
  345. if (itemName != nameBuffer)
  346. String_Free(itemName);
  347. if (NULL == item)
  348. return FALSE;
  349. device->SetNavigationItem(item);
  350. return TRUE;
  351. }
  352. static BOOL
  353. Navigation_DeviceRemove(HNAVITEM root, ifc_device *device)
  354. {
  355. HNAVITEM item;
  356. item = NavigationItem_FindFromDevice(root, device);
  357. if (NULL == item)
  358. return FALSE;
  359. return NavigationItem_Delete(item);
  360. }
  361. static BOOL
  362. Navigation_DeviceTitleChanged(HNAVITEM root, ifc_device *device)
  363. {
  364. NAVITEM itemInfo;
  365. wchar_t buffer[1024] = {0};
  366. itemInfo.hItem = NavigationItem_FindFromDevice(root, device);
  367. if (NULL == itemInfo.hItem)
  368. return FALSE;
  369. if (FALSE == Navigation_GetDeviceTitle(device, buffer, ARRAYSIZE(buffer)))
  370. return FALSE;
  371. itemInfo.cbSize = sizeof(itemInfo);
  372. itemInfo.pszText = buffer;
  373. itemInfo.cchTextMax = -1;
  374. itemInfo.mask = NIMF_TEXT;
  375. return MLNavItem_SetInfo(Plugin_GetLibraryWindow(), &itemInfo);
  376. }
  377. static BOOL
  378. Navigation_DeviceIconChanged(HNAVITEM root, ifc_device *device)
  379. {
  380. NAVITEM itemInfo;
  381. int iconIndex;
  382. HWND libraryWindow;
  383. if (NULL == root || NULL == device)
  384. return FALSE;
  385. libraryWindow = Plugin_GetLibraryWindow();
  386. itemInfo.hItem = NavigationItem_FindFromDevice(root, device);
  387. if (NULL == itemInfo.hItem)
  388. return FALSE;
  389. iconIndex = NavigationIcons_GetDeviceIconIndex(device);
  390. itemInfo.cbSize = sizeof(itemInfo);
  391. itemInfo.mask = NIMF_IMAGE;
  392. if (FALSE == MLNavItem_GetInfo(libraryWindow, &itemInfo))
  393. {
  394. NavigationIcons_ReleaseIconIndex(iconIndex);
  395. return FALSE;
  396. }
  397. if (itemInfo.iImage == iconIndex)
  398. {
  399. NavigationIcons_ReleaseIconIndex(iconIndex);
  400. return TRUE;
  401. }
  402. NavigationIcons_ReleaseIconIndex(itemInfo.iImage);
  403. itemInfo.mask = NIMF_IMAGE | NIMF_IMAGESEL;
  404. itemInfo.iImage = iconIndex;
  405. itemInfo.iSelectedImage = iconIndex;
  406. if (FALSE == MLNavItem_SetInfo(libraryWindow, &itemInfo))
  407. {
  408. NavigationIcons_ReleaseIconIndex(iconIndex);
  409. return FALSE;
  410. }
  411. return TRUE;
  412. }
  413. static void
  414. Navigation_AddExistingDevices(HNAVITEM root)
  415. {
  416. ifc_device *device;
  417. ifc_deviceobject *object;
  418. ifc_deviceobjectenum *enumerator;
  419. if (NULL == WASABI_API_DEVICES)
  420. return;
  421. if (FAILED(WASABI_API_DEVICES->DeviceEnumerate(&enumerator)))
  422. return;
  423. while(S_OK == enumerator->Next(&object, 1, NULL))
  424. {
  425. if (SUCCEEDED(object->QueryInterface(IFC_Device, (void**)&device)))
  426. {
  427. if (FALSE == device->GetHidden() &&
  428. FALSE != device->GetAttached())
  429. {
  430. Navigation_DeviceAdd(root, device);
  431. }
  432. device->Release();
  433. }
  434. object->Release();
  435. }
  436. enumerator->Release();
  437. }
  438. static void
  439. Navigation_DeviceCb(ifc_device *device, DeviceEvent eventId, void *user)
  440. {
  441. HNAVITEM rootItem;
  442. rootItem = (HNAVITEM)user;
  443. switch(eventId)
  444. {
  445. case Event_DeviceAdded:
  446. if (FALSE != device->GetAttached())
  447. Navigation_DeviceAdd(rootItem, device);
  448. break;
  449. case Event_DeviceRemoved:
  450. if (FALSE != device->GetAttached())
  451. Navigation_DeviceRemove(rootItem, device);
  452. break;
  453. case Event_DeviceHidden:
  454. if (FALSE != device->GetAttached())
  455. Navigation_DeviceRemove(rootItem, device);
  456. break;
  457. case Event_DeviceShown:
  458. if (FALSE != device->GetAttached())
  459. Navigation_DeviceAdd(rootItem, device);
  460. break;
  461. case Event_DeviceAttached:
  462. Navigation_DeviceAdd(rootItem, device);
  463. break;
  464. case Event_DeviceDetached:
  465. Navigation_DeviceRemove(rootItem, device);
  466. break;
  467. case Event_DeviceDisplayNameChanged:
  468. Navigation_DeviceTitleChanged(rootItem, device);
  469. break;
  470. case Event_DeviceIconChanged:
  471. Navigation_DeviceIconChanged(rootItem, device);
  472. break;
  473. }
  474. }
  475. static BOOL
  476. Navigation_RegisterDeviceHandler(HNAVITEM root)
  477. {
  478. HWND eventRelay;
  479. DeviceEventCallbacks callbacks;
  480. if (0 != deviceHandler)
  481. return FALSE;
  482. eventRelay = Plugin_GetEventRelayWindow();
  483. if (NULL == eventRelay)
  484. return FALSE;
  485. ZeroMemory(&callbacks, sizeof(callbacks));
  486. callbacks.deviceCb = Navigation_DeviceCb;
  487. deviceHandler = EVENTRELAY_REGISTER_HANDLER(eventRelay, &callbacks, root);
  488. return (0 != deviceHandler);
  489. }
  490. BOOL
  491. Navigation_Initialize(void)
  492. {
  493. HWND libraryWindow;
  494. wchar_t buffer[128] = {0};
  495. if (NULL != navigationRoot)
  496. return FALSE;
  497. libraryWindow = Plugin_GetLibraryWindow();
  498. MLNavCtrl_BeginUpdate(libraryWindow, NUF_LOCK_TOP);
  499. WASABI_API_LNGSTRINGW_BUF(IDS_DEVICES_NAVIGATION_NODE, buffer, ARRAYSIZE(buffer));
  500. navigationRoot = NavigationItem_Insert(NULL, NULL,
  501. NIMF_ITEMID | NIMF_TEXT | NIMF_STYLE | NIMF_TEXTINVARIANT |
  502. NIMF_PARAM | NIMF_IMAGE | NIMF_IMAGESEL,
  503. ML_TREEVIEW_ID_DEVICES,
  504. DEVICES_NAVITEM_PREFIX L"root",
  505. buffer,
  506. NIS_HASCHILDREN | NIS_ALLOWCHILDMOVE | NIS_DEFAULTIMAGE,
  507. -1,
  508. -1L);
  509. if (NULL == navigationRoot)
  510. return FALSE;
  511. Navigation_AddExistingDevices(navigationRoot);
  512. Navigation_RegisterDeviceHandler(navigationRoot);
  513. MLNavCtrl_EndUpdate(libraryWindow);
  514. return TRUE;
  515. }
  516. void
  517. Navigation_Uninitialize(void)
  518. {
  519. if (0 != deviceHandler)
  520. {
  521. HWND eventRelay;
  522. eventRelay = Plugin_GetEventRelayWindow();
  523. if (NULL != eventRelay)
  524. {
  525. EVENTRELAY_UNREGISTER_HANDLER(eventRelay, deviceHandler);
  526. }
  527. deviceHandler = NULL;
  528. }
  529. if (NULL != navigationRoot)
  530. {
  531. NavigationItem_Delete(navigationRoot);
  532. navigationRoot = FALSE;
  533. }
  534. NavigationIcons_ClearCache();
  535. }
  536. BOOL
  537. Navigation_SelectDevice(const char *name)
  538. {
  539. HNAVITEM item;
  540. item = NavigationItem_FindFromDeviceName(navigationRoot, name);
  541. if (NULL == item)
  542. return FALSE;
  543. return MLNavItem_Select(Plugin_GetLibraryWindow(), item);
  544. }
  545. BOOL
  546. Navigation_EditDeviceTitle(const char *name)
  547. {
  548. HNAVITEM item;
  549. item = NavigationItem_FindFromDeviceName(navigationRoot, name);
  550. if (NULL == item)
  551. return FALSE;
  552. return MLNavItem_EditTitle(Plugin_GetLibraryWindow(), item);
  553. }
  554. static void
  555. Navigation_DestroyCb()
  556. {
  557. }
  558. static BOOL
  559. NavigationItem_IsMine(HNAVITEM item, ifc_device **device)
  560. {
  561. NAVITEM info;
  562. wchar_t buffer[128] = {0};
  563. INT nameLength;
  564. INT prefixLength;
  565. if (NULL == item)
  566. return FALSE;
  567. if (item == navigationRoot)
  568. {
  569. if (NULL != device)
  570. *device = NULL;
  571. return TRUE;
  572. }
  573. info.cbSize = sizeof(NAVITEM);
  574. info.hItem = item;
  575. info.mask = NIMF_TEXTINVARIANT;
  576. info.cchInvariantMax = ARRAYSIZE(buffer);
  577. info.pszInvariant = buffer;
  578. if (FALSE == MLNavItem_GetInfo(Plugin_GetLibraryWindow(), &info))
  579. return FALSE;
  580. // to maintain some specific orders, we need to alter the insert position
  581. BOOL swappped = FALSE;
  582. if (!wcsnicmp(L"cloud_sources", info.pszInvariant, 13))
  583. {
  584. // and this will allow us to make the cloud library
  585. // root do a web page or a sources view as needed...
  586. lstrcpynW(info.pszInvariant, L"ml_devices_all_sources", ARRAYSIZE(buffer));
  587. swappped = TRUE;
  588. }
  589. nameLength = (NULL != info.pszInvariant) ? lstrlen(info.pszInvariant) : 0;
  590. prefixLength = ARRAYSIZE(DEVICES_NAVITEM_PREFIX) - 1;
  591. if (nameLength <= prefixLength)
  592. return FALSE;
  593. if (CSTR_EQUAL != CompareString(CSTR_INVARIANT, 0,
  594. DEVICES_NAVITEM_PREFIX, prefixLength, info.pszInvariant, prefixLength))
  595. {
  596. return FALSE;
  597. }
  598. if (NULL != device)
  599. {
  600. char name[ARRAYSIZE(buffer)] = {0};
  601. nameLength = WideCharToMultiByte(CP_UTF8, 0,
  602. info.pszInvariant + prefixLength, nameLength - prefixLength,
  603. name, ARRAYSIZE(name), NULL, NULL);
  604. name[nameLength] = '\0';
  605. if (0 == nameLength ||
  606. NULL == WASABI_API_DEVICES ||
  607. S_OK != WASABI_API_DEVICES->DeviceFind(name, device))
  608. {
  609. *device = NULL;
  610. if (swappped) return FALSE;
  611. }
  612. }
  613. return TRUE;
  614. }
  615. static HWND
  616. Navigation_CreateViewErrorWidget(HWND hostWindow, void *user)
  617. {
  618. return InfoWidget_CreateWindow(WIDGET_TYPE_VIEW_ERROR,
  619. MAKEINTRESOURCE(IDS_INFOWIDGET_TITLE),
  620. MAKEINTRESOURCE(IDS_CREATE_DEVICE_VIEW_FAILED),
  621. NULL,
  622. hostWindow, 0, 0, 0, 0, FALSE, 0);
  623. }
  624. static HWND
  625. NavigationItem_CreateViewCb(HNAVITEM item, HWND parentWindow)
  626. {
  627. HWND hwnd;
  628. ifc_device *device;
  629. if (NULL == item ||
  630. FALSE == NavigationItem_IsMine(item, &device))
  631. return NULL;
  632. if (NULL != device)
  633. {
  634. hwnd = device->CreateView(parentWindow);
  635. device->Release();
  636. if (NULL == hwnd)
  637. {
  638. hwnd = WidgetHost_Create(0, 0, 0, 0, 0, parentWindow, Navigation_CreateViewErrorWidget, NULL);
  639. }
  640. }
  641. else
  642. {
  643. hwnd = ManagerView_CreateWindow(parentWindow);
  644. }
  645. return hwnd;
  646. }
  647. static BOOL
  648. NavigationItem_ShowContextMenuCb(HNAVITEM item, HWND hostWindow, POINTS pts)
  649. {
  650. POINT pt;
  651. ifc_device *device;
  652. if (NULL == item ||
  653. FALSE == NavigationItem_IsMine(item, &device))
  654. return FALSE;
  655. POINTSTOPOINT(pt, pts);
  656. if (item != navigationRoot)
  657. {
  658. if (NULL != device)
  659. {
  660. char *deviceName = AnsiString_Duplicate(device->GetName());
  661. device->Release();
  662. device = NULL;
  663. if (NULL != deviceName)
  664. {
  665. NavigationItem_DisplayDeviceMenu(item, deviceName, hostWindow, pt);
  666. AnsiString_Free(deviceName);
  667. }
  668. }
  669. }
  670. else
  671. {
  672. NavigationItem_DisplayRootMenu(item, hostWindow, pt);
  673. }
  674. if (NULL != device)
  675. device->Release();
  676. return TRUE;
  677. }
  678. static BOOL
  679. NavigationItem_ShowHelpCb(HNAVITEM item, HWND hostWindow, POINTS pts)
  680. {
  681. if (NULL == item ||
  682. FALSE == NavigationItem_IsMine(item, NULL))
  683. {
  684. return FALSE;
  685. }
  686. Plugin_ShowHelp();
  687. return TRUE;
  688. }
  689. static BOOL
  690. NavigationItem_DeleteCb(HNAVITEM item)
  691. {
  692. ifc_device *device;
  693. if (NULL == item ||
  694. FALSE == NavigationItem_IsMine(item, &device))
  695. return FALSE;
  696. if (NULL != device)
  697. {
  698. device->SetNavigationItem(NULL);
  699. device->Release();
  700. }
  701. if (item != navigationRoot)
  702. {
  703. NAVITEM itemInfo;
  704. HWND libraryWindow;
  705. libraryWindow = Plugin_GetLibraryWindow();
  706. itemInfo.cbSize = sizeof(itemInfo);
  707. itemInfo.mask = NIMF_IMAGE;
  708. itemInfo.hItem = item;
  709. if (FALSE != MLNavItem_GetInfo(libraryWindow, &itemInfo) &&
  710. -1 != itemInfo.iImage)
  711. {
  712. NavigationIcons_ReleaseIconIndex(itemInfo.iImage);
  713. }
  714. }
  715. return TRUE;
  716. }
  717. static BOOL
  718. NavigationItem_KeyDownCb(HNAVITEM item, NMTVKEYDOWN *keyData)
  719. {
  720. ifc_device *device;
  721. if (NULL == keyData ||
  722. NULL == item ||
  723. FALSE == NavigationItem_IsMine(item, &device))
  724. {
  725. return FALSE;
  726. }
  727. if (NULL == device)
  728. return TRUE;
  729. switch(keyData->wVKey)
  730. {
  731. case VK_F2:
  732. MLNavItem_EditTitle(Plugin_GetLibraryWindow(), item);
  733. break;
  734. }
  735. device->Release();
  736. return TRUE;
  737. }
  738. static int
  739. NavigationItem_DropTargetCb(HNAVITEM item, unsigned int dataType, void *data)
  740. {
  741. ifc_device *device;
  742. int result;
  743. if (NULL == item ||
  744. FALSE == NavigationItem_IsMine(item, &device))
  745. {
  746. return 0;
  747. }
  748. if (NULL == device)
  749. return -1;
  750. result = -1;
  751. if (NULL == data)
  752. {
  753. if (S_OK == device->GetDropSupported(dataType))
  754. result = 1;
  755. }
  756. else
  757. {
  758. if (SUCCEEDED(device->Drop(data, dataType)))
  759. result = 1;
  760. }
  761. device->Release();
  762. return result;
  763. }
  764. static BOOL
  765. NavigationItem_TitleEditBeginCb(HNAVITEM item)
  766. { // return TRUE to cancel ediging (only on own items!!!);
  767. ifc_device *device;
  768. BOOL blockEditor;
  769. if (NULL == item ||
  770. FALSE == NavigationItem_IsMine(item, &device))
  771. {
  772. return FALSE;
  773. }
  774. if (NULL == device)
  775. return TRUE;
  776. blockEditor = (FALSE == DeviceCommand_GetEnabled(device, "rename",
  777. DeviceCommandContext_NavigationMenu));
  778. device->Release();
  779. return blockEditor;
  780. }
  781. static BOOL
  782. NavigationItem_TitleEditEndCb(HNAVITEM item, const wchar_t *title)
  783. {
  784. HRESULT hr;
  785. ifc_device *device;
  786. if (NULL == title)
  787. return FALSE;
  788. if (NULL == item ||
  789. FALSE == NavigationItem_IsMine(item, &device) ||
  790. NULL == device)
  791. {
  792. return FALSE;
  793. }
  794. hr = device->SetDisplayName(title);
  795. device->Release();
  796. if (FAILED(hr))
  797. {
  798. HWND libraryWindow;
  799. wchar_t title[256] = {0}, message[1024] = {0};
  800. libraryWindow = Plugin_GetLibraryWindow();
  801. WASABI_API_LNGSTRINGW_BUF(IDS_MESSAGEBOX_TITLE, title, ARRAYSIZE(title));
  802. WASABI_API_LNGSTRINGW_BUF(IDS_MESSAGE_UNABLE_TO_RENAME, message, ARRAYSIZE(message));
  803. MessageBox(libraryWindow, message, title, MB_OK | MB_ICONERROR);
  804. return FALSE;
  805. }
  806. return FALSE;
  807. }
  808. BOOL
  809. Navigation_ProcessMessage(INT msg, INT_PTR param1, INT_PTR param2, INT_PTR param3, INT_PTR *result)
  810. {
  811. if (msg == ML_MSG_NO_CONFIG)
  812. {
  813. *result = TRUE;
  814. return TRUE;
  815. }
  816. if (msg < ML_MSG_TREE_BEGIN || msg > ML_MSG_TREE_END)
  817. return FALSE;
  818. HNAVITEM item;
  819. switch(msg)
  820. {
  821. case ML_MSG_TREE_ONCREATEVIEW:
  822. item = NavigationItem_GetFromMessage(msg, param1);
  823. *result = (INT_PTR)NavigationItem_CreateViewCb(item, (HWND)param2);
  824. return TRUE;
  825. case ML_MSG_NAVIGATION_CONTEXTMENU:
  826. item = NavigationItem_GetFromMessage(msg, param1);
  827. *result = (INT_PTR)NavigationItem_ShowContextMenuCb(item, (HWND)param2, MAKEPOINTS(param3));
  828. return TRUE;
  829. case ML_MSG_NAVIGATION_HELP:
  830. item = NavigationItem_GetFromMessage(msg, param1);
  831. *result = (INT_PTR)NavigationItem_ShowHelpCb(item, (HWND)param2, MAKEPOINTS(param3));
  832. return TRUE;
  833. case ML_MSG_NAVIGATION_ONDELETE:
  834. item = NavigationItem_GetFromMessage(msg, param1);
  835. *result = (INT_PTR)NavigationItem_DeleteCb(item);
  836. return TRUE;
  837. case ML_MSG_TREE_ONKEYDOWN:
  838. item = NavigationItem_GetFromMessage(msg, param1);
  839. *result = (INT_PTR)NavigationItem_KeyDownCb(item, (NMTVKEYDOWN*)param2);
  840. return TRUE;
  841. case ML_MSG_TREE_ONDROPTARGET:
  842. item = NavigationItem_GetFromMessage(msg, param1);
  843. *result = (INT_PTR)NavigationItem_DropTargetCb(item, (unsigned int)param2, (void*)param3);
  844. return TRUE;
  845. case ML_MSG_NAVIGATION_ONBEGINTITLEEDIT:
  846. item = NavigationItem_GetFromMessage(msg, param1);
  847. *result = (INT_PTR)NavigationItem_TitleEditBeginCb(item);
  848. return TRUE;
  849. case ML_MSG_NAVIGATION_ONENDTITLEEDIT:
  850. item = NavigationItem_GetFromMessage(msg, param1);
  851. *result = (INT_PTR)NavigationItem_TitleEditEndCb(item, (const wchar_t*)param2);
  852. return TRUE;
  853. case ML_MSG_NAVIGATION_ONDESTROY:
  854. Navigation_DestroyCb();
  855. *result = 0L;
  856. return TRUE;
  857. }
  858. return FALSE;
  859. }