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