Tizen 2.1 base
[platform/upstream/hplip.git] / ui / unloadform.py
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 # Author: Don Welch
20 #
21
22 # Std Lib
23 import os, os.path
24 import operator
25
26 # Local
27 from base.g import *
28 from base import utils, device
29 from prnt import cups
30 from ui_utils import load_pixmap
31
32 # Qt
33 from qt import *
34 from scrollunload import ScrollUnloadView
35
36
37 class UnloadForm(QMainWindow):
38     def __init__(self, bus=['usb', 'par'], device_uri=None, printer_name=None,
39                  parent=None, name=None, fl=0):
40
41         QMainWindow.__init__(self,parent,name,fl)
42
43         self.pc = None
44         self.device_uri = device_uri
45         self.printer_name = printer_name
46         self.init_failed = False
47
48         self.setIcon(load_pixmap('hp_logo', '128x128'))
49
50         self.setCentralWidget(QWidget(self,"qt_central_widget"))
51         self.FormLayout = QGridLayout(self.centralWidget(),1,1,11,6,"FormLayout")
52
53         self.languageChange()
54
55         self.resize(QSize(600,480).expandedTo(self.minimumSizeHint()))
56         self.clearWState(Qt.WState_Polished)
57
58         if self.device_uri and self.printer_name:
59             log.error("You may not specify both a printer (-p) and a device (-d).")
60             self.device_uri, self.printer_name = None, None
61
62         if not self.device_uri and not self.printer_name:
63             probed_devices = device.probeDevices(bus=bus, filter={'pcard-type': (operator.eq, 1)})
64             cups_printers = cups.getPrinters()
65             log.debug(probed_devices)
66             log.debug(cups_printers)
67             max_deviceid_size, x, devices = 0, 0, {}
68
69             for d in probed_devices:
70                 if d.startswith('hp:'):
71                     printers = []
72                     for p in cups_printers:
73                         if p.device_uri == d:
74                             printers.append(p.name)
75                     devices[x] = (d, printers)
76                     x += 1
77                     max_deviceid_size = max(len(d), max_deviceid_size)
78
79             if x == 0:
80                 from nodevicesform import NoDevicesForm
81                 self.FailureUI(self.__tr("<p><b>No devices found that support photo card access.</b><p>Please make sure your device is properly installed and try again."))
82                 self.init_failed = True
83
84             elif x == 1:
85                 log.info(log.bold("Using device: %s" % devices[0][0]))
86                 self.device_uri = devices[0][0]
87
88             else:
89                 from choosedevicedlg import ChooseDeviceDlg
90                 dlg = ChooseDeviceDlg(devices)
91                 if dlg.exec_loop() == QDialog.Accepted:
92                     self.device_uri = dlg.device_uri
93                 else:
94                     self.init_failed = True
95
96         self.dbus_avail, self.service, session_bus = device.init_dbus()
97
98         self.UnloadView = ScrollUnloadView(self.service,
99             self.centralWidget(), self, "UnloadView")
100
101         self.FormLayout.addWidget(self.UnloadView,0,0)
102
103
104         if not self.init_failed:
105             try:
106                 self.cur_device = device.Device(device_uri=self.device_uri,
107                                                  printer_name=self.printer_name)
108             except Error, e:
109                 log.error("Invalid device URI or printer name.")
110                 self.FailureUI("<b>Invalid device URI or printer name.</b><p>Please check the parameters to hp-print and try again.")
111                 self.init_failed = True
112
113             else:
114                 self.device_uri = self.cur_device.device_uri
115                 user_conf.set('last_used', 'device_uri', self.device_uri)
116
117                 log.debug(self.device_uri)
118
119                 self.statusBar().message(self.device_uri)
120
121
122         QTimer.singleShot(0, self.initialUpdate)
123
124
125     def initialUpdate(self):
126         if self.init_failed:
127             self.close()
128             return
129
130         self.UnloadView.onDeviceChange(self.cur_device)
131
132     def FailureUI(self, error_text):
133         log.error(unicode(error_text).replace("<b>", "").replace("</b>", "").replace("<p>", " "))
134         QMessageBox.critical(self,
135                              self.caption(),
136                              error_text,
137                               QMessageBox.Ok,
138                               QMessageBox.NoButton,
139                               QMessageBox.NoButton)
140
141
142     def languageChange(self):
143         self.setCaption(self.__tr("HP Device Manager - Unload Photo Card"))
144
145     def __tr(self,s,c = None):
146         return qApp.translate("UnloadForm",s,c)
147