1# -*- coding: utf-8 -*-
2#
3# (c) Copyright 2001-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: Sarbeswar Meher
20#
21
22# Std Lib
23import operator
24import signal
25
26# Local
27from base.g import *
28from base import device, utils
29from prnt import cups
30from base.codes import *
31from .ui_utils import *
32
33# Qt
34from qt import *
35
36# Ui
37from .firmwaredialog_base import FirmwareDialog_Base
38
39class FirmwareDialog(QDialog, FirmwareDialog_Base):
40    def __init__(self, parent, device_uri):
41        QDialog.__init__(self, parent)
42        self.setupUi(self)
43        self.device_uri = device_uri
44        self.initUi()
45        QTimer.singleShot(0, self.updateUi)
46
47    def initUi(self):
48        self.DeviceComboBox.setFilter({'fw-download' : (operator.gt, 0)})
49        self.DeviceComboBox.setParent(self)
50        self.connect(self.CancelButton, SIGNAL("clicked()"), self.close)
51        self.connect(self.DownloadFirmwareButton, SIGNAL("clicked()"), self.downloadFirmware)
52        signal.signal(signal.SIGINT, signal.SIG_DFL)
53
54        # Application icon
55        self.setIcon(load_pixmap('hp_logo', '128x128'))
56
57        if self.device_uri:
58            self.DeviceComboBox.setInitialDevice(self.device_uri)
59
60
61    def updateUi(self):
62        self.DeviceComboBox.updateUi()
63
64
65    def DeviceUriComboBox_currentChanged(self, device_uri):
66        self.device_uri = device_uri
67        # Update
68
69
70    def DeviceUriComboBox_noDevices(self):
71        self.FailureUI(self.__tr("<b>No devices that support firmware download found.</b>"))
72        self.close()
73
74
75    def downloadFirmware(self):
76        d = None
77
78        try:
79            try:
80                d = device.Device(self.device_uri)
81            except Error:
82                self.CheckDeviceUI()
83                return
84
85            try:
86                d.open()
87            except Error:
88                self.CheckDeviceUI()
89            else:
90                if d.isIdleAndNoError():
91                    ok = d.downloadFirmware()
92
93                else:
94                    self.CheckDeviceUI()
95
96        finally:
97            if d is not None:
98                d.close()
99
100        self.close()
101
102
103
104    def __tr(self,s,c = None):
105        return qApp.translate("FirmwareDialog",s,c)
106
107    def FailureUI(self, error_text):
108
109        QMessageBox.critical(self,
110                self.caption(),
111                error_text,
112                QMessageBox.Ok,
113                QMessageBox.NoButton,
114                QMessageBox.NoButton)
115
116
117    def CheckDeviceUI(self):
118         return self.FailureUI(self.__tr("<b>Unable to communicate with device or device is in an error state.</b><p>Please check device setup and try again.</p>"))
119
120