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.

528 lines
12 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. FaxDeviceIds.cpp
  5. Abstract:
  6. Implementation of CFaxDeviceIds class.
  7. Author:
  8. Iv Garber (IvG) Jun, 2000
  9. Revision History:
  10. --*/
  11. #include "stdafx.h"
  12. #include "FaxComEx.h"
  13. #include "FaxDeviceIds.h"
  14. #include "faxutil.h"
  15. //
  16. //======================= UPDATE GROUP ======================================
  17. //
  18. STDMETHODIMP
  19. CFaxDeviceIds::UpdateGroup()
  20. /*++
  21. Routine name : CFaxDeviceIds::UpdateGroup
  22. Routine description:
  23. Update Group Info at Server.
  24. Author:
  25. Iv Garber (IvG), Jun, 2000
  26. Arguments:
  27. Return Value:
  28. Standard HRESULT code
  29. --*/
  30. {
  31. HRESULT hr = S_OK;
  32. DBG_ENTER(_T("CFaxDeviceIds::UpdateGroup"));
  33. //
  34. // Get Fax Handle
  35. //
  36. HANDLE faxHandle;
  37. hr = m_pIFaxServerInner->GetHandle(&faxHandle);
  38. ATLASSERT(SUCCEEDED(hr));
  39. if (faxHandle == NULL)
  40. {
  41. //
  42. // Fax Server is not connected
  43. //
  44. hr = Fax_HRESULT_FROM_WIN32(ERROR_NOT_CONNECTED);
  45. CALL_FAIL(GENERAL_ERR, _T("faxHandle == NULL"), hr);
  46. AtlReportError(CLSID_FaxDeviceIds, GetErrorMsgId(hr), IID_IFaxDeviceIds, hr);
  47. return hr;
  48. }
  49. //
  50. // Create Group Structure
  51. //
  52. FAX_OUTBOUND_ROUTING_GROUP groupData;
  53. groupData.dwNumDevices = m_coll.size();
  54. groupData.dwSizeOfStruct = sizeof(FAX_OUTBOUND_ROUTING_GROUP);
  55. groupData.lpctstrGroupName = m_bstrGroupName;
  56. groupData.Status = FAX_GROUP_STATUS_ALL_DEV_VALID;
  57. groupData.lpdwDevices = (DWORD *)MemAlloc(sizeof(DWORD) * groupData.dwNumDevices);
  58. if (!groupData.lpdwDevices)
  59. {
  60. hr = E_OUTOFMEMORY;
  61. CALL_FAIL(MEM_ERR, _T("MemAlloc(sizeof(DWORD) * groupData.dwNumDevices)"), hr);
  62. AtlReportError(CLSID_FaxDeviceIds, IDS_ERROR_OUTOFMEMORY, IID_IFaxDeviceIds, hr);
  63. return hr;
  64. }
  65. ContainerType::iterator DeviceIdIterator = m_coll.begin();
  66. DWORD i = 0;
  67. while ( DeviceIdIterator != m_coll.end())
  68. {
  69. groupData.lpdwDevices[i] = *DeviceIdIterator;
  70. DeviceIdIterator++;
  71. i++;
  72. }
  73. //
  74. // Call Server to Update Group's Info
  75. //
  76. if (!FaxSetOutboundGroup(faxHandle, &groupData))
  77. {
  78. hr = Fax_HRESULT_FROM_WIN32(GetLastError());
  79. CALL_FAIL(GENERAL_ERR, _T("FaxSetOutboundGroup(faxHandle, &groupData)"), hr);
  80. AtlReportError(CLSID_FaxDeviceIds, GetErrorMsgId(hr), IID_IFaxDeviceIds, hr);
  81. return hr;
  82. }
  83. return hr;
  84. }
  85. //
  86. //==================================== INIT ======================================
  87. //
  88. STDMETHODIMP
  89. CFaxDeviceIds::Init(
  90. /*[in]*/ DWORD *pDeviceIds,
  91. /*[in]*/ DWORD dwNum,
  92. /*[in]*/ BSTR bstrGroupName,
  93. /*[in]*/ IFaxServerInner *pServer
  94. )
  95. /*++
  96. Routine name : CFaxDeviceIds::Init
  97. Routine description:
  98. Initialize the DeviceIds Collection.
  99. Author:
  100. Iv Garber (IvG), Jun, 2000
  101. Arguments:
  102. pDeviceIds [in] - Ptr to the DeviceIds
  103. dwNum [in] - Count of the Device Ids
  104. bstrGroupName [in] - Name of the owner Group
  105. pServer [in] - Ptr to the Server Object
  106. Return Value:
  107. Standard HRESULT code
  108. --*/
  109. {
  110. HRESULT hr = S_OK;
  111. DBG_ENTER(_T("CFaxDeviceIds::Init"), hr, _T("NumDevices=%d GroupName=%s"), dwNum, bstrGroupName);
  112. m_bstrGroupName = bstrGroupName;
  113. if (bstrGroupName && !m_bstrGroupName)
  114. {
  115. hr = E_OUTOFMEMORY;
  116. CALL_FAIL(MEM_ERR, _T("CComBSTR::operator=()"), hr);
  117. AtlReportError(CLSID_FaxDeviceIds, IDS_ERROR_OUTOFMEMORY, IID_IFaxDeviceIds, hr);
  118. return hr;
  119. }
  120. //
  121. // Fill the Collection with Device Ids
  122. //
  123. for ( DWORD i=0 ; i<dwNum ; i++ )
  124. {
  125. try
  126. {
  127. m_coll.push_back(pDeviceIds[i]);
  128. }
  129. catch (exception &)
  130. {
  131. hr = E_OUTOFMEMORY;
  132. AtlReportError(CLSID_FaxDeviceIds, IDS_ERROR_OUTOFMEMORY, IID_IFaxDeviceIds, hr);
  133. CALL_FAIL(MEM_ERR, _T("m_coll.push_back(pDeviceIds[i])"), hr);
  134. return hr;
  135. }
  136. }
  137. //
  138. // Store and AddRef the Ptr to the Fax Server Object
  139. //
  140. hr = CFaxInitInnerAddRef::Init(pServer);
  141. return hr;
  142. }
  143. //
  144. //==================================== ADD ======================================
  145. //
  146. STDMETHODIMP
  147. CFaxDeviceIds::Add(
  148. /*[in]*/ long lDeviceId
  149. )
  150. /*++
  151. Routine name : CFaxDeviceIds::Add
  152. Routine description:
  153. Add new Device ID to the Collection
  154. Author:
  155. Iv Garber (IvG), Jun, 2000
  156. Arguments:
  157. lDeviceId [in] - the Device Id to add
  158. Return Value:
  159. Standard HRESULT code
  160. --*/
  161. {
  162. HRESULT hr = S_OK;
  163. DBG_ENTER(_T("CFaxDeviceIds::Add"), hr, _T("DeviceId=%ld"), lDeviceId);
  164. //
  165. // Check that we can Add Device Id
  166. //
  167. if (_tcsicmp(m_bstrGroupName, ROUTING_GROUP_ALL_DEVICES) == 0)
  168. {
  169. //
  170. // This is the "All Devices" Group
  171. //
  172. hr = E_INVALIDARG;
  173. CALL_FAIL(GENERAL_ERR, _T("All Devices Group"), hr);
  174. AtlReportError(CLSID_FaxDeviceIds, IDS_ERROR_ALLDEVICESGROUP, IID_IFaxDeviceIds, hr);
  175. return hr;
  176. }
  177. //
  178. // Put the Device Id in the collection
  179. //
  180. try
  181. {
  182. m_coll.push_back(lDeviceId);
  183. }
  184. catch (exception &)
  185. {
  186. hr = E_OUTOFMEMORY;
  187. AtlReportError(CLSID_FaxDeviceIds, IDS_ERROR_OUTOFMEMORY, IID_IFaxDeviceIds, hr);
  188. CALL_FAIL(MEM_ERR, _T("m_coll.push_back(lDeviceId)"), hr);
  189. return hr;
  190. }
  191. //
  192. // Update Group's Info at Server
  193. //
  194. hr = UpdateGroup();
  195. if (FAILED(hr))
  196. {
  197. //
  198. // Failed to Add the Device Id --> remove it from the Collection as well
  199. //
  200. try
  201. {
  202. m_coll.pop_back();
  203. }
  204. catch (exception &)
  205. {
  206. //
  207. // Only write to Debug
  208. //
  209. CALL_FAIL(MEM_ERR, _T("m_coll.push_back(lDeviceId)"), E_OUTOFMEMORY);
  210. }
  211. return hr;
  212. }
  213. return hr;
  214. }
  215. //
  216. //======================== REMOVE =================================================
  217. //
  218. STDMETHODIMP
  219. CFaxDeviceIds::Remove(
  220. /*[in]*/ long lIndex
  221. )
  222. /*++
  223. Routine name : CFaxDeviceIds::Remove
  224. Routine description:
  225. Remove the given Item from the Collection
  226. Author:
  227. Iv Garber (IvG), Jun, 2000
  228. Arguments:
  229. lIndex [in] - Index of the Item to remove
  230. Return Value:
  231. Standard HRESULT code
  232. --*/
  233. {
  234. HRESULT hr = S_OK;
  235. DBG_ENTER(_T("CFaxDeviceIds::Remove"), hr, _T("Index=%ld"), lIndex);
  236. //
  237. // Check that we can Remove the Device from the Collection
  238. //
  239. if (_tcsicmp(m_bstrGroupName, ROUTING_GROUP_ALL_DEVICES) == 0)
  240. {
  241. //
  242. // This is the "All Devices" Group
  243. //
  244. hr = E_INVALIDARG;
  245. CALL_FAIL(GENERAL_ERR, _T("All Devices Group"), hr);
  246. AtlReportError(CLSID_FaxDeviceIds, IDS_ERROR_ALLDEVICESGROUP, IID_IFaxDeviceIds, hr);
  247. return hr;
  248. }
  249. //
  250. // Check that Index is Valid
  251. //
  252. if ((lIndex > m_coll.size()) || (lIndex < 1))
  253. {
  254. hr = E_INVALIDARG;
  255. CALL_FAIL(GENERAL_ERR, _T("(lIndex > m_coll.size() or lIndex < 1)"), hr);
  256. AtlReportError(CLSID_FaxDeviceIds, IDS_ERROR_OUTOFRANGE, IID_IFaxDeviceIds, hr);
  257. return hr;
  258. }
  259. //
  260. // Remove the Item from the Collection
  261. //
  262. long lDeviceId;
  263. try
  264. {
  265. ContainerType::iterator it;
  266. it = m_coll.begin() + lIndex - 1;
  267. lDeviceId = *it;
  268. m_coll.erase(it);
  269. }
  270. catch (exception &)
  271. {
  272. hr = E_OUTOFMEMORY;
  273. AtlReportError(CLSID_FaxDeviceIds, IDS_ERROR_OUTOFMEMORY, IID_IFaxDeviceIds, hr);
  274. CALL_FAIL(MEM_ERR, _T("m_coll.erase(it)"), hr);
  275. return hr;
  276. }
  277. //
  278. // Update Group's Info at Server
  279. //
  280. hr = UpdateGroup();
  281. if (FAILED(hr))
  282. {
  283. //
  284. // Failed to Remove the Device --> Add it back into the Collection
  285. //
  286. try
  287. {
  288. ContainerType::iterator it;
  289. it = m_coll.begin() + lIndex - 1;
  290. m_coll.insert(it, lDeviceId);
  291. }
  292. catch (exception &)
  293. {
  294. //
  295. // Only Debug
  296. //
  297. CALL_FAIL(MEM_ERR, _T("m_coll.insert(it, lDeviceId)"), E_OUTOFMEMORY);
  298. }
  299. return hr;
  300. }
  301. return hr;
  302. }
  303. //
  304. //======================== SET ORDER =================================================
  305. //
  306. STDMETHODIMP
  307. CFaxDeviceIds::SetOrder(
  308. /*[in]*/ long lDeviceId,
  309. /*[in]*/ long lNewOrder
  310. )
  311. /*++
  312. Routine name : CFaxDeviceIds::SetOrder
  313. Routine description:
  314. Update Order for the Device Id
  315. Author:
  316. Iv Garber (IvG), Jun, 2000
  317. Arguments:
  318. lDeviceId [in] - the Device Id
  319. lNewOrder [in] - the new Order of the Device Id
  320. Return Value:
  321. Standard HRESULT code
  322. --*/
  323. {
  324. HRESULT hr = S_OK;
  325. DBG_ENTER(_T("CFaxDeviceIds::SetOrder"), hr, _T("Id=%ld Order=%ld"), lDeviceId, lNewOrder);
  326. //
  327. // Before setting the Device's Order at Server, check that the Device is present in the Collection
  328. //
  329. ContainerType::iterator it;
  330. it = m_coll.begin();
  331. while (it != m_coll.end())
  332. {
  333. if ((*it) == lDeviceId)
  334. {
  335. break;
  336. }
  337. it++;
  338. }
  339. if (it == m_coll.end())
  340. {
  341. //
  342. // Our Collection does not contain such Device Id
  343. //
  344. hr = E_INVALIDARG;
  345. CALL_FAIL(GENERAL_ERR, _T("(The Device Id does not found in the Collection !!)"), hr);
  346. AtlReportError(CLSID_FaxDeviceIds, GetErrorMsgId(hr), IID_IFaxDeviceIds, hr);
  347. return hr;
  348. }
  349. //
  350. // Get Fax Handle
  351. //
  352. HANDLE faxHandle;
  353. hr = m_pIFaxServerInner->GetHandle(&faxHandle);
  354. ATLASSERT(SUCCEEDED(hr));
  355. if (faxHandle == NULL)
  356. {
  357. //
  358. // Fax Server is not connected
  359. //
  360. hr = Fax_HRESULT_FROM_WIN32(ERROR_NOT_CONNECTED);
  361. CALL_FAIL(GENERAL_ERR, _T("faxHandle == NULL"), hr);
  362. AtlReportError(CLSID_FaxDeviceIds, GetErrorMsgId(hr), IID_IFaxDeviceIds, hr);
  363. return hr;
  364. }
  365. // Call Server to Update the Device's Order
  366. //
  367. if (!FaxSetDeviceOrderInGroup(faxHandle, m_bstrGroupName, lDeviceId, lNewOrder))
  368. {
  369. hr = Fax_HRESULT_FROM_WIN32(ERROR_NOT_CONNECTED);
  370. CALL_FAIL(GENERAL_ERR, _T("FaxSetDeviceOrderInGroup(faxHandle, m_bstrGroupName, lDeviceId, lNewOrder)"), hr);
  371. AtlReportError(CLSID_FaxDeviceIds, GetErrorMsgId(hr), IID_IFaxDeviceIds, hr);
  372. return hr;
  373. }
  374. //
  375. // Remove the Device Id from its Place in the Collection and Put it in the Desired Place
  376. //
  377. try
  378. {
  379. m_coll.erase(it);
  380. it = m_coll.begin() + lNewOrder - 1;
  381. m_coll.insert(it, lDeviceId);
  382. }
  383. catch (exception &)
  384. {
  385. hr = E_OUTOFMEMORY;
  386. AtlReportError(CLSID_FaxDeviceIds, IDS_ERROR_OUTOFMEMORY, IID_IFaxDeviceIds, hr);
  387. CALL_FAIL(MEM_ERR, _T("m_coll.erase(it)/insert(it, lDeviceId)"), hr);
  388. return hr;
  389. }
  390. return hr;
  391. }
  392. //
  393. //===================== SUPPORT ERROR INFO ======================================
  394. //
  395. STDMETHODIMP
  396. CFaxDeviceIds::InterfaceSupportsErrorInfo(
  397. REFIID riid
  398. )
  399. /*++
  400. Routine name : CFaxDeviceIds::InterfaceSupportsErrorInfo
  401. Routine description:
  402. ATL's implementation of the ISupportErrorInfo Interface.
  403. Author:
  404. Iv Garber (IvG), Jun, 2000
  405. Arguments:
  406. riid [in] - Reference to the Interface
  407. Return Value:
  408. Standard HRESULT code
  409. --*/
  410. {
  411. static const IID* arr[] =
  412. {
  413. &IID_IFaxDeviceIds
  414. };
  415. for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
  416. {
  417. if (InlineIsEqualGUID(*arr[i],riid))
  418. return S_OK;
  419. }
  420. return S_FALSE;
  421. }