1
0

storageIni.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #include "main.h"
  2. #include "./storageIni.h"
  3. #include "./resource.h"
  4. #include "./ifc_omservice.h"
  5. #include "./loaderIni.h"
  6. #include "./enumIniFile.h"
  7. #include "./enumAsync.h"
  8. #include "./ifc_omservicehost.h"
  9. #include "./ifc_omservicehostext.h"
  10. #include "./ifc_omstoragehandlerenum.h"
  11. #include "./ifc_omstorageext.h"
  12. #include <strsafe.h>
  13. OmStorageIni::OmStorageIni()
  14. : ref(1)
  15. {
  16. }
  17. OmStorageIni::~OmStorageIni()
  18. {
  19. }
  20. HRESULT OmStorageIni::CreateInstance(OmStorageIni **instance)
  21. {
  22. if (NULL == instance) return E_POINTER;
  23. *instance = new OmStorageIni();
  24. if (NULL == *instance) return E_OUTOFMEMORY;
  25. return S_OK;
  26. }
  27. size_t OmStorageIni::AddRef()
  28. {
  29. return InterlockedIncrement((LONG*)&ref);
  30. }
  31. size_t OmStorageIni::Release()
  32. {
  33. if (0 == ref)
  34. return ref;
  35. LONG r = InterlockedDecrement((LONG*)&ref);
  36. if (0 == r)
  37. delete(this);
  38. return r;
  39. }
  40. int OmStorageIni::QueryInterface(GUID interface_guid, void **object)
  41. {
  42. if (NULL == object) return E_POINTER;
  43. if (IsEqualIID(interface_guid, IFC_OmStorage))
  44. *object = static_cast<ifc_omstorage*>(this);
  45. else if (IsEqualIID(interface_guid, STID_OmFileStorage))
  46. *object = static_cast<ifc_omfilestorage*>(this);
  47. else if (IsEqualIID(interface_guid, SUID_OmStorageIni))
  48. *object = static_cast<ifc_omstorage*>(this);
  49. else
  50. {
  51. *object = NULL;
  52. return E_NOINTERFACE;
  53. }
  54. if (NULL == *object)
  55. return E_UNEXPECTED;
  56. AddRef();
  57. return S_OK;
  58. }
  59. HRESULT OmStorageIni::GetId(GUID *storageUid)
  60. {
  61. if (NULL == storageUid) return E_POINTER;
  62. *storageUid = SUID_OmStorageIni;
  63. return S_OK;
  64. }
  65. HRESULT OmStorageIni::GetType(GUID *storageType)
  66. {
  67. if (NULL == storageType) return E_POINTER;
  68. *storageType = STID_OmFileStorage;
  69. return S_OK;
  70. }
  71. UINT OmStorageIni::GetCapabilities()
  72. {
  73. return capLoad | capSave | capDelete | capReload | capPublic;
  74. }
  75. HRESULT OmStorageIni::GetDescription(LPWSTR pszBuffer, UINT cchBufferMax)
  76. {
  77. if (NULL == pszBuffer) return E_POINTER;
  78. Plugin_LoadString(IDS_STORAGE_INI, pszBuffer, cchBufferMax);
  79. return S_OK;
  80. }
  81. HRESULT OmStorageIni::Load(LPCWSTR pszAddress, ifc_omservicehost *host, ifc_omserviceenum **ppEnum)
  82. {
  83. if (NULL == ppEnum) return E_POINTER;
  84. *ppEnum = NULL;
  85. if (NULL == pszAddress || L'\0' == *pszAddress)
  86. return E_INVALIDARG;
  87. return EnumIniFile::CreateInstance(pszAddress, host, (EnumIniFile**)ppEnum);
  88. }
  89. HRESULT OmStorageIni::Save(ifc_omservice **serviceList, ULONG listCount, UINT saveFlags, ULONG *savedCount)
  90. {
  91. if (NULL != savedCount)
  92. *savedCount = 0;
  93. if(NULL == serviceList && 0 != listCount)
  94. return E_INVALIDARG;
  95. HRESULT hr = S_OK;
  96. ULONG saved = 0;
  97. LoaderIni loader;
  98. for(ULONG i = 0; i < listCount; i++)
  99. {
  100. ifc_omservice *service = serviceList[i];
  101. if (NULL == service) continue;
  102. if (FAILED(loader.Save(service, saveFlags)))
  103. hr = E_FAIL;
  104. else
  105. saved++;
  106. }
  107. if (NULL != savedCount)
  108. *savedCount = saved++;
  109. return hr;
  110. }
  111. HRESULT OmStorageIni::Delete(ifc_omservice **serviceList, ULONG listCount, ULONG *deletedCount)
  112. {
  113. if (NULL == serviceList)
  114. {
  115. if (NULL != deletedCount) *deletedCount = 0;
  116. return E_INVALIDARG;
  117. }
  118. WCHAR szBuffer[MAX_PATH * 2] = {0};
  119. ULONG deleted = 0;
  120. for(ULONG i = 0; i < listCount; i++)
  121. {
  122. if (SUCCEEDED(serviceList[i]->GetAddress(szBuffer, ARRAYSIZE(szBuffer))) &&
  123. 0 != DeleteFile(szBuffer))
  124. {
  125. deleted++;
  126. }
  127. }
  128. if (NULL != deletedCount)
  129. *deletedCount = deleted;
  130. return S_OK;
  131. }
  132. HRESULT OmStorageIni::Reload(ifc_omservice **serviceList, ULONG listCount, ULONG *reloadedCount)
  133. {
  134. if (NULL != reloadedCount)
  135. *reloadedCount = 0;
  136. if(NULL == serviceList && 0 != listCount)
  137. return E_INVALIDARG;
  138. HRESULT hr = S_OK;
  139. ULONG loaded = 0;
  140. LoaderIni loader;
  141. for(ULONG i = 0; i < listCount; i++)
  142. {
  143. ifc_omservice *service = serviceList[i];
  144. if (NULL == service) continue;
  145. loader.RegisterHandlers(NULL);
  146. ifc_omservicehostext *serviceExt = NULL;
  147. if (service->QueryInterface(IFC_OmServiceHostExt, (void**)&serviceExt))
  148. {
  149. ifc_omservicehost *serviceHost = NULL;
  150. if (SUCCEEDED(serviceExt->GetHost(&serviceHost)) && serviceHost != NULL)
  151. {
  152. ifc_omstorageext *storageExt = NULL;
  153. if (SUCCEEDED(serviceHost->QueryInterface(IFC_OmStorageExt, (void**)&storageExt)) && storageExt != NULL)
  154. {
  155. ifc_omstoragehandlerenum *handlerEnum = NULL;
  156. if (SUCCEEDED(storageExt->Enumerate(&SUID_OmStorageIni, &handlerEnum)) && handlerEnum != NULL)
  157. {
  158. loader.RegisterHandlers(handlerEnum);
  159. handlerEnum->Release();
  160. }
  161. storageExt->Release();
  162. }
  163. serviceHost->Release();
  164. }
  165. serviceExt->Release();
  166. }
  167. if (FAILED(loader.Reload(service)))
  168. hr = E_FAIL;
  169. else
  170. loaded++;
  171. }
  172. if (NULL != reloadedCount)
  173. *reloadedCount = loaded++;
  174. return hr;
  175. }
  176. HRESULT OmStorageIni::BeginLoad(LPCWSTR pszAddress, ifc_omservicehost *serviceHost, ifc_omstorageasync::AsyncCallback callback, void *data, ifc_omstorageasync **async)
  177. {
  178. if (NULL == async)
  179. return E_POINTER;
  180. ifc_omserviceenum *enumerator = NULL;
  181. HRESULT hr = Load(pszAddress, serviceHost, &enumerator);
  182. if (SUCCEEDED(hr) && enumerator != NULL)
  183. {
  184. EnumAsyncWrapper *asyncWrapper = NULL;
  185. hr = EnumAsyncWrapper::CreateInstance(enumerator, &asyncWrapper);
  186. if (SUCCEEDED(hr) && asyncWrapper != NULL)
  187. {
  188. asyncWrapper->SetCallback(callback);
  189. asyncWrapper->SetData(data);
  190. *async = asyncWrapper;
  191. hr = asyncWrapper->BeginEnumerate();
  192. if (FAILED(hr))
  193. {
  194. *async = NULL;
  195. asyncWrapper->Release();
  196. }
  197. }
  198. enumerator->Release();
  199. }
  200. return hr;
  201. }
  202. HRESULT OmStorageIni::EndLoad(ifc_omstorageasync *async, ifc_omserviceenum **ppEnum)
  203. {
  204. EnumAsyncWrapper *asyncWrapper = (EnumAsyncWrapper*)async;
  205. if (NULL == asyncWrapper) return E_INVALIDARG;
  206. if (NULL != ppEnum)
  207. *ppEnum = NULL;
  208. UINT state = 0;
  209. if (FAILED(async->GetState(&state)) || ifc_omstorageasync::stateCompleted != state)
  210. {
  211. HANDLE completed = NULL;
  212. if (FAILED(async->GetWaitHandle(&completed)))
  213. return E_UNEXPECTED;
  214. while(WAIT_OBJECT_0 != WaitForSingleObjectEx(completed, INFINITE, TRUE));
  215. CloseHandle(completed);
  216. }
  217. HRESULT hr = asyncWrapper->GetResultCode();
  218. if (SUCCEEDED(hr))
  219. {
  220. if (NULL != ppEnum)
  221. {
  222. hr = asyncWrapper->GetServiceList(ppEnum);
  223. }
  224. }
  225. return hr;
  226. }
  227. HRESULT OmStorageIni::RequestAbort(ifc_omstorageasync *async, BOOL fDrop)
  228. {
  229. EnumAsyncWrapper *asyncWrapper = (EnumAsyncWrapper*)async;
  230. if (NULL == asyncWrapper) return E_INVALIDARG;
  231. return asyncWrapper->RequestAbort(fDrop);
  232. }
  233. HRESULT OmStorageIni::GetFilter(LPWSTR pszBuffer, UINT cchBufferMax)
  234. {
  235. if (NULL == pszBuffer) return E_POINTER;
  236. return StringCchCopy(pszBuffer, cchBufferMax, L"*.ini");
  237. }
  238. #define CBCLASS OmStorageIni
  239. START_MULTIPATCH;
  240. START_PATCH(MPIID_OMSTORAGE)
  241. M_CB(MPIID_OMSTORAGE, ifc_omstorage, ADDREF, AddRef);
  242. M_CB(MPIID_OMSTORAGE, ifc_omstorage, RELEASE, Release);
  243. M_CB(MPIID_OMSTORAGE, ifc_omstorage, QUERYINTERFACE, QueryInterface);
  244. M_CB(MPIID_OMSTORAGE, ifc_omstorage, API_GETID, GetId);
  245. M_CB(MPIID_OMSTORAGE, ifc_omstorage, API_GETTYPE, GetType);
  246. M_CB(MPIID_OMSTORAGE, ifc_omstorage, API_GETCAPABILITIES, GetCapabilities);
  247. M_CB(MPIID_OMSTORAGE, ifc_omstorage, API_GETDESCRIPTION, GetDescription);
  248. M_CB(MPIID_OMSTORAGE, ifc_omstorage, API_LOAD, Load);
  249. M_CB(MPIID_OMSTORAGE, ifc_omstorage, API_SAVE, Save);
  250. M_CB(MPIID_OMSTORAGE, ifc_omstorage, API_DELETE, Delete);
  251. M_CB(MPIID_OMSTORAGE, ifc_omstorage, API_RELOAD, Reload);
  252. M_CB(MPIID_OMSTORAGE, ifc_omstorage, API_BEGINLOAD, BeginLoad);
  253. M_CB(MPIID_OMSTORAGE, ifc_omstorage, API_ENDLOAD, EndLoad);
  254. M_CB(MPIID_OMSTORAGE, ifc_omstorage, API_REQUESTABORT, RequestAbort);
  255. NEXT_PATCH(MPIID_OMFILESTORAGE)
  256. M_CB(MPIID_OMFILESTORAGE, ifc_omfilestorage, ADDREF, AddRef);
  257. M_CB(MPIID_OMFILESTORAGE, ifc_omfilestorage, RELEASE, Release);
  258. M_CB(MPIID_OMFILESTORAGE, ifc_omfilestorage, QUERYINTERFACE, QueryInterface);
  259. M_CB(MPIID_OMFILESTORAGE, ifc_omfilestorage, API_GETFILTER, GetFilter);
  260. END_PATCH
  261. END_MULTIPATCH;
  262. #undef CBCLASS