1 # -*- coding: utf-8 -*-
3 # (c) Copyright 2001-2009 Hewlett-Packard Development Company, L.P.
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.
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.
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
25 from base.codes import *
26 from base import utils, device
28 from ui_utils import load_pixmap
32 from scrollprint import ScrollPrintView
36 class PrinterForm(QMainWindow):
37 def __init__(self, printer_name=None, args=None,
38 parent=None,name=None,modal=0,fl=0):
40 QMainWindow.__init__(self,parent,name,fl)
42 self.printer_name = printer_name
45 self.init_failed = False
49 self.setIcon(load_pixmap('hp_logo', '128x128'))
52 self.setName("PrinterForm")
54 self.setCentralWidget(QWidget(self,"qt_central_widget"))
55 self.FormLayout = QGridLayout(self.centralWidget(),1,1,11,6,"FormLayout")
56 self.resize(QSize(600,480).expandedTo(self.minimumSizeHint()))
57 self.clearWState(Qt.WState_Polished)
60 self.cups_printers = device.getSupportedCUPSPrinters()
61 log.debug(self.cups_printers)
63 if not self.printer_name: # no -p provided
64 t = device.probeDevices(['cups'])
68 if d.startswith('hp:'):
69 probed_devices.append(d)
71 log.debug(probed_devices)
73 max_deviceid_size, x, devices = 0, 0, {}
75 for d in probed_devices:
77 for p in self.cups_printers:
79 printers.append(p.name)
80 devices[x] = (d, printers)
82 max_deviceid_size = max(len(d), max_deviceid_size)
85 from nodevicesform import NoDevicesForm
86 self.FailureUI(self.__tr("<p><b>No devices found.</b><p>Please make sure your device is properly installed and try again."))
87 self.init_failed = True
90 log.info(log.bold("Using device: %s" % devices[0][0]))
91 self.device_uri = devices[0][0]
94 from chooseprinterdlg import ChoosePrinterDlg
95 dlg = ChoosePrinterDlg(self.cups_printers)
96 if dlg.exec_loop() == QDialog.Accepted:
97 self.printer_name = dlg.printer_name
98 self.device_uri = dlg.device_uri
100 self.init_failed = True
103 for p in self.cups_printers:
104 if p.name == self.printer_name:
105 self.device_uri = p.device_uri
108 self.FailureUI("<b>Invalid printer name.</b><p>Please check the parameters to hp-print and try again.")
109 self.init_failed = True
112 if not self.init_failed:
113 self.PrintView = ScrollPrintView(None, self.centralWidget(), self, "PrintView")
114 self.FormLayout.addWidget(self.PrintView,0,0)
117 self.cur_device = device.Device(device_uri=self.device_uri,
118 printer_name=self.printer_name)
120 log.error("Invalid device URI or printer name.")
121 self.FailureUI("<b>Invalid device URI or printer name.</b><p>Please check the parameters to hp-print and try again.")
122 self.init_failed = True
125 self.device_uri = self.cur_device.device_uri
126 user_conf.set('last_used', 'device_uri', self.device_uri)
128 log.debug(self.device_uri)
130 self.statusBar().message(self.device_uri)
132 QTimer.singleShot(0, self.InitialUpdate)
135 def InitialUpdate(self):
140 self.PrintView.onDeviceChange(self.cur_device)
142 if self.args is not None:
144 self.PrintView.addFile(f)
146 if self.printer_name is not None:
147 self.PrintView.onPrinterChange(self.printer_name)
150 def FailureUI(self, error_text):
151 log.error(unicode(error_text).replace("<b>", "").replace("</b>", "").replace("<p>", " "))
152 QMessageBox.critical(self,
156 QMessageBox.NoButton,
157 QMessageBox.NoButton)
160 def languageChange(self):
161 self.setCaption(self.__tr("HP Device Manager - Print"))
164 def __tr(self,s,c = None):
165 return qApp.translate("PrinterForm",s,c)