Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

522 lines
11 KiB

  1. /*
  2. * f o l d e r s . c p p
  3. *
  4. * Purpose:
  5. * Implements the OE-MOM 'Folder' object and 'FolderCollection'
  6. *
  7. * History
  8. *
  9. * Copyright (C) Microsoft Corp. 1995, 1996.
  10. */
  11. #include <pch.hxx>
  12. #include "msoeobj.h"
  13. #include "folders.h"
  14. #include "instance.h"
  15. //+---------------------------------------------------------------
  16. //
  17. // Member: Constructor
  18. //
  19. // Synopsis:
  20. //
  21. //
  22. //---------------------------------------------------------------
  23. COEFolderCollection::COEFolderCollection() : CBaseDisp()
  24. {
  25. Assert (g_pInstance);
  26. m_pEnumChildren = 0;
  27. CoIncrementInit("COEFolderCollection::COEFolderCollection", MSOEAPI_START_SHOWERRORS, NULL, NULL);
  28. }
  29. //+---------------------------------------------------------------
  30. //
  31. // Member: Destructor
  32. //
  33. // Synopsis:
  34. //
  35. //
  36. //---------------------------------------------------------------
  37. COEFolderCollection::~COEFolderCollection()
  38. {
  39. Assert (g_pInstance);
  40. CoDecrementInit("COEFolderCollection::COEFolderCollection", NULL);
  41. }
  42. //+---------------------------------------------------------------
  43. //
  44. // Member: Init
  45. //
  46. // Synopsis:
  47. // Constructor that can fail
  48. //
  49. //---------------------------------------------------------------
  50. HRESULT COEFolderCollection::Init(FOLDERID idFolder)
  51. {
  52. m_idFolder = idFolder;
  53. return CBaseDisp::EnsureTypeLibrary((LPVOID *)(IOEFolderCollection *)this, IID_IOEFolderCollection);
  54. }
  55. //+---------------------------------------------------------------
  56. //
  57. // Member: PrivateQueryInterface
  58. //
  59. // Synopsis:
  60. // Exposes supported interfaces
  61. //
  62. //---------------------------------------------------------------
  63. HRESULT COEFolderCollection::PrivateQueryInterface(REFIID riid, LPVOID *lplpObj)
  64. {
  65. if(!lplpObj)
  66. return E_INVALIDARG;
  67. *lplpObj = NULL;
  68. if (IsEqualIID(riid, IID_IUnknown))
  69. *lplpObj = (LPVOID)(IOEFolderCollection *)this;
  70. else if (IsEqualIID(riid, IID_IOEFolderCollection))
  71. *lplpObj = (LPVOID)(IOEFolderCollection *)this;
  72. else
  73. return CBaseDisp::PrivateQueryInterface(riid, lplpObj);
  74. AddRef();
  75. return NOERROR;
  76. }
  77. //+---------------------------------------------------------------
  78. //
  79. // Member: get_folders
  80. //
  81. // Synopsis:
  82. // Returns the a folder collection, representing
  83. // the child folders of the current folder collection.
  84. //
  85. //---------------------------------------------------------------
  86. HRESULT COEFolderCollection::get_folders(IOEFolderCollection **p)
  87. {
  88. return CreateFolderCollection(m_idFolder, p);
  89. }
  90. //+---------------------------------------------------------------
  91. //
  92. // Member: get_length
  93. //
  94. // Synopsis:
  95. // returns the number of elements in the collection
  96. //
  97. //---------------------------------------------------------------
  98. HRESULT COEFolderCollection::get_length(long *p)
  99. {
  100. HRESULT hr;
  101. hr = _EnsureInit();
  102. if (FAILED(hr))
  103. {
  104. TraceResult(hr);
  105. goto exit;
  106. }
  107. hr = m_pEnumChildren->Count((ULONG *)p);
  108. exit:
  109. return hr;
  110. }
  111. //+---------------------------------------------------------------
  112. //
  113. // Member: get__newEnum
  114. //
  115. // Synopsis:
  116. // Returns a folder enumerator
  117. //
  118. //---------------------------------------------------------------
  119. HRESULT COEFolderCollection::get__newEnum(IUnknown **p)
  120. {
  121. HRESULT hr;
  122. hr = _EnsureInit();
  123. if (FAILED(hr))
  124. {
  125. TraceResult(hr);
  126. goto exit;
  127. }
  128. hr = E_NOTIMPL;
  129. exit:
  130. return hr;
  131. }
  132. //+---------------------------------------------------------------
  133. //
  134. // Member: item
  135. //
  136. // Synopsis:
  137. //
  138. //
  139. //---------------------------------------------------------------
  140. HRESULT COEFolderCollection::item(VARIANT name, VARIANT index, IDispatch **ppdisp)
  141. {
  142. HRESULT hr;
  143. FOLDERID idFolder;
  144. IOEFolder *pFolder=NULL;
  145. if (!ppdisp)
  146. return E_INVALIDARG;
  147. *ppdisp = NULL;
  148. hr = _EnsureInit();
  149. if (FAILED(hr))
  150. {
  151. TraceResult(hr);
  152. goto exit;
  153. }
  154. switch(name.vt)
  155. {
  156. case VT_BSTR:
  157. hr = _FindFolder(name.bstrVal, NULL, &idFolder);
  158. break;
  159. case VT_I4:
  160. hr = _FindFolder(NULL, name.lVal, &idFolder);
  161. break;
  162. }
  163. if (FAILED(hr))
  164. {
  165. TraceResult(hr);
  166. goto exit;
  167. }
  168. hr = CreateOEFolder(idFolder, &pFolder);
  169. if (FAILED(hr))
  170. {
  171. TraceResult(hr);
  172. goto exit;
  173. }
  174. hr = pFolder->QueryInterface(IID_IDispatch, (LPVOID *)ppdisp);
  175. if (FAILED(hr))
  176. {
  177. TraceResult(hr);
  178. goto exit;
  179. }
  180. exit:
  181. ReleaseObj(pFolder);
  182. return hr;
  183. }
  184. //+---------------------------------------------------------------
  185. //
  186. // Member: add
  187. //
  188. // Synopsis:
  189. //
  190. //
  191. //---------------------------------------------------------------
  192. HRESULT COEFolderCollection::add(BSTR bstrName, IDispatch **ppDisp)
  193. {
  194. HRESULT hr;
  195. hr = _EnsureInit();
  196. if (FAILED(hr))
  197. {
  198. TraceResult(hr);
  199. goto exit;
  200. }
  201. hr = E_NOTIMPL;
  202. exit:
  203. return hr;
  204. }
  205. //+---------------------------------------------------------------
  206. //
  207. // Member: InterfaceSupportsErrorInfo
  208. //
  209. // Synopsis:
  210. // Override CBaseDisp's method to provide error
  211. // information
  212. //
  213. //---------------------------------------------------------------
  214. HRESULT COEFolderCollection::InterfaceSupportsErrorInfo(REFIID riid)
  215. {
  216. if (IsEqualIID(riid, IID_IOEFolderCollection))
  217. return S_OK;
  218. return CBaseDisp::InterfaceSupportsErrorInfo(riid);
  219. }
  220. //+---------------------------------------------------------------
  221. //
  222. // Member: _EnsureInit
  223. //
  224. // Synopsis:
  225. // Make sure the folder enumerator is up and running
  226. //
  227. //---------------------------------------------------------------
  228. HRESULT COEFolderCollection::_EnsureInit()
  229. {
  230. if (g_pStore == NULL)
  231. return E_UNEXPECTED;
  232. SafeRelease(m_pEnumChildren);
  233. return g_pStore->EnumChildren(m_idFolder, TRUE, &m_pEnumChildren);
  234. }
  235. //+---------------------------------------------------------------
  236. //
  237. // Member: _FindFolder
  238. //
  239. // Synopsis:
  240. // find a folder by name or index
  241. //
  242. //---------------------------------------------------------------
  243. HRESULT COEFolderCollection::_FindFolder(BSTR bstr, LONG lIndex, FOLDERID *pidFolder)
  244. {
  245. HRESULT hr=E_FAIL;
  246. LONG c=0;
  247. FOLDERINFO fi;
  248. LPSTR pszFolder=0;
  249. *pidFolder = NULL;
  250. if (bstr)
  251. pszFolder = PszToANSI(CP_ACP, bstr);
  252. m_pEnumChildren->Reset();
  253. hr = m_pEnumChildren->Next(1, &fi, NULL);
  254. while (hr == S_OK)
  255. {
  256. // walk immediate children
  257. if (bstr)
  258. {
  259. if (lstrcmpi(fi.pszName, pszFolder)==0)
  260. {
  261. *pidFolder = fi.idFolder;
  262. break;
  263. }
  264. }
  265. else
  266. {
  267. if (lIndex == c++)
  268. {
  269. *pidFolder = fi.idFolder;
  270. break;
  271. }
  272. }
  273. hr = m_pEnumChildren->Next(1, &fi, NULL);
  274. }
  275. SafeMemFree(pszFolder);
  276. return *pidFolder ? S_OK : E_FAIL;
  277. }
  278. //+---------------------------------------------------------------
  279. //
  280. // Member: CreateFolderCollection
  281. //
  282. // Synopsis:
  283. // helper function to create an OE Folder Collection
  284. //
  285. //---------------------------------------------------------------
  286. HRESULT CreateFolderCollection(FOLDERID idFolder, IOEFolderCollection **ppFolderCollection)
  287. {
  288. // Locals
  289. COEFolderCollection *pNew=NULL;
  290. HRESULT hr=S_OK;
  291. if (ppFolderCollection == NULL)
  292. return E_INVALIDARG;
  293. *ppFolderCollection=NULL;
  294. pNew = new COEFolderCollection();
  295. if (!pNew)
  296. return E_OUTOFMEMORY;
  297. hr = pNew->Init(idFolder);
  298. if (FAILED(hr))
  299. goto error;
  300. hr = pNew->QueryInterface(IID_IOEFolderCollection, (LPVOID *)ppFolderCollection);
  301. error:
  302. ReleaseObj(pNew);
  303. return hr;
  304. }
  305. HRESULT CreateOEFolder(FOLDERID idFolder, IOEFolder **ppFolder)
  306. {
  307. COEFolder *pNew;
  308. HRESULT hr;
  309. if (!ppFolder)
  310. return E_INVALIDARG;
  311. *ppFolder =NULL;
  312. pNew = new COEFolder();
  313. if (!pNew)
  314. return E_OUTOFMEMORY;
  315. hr = pNew->Init(idFolder);
  316. if (FAILED(hr))
  317. goto error;
  318. *ppFolder = pNew;
  319. pNew = NULL;
  320. error:
  321. ReleaseObj(pNew);
  322. return hr;
  323. }
  324. COEFolder::COEFolder() : CBaseDisp()
  325. {
  326. m_idFolder = FOLDERID_INVALID;
  327. CoIncrementInit("COEFolder::COEFolder", MSOEAPI_START_SHOWERRORS, NULL, NULL);
  328. }
  329. COEFolder::~COEFolder()
  330. {
  331. CoDecrementInit("COEFolder::COEFolder", NULL);
  332. }
  333. HRESULT COEFolder::PrivateQueryInterface(REFIID riid, LPVOID *lplpObj)
  334. {
  335. if(!lplpObj)
  336. return E_INVALIDARG;
  337. *lplpObj = NULL;
  338. if (IsEqualIID(riid, IID_IUnknown))
  339. *lplpObj = (LPVOID)(IOEFolder *)this;
  340. else if (IsEqualIID(riid, IID_IDispatch))
  341. *lplpObj = (LPVOID)(IDispatch *)(CBaseDisp *)this;
  342. else if (IsEqualIID(riid, IID_IOEFolder))
  343. *lplpObj = (LPVOID)(IOEFolder *)this;
  344. else
  345. return E_NOINTERFACE;
  346. AddRef();
  347. return NOERROR;
  348. }
  349. HRESULT COEFolder::Init(FOLDERID idFolder)
  350. {
  351. HRESULT hr;
  352. m_idFolder = idFolder;
  353. hr = _EnsureInit();
  354. if (FAILED(hr))
  355. {
  356. TraceResult(hr);
  357. goto exit;
  358. }
  359. hr = CBaseDisp::EnsureTypeLibrary((LPVOID *)(IOEFolder *)this, IID_IOEFolder);
  360. exit:
  361. return hr;
  362. }
  363. HRESULT COEFolder::get_folders(IOEFolderCollection **p)
  364. {
  365. return CreateFolderCollection(m_idFolder, p);
  366. }
  367. // *** COEFolder**
  368. HRESULT COEFolder::get_messages(IOEMessageCollection **p)
  369. {
  370. return E_NOTIMPL;
  371. }
  372. HRESULT COEFolder::get_name(BSTR *pbstr)
  373. {
  374. if (pbstr == NULL)
  375. return E_INVALIDARG;
  376. *pbstr = NULL;
  377. return HrLPSZToBSTR(m_fi.pszName, pbstr);
  378. }
  379. HRESULT COEFolder::put_name(BSTR bstr)
  380. {
  381. return E_NOTIMPL;
  382. }
  383. HRESULT COEFolder::get_size(LONG *pl)
  384. {
  385. *pl = 1000;
  386. return S_OK;
  387. }
  388. HRESULT COEFolder::get_unread(LONG *pl)
  389. {
  390. *pl = m_fi.cUnread;
  391. return S_OK;
  392. }
  393. HRESULT COEFolder::get_id(LONG *pl)
  394. {
  395. *pl = (LONG)m_fi.idFolder;
  396. return S_OK;
  397. }
  398. HRESULT COEFolder::get_count(LONG *pl)
  399. {
  400. *pl = m_fi.cMessages;
  401. return S_OK;
  402. }
  403. HRESULT COEFolder::_EnsureInit()
  404. {
  405. return g_pStore->GetFolderInfo(m_idFolder, &m_fi);
  406. }