Back to index

system-config-printer  1.3.9+20120706
PrinterStateReasons.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 ## Printing troubleshooter
00004 
00005 ## Copyright (C) 2008, 2009, 2010, 2011 Red Hat, Inc.
00006 ## Authors:
00007 ##  Tim Waugh <twaugh@redhat.com>
00008 
00009 ## This program is free software; you can redistribute it and/or modify
00010 ## it under the terms of the GNU General Public License as published by
00011 ## the Free Software Foundation; either version 2 of the License, or
00012 ## (at your option) any later version.
00013 
00014 ## This program is distributed in the hope that it will be useful,
00015 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 ## GNU General Public License for more details.
00018 
00019 ## You should have received a copy of the GNU General Public License
00020 ## along with this program; if not, write to the Free Software
00021 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00022 
00023 import cups
00024 import ppdcache
00025 import statereason
00026 from timedops import TimedOperation
00027 from base import *
00028 class PrinterStateReasons(Question):
00029     def __init__ (self, troubleshooter):
00030         Question.__init__ (self, troubleshooter, "Printer state reasons")
00031         page = self.initial_vbox (_("Status Messages"),
00032                                   _("There are status messages associated with "
00033                                     "this queue."))
00034         self.label = gtk.Label ()
00035         self.label.set_alignment (0, 0)
00036         self.label.set_line_wrap (True)
00037         page.pack_start (self.label, False, False, 0)
00038 
00039         troubleshooter.new_page (page, self)
00040 
00041     def display (self):
00042         troubleshooter = self.troubleshooter
00043         try:
00044             queue = troubleshooter.answers['cups_queue']
00045         except KeyError:
00046             return False
00047 
00048         parent = self.troubleshooter.get_window ()
00049         cups.setServer ('')
00050         self.op = TimedOperation (cups.Connection, parent=parent)
00051         c = self.op.run ()
00052         self.op = TimedOperation (c.getPrinterAttributes,
00053                                   args=(queue,),
00054                                   parent=parent)
00055         dict = self.op.run ()
00056 
00057         the_ppdcache = ppdcache.PPDCache ()
00058 
00059         text = ''
00060         state_message = dict['printer-state-message']
00061         if state_message:
00062             text += _("The printer's state message is: '%s'.") % state_message
00063             text += '\n\n'
00064 
00065         state_reasons_list = dict['printer-state-reasons']
00066         if type (state_reasons_list) == unicode:
00067             state_reasons_list = [state_reasons_list]
00068 
00069         self.state_message = state_message
00070         self.state_reasons = state_reasons_list
00071 
00072         human_readable_errors = []
00073         human_readable_warnings = []
00074         for reason in state_reasons_list:
00075             if reason == "none":
00076                 continue
00077 
00078             r = statereason.StateReason (queue, reason, the_ppdcache)
00079             (title, description) = r.get_description ()
00080             level = r.get_level ()
00081             if level == statereason.StateReason.ERROR:
00082                 human_readable_errors.append (description)
00083             elif level == statereason.StateReason.WARNING:
00084                 human_readable_warnings.append (description)
00085 
00086         if human_readable_errors:
00087             text += _("Errors are listed below:") + '\n'
00088             text += reduce (lambda x, y: x + "\n" + y, human_readable_errors)
00089             text += '\n\n'
00090 
00091         if human_readable_warnings:
00092             text += _("Warnings are listed below:") + '\n'
00093             text += reduce (lambda x, y: x + "\n" + y, human_readable_warnings)
00094 
00095         self.label.set_text (text)
00096         if (state_message == '' and
00097             len (human_readable_errors) == 0 and
00098             len (human_readable_warnings) == 0):
00099             return False
00100 
00101         # If this screen has been show before, don't show it again if
00102         # nothing changed.
00103         if troubleshooter.answers.has_key ('printer-state-message'):
00104             if (troubleshooter.answers['printer-state-message'] ==
00105                 self.state_message and
00106                 troubleshooter.answers['printer-state-reasons'] ==
00107                 self.state_reasons):
00108                 return False
00109 
00110         return True
00111 
00112     def collect_answer (self):
00113         if not self.displayed:
00114             return {}
00115 
00116         return { 'printer-state-message': self.state_message,
00117                  'printer-state-reasons': self.state_reasons }
00118 
00119     def cancel_operation (self):
00120         self.op.cancel ()