updateService.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "main.h"
  2. #include "./updateService.h"
  3. #include "./api.h"
  4. #include "./externalCOM.h"
  5. #include "../omBrowser/obj_ombrowser.h"
  6. #include "../omBrowser/ifc_omservice.h"
  7. #include "../omBrowser/browserPopup.h"
  8. #include "../omBrowser/ifc_omdebugconfig.h"
  9. HRESULT ServiceSubclass_Attach(HWND hwnd, UpdateService *service);
  10. UpdateService::UpdateService(obj_ombrowser *browserObject, LPWSTR pszUrl)
  11. : ref(1), url(pszUrl), browserManager(browserObject)
  12. {
  13. if (NULL != browserManager)
  14. browserManager->AddRef();
  15. }
  16. UpdateService::~UpdateService()
  17. {
  18. Finish();
  19. }
  20. HRESULT UpdateService::CreateInstance(LPCSTR pszUrl, UpdateService **instance)
  21. {
  22. if (NULL == instance)
  23. return E_POINTER;
  24. *instance = NULL;
  25. waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid(OBJ_OmBrowser);
  26. if (NULL == sf) return E_FAIL;
  27. obj_ombrowser *browserManager = reinterpret_cast<obj_ombrowser*>(sf->getInterface());
  28. sf->Release();
  29. if (NULL == browserManager)
  30. return E_NOINTERFACE;
  31. HRESULT hr = browserManager->Initialize(NULL, hMainWindow);
  32. if (SUCCEEDED(hr))
  33. {
  34. LPWSTR url = NULL;
  35. if (NULL != pszUrl)
  36. {
  37. UINT cchUrlMax = MultiByteToWideChar(CP_UTF8, 0, pszUrl, -1, NULL, 0);
  38. if (0 != cchUrlMax)
  39. {
  40. cchUrlMax++;
  41. url = (LPWSTR)calloc(cchUrlMax, sizeof(WCHAR));
  42. if (NULL == url) hr = E_OUTOFMEMORY;
  43. else
  44. {
  45. if (0 == MultiByteToWideChar(CP_UTF8, 0, pszUrl, -1, url, cchUrlMax))
  46. hr = E_FAIL;
  47. }
  48. }
  49. }
  50. if (SUCCEEDED(hr))
  51. {
  52. *instance = new UpdateService(browserManager, url);
  53. if (NULL == *instance)
  54. {
  55. if (NULL != url) free(url);
  56. hr = E_OUTOFMEMORY;
  57. }
  58. }
  59. }
  60. browserManager->Release();
  61. return hr;
  62. }
  63. size_t UpdateService::AddRef()
  64. {
  65. return InterlockedIncrement((LONG*)&ref);
  66. }
  67. size_t UpdateService::Release()
  68. {
  69. if (0 == ref)
  70. return ref;
  71. LONG r = InterlockedDecrement((LONG*)&ref);
  72. if (0 == r)
  73. delete(this);
  74. return r;
  75. }
  76. int UpdateService::QueryInterface(GUID interface_guid, void **object)
  77. {
  78. if (NULL == object) return E_POINTER;
  79. if (IsEqualIID(interface_guid, IFC_OmService))
  80. *object = static_cast<ifc_omservice*>(this);
  81. else
  82. {
  83. *object = NULL;
  84. return E_NOINTERFACE;
  85. }
  86. if (NULL == *object)
  87. return E_UNEXPECTED;
  88. AddRef();
  89. return S_OK;
  90. }
  91. unsigned int UpdateService::GetId()
  92. {
  93. return 505;
  94. }
  95. HRESULT UpdateService::GetName(wchar_t *pszBuffer, int cchBufferMax)
  96. {
  97. getStringW(IDS_WINAMP_UPDATE, pszBuffer, cchBufferMax);
  98. return S_OK;
  99. }
  100. HRESULT UpdateService::GetUrl(wchar_t *pszBuffer, int cchBufferMax)
  101. {
  102. return StringCchCopyExW(pszBuffer, cchBufferMax, url, NULL, NULL, STRSAFE_IGNORE_NULLS );
  103. }
  104. HRESULT UpdateService::GetIcon(wchar_t *pszBuffer, int cchBufferMax)
  105. {
  106. return E_NOTIMPL;
  107. }
  108. HRESULT UpdateService::GetExternal(IDispatch **ppDispatch)
  109. {
  110. if (NULL == ppDispatch)
  111. return E_POINTER;
  112. ExternalCOM *external;
  113. HRESULT hr = JSAPI1_GetExternal(&external);
  114. if (FAILED(hr)) external = NULL;
  115. *ppDispatch = (IDispatch*)external;
  116. return S_OK;
  117. }
  118. HRESULT UpdateService::Show()
  119. {
  120. if (NULL == browserManager)
  121. return E_UNEXPECTED;
  122. LONG cx = 300;
  123. LONG cy = 200;
  124. RECT centerRect;
  125. HWND hCenter = (NULL != g_dialog_box_parent) ? g_dialog_box_parent : hMainWindow;
  126. if (NULL == hCenter ||
  127. 0 == GetWindowRect(hCenter, &centerRect) ||
  128. ((centerRect.right - centerRect.left) < cx) ||
  129. ((centerRect.bottom - centerRect.top) < cy))
  130. {
  131. HMONITOR hMonitor = MonitorFromWindow(hCenter, MONITOR_DEFAULTTONEAREST);
  132. MONITORINFO info;
  133. info.cbSize = sizeof(info);
  134. if (NULL != hMonitor && 0 != GetMonitorInfo(hMonitor, &info))
  135. CopyRect(&centerRect, &info.rcWork);
  136. else
  137. SetRectEmpty(&centerRect);
  138. }
  139. LONG x = centerRect.left + (centerRect.right - centerRect.left - cx)/2;
  140. LONG y = centerRect.top + (centerRect.bottom - centerRect.top - cy)/2;
  141. if (x < centerRect.left) x = centerRect.left;
  142. if (y < centerRect.top) y = centerRect.top;
  143. UINT popupFlags = NBCS_NOTOOLBAR | NBCS_NOSTATUSBAR | NBCS_DIALOGMODE | NBCS_BLOCKPOPUP;
  144. ifc_omdebugconfig *debugConfig;
  145. if (SUCCEEDED(browserManager->GetConfig(&IFC_OmDebugConfig, (void**)&debugConfig)))
  146. {
  147. if (S_OK == debugConfig->GetMenuFilterEnabled())
  148. popupFlags |= NBCS_DISABLECONTEXTMENU;
  149. debugConfig->Release();
  150. }
  151. HWND hPopup;
  152. HRESULT hr = browserManager->CreatePopup(this, x, y, cx, cy, hMainWindow, NULL, popupFlags, &hPopup);
  153. if (SUCCEEDED(hr))
  154. {
  155. if (FAILED(ServiceSubclass_Attach(hPopup, this)))
  156. {
  157. ShowWindowAsync(hPopup, SW_SHOWNORMAL);
  158. }
  159. }
  160. return hr;
  161. }
  162. HRESULT UpdateService::Finish()
  163. {
  164. if (NULL != browserManager)
  165. {
  166. browserManager->Finish();
  167. browserManager->Release();
  168. browserManager = NULL;
  169. }
  170. if (NULL != url)
  171. {
  172. free(url);
  173. url = NULL;
  174. }
  175. return S_OK;
  176. }
  177. #define CBCLASS UpdateService
  178. START_DISPATCH;
  179. CB(ADDREF, AddRef)
  180. CB(RELEASE, Release)
  181. CB(QUERYINTERFACE, QueryInterface)
  182. CB(API_GETID, GetId)
  183. CB(API_GETNAME, GetName)
  184. CB(API_GETURL, GetUrl)
  185. CB(API_GETICON, GetIcon)
  186. CB(API_GETEXTERNAL, GetExternal)
  187. END_DISPATCH;
  188. #undef CBCLASS