| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
## Printing troubleshooter |
| 4 |
|
| 5 |
## Copyright (C) 2008, 2009, 2010, 2011 Red Hat, Inc. |
| 6 |
## Authors: |
| 7 |
## Tim Waugh <[email protected]> |
| 8 |
|
| 9 |
## This program is free software; you can redistribute it and/or modify |
| 10 |
## it under the terms of the GNU General Public License as published by |
| 11 |
## the Free Software Foundation; either version 2 of the License, or |
| 12 |
## (at your option) any later version. |
| 13 |
|
| 14 |
## This program is distributed in the hope that it will be useful, |
| 15 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
## GNU General Public License for more details. |
| 18 |
|
| 19 |
## You should have received a copy of the GNU General Public License |
| 20 |
## along with this program; if not, write to the Free Software |
| 21 |
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 22 |
|
| 23 |
from gi.repository import Gtk |
| 24 |
|
| 25 |
import cups |
| 26 |
import ppdcache |
| 27 |
import statereason |
| 28 |
from timedops import TimedOperation |
| 29 |
from .base import * |
| 30 |
from functools import reduce |
| 31 |
class PrinterStateReasons(Question): |
| 32 |
def __init__ (self, troubleshooter): |
| 33 |
Question.__init__ (self, troubleshooter, "Printer state reasons") |
| 34 |
page = self.initial_vbox (_("Status Messages"), |
| 35 |
_("There are status messages associated with " |
| 36 |
"this queue.")) |
| 37 |
self.label = Gtk.Label () |
| 38 |
self.label.set_alignment (0, 0) |
| 39 |
self.label.set_line_wrap (True) |
| 40 |
page.pack_start (self.label, False, False, 0) |
| 41 |
|
| 42 |
troubleshooter.new_page (page, self) |
| 43 |
|
| 44 |
def display (self): |
| 45 |
troubleshooter = self.troubleshooter |
| 46 |
try: |
| 47 |
queue = troubleshooter.answers['cups_queue'] |
| 48 |
except KeyError: |
| 49 |
return False |
| 50 |
|
| 51 |
parent = self.troubleshooter.get_window () |
| 52 |
cups.setServer ('') |
| 53 |
self.op = TimedOperation (cups.Connection, parent=parent) |
| 54 |
c = self.op.run () |
| 55 |
self.op = TimedOperation (c.getPrinterAttributes, |
| 56 |
args=(queue,), |
| 57 |
parent=parent) |
| 58 |
dict = self.op.run () |
| 59 |
|
| 60 |
the_ppdcache = ppdcache.PPDCache () |
| 61 |
|
| 62 |
text = '' |
| 63 |
state_message = dict['printer-state-message'] |
| 64 |
if state_message: |
| 65 |
text += _("The printer's state message is: '%s'.") % state_message |
| 66 |
text += '\n\n' |
| 67 |
|
| 68 |
state_reasons_list = dict['printer-state-reasons'] |
| 69 |
if type (state_reasons_list) == str: |
| 70 |
state_reasons_list = [state_reasons_list] |
| 71 |
|
| 72 |
self.state_message = state_message |
| 73 |
self.state_reasons = state_reasons_list |
| 74 |
|
| 75 |
human_readable_errors = [] |
| 76 |
human_readable_warnings = [] |
| 77 |
for reason in state_reasons_list: |
| 78 |
if reason == "none": |
| 79 |
continue |
| 80 |
|
| 81 |
r = statereason.StateReason (queue, reason, the_ppdcache) |
| 82 |
(title, description) = r.get_description () |
| 83 |
level = r.get_level () |
| 84 |
if level == statereason.StateReason.ERROR: |
| 85 |
human_readable_errors.append (description) |
| 86 |
elif level == statereason.StateReason.WARNING: |
| 87 |
human_readable_warnings.append (description) |
| 88 |
|
| 89 |
if human_readable_errors: |
| 90 |
text += _("Errors are listed below:") + '\n' |
| 91 |
text += reduce (lambda x, y: x + "\n" + y, human_readable_errors) |
| 92 |
text += '\n\n' |
| 93 |
|
| 94 |
if human_readable_warnings: |
| 95 |
text += _("Warnings are listed below:") + '\n' |
| 96 |
text += reduce (lambda x, y: x + "\n" + y, human_readable_warnings) |
| 97 |
|
| 98 |
self.label.set_text (text) |
| 99 |
if (state_message == '' and |
| 100 |
len (human_readable_errors) == 0 and |
| 101 |
len (human_readable_warnings) == 0): |
| 102 |
return False |
| 103 |
|
| 104 |
# If this screen has been show before, don't show it again if |
| 105 |
# nothing changed. |
| 106 |
if 'printer-state-message' in troubleshooter.answers: |
| 107 |
if (troubleshooter.answers['printer-state-message'] == |
| 108 |
self.state_message and |
| 109 |
troubleshooter.answers['printer-state-reasons'] == |
| 110 |
self.state_reasons): |
| 111 |
return False |
| 112 |
|
| 113 |
return True |
| 114 |
|
| 115 |
def collect_answer (self): |
| 116 |
if not self.displayed: |
| 117 |
return {} |
| 118 |
|
| 119 |
return { 'printer-state-message': self.state_message, |
| 120 |
'printer-state-reasons': self.state_reasons } |
| 121 |
|
| 122 |
def cancel_operation (self): |
| 123 |
self.op.cancel () |