1
0

importUrl.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #include "main.h"
  2. #include "./import.h"
  3. #include "./wasabi.h"
  4. #include "./serviceHost.h"
  5. #include "./serviceHelper.h"
  6. #include "./navigation.h"
  7. #include "./resource.h"
  8. #include "./config.h"
  9. #include <ifc_omstorage.h>
  10. #include <ifc_omwebstorage.h>
  11. #include <ifc_omstorageenum.h>
  12. #include <ifc_omservice.h>
  13. #include <ifc_omserviceenum.h>
  14. #include <strsafe.h>
  15. typedef std::vector<WCHAR*> StringList;
  16. static StringList recentList;
  17. static BOOL recentListModified = FALSE;
  18. typedef struct __OPENURLDLG
  19. {
  20. HWND hOwner;
  21. LPCWSTR pszAddress;
  22. LPWSTR pszBuffer;
  23. UINT cchBufferMax;
  24. } OPENURLDLG;
  25. static INT_PTR ImportUrlDlg_Show(OPENURLDLG *poud);
  26. static HRESULT ImportUrl_GetEnumerator(ifc_omstorageenumerator **enumerator)
  27. {
  28. if (NULL == OMSERVICEMNGR) return E_UNEXPECTED;
  29. return OMSERVICEMNGR->EnumStorage(&STID_OmWebStorage, ifc_omstorage::capPublic | ifc_omstorage::capLoad, enumerator);
  30. }
  31. HRESULT ImportService_GetUrlSupported()
  32. {
  33. if (NULL == OMSERVICEMNGR)
  34. return E_UNEXPECTED;
  35. ifc_omstorageenumerator *enumerator;
  36. HRESULT hr = ImportUrl_GetEnumerator(&enumerator);
  37. if (SUCCEEDED(hr))
  38. {
  39. ifc_omstorage *storage;
  40. if(S_OK == enumerator->Next(1, &storage, NULL))
  41. {
  42. storage->Release();
  43. hr = S_OK;
  44. }
  45. else
  46. {
  47. hr = S_FALSE;
  48. }
  49. enumerator->Release();
  50. }
  51. return hr;
  52. }
  53. HRESULT ImportUrl_LoadAddress(HWND hOwner, LPCWSTR pszAddress, ifc_omstorageenumerator *enumerator,
  54. WebDevServiceHost *serviceHost, ifc_omstorage *serviceStorage, Navigation *navigation)
  55. {
  56. HRESULT hr = S_OK;
  57. ULONG loaded(0);
  58. ifc_omstorage *storage;
  59. enumerator->Reset();
  60. while(S_OK == enumerator->Next(1, &storage, NULL))
  61. {
  62. ifc_omstorageasync *async;
  63. hr = storage->BeginLoad(pszAddress, serviceHost, NULL, NULL, &async);
  64. if(SUCCEEDED(hr))
  65. {
  66. ifc_omserviceenum *serviceEnum;
  67. hr = storage->EndLoad(async, &serviceEnum);
  68. async->Release();
  69. if (SUCCEEDED(hr))
  70. {
  71. ifc_omservice *service;
  72. while(S_OK == serviceEnum->Next(1, &service, NULL))
  73. {
  74. loaded++;
  75. if (SUCCEEDED(service->SetAddress(NULL)))
  76. {
  77. ULONG savedOk;
  78. if (SUCCEEDED(serviceStorage->Save(&service, 1, ifc_omstorage::saveClearModified, &savedOk)))
  79. {
  80. navigation->CreateItem(service);
  81. }
  82. }
  83. service->Release();
  84. }
  85. serviceEnum->Release();
  86. }
  87. break;
  88. }
  89. else if (OMSTORAGE_E_UNKNOWN_FORMAT != hr)
  90. {
  91. break;
  92. }
  93. storage->Release();
  94. }
  95. return hr;
  96. }
  97. HRESULT ImportService_FromUrl(HWND hOwner)
  98. {
  99. OPENURLDLG dlg = {0};
  100. WCHAR szBuffer[4096] = {0};
  101. dlg.hOwner = hOwner;
  102. dlg.pszAddress = NULL;
  103. dlg.pszBuffer = szBuffer;
  104. dlg.cchBufferMax = ARRAYSIZE(szBuffer);
  105. if (IDOK != ImportUrlDlg_Show(&dlg))
  106. return S_FALSE;
  107. ifc_omstorageenumerator *enumerator;
  108. HRESULT hr = ImportUrl_GetEnumerator(&enumerator);
  109. if (SUCCEEDED(hr))
  110. {
  111. Navigation *navigation;
  112. hr = Plugin_GetNavigation(&navigation);
  113. if (SUCCEEDED(hr))
  114. {
  115. ifc_omstorage *serviceStorage;
  116. hr = ServiceHelper_QueryStorage(&serviceStorage);
  117. if (SUCCEEDED(hr))
  118. {
  119. WebDevServiceHost *serviceHost;
  120. hr = WebDevServiceHost::GetCachedInstance(&serviceHost);
  121. if (SUCCEEDED(hr))
  122. {
  123. hr = ImportUrl_LoadAddress(hOwner, szBuffer, enumerator, serviceHost, serviceStorage, navigation);
  124. serviceHost->Release();
  125. }
  126. serviceStorage->Release();
  127. }
  128. navigation->Release();
  129. }
  130. enumerator->Release();
  131. }
  132. return hr;
  133. }
  134. static INT_PTR ImportUrlDlg_OnInit(HWND hwnd, HWND hFocus, LPARAM param)
  135. {
  136. OPENURLDLG *poud = (OPENURLDLG*)param;
  137. if (NULL != poud)
  138. {
  139. SetProp(hwnd, L"OPENURLDLG", poud);
  140. HWND hAddress = GetDlgItem(hwnd, IDC_ADDRESS);
  141. if (NULL != hAddress)
  142. {
  143. LPWSTR p;
  144. size_t count = recentList.size();
  145. if (0 == count)
  146. {
  147. char szKey[32] = {0}, szBuffer[4096] = {0};
  148. for(int i = 1; i < 101; i++)
  149. {
  150. if (FAILED(StringCchPrintfA(szKey, ARRAYSIZE(szKey), "entry%d", i)) ||
  151. 0 == Config_ReadStr("RecentUrl", szKey, NULL, szBuffer, ARRAYSIZE(szBuffer)) ||
  152. '\0' == szBuffer[0])
  153. {
  154. break;
  155. }
  156. p = Plugin_MultiByteToWideChar(CP_UTF8, 0, szBuffer, -1);
  157. if (NULL != p) recentList.push_back(p);
  158. }
  159. count = recentList.size();
  160. }
  161. for (size_t i = 0; i < count; i ++)
  162. {
  163. p = recentList[i];
  164. if(NULL != p && L'\0' != *p)
  165. SendMessage(hAddress, CB_ADDSTRING, 0, (LPARAM)p);
  166. }
  167. if (NULL != poud->pszAddress)
  168. SetWindowText(hAddress, poud->pszAddress);
  169. }
  170. RECT ownerRect;
  171. if (NULL != poud->hOwner && IsWindowVisible(poud->hOwner) &&
  172. GetWindowRect(poud->hOwner, &ownerRect))
  173. {
  174. RECT myRect;
  175. GetWindowRect(hwnd, &myRect);
  176. LONG x = ownerRect.left + ((ownerRect.right - ownerRect.left) - (myRect.right - myRect.left))/2;
  177. LONG y = ownerRect.top + ((ownerRect.bottom - ownerRect.top) - (myRect.bottom - myRect.top))/2;
  178. SetWindowPos(hwnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  179. }
  180. }
  181. return 0;
  182. }
  183. static void ImportUrlDlg_OnDestroy(HWND hwnd)
  184. {
  185. RemoveProp(hwnd, L"OPENURLDLG");
  186. }
  187. static INT_PTR ImportUrlDlg_ReturnAddress(HWND hwnd)
  188. {
  189. OPENURLDLG *poud = (OPENURLDLG*)GetProp(hwnd, L"OPENURLDLG");
  190. if (NULL == poud) return -1;
  191. HWND hAddress = GetDlgItem(hwnd, IDC_ADDRESS);
  192. if (NULL == hAddress) return -2;
  193. if (0 == GetWindowText(hAddress, poud->pszBuffer, poud->cchBufferMax))
  194. return -3;
  195. if (NULL != poud->pszBuffer && L'\0' != *poud->pszBuffer)
  196. {
  197. LPWSTR p;
  198. size_t index = recentList.size();
  199. while(index--)
  200. {
  201. p = recentList[index];
  202. if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, poud->pszBuffer, -1, p, -1))
  203. {
  204. Plugin_FreeString(p);
  205. recentList.erase(recentList.begin() + index);
  206. }
  207. }
  208. p = Plugin_CopyString(poud->pszBuffer);
  209. if (NULL != p)
  210. {
  211. //recentList.push_front(p);
  212. recentList.insert(recentList.begin(), p);
  213. recentListModified = TRUE;
  214. }
  215. }
  216. return IDOK;
  217. }
  218. static void ImportUrlDlg_OnCommand(HWND hwnd, INT commandId, INT eventId, HWND hControl)
  219. {
  220. switch(commandId)
  221. {
  222. case IDOK:
  223. EndDialog(hwnd, ImportUrlDlg_ReturnAddress(hwnd));
  224. break;
  225. case IDCANCEL:
  226. EndDialog(hwnd, IDCANCEL);
  227. break;
  228. }
  229. }
  230. static INT_PTR WINAPI ImportUrlDlg_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  231. {
  232. switch(uMsg)
  233. {
  234. case WM_INITDIALOG: return ImportUrlDlg_OnInit(hwnd, (HWND)wParam, lParam);
  235. case WM_DESTROY: ImportUrlDlg_OnDestroy(hwnd); break;
  236. case WM_COMMAND: ImportUrlDlg_OnCommand(hwnd, LOWORD(wParam), HIWORD(wParam), (HWND)lParam); break;
  237. }
  238. return 0;
  239. }
  240. static INT_PTR ImportUrlDlg_Show(OPENURLDLG *poud)
  241. {
  242. if (NULL == poud || NULL == poud->pszBuffer)
  243. return -1;
  244. HWND hParent = poud->hOwner;
  245. if (NULL == poud->hOwner)
  246. hParent = Plugin_GetLibrary();
  247. return WASABI_API_DIALOGBOXPARAMW(IDD_OPENURL, hParent, ImportUrlDlg_DialogProc, (LPARAM)poud);
  248. }
  249. void ImportService_SaveRecentUrl()
  250. {
  251. if (FALSE == recentListModified)
  252. return;
  253. Config_WriteSection("RecentUrl", NULL);
  254. size_t count = recentList.size();
  255. if (count > 100) count = 100;
  256. char szKey[32], szBuffer[4096];
  257. UINT entry = 1;
  258. for (size_t i = 0; i < count; i++)
  259. {
  260. LPCWSTR p = recentList[i];
  261. if (NULL != p && L'\0' != *p &&
  262. 0 != WideCharToMultiByte(CP_UTF8, 0, p, -1, szBuffer, ARRAYSIZE(szBuffer), NULL, NULL) &&
  263. SUCCEEDED(StringCchPrintfA(szKey, ARRAYSIZE(szKey), "entry%d", entry)) &&
  264. SUCCEEDED(Config_WriteStr("RecentUrl", szKey, szBuffer)))
  265. {
  266. entry++;
  267. }
  268. }
  269. recentListModified = FALSE;
  270. }