1
0

commands.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #include "main.h"
  2. #include "./commands.h"
  3. #include "./api__ml_online.h"
  4. #include "./resource.h"
  5. #include "./navigation.h"
  6. #include "./preferences.h"
  7. #include "./messagebox.h"
  8. #include "./serviceHelper.h"
  9. #include "../winamp/wa_ipc.h"
  10. #include "./import.h"
  11. #include <ifc_omservice.h>
  12. #include <browserView.h>
  13. #include <wininet.h>
  14. #include <shlwapi.h>
  15. #include <strsafe.h>
  16. #define BEGIN_COMMAND_SELECT(__commandId) switch(commandId) {
  17. #define END_COMMAND_SELECT }
  18. #define OMCOMMAND(__commandId, __commandCode, __resultOut) case (__commandId):\
  19. { BOOL result = ##__commandCode; \
  20. if (NULL != (__resultOut)) { *(__resultOut) = result;}\
  21. return TRUE;}
  22. BOOL Command_SetServiceRating(ifc_omservice *service, INT rating)
  23. {
  24. return SUCCEEDED(ServiceHelper_SetRating(service, rating, SHF_NOTIFY | SHF_VERBAL | SHF_SAVE));
  25. }
  26. BOOL Command_OpenServiceView(ifc_omservice *service)
  27. {
  28. BOOL resultOk = FALSE;;
  29. Navigation *navigation;
  30. if (NULL != service && SUCCEEDED(Plugin_GetNavigation(&navigation)))
  31. {
  32. HNAVITEM hItem = navigation->FindService(service->GetId(), NULL);
  33. if (NULL != hItem)
  34. {
  35. HRESULT hr = navigation->SelectItem(hItem, NULL);
  36. if (SUCCEEDED(hr))
  37. resultOk = TRUE;
  38. }
  39. navigation->Release();
  40. }
  41. return resultOk;
  42. }
  43. HRESULT Command_NavigateService(ifc_omservice *service, LPCWSTR pszUrl, BOOL fActiveOnly)
  44. {
  45. if (NULL == service)
  46. return E_INVALIDARG;
  47. Navigation *navigation;
  48. if (FAILED(Plugin_GetNavigation(&navigation)))
  49. return E_UNEXPECTED;
  50. ifc_omservice *activeService;
  51. HWND hView = navigation->GetActiveView(&activeService);
  52. if (NULL == hView || activeService->GetId() != service->GetId())
  53. hView = NULL;
  54. if (NULL != activeService)
  55. activeService->Release();
  56. HRESULT hr = S_OK;
  57. if (NULL != hView)
  58. {
  59. if (FALSE == BrowserView_Navigate(hView, pszUrl, TRUE))
  60. hr = E_FAIL;
  61. }
  62. else
  63. {
  64. hr = (FALSE == fActiveOnly) ?
  65. navigation->ShowService(service->GetId(), pszUrl) : E_NOTIMPL;
  66. }
  67. navigation->Release();
  68. return hr;
  69. }
  70. HRESULT Command_EditService( ifc_omservice *service )
  71. {
  72. if ( NULL == service )
  73. return E_INVALIDARG;
  74. WCHAR szBuffer[ 2048 ] = { 0 };
  75. HRESULT hr = Plugin_MakeResourcePath( szBuffer, ARRAYSIZE( szBuffer ), RT_HTML, MAKEINTRESOURCE( IDR_HTML_EDITOR ), RESPATH_TARGETIE | RESPATH_COMPACT );
  76. if ( FAILED( hr ) )
  77. return hr;
  78. INT cchUrl = lstrlen( szBuffer );
  79. LPWSTR pszParam = szBuffer + cchUrl;
  80. INT cchParamMax = ARRAYSIZE( szBuffer ) - cchUrl;
  81. hr = StringCchPrintf( pszParam, cchParamMax, L"?serviceId=%u", service->GetId() );
  82. if ( FAILED( hr ) )
  83. return hr;
  84. return Command_NavigateService( service, szBuffer, FALSE );
  85. }
  86. BOOL Command_OpenServicePopup(ifc_omservice *service)
  87. {
  88. BOOL resultOk = FALSE;;
  89. Navigation *navigation;
  90. if (NULL != service && SUCCEEDED(Plugin_GetNavigation(&navigation)))
  91. {
  92. HNAVITEM hItem = navigation->FindService(service->GetId(), NULL);
  93. if (NULL != hItem)
  94. {
  95. HWND hPopup;
  96. HRESULT hr = navigation->CreatePopup(hItem, &hPopup);
  97. if (SUCCEEDED(hr))
  98. {
  99. ShowWindow(hPopup, SW_SHOWNORMAL);
  100. resultOk = TRUE;
  101. }
  102. }
  103. navigation->Release();
  104. }
  105. return resultOk;
  106. }
  107. BOOL Command_ReportService(ifc_omservice *service)
  108. {
  109. HWND hWinamp = Plugin_GetWinamp();
  110. if (NULL == hWinamp || !IsWindow(hWinamp))
  111. return FALSE;
  112. if (NULL == service)
  113. return FALSE;
  114. WCHAR szUrl[256] = {0};
  115. WCHAR szClient[128] = {0};
  116. OMBROWSERMNGR->GetClientId(szClient, ARRAYSIZE(szClient));
  117. StringCchPrintf(szUrl, ARRAYSIZE(szUrl), L"http://www.winamp.com/legal/abuse?svc_id=%u&unique=%s",
  118. service->GetId(), szClient);
  119. SENDWAIPC(hWinamp, IPC_OPEN_URL, szUrl);
  120. return TRUE;
  121. }
  122. BOOL Command_UnsubscribeService(ifc_omservice *service)
  123. {
  124. return (SUCCEEDED(ServiceHelper_Subscribe(service, FALSE, SHF_NOTIFY | SHF_VERBAL | SHF_SAVE)));
  125. }
  126. BOOL Command_ShowServiceInfo(ifc_omservice *service)
  127. {
  128. if (NULL == service)
  129. return FALSE;
  130. BOOL resultOk = FALSE;
  131. HRESULT hr;
  132. WCHAR szUrl[INTERNET_MAX_URL_LENGTH] = {0}, szName[INTERNET_MAX_URL_LENGTH] = {0};
  133. DWORD cchName = ARRAYSIZE(szName);
  134. if (FAILED(service->GetName(szUrl, ARRAYSIZE(szUrl))) ||
  135. FAILED(UrlEscape(szUrl, szName, &cchName, URL_ESCAPE_SEGMENT_ONLY | URL_ESCAPE_PERCENT)))
  136. {
  137. StringCchCopy(szName, ARRAYSIZE(szName), L"Info");
  138. }
  139. hr = StringCchPrintf(szUrl, ARRAYSIZE(szUrl), L"http://client.winamp.com/service/detail/%s/%d#", szName, service->GetId());
  140. if (FAILED(hr)) return hr;
  141. Navigation *navigation;
  142. hr = Plugin_GetNavigation(&navigation);
  143. if (SUCCEEDED(hr))
  144. {
  145. HNAVITEM hRoot = navigation->FindService(ROOTSERVICE_ID, NULL);
  146. if (NULL != hRoot)
  147. {
  148. HNAVITEM hActive = navigation->GetActive(NULL);
  149. if (hActive == hRoot)
  150. {
  151. HWND hView = navigation->GetActiveView(NULL);
  152. if (NULL != hView && FALSE != BrowserView_Navigate(hView, szUrl, TRUE))
  153. resultOk = TRUE;
  154. }
  155. if (FALSE == resultOk && SUCCEEDED(navigation->SelectItem(hRoot, szUrl)))
  156. resultOk = TRUE;
  157. }
  158. navigation->Release();
  159. }
  160. return resultOk;
  161. }
  162. BOOL Command_ResetServicePolicy(ifc_omservice *service)
  163. {
  164. return (SUCCEEDED(ServiceHelper_ResetPermissions(service, SHF_NOTIFY | SHF_VERBAL)));
  165. }
  166. BOOL Command_ResetSubscription()
  167. {
  168. HRESULT hr = ServiceHelper_ResetSubscription(SHF_VERBAL);
  169. return SUCCEEDED(hr);
  170. }
  171. static BOOL Command_OpenPreferences()
  172. {
  173. return Preferences_Show();
  174. }
  175. static BOOL Command_OpenHelp()
  176. {
  177. return (BOOL)SENDWAIPC(Plugin_GetWinamp(), IPC_OPEN_URL, L"https://help.winamp.com/hc/articles/8112533645844-Online-Services");
  178. }
  179. static BOOL Command_NavigateView(HWND hView, LPCWSTR navigateUrl)
  180. {
  181. Navigation *navigation;
  182. if (FAILED(Plugin_GetNavigation(&navigation)))
  183. return E_UNEXPECTED;
  184. HWND hActive = navigation->GetActiveView(NULL);
  185. if (hActive != hView) hView = NULL;
  186. BOOL resultOk = ( NULL != hView && FALSE != BrowserView_Navigate( hView, navigateUrl, TRUE ) );
  187. navigation->Release();
  188. return resultOk;
  189. }
  190. HRESULT Command_ImportFiles()
  191. {
  192. HWND hOwner = Plugin_GetDialogOwner();
  193. return ImportService_FromFile(hOwner);
  194. }
  195. HRESULT Command_ImportUrl()
  196. {
  197. HWND hOwner = Plugin_GetDialogOwner();
  198. return ImportService_FromUrl(hOwner);
  199. }
  200. HRESULT Command_CreateService()
  201. {
  202. Navigation *navigation;
  203. HRESULT hr = Plugin_GetNavigation(&navigation);
  204. if (FAILED(hr)) return hr;
  205. HNAVITEM hItem;
  206. hr = navigation->CreateUserService(&hItem);
  207. if (SUCCEEDED(hr))
  208. {
  209. ifc_omservice *service;
  210. hr= navigation->GetService(hItem, &service);
  211. if (SUCCEEDED(hr))
  212. {
  213. Command_EditService(service);
  214. service->Release();
  215. }
  216. }
  217. navigation->Release();
  218. return hr;
  219. }
  220. HRESULT Command_LocateService(ifc_omservice *service)
  221. {
  222. if (NULL == service)
  223. return E_INVALIDARG;
  224. WCHAR szPath[512];
  225. HRESULT hr = service->GetAddress(szPath, ARRAYSIZE(szPath));
  226. if (FAILED(hr)) return hr;
  227. if (L'\0' == szPath[0])
  228. return E_FAIL;
  229. Navigation *navigation;
  230. if (FAILED(Plugin_GetNavigation(&navigation)))
  231. return E_UNEXPECTED;
  232. navigation->Release();
  233. if (WASABI_API_EXPLORERFINDFILE)
  234. {
  235. WASABI_API_EXPLORERFINDFILE->AddFile(szPath);
  236. WASABI_API_EXPLORERFINDFILE->ShowFiles();
  237. }
  238. return E_UNEXPECTED;
  239. }
  240. HRESULT Command_EditServiceExternal(ifc_omservice *service)
  241. {
  242. if (NULL == service)
  243. return E_INVALIDARG;
  244. WCHAR szPath[512];
  245. HRESULT hr = service->GetAddress(szPath, ARRAYSIZE(szPath));
  246. if (FAILED(hr)) return hr;
  247. if (L'\0' == szPath[0])
  248. return E_FAIL;
  249. Navigation *navigation;
  250. if (FAILED(Plugin_GetNavigation(&navigation)))
  251. return E_UNEXPECTED;
  252. HWND hOwner = navigation->GetActiveView(NULL);
  253. navigation->Release();
  254. if (NULL == hOwner)
  255. hOwner = Plugin_GetLibrary();
  256. HINSTANCE hInst = ShellExecute(hOwner, L"open", szPath, NULL, NULL, SW_SHOWNORMAL);
  257. hr = ((INT_PTR)hInst > 32) ? S_OK : E_FAIL;
  258. return hr;
  259. }
  260. BOOL Command_ProcessService(HWND hView, ifc_omservice *service, INT commandId, BOOL *fSuccess)
  261. {
  262. BEGIN_COMMAND_SELECT(commandId)
  263. OMCOMMAND(ID_RATING_VALUE_5, Command_SetServiceRating(service, 5), fSuccess);
  264. OMCOMMAND(ID_RATING_VALUE_4, Command_SetServiceRating(service, 4), fSuccess);
  265. OMCOMMAND(ID_RATING_VALUE_3, Command_SetServiceRating(service, 3), fSuccess);
  266. OMCOMMAND(ID_RATING_VALUE_2, Command_SetServiceRating(service, 2), fSuccess);
  267. OMCOMMAND(ID_RATING_VALUE_1, Command_SetServiceRating(service, 1), fSuccess);
  268. OMCOMMAND(ID_VIEW_OPEN, Command_OpenServiceView(service), fSuccess);
  269. OMCOMMAND(ID_VIEW_OPENPOPUP, Command_OpenServicePopup(service), fSuccess);
  270. //OMCOMMAND(ID_SERVICE_REPORT, Command_ReportService(service), fSuccess);
  271. OMCOMMAND(ID_SERVICE_UNSUBSCRIBE, Command_UnsubscribeService(service), fSuccess);
  272. //OMCOMMAND(ID_SERVICE_GETINFO, Command_ShowServiceInfo(service), fSuccess);
  273. OMCOMMAND(ID_SERVICE_RESETPOLICY, Command_ResetServicePolicy(service), fSuccess);
  274. OMCOMMAND(ID_SERVICE_IMPORT_FILE, Command_ImportFiles(), fSuccess);
  275. OMCOMMAND(ID_SERVICE_IMPORT_URL, Command_ImportUrl(), fSuccess);
  276. OMCOMMAND(ID_NAVIGATION_REFRESH, Command_NavigateView(hView, NAVIGATE_REFRESH), fSuccess);
  277. OMCOMMAND(ID_SERVICE_NEW, Command_CreateService(), fSuccess);
  278. OMCOMMAND(ID_SERVICE_EDIT, Command_EditService(service), fSuccess);
  279. OMCOMMAND(ID_SERVICE_LOCATE, Command_LocateService(service), fSuccess);
  280. OMCOMMAND(ID_SERVICE_EDITEXTERNAL, Command_EditServiceExternal(service), fSuccess);
  281. END_COMMAND_SELECT
  282. return FALSE;
  283. }
  284. BOOL Command_ProcessGeneral(INT commandId, BOOL *fSuccess)
  285. {
  286. BEGIN_COMMAND_SELECT(commandId)
  287. //OMCOMMAND(ID_SERVICEMANAGER_RESET, Command_ResetSubscription(), fSuccess);
  288. OMCOMMAND(ID_PLUGIN_PREFERENCES, Command_OpenPreferences(), fSuccess);
  289. OMCOMMAND(ID_PLUGIN_HELP, Command_OpenHelp(), fSuccess);
  290. END_COMMAND_SELECT
  291. return FALSE;
  292. }
  293. static void CALLBACK BrowserOptions_Callback(HWND hOptions, UINT type, ULONG_PTR user)
  294. {
  295. HWND hLibrary = (HWND)user;
  296. switch(type)
  297. {
  298. case BOCALLBACK_INIT:
  299. {
  300. HWND hView = (HWND)SENDMLIPC(hLibrary, ML_IPC_GETCURRENTVIEW, 0);
  301. if (NULL != hView)
  302. {
  303. RECT viewRect, optionsRect;
  304. if (GetWindowRect(hView, &viewRect) && GetWindowRect(hOptions, &optionsRect))
  305. {
  306. INT x = viewRect.left + ((viewRect.right - viewRect.left) - (optionsRect.right - optionsRect.left))/2;
  307. INT y = viewRect.top + ((viewRect.bottom - viewRect.top) - (optionsRect.bottom - optionsRect.top))/2;
  308. SetWindowPos(hOptions, NULL, x, y, 0, 0, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOSIZE);
  309. SendMessage(hOptions, DM_REPOSITION, 0, 0L);
  310. }
  311. }
  312. }
  313. break;
  314. }
  315. }
  316. HRESULT Command_ShowBrowserOptions()
  317. {
  318. HWND hWinamp = Plugin_GetWinamp();
  319. if (NULL == hWinamp || NULL == OMBROWSERMNGR)
  320. return E_UNEXPECTED;
  321. HRESULT hr = OMBROWSERMNGR->Initialize(NULL, hWinamp);
  322. if (SUCCEEDED(hr))
  323. {
  324. HWND hOwner = Plugin_GetDialogOwner();
  325. hr = OMBROWSERMNGR->ShowOptions(hOwner, BOSTYLE_NORMAL | BOSTYLE_SHOWDEBUG,
  326. BrowserOptions_Callback, (ULONG_PTR)hOwner);
  327. }
  328. return hr;
  329. }
  330. BOOL Command_ProcessView(HWND hView, INT commandId, BOOL *fSuccess)
  331. {
  332. BEGIN_COMMAND_SELECT(commandId)
  333. OMCOMMAND(ID_NAVIGATION_HOME, Command_NavigateView(hView, NAVIGATE_HOME), fSuccess);
  334. OMCOMMAND(ID_NAVIGATION_BACK, Command_NavigateView(hView, NAVIGATE_BACK), fSuccess);
  335. OMCOMMAND(ID_NAVIGATION_FORWARD, Command_NavigateView(hView, NAVIGATE_FORWARD), fSuccess);
  336. OMCOMMAND(ID_NAVIGATION_REFRESH, Command_NavigateView(hView, NAVIGATE_REFRESH), fSuccess);
  337. OMCOMMAND(ID_NAVIGATION_STOP, Command_NavigateView(hView, NAVIGATE_STOP), fSuccess);
  338. OMCOMMAND(ID_OMBROWSER_OPTIONS, Command_ShowBrowserOptions(), fSuccess);
  339. OMCOMMAND(ID_SERVICE_IMPORT_FILE, Command_ImportFiles(), fSuccess);
  340. OMCOMMAND(ID_SERVICE_IMPORT_URL, Command_ImportUrl(), fSuccess);
  341. OMCOMMAND(ID_SERVICE_NEW, Command_CreateService(), fSuccess);
  342. END_COMMAND_SELECT
  343. return FALSE;
  344. }