importUrl.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "main.h"
  2. #include "./import.h"
  3. #include "./api__ml_online.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 <vector>
  15. #include <strsafe.h>
  16. typedef std::vector<WCHAR*> StringList;
  17. static StringList recentList;
  18. static BOOL recentListModified = FALSE;
  19. typedef struct __OPENURLDLG
  20. {
  21. HWND hOwner;
  22. LPCWSTR pszAddress;
  23. LPWSTR pszBuffer;
  24. UINT cchBufferMax;
  25. } OPENURLDLG;
  26. static INT_PTR ImportUrlDlg_Show(OPENURLDLG *poud);
  27. static HRESULT ImportUrl_GetEnumerator(ifc_omstorageenumerator **enumerator)
  28. {
  29. if (NULL == OMSERVICEMNGR) return E_UNEXPECTED;
  30. return OMSERVICEMNGR->EnumStorage(&STID_OmWebStorage, ifc_omstorage::capPublic | ifc_omstorage::capLoad, enumerator);
  31. }
  32. HRESULT ImportService_GetUrlSupported()
  33. {
  34. if (NULL == OMSERVICEMNGR)
  35. return E_UNEXPECTED;
  36. ifc_omstorageenumerator *enumerator;
  37. HRESULT hr = ImportUrl_GetEnumerator(&enumerator);
  38. if (SUCCEEDED(hr))
  39. {
  40. ifc_omstorage *storage;
  41. if(S_OK == enumerator->Next(1, &storage, NULL))
  42. {
  43. storage->Release();
  44. hr = S_OK;
  45. }
  46. else
  47. {
  48. hr = S_FALSE;
  49. }
  50. enumerator->Release();
  51. }
  52. return hr;
  53. }
  54. HRESULT ImportUrl_LoadAddress(HWND hOwner, LPCWSTR pszAddress, ifc_omstorageenumerator *enumerator,
  55. ServiceHost *serviceHost, ifc_omstorage *serviceStorage, Navigation *navigation)
  56. {
  57. HRESULT hr = S_OK;
  58. ULONG loaded(0);
  59. ifc_omstorage *storage;
  60. enumerator->Reset();
  61. while(S_OK == enumerator->Next(1, &storage, NULL))
  62. {
  63. ifc_omstorageasync *async;
  64. hr = storage->BeginLoad(pszAddress, serviceHost, NULL, NULL, &async);
  65. if(SUCCEEDED(hr))
  66. {
  67. ifc_omserviceenum *serviceEnum;
  68. hr = storage->EndLoad(async, &serviceEnum);
  69. async->Release();
  70. if (SUCCEEDED(hr))
  71. {
  72. ifc_omservice *service;
  73. while(S_OK == serviceEnum->Next(1, &service, NULL))
  74. {
  75. loaded++;
  76. if (SUCCEEDED(service->SetAddress(NULL)))
  77. {
  78. ULONG savedOk;
  79. if (SUCCEEDED(serviceStorage->Save(&service, 1, ifc_omstorage::saveClearModified, &savedOk)))
  80. {
  81. navigation->CreateItem(service, 1);
  82. }
  83. }
  84. service->Release();
  85. }
  86. serviceEnum->Release();
  87. }
  88. break;
  89. }
  90. else if (OMSTORAGE_E_UNKNOWN_FORMAT != hr)
  91. {
  92. break;
  93. }
  94. storage->Release();
  95. }
  96. return hr;
  97. }
  98. HRESULT ImportService_FromUrl(HWND hOwner)
  99. {
  100. OPENURLDLG dlg = {0};
  101. WCHAR szBuffer[4096] = {0};
  102. dlg.hOwner = hOwner;
  103. dlg.pszAddress = NULL;
  104. dlg.pszBuffer = szBuffer;
  105. dlg.cchBufferMax = ARRAYSIZE(szBuffer);
  106. if (IDOK != ImportUrlDlg_Show(&dlg))
  107. return S_FALSE;
  108. ifc_omstorageenumerator *enumerator;
  109. HRESULT hr = ImportUrl_GetEnumerator(&enumerator);
  110. if (SUCCEEDED(hr))
  111. {
  112. Navigation *navigation;
  113. hr = Plugin_GetNavigation(&navigation);
  114. if (SUCCEEDED(hr))
  115. {
  116. ifc_omstorage *serviceStorage;
  117. hr = ServiceHelper_QueryStorage(&serviceStorage);
  118. if (SUCCEEDED(hr))
  119. {
  120. ServiceHost *serviceHost;
  121. hr = ServiceHost::GetCachedInstance(&serviceHost);
  122. if (SUCCEEDED(hr))
  123. {
  124. hr = ImportUrl_LoadAddress(hOwner, szBuffer, enumerator, serviceHost, serviceStorage, navigation);
  125. serviceHost->Release();
  126. }
  127. serviceStorage->Release();
  128. }
  129. navigation->Release();
  130. }
  131. enumerator->Release();
  132. }
  133. return hr;
  134. }
  135. static INT_PTR ImportUrlDlg_OnInit(HWND hwnd, HWND hFocus, LPARAM param)
  136. {
  137. OPENURLDLG *poud = (OPENURLDLG*)param;
  138. if (NULL != poud)
  139. {
  140. SetProp(hwnd, L"OPENURLDLG", poud);
  141. HWND hAddress = GetDlgItem(hwnd, IDC_ADDRESS);
  142. if (NULL != hAddress)
  143. {
  144. LPWSTR p;
  145. size_t count = recentList.size();
  146. if (0 == count)
  147. {
  148. char szKey[32] = {0}, szBuffer[4096] = {0};
  149. for(int i = 1; i < 101; i++)
  150. {
  151. if (FAILED(StringCchPrintfA(szKey, ARRAYSIZE(szKey), "entry%d", i)) ||
  152. 0 == Config_ReadStr("RecentUrl", szKey, NULL, szBuffer, ARRAYSIZE(szBuffer)) ||
  153. '\0' == szBuffer[0])
  154. {
  155. break;
  156. }
  157. p = Plugin_MultiByteToWideChar(CP_UTF8, 0, szBuffer, -1);
  158. if (NULL != p) recentList.push_back(p);
  159. }
  160. count = recentList.size();
  161. }
  162. for (size_t i = 0; i < count; i ++)
  163. {
  164. p = recentList[i];
  165. if(NULL != p && L'\0' != *p)
  166. SendMessage(hAddress, CB_ADDSTRING, 0, (LPARAM)p);
  167. }
  168. if (NULL != poud->pszAddress)
  169. SetWindowText(hAddress, poud->pszAddress);
  170. }
  171. RECT ownerRect;
  172. if (NULL != poud->hOwner && IsWindowVisible(poud->hOwner) &&
  173. GetWindowRect(poud->hOwner, &ownerRect))
  174. {
  175. RECT myRect;
  176. GetWindowRect(hwnd, &myRect);
  177. LONG x = ownerRect.left + ((ownerRect.right - ownerRect.left) - (myRect.right - myRect.left))/2;
  178. LONG y = ownerRect.top + ((ownerRect.bottom - ownerRect.top) - (myRect.bottom - myRect.top))/2;
  179. SetWindowPos(hwnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  180. }
  181. }
  182. return 0;
  183. }
  184. static void ImportUrlDlg_OnDestroy(HWND hwnd)
  185. {
  186. RemoveProp(hwnd, L"OPENURLDLG");
  187. }
  188. static INT_PTR ImportUrlDlg_ReturnAddress(HWND hwnd)
  189. {
  190. OPENURLDLG *poud = (OPENURLDLG*)GetProp(hwnd, L"OPENURLDLG");
  191. if (NULL == poud) return -1;
  192. HWND hAddress = GetDlgItem(hwnd, IDC_ADDRESS);
  193. if (NULL == hAddress) return -2;
  194. if (0 == GetWindowText(hAddress, poud->pszBuffer, poud->cchBufferMax))
  195. return -3;
  196. if (NULL != poud->pszBuffer && L'\0' != *poud->pszBuffer)
  197. {
  198. LPWSTR p;
  199. size_t index = recentList.size();
  200. while(index--)
  201. {
  202. p = recentList[index];
  203. if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, poud->pszBuffer, -1, p, -1))
  204. {
  205. Plugin_FreeString(p);
  206. recentList.erase(recentList.begin() + index);
  207. }
  208. }
  209. p = Plugin_CopyString(poud->pszBuffer);
  210. if (NULL != p)
  211. {
  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. }