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.

839 lines
17 KiB

  1. // FaxJob.cpp : Implementation of CFaxJobs
  2. #include "stdafx.h"
  3. #include "FaxJob.h"
  4. /////////////////////////////////////////////////////////////////////////////
  5. // CFaxJobs
  6. CFaxJobs::~CFaxJobs()
  7. {
  8. if (m_pFaxServer)
  9. {
  10. m_pFaxServer->Release();
  11. }
  12. if (m_VarVect)
  13. {
  14. delete [] m_VarVect;
  15. }
  16. }
  17. BOOL CFaxJobs::Init(CFaxServer * pFaxServer)
  18. {
  19. PFAX_JOB_ENTRYW JobEntry = NULL;
  20. DWORD PortInfoSize = 0;
  21. HRESULT hr;
  22. //
  23. // get the ports from the server
  24. //
  25. if (!pFaxServer)
  26. {
  27. return FALSE;
  28. }
  29. m_pFaxServer = pFaxServer;
  30. hr = m_pFaxServer->AddRef();
  31. if (FAILED(hr))
  32. {
  33. m_pFaxServer = NULL;
  34. return FALSE;
  35. }
  36. if (!FaxEnumJobsW( m_pFaxServer->GetFaxHandle(), &JobEntry, &m_Jobs))
  37. {
  38. m_LastFaxError = GetLastError();
  39. return FALSE;
  40. }
  41. //
  42. // enumerate the ports
  43. //
  44. m_VarVect = new CComVariant[m_Jobs];
  45. if (!m_VarVect)
  46. {
  47. FaxFreeBuffer( JobEntry );
  48. m_LastFaxError = ERROR_OUTOFMEMORY;
  49. return FALSE;
  50. }
  51. for (DWORD i=0; i<m_Jobs; i++)
  52. {
  53. //
  54. // create the object
  55. //
  56. CComObject<CFaxJob> *pFaxJob;
  57. hr = CComObject<CFaxJob>::CreateInstance( &pFaxJob );
  58. if (FAILED(hr))
  59. {
  60. delete [] m_VarVect;
  61. m_VarVect = NULL;
  62. FaxFreeBuffer( JobEntry );
  63. return FALSE;
  64. }
  65. //
  66. // set the values
  67. //
  68. if (!pFaxJob->Initialize(
  69. m_pFaxServer,
  70. JobEntry[i].JobId,
  71. JobEntry[i].UserName,
  72. JobEntry[i].JobType,
  73. JobEntry[i].QueueStatus,
  74. JobEntry[i].Status,
  75. JobEntry[i].PageCount,
  76. JobEntry[i].RecipientNumber,
  77. JobEntry[i].RecipientName,
  78. JobEntry[i].Tsid,
  79. JobEntry[i].SenderName,
  80. JobEntry[i].SenderCompany,
  81. JobEntry[i].SenderDept,
  82. JobEntry[i].BillingCode,
  83. JobEntry[i].ScheduleAction,
  84. JobEntry[i].DocumentName
  85. ))
  86. {
  87. delete [] m_VarVect;
  88. m_VarVect = NULL;
  89. FaxFreeBuffer( JobEntry );
  90. return FALSE;
  91. }
  92. //
  93. // get IDispatch pointer
  94. //
  95. LPDISPATCH lpDisp = NULL;
  96. hr = pFaxJob->QueryInterface( IID_IDispatch, (void**)&lpDisp );
  97. if (FAILED(hr))
  98. {
  99. delete [] m_VarVect;
  100. m_VarVect = NULL;
  101. FaxFreeBuffer( JobEntry );
  102. return FALSE;
  103. }
  104. //
  105. // create a variant and add it to the collection
  106. //
  107. CComVariant &var = m_VarVect[i];
  108. var.vt = VT_DISPATCH;
  109. var.pdispVal = lpDisp;
  110. }
  111. FaxFreeBuffer( JobEntry );
  112. return TRUE;
  113. }
  114. STDMETHODIMP CFaxJobs::get_Count(long * pVal)
  115. {
  116. if (!pVal)
  117. {
  118. return E_POINTER;
  119. }
  120. __try
  121. {
  122. *pVal = m_Jobs;
  123. return S_OK;
  124. }
  125. __except (EXCEPTION_EXECUTE_HANDLER)
  126. {
  127. }
  128. return E_UNEXPECTED;
  129. }
  130. STDMETHODIMP CFaxJobs::get_Item(long Index, VARIANT * pVal)
  131. {
  132. if (!pVal)
  133. {
  134. return E_POINTER;
  135. }
  136. //
  137. // use 1-based index, VB like
  138. //
  139. if ((Index < 1) || (Index > (long) m_Jobs))
  140. {
  141. return E_INVALIDARG;
  142. }
  143. __try
  144. {
  145. VariantInit( pVal );
  146. pVal->vt = VT_UNKNOWN;
  147. pVal->punkVal = NULL;
  148. return VariantCopy( pVal, &m_VarVect[Index-1] );
  149. }
  150. __except (EXCEPTION_EXECUTE_HANDLER)
  151. {
  152. }
  153. return E_UNEXPECTED;
  154. }
  155. /////////////////////////////////////////////////////////////////////////////
  156. // CFaxJob
  157. STDMETHODIMP CFaxJob::get_JobId(long * pVal)
  158. {
  159. if (!pVal)
  160. {
  161. return E_POINTER;
  162. }
  163. __try
  164. {
  165. *pVal = m_JobId;
  166. return S_OK;
  167. }
  168. __except (EXCEPTION_EXECUTE_HANDLER)
  169. {
  170. }
  171. return E_UNEXPECTED;
  172. }
  173. STDMETHODIMP CFaxJob::get_Type(long * pVal)
  174. {
  175. if (!pVal)
  176. {
  177. return E_POINTER;
  178. }
  179. __try
  180. {
  181. *pVal = m_JobType;
  182. return S_OK;
  183. }
  184. __except (EXCEPTION_EXECUTE_HANDLER)
  185. {
  186. }
  187. return E_UNEXPECTED;
  188. }
  189. STDMETHODIMP CFaxJob::get_UserName(BSTR * pVal)
  190. {
  191. if (!pVal)
  192. {
  193. return E_POINTER;
  194. }
  195. BSTR Copy = SysAllocString(m_UserName);
  196. if (!Copy && m_UserName)
  197. {
  198. return E_OUTOFMEMORY;
  199. }
  200. __try
  201. {
  202. *pVal = Copy;
  203. return S_OK;
  204. }
  205. __except (EXCEPTION_EXECUTE_HANDLER)
  206. {
  207. SysFreeString(Copy);
  208. }
  209. return E_UNEXPECTED;
  210. }
  211. STDMETHODIMP CFaxJob::get_QueueStatus(BSTR * pVal)
  212. {
  213. if (!pVal)
  214. {
  215. return E_POINTER;
  216. }
  217. BSTR Copy = SysAllocString(m_szQueueStatus);
  218. if (!Copy && m_szQueueStatus)
  219. {
  220. return E_OUTOFMEMORY;
  221. }
  222. __try
  223. {
  224. *pVal = Copy;
  225. return S_OK;
  226. }
  227. __except (EXCEPTION_EXECUTE_HANDLER)
  228. {
  229. SysFreeString(Copy);
  230. }
  231. return E_UNEXPECTED;
  232. }
  233. STDMETHODIMP CFaxJob::get_DeviceStatus(BSTR * pVal)
  234. {
  235. if (!pVal)
  236. {
  237. return E_POINTER;
  238. }
  239. BSTR Copy = SysAllocString(m_szDeviceStatus);
  240. if (!Copy && m_szDeviceStatus)
  241. {
  242. return E_OUTOFMEMORY;
  243. }
  244. __try
  245. {
  246. *pVal = Copy;
  247. return S_OK;
  248. }
  249. __except (EXCEPTION_EXECUTE_HANDLER)
  250. {
  251. SysFreeString(Copy);
  252. }
  253. return E_UNEXPECTED;
  254. }
  255. STDMETHODIMP CFaxJob::get_PageCount(long * pVal)
  256. {
  257. if (!pVal)
  258. {
  259. return E_POINTER;
  260. }
  261. __try
  262. {
  263. *pVal = m_PageCount;
  264. return S_OK;
  265. }
  266. __except (EXCEPTION_EXECUTE_HANDLER)
  267. {
  268. }
  269. return E_UNEXPECTED;
  270. }
  271. STDMETHODIMP CFaxJob::get_FaxNumber(BSTR * pVal)
  272. {
  273. if (!pVal)
  274. {
  275. return E_POINTER;
  276. }
  277. BSTR Copy = SysAllocString(m_RecipientNumber);
  278. if (!Copy && m_RecipientNumber)
  279. {
  280. return E_OUTOFMEMORY;
  281. }
  282. __try
  283. {
  284. *pVal = Copy;
  285. return S_OK;
  286. }
  287. __except (EXCEPTION_EXECUTE_HANDLER)
  288. {
  289. SysFreeString(Copy);
  290. }
  291. return E_UNEXPECTED;
  292. }
  293. STDMETHODIMP CFaxJob::get_RecipientName(BSTR * pVal)
  294. {
  295. if (!pVal)
  296. {
  297. return E_POINTER;
  298. }
  299. BSTR Copy = SysAllocString(m_RecipientName);
  300. if (!Copy && m_RecipientName)
  301. {
  302. return E_OUTOFMEMORY;
  303. }
  304. __try
  305. {
  306. *pVal = Copy;
  307. return S_OK;
  308. }
  309. __except (EXCEPTION_EXECUTE_HANDLER)
  310. {
  311. SysFreeString(Copy);
  312. }
  313. return E_UNEXPECTED;
  314. }
  315. STDMETHODIMP CFaxJob::get_Tsid(BSTR * pVal)
  316. {
  317. if (!pVal)
  318. {
  319. return E_POINTER;
  320. }
  321. BSTR Copy = SysAllocString(m_Tsid);
  322. if (!Copy && m_Tsid)
  323. {
  324. return E_OUTOFMEMORY;
  325. }
  326. __try
  327. {
  328. *pVal = Copy;
  329. return S_OK;
  330. }
  331. __except (EXCEPTION_EXECUTE_HANDLER)
  332. {
  333. SysFreeString(Copy);
  334. }
  335. return E_UNEXPECTED;
  336. }
  337. STDMETHODIMP CFaxJob::get_SenderName(BSTR * pVal)
  338. {
  339. if (!pVal)
  340. {
  341. return E_POINTER;
  342. }
  343. BSTR Copy = SysAllocString(m_SenderName);
  344. if (!Copy && m_SenderName)
  345. {
  346. return E_OUTOFMEMORY;
  347. }
  348. __try
  349. {
  350. *pVal = Copy;
  351. return S_OK;
  352. }
  353. __except (EXCEPTION_EXECUTE_HANDLER)
  354. {
  355. SysFreeString(Copy);
  356. }
  357. return E_UNEXPECTED;
  358. }
  359. STDMETHODIMP CFaxJob::get_SenderCompany(BSTR * pVal)
  360. {
  361. if (!pVal)
  362. {
  363. return E_POINTER;
  364. }
  365. BSTR Copy = SysAllocString(m_SenderCompany);
  366. if (!Copy && m_SenderCompany)
  367. {
  368. return E_OUTOFMEMORY;
  369. }
  370. __try
  371. {
  372. *pVal = Copy;
  373. return S_OK;
  374. }
  375. __except (EXCEPTION_EXECUTE_HANDLER)
  376. {
  377. SysFreeString(Copy);
  378. }
  379. return E_UNEXPECTED;
  380. }
  381. STDMETHODIMP CFaxJob::get_SenderDept(BSTR * pVal)
  382. {
  383. if (!pVal)
  384. {
  385. return E_POINTER;
  386. }
  387. BSTR Copy = SysAllocString(m_SenderDept);
  388. if (!Copy && m_SenderDept)
  389. {
  390. return E_OUTOFMEMORY;
  391. }
  392. __try
  393. {
  394. *pVal = Copy;
  395. return S_OK;
  396. }
  397. __except (EXCEPTION_EXECUTE_HANDLER)
  398. {
  399. SysFreeString(Copy);
  400. }
  401. return E_UNEXPECTED;
  402. }
  403. STDMETHODIMP CFaxJob::get_BillingCode(BSTR * pVal)
  404. {
  405. if (!pVal)
  406. {
  407. return E_POINTER;
  408. }
  409. BSTR Copy = SysAllocString(m_BillingCode);
  410. if (!Copy && m_BillingCode)
  411. {
  412. return E_OUTOFMEMORY;
  413. }
  414. __try
  415. {
  416. *pVal = Copy;
  417. return S_OK;
  418. }
  419. __except (EXCEPTION_EXECUTE_HANDLER)
  420. {
  421. SysFreeString(Copy);
  422. }
  423. return E_UNEXPECTED;
  424. }
  425. STDMETHODIMP CFaxJob::get_DisplayName(BSTR * pVal)
  426. {
  427. if (!pVal)
  428. {
  429. return E_POINTER;
  430. }
  431. BSTR Copy = SysAllocString(m_DisplayName);
  432. if (!Copy && m_DisplayName)
  433. {
  434. return E_OUTOFMEMORY;
  435. }
  436. __try
  437. {
  438. *pVal = Copy;
  439. return S_OK;
  440. }
  441. __except (EXCEPTION_EXECUTE_HANDLER)
  442. {
  443. SysFreeString(Copy);
  444. }
  445. return E_UNEXPECTED;
  446. }
  447. STDMETHODIMP CFaxJob::get_DiscountSend(BOOL * pVal)
  448. {
  449. if (!pVal)
  450. {
  451. return E_POINTER;
  452. }
  453. __try
  454. {
  455. *pVal = m_DiscountTime;
  456. return S_OK;
  457. }
  458. __except (EXCEPTION_EXECUTE_HANDLER)
  459. {
  460. }
  461. return E_UNEXPECTED;
  462. }
  463. CFaxJob::~CFaxJob()
  464. {
  465. SysFreeString(m_UserName);
  466. SysFreeString(m_szQueueStatus);
  467. SysFreeString(m_szDeviceStatus);
  468. SysFreeString(m_RecipientNumber);
  469. SysFreeString(m_RecipientName);
  470. SysFreeString(m_Tsid);
  471. SysFreeString(m_SenderName);
  472. SysFreeString(m_SenderCompany);
  473. SysFreeString(m_SenderDept);
  474. SysFreeString(m_BillingCode);
  475. SysFreeString(m_DisplayName);
  476. if (m_pFaxServer)
  477. {
  478. m_pFaxServer->Release();
  479. }
  480. }
  481. BOOL CFaxJob::Initialize(
  482. CFaxServer *pFaxServer,
  483. DWORD JobId,
  484. LPCWSTR UserName,
  485. DWORD JobType,
  486. DWORD QueueStatus,
  487. DWORD DeviceStatus,
  488. DWORD PageCount,
  489. LPCWSTR RecipientNumber,
  490. LPCWSTR RecipientName,
  491. LPCWSTR Tsid,
  492. LPCWSTR SenderName,
  493. LPCWSTR SenderCompany,
  494. LPCWSTR SenderDept,
  495. LPCWSTR BillingCode,
  496. DWORD ScheduleAction,
  497. LPCWSTR DisplayName)
  498. {
  499. if (!pFaxServer)
  500. {
  501. return FALSE;
  502. }
  503. m_pFaxServer = pFaxServer;
  504. if ( FAILED(m_pFaxServer->AddRef()) )
  505. {
  506. m_pFaxServer = NULL;
  507. return FALSE;
  508. }
  509. m_UserName = SysAllocString(UserName);
  510. m_szQueueStatus = GetQueueStatus(m_QueueStatus);
  511. m_szDeviceStatus = GetDeviceStatus(m_DeviceStatus);
  512. m_RecipientNumber = SysAllocString(RecipientNumber);
  513. m_RecipientName = SysAllocString(RecipientName);
  514. m_Tsid = SysAllocString(Tsid);
  515. m_SenderName = SysAllocString(SenderName);
  516. m_SenderCompany = SysAllocString(SenderCompany);
  517. m_SenderDept = SysAllocString(SenderDept);
  518. m_BillingCode = SysAllocString(BillingCode);
  519. m_DisplayName = SysAllocString(DisplayName);
  520. if ( (!m_UserName && UserName) ||
  521. (!m_szQueueStatus) ||
  522. (!m_szDeviceStatus) ||
  523. (!m_RecipientNumber && RecipientNumber) ||
  524. (!m_RecipientName && RecipientName) ||
  525. (!m_Tsid && Tsid) ||
  526. (!m_SenderName && SenderName) ||
  527. (!m_SenderCompany && SenderCompany) ||
  528. (!m_SenderDept && SenderDept) ||
  529. (!m_BillingCode && BillingCode) ||
  530. (!m_DisplayName && DisplayName) )
  531. {
  532. goto error;
  533. }
  534. m_JobId = JobId;
  535. m_JobType = JobType;
  536. m_QueueStatus = QueueStatus;
  537. m_DeviceStatus = DeviceStatus;
  538. m_PageCount = PageCount;
  539. m_DiscountTime = (ScheduleAction == JSA_DISCOUNT_PERIOD) ? TRUE : FALSE;
  540. return TRUE;
  541. error:
  542. SysFreeString(m_DisplayName);
  543. SysFreeString(m_BillingCode);
  544. SysFreeString(m_SenderDept);
  545. SysFreeString(m_SenderCompany);
  546. SysFreeString(m_SenderName);
  547. SysFreeString(m_Tsid);
  548. SysFreeString(m_RecipientName);
  549. SysFreeString(m_RecipientNumber);
  550. SysFreeString(m_szDeviceStatus);
  551. SysFreeString(m_szQueueStatus);
  552. SysFreeString(m_UserName);
  553. m_pFaxServer->Release();
  554. m_pFaxServer = NULL;
  555. return FALSE;
  556. }
  557. BOOL CFaxJob::SetJob()
  558. {
  559. FAX_JOB_ENTRYW FaxJobEntry;
  560. ZeroMemory(&FaxJobEntry,sizeof(FAX_JOB_ENTRYW) );
  561. FaxJobEntry.SizeOfStruct = sizeof(FAX_JOB_ENTRYW);
  562. FaxJobEntry.JobId = m_JobId;
  563. FaxJobEntry.UserName = m_UserName;
  564. FaxJobEntry.JobType = m_JobType;
  565. FaxJobEntry.QueueStatus = m_QueueStatus;
  566. FaxJobEntry.Status = m_DeviceStatus;
  567. FaxJobEntry.PageCount = m_PageCount;
  568. FaxJobEntry.RecipientNumber = m_RecipientNumber;
  569. FaxJobEntry.RecipientName = m_RecipientName;
  570. FaxJobEntry.Tsid = m_Tsid;
  571. FaxJobEntry.SenderName = m_SenderName;
  572. FaxJobEntry.SenderCompany = m_SenderCompany;
  573. FaxJobEntry.SenderDept = m_SenderDept;
  574. FaxJobEntry.BillingCode = m_BillingCode;
  575. FaxJobEntry.ScheduleAction = m_DiscountTime ? JSA_DISCOUNT_PERIOD : JSA_NOW;
  576. FaxJobEntry.DocumentName = m_DisplayName;
  577. if (!FaxSetJobW(m_pFaxServer->GetFaxHandle(),m_JobId,m_Command,&FaxJobEntry) )
  578. {
  579. return FALSE;
  580. }
  581. return TRUE;
  582. }
  583. STDMETHODIMP CFaxJob::SetStatus(long Command)
  584. {
  585. m_Command = Command;
  586. if (!SetJob())
  587. {
  588. return Fax_HRESULT_FROM_WIN32(GetLastError());
  589. }
  590. return S_OK;
  591. }
  592. STDMETHODIMP CFaxJob::Refresh()
  593. {
  594. PFAX_JOB_ENTRYW FaxJobEntry;
  595. HRESULT hr = S_OK;
  596. if (!FaxGetJobW(m_pFaxServer->GetFaxHandle(),m_JobId,&FaxJobEntry) )
  597. {
  598. return Fax_HRESULT_FROM_WIN32(GetLastError());
  599. }
  600. if (m_UserName)
  601. {
  602. SysFreeString(m_UserName);
  603. }
  604. m_UserName = SysAllocString(FaxJobEntry->UserName);
  605. m_JobType = FaxJobEntry->JobType;
  606. m_QueueStatus = FaxJobEntry->QueueStatus;
  607. m_DeviceStatus = FaxJobEntry->Status;
  608. if (m_szQueueStatus)
  609. {
  610. SysFreeString(m_szQueueStatus);
  611. }
  612. m_szQueueStatus = GetQueueStatus(m_QueueStatus);
  613. if (m_szDeviceStatus)
  614. {
  615. SysFreeString(m_szDeviceStatus);
  616. }
  617. m_szDeviceStatus = GetDeviceStatus(m_DeviceStatus);
  618. m_PageCount = FaxJobEntry->PageCount;
  619. if (m_RecipientNumber )
  620. {
  621. SysFreeString(m_RecipientNumber);
  622. }
  623. m_RecipientNumber = SysAllocString(FaxJobEntry->RecipientNumber);
  624. if (m_RecipientName )
  625. {
  626. SysFreeString(m_RecipientName);
  627. }
  628. m_RecipientName = SysAllocString(FaxJobEntry->RecipientName);
  629. if (m_Tsid )
  630. {
  631. SysFreeString(m_Tsid);
  632. }
  633. m_Tsid = SysAllocString(FaxJobEntry->Tsid);
  634. if (m_SenderName )
  635. {
  636. SysFreeString(m_SenderName);
  637. }
  638. m_SenderName = SysAllocString(FaxJobEntry->SenderName);
  639. if (m_SenderCompany)
  640. {
  641. SysFreeString(m_SenderCompany);
  642. }
  643. m_SenderCompany = SysAllocString(FaxJobEntry->SenderCompany);
  644. if (m_SenderDept)
  645. {
  646. SysFreeString(m_SenderDept);
  647. }
  648. m_SenderDept = SysAllocString(FaxJobEntry->SenderDept);
  649. if (m_BillingCode )
  650. {
  651. SysFreeString(m_BillingCode);
  652. }
  653. m_BillingCode = SysAllocString(FaxJobEntry->BillingCode);
  654. if (m_DisplayName)
  655. {
  656. SysFreeString(m_DisplayName);
  657. }
  658. m_DisplayName = SysAllocString(FaxJobEntry->DocumentName);
  659. m_DiscountTime = (FaxJobEntry->ScheduleAction == JSA_DISCOUNT_PERIOD) ? TRUE : FALSE;
  660. if (!m_szQueueStatus || !m_szDeviceStatus ||
  661. (!m_UserName && FaxJobEntry->UserName) ||
  662. (!m_RecipientNumber && FaxJobEntry->RecipientNumber) ||
  663. (!m_RecipientName && FaxJobEntry->RecipientName) ||
  664. (!m_Tsid && FaxJobEntry->Tsid) ||
  665. (!m_SenderName && FaxJobEntry->SenderName) ||
  666. (!m_SenderCompany && FaxJobEntry->SenderCompany) ||
  667. (!m_SenderDept && FaxJobEntry->SenderDept) ||
  668. (!m_BillingCode && FaxJobEntry->BillingCode) ||
  669. (!m_DisplayName && FaxJobEntry->DocumentName) )
  670. {
  671. hr = E_OUTOFMEMORY;
  672. SysFreeString(m_DisplayName);
  673. SysFreeString(m_BillingCode);
  674. SysFreeString(m_SenderDept);
  675. SysFreeString(m_SenderCompany);
  676. SysFreeString(m_SenderName);
  677. SysFreeString(m_Tsid);
  678. SysFreeString(m_RecipientName);
  679. SysFreeString(m_RecipientNumber);
  680. SysFreeString(m_szDeviceStatus);
  681. SysFreeString(m_szQueueStatus);
  682. SysFreeString(m_UserName);
  683. }
  684. FaxFreeBuffer(FaxJobEntry);
  685. return hr;
  686. }