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.

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