Back to index

system-config-printer  1.3.9+20120706
statereason.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 ## Copyright (C) 2007, 2008, 2009, 2010 Red Hat, Inc.
00004 ## Authors:
00005 ##  Tim Waugh <twaugh@redhat.com>
00006 ##  Jiri Popelka <jpopelka@redhat.com>
00007 
00008 ## This program is free software; you can redistribute it and/or modify
00009 ## it under the terms of the GNU General Public License as published by
00010 ## the Free Software Foundation; either version 2 of the License, or
00011 ## (at your option) any later version.
00012 
00013 ## This program is distributed in the hope that it will be useful,
00014 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 ## GNU General Public License for more details.
00017 
00018 ## You should have received a copy of the GNU General Public License
00019 ## along with this program; if not, write to the Free Software
00020 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00021 
00022 import cups
00023 import os
00024 from gettext import gettext as _
00025 
00026 class StateReason:
00027     REPORT=1
00028     WARNING=2
00029     ERROR=3
00030 
00031     LEVEL_ICON={
00032         REPORT: "dialog-info",
00033         WARNING: "dialog-warning",
00034         ERROR: "dialog-error"
00035         }
00036 
00037     def __init__(self, printer, reason, ppdcache=None):
00038         self.printer = printer
00039         self.reason = reason
00040         self.level = None
00041         self.canonical_reason = None
00042         self._ppd = None
00043         if ppdcache:
00044             ppdcache.fetch_ppd (printer, self._got_ppd)
00045 
00046     def _got_ppd (self, name, result, exc):
00047         self._ppd = result
00048 
00049     def get_printer (self):
00050         return self.printer
00051 
00052     def get_level (self):
00053         if self.level != None:
00054             return self.level
00055 
00056         if (self.reason.endswith ("-report") or
00057             self.reason == "connecting-to-device"):
00058             self.level = self.REPORT
00059         elif self.reason.endswith ("-warning"):
00060             self.level = self.WARNING
00061         else:
00062             self.level = self.ERROR
00063 
00064         return self.level
00065 
00066     def get_reason (self):
00067         if self.canonical_reason:
00068             return self.canonical_reason
00069 
00070         level = self.get_level ()
00071         reason = self.reason
00072         if level == self.WARNING and reason.endswith ("-warning"):
00073             reason = reason[:-8]
00074         elif level == self.ERROR and reason.endswith ("-error"):
00075             reason = reason[:-6]
00076         self.canonical_reason = reason
00077         return self.canonical_reason
00078 
00079     def __repr__ (self):
00080         self.get_level()
00081         if self.level == self.REPORT:
00082             level = "REPORT"
00083         elif self.level == self.WARNING:
00084             level = "WARNING"
00085         else:
00086             level = "ERROR"
00087 
00088         return "<statereason.StateReason (%s,%s,%s)>" % (level,
00089                                                          self.get_printer (),
00090                                                          self.get_reason ())
00091 
00092     def get_description (self):
00093         messages = {
00094             'toner-low': (_("Toner low"),
00095                           _("Printer '%s' is low on toner.")),
00096             'toner-empty': (_("Toner empty"),
00097                             _("Printer '%s' has no toner left.")),
00098             'cover-open': (_("Cover open"),
00099                            _("The cover is open on printer '%s'.")),
00100             'door-open': (_("Door open"),
00101                           _("The door is open on printer '%s'.")),
00102             'media-low': (_("Paper low"),
00103                           _("Printer '%s' is low on paper.")),
00104             'media-empty': (_("Out of paper"),
00105                             _("Printer '%s' is out of paper.")),
00106             'marker-supply-low': (_("Ink low"),
00107                                   _("Printer '%s' is low on ink.")),
00108             'marker-supply-empty': (_("Ink empty"),
00109                                     _("Printer '%s' has no ink left.")),
00110             'offline': (_("Printer off-line"),
00111                         _("Printer '%s' is currently off-line.")),
00112             'connecting-to-device': (_("Not connected?"),
00113                                      _("Printer '%s' may not be connected.")),
00114             'other': (_("Printer error"),
00115                       _("There is a problem on printer '%s'.")),
00116 
00117             'cups-missing-filter': (_("Printer configuration error"),
00118                                     _("There is a missing print filter for "
00119                                       "printer '%s'.")),
00120             }
00121         try:
00122             (title, text) = messages[self.get_reason ()]
00123             try:
00124                 text = text % self.get_printer ()
00125             except TypeError:
00126                 # Probably an incorrect translation, missing a '%s'.
00127                 pass
00128         except KeyError:
00129             if self.get_level () == self.REPORT:
00130                 title = _("Printer report")
00131             elif self.get_level () == self.WARNING:
00132                 title = _("Printer warning")
00133             elif self.get_level () == self.ERROR:
00134                 title = _("Printer error")
00135 
00136             reason = self.get_reason ()
00137             if self._ppd:
00138                 try:
00139                     schemes = ["text", "http", "help", "file"]
00140                     localized_reason = ""
00141                     for scheme in schemes:
00142                         lreason = self._ppd.localizeIPPReason(self.reason,
00143                                                               scheme)
00144                         if lreason != None:
00145                             localized_reason = localized_reason + lreason + ", "
00146                     if localized_reason != "":
00147                         reason = localized_reason[:-2]
00148                 except RuntimeError:
00149                     pass
00150 
00151             text = _("Printer '%s': '%s'.") % (self.get_printer (), reason)
00152         return (title, text)
00153 
00154     def get_tuple (self):
00155         return (self.get_level (), self.get_printer (), self.get_reason ())
00156 
00157     def __cmp__(self, other):
00158         if other == None:
00159             return 1
00160         if other.get_level () != self.get_level ():
00161             return cmp (self.get_level (), other.get_level ())
00162         if other.get_printer () != self.get_printer ():
00163             return cmp (other.get_printer (), self.get_printer ())
00164         return cmp (other.get_reason (), self.get_reason ())