1
0

importFile.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #include "main.h"
  2. #include "./import.h"
  3. #include "./api__ml_online.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,
  100. LPCWSTR pszFile, ULONG *converted)
  101. {
  102. ifc_omstorage *storage;
  103. enumerator->Reset();
  104. Navigation *navigation;
  105. if (FAILED(Plugin_GetNavigation(&navigation)))
  106. return E_FAIL;
  107. ULONG loaded(0), saved(0);
  108. while(S_OK == enumerator->Next(1, &storage, NULL))
  109. {
  110. ifc_omserviceenum *serviceEnum;
  111. HRESULT hr = storage->Load(pszFile, serviceHost, &serviceEnum);
  112. if(SUCCEEDED(hr))
  113. {
  114. ifc_omservice *service;
  115. while(S_OK == serviceEnum->Next(1, &service, NULL))
  116. {
  117. loaded++;
  118. if (SUCCEEDED(service->SetAddress(NULL)))
  119. {
  120. service->UpdateFlags(SVCF_SUBSCRIBED | SVCF_PREAUTHORIZED);
  121. ULONG savedOk;
  122. if (SUCCEEDED(serviceStorage->Save(&service, 1, ifc_omstorage::saveClearModified, &savedOk)))
  123. {
  124. navigation->CreateItem(service, 1);
  125. saved += savedOk;
  126. }
  127. }
  128. service->Release();
  129. }
  130. serviceEnum->Release();
  131. break;
  132. }
  133. else if (OMSTORAGE_E_UNKNOWN_FORMAT != hr)
  134. {
  135. break;
  136. }
  137. storage->Release();
  138. }
  139. if (NULL != converted)
  140. *converted = saved;
  141. navigation->Release();
  142. return S_OK;
  143. }
  144. static HRESULT ImportFile_ProcessList(HWND hOwner, LPCWSTR pszList)
  145. {
  146. if (NULL == pszList)
  147. return E_INVALIDARG;
  148. LPCWSTR base, block, c;
  149. base = pszList;
  150. c = base;
  151. block = NULL;
  152. ULONG converted;
  153. ifc_omstorageenumerator *enumerator;
  154. HRESULT hr = ImportFile_GetEnumerator(&enumerator);
  155. if (FAILED(hr)) return hr;
  156. ServiceHost *serviceHost;
  157. hr = ServiceHost::GetCachedInstance(&serviceHost);
  158. if (SUCCEEDED(hr))
  159. {
  160. ifc_omstorage *serviceStorage;
  161. hr = ServiceHelper_QueryStorage(&serviceStorage);
  162. if (SUCCEEDED(hr))
  163. {
  164. ULONG scanned(0);
  165. while(L'\0' != *c)
  166. {
  167. block = c;
  168. while (L'\0' != *c) c++;
  169. if (c != block && block != base)
  170. {
  171. WCHAR szBuffer[MAX_PATH * 2] = {0};
  172. scanned++;
  173. if (SUCCEEDED(StringCchPrintf(szBuffer, ARRAYSIZE(szBuffer), L"%s\\%s", base, block)) &&
  174. SUCCEEDED(ImportFile_ProcessFile(hOwner, enumerator, serviceHost, serviceStorage, szBuffer, &converted)))
  175. {
  176. }
  177. }
  178. c++;
  179. }
  180. if (pszList == block && c != pszList &&
  181. SUCCEEDED(ImportFile_ProcessFile(hOwner, enumerator, serviceHost, serviceStorage, pszList, &converted)))
  182. {
  183. }
  184. serviceStorage->Release();
  185. }
  186. serviceHost->Release();
  187. }
  188. enumerator->Release();
  189. return hr;
  190. }
  191. HRESULT ImportService_FromFile(HWND hOwner)
  192. {
  193. if (NULL == OMSERVICEMNGR)
  194. return E_UNEXPECTED;
  195. OPENFILENAME of = {0};
  196. of.lStructSize = sizeof(of);
  197. WCHAR szFilter[1024] = {0};
  198. HRESULT hr = ImportFile_GetFilter(szFilter, ARRAYSIZE(szFilter), &of.nFilterIndex);
  199. if (FAILED(hr)) return hr;
  200. UINT cchResultMax = 16384;
  201. LPWSTR pszResult = Plugin_MallocString(cchResultMax);
  202. if (NULL == pszResult) return E_OUTOFMEMORY;
  203. *pszResult = L'\0';
  204. of.hwndOwner = hOwner;
  205. of.lpstrFilter = szFilter;
  206. of.lpstrFile = pszResult;
  207. of.nMaxFile = cchResultMax;
  208. of.lpstrInitialDir = WASABI_API_APP->path_getUserSettingsPath();
  209. of.lpstrTitle = WASABI_API_LNGSTRINGW(IDS_IMPORT_FILES);
  210. of.Flags = OFN_ENABLESIZING | OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_ALLOWMULTISELECT;
  211. if (0 == GetOpenFileName(&of))
  212. {
  213. INT err = CommDlgExtendedError();
  214. hr = (0 == err) ? S_FALSE : E_FAIL;
  215. }
  216. else
  217. {
  218. hr = ImportFile_ProcessList(hOwner, pszResult);
  219. }
  220. Plugin_FreeString(pszResult);
  221. return hr;
  222. }