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