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 os.path
24
25# Local
26from base.g import *
27from base import device
28from prnt import cups
29from base.codes import *
30from base.sixext import  to_unicode
31from .ui_utils import *
32
33# Qt
34from PyQt5.QtCore import *
35from PyQt5.QtGui import *
36
37# Ui
38from .infodialog_base import Ui_Dialog
39from .deviceuricombobox import DEVICEURICOMBOBOX_TYPE_PRINTER_AND_FAX
40
41
42class InfoDialog(QDialog, Ui_Dialog):
43    def __init__(self, parent, device_uri):
44        QDialog.__init__(self, parent)
45        self.device_uri = device_uri
46        #self.tabs = []
47        self.setupUi(self)
48        self.initUi()
49
50        QTimer.singleShot(0, self.updateUi)
51
52
53    def initUi(self):
54        # connect signals/slots
55        self.CancelButton.clicked.connect(self.CancelButton_clicked)
56        self.DeviceComboBox.DeviceUriComboBox_noDevices.connect(self.DeviceUriComboBox_noDevices)
57        self.DeviceComboBox.DeviceUriComboBox_currentChanged.connect(self.DeviceUriComboBox_currentChanged)
58
59        # Application icon
60        self.setWindowIcon(QIcon(load_pixmap('hp_logo', '128x128')))
61
62        if self.device_uri:
63            self.DeviceComboBox.setInitialDevice(self.device_uri)
64
65        self.DeviceComboBox.setType(DEVICEURICOMBOBOX_TYPE_PRINTER_AND_FAX)
66
67        self.headers = [self.__tr("Key"), self.__tr("Value")]
68        self.history_headers = [self.__tr("Date/Time"), None,
69                                self.__tr("Event Code"), self.__tr("Description"),
70                                self.__tr("User"), self.__tr("CUPS Job ID"),
71                                self.__tr("Doc. Title")]
72
73
74    def updateUi(self):
75        self.DeviceComboBox.updateUi()
76        #self.updateInfoTable()
77
78
79    def updateInfoTable(self):
80        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
81        self.DynamicTableWidget.clear()
82        self.DynamicTableWidget.setRowCount(0)
83        self.DynamicTableWidget.setColumnCount(0)
84        flags = Qt.ItemIsSelectable | Qt.ItemIsEnabled
85
86        while self.TabWidget.count() > 3:
87            self.TabWidget.removeTab(3)
88
89        self.DynamicTableWidget.clear()
90        self.DynamicTableWidget.setRowCount(0)
91        self.DynamicTableWidget.setColumnCount(len(self.headers))
92        self.DynamicTableWidget.setHorizontalHeaderLabels(self.headers)
93
94        #
95        # Static Data
96        #
97
98        try:
99            d = device.Device(self.device_uri, None)
100        except Error:
101            QApplication.restoreOverrideCursor()
102            FailureUI(self, self.__tr("<b>Unable to open device %s.</b>"%(self.device_uri)))
103            #self.close()
104            return
105
106        self.StaticTableWidget.clear()
107
108        self.StaticTableWidget.setColumnCount(len(self.headers))
109        self.StaticTableWidget.setHorizontalHeaderLabels(self.headers)
110
111        mq_keys = list(d.mq.keys())
112        mq_keys.sort()
113
114        self.StaticTableWidget.setRowCount(len(mq_keys))
115
116        for row, key in enumerate(mq_keys):
117            i = QTableWidgetItem(str(key))
118            i.setFlags(flags)
119            self.StaticTableWidget.setItem(row, 0, i)
120
121            i = QTableWidgetItem(str(d.mq[key]))
122            i.setFlags(flags)
123            self.StaticTableWidget.setItem(row, 1, i)
124
125        self.StaticTableWidget.resizeColumnToContents(0)
126        self.StaticTableWidget.resizeColumnToContents(1)
127        self.StaticTableWidget.setSortingEnabled(True)
128        self.StaticTableWidget.sortItems(0)
129
130        #
131        # Dynamic Data
132        #
133
134        try:
135            try:
136                d.open()
137                d.queryDevice()
138            except Error as e:
139                QApplication.restoreOverrideCursor()
140                FailureUI(self, self.__tr("<b>Unable to open device %s.</b>"%(self.device_uri)))
141                #self.close()
142                return
143
144            dq_keys = list(d.dq.keys())
145            dq_keys.sort()
146
147            self.DynamicTableWidget.setRowCount(len(dq_keys))
148
149            for row, key in enumerate(dq_keys):
150                i = QTableWidgetItem(str(key))
151                i.setFlags(flags)
152                self.DynamicTableWidget.setItem(row, 0, i)
153
154                i = QTableWidgetItem(str(d.dq[key]))
155                i.setFlags(flags)
156                self.DynamicTableWidget.setItem(row, 1, i)
157
158
159            self.DynamicTableWidget.resizeColumnToContents(0)
160            self.DynamicTableWidget.resizeColumnToContents(1)
161            self.DynamicTableWidget.setSortingEnabled(True)
162            self.DynamicTableWidget.sortItems(0)
163
164        finally:
165            d.close()
166
167        #
168        # History Table
169        #
170
171        self.HistoryTableWidget.clear()
172        self.HistoryTableWidget.setRowCount(0)
173
174        if d.device_type == DEVICE_TYPE_FAX:
175            self.history_headers[1] = self.__tr("Fax")
176        else:
177            self.history_headers[1] = self.__tr("Printer")
178
179        self.HistoryTableWidget.setColumnCount(len(self.history_headers))
180        self.HistoryTableWidget.setHorizontalHeaderLabels(self.history_headers)
181
182        history = d.queryHistory()
183        history.reverse()
184        self.HistoryTableWidget.setRowCount(len(history))
185
186        for row, h in enumerate(history):
187            dt = QDateTime()
188            dt.setTime_t(int(h.timedate))
189            dt = value_str(dt)
190
191            ess = device.queryString(h.event_code, 0)
192
193            for col, t in enumerate([dt, h.printer_name,
194                           to_unicode(h.event_code), ess,
195                           h.username, to_unicode(h.job_id),
196                           h.title]):
197
198                i = QTableWidgetItem(str(t))
199                i.setFlags(flags)
200                self.HistoryTableWidget.setItem(row, col, i)
201
202        self.HistoryTableWidget.resizeColumnToContents(0)
203        self.HistoryTableWidget.resizeColumnToContents(1)
204        self.HistoryTableWidget.setSortingEnabled(True)
205        self.HistoryTableWidget.sortItems(0)
206
207        #
208        # Printer Data
209        #
210
211        printers = cups.getPrinters()
212
213        for p in printers:
214            if p.device_uri == self.device_uri:
215                Tab = QWidget()
216                Tab.setObjectName(str(p.name))
217
218                GridLayout = QGridLayout(Tab)
219                GridLayout.setObjectName(str("GridLayout-%s" % p.name))
220
221                Table = QTableWidget(Tab)
222                Table.setAlternatingRowColors(True)
223                Table.setSelectionMode(QAbstractItemView.SingleSelection)
224                Table.setSelectionBehavior(QAbstractItemView.SelectRows)
225                Table.setVerticalScrollMode(QAbstractItemView.ScrollPerItem)
226                Table.setHorizontalScrollMode(QAbstractItemView.ScrollPerPixel)
227                Table.setGridStyle(Qt.DotLine)
228                Table.setObjectName(str("Table-%s" % p.name))
229                GridLayout.addWidget(Table, 0, 0, 1, 1)
230                self.TabWidget.addTab(Tab, str(p.name))
231
232                Table.setColumnCount(len(self.headers))
233                Table.setHorizontalHeaderLabels(self.headers)
234
235                cups.resetOptions()
236                cups.openPPD(p.name)
237                current_options = dict(cups.getOptions())
238
239                #current_options['cups_error_log_level'] = cups.getErrorLogLevel()
240
241                try:
242                    f = open(os.path.expanduser('~/.cups/lpoptions'))
243                except IOError as e:
244                    log.debug(str(e))
245                    current_options['lpoptions_file_data'] = str("(%s)"%str(e))
246                else:
247                    text = f.read()
248                    for d in text.splitlines():
249                        if p.name in d:
250                            current_options['lpoptions_file_data'] = d
251                            break
252                    else:
253                        current_options['lpoptions_file_data'] = self.__tr("(no data)")
254
255                keys = list(current_options.keys())
256                keys.sort()
257
258                Table.setRowCount(len(keys))
259
260                for row, key in enumerate(keys):
261                    i = QTableWidgetItem(str(key))
262                    i.setFlags(flags)
263                    Table.setItem(row, 0, i)
264
265                    if key == 'printer-state':
266                        state = int(current_options[key])
267                        if state == cups.IPP_PRINTER_STATE_IDLE:
268                            i = QTableWidgetItem(self.__tr("idle (%s)"%state))
269                        elif state == cups.IPP_PRINTER_STATE_PROCESSING:
270                            i = QTableWidgetItem(self.__tr("busy/printing (%s)"%state))
271                        elif state == cups.IPP_PRINTER_STATE_STOPPED:
272                            i = QTableWidgetItem(self.__tr("stopped (%s)"%state))
273                        else:
274                            i = QTableWidgetItem(str(state))
275                    else:
276                        i = QTableWidgetItem(str(current_options[key]))
277
278                    i.setFlags(flags)
279                    Table.setItem(row, 1, i)
280
281                Table.resizeColumnToContents(0)
282                Table.resizeColumnToContents(1)
283                Table.setSortingEnabled(True)
284                Table.sortItems(0)
285
286        cups.closePPD()
287        self.TabWidget.setCurrentIndex(0)
288        QApplication.restoreOverrideCursor()
289
290
291    def DeviceUriComboBox_currentChanged(self, device_uri):
292        self.device_uri = device_uri
293        self.updateInfoTable()
294
295
296    def DeviceUriComboBox_noDevices(self):
297        FailureUI(self, self.__tr("<b>No devices found.</b>"))
298        self.close()
299
300
301    def CancelButton_clicked(self):
302        self.close()
303
304    #
305    # Misc
306    #
307
308    def __tr(self,s,c = None):
309        return qApp.translate("InfoDialog",s,c)
310
311
312