| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
## Copyright (C) 2007, 2008, 2009, 2010, 2012, 2013, 2014 Red Hat, Inc. |
| 4 |
## Authors: |
| 5 |
## Tim Waugh <[email protected]> |
| 6 |
## Jiri Popelka <[email protected]> |
| 7 |
|
| 8 |
## This program is free software; you can redistribute it and/or modify |
| 9 |
## it under the terms of the GNU General Public License as published by |
| 10 |
## the Free Software Foundation; either version 2 of the License, or |
| 11 |
## (at your option) any later version. |
| 12 |
|
| 13 |
## This program is distributed in the hope that it will be useful, |
| 14 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
## GNU General Public License for more details. |
| 17 |
|
| 18 |
## You should have received a copy of the GNU General Public License |
| 19 |
## along with this program; if not, write to the Free Software |
| 20 |
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 |
|
| 22 |
import cups |
| 23 |
import os |
| 24 |
import config |
| 25 |
import gettext |
| 26 |
gettext.install(domain=config.PACKAGE, localedir=config.localedir) |
| 27 |
|
| 28 |
class StateReason: |
| 29 |
REPORT=1 |
| 30 |
WARNING=2 |
| 31 |
ERROR=3 |
| 32 |
|
| 33 |
LEVEL_ICON={ |
| 34 |
REPORT: "dialog-information", |
| 35 |
WARNING: "dialog-warning", |
| 36 |
ERROR: "dialog-error" |
| 37 |
} |
| 38 |
|
| 39 |
def __init__(self, printer, reason, ppdcache=None): |
| 40 |
self.printer = printer |
| 41 |
self.reason = reason |
| 42 |
self.level = None |
| 43 |
self.canonical_reason = None |
| 44 |
self._ppd = None |
| 45 |
if ppdcache: |
| 46 |
ppdcache.fetch_ppd (printer, self._got_ppd) |
| 47 |
|
| 48 |
def _got_ppd (self, name, result, exc): |
| 49 |
self._ppd = result |
| 50 |
|
| 51 |
def get_printer (self): |
| 52 |
return self.printer |
| 53 |
|
| 54 |
def get_level (self): |
| 55 |
if self.level is not None: |
| 56 |
return self.level |
| 57 |
|
| 58 |
if (self.reason.endswith ("-report") or |
| 59 |
self.reason in ["connecting-to-device", |
| 60 |
"cups-ipp-missing-cancel-job", |
| 61 |
"cups-ipp-missing-get-job-attributes", |
| 62 |
"cups-ipp-missing-get-printer-attributes", |
| 63 |
"cups-ipp-missing-job-history", |
| 64 |
"cups-ipp-missing-job-id", |
| 65 |
"cups-ipp-missing-job-state", |
| 66 |
"cups-ipp-missing-operations-supported", |
| 67 |
"cups-ipp-missing-print-job", |
| 68 |
"cups-ipp-missing-printer-is-accepting-jobs", |
| 69 |
"cups-ipp-missing-printer-state-reasons", |
| 70 |
"cups-ipp-missing-send-document", |
| 71 |
"cups-ipp-missing-validate-job", |
| 72 |
"cups-ipp-wrong-http-version"]): |
| 73 |
self.level = self.REPORT |
| 74 |
elif self.reason.endswith ("-warning"): |
| 75 |
self.level = self.WARNING |
| 76 |
else: |
| 77 |
self.level = self.ERROR |
| 78 |
|
| 79 |
return self.level |
| 80 |
|
| 81 |
def get_reason (self): |
| 82 |
if self.canonical_reason: |
| 83 |
return self.canonical_reason |
| 84 |
|
| 85 |
level = self.get_level () |
| 86 |
reason = self.reason |
| 87 |
if level == self.WARNING and reason.endswith ("-warning"): |
| 88 |
reason = reason[:-8] |
| 89 |
elif level == self.ERROR and reason.endswith ("-error"): |
| 90 |
reason = reason[:-6] |
| 91 |
self.canonical_reason = reason |
| 92 |
return self.canonical_reason |
| 93 |
|
| 94 |
def __repr__ (self): |
| 95 |
self.get_level() |
| 96 |
if self.level == self.REPORT: |
| 97 |
level = "REPORT" |
| 98 |
elif self.level == self.WARNING: |
| 99 |
level = "WARNING" |
| 100 |
else: |
| 101 |
level = "ERROR" |
| 102 |
|
| 103 |
return "<statereason.StateReason (%s,%s,%s)>" % (level, |
| 104 |
self.get_printer (), |
| 105 |
self.get_reason ()) |
| 106 |
|
| 107 |
def get_description (self): |
| 108 |
messages = { |
| 109 |
'toner-low': (_("Toner low"), |
| 110 |
_("Printer '%s' is low on toner.")), |
| 111 |
'toner-empty': (_("Toner empty"), |
| 112 |
_("Printer '%s' has no toner left.")), |
| 113 |
'cover-open': (_("Cover open"), |
| 114 |
_("The cover is open on printer '%s'.")), |
| 115 |
'door-open': (_("Door open"), |
| 116 |
_("The door is open on printer '%s'.")), |
| 117 |
'media-low': (_("Paper low"), |
| 118 |
_("Printer '%s' is low on paper.")), |
| 119 |
'media-empty': (_("Out of paper"), |
| 120 |
_("Printer '%s' is out of paper.")), |
| 121 |
'marker-supply-low': (_("Ink low"), |
| 122 |
_("Printer '%s' is low on ink.")), |
| 123 |
'marker-supply-empty': (_("Ink empty"), |
| 124 |
_("Printer '%s' has no ink left.")), |
| 125 |
'offline': (_("Printer off-line"), |
| 126 |
_("Printer '%s' is currently off-line.")), |
| 127 |
'connecting-to-device': (_("Not connected?"), |
| 128 |
_("Printer '%s' may not be connected.")), |
| 129 |
'other': (_("Printer error"), |
| 130 |
_("There is a problem on printer '%s'.")), |
| 131 |
|
| 132 |
'cups-missing-filter': (_("Printer configuration error"), |
| 133 |
_("There is a missing print filter for " |
| 134 |
"printer '%s'.")), |
| 135 |
} |
| 136 |
try: |
| 137 |
(title, text) = messages[self.get_reason ()] |
| 138 |
try: |
| 139 |
text = text % self.get_printer () |
| 140 |
except TypeError: |
| 141 |
# Probably an incorrect translation, missing a '%s'. |
| 142 |
pass |
| 143 |
except KeyError: |
| 144 |
if self.get_level () == self.REPORT: |
| 145 |
title = _("Printer report") |
| 146 |
elif self.get_level () == self.WARNING: |
| 147 |
title = _("Printer warning") |
| 148 |
elif self.get_level () == self.ERROR: |
| 149 |
title = _("Printer error") |
| 150 |
|
| 151 |
reason = self.get_reason () |
| 152 |
if self._ppd: |
| 153 |
try: |
| 154 |
schemes = ["text", "http", "help", "file"] |
| 155 |
localized_reason = "" |
| 156 |
for scheme in schemes: |
| 157 |
lreason = self._ppd.localizeIPPReason(self.reason, |
| 158 |
scheme) |
| 159 |
if lreason is not None: |
| 160 |
localized_reason = localized_reason + lreason + ", " |
| 161 |
if localized_reason != "": |
| 162 |
reason = localized_reason[:-2] |
| 163 |
except RuntimeError: |
| 164 |
pass |
| 165 |
|
| 166 |
text = (_("Printer '%s': '%s'.") % (self.get_printer (), reason)) |
| 167 |
return (title, text) |
| 168 |
|
| 169 |
def get_tuple (self): |
| 170 |
return (self.get_level (), self.get_printer (), self.get_reason ()) |
| 171 |
|
| 172 |
def __eq__(self, other): |
| 173 |
if type (other) != type (self): |
| 174 |
return False |
| 175 |
return self.get_level () == other.get_level () |
| 176 |
|
| 177 |
def __lt__(self, other): |
| 178 |
if type (other) != type (self): |
| 179 |
return False |
| 180 |
return self.get_level () < other.get_level () |