Debian Codegraph @

ui/faxsendjobform.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 # -*- coding: utf-8 -*-
2 #
3 # (c) Copyright 2003-2015 HP 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 # Author: Don Welch
20 #
21
22 # Std Lib
23 import operator
24
25 # Local
26 from base.g import *
27 from base.codes import *
28 from base import utils, device
29 from base.sixext import to_unicode
30 from prnt import cups
31 from .ui_utils import load_pixmap
32
33 if 1:
34 #try:
35 from fax import fax
36 #except ImportError:
37 if 0:
38 # This can fail on Python < 2.3 due to the datetime module
39 log.error("Fax send disabled - Python 2.3+ required.")
40 sys.exit(1)
41
42
43 # Qt/UI
44 from qt import *
45 from .scrollfax import ScrollFaxView
46
47 # dBus
48 dbus_avail = False
49 try:
50 import dbus
51 except ImportError:
52 dbus_avail = False
53
54
55
56 class FaxSendJobForm(QMainWindow):
57
58 def __init__(self, device_uri, printer_name, args,
59 parent=None, name=None,
60 modal=0, fl=0):
61
62 QMainWindow.__init__(self,parent,name,fl)
63
64 self.setIcon(load_pixmap('hp_logo', '128x128'))
65
66 self.init_failed = False
67 self.device_uri = device_uri
68 self.dev = None
69 self.printer_name = printer_name
70 bus = ['cups']
71 self.filename = ''
72 self.username = prop.username
73 self.args = args
74 self.setCentralWidget(QWidget(self,"qt_central_widget"))
75 self.FormLayout = QGridLayout(self.centralWidget(),1,1,11,6,"FormLayout")
76 self.resize(QSize(600,480).expandedTo(self.minimumSizeHint()))
77 self.clearWState(Qt.WState_Polished)
78 self.languageChange()
79
80 # if self.device_uri and self.printer_name:
81 # log.error("You may not specify both a printer (-p) and a device (-d).")
82 # self.FailureUI(self.__tr("<p><b>You may not specify both a printer (-p) and a device (-d)."))
83 # self.device_uri, self.printer_name = None, None
84 # self.init_failed = True
85
86 self.cups_printers = cups.getPrinters()
87 log.debug(self.cups_printers)
88
89 if self.printer_name:
90 found = False
91 for p in self.cups_printers:
92 if p.name == printer_name:
93 self.device_uri = p.device_uri
94 found = True
95 break
96
97 if not found:
98 self.FailureUI(self.__tr("<b>Unknown printer name: %1</b><p>Please check the printer name and try again.").arg(self.printer_name))
99
100 if found and not p.device_uri.startswith('hpfax:/'):
101 self.FailureUI(self.__tr("You must specify a printer that has a device URI in the form 'hpfax:/...'"))
102 self.init_failed = True
103
104 if not self.device_uri and not self.printer_name:
105 t = device.probeDevices(bus=bus, filter={'fax-type':(operator.gt, FAX_TYPE_NONE)})
106 #print t
107 probed_devices = []
108
109 for d in t:
110 probed_devices.append(d.replace('hp:/', 'hpfax:/'))
111
112 #print probed_devices
113
114 probed_devices = utils.uniqueList(probed_devices)
115 log.debug(probed_devices)
116
117 max_deviceid_size, x, devices = 0, 0, {}
118
119 for d in probed_devices:
120 printers = []
121 for p in self.cups_printers:
122 #print p.device_uri, d
123 if p.device_uri == d:
124 #print "OK"
125 printers.append(p.name)
126
127 devices[x] = (d, printers)
128 x += 1
129 max_deviceid_size = max(len(d), max_deviceid_size)
130
131 x = len(devices)
132
133 #print devices
134
135 if x == 0:
136 from .nodevicesform import NoDevicesForm
137 self.FailureUI(self.__tr("<p><b>No devices found.</b><p>Please make sure your device is properly installed and try again."))
138 self.init_failed = True
139
140 elif x == 1:
141 log.info(log.bold("Using device: %s" % devices[0][0]))
142 self.device_uri = devices[0][0]
143
144 else:
145 from .chooseprinterdlg import ChoosePrinterDlg
146 dlg = ChoosePrinterDlg(self.cups_printers, ['hpfax'])
147
148 if dlg.exec_loop() == QDialog.Accepted:
149 self.device_uri = dlg.device_uri
150 else:
151 self.init_failed = True
152
153 self.dbus_avail, self.service, session_bus = device.init_dbus()
154
155 self.FaxView = ScrollFaxView(self.service, self.centralWidget(), self)
156 self.FormLayout.addWidget(self.FaxView,0,0)
157
158 if not self.init_failed:
159 if not self.device_uri or not self.device_uri.startswith("hpfax:"):
160 log.error("Invalid device URI: %s" % repr(device_uri))
161 self.FailureUI(self.__tr("<b>Invalid device URI %1.</b><p>Please check the parameters to hp-print and try again.").arg(repr(device_uri)));
162 self.init_failed = True
163
164 else:
165 try:
166 self.cur_device = device.Device(device_uri=self.device_uri,
167 printer_name=self.printer_name)
168 except Error as e:
169 log.error("Invalid device URI or printer name.")
170 self.FailureUI("<b>Invalid device URI or printer name.</b><p>Please check the parameters to hp-print and try again.")
171 self.init_failed = True
172
173 else:
174 self.device_uri = self.cur_device.device_uri
175 user_conf.set('last_used', 'device_uri', self.device_uri)
176
177 log.debug(self.device_uri)
178
179 self.statusBar().message(self.device_uri)
180
181
182 QTimer.singleShot(0, self.InitialUpdate)
183
184
185 def InitialUpdate(self):
186 if self.init_failed:
187 self.close()
188 return
189
190 self.FaxView.onDeviceChange(self.cur_device)
191
192 if self.args is not None:
193 for f in self.args:
194 self.FaxView.processFile(f)
195
196 if self.printer_name is not None:
197 self.FaxView.onPrinterChange(self.printer_name)
198
199
200 def languageChange(self):
201 self.setCaption(self.__tr("HP Device Manager - Send Fax"))
202
203 def closeEvent(self, event):
204 #print "close"
205 #print self.FaxView.lock_file
206 utils.unlock(self.FaxView.lock_file)
207 event.accept()
208
209 def SuccessUI(self):
210 QMessageBox.information(self,
211 self.caption(),
212 self.__tr("<p><b>Fax send completed successfully.</b>"),
•213 QMessageBox.Ok |\
214 QMessageBox.NoButton,
215 QMessageBox.NoButton)
216
217 def FailureUI(self, error_text):
218 log.error(to_unicode(error_text).replace("<b>", "").replace("</b>", "").replace("<p>", " "))
219 QMessageBox.critical(self,
220 self.caption(),
221 error_text,
•222 QMessageBox.Ok |\
223 QMessageBox.NoButton,
224 QMessageBox.NoButton)
225
226 def WarningUI(self, error_text):
227 log.warn(to_unicode(error_text).replace("<b>", "").replace("</b>", "").replace("<p>", " "))
228 QMessageBox.warning(self,
229 self.caption(),
230 error_text,
•231 QMessageBox.Ok |\
232 QMessageBox.NoButton,
233 QMessageBox.NoButton)
234
235 def __tr(self,s,c = None):
236 return qApp.translate("FaxSendJobForm", s, c)