Source code of Windows XP (NT5)
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.

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