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.

787 lines
15 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. faxsvr.cpp
  5. Abstract:
  6. This file implements the CFaxServer interface.
  7. Author:
  8. Wesley Witt (wesw) 1-June-1997
  9. Environment:
  10. User Mode
  11. --*/
  12. #include "stdafx.h"
  13. #include "faxsvr.h"
  14. #include "faxport.h"
  15. #include "FaxDoc.h"
  16. #include "FaxJob.h"
  17. CFaxServer::CFaxServer()
  18. {
  19. m_LastFaxError = NO_ERROR;
  20. m_FaxHandle = 0;
  21. m_Retries = 0;
  22. m_RetryDelay = 0;
  23. m_DirtyDays = 0;
  24. m_UseDeviceTsid = FALSE;
  25. m_ServerCp = FALSE;
  26. m_PauseServerQueue = FALSE;;
  27. m_StartCheapTime.Hour = 0;
  28. m_StartCheapTime.Minute = 0;
  29. m_StopCheapTime.Hour = 0;
  30. m_StartCheapTime.Minute = 0;
  31. m_ArchiveOutgoingFaxes = FALSE;;
  32. m_ArchiveDirectory = NULL;
  33. m_InboundProfile = NULL;
  34. }
  35. CFaxServer::~CFaxServer()
  36. {
  37. if (m_FaxHandle) {
  38. FaxClose( m_FaxHandle );
  39. }
  40. if (m_ArchiveDirectory)
  41. SysFreeString(m_ArchiveDirectory);
  42. if (m_InboundProfile)
  43. SysFreeString(m_InboundProfile);
  44. }
  45. STDMETHODIMP CFaxServer::Connect(BSTR ServerName)
  46. {
  47. if (!FaxConnectFaxServer( ServerName, &m_FaxHandle )) {
  48. m_LastFaxError = GetLastError();
  49. m_FaxHandle = NULL;
  50. return E_FAIL;
  51. }
  52. DWORD FaxServerVersion = 0;
  53. if (!FaxGetVersion( m_FaxHandle, &FaxServerVersion )) {
  54. m_LastFaxError = GetLastError();
  55. FaxClose( m_FaxHandle );
  56. m_FaxHandle = NULL;
  57. return E_FAIL;
  58. }
  59. if (!RetrieveConfiguration()) {
  60. return HRESULT_FROM_WIN32(m_LastFaxError);
  61. }
  62. return S_OK;
  63. }
  64. STDMETHODIMP CFaxServer::Disconnect()
  65. {
  66. if (m_FaxHandle == NULL) {
  67. return E_FAIL;
  68. }
  69. if (!FaxClose( m_FaxHandle )) {
  70. return E_FAIL;
  71. }
  72. m_FaxHandle = NULL;
  73. m_Retries = 0;
  74. m_RetryDelay = 0;
  75. m_DirtyDays = 0 ;
  76. m_Branding = FALSE;
  77. m_UseDeviceTsid = FALSE;
  78. m_ServerCp = FALSE;
  79. m_PauseServerQueue = FALSE;
  80. m_StartCheapTime.Hour = 0;
  81. m_StartCheapTime.Minute = 0;
  82. m_StopCheapTime.Hour = 0;
  83. m_StopCheapTime.Minute = 0;
  84. m_StartCheapTime.Hour = 0;
  85. m_ArchiveOutgoingFaxes = FALSE;
  86. if (m_ArchiveDirectory)
  87. SysFreeString(m_ArchiveDirectory);
  88. m_ArchiveDirectory = NULL;
  89. if (m_InboundProfile)
  90. SysFreeString(m_InboundProfile);
  91. m_InboundProfile = NULL;
  92. return S_OK;
  93. }
  94. STDMETHODIMP CFaxServer::GetPorts(VARIANT * retval)
  95. {
  96. HRESULT hr;
  97. if (!retval) {
  98. return E_POINTER;
  99. }
  100. CComObject<CFaxPorts>* p = new CComObject<CFaxPorts>;
  101. if (!p) {
  102. return E_OUTOFMEMORY;
  103. }
  104. if (!p->Init(this)) {
  105. delete p;
  106. return E_FAIL;
  107. }
  108. IDispatch* pDisp;
  109. hr = p->QueryInterface(IID_IDispatch, (void**)&pDisp);
  110. if (FAILED(hr)) {
  111. delete p;
  112. return hr;
  113. }
  114. __try {
  115. VariantInit(retval);
  116. retval->vt = VT_DISPATCH;
  117. retval->pdispVal = pDisp;
  118. return S_OK;
  119. } __except (EXCEPTION_EXECUTE_HANDLER) {
  120. }
  121. pDisp->Release();
  122. delete p;
  123. return E_UNEXPECTED;
  124. }
  125. STDMETHODIMP CFaxServer::CreateDocument(BSTR FileName, VARIANT * retval)
  126. {
  127. HRESULT hr;
  128. if (!FileName || !retval) {
  129. return E_POINTER;
  130. }
  131. CComObject<CFaxDoc>* p = new CComObject<CFaxDoc>;
  132. if (!p) {
  133. return E_OUTOFMEMORY;
  134. }
  135. if (!p->Init(FileName,this)) {
  136. delete p;
  137. return E_FAIL;
  138. }
  139. IDispatch* pDisp;
  140. hr = p->QueryInterface(IID_IDispatch, (void**)&pDisp);
  141. if (FAILED(hr)) {
  142. delete p;
  143. return hr;
  144. }
  145. __try {
  146. VariantInit(retval);
  147. retval->vt = VT_DISPATCH;
  148. retval->pdispVal = pDisp;
  149. return S_OK;
  150. } __except (EXCEPTION_EXECUTE_HANDLER) {
  151. }
  152. pDisp->Release();
  153. delete p;
  154. return E_UNEXPECTED;
  155. }
  156. STDMETHODIMP CFaxServer::GetJobs(VARIANT * retval)
  157. {
  158. HRESULT hr;
  159. if (!retval) {
  160. return(E_POINTER);
  161. }
  162. CComObject<CFaxJobs>* p = new CComObject<CFaxJobs>;
  163. if (!p) {
  164. return E_OUTOFMEMORY;
  165. }
  166. if (!p->Init(this)) {
  167. delete p;
  168. return E_FAIL;
  169. }
  170. IDispatch* pDisp;
  171. hr = p->QueryInterface(IID_IDispatch, (void**)&pDisp);
  172. if (FAILED(hr)) {
  173. delete p;
  174. return hr;
  175. }
  176. __try {
  177. VariantInit(retval);
  178. retval->vt = VT_DISPATCH;
  179. retval->pdispVal = pDisp;
  180. return S_OK;
  181. } __except (EXCEPTION_EXECUTE_HANDLER) {
  182. }
  183. pDisp->Release();
  184. delete p;
  185. return E_UNEXPECTED;
  186. }
  187. STDMETHODIMP CFaxServer::get_Retries(long * pVal)
  188. {
  189. if (!pVal) {
  190. return E_POINTER;
  191. }
  192. __try {
  193. *pVal = m_Retries;
  194. return S_OK;
  195. } __except(EXCEPTION_EXECUTE_HANDLER) {
  196. }
  197. return E_UNEXPECTED;
  198. }
  199. STDMETHODIMP CFaxServer::put_Retries(long newVal)
  200. {
  201. long oldval = m_Retries;
  202. m_Retries = newVal;
  203. if (!UpdateConfiguration() ) {
  204. m_Retries = oldval;
  205. return HRESULT_FROM_WIN32(m_LastFaxError);
  206. }
  207. else
  208. return S_OK;
  209. }
  210. STDMETHODIMP CFaxServer::get_RetryDelay(long * pVal)
  211. {
  212. if (!pVal) {
  213. return E_POINTER;
  214. }
  215. __try {
  216. *pVal = m_RetryDelay;
  217. return S_OK;
  218. } __except(EXCEPTION_EXECUTE_HANDLER) {
  219. }
  220. return E_UNEXPECTED;
  221. }
  222. STDMETHODIMP CFaxServer::put_RetryDelay(long newVal)
  223. {
  224. long oldval = m_RetryDelay;
  225. m_RetryDelay = newVal;
  226. if (!UpdateConfiguration() ) {
  227. m_RetryDelay = oldval;
  228. return HRESULT_FROM_WIN32(m_LastFaxError);
  229. }
  230. else
  231. return S_OK;
  232. }
  233. STDMETHODIMP CFaxServer::get_DirtyDays(long * pVal)
  234. {
  235. if (!pVal) {
  236. return E_POINTER;
  237. }
  238. __try {
  239. *pVal = m_DirtyDays;
  240. return S_OK;
  241. } __except(EXCEPTION_EXECUTE_HANDLER) {
  242. }
  243. return E_UNEXPECTED;
  244. }
  245. STDMETHODIMP CFaxServer::put_DirtyDays(long newVal)
  246. {
  247. long oldval = m_DirtyDays;
  248. m_DirtyDays = newVal;
  249. if (!UpdateConfiguration() ) {
  250. m_DirtyDays = oldval;
  251. return HRESULT_FROM_WIN32(m_LastFaxError);
  252. }
  253. else
  254. return S_OK;
  255. }
  256. STDMETHODIMP CFaxServer::get_Branding(BOOL * pVal)
  257. {
  258. if (!pVal) {
  259. return E_POINTER;
  260. }
  261. __try {
  262. *pVal = m_Branding;
  263. return S_OK;
  264. } __except(EXCEPTION_EXECUTE_HANDLER) {
  265. }
  266. return E_UNEXPECTED;
  267. }
  268. STDMETHODIMP CFaxServer::put_Branding(BOOL newVal)
  269. {
  270. BOOL oldval = m_Branding;
  271. m_Branding = newVal;
  272. if (!UpdateConfiguration() ) {
  273. m_Branding = oldval;
  274. return HRESULT_FROM_WIN32(m_LastFaxError);
  275. }
  276. else
  277. return S_OK;
  278. }
  279. STDMETHODIMP CFaxServer::get_UseDeviceTsid(BOOL * pVal)
  280. {
  281. if (!pVal) {
  282. return E_POINTER;
  283. }
  284. __try {
  285. *pVal = m_UseDeviceTsid;
  286. return S_OK;
  287. } __except(EXCEPTION_EXECUTE_HANDLER) {
  288. }
  289. return E_UNEXPECTED;
  290. }
  291. STDMETHODIMP CFaxServer::put_UseDeviceTsid(BOOL newVal)
  292. {
  293. BOOL oldval = m_UseDeviceTsid;
  294. m_UseDeviceTsid= newVal;
  295. if (!UpdateConfiguration() ) {
  296. m_UseDeviceTsid = oldval;
  297. return HRESULT_FROM_WIN32(m_LastFaxError);
  298. }
  299. else
  300. return S_OK;
  301. }
  302. STDMETHODIMP CFaxServer::get_ServerCoverpage(BOOL * pVal)
  303. {
  304. if (!pVal) {
  305. return E_POINTER;
  306. }
  307. __try {
  308. *pVal = m_ServerCp;
  309. return S_OK;
  310. } __except(EXCEPTION_EXECUTE_HANDLER) {
  311. }
  312. return E_UNEXPECTED;
  313. }
  314. STDMETHODIMP CFaxServer::put_ServerCoverpage(BOOL newVal)
  315. {
  316. BOOL oldval = m_ServerCp;
  317. m_ServerCp = newVal;
  318. if (!UpdateConfiguration() ) {
  319. m_ServerCp = oldval;
  320. return HRESULT_FROM_WIN32(m_LastFaxError);
  321. }
  322. else
  323. return S_OK;
  324. }
  325. STDMETHODIMP CFaxServer::get_PauseServerQueue(BOOL * pVal)
  326. {
  327. if (!pVal) {
  328. return E_POINTER;
  329. }
  330. __try {
  331. *pVal = m_PauseServerQueue;
  332. return S_OK;
  333. } __except(EXCEPTION_EXECUTE_HANDLER) {
  334. }
  335. return E_UNEXPECTED;
  336. }
  337. STDMETHODIMP CFaxServer::put_PauseServerQueue(BOOL newVal)
  338. {
  339. BOOL oldval = m_PauseServerQueue;
  340. m_PauseServerQueue = newVal;
  341. if (!UpdateConfiguration() ) {
  342. m_PauseServerQueue = oldval;
  343. return HRESULT_FROM_WIN32(m_LastFaxError);
  344. }
  345. else
  346. return S_OK;
  347. }
  348. STDMETHODIMP CFaxServer::get_ArchiveOutboundFaxes(BOOL * pVal)
  349. {
  350. if (!pVal) {
  351. return E_POINTER;
  352. }
  353. __try {
  354. *pVal = m_ArchiveOutgoingFaxes;
  355. return S_OK;
  356. } __except(EXCEPTION_EXECUTE_HANDLER) {
  357. }
  358. return E_UNEXPECTED;
  359. }
  360. STDMETHODIMP CFaxServer::put_ArchiveOutboundFaxes(BOOL newVal)
  361. {
  362. BOOL oldval = m_ArchiveOutgoingFaxes;
  363. m_ArchiveOutgoingFaxes = newVal;
  364. if (!UpdateConfiguration() ) {
  365. m_ArchiveOutgoingFaxes = oldval;
  366. return HRESULT_FROM_WIN32(m_LastFaxError);
  367. }
  368. else
  369. return S_OK;
  370. }
  371. STDMETHODIMP CFaxServer::get_ArchiveDirectory(BSTR * pVal)
  372. {
  373. if (!pVal) {
  374. return E_POINTER;
  375. }
  376. BSTR Copy = SysAllocString(m_ArchiveDirectory);
  377. if (!Copy && m_ArchiveDirectory) {
  378. return E_OUTOFMEMORY;
  379. }
  380. __try {
  381. *pVal = Copy;
  382. return S_OK;
  383. } __except(EXCEPTION_EXECUTE_HANDLER) {
  384. SysFreeString( Copy );
  385. }
  386. return E_UNEXPECTED;
  387. }
  388. STDMETHODIMP CFaxServer::put_ArchiveDirectory(BSTR newVal)
  389. {
  390. BSTR tmp = SysAllocString(newVal);
  391. BSTR old = m_ArchiveDirectory;
  392. if (!tmp && newVal) {
  393. return E_OUTOFMEMORY;
  394. }
  395. m_ArchiveDirectory = tmp;
  396. if (!UpdateConfiguration() ) {
  397. SysFreeString(tmp);
  398. m_ArchiveDirectory = old;
  399. return HRESULT_FROM_WIN32(m_LastFaxError);
  400. } else {
  401. SysFreeString(old);
  402. return S_OK;
  403. }
  404. }
  405. STDMETHODIMP CFaxServer::get_ServerMapiProfile(BSTR * pVal)
  406. {
  407. if (!pVal) {
  408. return E_POINTER;
  409. }
  410. BSTR Copy = SysAllocString(m_InboundProfile);
  411. if (!Copy && m_InboundProfile) {
  412. return E_OUTOFMEMORY;
  413. }
  414. __try {
  415. *pVal = Copy;
  416. return S_OK;
  417. } __except(EXCEPTION_EXECUTE_HANDLER) {
  418. SysFreeString(Copy);
  419. }
  420. return E_UNEXPECTED;
  421. }
  422. STDMETHODIMP CFaxServer::put_ServerMapiProfile(BSTR newVal)
  423. {
  424. BSTR tmp = SysAllocString(newVal);
  425. BSTR old = m_InboundProfile;
  426. if (!tmp && newVal) {
  427. return E_OUTOFMEMORY;
  428. }
  429. m_InboundProfile = tmp;
  430. if (!UpdateConfiguration() ) {
  431. SysFreeString(tmp);
  432. m_InboundProfile = old;
  433. return HRESULT_FROM_WIN32(m_LastFaxError);
  434. } else {
  435. SysFreeString(old);
  436. return S_OK;
  437. }
  438. }
  439. STDMETHODIMP CFaxServer::get_DiscountRateStartHour(short * pVal)
  440. {
  441. if (!pVal) {
  442. return E_POINTER;
  443. }
  444. __try {
  445. *pVal = m_StartCheapTime.Hour;
  446. return S_OK;
  447. } __except(EXCEPTION_EXECUTE_HANDLER) {
  448. }
  449. return E_UNEXPECTED;
  450. }
  451. STDMETHODIMP CFaxServer::put_DiscountRateStartHour(short newVal)
  452. {
  453. short old = m_StartCheapTime.Hour;
  454. m_StartCheapTime.Hour = newVal;
  455. if (!UpdateConfiguration() ) {
  456. m_StartCheapTime.Hour = old;
  457. return HRESULT_FROM_WIN32(m_LastFaxError);
  458. }
  459. else
  460. return S_OK;
  461. }
  462. STDMETHODIMP CFaxServer::get_DiscountRateStartMinute(short * pVal)
  463. {
  464. if (!pVal) {
  465. return E_POINTER;
  466. }
  467. __try {
  468. *pVal = m_StartCheapTime.Minute;
  469. return S_OK;
  470. } __except(EXCEPTION_EXECUTE_HANDLER) {
  471. }
  472. return E_UNEXPECTED;
  473. }
  474. STDMETHODIMP CFaxServer::put_DiscountRateStartMinute(short newVal)
  475. {
  476. short old = m_StartCheapTime.Minute;
  477. m_StartCheapTime.Minute = newVal;
  478. if (!UpdateConfiguration() ) {
  479. m_StartCheapTime.Minute = old;
  480. return HRESULT_FROM_WIN32(m_LastFaxError);
  481. }
  482. else
  483. return S_OK;
  484. }
  485. STDMETHODIMP CFaxServer::get_DiscountRateEndHour(short * pVal)
  486. {
  487. if (!pVal) {
  488. return E_POINTER;
  489. }
  490. __try {
  491. *pVal = m_StopCheapTime.Hour;
  492. return S_OK;
  493. } __except(EXCEPTION_EXECUTE_HANDLER) {
  494. }
  495. return E_UNEXPECTED;
  496. }
  497. STDMETHODIMP CFaxServer::put_DiscountRateEndHour(short newVal)
  498. {
  499. short old = m_StopCheapTime.Hour;
  500. m_StopCheapTime.Hour = newVal;
  501. if (!UpdateConfiguration() ) {
  502. m_StopCheapTime.Hour = old;
  503. return HRESULT_FROM_WIN32(m_LastFaxError);
  504. }
  505. else
  506. return S_OK;
  507. }
  508. STDMETHODIMP CFaxServer::get_DiscountRateEndMinute(short * pVal)
  509. {
  510. if (!pVal) {
  511. return E_POINTER;
  512. }
  513. __try {
  514. *pVal = m_StopCheapTime.Minute;
  515. return S_OK;
  516. } __except(EXCEPTION_EXECUTE_HANDLER) {
  517. }
  518. return E_UNEXPECTED;
  519. }
  520. STDMETHODIMP CFaxServer::put_DiscountRateEndMinute(short newVal)
  521. {
  522. short old = m_StopCheapTime.Minute;
  523. m_StopCheapTime.Minute = newVal;
  524. if (!UpdateConfiguration() ) {
  525. m_StopCheapTime.Minute = old;
  526. return HRESULT_FROM_WIN32(m_LastFaxError);
  527. }
  528. else
  529. return S_OK;
  530. }
  531. BOOL CFaxServer::RetrieveConfiguration()
  532. {
  533. if (!m_FaxHandle) {
  534. return FALSE;
  535. }
  536. PFAX_CONFIGURATIONW FaxConfig;
  537. if (!FaxGetConfigurationW(m_FaxHandle,&FaxConfig) ) {
  538. m_LastFaxError = GetLastError();
  539. return FALSE;
  540. }
  541. m_Retries = FaxConfig->Retries;
  542. m_RetryDelay =FaxConfig->RetryDelay;
  543. m_DirtyDays = FaxConfig->DirtyDays;
  544. m_Branding = FaxConfig->Branding;
  545. m_UseDeviceTsid = FaxConfig->UseDeviceTsid;
  546. m_ServerCp = FaxConfig->ServerCp;
  547. m_PauseServerQueue = FaxConfig->PauseServerQueue;
  548. m_StartCheapTime.Hour = FaxConfig->StartCheapTime.Hour;
  549. m_StartCheapTime.Minute = FaxConfig->StartCheapTime.Minute;
  550. m_StopCheapTime.Hour = FaxConfig->StopCheapTime.Hour;
  551. m_StopCheapTime.Minute = FaxConfig->StopCheapTime.Minute;
  552. m_StartCheapTime.Hour = FaxConfig->StartCheapTime.Hour;
  553. m_ArchiveOutgoingFaxes = FaxConfig->ArchiveOutgoingFaxes;
  554. m_ArchiveDirectory = SysAllocString(FaxConfig->ArchiveDirectory);
  555. if ((!m_ArchiveDirectory && FaxConfig->ArchiveDirectory)) {
  556. m_LastFaxError = ERROR_OUTOFMEMORY;
  557. }
  558. m_InboundProfile = SysAllocString(FaxConfig->InboundProfile);
  559. if ((!m_InboundProfile && FaxConfig->InboundProfile)) {
  560. m_LastFaxError = ERROR_OUTOFMEMORY;
  561. }
  562. FaxFreeBuffer(FaxConfig);
  563. return (m_LastFaxError == NO_ERROR) ? TRUE : FALSE;
  564. }
  565. BOOL CFaxServer::UpdateConfiguration()
  566. {
  567. if (!m_FaxHandle) {
  568. return FALSE;
  569. }
  570. FAX_CONFIGURATIONW FaxConfig;
  571. ZeroMemory(&FaxConfig,sizeof(FAX_CONFIGURATIONW) );
  572. FaxConfig.SizeOfStruct = sizeof(FAX_CONFIGURATIONW);
  573. FaxConfig.Retries = m_Retries;
  574. FaxConfig.RetryDelay = m_RetryDelay ;
  575. FaxConfig.DirtyDays = m_DirtyDays;
  576. FaxConfig.Branding = m_Branding;
  577. FaxConfig.UseDeviceTsid = m_UseDeviceTsid;
  578. FaxConfig.ServerCp = m_ServerCp;
  579. FaxConfig.PauseServerQueue = m_PauseServerQueue;
  580. FaxConfig.StartCheapTime.Hour = m_StartCheapTime.Hour;
  581. FaxConfig.StartCheapTime.Minute = m_StartCheapTime.Minute;
  582. FaxConfig.StopCheapTime.Hour = m_StopCheapTime.Hour;
  583. FaxConfig.StopCheapTime.Minute = m_StopCheapTime.Minute;
  584. FaxConfig.StartCheapTime.Hour = m_StartCheapTime.Hour;
  585. FaxConfig.ArchiveOutgoingFaxes = m_ArchiveOutgoingFaxes;
  586. FaxConfig.ArchiveDirectory = m_ArchiveDirectory;
  587. FaxConfig.InboundProfile = m_InboundProfile;
  588. if (!FaxSetConfigurationW(m_FaxHandle,&FaxConfig) ) {
  589. m_LastFaxError = GetLastError();
  590. return FALSE;
  591. }
  592. return TRUE;
  593. }