home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / hplip / ui4 / setupdialog.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  29.3 KB  |  879 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import socket
  5. import operator
  6. from base.g import *
  7. from base import device, utils, models
  8. from prnt import cups
  9. from base.codes import *
  10. from ui_utils import *
  11. from PyQt4.QtCore import *
  12. from PyQt4.QtGui import *
  13. from setupdialog_base import Ui_Dialog
  14. from plugindialog import PluginDialog
  15.  
  16. try:
  17.     from fax import fax
  18.     fax_import_ok = True
  19. except ImportError:
  20.     fax_import_ok = False
  21.     log.warning('Fax setup disabled - Python 2.3+ required.')
  22.  
  23. PAGE_DISCOVERY = 0
  24. PAGE_DEVICES = 1
  25. PAGE_ADD_PRINTER = 2
  26. PAGE_MAX = 2
  27. BUTTON_NEXT = 0
  28. BUTTON_FINISH = 1
  29. BUTTON_ADD_PRINTER = 2
  30. ADVANCED_SHOW = 0
  31. ADVANCED_HIDE = 1
  32. DEVICE_DESC_ALL = 0
  33. DEVICE_DESC_SINGLE_FUNC = 1
  34. DEVICE_DESC_MULTI_FUNC = 2
  35.  
  36. class DeviceTableWidgetItem(QTableWidgetItem):
  37.     
  38.     def __init__(self, text, device_uri):
  39.         QTableWidgetItem.__init__(self, text, QTableWidgetItem.UserType)
  40.         self.device_uri = device_uri
  41.  
  42.  
  43.  
  44. class SetupDialog(QDialog, Ui_Dialog):
  45.     
  46.     def __init__(self, parent, param, jd_port, device_uri = None):
  47.         QDialog.__init__(self, parent)
  48.         self.setupUi(self)
  49.         self.param = param
  50.         self.jd_port = jd_port
  51.         self.device_uri = device_uri
  52.         if device_uri:
  53.             log.info('Using device: %s' % device_uri)
  54.         
  55.         self.initUi()
  56.         if self.skip_discovery:
  57.             QTimer.singleShot(0, self.showDevicesPage)
  58.         else:
  59.             QTimer.singleShot(0, self.showDiscoveryPage)
  60.  
  61.     
  62.     def initUi(self):
  63.         self.setWindowIcon(QIcon(load_pixmap('prog', '48x48')))
  64.         self.connect(self.CancelButton, SIGNAL('clicked()'), self.CancelButton_clicked)
  65.         self.connect(self.BackButton, SIGNAL('clicked()'), self.BackButton_clicked)
  66.         self.connect(self.NextButton, SIGNAL('clicked()'), self.NextButton_clicked)
  67.         self.connect(self.ManualGroupBox, SIGNAL('clicked(bool)'), self.ManualGroupBox_clicked)
  68.         self.initDiscoveryPage()
  69.         self.initDevicesPage()
  70.         self.initAddPrinterPage()
  71.  
  72.     
  73.     def initDiscoveryPage(self):
  74.         self.UsbRadioButton.setChecked(True)
  75.         self.setUsbRadioButton(True)
  76.         self.ManualGroupBox.setChecked(False)
  77.         self.advanced = False
  78.         self.manual = False
  79.         self.skip_discovery = False
  80.         self.NetworkRadioButton.setEnabled(prop.net_build)
  81.         self.ParallelRadioButton.setEnabled(prop.par_build)
  82.         self.devices = { }
  83.         self.bus = 'usb'
  84.         self.timeout = 5
  85.         self.ttl = 4
  86.         self.search = ''
  87.         self.print_test_page = False
  88.         self.device_desc = DEVICE_DESC_ALL
  89.         if self.param:
  90.             log.info('Searching for device...')
  91.             self.manual = True
  92.             self.advanced = True
  93.             self.ManualParamLineEdit.setText(self.param)
  94.             self.JetDirectSpinBox.setValue(self.jd_port)
  95.             self.ManualGroupBox.setChecked(True)
  96.             self.DiscoveryOptionsGroupBox.setEnabled(False)
  97.             if self.manualDiscovery():
  98.                 self.skip_discovery = True
  99.             else:
  100.                 FailureUI(self, self._SetupDialog__tr('<b>Device not found.</b> <p>Please make sure your printer is properly connected and powered-on.'))
  101.                 match = device.usb_pat.match(self.param)
  102.                 if match is not None:
  103.                     self.UsbRadioButton.setChecked(True)
  104.                     self.setUsbRadioButton(True)
  105.                 else:
  106.                     match = device.dev_pat.match(self.param)
  107.                     if match is not None and prop.par_build:
  108.                         self.ParallelRadioButton.setChecked(True)
  109.                         self.setParallelRadioButton(True)
  110.                     else:
  111.                         match = device.ip_pat.match(self.param)
  112.                         if match is not None and prop.net_build:
  113.                             self.NetworkRadioButton.setChecked(True)
  114.                             self.setNetworkRadioButton(True)
  115.                         else:
  116.                             FailureUI(self, self._SetupDialog__tr('<b>Invalid manual discovery parameter.</b>'))
  117.         elif self.device_uri:
  118.             
  119.             try:
  120.                 (back_end, is_hp, self.bus, model, serial, dev_file, host, port) = device.parseDeviceURI(self.device_uri)
  121.             except Error:
  122.                 log.error('Invalid device URI specified: %s' % self.device_uri)
  123.  
  124.             name = ''
  125.             if self.bus == 'net':
  126.                 
  127.                 try:
  128.                     log.debug('Trying to get hostname for device...')
  129.                     name = socket.gethostbyaddr(host)[0]
  130.                 except socket.herror:
  131.                     log.debug('Failed.')
  132.  
  133.                 log.debug('Host name=%s' % name)
  134.             
  135.             self.devices = {
  136.                 self.device_uri: (model, model, name) }
  137.             self.skip_discovery = True
  138.         elif not (prop.par_build) and not (prop.net_build):
  139.             self.skip_discovery = True
  140.             self.bus = 'usb'
  141.             self.UsbRadioButton.setChecked(True)
  142.             self.setUsbRadioButton(True)
  143.         
  144.         self.DeviceTypeComboBox.addItem('All devices/printers', QVariant(DEVICE_DESC_ALL))
  145.         self.DeviceTypeComboBox.addItem('Single function printers only', QVariant(DEVICE_DESC_SINGLE_FUNC))
  146.         self.DeviceTypeComboBox.addItem('All-in-one/MFP devices only', QVariant(DEVICE_DESC_MULTI_FUNC))
  147.         self.connect(self.AdvancedButton, SIGNAL('clicked()'), self.AdvancedButton_clicked)
  148.         self.connect(self.UsbRadioButton, SIGNAL('toggled(bool)'), self.UsbRadioButton_toggled)
  149.         self.connect(self.NetworkRadioButton, SIGNAL('toggled(bool)'), self.NetworkRadioButton_toggled)
  150.         self.connect(self.ParallelRadioButton, SIGNAL('toggled(bool)'), self.ParallelRadioButton_toggled)
  151.         self.connect(self.NetworkTTLSpinBox, SIGNAL('valueChanged(int)'), self.NetworkTTLSpinBox_valueChanged)
  152.         self.connect(self.NetworkTimeoutSpinBox, SIGNAL('valueChanged(int)'), self.NetworkTimeoutSpinBox_valueChanged)
  153.         self.connect(self.ManualGroupBox, SIGNAL('toggled(bool)'), self.ManualGroupBox_toggled)
  154.         self.showAdvanced()
  155.  
  156.     
  157.     def ManualGroupBox_toggled(self, checked):
  158.         self.DiscoveryOptionsGroupBox.setEnabled(not checked)
  159.  
  160.     
  161.     def manualDiscovery(self):
  162.         ret = False
  163.         (device_uri, sane_uri, fax_uri) = device.makeURI(self.param, self.jd_port)
  164.         if device_uri:
  165.             (back_end, is_hp, bus, model, serial, dev_file, host, port) = device.parseDeviceURI(device_uri)
  166.             name = ''
  167.             if bus == 'net':
  168.                 
  169.                 try:
  170.                     name = socket.gethostbyaddr(host)[0]
  171.                 except socket.herror:
  172.                     pass
  173.                 except:
  174.                     None<EXCEPTION MATCH>socket.herror
  175.                 
  176.  
  177.             None<EXCEPTION MATCH>socket.herror
  178.             self.devices = {
  179.                 device_uri: (model, model, name) }
  180.             if bus == 'usb':
  181.                 self.UsbRadioButton.setChecked(True)
  182.                 self.setUsbRadioButton(True)
  183.             elif bus == 'net' and prop.net_build:
  184.                 self.NetworkRadioButton.setChecked(True)
  185.                 self.setNetworkRadioButton(True)
  186.             elif bus == 'par' and prop.par_build:
  187.                 self.ParallelRadioButton.setChecked(True)
  188.                 self.setParallelRadioButton(True)
  189.             
  190.             ret = True
  191.         
  192.         return ret
  193.  
  194.     
  195.     def ManualGroupBox_clicked(self, checked):
  196.         self.manual = checked
  197.         network = self.NetworkRadioButton.isChecked()
  198.         self.setJetDirect(network)
  199.  
  200.     
  201.     def showDiscoveryPage(self):
  202.         self.BackButton.setEnabled(False)
  203.         self.NextButton.setEnabled(True)
  204.         self.setNextButton(BUTTON_NEXT)
  205.         self.displayPage(PAGE_DISCOVERY)
  206.  
  207.     
  208.     def AdvancedButton_clicked(self):
  209.         self.advanced = not (self.advanced)
  210.         self.showAdvanced()
  211.  
  212.     
  213.     def showAdvanced(self):
  214.         if self.advanced:
  215.             self.AdvancedStackedWidget.setCurrentIndex(ADVANCED_SHOW)
  216.             self.AdvancedButton.setText(self._SetupDialog__tr('Hide Advanced Options'))
  217.             self.AdvancedButton.setIcon(QIcon(load_pixmap('minus', '16x16')))
  218.         else:
  219.             self.AdvancedStackedWidget.setCurrentIndex(ADVANCED_HIDE)
  220.             self.AdvancedButton.setText(self._SetupDialog__tr('Show Advanced Options'))
  221.             self.AdvancedButton.setIcon(QIcon(load_pixmap('plus', '16x16')))
  222.  
  223.     
  224.     def setJetDirect(self, enabled):
  225.         if enabled:
  226.             pass
  227.         self.JetDirectLabel.setEnabled(self.manual)
  228.         if enabled:
  229.             pass
  230.         self.JetDirectSpinBox.setEnabled(self.manual)
  231.  
  232.     
  233.     def setNetworkOptions(self, enabled):
  234.         self.NetworkTimeoutLabel.setEnabled(enabled)
  235.         self.NetworkTimeoutSpinBox.setEnabled(enabled)
  236.         self.NetworkTTLLabel.setEnabled(enabled)
  237.         self.NetworkTTLSpinBox.setEnabled(enabled)
  238.  
  239.     
  240.     def setNetworkDiscovery(self, enabled):
  241.         if enabled:
  242.             pass
  243.         self.NetworkDiscoveryMethodLabel.setEnabled(self.manual)
  244.         if enabled:
  245.             pass
  246.         self.NetworkDiscoveryMethodComboBox.setEnabled(self.manual)
  247.  
  248.     
  249.     def UsbRadioButton_toggled(self, radio_enabled):
  250.         self.setUsbRadioButton(radio_enabled)
  251.  
  252.     
  253.     def setUsbRadioButton(self, checked):
  254.         self.setNetworkDiscovery(not checked)
  255.         self.setJetDirect(not checked)
  256.         self.setNetworkOptions(not checked)
  257.         if checked:
  258.             self.ManualParamLabel.setText(self._SetupDialog__tr('USB bus ID:device ID (bbb:ddd):'))
  259.             self.bus = 'usb'
  260.         
  261.  
  262.     
  263.     def NetworkRadioButton_toggled(self, radio_enabled):
  264.         self.setNetworkRadioButton(radio_enabled)
  265.  
  266.     
  267.     def setNetworkRadioButton(self, checked):
  268.         self.setNetworkDiscovery(checked)
  269.         self.setJetDirect(checked)
  270.         self.setNetworkOptions(checked)
  271.         if checked:
  272.             self.ManualParamLabel.setText(self._SetupDialog__tr('IP Address or network name:'))
  273.             self.bus = 'net'
  274.         
  275.  
  276.     
  277.     def ParallelRadioButton_toggled(self, radio_enabled):
  278.         self.setParallelRadioButton(radio_enabled)
  279.  
  280.     
  281.     def setParallelRadioButton(self, checked):
  282.         self.setNetworkDiscovery(not checked)
  283.         self.setJetDirect(not checked)
  284.         self.setNetworkOptions(not checked)
  285.         if checked:
  286.             self.ManualParamLabel.setText(self._SetupDialog__tr('Device node (/dev/...):'))
  287.             self.bus = 'par'
  288.         
  289.  
  290.     
  291.     def NetworkTTLSpinBox_valueChanged(self, ttl):
  292.         self.ttl = ttl
  293.  
  294.     
  295.     def NetworkTimeoutSpinBox_valueChanged(self, timeout):
  296.         self.timeout = timeout
  297.  
  298.     
  299.     def initDevicesPage(self):
  300.         self.connect(self.RefreshButton, SIGNAL('clicked()'), self.RefreshButton_clicked)
  301.  
  302.     
  303.     def showDevicesPage(self):
  304.         self.BackButton.setEnabled(True)
  305.         self.setNextButton(BUTTON_NEXT)
  306.         QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  307.         
  308.         try:
  309.             if not self.devices:
  310.                 if self.manual and self.param:
  311.                     self.manualDiscovery()
  312.                 elif self.bus == 'net':
  313.                     if not self.search:
  314.                         pass
  315.                     log.info('Searching... (bus=%s, timeout=%d, ttl=%d, search=%s desc=%d)' % (self.bus, self.timeout, self.ttl, '(None)', self.device_desc))
  316.                 elif not self.search:
  317.                     pass
  318.                 log.info('Searching... (bus=%s, search=%s desc=%d)' % (self.bus, '(None)', self.device_desc))
  319.                 if self.device_desc == DEVICE_DESC_SINGLE_FUNC:
  320.                     filter_dict = {
  321.                         'scan-type': (operator.le, SCAN_TYPE_NONE) }
  322.                 elif self.device_desc == DEVICE_DESC_MULTI_FUNC:
  323.                     filter_dict = {
  324.                         'scan-type': (operator.gt, SCAN_TYPE_NONE) }
  325.                 else:
  326.                     filter_dict = { }
  327.                 self.devices = device.probeDevices([
  328.                     self.bus], self.timeout, self.ttl, filter_dict, self.search)
  329.         finally:
  330.             QApplication.restoreOverrideCursor()
  331.  
  332.         self.clearDevicesTable()
  333.         if self.devices:
  334.             self.NextButton.setEnabled(True)
  335.             self.DevicesFoundIcon.setPixmap(load_pixmap('info', '16x16'))
  336.             if len(self.devices) == 1:
  337.                 self.DevicesFoundLabel.setText(self._SetupDialog__tr('<b>1 device found.</b> Click <i>Next</i> to continue.'))
  338.             else:
  339.                 self.DevicesFoundLabel.setText(self._SetupDialog__tr('<b>%1 devices found.</b> Select the device to install and click <i>Next</i> to continue.').arg(len(self.devices)))
  340.             self.loadDevicesTable()
  341.         else:
  342.             self.NextButton.setEnabled(False)
  343.             self.DevicesFoundIcon.setPixmap(load_pixmap('error', '16x16'))
  344.             log.error('No devices found on bus: %s' % self.bus)
  345.             self.DevicesFoundLabel.setText(self._SetupDialog__tr('<b>No devices found.</b><br>Click <i>Back</i> to change discovery options, or <i>Refresh</i> to search again.'))
  346.         self.displayPage(PAGE_DEVICES)
  347.  
  348.     
  349.     def loadDevicesTable(self):
  350.         self.DevicesTableWidget.setRowCount(len(self.devices))
  351.         if self.bus == 'net':
  352.             headers = [
  353.                 self._SetupDialog__tr('Model'),
  354.                 self._SetupDialog__tr('IP Address'),
  355.                 self._SetupDialog__tr('Host Name'),
  356.                 self._SetupDialog__tr('Device URI')]
  357.             device_uri_col = 3
  358.         else:
  359.             headers = [
  360.                 self._SetupDialog__tr('Model'),
  361.                 self._SetupDialog__tr('Device URI')]
  362.             device_uri_col = 1
  363.         self.DevicesTableWidget.setColumnCount(len(headers))
  364.         self.DevicesTableWidget.setHorizontalHeaderLabels(headers)
  365.         flags = Qt.ItemIsSelectable | Qt.ItemIsEnabled
  366.         for row, d in enumerate(self.devices):
  367.             (back_end, is_hp, bus, model, serial, dev_file, host, port) = device.parseDeviceURI(d)
  368.             model_ui = models.normalizeModelUIName(model)
  369.             i = DeviceTableWidgetItem(QString(model_ui), d)
  370.             i.setFlags(flags)
  371.             self.DevicesTableWidget.setItem(row, 0, i)
  372.             i = QTableWidgetItem(QString(d))
  373.             i.setFlags(flags)
  374.             self.DevicesTableWidget.setItem(row, device_uri_col, i)
  375.             if self.bus == 'net':
  376.                 i = QTableWidgetItem(QString(host))
  377.                 i.setFlags(flags)
  378.                 self.DevicesTableWidget.setItem(row, 1, i)
  379.                 i = QTableWidgetItem(QString(self.devices[d][2]))
  380.                 i.setFlags(flags)
  381.                 self.DevicesTableWidget.setItem(row, 2, i)
  382.                 continue
  383.         
  384.         self.DevicesTableWidget.resizeColumnsToContents()
  385.         self.DevicesTableWidget.selectRow(0)
  386.         self.DevicesTableWidget.setSortingEnabled(True)
  387.         self.DevicesTableWidget.sortItems(0)
  388.  
  389.     
  390.     def clearDevicesTable(self):
  391.         self.DevicesTableWidget.clear()
  392.         self.DevicesTableWidget.setRowCount(0)
  393.         self.DevicesTableWidget.setColumnCount(0)
  394.  
  395.     
  396.     def RefreshButton_clicked(self):
  397.         self.clearDevicesTable()
  398.         self.devices = []
  399.         QTimer.singleShot(0, self.showDevicesPage)
  400.  
  401.     
  402.     def initAddPrinterPage(self):
  403.         self.mq = { }
  404.         self.connect(self.PrinterNameLineEdit, SIGNAL('textEdited(const QString &)'), self.PrinterNameLineEdit_textEdited)
  405.         self.connect(self.FaxNameLineEdit, SIGNAL('textEdited(const QString &)'), self.FaxNameLineEdit_textEdited)
  406.         self.PrinterNameLineEdit.setValidator(PrinterNameValidator(self.PrinterNameLineEdit))
  407.         self.FaxNameLineEdit.setValidator(PrinterNameValidator(self.FaxNameLineEdit))
  408.         self.FaxNumberLineEdit.setValidator(PhoneNumValidator(self.FaxNumberLineEdit))
  409.         self.OtherPPDButton.setIcon(QIcon(load_pixmap('folder_open', '16x16')))
  410.         self.connect(self.OtherPPDButton, SIGNAL('clicked(bool)'), self.OtherPPDButton_clicked)
  411.         self.OtherPPDButton.setToolTip(self._SetupDialog__tr('Browse for an alternative PPD file for this printer.'))
  412.         self.printer_fax_names_same = False
  413.         self.printer_name = ''
  414.         self.fax_name = ''
  415.         self.fax_setup_ok = True
  416.         self.fax_setup = False
  417.  
  418.     
  419.     def showAddPrinterPage(self):
  420.         plugin = self.mq.get('plugin', PLUGIN_NONE)
  421.         if plugin > PLUGIN_NONE:
  422.             form = PluginDialog(self, plugin)
  423.             if not form.isPluginInstalled():
  424.                 form.exec_()
  425.                 if not (form.result) and plugin == PLUGIN_REQUIRED:
  426.                     FailureUI(self, self._SetupDialog__tr('<b>The printer you are trying to setup requires a binary driver plug-in and it failed to install.</b><p>Please check you internet connection and try again.</p></p>Visit <u>http://hplipopensource.com</u> for more infomation.</p>'))
  427.                     return None
  428.             
  429.         
  430.         self.setNextButton(BUTTON_ADD_PRINTER)
  431.         if not self.printer_name:
  432.             self.setDefaultPrinterName()
  433.         
  434.         self.findPrinterPPD()
  435.         if fax_import_ok and prop.fax_build and self.mq.get('fax-type', FAX_TYPE_NONE) not in (FAX_TYPE_NONE, FAX_TYPE_NOT_SUPPORTED):
  436.             self.fax_setup = True
  437.             self.SetupFaxGroupBox.setChecked(True)
  438.             self.SetupFaxGroupBox.setEnabled(True)
  439.             if not self.fax_name:
  440.                 self.setDefaultFaxName()
  441.             
  442.             self.findFaxPPD()
  443.             self.readwriteFaxInformation()
  444.         else:
  445.             self.SetupFaxGroupBox.setChecked(False)
  446.             self.SetupFaxGroupBox.setEnabled(False)
  447.             self.fax_name = ''
  448.             self.fax_name_ok = True
  449.             self.fax_setup = False
  450.             self.fax_setup_ok = True
  451.         self.updatePPD()
  452.         self.setAddPrinterButton()
  453.         self.displayPage(PAGE_ADD_PRINTER)
  454.  
  455.     
  456.     def updatePPD(self):
  457.         if self.print_ppd is None:
  458.             log.error('No appropriate print PPD file found for model %s' % self.model)
  459.             self.PPDFileLineEdit.setText(self._SetupDialog__tr('(Not found. Click browse button to select a PPD file.)'))
  460.             
  461.             try:
  462.                 self.PPDFileLineEdit.setStyleSheet('background-color: yellow')
  463.             except AttributeError:
  464.                 pass
  465.  
  466.             self.PrinterDescriptionLineEdit.setText(QString(''))
  467.         else:
  468.             self.PPDFileLineEdit.setText(self.print_ppd[0])
  469.             self.PrinterDescriptionLineEdit.setText(self.print_ppd[1])
  470.             
  471.             try:
  472.                 self.PPDFileLineEdit.setStyleSheet('')
  473.             except AttributeError:
  474.                 pass
  475.  
  476.  
  477.     
  478.     def OtherPPDButton_clicked(self, b):
  479.         ppd_file = unicode(QFileDialog.getOpenFileName(self, self._SetupDialog__tr('Select PPD File'), sys_conf.get('dirs', 'ppd'), self._SetupDialog__tr('PPD Files (*.ppd *.ppd.gz);;All Files (*)')))
  480.         if ppd_file and os.path.exists(ppd_file):
  481.             self.print_ppd = (ppd_file, cups.getPPDDescription(ppd_file))
  482.             self.updatePPD()
  483.             self.setAddPrinterButton()
  484.         
  485.  
  486.     
  487.     def findPrinterPPD(self):
  488.         QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  489.         
  490.         try:
  491.             self.print_ppd = None
  492.             self.ppds = cups.getSystemPPDs()
  493.             model = cups.stripModel2(self.model)
  494.             self.print_ppd = cups.getPPDFile2(model, self.ppds)
  495.         finally:
  496.             QApplication.restoreOverrideCursor()
  497.  
  498.  
  499.     
  500.     def findFaxPPD(self):
  501.         QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  502.         
  503.         try:
  504.             log.debug('Searching for fax PPD for model %s' % self.model)
  505.             if self.mq.get('fax-type', FAX_TYPE_NONE) == FAX_TYPE_SOAP:
  506.                 fax_ppd_name = 'HP-Fax2-hplip'
  507.                 nick = 'HP Fax 2'
  508.             else:
  509.                 fax_ppd_name = 'HP-Fax-hplip'
  510.                 nick = 'HP Fax'
  511.             ppds = []
  512.             for f in utils.walkFiles(sys_conf.get('dirs', 'ppd'), pattern = 'HP-Fax*.ppd*', abs_paths = True):
  513.                 ppds.append(f)
  514.             
  515.             for f in ppds:
  516.                 if f.find(fax_ppd_name) >= 0 and cups.getPPDDescription(f) == nick:
  517.                     self.fax_ppd = f
  518.                     self.fax_setup_ok = True
  519.                     print 'Found fax PPD:', f
  520.                     break
  521.                     continue
  522.             else:
  523.                 self.fax_ppd = None
  524.                 self.fax_setup_ok = False
  525.         finally:
  526.             QApplication.restoreOverrideCursor()
  527.  
  528.  
  529.     
  530.     def setDefaultPrinterName(self):
  531.         self.installed_print_devices = device.getSupportedCUPSDevices([
  532.             'hp'])
  533.         log.debug(self.installed_print_devices)
  534.         self.installed_queues = [ p.name for p in cups.getPrinters() ]
  535.         (back_end, is_hp, bus, model, serial, dev_file, host, port) = device.parseDeviceURI(self.device_uri)
  536.         default_model = utils.xstrip(model.replace('series', '').replace('Series', ''), '_')
  537.         printer_name = default_model
  538.         self.printer_name_ok = True
  539.         self.PrinterNameLineEdit.setText(printer_name)
  540.         log.debug(printer_name)
  541.         self.printer_name = printer_name
  542.  
  543.     
  544.     def setDefaultFaxName(self):
  545.         self.installed_fax_devices = device.getSupportedCUPSDevices([
  546.             'hpfax'])
  547.         log.debug(self.installed_fax_devices)
  548.         self.fax_uri = self.device_uri.replace('hp:', 'hpfax:')
  549.         (back_end, is_hp, bus, model, serial, dev_file, host, port) = device.parseDeviceURI(self.fax_uri)
  550.         default_model = utils.xstrip(model.replace('series', '').replace('Series', ''), '_')
  551.         fax_name = default_model + '_fax'
  552.         if self.fax_uri in self.installed_fax_devices and fax_name in self.installed_fax_devices[self.fax_uri]:
  553.             i = 2
  554.             while True:
  555.                 t = fax_name + '_%d' % i
  556.                 if t not in self.installed_fax_devices[self.fax_uri]:
  557.                     fax_name += '_%d' % i
  558.                     break
  559.                 
  560.                 i += 1
  561.         
  562.         self.fax_name_ok = True
  563.         self.FaxNameLineEdit.setText(fax_name)
  564.         self.fax_name = fax_name
  565.  
  566.     
  567.     def PrinterNameLineEdit_textEdited(self, t):
  568.         self.printer_name = unicode(t)
  569.         self.printer_name_ok = True
  570.         if not self.printer_name:
  571.             self.PrinterNameLineEdit.setToolTip(self._SetupDialog__tr('You must enter a name for the printer.'))
  572.             self.printer_name_ok = False
  573.         elif self.fax_name == self.printer_name:
  574.             s = self._SetupDialog__tr('The printer name and fax name must be different. Please choose different names.')
  575.             self.PrinterNameLineEdit.setToolTip(s)
  576.             self.FaxNameLineEdit.setToolTip(s)
  577.             self.fax_name_ok = False
  578.             self.printer_name_ok = False
  579.             self.printer_fax_names_same = True
  580.         elif self.printer_name in self.installed_queues:
  581.             self.PrinterNameLineEdit.setToolTip(self._SetupDialog__tr('A printer already exists with this name. Please choose a different name.'))
  582.             self.printer_name_ok = False
  583.         elif self.printer_fax_names_same:
  584.             if self.fax_name != self.printer_name:
  585.                 self.printer_fax_names_same = False
  586.                 self.printer_name_ok = True
  587.                 self.FaxNameLineEdit.emit(SIGNAL('textChanged(const QString &)'), (self.FaxNameLineEdit.text(),))
  588.             
  589.         
  590.         self.setIndicators()
  591.         self.setAddPrinterButton()
  592.  
  593.     
  594.     def FaxNameLineEdit_textEdited(self, t):
  595.         self.fax_name = unicode(t)
  596.         self.fax_name_ok = True
  597.         if not self.fax_name:
  598.             self.FaxNameLineEdit.setToolTip(self._SetupDialog__tr('You must enter a fax name.'))
  599.             self.fax_name_ok = False
  600.         elif self.fax_name == self.printer_name:
  601.             s = self._SetupDialog__tr('The printer name and fax name must be different. Please choose different names.')
  602.             self.PrinterNameLineEdit.setToolTip(s)
  603.             self.FaxNameLineEdit.setToolTip(s)
  604.             self.printer_name_ok = False
  605.             self.fax_name_ok = False
  606.             self.printer_fax_names_same = True
  607.         elif self.fax_name in self.installed_queues:
  608.             self.FaxNameLineEdit.setToolTip(self._SetupDialog__tr('A fax already exists with this name. Please choose a different name.'))
  609.             self.fax_name_ok = False
  610.         elif self.printer_fax_names_same:
  611.             if self.fax_name != self.printer_name:
  612.                 self.printer_fax_names_same = False
  613.                 self.fax_name_ok = True
  614.                 self.PrinterNameLineEdit.emit(SIGNAL('textChanged(const QString&)'), (self.PrinterNameLineEdit.text(),))
  615.             
  616.         
  617.         self.setIndicators()
  618.         self.setAddPrinterButton()
  619.  
  620.     
  621.     def setIndicators(self):
  622.         if self.printer_name_ok:
  623.             self.PrinterNameLineEdit.setToolTip(QString(''))
  624.             
  625.             try:
  626.                 self.PrinterNameLineEdit.setStyleSheet('')
  627.             except AttributeError:
  628.                 pass
  629.             except:
  630.                 None<EXCEPTION MATCH>AttributeError
  631.             
  632.  
  633.         None<EXCEPTION MATCH>AttributeError
  634.         
  635.         try:
  636.             self.PrinterNameLineEdit.setStyleSheet('background-color: yellow')
  637.         except AttributeError:
  638.             pass
  639.  
  640.         if self.fax_name_ok:
  641.             self.FaxNameLineEdit.setToolTip(QString(''))
  642.             
  643.             try:
  644.                 self.PrinterNameLineEdit.setStyleSheet('')
  645.             except AttributeError:
  646.                 pass
  647.             except:
  648.                 None<EXCEPTION MATCH>AttributeError
  649.             
  650.  
  651.         None<EXCEPTION MATCH>AttributeError
  652.         
  653.         try:
  654.             self.PrinterNameLineEdit.setStyleSheet('background-color: yellow')
  655.         except AttributeError:
  656.             pass
  657.  
  658.  
  659.     
  660.     def setAddPrinterButton(self):
  661.         if self.printer_name_ok and self.fax_name_ok and self.print_ppd is not None:
  662.             pass
  663.         self.NextButton.setEnabled(self.fax_setup_ok)
  664.  
  665.     
  666.     def addPrinter(self):
  667.         self.setupPrinter()
  668.         if self.fax_setup:
  669.             self.setupFax()
  670.             self.readwriteFaxInformation(False)
  671.         
  672.         if self.print_test_page:
  673.             self.printTestPage()
  674.         
  675.         self.close()
  676.  
  677.     
  678.     def setupPrinter(self):
  679.         QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  680.         
  681.         try:
  682.             if not os.path.exists(self.print_ppd[0]):
  683.                 (status, status_str) = cups.addPrinter(self.printer_name.encode('utf8'), self.device_uri, self.print_location, '', self.print_ppd[0], self.print_desc)
  684.             else:
  685.                 (status, status_str) = cups.addPrinter(self.printer_name.encode('utf8'), self.device_uri, self.print_location, self.print_ppd[0], '', self.print_desc)
  686.             log.debug('addPrinter() returned (%d, %s)' % (status, status_str))
  687.             self.installed_print_devices = device.getSupportedCUPSDevices([
  688.                 'hp'])
  689.             log.debug(self.installed_print_devices)
  690.             if self.device_uri not in self.installed_print_devices or self.printer_name not in self.installed_print_devices[self.device_uri]:
  691.                 QApplication.restoreOverrideCursor()
  692.                 FailureUI(self, self._SetupDialog__tr('<b>Printer queue setup failed.</b><p>Please restart CUPS and try again.'))
  693.         finally:
  694.             QApplication.restoreOverrideCursor()
  695.  
  696.  
  697.     
  698.     def setupFax(self):
  699.         QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  700.         
  701.         try:
  702.             if not os.path.exists(self.fax_ppd):
  703.                 (status, status_str) = cups.addPrinter(self.fax_name.encode('utf8'), self.fax_uri, self.fax_location, '', self.fax_ppd, self.fax_desc)
  704.             else:
  705.                 (status, status_str) = cups.addPrinter(self.fax_name.encode('utf8'), self.fax_uri, self.fax_location, self.fax_ppd, '', self.fax_desc)
  706.             log.debug('addPrinter() returned (%d, %s)' % (status, status_str))
  707.             self.installed_fax_devices = device.getSupportedCUPSDevices([
  708.                 'hpfax'])
  709.             log.debug(self.installed_fax_devices)
  710.             if self.fax_uri not in self.installed_fax_devices or self.fax_name not in self.installed_fax_devices[self.fax_uri]:
  711.                 QApplication.restoreOverrideCursor()
  712.                 FailureUI(self, self._SetupDialog__tr('<b>Fax queue setup failed.</b><p>Please restart CUPS and try again.'))
  713.         finally:
  714.             QApplication.restoreOverrideCursor()
  715.  
  716.  
  717.     
  718.     def readwriteFaxInformation(self, read = True):
  719.         
  720.         try:
  721.             QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  722.             d = fax.getFaxDevice(self.fax_uri, disable_dbus = True)
  723.             while True:
  724.                 
  725.                 try:
  726.                     d.open()
  727.                 except Error:
  728.                     error_text = self._SetupDialog__tr('Unable to communicate with the device. Please check the device and try again.')
  729.                     log.error(unicode(error_text))
  730.                     if QMessageBox.critical(self, self.windowTitle(), error_text, QMessageBox.Retry | QMessageBox.Default, QMessageBox.Cancel | QMessageBox.Escape, QMessageBox.NoButton) == QMessageBox.Cancel:
  731.                         break
  732.                     
  733.                     QMessageBox.critical(self, self.windowTitle(), error_text, QMessageBox.Retry | QMessageBox.Default, QMessageBox.Cancel | QMessageBox.Escape, QMessageBox.NoButton) == QMessageBox.Cancel
  734.  
  735.                 
  736.                 try:
  737.                     tries = 0
  738.                     ok = True
  739.                     while True:
  740.                         tries += 1
  741.                         
  742.                         try:
  743.                             if read:
  744.                                 self.fax_number = unicode(d.getPhoneNum())
  745.                                 self.fax_name_company = unicode(d.getStationName())
  746.                             else:
  747.                                 d.setStationName(self.fax_name_company)
  748.                                 d.setPhoneNum(self.fax_number)
  749.                         except Error:
  750.                             error_text = self._SetupDialog__tr('<b>Device I/O Error</b><p>Could not communicate with device. Device may be busy.')
  751.                             log.error(unicode(error_text))
  752.                             if QMessageBox.critical(self, self.windowTitle(), error_text, QMessageBox.Retry | QMessageBox.Default, QMessageBox.Cancel | QMessageBox.Escape, QMessageBox.NoButton) == QMessageBox.Cancel:
  753.                                 break
  754.                             
  755.                             time.sleep(5)
  756.                             ok = False
  757.                             if tries > 12:
  758.                                 break
  759.                             
  760.                             tries > 12
  761.  
  762.                         ok = True
  763.                         break
  764.                 finally:
  765.                     d.close()
  766.  
  767.                 if ok and read:
  768.                     self.FaxNumberLineEdit.setText(self.fax_number)
  769.                     self.NameCompanyLineEdit.setText(self.fax_name_company)
  770.                 
  771.                 break
  772.         finally:
  773.             QApplication.restoreOverrideCursor()
  774.  
  775.  
  776.     
  777.     def printTestPage(self):
  778.         
  779.         try:
  780.             d = device.Device(self.device_uri)
  781.         except Error:
  782.             e = None
  783.             FailureUI(self, self._SetupDialog__tr('<b>Device error:</b><p>%s (%s).' % (e.msg, e.opt)))
  784.  
  785.         
  786.         try:
  787.             d.open()
  788.         except Error:
  789.             FailureUI(self, self._SetupDialog__tr('<b>Unable to print to printer.</b><p>Please check device and try again.'))
  790.  
  791.         if d.isIdleAndNoError():
  792.             d.close()
  793.             
  794.             try:
  795.                 d.printTestPage(self.printer_name)
  796.             except Error:
  797.                 e = None
  798.                 if e.opt == ERROR_NO_CUPS_QUEUE_FOUND_FOR_DEVICE:
  799.                     FailureUI(self, self._SetupDialog__tr('<b>No CUPS queue found for device.</b><p>Please install the printer in CUPS and try again.'))
  800.                 else:
  801.                     FailureUI(self, self._SetupDialog__tr('<b>Printer Error</b><p>An error occured: %s (code=%d).' % (e.msg, e.opt)))
  802.             except:
  803.                 e.opt == ERROR_NO_CUPS_QUEUE_FOUND_FOR_DEVICE
  804.             
  805.  
  806.         None<EXCEPTION MATCH>Error
  807.         FailureUI(self, self._SetupDialog__tr('<b>Printer Error.</b><p>Printer is busy, offline, or in an error state. Please check the device and try again.'))
  808.         d.close()
  809.  
  810.     
  811.     def NextButton_clicked(self):
  812.         p = self.StackedWidget.currentIndex()
  813.         if p == PAGE_DISCOVERY:
  814.             self.manual = self.ManualGroupBox.isChecked()
  815.             self.param = unicode(self.ManualParamLineEdit.text())
  816.             self.jd_port = self.JetDirectSpinBox.value()
  817.             self.search = unicode(self.SearchLineEdit.text())
  818.             self.device_desc = int(self.DeviceTypeComboBox.itemData(self.DeviceTypeComboBox.currentIndex()).toInt()[0])
  819.             self.showDevicesPage()
  820.         elif p == PAGE_DEVICES:
  821.             row = self.DevicesTableWidget.currentRow()
  822.             self.device_uri = self.DevicesTableWidget.item(row, 0).device_uri
  823.             self.mq = device.queryModelByURI(self.device_uri)
  824.             (back_end, is_hp, bus, model, serial, dev_file, host, port) = device.parseDeviceURI(self.device_uri)
  825.             self.model = models.normalizeModelName(model).lower()
  826.             self.showAddPrinterPage()
  827.         elif p == PAGE_ADD_PRINTER:
  828.             self.print_test_page = self.SendTestPageCheckBox.isChecked()
  829.             self.print_desc = unicode(self.PrinterDescriptionLineEdit.text()).encode('utf8')
  830.             self.print_location = unicode(self.PrinterLocationLineEdit.text()).encode('utf8')
  831.             self.fax_setup = self.SetupFaxGroupBox.isChecked()
  832.             self.fax_desc = unicode(self.FaxDescriptionLineEdit.text()).encode('utf8')
  833.             self.fax_location = unicode(self.FaxLocationLineEdit.text()).encode('utf8')
  834.             self.fax_name_company = unicode(self.NameCompanyLineEdit.text()).encode('utf8')
  835.             self.fax_number = unicode(self.FaxNumberLineEdit.text()).encode('utf8')
  836.             self.addPrinter()
  837.         else:
  838.             log.error('Invalid page!')
  839.  
  840.     
  841.     def BackButton_clicked(self):
  842.         p = self.StackedWidget.currentIndex()
  843.         if p == PAGE_DEVICES:
  844.             self.devices = { }
  845.             self.showDiscoveryPage()
  846.         elif p == PAGE_ADD_PRINTER:
  847.             self.showDevicesPage()
  848.         else:
  849.             log.error('Invalid page!')
  850.  
  851.     
  852.     def CancelButton_clicked(self):
  853.         self.close()
  854.  
  855.     
  856.     def displayPage(self, page):
  857.         self.updateStepText(page)
  858.         self.StackedWidget.setCurrentIndex(page)
  859.  
  860.     
  861.     def setNextButton(self, typ = BUTTON_FINISH):
  862.         if typ == BUTTON_ADD_PRINTER:
  863.             self.NextButton.setText(self._SetupDialog__tr('Add Printer'))
  864.         elif typ == BUTTON_NEXT:
  865.             self.NextButton.setText(self._SetupDialog__tr('Next >'))
  866.         elif typ == BUTTON_FINISH:
  867.             self.NextButton.setText(self._SetupDialog__tr('Finish'))
  868.         
  869.  
  870.     
  871.     def updateStepText(self, p):
  872.         self.StepText.setText(self._SetupDialog__tr('Step %1 of %2').arg(p + 1).arg(PAGE_MAX + 1))
  873.  
  874.     
  875.     def __tr(self, s, c = None):
  876.         return qApp.translate('SetupDialog', s, c)
  877.  
  878.  
  879.