| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
## system-config-printer |
| 4 |
|
| 5 |
## Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Red Hat, Inc. |
| 6 |
## Authors: |
| 7 |
## Tim Waugh <[email protected]> |
| 8 |
## Till Kamppeter <[email protected]> |
| 9 |
|
| 10 |
## This program is free software; you can redistribute it and/or modify |
| 11 |
## it under the terms of the GNU General Public License as published by |
| 12 |
## the Free Software Foundation; either version 2 of the License, or |
| 13 |
## (at your option) any later version. |
| 14 |
|
| 15 |
## This program is distributed in the hope that it will be useful, |
| 16 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
## GNU General Public License for more details. |
| 19 |
|
| 20 |
## You should have received a copy of the GNU General Public License |
| 21 |
## along with this program; if not, write to the Free Software |
| 22 |
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 23 |
|
| 24 |
# config is generated from config.py.in by configure |
| 25 |
import config |
| 26 |
|
| 27 |
import cupshelpers |
| 28 |
from debug import * |
| 29 |
|
| 30 |
from gi.repository import GObject |
| 31 |
|
| 32 |
class OpenPrintingRequest(GObject.GObject): |
| 33 |
__gsignals__ = { |
| 34 |
'finished': (GObject.SignalFlags.RUN_LAST, None, |
| 35 |
( |
| 36 |
# list of (printerid,name) tuples |
| 37 |
GObject.TYPE_PYOBJECT, |
| 38 |
|
| 39 |
# dict of printerid: dict of drivername: dict |
| 40 |
# for driver info |
| 41 |
GObject.TYPE_PYOBJECT, |
| 42 |
)), |
| 43 |
|
| 44 |
'error': (GObject.SignalFlags.RUN_LAST, None, |
| 45 |
( |
| 46 |
# HTTP status |
| 47 |
int, |
| 48 |
|
| 49 |
GObject.TYPE_PYOBJECT, |
| 50 |
)), |
| 51 |
} |
| 52 |
|
| 53 |
def __init__ (self, **args): |
| 54 |
GObject.GObject.__init__ (self) |
| 55 |
debugprint ("Starting") |
| 56 |
self.openprinting = cupshelpers.openprinting.OpenPrinting (**args) |
| 57 |
self._handle = None |
| 58 |
debugprint ("+%s" % self) |
| 59 |
|
| 60 |
def __del__ (self): |
| 61 |
debugprint ("-%s" % self) |
| 62 |
|
| 63 |
def cancel (self): |
| 64 |
debugprint ("%s: cancel()" % self) |
| 65 |
if self._handle is not None: |
| 66 |
self.openprinting.cancelOperation (self._handle) |
| 67 |
self._handle = None |
| 68 |
|
| 69 |
debugprint ("%s -> 'error'" % self) |
| 70 |
self.emit ('error', 0, 'canceled') |
| 71 |
|
| 72 |
def searchPrinters (self, searchterm, user_data=None): |
| 73 |
debugprint ("%s: searchPrinters()" % self) |
| 74 |
self._handle = self.openprinting.searchPrinters (searchterm, |
| 75 |
self._printers_got, |
| 76 |
user_data) |
| 77 |
|
| 78 |
def _printers_got (self, status, user_data, printers): |
| 79 |
self._handle = None |
| 80 |
if status != 0: |
| 81 |
debugprint ("%s -> 'error'" % self) |
| 82 |
self.emit ('error', status, printers) |
| 83 |
return |
| 84 |
|
| 85 |
self.downloadable_printers_unchecked = [(x, printers[x]) |
| 86 |
for x in printers] |
| 87 |
self.downloadable_printers = [] |
| 88 |
self.downloadable_drivers = dict() # by printer id of dict |
| 89 |
|
| 90 |
# Kick off a search for drivers for each model. |
| 91 |
if not self._query_next_printer (): |
| 92 |
self._drivers_got () |
| 93 |
|
| 94 |
def _query_next_printer (self): |
| 95 |
""" |
| 96 |
If there are more printers to query, kick off a query and |
| 97 |
return True. |
| 98 |
|
| 99 |
Otherwise return False. |
| 100 |
""" |
| 101 |
|
| 102 |
try: |
| 103 |
user_data = self.downloadable_printers_unchecked.pop () |
| 104 |
(printer_id, printer_name) = user_data |
| 105 |
except IndexError: |
| 106 |
debugprint ("%s: All printer driver queries finished" % self) |
| 107 |
return False |
| 108 |
|
| 109 |
if config.DOWNLOADABLE_ONLYFREE: |
| 110 |
self.openprinting.onlyfree = 1 |
| 111 |
else: |
| 112 |
self.openprinting.onlyfree = 0 |
| 113 |
|
| 114 |
options = dict() |
| 115 |
if config.DOWNLOADABLE_ONLYPPD: |
| 116 |
options['onlyppdfiles'] = '1' |
| 117 |
else: |
| 118 |
options['onlydownload'] = '1' |
| 119 |
options['packagesystem'] = config.packagesystem |
| 120 |
|
| 121 |
debugprint ("%s: Querying drivers for %s" % (self, printer_id)) |
| 122 |
self._handle = self.openprinting.listDrivers (printer_id, |
| 123 |
self._printer_drivers_got, |
| 124 |
user_data=user_data, |
| 125 |
extra_options=options) |
| 126 |
|
| 127 |
return True |
| 128 |
|
| 129 |
def _printer_drivers_got (self, status, user_data, drivers): |
| 130 |
self._handle = None |
| 131 |
if status != 0: |
| 132 |
debugprint ("%s -> 'error'" % self) |
| 133 |
self.emit ('error', status, drivers) |
| 134 |
return |
| 135 |
|
| 136 |
if drivers: |
| 137 |
debugprint ("%s: - drivers found" % self) |
| 138 |
drivers_installable = { } |
| 139 |
for driverkey in drivers.keys (): |
| 140 |
driver = drivers[driverkey] |
| 141 |
if (('ppds' in driver and |
| 142 |
len(driver['ppds']) > 0) or |
| 143 |
(not config.DOWNLOADABLE_ONLYPPD and |
| 144 |
'packages' in driver and |
| 145 |
len(driver['packages']) > 0)): |
| 146 |
# Driver entry with installable resources (Package or |
| 147 |
# PPD), overtake it |
| 148 |
drivers_installable[driverkey] = drivers[driverkey] |
| 149 |
else: |
| 150 |
debugprint ("Not using invalid driver entry %s" % |
| 151 |
driverkey) |
| 152 |
|
| 153 |
if len(drivers_installable) > 0: |
| 154 |
debugprint ("%s: - drivers with installable resources found" % |
| 155 |
self) |
| 156 |
(printer_id, printer_name) = user_data |
| 157 |
self.downloadable_drivers[printer_id] = drivers_installable |
| 158 |
self.downloadable_printers.append (user_data) |
| 159 |
|
| 160 |
if not self._query_next_printer (): |
| 161 |
self._drivers_got () |
| 162 |
|
| 163 |
def _drivers_got (self): |
| 164 |
self._handle = None |
| 165 |
debugprint ("%s -> 'finished'" % self) |
| 166 |
self.emit ('finished', |
| 167 |
self.downloadable_printers, |
| 168 |
self.downloadable_drivers) |
| 169 |
|
| 170 |
if __name__ == '__main__': |
| 171 |
from pprint import pprint |
| 172 |
mainloop = GObject.MainLoop () |
| 173 |
set_debugging (True) |
| 174 |
cupshelpers.set_debugprint_fn (debugprint) |
| 175 |
req = OpenPrintingRequest () |
| 176 |
handlers = [] |
| 177 |
|
| 178 |
def done (obj): |
| 179 |
for handler in handlers: |
| 180 |
obj.disconnect (handler) |
| 181 |
|
| 182 |
GObject.timeout_add_seconds (1, mainloop.quit) |
| 183 |
|
| 184 |
def error (obj, status, err): |
| 185 |
print ("Error: %d" % status) |
| 186 |
print (repr (err)) |
| 187 |
done (obj) |
| 188 |
|
| 189 |
def finished (obj, printers, drivers): |
| 190 |
pprint (printers) |
| 191 |
pprint (drivers) |
| 192 |
done (obj) |
| 193 |
|
| 194 |
handlers.append (req.connect ('error', error)) |
| 195 |
handlers.append (req.connect ('finished', finished)) |
| 196 |
GObject.idle_add (req.searchPrinters, 'ricoh 8000') |
| 197 |
mainloop.run () |
| 198 |
del req |