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# Authors: Don Welch
20#
21
22# StdLib
23import operator
24import string
25
26# Local
27from base.g import *
28from base import device, pml
29from prnt import cups
30from base.codes import *
31from .ui_utils import *
32
33# Qt
34from PyQt4.QtCore import *
35from PyQt4.QtGui import *
36
37# Ui
38from .devicesetupdialog_base import Ui_Dialog
39
40TAB_POWER_SETTINGS = 0
41
42class DeviceSetupDialog(QDialog, Ui_Dialog):
43    def __init__(self, parent, device_uri):
44        QDialog.__init__(self, parent)
45        self.setupUi(self)
46        self.device_uri = device_uri
47        self.mq = {}
48        self.dev = None
49        self.initUi()
50
51        QTimer.singleShot(0, self.updateUi)
52
53
54    def initUi(self):
55        # connect signals/slots
56        self.connect(self.CancelButton, SIGNAL("clicked()"), self.CancelButton_clicked)
57        #self.connect(self.ApplyButton, SIGNAL("clicked()"), self.ApplyButton_clicked)
58        self.connect(self.DeviceComboBox, SIGNAL("DeviceUriComboBox_noDevices"), self.DeviceUriComboBox_noDevices)
59        self.connect(self.DeviceComboBox, SIGNAL("DeviceUriComboBox_currentChanged"),
60                    self.DeviceUriComboBox_currentChanged)
61
62        self.DeviceComboBox.setFilter({'power-settings': (operator.gt, 0)})
63
64        # Application icon
65        self.setWindowIcon(QIcon(load_pixmap('hp_logo', '128x128')))
66
67        if self.device_uri:
68            self.DeviceComboBox.setInitialDevice(self.device_uri)
69
70        self.DurationComboBox.addItem(self.__tr("15 minutes"), 15)
71        self.DurationComboBox.addItem(self.__tr("30 minutes"), 30)
72        self.DurationComboBox.addItem(self.__tr("45 minutes"), 45)
73        self.DurationComboBox.addItem(self.__tr("1 hour"), 60)
74        self.DurationComboBox.addItem(self.__tr("2 hours"), 120)
75        self.DurationComboBox.addItem(self.__tr("3 hours"), 180)
76
77        self.connect(self.DurationComboBox, SIGNAL("activated(int)"), self.DurationComboBox_activated)
78
79        self.connect(self.OnRadioButton, SIGNAL("toggled(bool)"), self.OnRadioButton_toggled)
80
81
82    def OnRadioButton_toggled(self, b):
83        i = self.DurationComboBox.currentIndex()
84        if i == -1:
85            return
86        v, ok = value_int(self.DurationComboBox.itemData(i))
87        if not ok:
88            return
89
90        if self.power_settings == POWER_SETTINGS_EPML:
91            if b:
92                self.setPowerSettingsEPML('999')
93            else:
94                self.setPowerSettingsEPML(string.zfill(v, 3))
95
96        elif self.power_settings == POWER_SETTINGS_PML:
97            if b:
98                self.setPowerSettingsPML(pml.OID_POWER_SETTINGS_NEVER)
99            else:
100                self.setPowerSettingsPML(self.getPMLSettingsValue(v))
101
102
103
104    def updateUi(self):
105        self.DeviceComboBox.updateUi()
106
107
108    def updatePowerSettingsUi(self):
109        pass
110
111
112    def DeviceUriComboBox_currentChanged(self, device_uri):
113        beginWaitCursor()
114        try:
115            self.device_uri = device_uri
116
117            if self.dev is not None:
118                self.dev.close()
119
120            self.dev = device.Device(self.device_uri)
121
122            # Update
123            self.mq = device.queryModelByURI(self.device_uri)
124            self.power_settings = self.mq.get('power-settings', POWER_SETTINGS_NONE)
125
126            self.TabWidget.setTabEnabled(TAB_POWER_SETTINGS, self.power_settings != POWER_SETTINGS_NONE)
127
128            if self.power_settings == POWER_SETTINGS_EPML:
129                self.updatePowerSettingsEPML()
130
131            elif self.power_settings == POWER_SETTINGS_PML:
132                self.updatePowerSettingsPML()
133
134        finally:
135            endWaitCursor()
136
137    # DJ 4x0 battery power settings
138
139    # 15min = 015
140    # 30min = 030
141    # 45min = 045
142    # 1hr   = 060
143    # 2hr   = 120
144    # 3hr   = 180
145    # never = 999
146
147    def updatePowerSettingsEPML(self):
148        value = self.getPowerSettingsEPML()
149
150        if value == '999':
151            self.OnRadioButton.setChecked(True)
152            self.OffRadioButton.setChecked(False)
153        else:
154            self.OnRadioButton.setChecked(False)
155            self.OffRadioButton.setChecked(True)
156
157            find = int(value)
158            index = self.DurationComboBox.findData(find)
159
160            if index != -1:
161                self.DurationComboBox.setCurrentIndex(index)
162
163
164    def getPowerSettingsEPML(self):
165        value = self.dev.getDynamicCounter(256, False)
166        log.debug("Current power settings: %s" % value)
167        self.dev.closePrint()
168        return value[6:9]
169
170
171    def setPowerSettingsEPML(self, value):
172        log.debug("Setting power setting to %s" % value)
173        pcl= \
174    """\x1b%%-12345X@PJL ENTER LANGUAGE=PCL3GUI\n\x1bE\x1b%%Pmech.set_battery_autooff %s;\nudw.quit;\x1b*rC\x1bE\x1b%%-12345X""" % value
175        self.dev.printData(pcl, direct=True)
176        self.dev.closePrint()
177
178    # h470
179
180    # PML
181    # OID_POWER_SETTINGS = ('1.1.2.118', TYPE_ENUMERATION)
182    # OID_POWER_SETTINGS_15MIN = 1
183    # OID_POWER_SETTINGS_30MIN = 2
184    # OID_POWER_SETTINGS_45MIN = 3
185    # OID_POWER_SETTINGS_1HR = 4
186    # OID_POWER_SETTINGS_2HR = 5
187    # OID_POWER_SETTINGS_3HR = 6
188    # OID_POWER_SETTINGS_NEVER = 999
189
190    def updatePowerSettingsPML(self):
191        value = self.getPowerSettingsPML()
192        if value == pml.OID_POWER_SETTINGS_NEVER:
193            self.OnRadioButton.setChecked(True)
194            self.OffRadioButton.setChecked(False)
195        else:
196            self.OnRadioButton.setChecked(False)
197            self.OffRadioButton.setChecked(True)
198
199            find = 15
200            if value == pml.OID_POWER_SETTINGS_15MIN:
201                find = 15
202            elif value == pml.OID_POWER_SETTINGS_30MIN:
203                find = 30
204            elif value == pml.OID_POWER_SETTINGS_45MIN:
205                find = 45
206            elif value == pml.OID_POWER_SETTINGS_1HR:
207                find = 60
208            elif value == pml.OID_POWER_SETTINGS_2HR:
209                find = 120
210            elif value == pml.OID_POWER_SETTINGS_3HR:
211                find = 180
212
213            index = self.DurationComboBox.findData(find)
214
215            if index != -1:
216                self.DurationComboBox.setCurrentIndex(index)
217
218
219
220    def getPowerSettingsPML(self):
221        pml_result_code, value = self.dev.getPML(pml.OID_POWER_SETTINGS)
222        self.dev.closePML()
223        log.debug("Current power settings: %s" % value)
224        return value
225
226
227    def setPowerSettingsPML(self, value):
228        log.debug("Setting power setting to %s" % value)
229        pml_result_code = self.dev.setPML(pml.OID_POWER_SETTINGS, value)
230        self.dev.closePML()
231
232    # #####################
233
234
235    def DurationComboBox_activated(self, i):
236        if i == -1:
237            return
238        v, ok = value_int(self.DurationComboBox.itemData(i))
239        if not ok:
240            return
241        if self.power_settings == POWER_SETTINGS_EPML:
242            beginWaitCursor()
243            try:
244                self.setPowerSettingsEPML(string.zfill(v, 3))
245            finally:
246                endWaitCursor()
247
248        elif self.power_settings == POWER_SETTINGS_PML:
249            beginWaitCursor()
250            try:
251                self.setPowerSettingsPML(self.getPMLSettingsValue(v))
252            finally:
253                endWaitCursor()
254
255
256    def getPMLSettingsValue(self, v):
257        x = pml.OID_POWER_SETTINGS_15MIN
258
259        if v == 15:
260            x = pml.OID_POWER_SETTINGS_15MIN
261        elif v == 30:
262            x = pml.OID_POWER_SETTINGS_30MIN
263        elif v == 45:
264            x = pml.OID_POWER_SETTINGS_45MIN
265        elif v == 60:
266            x = pml.OID_POWER_SETTINGS_1HR
267        elif v == 120:
268            x = pml.OID_POWER_SETTINGS_2HR
269        elif v == 180:
270            x = pml.OID_POWER_SETTINGS_3HR
271
272        return x
273
274
275    def DeviceUriComboBox_noDevices(self):
276        FailureUI(self, self.__tr("<b>No devices that support device setup found.</b>"))
277        self.close()
278
279
280    def CancelButton_clicked(self):
281        if self.dev is not None:
282            self.dev.close()
283
284        self.close()
285
286
287#    def ApplyButton_clicked(self):
288#        pass
289
290    #
291    # Misc
292    #
293
294    def __tr(self,s,c = None):
295        return qApp.translate("DeviceSetupDialog",s,c)
296
297
298