2 # -*- coding: utf-8 -*-
4 # (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 __title__ = "Make Copies Utility"
25 __mod__ = 'hp-makecopies'
26 __doc__ = "PC initiated make copies function on supported HP AiO and MFP devices. (Note: Currently unsupported in Qt4.)"
39 from base import utils, device, pml, tui, module
40 from copier import copier
44 mod = module.Module(__mod__, __title__, __version__, __doc__, None,
45 (NON_INTERACTIVE_MODE, GUI_MODE),
46 (UI_TOOLKIT_QT3, UI_TOOLKIT_QT4))
48 mod.setUsage(module.USAGE_FLAG_DEVICE_ARGS,
50 ("Number of copies:", "-m<num_copies> or --copies=<num_copies> or --num=<num_copies> (1-99)", "option", False),
51 ("Reduction/enlargement:", "-r<%> or --reduction=<%> or --enlargement=<%> (25-400%)", "option", False),
52 ("Quality:", "-q<quality> or --quality=<quality> (where quality is: 'fast', 'draft', 'normal', 'presentation', or 'best')", "option", False),
53 ("Contrast:", "-c<contrast> or --contrast=<contrast> (-5 to +5)", "option", False),
54 ("Fit to page (flatbed only):", "-f or --fittopage or --fit (overrides reduction/enlargement)", "option", False)])
56 opts, device_uri, printer_name, mode, ui_toolkit, loc = \
57 mod.parseStdOpts('m:r:q:c:f',
58 ['num=', 'copies=', 'reduction=', 'enlargement=', 'quality=',
59 'contrast=', 'fittopage', 'fit', 'fit-to-page'])
61 device_uri = mod.getDeviceUri(device_uri, printer_name,
62 filter={'copy-type': (operator.gt, 0)})
66 reduction_spec = False
73 if o in ('-m', '--num', '--copies'):
77 log.warning("Invalid number of copies. Set to default of 1.")
81 log.warning("Invalid number of copies. Set to minimum of 1.")
85 log.warning("Invalid number of copies. Set to maximum of 99.")
88 elif o in ('-c', '--contrast'):
92 log.warning("Invalid contrast setting. Set to default of 0.")
96 log.warning("Invalid contrast setting. Set to minimum of -5.")
100 log.warning("Invalid contrast setting. Set to maximum of +5.")
105 elif o in ('-q', '--quality'):
106 a = a.lower().strip()
109 quality = pml.COPIER_QUALITY_FAST
111 elif a.startswith('norm'):
112 quality = pml.COPIER_QUALITY_NORMAL
114 elif a.startswith('pres'):
115 quality = pml.COPIER_QUALITY_PRESENTATION
117 elif a.startswith('draf'):
118 quality = pml.COPIER_QUALITY_DRAFT
121 quality = pml.COPIER_QUALITY_BEST
124 log.warning("Invalid quality. Set to default of 'normal'.")
126 elif o in ('-r', '--reduction', '--enlargement'):
127 reduction_spec = True
129 reduction = int(a.replace('%', ''))
131 log.warning("Invalid reduction %. Set to default of 100%.")
135 log.warning("Invalid reduction %. Set to minimum of 25%.")
138 elif reduction > 400:
139 log.warning("Invalid reduction %. Set to maximum of 400%.")
142 elif o in ('-f', '--fittopage', '--fit', '--fit-to-page'):
143 fit_to_page = pml.COPIER_FIT_TO_PAGE_ENABLED
147 if fit_to_page == pml.COPIER_FIT_TO_PAGE_ENABLED and reduction_spec:
148 log.warning("Fit to page specfied: Reduction/enlargement parameter ignored.")
152 if ui_toolkit == 'qt3':
153 if not utils.canEnterGUIMode():
154 log.error("%s requires GUI support (try running with --qt4). Also, try using non-interactive (-n) mode." % __mod__)
157 if not utils.canEnterGUIMode4():
158 log.error("%s requires GUI support (try running with --qt3). Also, try using non-interactive (-n) mode." % __mod__)
163 if ui_toolkit == 'qt3':
169 from ui.makecopiesform import MakeCopiesForm
171 log.error("Unable to load Qt3 support. Is it installed?")
174 # create the main application object
175 app = QApplication(sys.argv)
178 loc = user_conf.get('ui', 'loc', 'system')
179 if loc.lower() == 'system':
180 loc = str(QTextCodec.locale())
181 log.debug("Using system locale: %s" % loc)
183 if loc.lower() != 'c':
186 l, x = loc.split('.')
187 loc = '.'.join([l, e])
190 loc = '.'.join([loc, e])
192 log.debug("Trying to load .qm file for %s locale." % loc)
193 trans = QTranslator(None)
195 qm_file = 'hplip_%s.qm' % l
196 log.debug("Name of .qm file: %s" % qm_file)
197 loaded = trans.load(qm_file, prop.localization_dir)
200 app.installTranslator(trans)
205 log.debug("Using default 'C' locale")
207 log.debug("Using locale: %s" % loc)
208 QLocale.setDefault(QLocale(loc))
211 locale.setlocale(locale.LC_ALL, locale.normalize(loc))
216 makecopiesdlg = MakeCopiesForm(bus, device_uri, printer_name,
217 num_copies, contrast, quality,
218 reduction, fit_to_page)
221 app.setMainWidget(makecopiesdlg)
224 log.debug("Starting GUI loop...")
226 except KeyboardInterrupt:
231 from PyQt4.QtGui import QApplication
232 from ui4.makecopiesdialog import MakeCopiesDialog
234 log.error("Unable to load Qt4 support. Is it installed?")
239 app = QApplication(sys.argv)
241 dlg = MakeCopiesDialog(None, device_uri)
244 log.debug("Starting GUI loop...")
246 except KeyboardInterrupt:
254 else: # NON_INTERACTIVE_MODE
256 dev = copier.PMLCopyDevice(device_uri, printer_name)
262 if num_copies is None:
263 result_code, num_copies = dev.getPML(pml.OID_COPIER_NUM_COPIES)
266 result_code, contrast = dev.getPML(pml.OID_COPIER_CONTRAST)
268 if reduction is None:
269 result_code, reduction = dev.getPML(pml.OID_COPIER_REDUCTION)
272 result_code, quality = dev.getPML(pml.OID_COPIER_QUALITY)
274 if fit_to_page is None and dev.copy_type == COPY_TYPE_DEVICE:
275 result_code, fit_to_page = dev.getPML(pml.OID_COPIER_FIT_TO_PAGE)
277 fit_to_page = pml.COPIER_FIT_TO_PAGE_DISABLED
279 result_code, max_reduction = dev.getPML(pml.OID_COPIER_REDUCTION_MAXIMUM)
280 result_code, max_enlargement = dev.getPML(pml.OID_COPIER_ENLARGEMENT_MAXIMUM)
286 scan_src = dev.mq.get('scan-src', SCAN_SRC_FLATBED)
289 if scan_src == SCAN_SRC_SCROLLFED:
290 fit_to_page = pml.COPIER_FIT_TO_PAGE_DISABLED
292 log.debug("num_copies = %d" % num_copies)
293 log.debug("contrast= %d" % contrast)
294 log.debug("reduction = %d" % reduction)
295 log.debug("quality = %d" % quality)
296 log.debug("fit_to_page = %d" % fit_to_page)
297 log.debug("max_reduction = %d" % max_reduction)
298 log.debug("max_enlargement = %d" % max_enlargement)
299 log.debug("scan_src = %d" % scan_src)
301 update_queue = Queue.Queue()
302 event_queue = Queue.Queue()
304 dev.copy(num_copies, contrast, reduction,
305 quality, fit_to_page, scan_src,
306 update_queue, event_queue)
311 while update_queue.qsize():
313 status = update_queue.get(0)
317 if status == copier.STATUS_IDLE:
321 elif status in (copier.STATUS_SETTING_UP, copier.STATUS_WARMING_UP):
322 log.info("Warming up...")
325 elif status == copier.STATUS_ACTIVE:
326 log.info("Copying...")
329 elif status in (copier.STATUS_ERROR, copier.STATUS_DONE):
331 if status == copier.STATUS_ERROR:
332 log.error("Copier error!")
333 dev.sendEvent(EVENT_COPY_JOB_FAIL)
337 elif status == copier.STATUS_DONE:
346 except KeyboardInterrupt:
347 log.error("User interrupt. Canceling...")
348 event_queue.put(copier.COPY_CANCELED)
349 dev.sendEvent(EVENT_COPY_JOB_CANCELED)
351 dev.waitForCopyThread()
352 dev.sendEvent(EVENT_END_COPY_JOB)