home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / hplip / ui4 / printdialog.py < prev    next >
Encoding:
Python Source  |  2009-04-14  |  12.3 KB  |  352 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2001-2009 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Authors: Don Welch
  20. #
  21.  
  22.  
  23. # Local
  24. from base.g import *
  25. from base import device, utils
  26. from prnt import cups
  27. from base.codes import *
  28. from ui_utils import *
  29.  
  30. # Qt
  31. from PyQt4.QtCore import *
  32. from PyQt4.QtGui import *
  33.  
  34. # Ui
  35. from printdialog_base import Ui_Dialog
  36. from filetable import FileTable, FILETABLE_TYPE_PRINT
  37. from printernamecombobox import PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY
  38.  
  39. PAGE_FILE = 0
  40. PAGE_OPTIONS = 1
  41. PAGE_MAX = 1
  42.  
  43.  
  44. class PrintDialog(QDialog, Ui_Dialog):
  45.     def __init__(self, parent, printer_name, args=None):
  46.         QDialog.__init__(self, parent)
  47.         self.setupUi(self)
  48.  
  49.         self.printer_name = printer_name
  50.  
  51.         # User settings
  52.         self.user_settings = UserSettings()
  53.         self.user_settings.load()
  54.         self.user_settings.debug()
  55.  
  56.         self.initUi()
  57.  
  58.         self.file_list = []
  59.         if args is not None:
  60.             for a in args:
  61.                 self.Files.addFileFromUI(os.path.abspath(a))
  62.  
  63.         self.devices = {}
  64.  
  65.  
  66.         QTimer.singleShot(0, self.updateFilePage)
  67.  
  68.  
  69.     def initUi(self):
  70.         self.OptionsToolBox.include_job_options = True
  71.  
  72.         # connect signals/slots
  73.         self.connect(self.CancelButton, SIGNAL("clicked()"), self.CancelButton_clicked)
  74.         self.connect(self.BackButton, SIGNAL("clicked()"), self.BackButton_clicked)
  75.         self.connect(self.NextButton, SIGNAL("clicked()"), self.NextButton_clicked)
  76.  
  77.         self.initFilePage()
  78.         self.initOptionsPage()
  79.  
  80.         # Application icon
  81.         self.setWindowIcon(QIcon(load_pixmap('prog', '48x48')))
  82.  
  83.         if self.printer_name:
  84.             self.PrinterName.setInitialPrinter(self.printer_name)
  85.  
  86.         self.StackedWidget.setCurrentIndex(0)
  87.  
  88.  
  89.     #
  90.     # File Page
  91.     #
  92.  
  93.     def initFilePage(self):
  94.         self.Files.setType(FILETABLE_TYPE_PRINT)
  95.         self.Files.setWorkingDir(user_conf.workingDirectory())
  96.         self.connect(self.Files, SIGNAL("isEmpty"), self.Files_isEmpty)
  97.         self.connect(self.Files, SIGNAL("isNotEmpty"), self.Files_isNotEmpty)
  98.  
  99.  
  100.     def updateFilePage(self):
  101.         self.NextButton.setText(self.__tr("Next >"))
  102.         self.NextButton.setEnabled(self.Files.isNotEmpty())
  103.         self.BackButton.setEnabled(False)
  104.         self.updateStepText(PAGE_FILE)
  105.         self.Files.updateUi()
  106.  
  107.     def Files_isEmpty(self):
  108.         self.NextButton.setEnabled(False)
  109.  
  110.  
  111.     def Files_isNotEmpty(self):
  112.         self.NextButton.setEnabled(True)
  113.  
  114.  
  115.     #
  116.     # Options Page
  117.     #
  118.  
  119.     def initOptionsPage(self):
  120.         self.BackButton.setEnabled(True)
  121.         self.PrinterName.setType(PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY)
  122.  
  123.         self.connect(self.PrinterName, SIGNAL("PrinterNameComboBox_currentChanged"),
  124.             self.PrinterNameComboBox_currentChanged)
  125.  
  126.         self.connect(self.PrinterName, SIGNAL("PrinterNameComboBox_noPrinters"),
  127.             self.PrinterNameComboBox_noPrinters)
  128.  
  129.  
  130.     def updateOptionsPage(self):
  131.         QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  132.         try:
  133.             self.PrinterName.updateUi()
  134.             self.BackButton.setEnabled(True)
  135.             num_files = len(self.Files.file_list)
  136.  
  137.             if  num_files > 1:
  138.                 self.NextButton.setText(self.__tr("Print %1 Files").arg(num_files))
  139.             else:
  140.                 self.NextButton.setText(self.__tr("Print File"))
  141.  
  142.             self.updateStepText(PAGE_OPTIONS)
  143.             # TODO: Enable print button only if printer is accepting and all options are OK (esp. page range)
  144.         finally:
  145.             QApplication.restoreOverrideCursor()
  146.  
  147.  
  148.     def PrinterNameComboBox_currentChanged(self, device_uri, printer_name):
  149.         try:
  150.             self.devices[device_uri]
  151.         except KeyError:
  152.             self.devices[device_uri] = device.Device(device_uri)
  153.  
  154.         self.OptionsToolBox.updateUi(self.devices[device_uri], printer_name)
  155.  
  156.  
  157.     def PrinterNameComboBox_noPrinters(self):
  158.         FailureUI(self, self.__tr("<b>No printers found.</b><p>Please setup a printer and try again."))
  159.         self.close()
  160.  
  161.  
  162.     #
  163.     # Print
  164.     #
  165.  
  166.     def executePrint(self):
  167.         for cmd in self.OptionsToolBox.getPrintCommands(self.Files.file_list):
  168.             log.debug(cmd)
  169.             status, output = utils.run(cmd, log_output=True, password_func=None, timeout=1)
  170.             if status != 0:
  171.                 FailureUI(self, self.__tr("<b>Print command failed with status code %1.</b><p>%2</p>").arg(status).arg(cmd))
  172.  
  173.         self.close()
  174.         #print file('/home/dwelch/.cups/lpoptions', 'r').read()
  175.  
  176.     #
  177.     # Misc
  178.     #
  179.  
  180.     def CancelButton_clicked(self):
  181.         self.close()
  182.  
  183.  
  184.     def BackButton_clicked(self):
  185.         p = self.StackedWidget.currentIndex()
  186.         if p == PAGE_OPTIONS:
  187.             self.StackedWidget.setCurrentIndex(PAGE_FILE)
  188.             self.updateFilePage()
  189.  
  190.         else:
  191.             log.error("Invalid page!") # shouldn't happen!
  192.  
  193.  
  194.     def NextButton_clicked(self):
  195.         p = self.StackedWidget.currentIndex()
  196.         if p == PAGE_FILE:
  197.             self.StackedWidget.setCurrentIndex(PAGE_OPTIONS)
  198.             self.updateOptionsPage()
  199.  
  200.         elif p == PAGE_OPTIONS:
  201.             self.executePrint()
  202.  
  203.  
  204.     def updateStepText(self, p):
  205.         self.StepText.setText(self.__tr("Step %1 of %2").arg(p+1).arg(PAGE_MAX+1))
  206.  
  207.  
  208.     def __tr(self,s,c = None):
  209.         return qApp.translate("PrintDialog",s,c)
  210.  
  211.  
  212.  
  213. """
  214.    def printButton_clicked(self):
  215.         if self.invalid_page_range:
  216.             self.form.FailureUI(self.__tr("<b>Cannot print: Invalid page range: %1</b><p>A valid page range is a list of pages or ranges of pages separated by commas (e.g., 1-2,4,6-7)").arg(self.pageRangeEdit.text()))
  217.             return
  218.  
  219.         try:
  220.             try:
  221.                 self.cur_device.open()
  222.             except Error:
  223.                 self.form.FailureUI(self.__tr("<b>Cannot print: Device is busy or not available.</b><p>Please check device and try again."))
  224.                 return
  225.  
  226.             if 1: # Go ahead and allow - print will be queued in CUPS if not rejecting
  227.                 printers = cups.getPrinters()
  228.                 for p in printers:
  229.                     if p.name == self.cur_printer:
  230.                         break
  231.  
  232.                 if p.state == cups.IPP_PRINTER_STATE_STOPPED:
  233.                     self.form.FailureUI(self.__tr("<b>Cannot print: Printer is stopped.</b><p>Please START the printer to continue this print. Job will begin printing once printer is started."))
  234.  
  235.                 if not p.accepting:
  236.                     self.form.FailureUI(self.__tr("<b>Cannot print: Printer is not accepting jobs.</b><p>Please set the printer to ACCEPTING JOBS to continue printing."))
  237.                     return
  238.  
  239.                 copies = int(self.copiesSpinBox.value())
  240.                 all_pages = self.pages_button_group == 0
  241.                 page_range = unicode(self.pageRangeEdit.text())
  242.                 page_set = int(self.pageSetComboBox.currentItem())
  243.  
  244.                 cups.resetOptions()
  245.                 cups.openPPD(self.cur_printer)
  246.                 current_options = dict(cups.getOptions())
  247.                 cups.closePPD()
  248.  
  249.                 nup = int(current_options.get("number-up", 1))
  250.  
  251.                 for p, t, d in self.file_list:
  252.  
  253.                     alt_nup = (nup > 1 and t == 'application/postscript' and utils.which('psnup'))
  254.  
  255.                     if utils.which('lpr'):
  256.                         if alt_nup:
  257.                             cmd = ' '.join(['psnup', '-%d' % nup, ''.join(['"', p, '"']), '| lpr -P', self.cur_printer])
  258.                         else:
  259.                             cmd = ' '.join(['lpr -P', self.cur_printer])
  260.  
  261.                         if copies > 1:
  262.                             cmd = ' '.join([cmd, '-#%d' % copies])
  263.  
  264.                     else:
  265.                         if alt_nup:
  266.                             cmd = ' '.join(['psnup', '-%d' % nup, ''.join(['"', p, '"']), '| lp -c -d', self.cur_printer])
  267.                         else:
  268.                             cmd = ' '.join(['lp -c -d', self.cur_printer])
  269.  
  270.                         if copies > 1:
  271.                             cmd = ' '.join([cmd, '-n%d' % copies])
  272.  
  273.  
  274.                     if not all_pages and len(page_range) > 0:
  275.                         cmd = ' '.join([cmd, '-o page-ranges=%s' % page_range])
  276.  
  277.                     if page_set > 0:
  278.                         if page_set == 1:
  279.                             cmd = ' '.join([cmd, '-o page-set=even'])
  280.                         else:
  281.                             cmd = ' '.join([cmd, '-o page-set=odd'])
  282.  
  283.  
  284.                     # Job Storage
  285.                     # self.job_storage_mode = (0=Off, 1=P&H, 2=PJ, 3=QC, 4=SJ)
  286.                     # self.job_storage_pin = u"" (dddd)
  287.                     # self.job_storage_use_pin = True|False
  288.                     # self.job_storage_username = u""
  289.                     # self.job_storage_auto_username = True|False
  290.                     # self.job_storage_jobname = u""
  291.                     # self.job_storage_auto_jobname = True|False
  292.                     # self.job_storage_job_exist = (0=replace, 1=job name+(1-99))
  293.  
  294.                     if self.job_storage_avail:
  295.                         if self.job_storage_mode: # On
  296.  
  297.                             if self.job_storage_mode == 1: # Proof and Hold
  298.                                 cmd = ' '.join([cmd, '-o HOLD=PROOF'])
  299.  
  300.                             elif self.job_storage_mode == 2: # Private Job
  301.                                 if self.job_storage_use_pin:
  302.                                     cmd = ' '.join([cmd, '-o HOLD=ON'])
  303.                                     cmd = ' '.join([cmd, '-o HOLDTYPE=PRIVATE'])
  304.                                     cmd = ' '.join([cmd, '-o HOLDKEY=%s' % self.job_storage_pin.encode('ascii')])
  305.                                 else:
  306.                                     cmd = ' '.join([cmd, '-o HOLD=PROOF'])
  307.                                     cmd = ' '.join([cmd, '-o HOLDTYPE=PRIVATE'])
  308.  
  309.                             elif self.job_storage_mode == 3: # Quick Copy
  310.                                 cmd = ' '.join([cmd, '-o HOLD=ON'])
  311.                                 cmd = ' '.join([cmd, '-o HOLDTYPE=PUBLIC'])
  312.  
  313.                             elif self.job_storage_mode == 4: # Store Job
  314.                                 if self.job_storage_use_pin:
  315.                                     cmd = ' '.join([cmd, '-o HOLD=STORE'])
  316.                                     cmd = ' '.join([cmd, '-o HOLDTYPE=PRIVATE'])
  317.                                     cmd = ' '.join([cmd, '-o HOLDKEY=%s' % self.job_storage_pin.encode('ascii')])
  318.                                 else:
  319.                                     cmd = ' '.join([cmd, '-o HOLD=STORE'])
  320.  
  321.                             cmd = ' '.join([cmd, '-o USERNAME=%s' % self.job_storage_username.encode('ascii')\
  322.                                 .replace(" ", "_")])
  323.  
  324.                             cmd = ' '.join([cmd, '-o JOBNAME=%s' % self.job_storage_jobname.encode('ascii')\
  325.                                 .replace(" ", "_")])
  326.  
  327.                             if self.job_storage_job_exist == 1:
  328.                                 cmd = ' '.join([cmd, '-o DUPLICATEJOB=APPEND'])
  329.                             else:
  330.                                 cmd = ' '.join([cmd, '-o DUPLICATEJOB=REPLACE'])
  331.  
  332.                         else: # Off
  333.                             cmd = ' '.join([cmd, '-o HOLD=OFF'])
  334.  
  335.  
  336.                     if not alt_nup:
  337.                         cmd = ''.join([cmd, ' "', p, '"'])
  338.  
  339.                     log.debug("Printing: %s" % cmd)
  340.  
  341.                     code = os.system(cmd)
  342.                     if code != 0:
  343.                         log.error("Print command failed.")
  344.                         self.form.FailureUI(self.__tr("Print command failed with error code %1").arg(code))
  345.  
  346.                 self.form.close()
  347.  
  348.         finally:
  349.             self.cur_device.close()
  350.  
  351. """
  352.