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.

550 lines
12 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1995 - 1998
  3. All rights reserved.
  4. Module Name:
  5. printer.hxx
  6. Abstract:
  7. The printer object handles all information regarding a print
  8. queue (and eventually server object). The printer is not
  9. concerned with UI or listviews; it takes a printer name and
  10. returns via callbacks any changes of the printer state.
  11. The remote server notifies the local printer of changes through
  12. either a single DWORD (downlevel connections), or full change
  13. information (uplevel 3.51+ connections). The TData object
  14. abstracts the changes for us.
  15. Author:
  16. Albert Ting (AlbertT) 10-June-1995
  17. Revision History:
  18. --*/
  19. #ifndef _PRINTER_HXX
  20. #define _PRINTER_HXX
  21. class TPrinter;
  22. /********************************************************************
  23. MPrinterClient
  24. ********************************************************************/
  25. class MPrinterClient : public VInfoClient, public MRefCom {
  26. SIGNATURE( 'prc' )
  27. public:
  28. MPrinterClient(
  29. VOID
  30. ) : _pPrinter( NULL )
  31. { }
  32. VAR( TPrinter*, pPrinter );
  33. /********************************************************************
  34. Retrieve selected items. Used when processing commands against
  35. items or saving and restoring the selection during a refresh.
  36. ********************************************************************/
  37. virtual
  38. COUNT
  39. cSelected(
  40. VOID
  41. ) const;
  42. virtual
  43. HITEM
  44. GetFirstSelItem(
  45. VOID
  46. ) const;
  47. virtual
  48. HITEM
  49. GetNextSelItem(
  50. HITEM hItem
  51. ) const;
  52. virtual
  53. IDENT
  54. GetId(
  55. HITEM hItem
  56. ) const;
  57. //
  58. // kContainerReloadItems: pPrinter has just refreshed and so the
  59. // client must invalidate the old pointers and use re-enumerate
  60. // new items.
  61. //
  62. // kContainerClearItems: pPrinter has just zeroed its list of items,
  63. // so the client must invalidate all old pointers.
  64. //
  65. virtual
  66. VOID
  67. vContainerChanged(
  68. CONTAINER_CHANGE ContainerChange,
  69. INFO Info
  70. ) = 0;
  71. virtual
  72. VOID
  73. vItemChanged(
  74. ITEM_CHANGE ItemChange,
  75. HITEM hItem,
  76. INFO Info,
  77. INFO InfoNew
  78. ) = 0;
  79. /********************************************************************
  80. VInfoClient virtual defaults.
  81. ********************************************************************/
  82. virtual
  83. VOID
  84. vSaveSelections(
  85. VOID
  86. );
  87. virtual
  88. VOID
  89. vRestoreSelections(
  90. VOID
  91. );
  92. virtual
  93. BOOL
  94. bGetPrintLib(
  95. TRefLock<TPrintLib> &refLock
  96. ) const = 0;
  97. };
  98. /********************************************************************
  99. Class definitions.
  100. ********************************************************************/
  101. class TPrinter : public MExecWork, public MDataClient {
  102. SIGNATURE( 'prnt' )
  103. SAFE_NEW
  104. public:
  105. /********************************************************************
  106. Public constants.
  107. ********************************************************************/
  108. enum _CONSTANTS {
  109. kOneSecond = 1000,
  110. kSleepRetry = 30 * kOneSecond, // 30 sec. timeout
  111. };
  112. enum _EXEC {
  113. #ifdef SLEEP_ON_MINIMIZE
  114. kExecSleep = 0x0000001,
  115. kExecAwake = 0x0000002,
  116. #endif
  117. kExecError = 0x0000004, // Error: don't try re-opening.
  118. kExecReopen = 0x0000008,
  119. kExecDelay = 0x0000010, // GetLastError set to error.
  120. kExecClose = 0x0000020,
  121. kExecRequestExit = 0x0000040,
  122. kExecNotifyStart = 0x0000100,
  123. kExecRegister = 0x0000200,
  124. kExecNotifyEnd = 0x0000400,
  125. kExecRefresh = 0x0001000,
  126. kExecRefreshContainer = 0x0002000,
  127. kExecRefreshItem = 0x0004000,
  128. kExecRefreshAll = 0x0007000,
  129. kExecCommand = 0x0010000,
  130. };
  131. enum EJobStatusString {
  132. kMultipleJobStatusString, // Prepend job status string information
  133. kSingleJobStatusString, // Don't prepend job status string information
  134. };
  135. public:
  136. //
  137. // The sub-object that abstracts the type of remote printer from us.
  138. // (May have the polling notifications, single DWORD notifications,
  139. // or full information notifications.)
  140. //
  141. VAR( VData*, pData );
  142. VAR( TRefLock<TPrintLib>, pPrintLib );
  143. //
  144. // Initialized once, then read only; no guarding needed.
  145. // OR called by UI thread.
  146. //
  147. VAR( DWORD, dwAccess );
  148. VAR( EJobStatusString, eJobStatusStringType );
  149. BOOL
  150. bValid(
  151. VOID
  152. ) const
  153. {
  154. return VALID_OBJ( PrinterGuard._strPrinter );
  155. }
  156. /********************************************************************
  157. Don't use the standard ctr and dtr to create and destroy these
  158. objects; instead, use these member functions.
  159. pNew - Returns a new printer object.
  160. vDelete - Request that the printer destroy itself when all
  161. pending commands (pausing printer, jobs, etc.) have completed.
  162. ********************************************************************/
  163. static
  164. TPrinter*
  165. pNew(
  166. MPrinterClient* pPrinterClient,
  167. LPCTSTR pszPrinter,
  168. DWORD dwAccess
  169. );
  170. VOID
  171. vDelete(
  172. VOID
  173. );
  174. const TString&
  175. strPrinter(
  176. VOID
  177. ) const
  178. {
  179. return PrinterGuard._strPrinter;
  180. }
  181. BOOL
  182. bSyncRefresh(
  183. VOID
  184. );
  185. /********************************************************************
  186. UI interface routines: the UI client calls through these
  187. functions for services.
  188. vCommandQueue - Allocate a command object and put it into the
  189. command queue (commands execute serially in a separate
  190. thread).
  191. vCommandRequested - Call when a new command has been added that
  192. requires a sleeping printer to re-awake. This allows a
  193. refresh to take place even if the printer is sleeping in
  194. poll mode.
  195. vProcessData - Call when synchronous data must be processed.
  196. ********************************************************************/
  197. VOID
  198. vCommandQueue(
  199. TSelection* pSelection
  200. );
  201. VOID
  202. vCommandRequested(
  203. VOID
  204. );
  205. VOID
  206. vProcessData(
  207. DWORD dwParam,
  208. HBLOCK hBlock
  209. );
  210. LPTSTR
  211. pszPrinterName(
  212. LPTSTR pszPrinterBuffer
  213. ) const;
  214. LPTSTR
  215. pszServerName(
  216. LPTSTR pszServerBuffer
  217. ) const;
  218. /********************************************************************
  219. Generic exported services.
  220. ********************************************************************/
  221. static
  222. STATUS
  223. sOpenPrinter(
  224. LPCTSTR pszPrinter,
  225. PDWORD pdwAccess,
  226. PHANDLE phPrinter
  227. );
  228. private:
  229. class TPrinterClientRef {
  230. public:
  231. TPrinterClientRef(
  232. const TPrinter* pPrinter
  233. );
  234. ~TPrinterClientRef(
  235. VOID
  236. );
  237. BOOL
  238. bValid(
  239. VOID
  240. ) const
  241. {
  242. return _pPrinterClient != NULL;
  243. }
  244. MPrinterClient*
  245. ptr(
  246. VOID
  247. )
  248. {
  249. return _pPrinterClient;
  250. }
  251. private:
  252. MPrinterClient* _pPrinterClient;
  253. };
  254. //
  255. // Guarded by TExec: one thread processing TPrinter at a time,
  256. // and/or guarded by state machine.
  257. //
  258. struct _EXEC_GUARD {
  259. VAR( HANDLE, hPrinter );
  260. } ExecGuard;
  261. //
  262. // All field in this structure guarded by gpCritSec.
  263. //
  264. struct PRINTER_GUARD {
  265. //
  266. // Written by Exec threads only; guarding not needed from
  267. // reading from ExecGuard places.
  268. //
  269. VAR( TString, strPrinter );
  270. VAR( TString, strServer );
  271. VAR( HANDLE, hEventCommand );
  272. VAR( DWORD, dwError );
  273. //
  274. // Only changed in UIThread.
  275. //
  276. VAR( MPrinterClient*, pPrinterClient );
  277. //
  278. // Linked list of commands to execute.
  279. //
  280. DLINK_BASE( TSelection, Selection, Selection );
  281. } PrinterGuard;
  282. /********************************************************************
  283. Ctr, dtr declared private so that clients are forced to use
  284. pNew, vDelete.
  285. ********************************************************************/
  286. TPrinter(
  287. MPrinterClient* pPrinterClient,
  288. LPCTSTR pszPrinter,
  289. DWORD dwAccess
  290. );
  291. ~TPrinter(
  292. VOID
  293. );
  294. /********************************************************************
  295. Status reporting.
  296. ********************************************************************/
  297. VOID
  298. vErrorStatusChanged(
  299. DWORD dwStatus
  300. );
  301. VOID
  302. vConnectStatusChanged(
  303. CONNECT_STATUS ConnectStatus
  304. );
  305. /********************************************************************
  306. Define virtual funcs for MExecWork:
  307. svExecute - A job is ready to execute and should run now in the
  308. current thread.
  309. vExecFailedAddJob - This printer object tried to add a job,
  310. but the add failed and we need to put up a suitable
  311. error message.
  312. vExecExitComplete - We requested an EXIT job and we are finally
  313. processing it now (the exit will wait until the currently
  314. pending job has completed). This routine will delete
  315. the object.
  316. ********************************************************************/
  317. STATEVAR
  318. svExecute(
  319. STATEVAR StateVar
  320. );
  321. VOID
  322. vExecFailedAddJob(
  323. VOID
  324. );
  325. VOID
  326. vExecExitComplete(
  327. VOID
  328. );
  329. /********************************************************************
  330. Called by worker threads only; these operations may hit
  331. the network and therefore take a long time to complete.
  332. ********************************************************************/
  333. #ifdef SLEEP_ON_MINIMIZE
  334. STATEVAR
  335. svAwake(
  336. STATEVAR StateVar
  337. );
  338. #endif
  339. STATEVAR
  340. svSleep(
  341. STATEVAR StateVar
  342. );
  343. STATEVAR
  344. svRefresh(
  345. STATEVAR StateVar
  346. );
  347. STATEVAR
  348. svReopen(
  349. STATEVAR StateVar
  350. );
  351. STATEVAR
  352. svDelay(
  353. STATEVAR StateVar
  354. );
  355. STATEVAR
  356. svClose(
  357. STATEVAR StateVar
  358. );
  359. STATEVAR
  360. svRequestExit(
  361. STATEVAR StateVar
  362. );
  363. STATEVAR
  364. svNotifyStart(
  365. STATEVAR StateVar
  366. );
  367. STATEVAR
  368. svNotifyEnd(
  369. STATEVAR StateVar
  370. );
  371. STATEVAR
  372. svCommand(
  373. STATEVAR StateVar
  374. );
  375. /********************************************************************
  376. MDataClient virtual definitions.
  377. ********************************************************************/
  378. VOID
  379. vContainerChanged(
  380. CONTAINER_CHANGE ContainerChange,
  381. INFO Info
  382. );
  383. VOID
  384. vItemChanged(
  385. ITEM_CHANGE ItemChange,
  386. HITEM hItem,
  387. INFO Info,
  388. INFO InfoNew
  389. );
  390. VOID
  391. vSaveSelections(
  392. VOID
  393. );
  394. VOID
  395. vRestoreSelections(
  396. VOID
  397. );
  398. BOOL
  399. bGetPrintLib(
  400. TRefLock<TPrintLib> &refLock
  401. ) const;
  402. VDataNotify*
  403. pNewNotify(
  404. MDataClient* pDataClient
  405. ) const;
  406. VDataRefresh*
  407. pNewRefresh(
  408. MDataClient* pDataClient
  409. ) const;
  410. HANDLE
  411. hPrinter(
  412. VOID
  413. ) const;
  414. HANDLE
  415. hPrinterNew(
  416. VOID
  417. ) const;
  418. friend TPrinterClientRef;
  419. };
  420. #endif // ifndef _PRINTER_HXX