Tizen 2.1 base
[platform/upstream/hplip.git] / ui / pluginform2.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 from base.g import *
23 from base import device
24 from installer import core_install
25 from installer.core_install import  PLUGIN_INSTALL_ERROR_NONE, \
26                                     PLUGIN_INSTALL_ERROR_PLUGIN_FILE_NOT_FOUND, \
27                                     PLUGIN_INSTALL_ERROR_DIGITAL_SIG_NOT_FOUND, \
28                                     PLUGIN_INSTALL_ERROR_DIGITAL_SIG_BAD, \
29                                     PLUGIN_INSTALL_ERROR_PLUGIN_FILE_CHECKSUM_ERROR, \
30                                     PLUGIN_INSTALL_ERROR_NO_NETWORK, \
31                                     PLUGIN_INSTALL_ERROR_DIRECTORY_ERROR, \
32                                     PLUGIN_INSTALL_ERROR_UNABLE_TO_RECV_KEYS
33
34 from qt import *
35 from pluginform2_base import PluginForm2_base
36
37 class PluginForm2(PluginForm2_base):
38     def __init__(self,parent = None,name = None,modal = 0,fl = 0):
39         PluginForm2_base.__init__(self,parent,name,modal,fl)
40         self.path = None
41         self.version = prop.installed_version
42         self.bg = self.pathLineEdit.paletteBackgroundColor()
43
44         self.titleTextLabel.setFont(QFont('Helvetica', 16))
45
46         self.sourceGroup.emit(SIGNAL("clicked(int)"), (0,))
47
48     def sourceGroup_clicked(self, item):
49         self.pathLineEdit.setEnabled(item == 1)
50         self.browsePushButton.setEnabled(item == 1)
51
52         if item == 0: # download
53             QToolTip.remove(self.pathLineEdit)
54             self.actionPushButton.setText(self.__tr("Download and Install"))
55             self.actionPushButton.setEnabled(True)
56             self.path = None
57         else: # path
58             self.path = unicode(self.pathLineEdit.text())
59             self.pathLineEdit.emit(SIGNAL("textChanged(const QString&)"), (self.path,))
60
61             if self.path.startswith(u"http://"):
62                 self.actionPushButton.setText(self.__tr("Download and Install"))
63             else:
64                 self.actionPushButton.setText(self.__tr("Copy and Install"))
65
66
67     def browsePushButton_clicked(self):
68         dlg = QFileDialog(user_conf.workingDirectory(), self.__tr("HPLIP %s Plug-in (*.run)" %
69             self.version), None, None, True)
70
71         dlg.setCaption("openfile")
72         dlg.setMode(QFileDialog.ExistingFile)
73         dlg.show()
74
75         if dlg.exec_loop() == QDialog.Accepted:
76             results = dlg.selectedFile()
77             working_directory = unicode(dlg.dir().absPath())
78             log.debug("results: %s" % results)
79             user_conf.setWorkingDirectory(working_directory)
80
81             if results:
82                 self.path = unicode(results)
83                 self.pathLineEdit.setText(self.path)
84
85
86     def pathLineEdit_textChanged(self, path):
87         path, ok = unicode(path), True
88
89         if not path.startswith(u'http://'):
90             self.actionPushButton.setText(self.__tr("Copy and Install"))
91
92             if not path or not os.path.exists(path):
93                 QToolTip.add(self.pathLineEdit, self.__tr('File not found.'))
94                 ok = False
95
96             elif os.path.basename(path) != u'hplip-%s-plugin.run' % self.version:
97                 log.error("Incorrect file: %s (should be: %s)" % (path, 'hplip-%s-plugin.run' % self.version))
98                 QToolTip.add(self.pathLineEdit, self.__tr("Incorrect file. Must be '%1'")\
99                     .arg('hplip-%s-plugin.run' % self.version))
100
101                 ok = False
102
103         else:
104             self.actionPushButton.setText(self.__tr("Download and Install"))
105
106         self.actionPushButton.setEnabled(ok)
107
108         if not ok:
109             self.pathLineEdit.setPaletteBackgroundColor(QColor(0xff, 0x99, 0x99))
110
111         else:
112             QToolTip.remove(self.pathLineEdit)
113             self.pathLineEdit.setPaletteBackgroundColor(self.bg)
114             self.path = path
115
116
117     def actionPushButton_clicked(self):
118         core = core_install.CoreInstall()
119         core.set_plugin_version() #self.version)
120
121         if self.path is None: # download
122             # read plugin.conf (local or on sf.net) to get plugin_path (http://)
123             plugin_conf_url = core.get_plugin_conf_url()
124
125             if plugin_conf_url.startswith('file://'):
126                 pass
127             else:
128                 pass
129
130                 log.info("Checking for network connection...")
131                 ok = core.check_network_connection()
132
133                 if not ok:
134                     log.error("Network connection not detected.")
135                     self.FailureUI(self.__tr("Network connection not detected."))
136                     self.close()
137                     return
138
139
140             log.info("Downloading configuration file from: %s" % plugin_conf_url)
141
142             self.path, size, checksum, timestamp, ok = core.get_plugin_info(plugin_conf_url,
143                 self.plugin_download_callback)
144
145             log.debug("path=%s, size=%d, checksum=%s, timestamp=%f, ok=%s" %
146                     (self.path, size, checksum, timestamp, ok))
147
148             if not self.path.startswith('http://') and not self.path.startswith('file://'):
149                 self.path = 'file://' + self.path
150
151
152         else: # path
153             if not self.path.startswith('http://'):
154                 self.path = 'file://' + self.path
155
156             size, checksum, timestamp = 0, '', 0
157
158
159         if self.path.startswith('file://'):
160             pass
161         else:
162
163             log.info("Checking for network connection...")
164             ok = core.check_network_connection()
165
166             if not ok:
167                 log.error("Network connection not detected.")
168                 self.FailureUI(self.__tr("Network connection not detected."))
169                 self.close()
170                 return
171
172         log.info("Downloading plug-in from: %s" % self.path)
173
174         status, ret = core.download_plugin(self.path, size, checksum, timestamp,
175             self.plugin_download_callback)
176
177         if status != PLUGIN_INSTALL_ERROR_NONE:
178
179             if status == PLUGIN_INSTALL_ERROR_PLUGIN_FILE_NOT_FOUND:
180                 desc = self.__tr("<b>ERROR: Plug-in file not found (server returned 404 or similar error).</b><p>Error code: %1</p>").arg(str(ret))
181
182             elif status == PLUGIN_INSTALL_ERROR_DIGITAL_SIG_NOT_FOUND:
183                 desc = self.__tr("<b>ERROR: Plug-in digital signature file not found (server returned 404 or similar error).</b><p>Error code: %1</p>").arg(str(ret))
184
185             elif status == PLUGIN_INSTALL_ERROR_DIGITAL_SIG_BAD:
186                 desc = self.__tr("<b>ERROR: Plug-in file does not match its digital signature.</b><p>File may have been corrupted or altered.</p><p>Error code: %1</p>").arg(str(ret))
187
188             elif status == PLUGIN_INSTALL_ERROR_PLUGIN_FILE_CHECKSUM_ERROR:
189                 desc = self.__tr("<b>ERROR: Plug-in file does not match its checksum. File may have been corrupted or altered.")
190
191             elif status == PLUGIN_INSTALL_ERROR_NO_NETWORK:
192                 desc = self.__tr("<b>ERROR: Unable to connect to network to download the plug-in.</b><p>Please check your network connection and try again.</p>")
193
194             elif status == PLUGIN_INSTALL_ERROR_DIRECTORY_ERROR:
195                 desc = self.__tr("<b>ERROR: Unable to create the plug-in directory.</b><p>Please check your permissions and try again.</p>")
196
197             elif status == PLUGIN_INSTALL_ERROR_UNABLE_TO_RECV_KEYS:
198                 desc = self.__tr("<b>ERROR: Unable to download the public HPLIP keys from the keyserver.</b><p>Error code: %1</p>").arg(str(ret))
199
200             core.delete_plugin()
201             self.FailureUI(desc)
202             self.close()
203             return
204
205         local_plugin = ret
206
207         if not core.run_plugin(GUI_MODE, self.plugin_install_callback):
208             core.delete_plugin()
209             self.FailureUI(self.__tr("Plug-in install failed."))
210             self.close()
211             return
212
213         cups_devices = device.getSupportedCUPSDevices(['hp']) #, 'hpfax'])
214
215         for dev in cups_devices:
216             mq = device.queryModelByURI(dev)
217
218             if mq.get('fw-download', False):
219
220                 # Download firmware if needed
221                 log.info(log.bold("\nDownloading firmware to device %s..." % dev))
222                 try:
223                     d = device.Device(dev)
224                 except Error:
225                     log.error("Error opening device.")
226                     continue
227
228                 if d.downloadFirmware():
229                     log.info("Firmware download successful.\n")
230
231                 d.close()
232
233
234         core.delete_plugin()
235         self.SuccessUI("Plug-in install successful.")
236         self.close()
237
238
239     def FailureUI(self, error_text):
240         QMessageBox.critical(self,
241             self.caption(),
242             error_text,
243             QMessageBox.Ok,
244             QMessageBox.NoButton,
245             QMessageBox.NoButton)
246
247     def SuccessUI(self, text):
248         QMessageBox.information(self,
249                              self.caption(),
250                              text,
251                               QMessageBox.Ok,
252                               QMessageBox.NoButton,
253                               QMessageBox.NoButton)
254
255
256
257     def plugin_download_callback(self, c, s, t):
258         pass
259
260
261     def plugin_install_callback(self, s):
262         print s
263
264
265     def cancelPushButton_clicked(self):
266         self.close()
267
268
269     def __tr(self,s,c = None):
270         return qApp.translate("PluginForm_base",s,c)