commands.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "main.h"
  2. #include "./commands.h"
  3. #include "./wasabi.h"
  4. #include "./resource.h"
  5. #include "./navigation.h"
  6. #include "./import.h"
  7. #include "./serviceHelper.h"
  8. #include "../../General/gen_ml/ml_ipc_0313.h"
  9. #include <ifc_omservice.h>
  10. #include <browserView.h>
  11. #include <strsafe.h>
  12. HRESULT Command_NavigateService(ifc_omservice *service, LPCWSTR pszUrl, BOOL fActiveOnly)
  13. {
  14. if (NULL == service)
  15. return E_INVALIDARG;
  16. Navigation *navigation;
  17. if (FAILED(Plugin_GetNavigation(&navigation)))
  18. return E_UNEXPECTED;
  19. ifc_omservice *activeService;
  20. HWND hView = navigation->GetActiveView(&activeService);
  21. if (NULL == hView || activeService->GetId() != service->GetId())
  22. hView = NULL;
  23. if (NULL != activeService)
  24. activeService->Release();
  25. HRESULT hr = S_OK;
  26. if (NULL != hView)
  27. {
  28. if (FALSE == BrowserView_Navigate(hView, pszUrl, TRUE))
  29. hr = E_FAIL;
  30. }
  31. else
  32. {
  33. hr = (FALSE == fActiveOnly) ?
  34. navigation->ShowService(service->GetId(), pszUrl) : E_NOTIMPL;
  35. }
  36. navigation->Release();
  37. return hr;
  38. }
  39. static void CALLBACK ThreadCallback_NavigateService(Dispatchable *object, ULONG_PTR param1, ULONG_PTR param2)
  40. {
  41. ifc_omservice *service = (ifc_omservice*)object;
  42. LPWSTR pszUrl = (LPWSTR)param1;
  43. Command_NavigateService(service, pszUrl, (BOOL)param2);
  44. Plugin_FreeResString(pszUrl);
  45. }
  46. HRESULT Command_PostNavigateSvc(ifc_omservice *service, LPCWSTR pszUrl, BOOL fActiveOnly)
  47. {
  48. if (NULL == OMUTILITY)
  49. return E_FAIL;
  50. if (NULL == service)
  51. return E_INVALIDARG;
  52. LPWSTR pszUrlCopy = Plugin_DuplicateResString(pszUrl);
  53. HRESULT hr = OMUTILITY->PostMainThreadCallback2(ThreadCallback_NavigateService, service, (ULONG_PTR)pszUrlCopy, fActiveOnly);
  54. if (FAILED(hr))
  55. {
  56. Plugin_FreeResString(pszUrlCopy);
  57. }
  58. return hr;
  59. }
  60. HRESULT Command_EditService(ifc_omservice *service)
  61. {
  62. if (NULL == service)
  63. return E_INVALIDARG;
  64. WCHAR szBuffer[2048];
  65. HRESULT hr = Plugin_MakeResourcePath(szBuffer, ARRAYSIZE(szBuffer), RT_HTML, MAKEINTRESOURCE(IDR_HTML_EDITOR), RESPATH_TARGETIE | RESPATH_COMPACT);
  66. if (FAILED(hr)) return hr;
  67. INT cchUrl = lstrlen(szBuffer);
  68. LPWSTR pszParam = szBuffer + cchUrl;
  69. INT cchParamMax = ARRAYSIZE(szBuffer) - cchUrl;
  70. hr = StringCchPrintf(pszParam, cchParamMax, L"?serviceId=%u", service->GetId());
  71. if (FAILED(hr)) return hr;
  72. return Command_NavigateService(service, szBuffer, FALSE);
  73. }
  74. HRESULT Command_ReloadService(ifc_omservice *service)
  75. {
  76. if (NULL == service)
  77. return E_INVALIDARG;
  78. HRESULT hr = ServiceHelper_Reload(service);
  79. return hr;
  80. }
  81. HRESULT Command_ResetPermissions(ifc_omservice *service)
  82. {
  83. if (NULL == service)
  84. return E_INVALIDARG;
  85. if (NULL == AGAVE_API_JSAPI2_SECURITY)
  86. return E_UNEXPECTED;
  87. WCHAR szBuffer[64] = {0};
  88. if (FAILED(StringCchPrintfW(szBuffer, ARRAYSIZE(szBuffer), L"%u", service->GetId())))
  89. return E_FAIL;
  90. AGAVE_API_JSAPI2_SECURITY->ResetAuthorization(szBuffer);
  91. return S_OK;
  92. }
  93. HRESULT Command_LocateService(ifc_omservice *service)
  94. {
  95. if (NULL == service)
  96. return E_INVALIDARG;
  97. WCHAR szPath[512];
  98. HRESULT hr = service->GetAddress(szPath, ARRAYSIZE(szPath));
  99. if (FAILED(hr)) return hr;
  100. if (L'\0' == szPath[0])
  101. return E_FAIL;
  102. Navigation *navigation;
  103. if (FAILED(Plugin_GetNavigation(&navigation)))
  104. return E_UNEXPECTED;
  105. navigation->Release();
  106. if (WASABI_API_EXPLORERFINDFILE)
  107. {
  108. WASABI_API_EXPLORERFINDFILE->AddFile(szPath);
  109. WASABI_API_EXPLORERFINDFILE->ShowFiles();
  110. }
  111. return E_UNEXPECTED;
  112. }
  113. HRESULT Command_EditServiceExternal(ifc_omservice *service)
  114. {
  115. if (NULL == service)
  116. return E_INVALIDARG;
  117. WCHAR szPath[512] = {0};
  118. HRESULT hr = service->GetAddress(szPath, ARRAYSIZE(szPath));
  119. if (FAILED(hr)) return hr;
  120. if (L'\0' == szPath[0])
  121. return E_FAIL;
  122. Navigation *navigation;
  123. if (FAILED(Plugin_GetNavigation(&navigation)))
  124. return E_UNEXPECTED;
  125. HWND hOwner = navigation->GetActiveView(NULL);
  126. navigation->Release();
  127. if (NULL == hOwner)
  128. hOwner = Plugin_GetLibrary();
  129. HINSTANCE hInst = ShellExecute(hOwner, L"open", szPath, NULL, NULL, SW_SHOWNORMAL);
  130. hr = ((INT_PTR)hInst > 32) ? S_OK : E_FAIL;
  131. return hr;
  132. }
  133. HRESULT Command_DeleteItem(HNAVITEM hItem)
  134. {
  135. Navigation *navigation;
  136. HRESULT hr = Plugin_GetNavigation(&navigation);
  137. if (SUCCEEDED(hr))
  138. {
  139. hr = navigation->DeleteItem(hItem);
  140. navigation->Release();
  141. }
  142. return hr;
  143. }
  144. HRESULT Command_DeleteAll()
  145. {
  146. Navigation *navigation;
  147. HRESULT hr = Plugin_GetNavigation(&navigation);
  148. if (SUCCEEDED(hr))
  149. {
  150. hr = navigation->DeleteAll();
  151. navigation->Release();
  152. }
  153. return hr;
  154. }
  155. HRESULT Command_CreateService()
  156. {
  157. Navigation *navigation;
  158. HRESULT hr = Plugin_GetNavigation(&navigation);
  159. if (FAILED(hr)) return hr;
  160. HNAVITEM hItem;
  161. hr = navigation->CreateUserService(&hItem);
  162. if (SUCCEEDED(hr))
  163. {
  164. ifc_omservice *service;
  165. hr= navigation->GetService(hItem, &service);
  166. if (SUCCEEDED(hr))
  167. {
  168. Command_EditService(service);
  169. service->Release();
  170. }
  171. }
  172. navigation->Release();
  173. return hr;
  174. }
  175. HRESULT Command_NewWindow(HNAVITEM hItem)
  176. {
  177. Navigation *navigation;
  178. HRESULT hr = Plugin_GetNavigation(&navigation);
  179. if (SUCCEEDED(hr))
  180. {
  181. HWND hWindow;
  182. HRESULT hr = navigation->CreatePopup(hItem, &hWindow);
  183. if (SUCCEEDED(hr))
  184. {
  185. ShowWindow(hWindow, SW_SHOWNORMAL);
  186. }
  187. navigation->Release();
  188. }
  189. return hr;
  190. }
  191. HRESULT Command_OpenView(HNAVITEM hItem)
  192. {
  193. Navigation *navigation;
  194. HRESULT hr = Plugin_GetNavigation(&navigation);
  195. if (SUCCEEDED(hr))
  196. {
  197. hr = navigation->SelectItem(hItem, NULL);
  198. navigation->Release();
  199. }
  200. return hr;
  201. }
  202. void Command_ImportFiles()
  203. {
  204. HWND hOwner = Plugin_GetDialogOwner();
  205. ImportService_FromFile(hOwner);
  206. }
  207. void Command_ImportUrl()
  208. {
  209. HWND hOwner = Plugin_GetDialogOwner();
  210. ImportService_FromUrl(hOwner);
  211. }
  212. static void CALLBACK BrowserOptions_Callback(HWND hOptions, UINT type, ULONG_PTR user)
  213. {
  214. HWND hLibrary = (HWND)user;
  215. switch(type)
  216. {
  217. case BOCALLBACK_INIT:
  218. {
  219. HWND hView = (HWND)SENDMLIPC(hLibrary, ML_IPC_GETCURRENTVIEW, 0);
  220. if (NULL != hView)
  221. {
  222. RECT viewRect, optionsRect;
  223. if (GetWindowRect(hView, &viewRect) && GetWindowRect(hOptions, &optionsRect))
  224. {
  225. INT x = viewRect.left + ((viewRect.right - viewRect.left) - (optionsRect.right - optionsRect.left))/2;
  226. INT y = viewRect.top + ((viewRect.bottom - viewRect.top) - (optionsRect.bottom - optionsRect.top))/2;
  227. SetWindowPos(hOptions, NULL, x, y, 0, 0, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOSIZE);
  228. SendMessage(hOptions, DM_REPOSITION, 0, 0L);
  229. }
  230. }
  231. }
  232. break;
  233. }
  234. }
  235. HRESULT Command_ShowBrowserOptions()
  236. {
  237. HWND hWinamp = Plugin_GetWinamp();
  238. if (NULL == hWinamp || NULL == OMBROWSERMNGR)
  239. return E_UNEXPECTED;
  240. HRESULT hr = OMBROWSERMNGR->Initialize(NULL, hWinamp);
  241. if (SUCCEEDED(hr))
  242. {
  243. HWND hOwner = Plugin_GetDialogOwner();
  244. hr = OMBROWSERMNGR->ShowOptions(hOwner, BOSTYLE_NORMAL | BOSTYLE_SHOWDEBUG,
  245. BrowserOptions_Callback, (ULONG_PTR)hOwner);
  246. }
  247. return hr;
  248. }
  249. BOOL CommandManager_Process(HNAVITEM hItem, ifc_omservice *service, UINT commandId)
  250. {
  251. switch(commandId)
  252. {
  253. case ID_VIEW_OPEN: Command_OpenView(hItem); return TRUE;
  254. case ID_VIEW_NEWWINDOW: Command_NewWindow(hItem); return TRUE;
  255. case ID_SERVICE_NEW: Command_CreateService(); return TRUE;
  256. case ID_SERVICE_DELETE: Command_DeleteItem(hItem); return TRUE;
  257. case ID_SERVICE_DELETEALL: Command_DeleteAll(); return TRUE;
  258. case ID_SERVICE_EDIT: Command_EditService(service); return TRUE;
  259. case ID_SERVICE_LOCATE: Command_LocateService(service); return TRUE;
  260. case ID_SERVICE_EDITEXTERNAL: Command_EditServiceExternal(service); return TRUE;
  261. case ID_SERVICE_RELOAD: Command_ReloadService(service); return TRUE;
  262. case ID_SERVICE_RESETPERMISSIONS: Command_ResetPermissions(service); return TRUE;
  263. case ID_NAVIGATION_BACK: Command_NavigateService(service, NAVIGATE_BACK, TRUE); return TRUE;
  264. case ID_NAVIGATION_FORWARD: Command_NavigateService(service, NAVIGATE_FORWARD, TRUE); return TRUE;
  265. case ID_NAVIGATION_HOME: Command_NavigateService(service, NAVIGATE_HOME, TRUE); return TRUE;
  266. case ID_NAVIGATION_STOP: Command_NavigateService(service, NAVIGATE_STOP, TRUE); return TRUE;
  267. case ID_NAVIGATION_REFRESH: Command_NavigateService(service, NAVIGATE_REFRESH, TRUE); return TRUE;
  268. case ID_OMBROWSER_OPTIONS: Command_ShowBrowserOptions(); return TRUE;
  269. case ID_SERVICE_IMPORT_FILE: Command_ImportFiles(); return TRUE;
  270. case ID_SERVICE_IMPORT_URL: Command_ImportUrl(); return TRUE;
  271. }
  272. return FALSE;
  273. }