importFile.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "main.h"
  2. #include "./import.h"
  3. #include "./wasabi.h"
  4. #include "./resource.h"
  5. #include "./serviceHost.h"
  6. #include "./serviceHelper.h"
  7. #include "./navigation.h"
  8. #include <ifc_omstorage.h>
  9. #include <ifc_omfilestorage.h>
  10. #include <ifc_omstorageenum.h>
  11. #include <ifc_omservice.h>
  12. #include <ifc_omserviceenum.h>
  13. #include <strsafe.h>
  14. static HRESULT ImportFile_GetEnumerator(ifc_omstorageenumerator **enumerator)
  15. {
  16. if (NULL == OMSERVICEMNGR) return E_UNEXPECTED;
  17. return OMSERVICEMNGR->EnumStorage(&STID_OmFileStorage, ifc_omstorage::capPublic | ifc_omstorage::capLoad, enumerator);
  18. }
  19. HRESULT ImportService_GetFileSupported()
  20. {
  21. if (NULL == OMSERVICEMNGR)
  22. return E_UNEXPECTED;
  23. ifc_omstorageenumerator *enumerator;
  24. HRESULT hr = ImportFile_GetEnumerator(&enumerator);
  25. if (SUCCEEDED(hr))
  26. {
  27. ifc_omstorage *storage;
  28. if(S_OK == enumerator->Next(1, &storage, NULL))
  29. {
  30. storage->Release();
  31. hr = S_OK;
  32. }
  33. else
  34. {
  35. hr = S_FALSE;
  36. }
  37. enumerator->Release();
  38. }
  39. return hr;
  40. }
  41. static HRESULT ImportFile_GetFilter(LPWSTR pszBuffer, UINT cchBufferMax, DWORD *defaultIndex)
  42. {
  43. if (NULL != defaultIndex)
  44. *defaultIndex = 0;
  45. if (NULL == pszBuffer)
  46. return E_POINTER;
  47. HRESULT hr;
  48. WCHAR szName[128] = {0}, szList[512] = {0};
  49. LPWSTR cursor = pszBuffer;
  50. size_t remaining = cchBufferMax;
  51. LPWSTR listC = szList;
  52. size_t listR = ARRAYSIZE(szList);
  53. DWORD counter = 0;
  54. szList[0] = L'\0';
  55. pszBuffer[0] = L'\0';
  56. WASABI_API_LNGSTRINGW_BUF(IDS_FILEFILTER_ALL, szName, ARRAYSIZE(szName));
  57. hr = Plugin_AppendFileFilter(cursor, remaining, szName, L"*.*", &cursor, &remaining, TRUE);
  58. if (FAILED(hr)) return hr;
  59. counter++;
  60. ifc_omstorageenumerator *enumerator;
  61. hr = ImportFile_GetEnumerator(&enumerator);
  62. if (SUCCEEDED(hr))
  63. {
  64. ifc_omstorage *storage;
  65. ifc_omfilestorage *fileStorage;
  66. while(S_OK == enumerator->Next(1, &storage, NULL))
  67. {
  68. if (SUCCEEDED(storage->QueryInterface(IFC_OmFileStorage, (void**)&fileStorage)))
  69. {
  70. WCHAR szFilter[64] = {0};
  71. if (SUCCEEDED(fileStorage->GetFilter(szFilter, ARRAYSIZE(szFilter))) &&
  72. L'\0' != szFilter[0] &&
  73. SUCCEEDED(storage->GetDescription(szName, ARRAYSIZE(szName))))
  74. {
  75. hr = Plugin_AppendFileFilter(cursor, remaining, szName, szFilter, &cursor, &remaining, TRUE);
  76. if (FAILED(hr)) break;
  77. counter++;
  78. if (listC == szList || SUCCEEDED(StringCchCopyEx(listC, listR, L";", &listC, &listR, 0)))
  79. StringCchCopyEx(listC, listR, szFilter, &listC, &listR, 0);
  80. }
  81. fileStorage->Release();
  82. }
  83. storage->Release();
  84. }
  85. enumerator->Release();
  86. }
  87. if (SUCCEEDED(hr) && L'\0' != szList[0])
  88. {
  89. WASABI_API_LNGSTRINGW_BUF(IDS_FILEFILTER_ALLKNOWN, szName, ARRAYSIZE(szName));
  90. hr = Plugin_AppendFileFilter(cursor, remaining, szName, szList, &cursor, &remaining, TRUE);
  91. if (FAILED(hr)) return hr;
  92. counter++;
  93. if (NULL != defaultIndex)
  94. *defaultIndex = counter;
  95. }
  96. return hr;
  97. }
  98. static HRESULT ImportFile_ProcessFile(HWND hOwner, ifc_omstorageenumerator *enumerator,
  99. ifc_omservicehost *serviceHost, ifc_omstorage *serviceStorage, LPCWSTR pszFile, ULONG *converted)
  100. {
  101. ifc_omstorage *storage;
  102. enumerator->Reset();
  103. Navigation *navigation;
  104. if (FAILED(Plugin_GetNavigation(&navigation)))
  105. return E_FAIL;
  106. ULONG loaded(0), saved(0);
  107. while(S_OK == enumerator->Next(1, &storage, NULL))
  108. {
  109. ifc_omserviceenum *serviceEnum;
  110. HRESULT hr = storage->Load(pszFile, serviceHost, &serviceEnum);
  111. if(SUCCEEDED(hr))
  112. {
  113. ifc_omservice *service;
  114. while(S_OK == serviceEnum->Next(1, &service, NULL))
  115. {
  116. loaded++;
  117. if (SUCCEEDED(service->SetAddress(NULL)))
  118. {
  119. ULONG savedOk;
  120. if (SUCCEEDED(serviceStorage->Save(&service, 1, ifc_omstorage::saveClearModified, &savedOk)))
  121. {
  122. navigation->CreateItem(service);
  123. saved += savedOk;
  124. }
  125. }
  126. service->Release();
  127. }
  128. serviceEnum->Release();
  129. break;
  130. }
  131. else if (OMSTORAGE_E_UNKNOWN_FORMAT != hr)
  132. {
  133. break;
  134. }
  135. storage->Release();
  136. }
  137. if (NULL != converted)
  138. *converted = saved;
  139. navigation->Release();
  140. return S_OK;
  141. }
  142. static HRESULT ImportFile_ProcessList(HWND hOwner, LPCWSTR pszList)
  143. {
  144. if (NULL == pszList)
  145. return E_INVALIDARG;
  146. LPCWSTR base, block, c;
  147. base = pszList;
  148. c = base;
  149. block = NULL;
  150. ULONG converted;
  151. HRESULT hr;
  152. ifc_omstorageenumerator *enumerator;
  153. hr = ImportFile_GetEnumerator(&enumerator);
  154. if (FAILED(hr)) return hr;
  155. WebDevServiceHost *serviceHost;
  156. hr = WebDevServiceHost::GetCachedInstance(&serviceHost);
  157. if (SUCCEEDED(hr))
  158. {
  159. ifc_omstorage *serviceStorage;
  160. hr = ServiceHelper_QueryStorage(&serviceStorage);
  161. if (SUCCEEDED(hr))
  162. {
  163. ULONG scanned(0);
  164. while(L'\0' != *c)
  165. {
  166. block = c;
  167. while (L'\0' != *c) c++;
  168. if (c != block && block != base)
  169. {
  170. WCHAR szBuffer[MAX_PATH * 2] = {0};
  171. scanned++;
  172. if (SUCCEEDED(StringCchPrintf(szBuffer, ARRAYSIZE(szBuffer), L"%s\\%s", base, block)) &&
  173. SUCCEEDED(ImportFile_ProcessFile(hOwner, enumerator, serviceHost, serviceStorage, szBuffer, &converted)))
  174. {
  175. }
  176. }
  177. c++;
  178. }
  179. if (pszList == block && c != pszList &&
  180. SUCCEEDED(ImportFile_ProcessFile(hOwner, enumerator, serviceHost, serviceStorage, pszList, &converted)))
  181. {
  182. }
  183. serviceStorage->Release();
  184. }
  185. serviceHost->Release();
  186. }
  187. enumerator->Release();
  188. return hr;
  189. }
  190. HRESULT ImportService_FromFile(HWND hOwner)
  191. {
  192. if (NULL == OMSERVICEMNGR)
  193. return E_UNEXPECTED;
  194. HRESULT hr;
  195. OPENFILENAME of;
  196. ZeroMemory(&of, sizeof(of));
  197. of.lStructSize = sizeof(of);
  198. WCHAR szFilter[1024];
  199. hr = ImportFile_GetFilter(szFilter, ARRAYSIZE(szFilter), &of.nFilterIndex);
  200. if (FAILED(hr)) return hr;
  201. UINT cchResultMax = 16384;
  202. LPWSTR pszResult = Plugin_MallocString(cchResultMax);
  203. if (NULL == pszResult) return E_OUTOFMEMORY;
  204. *pszResult = L'\0';
  205. of.hwndOwner = hOwner;
  206. of.lpstrFilter = szFilter;
  207. of.lpstrFile = pszResult;
  208. of.nMaxFile = cchResultMax;
  209. of.lpstrInitialDir = WASABI_API_APP->path_getUserSettingsPath();
  210. of.lpstrTitle = WASABI_API_LNGSTRINGW(IDS_IMPORT_FILES);
  211. of.Flags = OFN_ENABLESIZING | OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_ALLOWMULTISELECT;
  212. if (0 == GetOpenFileName(&of))
  213. {
  214. INT err = CommDlgExtendedError();
  215. hr = (0 == err) ? S_FALSE : E_FAIL;
  216. }
  217. else
  218. {
  219. hr = ImportFile_ProcessList(hOwner, pszResult);
  220. }
  221. Plugin_FreeString(pszResult);
  222. return hr;
  223. }